From d433e2c3576945aae91dfd5c3d6c85e443ca71aa Mon Sep 17 00:00:00 2001 From: thomaszwagerman Date: Fri, 6 Dec 2024 15:08:29 +0000 Subject: [PATCH 1/2] add file output to cli plotting --- src/asli/asli.py | 29 ++++++++++++++++++++++++----- src/asli/plot.py | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/asli/asli.py b/src/asli/asli.py index 383e167..fd6f1b2 100644 --- a/src/asli/asli.py +++ b/src/asli/asli.py @@ -4,6 +4,7 @@ import datetime import logging import os +import matplotlib.pyplot as plt from pathlib import Path from typing import Mapping, Union @@ -220,7 +221,8 @@ def read_mask_data(self): ).lsm.squeeze() else: self.land_sea_mask = xr.open_dataset( - Path(self.data_dir, self.mask_filename) + Path(self.data_dir, self.mask_filename), + engine = "netcdf4" ).lsm.squeeze() def read_msl_data(self, include_era5t: bool=False): @@ -435,7 +437,7 @@ def _cli_common_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser "-o", "--output", type=str, - help="Output file path for CSV, relative to .", + help="Output file path for file, relative to .", ) parser.add_argument( "msl_files", @@ -461,6 +463,13 @@ def _get_cli_plot_args(): type=str, help="Input CSV file, relative to .", ) + parser.add_argument( + "-y", + "--year", + nargs="?", + type=int, + help="When present, plot only the year specified" + ) parser = _cli_common_args(parser) return parser.parse_args() @@ -499,8 +508,18 @@ def _cli_plot(): a = ASLICalculator(args.datadir, args.mask, args.msl_files[0]) a.read_mask_data() a.read_msl_data() - a.import_from_csv(args.input) - a.plot_region_all() + # Perform the calculation if no input file is provided + if args.input: + a.import_from_csv(args.input) + else: + a.calculate() + # Plot all if no specific year is provided + if args.year: + a.plot_region_year(args.year) + else: + a.plot_region_all() + if args.output: + plt.savefig(os.path.join(args.datadir, args.output)) def _cli_calc(): """Command-line interface to ASL calculation.""" @@ -517,4 +536,4 @@ def _cli_calc(): if __name__ == "__main__": - _cli_calc() + _cli_calc() \ No newline at end of file diff --git a/src/asli/plot.py b/src/asli/plot.py index e51b4d3..d4da690 100644 --- a/src/asli/plot.py +++ b/src/asli/plot.py @@ -110,4 +110,4 @@ def plot_lows( if regionbox: draw_regional_box(regionbox) - return ax + return ax \ No newline at end of file From 6be73bff28415a28b16d6644253ccb9805007a76 Mon Sep 17 00:00:00 2001 From: thomaszwagerman Date: Fri, 6 Dec 2024 15:12:43 +0000 Subject: [PATCH 2/2] remove read mask engine specification, not relevant here --- src/asli/asli.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/asli/asli.py b/src/asli/asli.py index fd6f1b2..f0183d2 100644 --- a/src/asli/asli.py +++ b/src/asli/asli.py @@ -221,8 +221,7 @@ def read_mask_data(self): ).lsm.squeeze() else: self.land_sea_mask = xr.open_dataset( - Path(self.data_dir, self.mask_filename), - engine = "netcdf4" + Path(self.data_dir, self.mask_filename) ).lsm.squeeze() def read_msl_data(self, include_era5t: bool=False):