diff --git a/docs/whats_new.md b/docs/whats_new.md index e565ed6..8cf10a7 100644 --- a/docs/whats_new.md +++ b/docs/whats_new.md @@ -1,5 +1,17 @@ # What's New +## v0.9.6 (November 15, 2024) + +* made caching directory creation and saving to cache optional with input option `use_cache`. + +## v0.9.5 (November 14, 2024) + +* fixed error in output file + +## v0.9.4 (November 14, 2024) + +* Updated locations for local model output. + ## v0.9.3 (November 13, 2024) * Moved `output_format` parameter to manager config from model config diff --git a/particle_tracking_manager/models/opendrift/opendrift.py b/particle_tracking_manager/models/opendrift/opendrift.py index 5b46d53..28620cc 100644 --- a/particle_tracking_manager/models/opendrift/opendrift.py +++ b/particle_tracking_manager/models/opendrift/opendrift.py @@ -11,8 +11,6 @@ from pathlib import Path from typing import Optional, Union -import appdirs - # using my own version of ROMS reader # from .reader_ROMS_native import Reader import pandas as pd @@ -858,22 +856,11 @@ def run_add_reader( "start_time and end_time must be set to narrow model output to simulation time" ) - # save interpolators to save time - cache_dir = Path( - appdirs.user_cache_dir( - appname="particle-tracking-manager", appauthor="axiom-data-science" - ) - ) - cache_dir.mkdir(parents=True, exist_ok=True) - self.cache_dir = cache_dir - self.interpolator_filename = cache_dir / Path( - f"{self.ocean_model}_interpolator" - ) reader = reader_ROMS_native.Reader( filename=ds, name=self.ocean_model, standard_name_mapping=standard_name_mapping, - save_interpolator=True, + save_interpolator=self.save_interpolator, interpolator_filename=self.interpolator_filename, ) diff --git a/particle_tracking_manager/the_manager.py b/particle_tracking_manager/the_manager.py index 7a1b017..0446816 100644 --- a/particle_tracking_manager/the_manager.py +++ b/particle_tracking_manager/the_manager.py @@ -1,7 +1,6 @@ """Contains logic for configuring particle tracking simulations.""" -# from docstring_inheritance import NumpyDocstringInheritanceMeta import datetime import json import logging @@ -9,6 +8,8 @@ from typing import Optional, Union +# from docstring_inheritance import NumpyDocstringInheritanceMeta +import appdirs import pandas as pd from .cli import is_None @@ -139,6 +140,8 @@ class ParticleTrackingManager: Name of output file to save, by default None. If None, default is set in the model. Without any suffix. output_format : str, default "netcdf" Name of input/output module type to use for writing Lagrangian model output. Default is "netcdf". + use_cache : bool + Set to True to use cache for saving interpolators, by default True. Notes ----- @@ -165,6 +168,7 @@ class ParticleTrackingManager: output_file: str output_format: str output_file_initial: Optional[str] + interpolator_filename: Optional[pathlib.Path] def __init__( self, @@ -193,6 +197,7 @@ def __init__( use_static_masks: bool = config_ptm["use_static_masks"]["default"], output_file: Optional[str] = config_ptm["output_file"]["default"], output_format: str = config_ptm["output_format"]["default"], + use_cache: bool = config_ptm["use_cache"]["default"], **kw, ) -> None: """Inputs necessary for any particle tracking.""" @@ -466,6 +471,29 @@ def __setattr__(self, name: str, value) -> None: ) self.seed_seafloor = False + # save interpolators to save time + if name == "use_cache": + if value: + cache_dir = pathlib.Path( + appdirs.user_cache_dir( + appname="particle-tracking-manager", + appauthor="axiom-data-science", + ) + ) + cache_dir.mkdir(parents=True, exist_ok=True) + self.cache_dir = cache_dir + self.interpolator_filename = cache_dir / pathlib.Path( + f"{self.ocean_model}_interpolator" + ) + self.save_interpolator = True + self.logger.info( + f"Interpolators will be saved to {self.interpolator_filename}." + ) + else: + self.save_interpolator = False + self.interpolator_filename = None + self.logger.info("Interpolators will not be saved.") + # if reader, lon, and lat set, check inputs if ( name == "has_added_reader" diff --git a/particle_tracking_manager/the_manager_config.json b/particle_tracking_manager/the_manager_config.json index 064460d..809761e 100644 --- a/particle_tracking_manager/the_manager_config.json +++ b/particle_tracking_manager/the_manager_config.json @@ -187,5 +187,11 @@ "default": "netcdf", "description": "Output file format. Options are \"netcdf\" or \"parquet\".", "ptm_level": 2 + }, + "use_cache": { + "type": "bool", + "default": true, + "description": "Set to True to use cache for storing interpolators. This saves time on repeat simulations, may be used for other items in the future.", + "ptm_level": 3 } } diff --git a/tests/test_manager.py b/tests/test_manager.py index 8a39c2b..d79ff36 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -240,6 +240,13 @@ def test_input_too_many_end_of_simulation(): ) +def test_no_cache(): + """Test having no cache""" + + m = ptm.OpenDriftModel(use_cache=False) + assert m.interpolator_filename is None + + def test_changing_end_of_simulation(): """change end_time, steps, and duration