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.
Yes, using ncks
from the NCO
package: http://nco.sourceforge.net/nco.html
You'll need to 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. Then you could crop the netCDF file with
ncks -d lat,30,40 -d lon,25,50 example.nc -O cropped_example.nc
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 and means you can specify directly the longitude and latitude values:
cdo sellonlatbox,lon1,lon2,lat1,lat2 infile.nc outfile.nc
where lon1,lon2,lat1,lat2 define the boundaries of the area you require.
If you don't have it 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 CDF conventions. The ncks approach is much more robust as it cuts for any axis, but you need to work out the index.