Change grid size of a netCDF file

折月煮酒 提交于 2021-02-19 03:19:19

问题


Let's assume I have 2 netCDF data files with data for the same region (like South America, Africa, etc) but the different grid sizes as 0.5 degrees x 0.5 degrees and 1.0 degrees x 1.0 degrees in another. I want to increase or decrease its grid size to a different value such as 0.25 x 0.25 or 1.0 x 1.0 so that I can use this easily for raster calculations and comparison, etc.

Is there a method to do this using any bash script, CDO, etc.

A sample data can be downloaded from here. https://www.dropbox.com/sh/0vdfn20p355st3i/AABKYO4do_raGHC34VnsXGPqa?dl

Can different methods be followed for this like bilinear interpolation or cubic interpolation? This is quite easy with ArcGIS and other software but is there a way to do it for a big netCDF file with large datasets. Assume that this is just a subset of the data. What I will be later converting is a whole set of yearly data.

The resulted file should be a .nc file with the changed grid size as defined by the user.


回答1:


You can use cdo to remap grids, e.g. to a regular 1 degree grid you can use:

cdo remapcon,r360x180 input.nc output.nc

As well as conservative first order remapping (remapcon), other options are :

remapbil : bilinear interpolation
remapnn  : nearest neighbour interpolation
remapcon2 : 2nd order conservative remapping

It is also possible to remap one file to the grid used in another if you prefer:

cdo remapcon,my_target_file.nc in.nc out.nc 

EDIT: to answer the comment below. In general if you are interpolating from high resolution to low resolution ("coarse gridding") you don't want to use bilinear interpolation as it will essentially subsample the field. This is especially problematic for non-smooth, highly heterogeneous fields such as precipitation. In those cases I would always suggest to use a conservative method (remapcon or remapcon2). These methods are much slower, so I often use bilinear while testing a script for speed, and then switch to remapcon for the final run.

Another tip for speed is that, if you are performing the same interpolation procedure on many input files with the same resolution, then you can calculate the interpolation weights once using genbil, gencon etc, and then do the remapping function using those in the loop over the file. This is much faster, as the generation of the weights is the slow part of remapcon




回答2:


NCO's ncremap has a one-line solution as well. Consider regriding a.nc to be on the same grid as b.nc. We will name the answer c.nc (this is the regridded a.nc).

ncremap -d b.nc a.nc c.nc

To choose conservative instead of bilinear interpolation (the default), use -a:

ncremap -a conserve -d b.nc a.nc c.nc


来源:https://stackoverflow.com/questions/55746419/change-grid-size-of-a-netcdf-file

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