#!/usr/bin/env python # coding: utf-8 # # Test `sandheads_winds` Module # # Render figure object produced by the `nowcast.figures.comparison.sandheads_winds` module. # # Set-up and function call replicates as nearly as possible what is done in the `nowcast.workers.make_plots` worker # to help ensure that the module will work in the nowcast production context. # Notebooks like this should be developed in a # [Nowcast Figures Development Environment](https://salishsea-nowcast.readthedocs.io/en/latest/figures/fig_dev_env.html) # so that all of the necessary dependency packages are installed. # The development has to be done on a workstation that has the Salish Sea Nowcast system `/results/` partition mounted. # In[1]: # reload lets us pull in changes from modules that we edit from importlib import reload # In[2]: # imports from the top of the make_plots worker import datetime from glob import glob import logging import os from pathlib import Path import matplotlib import arrow import cmocean from nemo_nowcast import NowcastWorker import netCDF4 as nc import scipy.io as sio from nowcast import lib from nowcast.figures.comparison import sandheads_winds # In[3]: # some extra imports that we need to simulate how the nowcast system works import io import yaml # In[4]: # render figure objects in the notebook get_ipython().run_line_magic('matplotlib', 'inline') # ## Simulate Nowcast System Config # # The `config` multiline string contains the pieces of `nowcast/config/nowcast.yaml` # that are needed to set up the conditions as `nowcast.workers.make_plots` does # before calling the figure module `make_figure()` function. # # The `yaml.load()` call at the bottom of the cell converts the string into a `config` dict. # In[5]: config = ''' run_types: nowcast: bathymetry: /results/nowcast-sys/grid/bathymetry_201702.nc mesh_mask: /results/nowcast-sys/grid/mesh_mask201702.nc forecast2: bathymetry: /results/nowcast-sys/grid/bathymetry_201702.nc mesh_mask: /results/nowcast-sys/grid/mesh_mask201702.nc figures: coastline: /ocean/rich/more/mmapbase/bcgeo/PNW.mat dataset URLs: HRDPS fields: https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSaSurfaceAtmosphereFieldsV1 run: results_archive: nowcast: /results/SalishSea/nowcast-blue/ forecast: /results/SalishSea/forecast2/ ''' config = yaml.load(io.StringIO(config)) # ## Simulate the `make_plots` Command-line Interface # # `make_plots` gets `run_type`, `plot_type`, and `run_date` from the command-line. # In[6]: run_type = 'nowcast' plot_type = 'comparison' run_date = arrow.get('2018-10-14') # ## Simulate the Set-up of Variables and Paths in the `make_plots.make_plots()` Function # # The `make_plots()` function uses paths defined in the nowcast system configuration file # to set up a collection of commonly used variables, paths, and datasets. # These are just the ones that we need for our test. # In[7]: dmy = run_date.format('DDMMMYY').lower() coastline = sio.loadmat(config['figures']['coastline']) # ## Simulate Dataset URL Lookup in `_prep_comparison_fig_functions()` # In[8]: hrdps_dataset_url = config['figures']['dataset URLs']['HRDPS fields'] # ## Render the Figure # # The `%%timeit` cell magic lets us keep an eye on how log the figure takes to process. # Setting `-n1 -r1` prevents it from processing the figure more than once # as it might try to do to generate better statistics. # # The `reload()` calls reload imported modules so that any edits you have made will take effect here. # # The `make_figure()` call should be the same as goes in the `make_plots` worker module. # In[9]: get_ipython().run_cell_magic('timeit', '-n1 -r1', '\nfrom salishsea_tools import places, stormtools\nfrom nowcast.figures import shared\nfrom nowcast.figures import website_theme\nreload(places)\nreload(stormtools)\nreload(shared)\nreload(website_theme)\nreload(sandheads_winds)\n\nfig = sandheads_winds.make_figure(\n hrdps_dataset_url, run_type, run_date, coastline,\n)\n') # In[ ]: