From: Ryan M. <rm...@gm...> - 2009-11-11 15:04:00
|
On Wed, Nov 11, 2009 at 7:31 AM, John [H2O] <was...@gm...> wrote: > > Hello, I'm trying to write a function to create a 'vanilla' NetCDF file to > which I can add data. > > So far I have created the following, which is designed to set up a netcdf > file for the addition of global or at least lat/lon datasets. My question > has to do with attribute setting. Is it valid to do what I'm doing below > "nco.attribute = attributevalue" or do you have to use the setattr function > as outlined in some tutorials? > > Thanks! > > def default_netcdf(nco_filename, > lon0=-179.5,lat0=-89.5, > nx=720,ny=360, > dx=0.5,dy=0.5): > """ add default attributes and dimensions to the nc file """ > nco = NetCDFFile(nco_filename,'w') > nco.author = "Some One" > nco.createdate = dt.datetime.now().ctime() > nco.contact = "som...@py..." > nco.Conventions = "CF-1.4" > > nco.createDimension('lon',nx) > nco.createDimension('lat',ny) > nco.createVariable('lat','d',('lat',)) > nco.createVariable('lon','d',('lon',)) > lon = np.arange(lon0,lon0+(nx*dx),dx) > lat = np.arange(lat0,lat0+(ny*dy),dy) > nco.variables['lat'][:] = lat > nco.variables['lon'][:] = lon > nco.createVariable('data','d',('lon','lat')) > > return nco That should work fine for settings attributes. Are you having a problem? If so, can you paste the actual traceback? Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma |