CDMS has functions to interpolate gridded data:
The simplest method to regrid a variable from one horizontal, lat/lon grid to another is to use the regrid function defined for variables. This function takes the target grid as an argument, and returns the variable regridded to the target grid:
>>> f = cdms.open('/pcmdi/cdms/exp/cmip2/ccc/perturb.xml')
>>> rlsf = f('rls') # Read the data
>>> g = cdms.open('/pcmdi/cdms/exp/cmip2/mri/perturb.xml')
>>> rlsg = g['rls'] # Get the file variable (no data read)
>>> outgrid = rlsg.getGrid() # Get the target grid
>>> rlsnew = rlsf.regrid(outgrid)
A somewhat more efficient method is to create a regridder function. This has the advantage that the mapping is created only once and can be used for multiple arrays. Also, this method can be used with data in the form of an MA.MaskedArray or Numeric array. The steps in this process are:
The following example illustrates this process. The regridder function is generated at line 9, and the regridding is performed at line 10:
3 from regrid import Regridder
4 f = cdms.open('/pcmdi/cdms/exp/cmip2/ccc/perturb.xml')
7 g = cdms.open('/pcmdi/cdms/exp/cmip2/mri/perturb.xml')
8 outgrid = g['rls'].getGrid()
9 regridfunc = Regridder(ingrid, outgrid)
To regrid a variable which is a function of latitude, longitude, pressure level, and (optionally) time to a new set of pressure levels, use the pressureRegrid function defined for variables. This function takes an axis representing the target set of pressure levels, and returns a new variable d regridded to that dimension.
['level', 'latitude', 'longitude']
To regrid a variable which is a function of latitude, height, and (optionally) time to a new latitude/height cross-section, use the crossSectionRegridder defined for variables. This function takes as arguments the new latitudes and heights, and returns the variable regridded to those axes.
[ 10., 30., 50., 70., 100., 200., 300., 400., 500., 700., 850.,
The regrid module implements the regridding functionality. Although this module is not strictly a part of CDMS, it is designed to work with CDMS objects. The Python command
makes the Regridder class available within a Python program. An instance of Regridder is a function which regrids data from input to output grid.
|
regridFunction = Regridder(inputGrid, outputGrid) Create a regridder function which interpolates a data array from input to output grid. Table 4.2 describes the calling sequence of this function. inputGrid and outputGrid are CDMS grid objects. Note: To set the mask associated with inputGrid or outputGrid, use the grid setMask function. |
A regridder function is an instance of the Regridder class. The function is associated with an input and output grid. Typically its use is straightforward: the function is passed an input array and returns the regridded array. However, when the array has missing data, or the input and/or output grids are masked, the logic becomes more complicated.
Step 1: The regridder function first forms an input mask. This mask is either two-dimensional or `n-dimensional', depending on the rank of the user-supplied mask. If no mask or missing value is specified, the mask is obtained from the data array mask if present.
N-dimensional case: If the user mask is 3 or 4-dimensional with the same shape as the input array, it is used as the input mask.
Step 2: The data is then regridded. In the two-dimensional case, the input mask is `broadcast' across the other dimensions of the array. In other words, it assumes that all horizontal slices of the array have the same mask. The result is a new array, defined on the output grid. Optionally, the regridder function can also return an array having the same shape as the output array, defining the fractional area of the output array which overlaps a non-missing input grid cell. This is useful for calculating area-weighted means of masked data.
Step 3: Finally, if the output grid has a mask, it is applied to the result array. Where the output mask is 0, data values are set to the missing data value, or 1.0e20 if undefined. The result array or transient variable will have a mask value of 1 (invalid value) for those output grid cells which completely overlap input grid cells with missing values.
Example: Regrid data to a uniform output grid.
3 from regrid import Regridder
4 f = cdms.open('rls_ccc_per.nc')
7 outgrid = cdms.createUniformGrid(90.0, 46, -4.0, 0.0, 72, 5.0)
8 regridFunc = Regridder(ingrid, outgrid)
|
Create a 4 x 5 degree output grid. Note that this grid is not associated with a file or dataset |
|
|
Read all data and regrid. The missing data value is obtained from variable rlsf. |
Example: Get a mask from a separate file, and set as the input grid mask.
2 from regrid import Regridder
4 f = cdms.open('so_ccc_per.nc')
7 g = cdms.open('rls_mri_per.nc')
10 regridFunc = Regridder(ingrid,outgrid)
11 h = cdms.open('sft_ccc.nc')
12 sfmaskvar = h.variables['sfmask']
14 outArray = regridFunc(sof.subSlice(time=0),mask=sfmask)
Example: Generate an array of zonal mean values.
1 f = cdms.open('rls_ccc_per.nc')
4 outgrid = cdms.createZonalGrid(ingrid)
5 regridFunc = Regridder(ingrid,outgrid)
|
Create a zonal grid. outgrid has the same latitudes as ingrid , and a singleton longitude dimension. createGlobalMeanGrid could be used here to generate a global mean array. |
|
Example: Regrid an array with missing data, and calculate the area-weighted mean of the result.
2 outgrid = cdms.createUniformGrid(90.0, 46, -4.0, 0.0, 72, 5.0)
3 outlatw, outlonw = outgrid.getWeights()
4 outweights = outerproduct(outlatw, outlonw)
7 latw, lonw = grid.getWeights()
8 weights = outerproduct(latw, lonw)
9 inmask = where(greater(absolute(sample),1.e15),0,1)
10 mean = add.reduce(ravel(inmask*weights*sample))/add.reduce(ravel(inmask*weights))
11 regridFunc = Regridder(grid, outgrid)
12 outsample, outmask = regridFunc(sample, mask=inmask, returnTuple=1)
13 outmean = add.reduce(ravel(outmask*outweights*outsample))/add.reduce(ravel(outmask*outweights))
|
Regrid. Because returnTuple is set to 1, the result is a tuple (dataArray, maskArray). |
|
|
Calculate the area-weighted mean of the regridded data. mean and outmean should be approximately equal. |