Skip to content

Commit

Permalink
Add plots
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaze committed Mar 27, 2024
1 parent 4fefc14 commit efc6bbf
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 150 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ vfrecovery describe WMO CYC
vfrecovery describe WMO CYC1 CYC2 CYC3
```

```bash
vfrecovery describe velocity WMO CYC
```

## Other commands

```bash
Expand Down
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies:
- parcels>=3.0.0
- dask
- distributed
- dask-kubernetes
# - dask-kubernetes
- bottleneck
- gcsfs
- zarr
Expand Down Expand Up @@ -57,4 +57,4 @@ dependencies:
- geojson
- dominate
- copernicusmarine>=1.0<=2.0
- git+https://github.com/euroargodev/VirtualFleet.git@master
# - git+https://github.com/euroargodev/VirtualFleet.git@master # better with `pip install --editable`
48 changes: 41 additions & 7 deletions vfrecovery/command_line_interface/group_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@
import argopy.plot as argoplot
from argopy.errors import DataNotFound
from argopy import ArgoIndex
import os
from pathlib import Path
import glob

from vfrecovery.utils.misc import list_float_simulation_folders

from vfrecovery.core.describe import describe_function

root_logger = logging.getLogger("vfrecovery_root_logger")
blank_logger = logging.getLogger("vfrecovery_blank_logger")



@click.group()
def cli_group_describe() -> None:
pass


@cli_group_describe.command(
"describe",
short_help="Describe VirtualFleet-Recovery simulation results",
short_help="Describe VirtualFleet-Recovery data and simulation results",
help="""
Returns data about an existing VirtualFleet-Recovery prediction
TARGET select what is to be described. A string in: 'all', 'obs', 'velocity'.
WMO is the float World Meteorological Organisation number
Data could be a JSON file, specific metrics or images
CYC is the cycle number location to restrict description to
""",
epilog="""
Examples:
Expand All @@ -34,7 +40,7 @@ def cli_group_describe() -> None:
\b
vfrecovery describe 6903091 112
""", # noqa
)
)
@click.option(
"--log-level",
type=click.Choice(["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "QUIET"]),
Expand All @@ -45,9 +51,11 @@ def cli_group_describe() -> None:
"(based on standard logging library)."
),
)
@click.argument('TARGET', nargs=1, type=str)
@click.argument('WMO', nargs=1, type=int)
@click.argument("CYC", nargs=-1, type=int)
def describe(
target,
wmo,
cyc,
log_level,
Expand All @@ -61,19 +69,45 @@ def describe(
root_logger.debug("DEBUG mode activated")

# Validate arguments:
if target.lower() not in ["all", "obs", "velocity"]:
raise ValueError("The first argument TARGET must be one in ['all', 'obs', 'velocity']")

assert is_wmo(wmo)
wmo = check_wmo(wmo)[0]
cyc = list(cyc)
if len(cyc) > 0:
assert is_cyc(cyc)
cyc = check_cyc(cyc)

#
# json_dump = describe_function(wmo,
# cyc=cyc,
# log_level=log_level)
# blank_logger.info(json_dump)

if target == 'obs':
describe_obs(wmo, cyc)

elif target == 'velocity':
describe_velocity(wmo, cyc)


def describe_velocity(wmo, cyc):

# List folders to examine:
plist = list_float_simulation_folders(wmo, cyc)

# List all available velocity files:
for c in plist.keys():
p = plist[c]
click.secho("Velocity file(s) for WMO=%s / CYC=%s:" % (wmo, c), fg='green')
vlist = sorted(p.glob("velocity_*.nc"))
if len(vlist) > 0:
[click.secho("\t- %s" % v) for v in vlist]
else:
click.secho("\tNo velocity file", fg='red')


def describe_obs(wmo, cyc):
url = argoplot.dashboard(wmo, url_only=True)
# txt = "You can check this float dashboard while we search for float profiles in the index: %s" % url
click.secho("\nYou can check this float dashboard while we search for float profile(s) in the index:")
Expand Down
81 changes: 81 additions & 0 deletions vfrecovery/command_line_interface/group_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import click
import logging
from argopy.utils import is_wmo, is_cyc, check_cyc, check_wmo
import argopy.plot as argoplot
from argopy.errors import DataNotFound
from argopy import ArgoIndex
import os
from pathlib import Path
import glob

from vfrecovery.core.plot import plot_velocity

root_logger = logging.getLogger("vfrecovery_root_logger")
blank_logger = logging.getLogger("vfrecovery_blank_logger")


@click.group()
def cli_group_plot() -> None:
pass


@cli_group_plot.command(
"plot",
short_help="Plot VirtualFleet-Recovery data or simulation results",
help="""
TARGET select what is to be plotted. A string in: 'velocity'.
WMO is the float World Meteorological Organisation number
CYC is the cycle number location to restrict plots to
""",
epilog="""
Examples:
\b
vfrecovery plot velocity 6903091 80
""", # noqa
)
@click.option(
"--log-level",
type=click.Choice(["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL", "QUIET"]),
default="INFO",
show_default=True,
help=(
"Set the details printed to console by the command "
"(based on standard logging library)."
),
)
@click.argument('TARGET', nargs=1, type=str)
@click.argument('WMO', nargs=1, type=int)
@click.argument("CYC", nargs=-1, type=int)
def plot(
target,
wmo,
cyc,
log_level,
) -> None:
if log_level == "QUIET":
root_logger.disabled = True
log_level = "CRITICAL"
root_logger.setLevel(level=getattr(logging, log_level.upper()))

if root_logger.isEnabledFor(logging.DEBUG):
root_logger.debug("DEBUG mode activated")

# Validate arguments:
if target.lower() not in ["all", "obs", "velocity"]:
raise ValueError("The first argument TARGET must be one in ['all', 'obs', 'velocity']")

assert is_wmo(wmo)
wmo = check_wmo(wmo)[0]
cyc = list(cyc)
if len(cyc) > 0:
assert is_cyc(cyc)
cyc = check_cyc(cyc)

if target == 'velocity':
plot_velocity(wmo, cyc,
log_level=log_level,
)
2 changes: 2 additions & 0 deletions vfrecovery/command_line_interface/virtualfleet_recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from vfrecovery.command_line_interface.group_describe import cli_group_describe
from vfrecovery.command_line_interface.group_predict import cli_group_predict
from vfrecovery.command_line_interface.group_plot import cli_group_plot

@click.command(
cls=click.CommandCollection,
sources=[
cli_group_describe,
cli_group_predict,
cli_group_plot,
],
context_settings=dict(help_option_names=["-h", "--help"]),
)
Expand Down
2 changes: 1 addition & 1 deletion vfrecovery/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# from deployment_plan import setup_deployment_plan
from .trajfile_handler import Trajectories
from .run_handler import Simulation
from .run_handler import RunAnalyser
# from predict import predict_function
36 changes: 21 additions & 15 deletions vfrecovery/core/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
root_logger = logging.getLogger("vfrecovery_root_logger")


def describe_function(
wmo: int,
cyc: Union[int, None],
log_level: str,
) -> str:
if log_level == "QUIET":
root_logger.disabled = True
log_level = "CRITICAL"
root_logger.setLevel(level=getattr(logging, log_level.upper()))
def describe_obs(wmo, cyc):

# Validate arguments:
assert is_wmo(wmo)
Expand All @@ -28,7 +20,6 @@ def describe_function(
assert is_cyc(cyc)
cyc = check_cyc(cyc)[0]


#
url = argoplot.dashboard(wmo, url_only=True)
txt = "You can check this float dashboard while we search for float profiles in the index: %s" % url
Expand All @@ -48,8 +39,23 @@ def describe_function(
df = df.sort_values(by='date')
root_logger.info("\n%s" % df.to_string(max_colwidth=15))

output = {'wmo': wmo, 'cyc': cyc}
json_dump = json.dumps(
output, sort_keys=False, indent=2
)
return json_dump
# output = {'wmo': wmo, 'cyc': cyc}
# json_dump = json.dumps(
# output, sort_keys=False, indent=2
# )
# return json_dump


def describe_function(
wmo: int,
cyc: Union[int, None],
target: str,
log_level: str,
) -> str:
if log_level == "QUIET":
root_logger.disabled = True
log_level = "CRITICAL"
root_logger.setLevel(level=getattr(logging, log_level.upper()))

if target == 'obs':
describe_obs(wmo, cyc)
66 changes: 66 additions & 0 deletions vfrecovery/core/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import logging
import xarray as xr
from virtualargofleet import Velocity

from vfrecovery.utils.misc import list_float_simulation_folders
import vfrecovery.plots.velocity as pltvel


root_logger = logging.getLogger("vfrecovery_root_logger")
plot_logger = logging.getLogger("vfrecovery_plot")


class log_this:

def __init__(self, txt, log_level):
"""Log text to simulation and possibly root logger(s)"""
getattr(root_logger, log_level.lower())(txt)
getattr(plot_logger, log_level.lower())(txt)

@staticmethod
def info(txt) -> 'log_this':
return log_this(txt, 'INFO')

@staticmethod
def debug(txt) -> 'log_this':
return log_this(txt, 'DEBUG')

@staticmethod
def warning(txt) -> 'log_this':
return log_this(txt, 'WARNING')

@staticmethod
def error(txt) -> 'log_this':
return log_this(txt, 'ERROR')


def plot_velocity(
wmo: int,
cyc: int,
log_level: str,
):
if log_level == "QUIET":
root_logger.disabled = True
log_level = "CRITICAL"
root_logger.setLevel(level=getattr(logging, log_level.upper()))

# List folders to examine:
plist = list_float_simulation_folders(wmo, cyc)

#
for c in plist.keys():
p = plist[c]
log_this.info("Velocity figure(s) for WMO=%s / CYC=%s:" % (wmo, c))
ilist = sorted(p.glob("velocity_*.png"))
if len(ilist) > 0:
[log_this.info("\t- %s" % i) for i in ilist]
else:
log_this.info("No velocity figures ! Generating new ones from velocity files")

# Load velocity field
vlist = sorted(p.glob("velocity_*.nc"))
for v in vlist:
log_this.info("Loading '%s'" % v)
# ds_vel = xr.open_dataset(v)
# VEL = Velocity(model='GLORYS12V1' if 'GLORYS' in str(v) else 'ARMOR3D', src=ds_vel)
# pltvel.plot(VEL, wmo, cyc, save_figure=False, workdir=p)
Loading

0 comments on commit efc6bbf

Please sign in to comment.