Skip to content

Commit

Permalink
use gaussian grid as common grid to be more consistent with the CDMS/…
Browse files Browse the repository at this point in the history
…CDAT version of the code
  • Loading branch information
lee1043 committed May 9, 2024
1 parent 493076a commit 6a88bd3
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions pcmdi_metrics/mjo/lib/lib_mjo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,43 @@

import numpy as np
import xarray as xr
import xcdat as xc
from scipy import signal

from pcmdi_metrics.io import base, get_time_key, select_subset
from pcmdi_metrics.utils import create_target_grid, regrid
from pcmdi_metrics.utils import regrid

# from pcmdi_metrics.utils import create_target_grid


def interp2commonGrid(ds, data_var, dlat, dlon=None, debug=False):
if dlon is None:
dlon = dlat
# nlat = int(180 / dlat)
# nlon = int(360 / dlon)
grid = create_target_grid(target_grid_resolution=f"{dlat}x{dlon}")

# grid = create_target_grid(target_grid_resolution=f"{dlat}x{dlon}")
nlat = int(180 / dlat)
grid = xc.create_gaussian_grid(nlat)

# If the longitude values include 0 and 360, then remove 360 to avoid having repeating grid
if 0 in grid.lon.values and 360 in grid.lon.values:
min_lon = grid.lon.values[0] # 0
# max_lon = grid.lon.values[-1] # 360
second_max_lon = grid.lon.values[-2] # 360-dlat
grid = grid.sel(lon=slice(min_lon, second_max_lon))

# Reverse latitude if needed
if grid.lat.values[0] > grid.lat.values[-1]:
grid = grid.isel(lat=slice(None, None, -1))

# Regrid
ds_regrid = regrid(ds, data_var, grid)
ds_regrid_subset = select_subset(ds_regrid, lat=(-10, 10))

if debug:
print(
"debug: ds_regrid_subset[data_var] shape:", ds_regrid_subset[data_var].shape
)

return ds_regrid_subset


Expand Down

0 comments on commit 6a88bd3

Please sign in to comment.