Skip to content

Commit

Permalink
updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
jreniel committed Aug 17, 2021
1 parent 51b5c44 commit 116381a
Show file tree
Hide file tree
Showing 24 changed files with 938 additions and 1,512 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ forecast
.vscode
*.swp
*.nc
hgrid.*
fgrid.*
vgrid.*
*.gr3
*.ll
.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ hgrid.write("/path/to/output/file.2dm", fmt='2dm')
Questions, comments and suggestions are welcome. Please follow the instructions on the `CONTRIBUTING.md` file for contributions. For bug reports and feature requests, please open an issue using the issue tracker.
Author contact: jrcalzada@wm.edu



“Marconi is a good fellow. Let him continue. He is using seventeen of my patents.”
- Nikola Tesla
2 changes: 2 additions & 0 deletions pyschism/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pyschism.cmd.grd2sms import Grd2SmsCli
from pyschism.cmd.hgrid import HgridCli
from pyschism.cmd.outputs import OutputsCli
from pyschism.cmd.sflux import SfluxCli
from pyschism.cmd.sms2grd import Sms2grdCli
from pyschism.cmd.stations import StationsCli
from pyschism.cmd.tvdflag import TvdflagCli
Expand All @@ -22,6 +23,7 @@
"Grd2SmsCli",
"HgridCli",
"OutputsCli",
"SfluxCli",
"Sms2grdCli",
"StationsCli",
"TvdflagCli",
Expand Down
14 changes: 14 additions & 0 deletions pyschism/cmd/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
import argparse


class CliComponent(ABC):

@abstractmethod
def __init__(self, args: argparse.Namespace):
self.args = args

@staticmethod
@abstractmethod
def add_subparser_action(subparsers: argparse._SubParsersAction) -> None:
raise NotImplementedError('Method must be implemented by subclass.')
8 changes: 8 additions & 0 deletions pyschism/cmd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,3 +1522,11 @@ def add_stations_outputs_to_parser(parser):
action="store_true",
help="Include water level output for stations.",
)


def add_vmin_to_parser(parser):
parser.add_argument('--vmin', type=float)


def add_vmax_to_parser(parser):
parser.add_argument('--vmax', type=float)
37 changes: 35 additions & 2 deletions pyschism/cmd/outputs.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
from argparse import Namespace
import sys

from pyschism.cmd import common
from pyschism.outputs.outputs import OutputsCollector


class OutputsCli:

def __init__(self, args: Namespace):
outputs = OutputsCollector(args.outputs_directory)
getattr(outputs, args.variable_name).make_plot(show=True)
if args.action == 'plot':
getattr(outputs, args.variable_name).make_plot(show=True)

elif args.action == 'animate':
getattr(outputs, args.variable_name).animation(
vmin=args.vmin,
vmax=args.vmax,
show=args.show,
fps=args.fps,
save=args.save,
)

@staticmethod
def add_subparser_action(subparsers):
add_outputs_options_to_parser(subparsers.add_parser('outputs'))


def add_outputs_options_to_parser(parser):

parser.add_argument('outputs_directory')
parser.add_argument('variable_name')
parser.add_argument('step', type=int)
subparsers = parser.add_subparsers(dest='action')
add_plot_action(subparsers)
add_animate_action(subparsers)


def add_plot_action(subparsers):
parser = subparsers.add_parser('plot')
parser.add_argument('step')


def add_animate_action(subparsers):
parser = subparsers.add_parser('animate')
common.add_vmin_to_parser(parser)
common.add_vmax_to_parser(parser)
show = parser.add_mutually_exclusive_group()
show.add_argument('--show', dest='show', action='store_true')
show.add_argument('--no-show', dest='show', action='store_false')
parser.set_defaults(show=False if "--save" in " ".join(sys.argv) else True)
parser.add_argument('--fps', type=float)
parser.add_argument('--save')
parser.add_argument('--figsize', nargs=2, type=float)
70 changes: 70 additions & 0 deletions pyschism/cmd/sflux.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import argparse
import pathlib
import sys

import matplotlib.pyplot as plt

from pyschism.cmd.base import CliComponent
from pyschism.forcing.nws.nws2 import NWS2, SfluxDataset

sflux_variables = {
"air": ["prmsl", "spfh", "stmp", "uwind", "vwind"],
"prc": ["prate"],
"rad": ["dlwrf", "dswrf"],
}


class SfluxCli(CliComponent):
def __init__(self, args: argparse.Namespace):
kwargs = {}
if args.sflux_1_glob is not None:
kwargs.setdefault("sflux_1_glob", args.sflux_1_glob)
if args.sflux_2_glob is not None:
kwargs.setdefault("sflux_2_glob", args.sflux_2_glob)
nws2 = NWS2.read(args.sflux_directory, **kwargs)
if args.action == "animate":
sflux = getattr(nws2, f"sflux_{args.level}")
for vargroup, vars in sflux_variables.items():
component = getattr(sflux, f"{vargroup}")
for variable in vars:
animations = []
if getattr(args, variable) is True:
animations.append(
getattr(component, f"{variable}").animation(
save=args.save / f"{variable}.gif",
fps=args.fps,
)
)
if args.show is True:
plt.show()

@staticmethod
def add_subparser_action(subparsers: argparse._SubParsersAction) -> None:
parser = subparsers.add_parser("sflux")
parser.add_argument("sflux_directory")
parser.add_argument("--level", choices=["1", "2"], default="1")
parser.add_argument("--sflux-1-glob")
parser.add_argument("--sflux-2-glob")
subparsers = parser.add_subparsers(dest="action")
add_animate_to_subparsers(subparsers)


def add_animate_to_subparsers(subparsers):
parser = subparsers.add_parser("animate")
for vargroup, variables in sflux_variables.items():
arg_group = parser.add_argument_group(f"{vargroup} variables")
for variable in variables:
arg_group.add_argument(f"--{variable}", action="store_true", default=False)
parser.add_argument("--save", type=lambda x: pathlib.Path(x))
parser.add_argument("--fps", type=float)
parser.add_argument(
"--show",
action="store_true",
dest="show",
)
parser.add_argument(
"--no-show",
action="store_true",
dest="show",
)
parser.set_defaults(show=False if "--save" in " ".join(sys.argv) else True)
Loading

0 comments on commit 116381a

Please sign in to comment.