Skip to content

Commit

Permalink
Merge pull request #1209 from PCMDI/MoV-clean-up
Browse files Browse the repository at this point in the history
MoV code clean up
  • Loading branch information
lee1043 authored Dec 18, 2024
2 parents 0ebe08a + 2c7d713 commit d364873
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 162 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification.

## Disclaimer
This document was prepared as an account of work sponsored by an
agency of the United States government. Neither the United States government
nor Lawrence Livermore National Security, LLC, nor any of their employees
makes any warranty, expressed or implied, or assumes any legal liability or
responsibility for the accuracy, completeness, or usefulness of any
information, apparatus, product, or process disclosed, or represents that its
use would not infringe privately owned rights. Reference herein to any specific
commercial product, process, or service by trade name, trademark, manufacturer,
or otherwise does not necessarily constitute or imply its endorsement,
recommendation, or favoring by the United States government or Lawrence
Livermore National Security, LLC. The views and opinions of authors expressed
herein do not necessarily state or reflect those of the United States
government or Lawrence Livermore National Security, LLC, and shall not be used
for advertising or product endorsement purposes.




Expand Down
71 changes: 46 additions & 25 deletions pcmdi_metrics/variability_mode/lib/calc_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,51 @@ def calc_stats_save_dict(
debug=False,
):
"""
NOTE: Calculate statistics and save numbers to dictionary for JSON.
Input
- mode: [str] name of variability mode
- dict_head: [dict] subset of dictionary
- eof: [2d field] linear regressed eof pattern (eof domain)
- eof_lr: [2d field] linear regressed eof pattern (global)
- slope: [2d field] slope from above linear regression (bring it here to calculate rmsc)
- pc: [1d field] principle component time series
- stdv_pc: [float] standard deviation of principle component time series
- frac: [1 number field] fraction of explained variance
- eof_obs: [2d field] eof pattern over subdomain from observation
- eof_lr_obs: [2d field] eof pattern over globe (linear regressed) from observation
- stdv_pc_obs: [float] standard deviation of principle component time series of observation
- obs_compare: [bool] calculate statistics against given observation (default=True)
- method: [string] 'eof' or 'cbf'
- debug: [bool]
Calculate statistics and save results to a dictionary for JSON output.
This function computes various statistics based on the provided model and observational
data, and stores the results in a dictionary format suitable for JSON serialization.
Parameters
----------
mode : str
The name of the variability mode.
dict_head : dict
A subset of the dictionary to which results will be added.
model_ds : xr.Dataset
The dataset containing model data.
model_data_var : str
The variable name in the model dataset.
eof : xr.Dataset
The linear regressed EOF pattern for the EOF domain (2D field).
eof_lr : xr.Dataset
The linear regressed EOF pattern for the global domain (2D field).
pc : array-like
The principal component time series (1D field).
stdv_pc : float
The standard deviation of the principal component time series.
frac : float
The fraction of explained variance.
regions_specs : dict, optional
Specifications for regions, if applicable. Default is None.
obs_ds : xr.Dataset, optional
The dataset containing observational data. Default is None.
eof_obs : optional
The EOF pattern over the subdomain from observations (2D field). Default is None.
eof_lr_obs : optional
The linear regressed EOF pattern over the globe from observations (2D field). Default is None.
stdv_pc_obs : float, optional
The standard deviation of the principal component time series of observations. Default is None.
obs_compare : bool, optional
Whether to calculate statistics against the given observations. Default is True.
method : str, optional
The method to use, either 'eof' or 'cbf'. Default is 'eof'.
debug : bool, optional
A flag to enable debugging output. Default is False.
Returns
-------
None
"""

# Add to dictionary for json output
Expand All @@ -60,20 +89,14 @@ def calc_stats_save_dict(
# Note: '_glo' indicates statistics calculated over global domain
# . . . . . . . . . . . . . . . . . . . . . . . . .
if obs_compare:
# ref_grid_global = get_grid(eof_lr_obs)
ref_grid_global = get_grid(obs_ds)
# Regrid (interpolation, model grid to ref grid)
debug_print("regrid (global) start", debug)
# eof_model_global = eof_lr.regrid(eof_lr,
# ref_grid_global, regridTool="regrid2", mkCyclic=True
# )
# eof_model_global = regrid(eof_lr, ref_grid_global)
eof_model_global = regrid(
model_ds, data_var=model_data_var, target_grid=ref_grid_global
)[model_data_var]
debug_print("regrid end", debug)
# Extract subdomain
# eof_model = eof_model_global(region_subdomain)
eof_model = region_subset(eof_model_global, mode, regions_specs=regions_specs)

# Spatial correlation weighted by area ('generate' option for weights)
Expand All @@ -86,9 +109,7 @@ def calc_stats_save_dict(
# Double check for arbitrary sign control
if cor < 0:
debug_print("eof pattern pcor < 0, flip sign!", debug)
# variables_to_flip_sign = [eof, pc, eof_lr, eof_model_global, eof_model]
# for variable in variables_to_flip_sign:
# variable *= -1

eof.values = eof.values * -1
pc.values = pc.values * -1
eof_lr.values = eof_lr.values * -1
Expand Down
Loading

0 comments on commit d364873

Please sign in to comment.