14

Imagine that you have a file example.nc, that has wind data defined in 90N, 90S, 180E, 180W region. Is there anyway I could in linux, with a simple nc-type command (without extracting the data in matlab/python to rewrite), crop this file to include a smaller region, subset of the above.

For example, 30N, 10S, 60E and 30W.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
jhc
  • 1,739
  • 3
  • 21
  • 46

3 Answers3

18

Yes, using ncks from the NCO package: http://nco.sourceforge.net/nco.html

If you know the indices corresponding to the lat/lon range you want, let's say they are 30-40 in latitude and 25-50 for longitude for example, then you could crop the netCDF file with

ncks -d lat,30,40 -d lon,25,50 example.nc -O cropped_example.nc

make sure you specify the indices with integer values.

Otherwise you can also directly specify the range of the lat and lon values you want, but in this case you must make sure to use decimal points to pass the range as floats.

 ncks -d lat,30.,-10. -d lon,-30.,60. example.nc -O cropped_example.nc
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
N1B4
  • 3,377
  • 1
  • 21
  • 24
  • Just as a heads up: Sometimes you might get an error unless you place a decimal after your dim ranges. So, if you have trouble, try: `ncks -d lat,30.,40. -d lon,25.,50. example.nc -O cropped_example.nc` instead. – brews Aug 11 '15 at 19:01
  • 7
    @brews using a decimal indicates the range of actual lats/lons to extract over, while using integers indicates the range of _indices_ corresponding to the actual lats/lons. Good to clarify! – N1B4 Aug 11 '15 at 20:13
12

nco works fine, but just to list an alternative, one can also do it using cdo (climate data operators), which I find easier to remember. You can specify directly the longitude and latitude values in this way:

cdo sellonlatbox,lon1,lon2,lat1,lat2 infile.nc outfile.nc

where lon1,lon2,lat1,lat2 define the boundaries of the area you require.

Note that longitude can be specified using 0:360 or also -180:180 conventions irrespective of that used in the input file. The output conventions will follow those used in the cdo command. This this command can also be used to convert a file from one format to the other.

For more details on extracting subregions, I have posted this video tutorial on youtube

If you don't have cdo already installed you can get it on Ubuntu with

sudo apt-get install cdo

cdo has many other functions for processing, combining and splitting files and an excellent online documentation. Note that for cdo to work the coordinate variables (lat/lon) need to be defined according to CF conventions, so in that way the nco solution is more robust.

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
  • 2
    +1 for cdo. I've just been experimenting and it has a *lot* of useful features, I put up a couple of examples how to [crop by bbox, z axis, and temporal dimension here](https://medium.com/@danwild.io/crop-a-netcdf-file-by-xy-z-and-time-with-cdo-ee3f1e3a2678) (without needing to know index of course ) – danwild Mar 22 '18 at 22:38
3

If you are on Linux or macOS, you can do this easily using nctoolkit (https://nctoolkit.readthedocs.io/en/latest/) on Python.

import nctoolkit as nc
data = nc.open_data("example.nc")
data.crop(lon = [25, 50], lat = [30, 40])
data.to_nc("output.nc")

Under the hood, nctoolkit uses CDO. So the above is the equivalent of the CDO approach mentioned above:

cdo sellonlatbox,lon1,lon2,lat1,lat2 infile.nc outfile.nc
Robert Wilson
  • 3,192
  • 11
  • 19