-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add precipitation histogram to prognostic run report #1271
Merged
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
98e8061
Add OrderedList class to report
41976a2
Add diagnostic of precipitation histogram
df2db9a
Stop requiring two runs if making report from folder
56bc134
Better OrderedList API
ebccbef
Set histogram diagnostic coordinate to bin midpoints
29a8f80
Add process.html page with diurnal cycle and precip PDF
53ce8fd
Remove obsolete failing units tests
2bc64e1
Merge branch 'master' into feature/process-page
7085fc9
Case HISTOGRAM_BINS.keys() to list
095423d
Save bin widths to allow accurate cdf calculation
02d5be2
Add metric for various percentiles
5647c1e
Delete unused constant
eb099c9
Rename function to not be duplicated
caec3f9
Fix navigation link
ee55d10
Add table of precip percentiles to process page
0fa128f
Refactor compute_percentile function and add tests
48e9f6a
Add docstring
92edd8c
Refactor histogram calc to new function in VCM
c3c476b
Fix lint and typechecking
7097de2
Compute 3-hourly mean before histogram
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
from .create_report import create_html, insert_report_figure, Metrics, Metadata, Link | ||
from .create_report import ( | ||
create_html, | ||
insert_report_figure, | ||
Metrics, | ||
Metadata, | ||
Link, | ||
OrderedList, | ||
) | ||
|
||
__version__ = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,12 @@ | |
grouping contains outputs from the physics routines (`sfc_dt_atmos.tile*.nc` and | ||
`diags.zarr`). | ||
""" | ||
import os | ||
import sys | ||
|
||
import datetime | ||
import tempfile | ||
import intake | ||
import numpy as np | ||
import xarray as xr | ||
import shutil | ||
from dask.diagnostics import ProgressBar | ||
import fsspec | ||
|
||
|
@@ -37,6 +34,7 @@ | |
from fv3net.diagnostics.prognostic_run import diurnal_cycle | ||
from fv3net.diagnostics.prognostic_run import transform | ||
from fv3net.diagnostics.prognostic_run.constants import ( | ||
HISTOGRAM_BINS, | ||
HORIZONTAL_DIMS, | ||
DiagArg, | ||
GLOBAL_AVERAGE_DYCORE_VARS, | ||
|
@@ -229,16 +227,6 @@ def _assign_diagnostic_time_attrs( | |
return diagnostics_ds | ||
|
||
|
||
def dump_nc(ds: xr.Dataset, f): | ||
# to_netcdf closes file, which will delete the buffer | ||
# need to use a buffer since seek doesn't work with GCSFS file objects | ||
with tempfile.TemporaryDirectory() as dirname: | ||
url = os.path.join(dirname, "tmp.nc") | ||
ds.to_netcdf(url, engine="h5netcdf") | ||
with open(url, "rb") as tmp1: | ||
shutil.copyfileobj(tmp1, f) | ||
|
||
|
||
@add_to_diags("dycore") | ||
@diag_finalizer("rms_global") | ||
@transform.apply("resample_time", "3H", inner_join=True) | ||
|
@@ -503,6 +491,34 @@ def _diurnal_func( | |
return _assign_diagnostic_time_attrs(diag, prognostic) | ||
|
||
|
||
@add_to_diags("physics") | ||
@diag_finalizer("histogram") | ||
@transform.apply("resample_time", "3H", inner_join=True) | ||
@transform.apply("subset_variables", HISTOGRAM_BINS.keys()) | ||
def compute_histogram(prognostic, verification, grid): | ||
oliverwm1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.info("Computing histograms for physics diagnostics") | ||
counts = xr.Dataset() | ||
for varname in prognostic.data_vars: | ||
# bins = HISTOGRAM_BINS[varname] | ||
# counts[varname] = prognostic.groupby_bins(varname, bins).count()[varname] | ||
# bin_midpoints = [x.item().mid for x in counts[f"{varname}_bins"]] | ||
# counts = counts.assign_coords({f"{varname}_bins": bin_midpoints}) | ||
# counts[varname] /= counts[varname].sum() | ||
# counts[varname] /= bins[1:] - bins[:1] | ||
# counts[varname].attrs["units"] = f"({prognostic[varname].units})^-1" | ||
# counts[varname].attrs["long_name"] = "Frequency" | ||
# counts[f"{varname}_bins"].attrs = prognostic[varname].attrs | ||
count, bins = np.histogram( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found using |
||
prognostic[varname], bins=HISTOGRAM_BINS[varname], density=True | ||
) | ||
bin_midpoints = 0.5 * (bins[:-1] + bins[1:]) | ||
coords = {f"{varname}_bins": bin_midpoints} | ||
count_da = xr.DataArray(count, coords=coords, dims=list(coords.keys())) | ||
count_da[f"{varname}_bins"].attrs["units"] = prognostic[varname].units | ||
counts[varname] = count_da | ||
return _assign_diagnostic_time_attrs(counts, prognostic) | ||
|
||
|
||
def register_parser(subparsers): | ||
parser = subparsers.add_parser("save", help="Compute the prognostic run diags.") | ||
parser.add_argument("url", help="Prognostic run output location.") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,14 @@ | |
RunMetrics, | ||
) | ||
|
||
from report import create_html, Link | ||
from report import create_html, Link, OrderedList | ||
from report.holoviews import HVPlot, get_html_header | ||
from .matplotlib import plot_2d_matplotlib, plot_cubed_sphere_map, raw_html | ||
from .matplotlib import ( | ||
plot_2d_matplotlib, | ||
plot_cubed_sphere_map, | ||
raw_html, | ||
plot_histogram, | ||
) | ||
|
||
import logging | ||
|
||
|
@@ -75,9 +80,7 @@ def make_plots(self, data) -> Iterable: | |
yield func(data) | ||
|
||
|
||
def plot_1d( | ||
run_diags: RunDiagnostics, varfilter: str, run_attr_name: str = "run", | ||
) -> HVPlot: | ||
def plot_1d(run_diags: RunDiagnostics, varfilter: str) -> HVPlot: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleted unused argument |
||
"""Plot all diagnostics whose name includes varfilter. Plot is overlaid across runs. | ||
All matching diagnostics must be 1D.""" | ||
p = hv.Cycle("Colorblind") | ||
|
@@ -95,10 +98,7 @@ def plot_1d( | |
|
||
|
||
def plot_1d_min_max_with_region_bar( | ||
run_diags: RunDiagnostics, | ||
varfilter_min: str, | ||
varfilter_max: str, | ||
run_attr_name: str = "run", | ||
run_diags: RunDiagnostics, varfilter_min: str, varfilter_max: str, | ||
) -> HVPlot: | ||
"""Plot all diagnostics whose name includes varfilter. Plot is overlaid across runs. | ||
All matching diagnostics must be 1D.""" | ||
|
@@ -123,9 +123,7 @@ def plot_1d_min_max_with_region_bar( | |
return HVPlot(_set_opts_and_overlay(hmap)) | ||
|
||
|
||
def plot_1d_with_region_bar( | ||
run_diags: RunDiagnostics, varfilter: str, run_attr_name: str = "run" | ||
) -> HVPlot: | ||
def plot_1d_with_region_bar(run_diags: RunDiagnostics, varfilter: str) -> HVPlot: | ||
"""Plot all diagnostics whose name includes varfilter. Plot is overlaid across runs. | ||
Region will be selectable through a drop-down bar. Region is assumed to be part of | ||
variable name after last underscore. All matching diagnostics must be 1D.""" | ||
|
@@ -189,6 +187,7 @@ def diurnal_component_plot( | |
hovmoller_plot_manager = PlotManager() | ||
zonal_pressure_plot_manager = PlotManager() | ||
diurnal_plot_manager = PlotManager() | ||
histogram_plot_manager = PlotManager() | ||
|
||
# this will be passed the data from the metrics.json files | ||
metrics_plot_manager = PlotManager() | ||
|
@@ -291,6 +290,11 @@ def diurnal_cycle_component_plots(diagnostics: Iterable[xr.Dataset]) -> HVPlot: | |
return diurnal_component_plot(diagnostics) | ||
|
||
|
||
@histogram_plot_manager.register | ||
def histogram_plots(diagnostics: Iterable[xr.Dataset]) -> HVPlot: | ||
return plot_histogram(diagnostics, "total_precip_to_surface_histogram") | ||
|
||
|
||
# Routines for plotting the "metrics" | ||
# New plotting routines can be registered here. | ||
@metrics_plot_manager.register | ||
|
@@ -325,20 +329,21 @@ def generic_metric_plot(metrics: RunMetrics, metric_type: str) -> hv.HoloMap: | |
return HVPlot(hmap.opts(**bar_opts)) | ||
|
||
|
||
navigation = [ | ||
navigation = OrderedList( | ||
Link("Home", "index.html"), | ||
Link("Process diagnostics", "process.html"), | ||
Link("Latitude versus time hovmoller", "hovmoller.html"), | ||
Link("Time-mean maps", "maps.html"), | ||
Link("Time-mean zonal-pressure profiles", "zonal_pressure.html"), | ||
] | ||
) | ||
navigation = [navigation] # must be iterable for Jinja HTML template | ||
|
||
|
||
def render_index(metadata, diagnostics, metrics, movie_links): | ||
sections_index = { | ||
"Links": navigation, | ||
"Timeseries": list(timeseries_plot_manager.make_plots(diagnostics)), | ||
"Zonal mean": list(zonal_mean_plot_manager.make_plots(diagnostics)), | ||
"Diurnal cycle": list(diurnal_plot_manager.make_plots(diagnostics)), | ||
} | ||
|
||
if not metrics.empty: | ||
|
@@ -398,6 +403,20 @@ def render_zonal_pressures(metadata, diagnostics): | |
) | ||
|
||
|
||
def render_process_diagnostics(metadata, diagnostics): | ||
sections = { | ||
"Links": navigation, | ||
"Diurnal cycle": list(diurnal_plot_manager.make_plots(diagnostics)), | ||
"Precipitation histogram": list(histogram_plot_manager.make_plots(diagnostics)), | ||
} | ||
return create_html( | ||
title="Process diagnostics", | ||
metadata=metadata, | ||
sections=sections, | ||
html_header=get_html_header(), | ||
) | ||
|
||
|
||
def _html_link(url, tag): | ||
return f"<a href='{url}'>{tag}</a>" | ||
|
||
|
@@ -427,6 +446,7 @@ def make_report(computed_diagnostics: ComputedDiagnosticsList, output): | |
"hovmoller.html": render_hovmollers(metadata, diagnostics), | ||
"maps.html": render_maps(metadata, diagnostics, metrics), | ||
"zonal_pressure.html": render_zonal_pressures(metadata, diagnostics), | ||
"process_diagnostics.html": render_process_diagnostics(metadata, diagnostics), | ||
} | ||
|
||
for filename, html in pages.items(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We switched to using the
vcm.dump_nc
version of this a while ago, so this func is unused.