The script has been updated to:
- include the time variable to facilitate the SOSIE interpolation.
- The order of dimensions are set in a way that ferret would recognize them correctly (time, lat, lon) which gets translated in ferret as (L, J, I).
- NOTE: read_csv function has a flag header, which should be None for my Mac, but 0 for my office computer in order to extract the correct number of data in row (=180). Not sure why, but they behave differently.
- included the loop to save all 12 months in one file.
Reference used to develop the script: http://qiita.com/okadate/items/954574a95545b06ca257
csv2nc.py
import netCDF4
from pylab import *
from pandas import read_csv
ncout = netCDF4.Dataset('test.nc','w')
fmonth = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
for x in range(12):
data=array(read_csv('../lana_dms_src/DMSclim_'+fmonth[x]+'.csv',header=0))
if x == 0:
dim0 = size(data,0)
dim1 = size(data,1)
lats_out = 89.5 - 1.0*arange(dim0,dtype='float32')
lons_out = -179.5 + 1.0*arange(dim1,dtype='float32')
ncout.createDimension('latitude',dim0)
ncout.createDimension('longitude',dim1)
ncout.createDimension('time',None)
lats = ncout.createVariable('latitude',dtype('float32').char,('latitude',))
lons = ncout.createVariable('longitude',dtype('float32').char,('longitude',))
lats.units = 'degrees_north'
lons.units = 'degrees_east'
lats[:] = lats_out
lons[:] = lons_out
time = ncout.createVariable('time', dtype('int32').char, ('time',))
time.long_name = 'Climatological Month'
time.units = 'days since 1968-05-23 00:00:00'
time[:] = 1 + arange(len(fmonth),dtype='float32')
dms = ncout.createVariable('DMS',dtype('float32').char,('time','latitude','longitude'))
dms.units = 'nmol L-1'
data_all = empty((len(fmonth),dim0,dim1))
data_all[x,:,:] = data
dms[:,:,:] = data_all
ncout.close()