Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MJO xcdat conversion #1091

Merged
merged 23 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
680dd7c
in progress ...
lee1043 Apr 30, 2024
8236449
update
lee1043 May 1, 2024
aad15b8
update and clean up
lee1043 May 1, 2024
ebc9b50
update and clean up
lee1043 May 1, 2024
bf34acc
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 1, 2024
2ac7e79
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 2, 2024
1c97c33
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 2, 2024
953f1a5
clean up
lee1043 May 2, 2024
a56b94c
pre-commit fix
lee1043 May 2, 2024
f781c20
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 2, 2024
108636a
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 2, 2024
087e30e
move dict_merge script to lib
lee1043 May 2, 2024
f2a4e11
Merge branch 'feature/1084_lee1043_MJO-xcdat' of github.com:PCMDI/pcm…
lee1043 May 2, 2024
493076a
use more straitforward code because otherwise error occurs with non-s…
lee1043 May 8, 2024
6a88bd3
use gaussian grid as common grid to be more consistent with the CDMS/…
lee1043 May 9, 2024
550f4d8
clean up; potential bug fix that could happen when segment length is …
lee1043 May 9, 2024
84f5730
add gaussian grid option to utils function and refer to it
lee1043 May 9, 2024
5b61d85
clean up
lee1043 May 9, 2024
240627e
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 May 14, 2024
f455307
adjust criteria for frequency domain selection to make the selected f…
lee1043 May 14, 2024
2e95b74
cleaned up
lee1043 May 15, 2024
177f616
clean up
lee1043 May 15, 2024
e9e3be0
Merge branch 'main' into feature/1084_lee1043_MJO-xcdat
lee1043 Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add gaussian grid option to utils function and refer to it
  • Loading branch information
lee1043 committed May 9, 2024
commit 84f5730b0a0980941c438254b210fd3a7f966a9c
23 changes: 5 additions & 18 deletions pcmdi_metrics/mjo/lib/lib_mjo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,20 @@

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 regrid

# from pcmdi_metrics.utils import create_target_grid
from pcmdi_metrics.utils import create_target_grid, regrid


def interp2commonGrid(ds, data_var, dlat, dlon=None, debug=False):
if dlon is None:
dlon = dlat

# 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))
# Generate grid
grid = create_target_grid(
target_grid_resolution=f"{dlat}x{dlon}", grid_type="uniform"
)

# Regrid
ds_regrid = regrid(ds, data_var, grid)
Expand Down
38 changes: 32 additions & 6 deletions pcmdi_metrics/utils/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create_target_grid(
lon1: float = 0.0,
lon2: float = 360.0,
target_grid_resolution: str = "2.5x2.5",
grid_type: str = "uniform",
) -> xr.Dataset:
"""Generate a uniform grid for given latitude/longitude ranges and resolution

Expand All @@ -32,6 +33,8 @@ def create_target_grid(
Starting latitude, by default 360.
target_grid_resolution : str, optional
grid resolution in degree for lat and lon, by default "2.5x2.5"
grid_type : str, optional
type of the grid ('uniform' or 'gaussian'), by default "uniform"

Returns
-------
Expand All @@ -46,11 +49,11 @@ def create_target_grid(

Global uniform grid:

>>> t_grid = create_target_grid(-90, 90, 0, 360, target_grid="5x5")
>>> grid = create_target_grid(-90, 90, 0, 360, target_grid="5x5")

Regional uniform grid:

>>> t_grid = create_target_grid(30, 50, 100, 150, target_grid="0.5x0.5")
>>> grid = create_target_grid(30, 50, 100, 150, target_grid="0.5x0.5")
"""
# generate target grid
res = target_grid_resolution.split("x")
Expand All @@ -60,10 +63,33 @@ def create_target_grid(
start_lon = lon1 + lon_res / 2.0
end_lat = lat2 - lat_res / 2
end_lon = lon2 - lon_res / 2
t_grid = xc.create_uniform_grid(
start_lat, end_lat, lat_res, start_lon, end_lon, lon_res
)
return t_grid

if grid_type == "uniform":
grid = xc.create_uniform_grid(
start_lat, end_lat, lat_res, start_lon, end_lon, lon_res
)
elif grid_type == "gaussian":
nlat = int(180 / lat_res)
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))

grid = grid.sel(lat=slice(start_lat, end_lat), lon=slice(start_lon, end_lon))
else:
raise ValueError(
f"grid_type {grid_type} is undefined. Please use either `uniform` or `gaussian"
)

return grid


def __haversine(lat1, lon1, lat2, lon2):
Expand Down
Loading