[gradsusr] need help packing the netcdf data

Reto Stauffer Reto.Stauffer at student.uibk.ac.at
Sat Jul 14 11:17:15 EDT 2012


Hy Kishore

I was working on that today for about 5 hours using Python.
I can say some words to the python procedure but it is nearly the same  
in all other packages i guess.

# - Open new netCdf file
ncf = pupynere.NetCDFFile(outfile+".nc",'w')

# - Then you have to define the dimensions.
#   If you have e.g. some surface data and only
#   one timestep you need just "longitude" and "latitude"
#   but there is the option to store 3-D or 4-D fields
#   with additional time and vertical coordinate.
#   I am creating all 4 dimensions.
londim  = ncf.createDimension('longitude',longitude.shape[0]);
latdim  = ncf.createDimension('latitude',latitude.shape[0]);
levdim  = ncf.createDimension('levelist',len(unique_levels));
timedim = ncf.createDimension('time',len(unique_times));

# - Then store the dimension variables into the created
#   Dimensions (e.g. level vector or longitude vector)
#   and setting some additional attributes.
# - IMPORTANT there is no default unit for time and
#   for model level. You have to define it (hPa in my case)
# - Longitude vector
nclon  = ncf.createVariable('longitude','f',('longitude',))
setattr(nclon,'long_name','longitude')
setattr(nclon,'units','degrees_east')
# - Latitude vector
nclat  = ncf.createVariable('latitude','f',('latitude',))
setattr(nclat,'long_name','latitude')
setattr(nclat,'units','degrees_north')
# - Level vector
nclev  = ncf.createVariable('levelist','f',('levelist',))
setattr(nclev,'long_name','model_level_number')
setattr(nclev,'units','hPa')
# - Time.
nctime = ncf.createVariable('time','f',('time',))
setattr(nctime,'long_name','time')
# - Setting those variables
print "* Write long, lat, levelist and time to nc file"
nclon[:]  = longitude;
nclat[:]  = latitude;
nclev[:]  = np.array( unique_levels );
nctime[:] = np.array( unique_times );



# - Now all the dimensions are defined and you can
#   create your data variables now. First: define
#   variable with sutable dimension.
#   It is possible to mix fields (i am using a few
#   with long/lat/time only and others with
#   long/lat/level/time. But either all levels are
#   in there or just one. I guess it is not possible
#   to define different numbers of vertical levels
#   for every single variable.

# - As an example: create one variable and
#   store data into it. In this case i am creating
#   a variable with all 4 dimensions. So the "data"
#   variable i store at the end has to have the
#   same dimension
ncvar   =  
ncf.createVariable(str(var),'f',('latitude','longitude','levelist','time'))
setattr(ncvar,'_FillValue',0.)
setattr(ncvar,'missing_value',-999.)
setattr(ncvar,'add_offset','0')
setattr(ncvar,'long_name',str(nc_desc[var]))
setattr(ncvar,'scale_factor',1.)
setattr(ncvar,'units','scaled radiance')
ncvar[:] = np.int32(data)

# - Syncing netcdf file (write)"
ncf.sync();

# - Close netcdf file"
ncf.close();


Probably it is getting a little bit more transparent for you. I am  
using the pupynere package (pure python netcdf reader) but in newer  
scipy versions it is allready included (scipy.netcdf i guess).

As checklist
- define variables (with attributes for time or levels!)
- u dont need a vertical dimension when you only have "surface" fields  
(2-d-fields)
- store dimension values into created dimensions
- create variable (2,3,4-dimensional?
- store data with same size into that
- write all the stuff into the netcdf file

Greets from Austria
Reto


Zitat von Kishore Babu <kishoreragi at gmail.com>:

> Dear GrADS users,
>
> Could anyone tell me how to pack the netcdf data? I do understand
> scale_factor and add_offset , but I don't know how to do with them.
>
> help will greatly save my time to do that. I have already spent a lot of
> time to do that...
>
>
> Thank you in advance,
>
> Kishore
>





More information about the gradsusr mailing list