Note
Click here to download the full example code
Comparing two datasets with different resolutions using regriddingΒΆ
In this recipe, we will regrid two different datasets with different resolutions. An example use case could be one where the observational dataset with a higher resolution needs to be regridded to that of the model dataset so that they can be compared with each other.
Import cf-python:
import cf
Read the field constructs:
[<CF Field: ncvar%stn(long_name=time(1452), long_name=latitude(360), long_name=longitude(720))>,
<CF Field: long_name=near-surface temperature(long_name=time(1452), long_name=latitude(360), long_name=longitude(720)) degrees Celsius>]
[<CF Field: air_temperature(time(1980), latitude(144), longitude(192)) K>]
Select observation and model temperature fields by identity and index respectively, and look at their contents:
obs_temp = obs.select_field("long_name=near-surface temperature")
print(obs_temp)
Field: long_name=near-surface temperature (ncvar%tmp)
-----------------------------------------------------
Data : long_name=near-surface temperature(long_name=time(1452), long_name=latitude(360), long_name=longitude(720)) degrees Celsius
Dimension coords: long_name=time(1452) = [1901-01-16 00:00:00, ..., 2021-12-16 00:00:00] gregorian
: long_name=latitude(360) = [-89.75, ..., 89.75] degrees_north
: long_name=longitude(720) = [-179.75, ..., 179.75] degrees_east
model_temp = model[0]
print(model_temp)
Field: air_temperature (ncvar%tas)
----------------------------------
Data : air_temperature(time(1980), latitude(144), longitude(192)) K
Cell methods : time(1980): mean (interval: 1 hour)
Dimension coords: time(1980) = [1850-01-16 00:00:00, ..., 2014-12-16 00:00:00] 360_day
: latitude(144) = [-89.375, ..., 89.375] degrees_north
: longitude(192) = [0.9375, ..., 359.0625] degrees_east
: height(1) = [1.5] m
Coord references: grid_mapping_name:latitude_longitude
Regrid observational data to that of the model data and create a new low resolution observational data using bilinear interpolation:
obs_temp_regrid = obs_temp.regrids(model_temp, method="linear")
print(obs_temp_regrid)
Field: long_name=near-surface temperature (ncvar%tmp)
-----------------------------------------------------
Data : long_name=near-surface temperature(long_name=time(1452), latitude(144), longitude(192)) degrees Celsius
Dimension coords: long_name=time(1452) = [1901-01-16 00:00:00, ..., 2021-12-16 00:00:00] gregorian
: latitude(144) = [-89.375, ..., 89.375] degrees_north
: longitude(192) = [0.9375, ..., 359.0625] degrees_east
Coord references: grid_mapping_name:latitude_longitude
Total running time of the script: ( 0 minutes 3.109 seconds)