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 = "some.one@...2855..."
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
···
--
View this message in context: http://old.nabble.com/function-to-create-netcdf-file-tp26301238p26301238.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
That should work fine for settings attributes. Are you having a
problem? If so, can you paste the actual traceback?
Ryan
···
On Wed, Nov 11, 2009 at 7:31 AM, John [H2O] <washakie@...287...> 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 = "some.one@...2855..."
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
--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
No problem… just wanted to make sure it would work and is ‘pythonic’.
I guess the biggest concern is that for others using the netcdf file, say from matlab or somewhere else, will be able to access the attributes.
It seemed so easy to do it this way, I was curious why the tutorials suggest using setattr(), and I wonder if perhaps what I’m setting is not ‘global’ or perhaps wouldn’t match some ISO or otherwise NetCDF standard??
Thanks!
···
On Wed, Nov 11, 2009 at 4:03 PM, Ryan May <rmay31@…287…> wrote:
On Wed, Nov 11, 2009 at 7:31 AM, John [H2O] <washakie@…287…> 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 = “some.one@…2855…”
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
–
Configuration
Plone 2.5.3-final,
CMF-1.6.4,
Zope (Zope 2.9.7-final, python 2.4.4, linux2),
Five 1.4.1,
Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat 4.1.1-51)],
PIL 1.1.6
Mailman 2.1.9
Postfix 2.4.5
Procmail v3.22 2001/09/10
</details>
It's actually just a nice part of the python language, setattr(a,
'foo', 'bar') is the same code as a.foo = bar. When working with
NetCDF, the only time I've needed to use setarr (or getattr) is when
the name of the attribute I want isn't a valid python identifier (like
if an attribute has a - in it, e.g. 'unit-type').
Ryan
···
On Wed, Nov 11, 2009 at 9:26 AM, John <washakie@...287...> wrote:
No problem.. just wanted to make sure it would work and is 'pythonic'.
I guess the biggest concern is that for others using the netcdf file, say
from matlab or somewhere else, will be able to access the attributes.
It seemed so easy to do it this way, I was curious why the tutorials suggest
using setattr(), and I wonder if perhaps what I'm setting is not 'global' or
perhaps wouldn't match some ISO or otherwise NetCDF standard??
--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
It’s actually just a nice part of the python language, setattr(a,
‘foo’, ‘bar’) is the same code as a.foo = bar. When working with
NetCDF, the only time I’ve needed to use setarr (or getattr) is when
the name of the attribute I want isn’t a valid python identifier (like
if an attribute has a - in it, e.g. ‘unit-type’).
Slick! Another great reason for python. Thanks for the rapid response.
···