Skip to content
Closed
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
14 changes: 7 additions & 7 deletions zppy_interfaces/global_time_series/coupled_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def set_var(
new_var_list: List[Variable] = []
if exp[exp_key] != "":
try:
dataset_wrapper: DatasetWrapper = DatasetWrapper(exp[exp_key])
dataset_wrapper: DatasetWrapper = DatasetWrapper(exp[exp_key], var_list)
except Exception as e:
logger.critical(e)
logger.critical(
Expand Down Expand Up @@ -253,16 +253,16 @@ def process_data(

# Optionally read ohc
if exp["ocean"] != "":
dataset_wrapper = DatasetWrapper(exp["ocean"])
exp["annual"]["ohc"], _ = dataset_wrapper.globalAnnual(Variable("ohc"))
ohc_variable = Variable("ohc")
dataset_wrapper = DatasetWrapper(exp["ocean"], [ohc_variable])
exp["annual"]["ohc"], _ = dataset_wrapper.globalAnnual(ohc_variable)
# anomalies with respect to first year
exp["annual"]["ohc"][:] = exp["annual"]["ohc"][:] - exp["annual"]["ohc"][0]

if exp["vol"] != "":
dataset_wrapper = DatasetWrapper(exp["vol"])
exp["annual"]["volume"], _ = dataset_wrapper.globalAnnual(
Variable("volume")
)
vol_variable = Variable("volume")
dataset_wrapper = DatasetWrapper(exp["vol"], [vol_variable])
exp["annual"]["volume"], _ = dataset_wrapper.globalAnnual(vol_variable)
# annomalies with respect to first year
exp["annual"]["volume"][:] = (
exp["annual"]["volume"][:] - exp["annual"]["volume"][0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Any, List, Optional, Tuple

import xarray
import xcdat
Expand All @@ -10,16 +10,20 @@


class DatasetWrapper(object):
def __init__(self, directory):
def __init__(self, directory: str, var_list: List[Variable]):

self.directory: str = directory

# `directory` will be of the form `{case_dir}/post/<component>/glb/ts/monthly/{ts_num_years_str}yr/`
file_path_list: List[str] = []
for var in var_list:
# `var` will be of the form `FSNS`, `FLNS`, etc.
file_path_list.append(f"{directory}{var.variable_name}*.nc")
Comment on lines +18 to +21
Copy link
Collaborator Author

@forsyth2 forsyth2 Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #21:

Xarray/xCDAT will attempt to concatenate all of these files into a single Dataset object, which can be slow depending on the number of files, the file sizes, and the shape/structure of the data.

I'm almost wondering if it'd be better to read in one variable at a time: at the beginning of globalAnnual open just that variable. As far as I can tell, with two important exceptions, variables aren't requested multiple times. Those exceptions are:

  1. The variables used for derivation in globalAnnualHelper can end up requested multiple times; they don't have to be re-loaded though because they already exist in the DatasetWrapper
  2. The variables used in both "original" and "atm"/"ocn" components are loaded twice -- as plots_original variables and as plots_atm/plots_ocn variables.
> git grep -n  dataset_wrapper
zppy_interfaces/global_time_series/coupled_global.py:10:from zppy_interfaces.global_time_series.coupled_global_dataset_wrapper import (
zppy_interfaces/global_time_series/coupled_global.py:168:            dataset_wrapper: DatasetWrapper = DatasetWrapper(exp[exp_key], var_list)
zppy_interfaces/global_time_series/coupled_global.py:180:                data_array, units = dataset_wrapper.globalAnnual(var)
zppy_interfaces/global_time_series/coupled_global.py:214:        del dataset_wrapper
zppy_interfaces/global_time_series/coupled_global.py:257:            dataset_wrapper = DatasetWrapper(exp["ocean"], [ohc_variable])
zppy_interfaces/global_time_series/coupled_global.py:258:            exp["annual"]["ohc"], _ = dataset_wrapper.globalAnnual(ohc_variable)
zppy_interfaces/global_time_series/coupled_global.py:264:            dataset_wrapper = DatasetWrapper(exp["vol"], [vol_variable])
zppy_interfaces/global_time_series/coupled_global.py:265:            exp["annual"]["volume"], _ = dataset_wrapper.globalAnnual(vol_variable)
> git grep -n set_area_tuple
zppy_interfaces/global_time_series/coupled_global_dataset_wrapper.py:28:    def set_area_tuple(self):
zppy_interfaces/global_time_series/coupled_global_dataset_wrapper.py:162:                    self.set_area_tuple()

self.dataset: xarray.core.dataset.Dataset = xcdat.open_mfdataset(
f"{directory}*.nc", center_times=True
file_path_list, center_times=True
)

self.area_tuple = None
self.area_tuple: Optional[Tuple[Any, Any, Any]] = None

def set_area_tuple(self):
keys = list(self.dataset.keys())
Expand Down
Loading