Quantcast

help in starting with netCDF

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

help in starting with netCDF

Timmie
Administrator
Hello,
I am finally diving into netCDF.

I need some assistance in defining coordinate variables such as
"humidity:coordinates"

May someone please help me creating a python file with this CDL:

dimensions:
  station = 10 ;  // measurement locations
  pressure = 11 ; // pressure levels
  time = UNLIMITED ;
variables:
  float humidity(time,pressure,station) ;
    humidity:long_name = "specific humidity" ;
    humidity:coordinates = "lat lon" ;
 
 double time(time) ;
    time:long_name = "time of measurement" ;
    time:units = "days since 1970-01-01 00:00:00" ;

        float lon(station) ;
    lon:long_name = "station longitude";
    lon:units = "degrees_east";
  float lat(station) ;
    lat:long_name = "station latitude" ;
    lat:units = "degrees_north" ;
  float pressure(pressure) ;
    pressure:long_name = "pressure" ;
    pressure:units = "hPa" ;

Source: http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/ch05s04.html#id3141969

Here is my start:

from netCDF4 import Dataset

ncfile = Dataset('station_data.nc','w')

ncfile.createDimension('station', 10)
ncfile.createDimension('pressure', 11)
ncfile.createDimension('time',None)

hum = ncfile.createVariable('hum',dtype('float32'),
                            ('lat', 'lon', 'time', 'pressure', 'station'))
hum.long_name = 'specific humidity'

timevar = ncfile.createVariable('time',dtype('float32'), ('time'))
timevar.long_name = 'time of measurement'
timevar.units = 'days since 1970-01-01 00:00:00'

lon = ncfile.createVariable('lon',dtype('float32'), ('station'))
lon.long_name = 'station longitude'
lon.units = 'degrees_east'

lat = ncfile.createVariable('lat',dtype('float32'), ('station'))
lat.long_name = 'station latitude'
lat.units = 'degrees_west'

pressure = ncfile.createVariable('pressure',dtype('float32'), ('pressure'))
pressure.long_name = 'pressure'
pressure.units = 'hPa'

ncfile.close()

I also have the following question:
Is it possible with some python library to convert cdl or ncml into a nc file?

I would like to use this NC file in order to stort 4D data: network of
measurements stations with a corresponding time series for each station.

Thanks a lot in advance,
Timmie

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: help in starting with netCDF

Benjamin Root-2
On Tue, Aug 10, 2010 at 10:41 AM, Timmie <[hidden email]> wrote:
Hello,
I am finally diving into netCDF.

I need some assistance in defining coordinate variables such as
"humidity:coordinates"

May someone please help me creating a python file with this CDL:

dimensions:
 station = 10 ;  // measurement locations
 pressure = 11 ; // pressure levels
 time = UNLIMITED ;
variables:
 float humidity(time,pressure,station) ;
   humidity:long_name = "specific humidity" ;
   humidity:coordinates = "lat lon" ;

 double time(time) ;
   time:long_name = "time of measurement" ;
   time:units = "days since 1970-01-01 00:00:00" ;

       float lon(station) ;
   lon:long_name = "station longitude";
   lon:units = "degrees_east";
 float lat(station) ;
   lat:long_name = "station latitude" ;
   lat:units = "degrees_north" ;
 float pressure(pressure) ;
   pressure:long_name = "pressure" ;
   pressure:units = "hPa" ;

Source: http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/ch05s04.html#id3141969

Here is my start:

from netCDF4 import Dataset

ncfile = Dataset('station_data.nc','w')

ncfile.createDimension('station', 10)
ncfile.createDimension('pressure', 11)
ncfile.createDimension('time',None)

hum = ncfile.createVariable('hum',dtype('float32'),
                           ('lat', 'lon', 'time', 'pressure', 'station'))
hum.long_name = 'specific humidity'

timevar = ncfile.createVariable('time',dtype('float32'), ('time'))
timevar.long_name = 'time of measurement'
timevar.units = 'days since 1970-01-01 00:00:00'

lon = ncfile.createVariable('lon',dtype('float32'), ('station'))
lon.long_name = 'station longitude'
lon.units = 'degrees_east'

lat = ncfile.createVariable('lat',dtype('float32'), ('station'))
lat.long_name = 'station latitude'
lat.units = 'degrees_west'

pressure = ncfile.createVariable('pressure',dtype('float32'), ('pressure'))
pressure.long_name = 'pressure'
pressure.units = 'hPa'

ncfile.close()

I also have the following question:
Is it possible with some python library to convert cdl or ncml into a nc file?

I would like to use this NC file in order to stort 4D data: network of
measurements stations with a corresponding time series for each station.

Thanks a lot in advance,
Timmie


Timmie,

If you are already using scipy, then you can use scipy.io.netcdf to help you (this presumes that the arrays 'humidityData', 'lonData', 'latData', 'timeData', and 'pressureData' exists).

from scipy.io import netcdf

ncfile = netcdf.netcdf_file('station_data.nc','w')

ncfile.createDimension('station', 10)
ncfile.createDimension('pressure', 11)
ncfile.createDimension('time',None)

hum = ncfile.createVariable('humidity', 'f', ('time', 'pressure', 'station'))
hum.long_name = 'specific humidity'
hum.coordinates = 'lat lon'
hum[:] = humidityData

timevar = ncfile.createVariable('time', 'f', ('time',))
timevar.long_name = 'time of measurement'
timevar.units = 'days since 1970-01-01 00:00:00'
timevar[:] = timeData

lon = ncfile.createVariable('lon', 'f', ('station',))
lon.long_name = 'station longitude'
lon.units = 'degrees_east'
lon[:] = lonData
 
lat = ncfile.createVariable('lat', 'f', ('station',))
lat.long_name = 'station latitude'
lat.units = 'degrees_west'
lat[:] = latData

pressure = ncfile.createVariable('pressure', 'f', ('pressure',))
pressure.long_name = 'pressure'
pressure.units = 'hPa'
pressure[:] = pressureData

ncfile.close()



Now, if you want hum.coordinates to be an array, you can do:
hum.coordinates = ['lat', 'lon']

Also, this won't exactly produce the right CDL, as the type for timevar is set as a float, not a double.  I haven't checked, but I think a 'd' is a valid descriptor.

Anyway, to find out the lat and lon for a particular humidity observation (lets say it is the 4th station):

stationHum = ncfile.variables['humidity'][:, :, 3]
stationLat = ncfile.variables['lat'][3]
stationLon = ncfile.variables['lon'][3]

I hope this is helpful!
Ben Root


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Loading...