Is there a way to crop a NETCDF file?

六月ゝ 毕业季﹏ 提交于 2020-01-12 08:14:07

问题


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.


回答1:


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



回答2:


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. 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, so in that way the NCO solution is more robust (see the comments).



来源:https://stackoverflow.com/questions/25731393/is-there-a-way-to-crop-a-netcdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!