Script demonstrating several useful cdms2 features
Opened this issue · 4 comments
jypeter commented
@doutriaux1 @dnadeau4 I have a short script below that can probably be quickly turned into a notebook by a notebook expert
It shows several useful features of cdms2, i.e. how to:
- create a regular grid
- get 1D weights
- compute the 2D weights
- create a cdms2 variable with some extra metadata
- save a variable to an nc file
#!/usr/bin/env python
# Generate the weights of a regular/uniform grid, and store the
# variable in a netcdf file
# J-Y Peterschmitt - LSCE - 05/2019
import numpy as np, time, os
import cdms2
# Define and generate the grid. The cell bounds are automatically
# generated and will be automatically used for computing the cell weights
# inc = 0.5
# nb_lat = 360
inc = 1
nb_lat = 180
startLat, nlat, deltaLat, startLon, nlon, deltaLon = (-90 + inc/2., nb_lat, inc,
-180 + inc/2., 2*nb_lat, inc)
uniform_grid = cdms2.createUniformGrid(startLat, nlat, deltaLat, startLon, nlon, deltaLon)
# Check the generated grid
print('Grid information:')
print(uniform_grid)
# Get the grid axes
lat_axis = uniform_grid.getLatitude()
lon_axis = uniform_grid.getLongitude()
print('\nLongitude information:')
print(lon_axis)
print('\nLongitude bounds information:')
print(lon_axis.getBounds()[:3, :])
print(lon_axis.getBounds()[-3:, :])
print('\nLatitude information:')
print(lat_axis)
print('\nLatitude bounds information:')
print(lat_axis.getBounds()[:3, :])
print(lat_axis.getBounds()[-3:, :])
# Get the axes' weights and combine them into a 2D variable
lat_weights, lon_weights = uniform_grid.getWeights()
grid_weights = np.outer(lat_weights, lon_weights)
print('\nCheck that the sum of the generated weights is 1.0:')
print(' lat_weights.sum() =', lat_weights.sum())
print(' lon_weights.sum() =', lon_weights.sum())
print(' grid_weights.sum() =', grid_weights.sum())
# Combine the 2D weights var with the axes to create a variable
weight_var = cdms2.createVariable(grid_weights, axes=[lat_axis,
lon_axis] , id='cell_weight')
# Easily add some extra metadata
weight_var.units = '1'
weight_var.history = 'File generated with cdms2 on ' + time.asctime()
# Save the variable in a NetCDF 3 file
cdms2.useNetcdf3()
nc_out = './weight_file.nc'
f_out = cdms2.open(nc_out, 'w')
f_out.write(weight_var)
f_out.close()
print('\nSuccessfully saved the weights to ' + nc_out)
print('\nChecking the header of the generated file\n')
os.system('ncdump -h '+ nc_out)
# The end
dnadeau4 commented
jypeter commented
Thanks @dnadeau4 @reshel3 ! Il looks pretty good to me, with the following exceptions/improvements:
- I assume this notebook will eventually appear in the Jupyter-notebooks Tutorials page, and we can probably make the title more informative than CDMS Helpful Features if we want the users to find/read it. I'm not too sure about that, if you have to also keep the title short (is there a limit on that?). Maybe something like: Using cdms for NetCDF I/O, regular grid creation and cell weight computation (that's quite a mouthful)?
- In [2]: We still get the bounds' round up error related warning that was mentioned in CDAT/cdms#189. In this particular example, we absolutely need the automatic generation of the bounds, but is there a way to remove this confusing warning, or at least change the Your bounds bounds string to Your last bounds?
- In [5]: I had not realized that the print output would look a bit odd when executed in Python 2. The print commands should probably be written the following way:
print(' lat_weights.sum() = ' + lat_weights.sum())
- after In [11], we should probably use and demonstrate the super useful
.info()
function, with a Check the variable metadata title followed byweight_var.info()
orprint(weight_var.info())
(don't know if you can use aprint
here) line - In [12]: it seems that unfortunately the output of
os.system('ncdump -h '+ nc_out)
does not go to the notebook, so theos.system
line should probably be commented out (but kept in the notebook), followed by a!ncdump nc_out
(don't know if this works), or!ncdump ./weight_file.nc
dnadeau4 commented
I will disable the warning...