Skip to content

Commit

Permalink
Merge pull request #973 from PCMDI/972_ao_targetgrid
Browse files Browse the repository at this point in the history
Add custom target grid to mean climate
  • Loading branch information
acordonez committed Aug 17, 2023
2 parents 2381f3e + ca6d38a commit a62125f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def create_mean_climate_parser():
dest="target_grid",
help='Options are "2.5x2.5" or an actual cdms2 grid object',
required=False,
default="2.5x2.5"
)

parser.add_argument(
Expand Down
43 changes: 25 additions & 18 deletions pcmdi_metrics/mean_climate/mean_climate_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,31 @@
print('--- prepare mean climate metrics calculation ---')

# generate target grid
if target_grid == "2.5x2.5":
# target grid for regridding
t_grid = xc.create_uniform_grid(-88.875, 88.625, 2.5, 0, 357.5, 2.5)
if debug:
print('type(t_grid):', type(t_grid)) # Expected type is 'xarray.core.dataset.Dataset'
print('t_grid:', t_grid)
# identical target grid in cdms2 to use generateLandSeaMask function that is yet to exist in xcdat
t_grid_cdms2 = cdms2.createUniformGrid(-88.875, 72, 2.5, 0, 144, 2.5)
# generate land sea mask for the target grid
sft = cdutil.generateLandSeaMask(t_grid_cdms2)
if debug:
print('sft:', sft)
print('sft.getAxisList():', sft.getAxisList())
# add sft to target grid dataset
t_grid['sftlf'] = (['lat', 'lon'], np.array(sft))
if debug:
print('t_grid (after sftlf added):', t_grid)
t_grid.to_netcdf('target_grid.nc')
res=target_grid.split("x")
lat_res=float(res[0])
lon_res=float(res[1])
start_lat=-90.+lat_res/2
start_lon=0.
end_lat = 90.-lat_res/2
end_lon = 360.-lon_res
nlat = ((end_lat - start_lat) * 1./lat_res) + 1
nlon = ((end_lon - start_lon) * 1./lon_res) + 1
t_grid=xc.create_uniform_grid(start_lat,end_lat,lat_res,start_lon,end_lon,lon_res)
if debug:
print('type(t_grid):', type(t_grid)) # Expected type is 'xarray.core.dataset.Dataset'
print('t_grid:', t_grid)
# identical target grid in cdms2 to use generateLandSeaMask function that is yet to exist in xcdat
t_grid_cdms2 = cdms2.createUniformGrid(start_lat,nlat,lat_res,start_lon,nlon,lon_res)
# generate land sea mask for the target grid
sft = cdutil.generateLandSeaMask(t_grid_cdms2)
if debug:
print('sft:', sft)
print('sft.getAxisList():', sft.getAxisList())
# add sft to target grid dataset
t_grid['sftlf'] = (['lat', 'lon'], np.array(sft))
if debug:
print('t_grid (after sftlf added):', t_grid)
t_grid.to_netcdf('target_grid.nc')

# load obs catalogue json
egg_pth = resources.resource_path()
Expand Down

0 comments on commit a62125f

Please sign in to comment.