Skip to content

Commit

Permalink
feat(cli): run the cli script as modules with python -m
Browse files Browse the repository at this point in the history
  • Loading branch information
engeir committed Jan 30, 2024
1 parent 0230a3c commit 70928c2
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ For more information, see
volcano-cooking --help
```

> [!TIP]
>
> It is also possible to run the CLI as a python module. Try: `python -m volcano_cooking
> --help`, `python -m volcano_cooking.view_forcing --help` or `python -m
> volcano_cooking.sparse_to_lin --help`.
#### Option 0 (default, using NCL-script)

##### TL;DR
Expand Down
1 change: 1 addition & 0 deletions src/volcano_cooking/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Create source and forcing files used in the CESM2 climate model."""

from importlib_metadata import version

__version__ = version(__package__)
4 changes: 4 additions & 0 deletions src/volcano_cooking/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ def get_forcing_file(
)
else:
sys.exit("Okay, I won't download it. Exiting gracefully.")


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
generated in that file, before a second file is saved with the shifted eruption.
"""


from typing import Optional, Tuple

import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Modified from script provided by Herman Fæhn Fuglestvedt and using
http://svn.code.sf.net/p/codescripts/code/trunk/ncl/emission/createVolcEruptV3.ncl
"""

import os
from typing import Tuple

Expand Down
16 changes: 8 additions & 8 deletions src/volcano_cooking/modules/create/create_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ def make_dataset(self) -> None:
self.my_frc["Total_Emission"] = self.my_frc.Total_Emission.assign_attrs(
Units="Tg_of_SO2"
)
self.my_frc[
"Maximum_Injection_Height"
] = self.my_frc.Maximum_Injection_Height.assign_attrs(
Units="km_above_mean_sea_level"
self.my_frc["Maximum_Injection_Height"] = (
self.my_frc.Maximum_Injection_Height.assign_attrs(
Units="km_above_mean_sea_level"
)
)
self.my_frc[
"Minimum_Injection_Height"
] = self.my_frc.Minimum_Injection_Height.assign_attrs(
Units="km_above_mean_sea_level"
self.my_frc["Minimum_Injection_Height"] = (
self.my_frc.Minimum_Injection_Height.assign_attrs(
Units="km_above_mean_sea_level"
)
)

def save_to_file(self) -> None:
Expand Down
1 change: 0 additions & 1 deletion src/volcano_cooking/modules/create/create_dates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Script to put any function that creates correctly formatted dates."""


import datetime as dt
from itertools import cycle
from typing import List, Tuple, Union
Expand Down
13 changes: 7 additions & 6 deletions src/volcano_cooking/modules/create/rewrite_frc_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
and find a suitable altitude (perhaps from `volcano-cooking`). Then the values are set
using the implementations from `volcano-cooking`.
"""

import os
from datetime import datetime

Expand Down Expand Up @@ -159,9 +160,9 @@ def __set_global_attrs(self, file) -> None:
+ f"volcanoes, {start}-{end}"
)
elif a == "data_creator":
self.my_frc.attrs[
a
] = "Eirik Rolland Enger, University of Tromsø, [email protected]"
self.my_frc.attrs[a] = (
"Eirik Rolland Enger, University of Tromsø, [email protected]"
)
elif a == "data_doi":
self.my_frc.attrs[a] = "No doi yet"
elif a == "data_source_url":
Expand All @@ -179,9 +180,9 @@ def __set_global_attrs(self, file) -> None:
elif a == "cesm_contact":
self.my_frc.attrs[a] = "None"
elif a == "data_script":
self.my_frc.attrs[
a
] = "Generated with the 'volcano-cooking' CLI with the re-write option."
self.my_frc.attrs[a] = (
"Generated with the 'volcano-cooking' CLI with the re-write option."
)
elif a == "data_summary":
nd = (
10000 * self.yoes.astype(np.float32)
Expand Down
16 changes: 9 additions & 7 deletions src/volcano_cooking/plotting/figure_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ def log_tick_format(axes: plt.Axes, which: str, base: float = 10) -> plt.Axes:
f = getattr(axes, ax)
f.set_major_formatter(
ticker.FuncFormatter(
lambda x, _: "{:g}".format(x)
if np.log(x) / np.log(base) in [0, 1]
else r"$"
+ str(base)
+ "^{"
+ "{:g}".format(np.log(x) / np.log(base))
+ r"}$"
lambda x, _: (
"{:g}".format(x)
if np.log(x) / np.log(base) in [0, 1]
else r"$"
+ str(base)
+ "^{"
+ "{:g}".format(np.log(x) / np.log(base))
+ r"}$"
)
)
)
return axes
1 change: 1 addition & 0 deletions src/volcano_cooking/synthetic_volcanoes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
The lat/lon location is also included, but considered unimportant and thus the
same location is used for all volcanoes.
"""

from typing import Optional

import volcano_cooking.modules.create as create
Expand Down
4 changes: 4 additions & 0 deletions src/volcano_cooking/view_force.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ def main(
if style not in styles:
sys.exit(f"Style '{style}' not available. Available styles: {styles}")
v.view_forcing(in_file=filename, width=width, style=style, dark=dark, save=save)


if __name__ == "__main__":
main()

0 comments on commit 70928c2

Please sign in to comment.