Search code examples
python-3.xbashnetcdfnetcdf4cdo-climate

Change grid size of a netCDF file


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.


Solution

  • 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 2021: new video available...

    To answer the comment below asking about which method to use, for a full guide on these interpolation methods and the issues you have to look out for regarding subsampling when coarse graining data, you can refer to my "regridding and interpolation" video guide on youtube.

    In general if you are interpolating from high resolution to low resolution ("coarse gridding") by more than a factor of 2 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). See my video guide for details.

    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