31
Programming / Writing two dimensional netCDF file in python-Reg
« on: February 08, 2018, 12:45:42 AM »
Dear all
I tried to write a two dimensional nc file in python. I could able to generate the file but that spatial extent and resolution is not in proper. so it is not showing properly in ArcGIS.
Here is the code
created nc file is attached
any suggestions pls.. Thank you..
I tried to write a two dimensional nc file in python. I could able to generate the file but that spatial extent and resolution is not in proper. so it is not showing properly in ArcGIS.
Here is the code
Code: [Select]
from netCDF4 import *
from numpy import *
from openpyxl import *
import time
#Load the data sheet
wb = load_workbook('D:\\R_Workspace\\UB_Try.xlsx')
ws = wb['UB_details']
#To get the prec variable
ppt = []
for i in range(2,1696):
ppt.append(ws.cell(row=i,column=3).value)
ppt_arr = asarray(ppt)
#Writing nc file
nc_file = Dataset('D:\\R_Workspace\\Test.nc','w',format='NETCDF4')
nc_file.description = 'Example dataset'
nc_file.history = 'Created on ' +time.ctime(time.time())
#Defining dimensions
lon = nc_file.createDimension('lon',57)
lat = nc_file.createDimension('lat',52)
#Creating variables
latitude = nc_file.createVariable('Latitude',float32,('lat',))
latitude.units = 'Degree_North'
longitude = nc_file.createVariable('Longitude',float32,('lon',))
longitude.units = 'Degree_East'
prec = nc_file.createVariable('prec',float32,('lon','lat'),fill_value = -9999.0)
prec.units = 'mm'
#Writing data to variables
lats = arange(16.875,19.425,0.05)
lat_reverse = lats[::-1]
lons = arange(73.325,76.175,0.05)
latitude[:] = lat_reverse
longitude[:] = lons
temp = zeros(52 * 57, dtype=float32)
temp[:ppt_arr.size] = ppt_arr
prec[:] = temp
nc_file.close()
created nc file is attached
any suggestions pls.. Thank you..
