get_cdo_griddes.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. set -u
  3. # This script generates CDO grid file descriptions.
  4. # These CDO description files are used for horizontal interpolation from/to a given grid.
  5. # The script assumes that the horizontal dimensions are called (y,x) but feel free to edit.
  6. # Basically, from the netCDF gridfile, this scripts creates a dummy netCDF that CDO recognizes, to get the CDO grid description file. We'll do that:
  7. # - longitude variable with netCDF attributes standard_name="longitude", units="degrees East"
  8. # - latitude variable with netCDF attributes standard_name="latitude", units="degrees North"
  9. # - one dummy variable with a netCDF attribute coordinates='<lat> <lon>'
  10. # Then cdo griddes dummy.nc > my_grid.grid will work.
  11. # Input file with lat, lon and depth
  12. input_file="/mfast/pelletie/oras5/grids/mesh_mask.nc"
  13. lat_var="gphit"
  14. lon_var="glamt"
  15. depth_var="gdept_1d"
  16. output_griddes="/mfast/pelletie/oras5/grids/example_mine.grid"
  17. module purge
  18. module load 2018b CDO NCO
  19. pid="$$"
  20. # Just take lat/lon from file
  21. ncks -C -v ${lat_var},${lon_var} ${input_file} -O tmp_pid${pid}.nc
  22. # Edit lat attributes
  23. ncatted -a standard_name,${lat_var},o,c,"latitude" -a units,${lat_var},o,c,"degrees North" tmp_pid${pid}.nc
  24. # Edit lon attributes
  25. ncatted -a standard_name,${lon_var},o,c,"longitude" -a units,${lon_var},o,c,"degrees East" tmp_pid${pid}.nc
  26. # Create some dummy variable. Here, dimension names are assumed to be y/x. Change if needed.
  27. ncap2 -O -s "dummy[\$y,\$x]=1.f;" tmp_pid${pid}.nc tmp2_pid${pid}.nc
  28. # Edit dummy variable attributes to say its coordinates are you lat/lons.
  29. ncatted -a coordinates,dummy,o,c,"${lat_var} ${lon_var}" tmp2_pid${pid}.nc
  30. # Get CDO griddes
  31. cdo griddes tmp2_pid${pid}.nc > ${output_griddes}
  32. rm -f ./*pid${pid}*
  33. # Expert level: if you want to perform conservative interpolation, you'll need grid corners as well.
  34. # For ORCA grids (the NEMO grids), this can be done using F nodes (grid corners), check `get_orca_corners.py`
  35. # Then, you need:
  36. # 1) One additional dimension <corn> with size 4 (the name does not matter) and two additional fields:
  37. # 1a) <lon_corn>(y,x,<corn>): longitudes of grid corners
  38. # 1b) <lat_corn>(y,x,<corn>): latitudes of grid corners
  39. # 2) Add an attributes "bounds" to the lat/lon fields in the dummy file, i.e.:
  40. # ncatted -a standard_name,${lat_var},o,c,"latitude" -a units,${lat_var},o,c,"degrees North" -a bounds,${lat_var},o,c,<lat_corn> tmp_pid${pid}.nc
  41. # ncatted -a standard_name,${lon_var},o,c,"longitude" -a units,${lon_var},o,c,"degrees East" -a bounds,${lon_var},o,c,<lon_corn> tmp_pid${pid}.nc
  42. # Then, create the dummy var the same way as above. The CDO gridfile description should then be much larger (containing corners). And can be used for conservative interpolation.