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

Feature/lee1043 mov modularize #1060

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 15 additions & 16 deletions pcmdi_metrics/variability_mode/lib/eof_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ def get_anomaly_timeseries(ds: xr.Dataset, data_var: str, season: str) -> xr.Dat
raise TypeError(
"The first parameter of get_anomaly_timeseries must be an xarray Dataset"
)
# Get anomaly field
# Get anomaly field by removing annual cycle
ds_anomaly = ds.temporal.departures(data_var, freq="month", weighted=True)
# Get temporal average if needed
if season == "yearly":
# remove seasonal cycle
ds_anomaly = ds.temporal.departures(data_var, freq="month", weighted=True)
# yearly time series
ds_anomaly = ds_anomaly.temporal.group_average(
data_var, freq="year", weighted=True
Expand All @@ -410,27 +410,26 @@ def get_anomaly_timeseries(ds: xr.Dataset, data_var: str, season: str) -> xr.Dat
ds_ave = ds_anomaly.temporal.average(data_var)
# anomaly
ds_anomaly[data_var] = ds_anomaly[data_var] - ds_ave[data_var]
else:
# Remove annual cycle
ds_anomaly = ds.temporal.departures(data_var, freq="month", weighted=True)
if season != "monthly":
ds_anomaly_all_seasons = ds_anomaly.temporal.departures(
data_var,
freq="season",
weighted=True,
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
)
ds_anomaly = select_by_season(ds_anomaly_all_seasons, season)
elif season.upper() in ["DJF", "MAM", "JJA", "SON"]:
ds_anomaly_all_seasons = ds_anomaly.temporal.departures(
data_var,
freq="season",
weighted=True,
season_config={"dec_mode": "DJF", "drop_incomplete_djf": True},
)
ds_anomaly = select_by_season(ds_anomaly_all_seasons, season)
# return result
return ds_anomaly


def select_by_season(ds: xr.Dataset, season: str) -> xr.Dataset:
time_key = get_time_key(ds)
lat_bnds_key = get_latitude_bounds_key(ds)
lon_bnds_key = get_longitude_bounds_key(ds)
ds_subset = ds.where(ds[time_key].dt.season == season, drop=True)
# Preserve original spatial bounds info
# Extract original bounds
lat_bnds_key = get_latitude_bounds_key(ds)
lon_bnds_key = get_longitude_bounds_key(ds)
# Assign back to the new dataset
ds_subset[lat_bnds_key] = get_latitude_bounds(ds)
ds_subset[lon_bnds_key] = get_longitude_bounds(ds)
return ds_subset
Expand Down
6 changes: 3 additions & 3 deletions pcmdi_metrics/variability_mode/variability_modes_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@
debug=debug,
)

# Save global grid information for regrid below
# Get global grid information for later use: regrid
ref_grid_global = get_grid(obs_timeseries)

# Declare dictionary variables to keep information from observation
# Set dictionary variables to keep information from observation in the memory during the season and model loop
eof_obs = {}
pc_obs = {}
frac_obs = {}
Expand All @@ -317,7 +317,7 @@
stdv_pc_obs = {}
obs_timeseries_season_dict = {}

# Dictonary for json archive
# Set dictonary for json archive
if "obs" not in result_dict["REF"]:
result_dict["REF"]["obs"] = {}
if "defaultReference" not in result_dict["REF"]["obs"]:
Expand Down
Loading