Skip to content
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

bringing units to the colorscale #475

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pepsico/app_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def unit_conversion(variable):
variable.attrs['units'] = 'mm/day' #rename unit to converted
elif variable.name in ['tas', 'tasmin', 'tasmax']:
variable -= 273.15
variable.attrs['units'] = 'Celsius'
variable.attrs['units'] = '˚C'
elif variable.name == 'prsn':
#this is really needed by the colorscale ticks
variable *= 10e6
Expand Down
12 changes: 5 additions & 7 deletions pepsico/projections/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,15 @@ def map_layout():
[dlf.Marker(id="loc_marker", position=(0, 0))],
id="layers_group"
),
dlf.ScaleControl(imperial=False, position="bottomleft"),
dlf.ScaleControl(imperial=False, position="bottomright"),
dlf.Colorbar(
id="colorbar",
position="bottomleft",
width=300,
height=10,
min=0,
max=1,
nTicks=11,
nTicks=9,
opacity=1,
tooltip=True,
position="topright",
width=10,
height=300,
className="p-1",
style={
"background": "white", "border-style": "inset",
Expand Down
67 changes: 35 additions & 32 deletions pepsico/projections/maproom.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def plot_ts(ts, name, color, start_format, units):
x=ts["T"].dt.strftime(STD_TIME_FORMAT),
y=ts.values,
customdata=ts["seasons_ends"].dt.strftime("%B %Y"),
hovertemplate=("%{x|"+start_format+"}%{customdata}: %{y:.2f}" + units),
hovertemplate=("%{x|"+start_format+"}%{customdata}: %{y:.2f} " + units),
name=name,
line=pgo.scatter.Line(color=color),
connectgaps=False,
Expand Down Expand Up @@ -288,10 +288,6 @@ def local_plots(marker_pos, region, n_clicks, model, variable, start_month, end_
start_format = "%b %Y - "
else:
start_format = "%b-"
if data_ds["histo"].attrs["units"] == "Celsius" :
units = "˚C"
else:
units = data_ds["histo"].attrs["units"]
local_graph = pgo.Figure()
data_color = {
"histo": "blue", "picontrol": "green",
Expand All @@ -301,11 +297,15 @@ def local_plots(marker_pos, region, n_clicks, model, variable, start_month, end_
lat_units = "˚N" if (lat >= 0) else "˚S"
for var in data_ds.data_vars:
local_graph.add_trace(plot_ts(
data_ds[var], var, data_color[var], start_format, units
data_ds[var], var, data_color[var], start_format,
data_ds[var].attrs["units"]
))
local_graph.update_layout(
xaxis_title="Time",
yaxis_title=f'{data_ds["histo"].attrs["long_name"]} ({units})',
yaxis_title=(
f'{data_ds["histo"].attrs["long_name"]} '
f'({data_ds["histo"].attrs["units"]})'
),
title={
"text": (
f'{data_ds["histo"]["T"].dt.strftime("%b")[0].values}-'
Expand Down Expand Up @@ -401,28 +401,31 @@ def seasonal_change(
ac.read_data("historical", model, variable, region, unit_convert=True),
start_month, end_month,
start_year=start_year_ref, end_year=end_year_ref,
).mean(dim="T")
).mean(dim="T", keep_attrs=True)
data = ac.seasonal_data(
ac.read_data(scenario, model, variable, region, unit_convert=True),
start_month, end_month,
start_year=start_year, end_year=end_year,
).mean(dim="T")
data = data - ref
).mean(dim="T", keep_attrs=True)
#Tedious way to make a subtraction only to keep attributes
data = xr.apply_ufunc(
np.subtract, data, ref, dask="allowed", keep_attrs="drop_conflicts",
)
if variable in ["hurs", "huss", "pr"]:
data = 100. * data / ref
data["units"] = "%"
data.attrs["units"] = "%"
return data.rename({"X": "lon", "Y": "lat"})


def map_attributes(variable, data=None):
def map_attributes(data):
variable = data.name
if variable in ["tas", "tasmin", "tasmax"]:
colorscale = CMAPS["temp_anomaly"]
elif variable in ["hurs", "huss"]:
colorscale = CMAPS["prcp_anomaly"].rescaled(-30, 30)
elif variable in ["pr"]:
colorscale = CMAPS["prcp_anomaly"].rescaled(-100, 100)
else:
assert (data is not None)
map_amp = np.max(np.abs(data)).values
if variable in ["prsn"]:
colorscale = CMAPS["prcp_anomaly_blue"]
Expand All @@ -438,6 +441,7 @@ def map_attributes(variable, data=None):
Output("colorbar", "colorscale"),
Output("colorbar", "min"),
Output("colorbar", "max"),
Output("colorbar", "unit"),
Input("region", "value"),
Input("submit_controls","n_clicks"),
State("scenario", "value"),
Expand All @@ -463,23 +467,20 @@ def draw_colorbar(
start_year_ref,
end_year_ref,
):
if variable in ["tas", "tasmin", "tasmax", "hurs", "huss", "pr"]:
data = None
else:
data = seasonal_change(
scenario,
model,
variable,
region,
ac.strftimeb2int(start_month),
ac.strftimeb2int(end_month),
int(start_year),
int(end_year),
int(start_year_ref),
int(end_year_ref),
)
colorscale, map_min, map_max = map_attributes(variable, data=data)
return colorscale.to_dash_leaflet(), map_min, map_max
data = seasonal_change(
scenario,
model,
variable,
region,
ac.strftimeb2int(start_month),
ac.strftimeb2int(end_month),
int(start_year),
int(end_year),
int(start_year_ref),
int(end_year_ref),
)
colorbar, min, max = map_attributes(data)
return colorbar.to_dash_leaflet(), min, max, data.attrs["units"]


@APP.callback(
Expand Down Expand Up @@ -588,7 +589,9 @@ def fcst_tiles(tz, tx, ty,
int(end_year_ref),
)
(
data.attrs["colormap"], data.attrs["scale_min"], data.attrs["scale_max"]
) = map_attributes(variable, data=data)
data.attrs["colormap"],
data.attrs["scale_min"],
data.attrs["scale_max"],
) = map_attributes(data)
resp = pingrid.tile(data, tx, ty, tz)
return resp