From 1fa815ff8f9629de5f900a19d9b0a6a63115c493 Mon Sep 17 00:00:00 2001 From: remic Date: Wed, 19 Apr 2023 16:09:52 -0400 Subject: [PATCH 1/4] taw read from file depends on X/Y instead of constant --- enacts/config-sng.yaml | 1 + enacts/wat_bal/agronomy.py | 3 +-- enacts/wat_bal/maproom_monit.py | 30 +++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/enacts/config-sng.yaml b/enacts/config-sng.yaml index 428c4917b..edcfa47a4 100644 --- a/enacts/config-sng.yaml +++ b/enacts/config-sng.yaml @@ -80,4 +80,5 @@ wat_bal_monit: crop_name: Rice kc_v: [0, 0, 1.1, 1.1, 0] kc_l: [3, 27, 45, 60] + taw_file: /data/remic/mydatafiles/soilgrids/Senegal/GYGA_ERZD_wat_cap_abs_anacim_enacts3.nc diff --git a/enacts/wat_bal/agronomy.py b/enacts/wat_bal/agronomy.py index ae83881c7..85737c457 100644 --- a/enacts/wat_bal/agronomy.py +++ b/enacts/wat_bal/agronomy.py @@ -59,7 +59,6 @@ def soil_plant_water_step( .. math:: drainage = |wb - sm| """ - # Water Balance wb = (sm_yesterday + peffective - et).clip(min=0) drainage = (wb - taw).clip(min=0) @@ -500,4 +499,4 @@ def solar_radiation(doy, lat): / np.pi ).rename("ra") ra.attrs = dict(description="Extraterrestrial Radiation", units="MJ/m**2/day") - return ra \ No newline at end of file + return ra diff --git a/enacts/wat_bal/maproom_monit.py b/enacts/wat_bal/maproom_monit.py index 3af9cecd6..162800551 100644 --- a/enacts/wat_bal/maproom_monit.py +++ b/enacts/wat_bal/maproom_monit.py @@ -44,6 +44,16 @@ DR_PATH = f"{GLOBAL_CONFIG['daily']['zarr_path']}{DATA_PATH}" RR_MRG_ZARR = Path(DR_PATH) rr_mrg = calc.read_zarr_data(RR_MRG_ZARR) +#rr_mrg["X"] = rr_mrg["X"].astype(np.single) +#rr_mrg["Y"] = rr_mrg["Y"].astype(np.single) + +kaka, taw = xr.align( + rr_mrg, + xr.open_dataarray(Path(CONFIG["taw_file"])), + join="override", + exclude="T", +) +taw_max = taw.max() # Assumes that grid spacing is regular and cells are square. When we # generalize this, don't make those assumptions. @@ -364,12 +374,13 @@ def wat_bal_plots( error_fig = pingrid.error_fig(error_msg="Grid box out of data domain") return error_fig precip.load() + taw = pingrid.sel_snap(xr.open_dataarray(Path(CONFIG["taw_file"])), lat, lng) try: sm, drainage, et_crop, et_crop_red, planting_date = ag.soil_plant_water_balance( precip, et=5, - taw=60, - sminit=60./3., + taw=taw, + sminit=taw/3., kc_params=kc_params, planting_date=p_d, ) @@ -459,14 +470,19 @@ def wat_bal_tile(tz, tx, ty): ) mymap_min = 0 - mymap_max = 60 #taw.max() + mymap_max = 8* (int(taw_max.values) // 8) mycolormap = CMAPS["precip"] + taw_tile = taw.sel( + X=slice(x_min - x_min % RESOLUTION, x_max + RESOLUTION - x_max % RESOLUTION), + Y=slice(y_min - y_min % RESOLUTION, y_max + RESOLUTION - y_max % RESOLUTION), + ).compute() + sm, drainage, et_crop, et_crop_red, planting_date = ag.soil_plant_water_balance( precip_tile, et=5, - taw=60, - sminit=60./3., + taw=taw_tile, + sminit=taw_tile/3., kc_params=kc_params, planting_date=p_d, ) @@ -497,12 +513,12 @@ def wat_bal_tile(tz, tx, ty): def set_colorbar( map_choice, ): - mymap_max = 60 #taw.max() + mymap_max = 8 * (int(taw_max.values) // 8) return ( f"{CONFIG['map_text'][map_choice]['menu_label']} [{CONFIG['map_text'][map_choice]['units']}]", CMAPS["precip"].to_dash_leaflet(), mymap_max, - [i for i in range(0, mymap_max + 1) if i % int(mymap_max/12) == 0], + [i for i in range(0, mymap_max + 1) if i % int(mymap_max/8) == 0], ) From 7c35391a967e11345a7d9d4aeb1f1c75b8d73ef9 Mon Sep 17 00:00:00 2001 From: remic Date: Mon, 1 May 2023 09:31:53 -0400 Subject: [PATCH 2/4] taw_max in config thus taw file reading always in callbacks --- enacts/config-sng.yaml | 1 + enacts/wat_bal/maproom_monit.py | 22 +++++++++------------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/enacts/config-sng.yaml b/enacts/config-sng.yaml index edcfa47a4..b98af7af1 100644 --- a/enacts/config-sng.yaml +++ b/enacts/config-sng.yaml @@ -81,4 +81,5 @@ wat_bal_monit: kc_v: [0, 0, 1.1, 1.1, 0] kc_l: [3, 27, 45, 60] taw_file: /data/remic/mydatafiles/soilgrids/Senegal/GYGA_ERZD_wat_cap_abs_anacim_enacts3.nc + taw_max: 136 diff --git a/enacts/wat_bal/maproom_monit.py b/enacts/wat_bal/maproom_monit.py index 162800551..c663963e2 100644 --- a/enacts/wat_bal/maproom_monit.py +++ b/enacts/wat_bal/maproom_monit.py @@ -44,16 +44,6 @@ DR_PATH = f"{GLOBAL_CONFIG['daily']['zarr_path']}{DATA_PATH}" RR_MRG_ZARR = Path(DR_PATH) rr_mrg = calc.read_zarr_data(RR_MRG_ZARR) -#rr_mrg["X"] = rr_mrg["X"].astype(np.single) -#rr_mrg["Y"] = rr_mrg["Y"].astype(np.single) - -kaka, taw = xr.align( - rr_mrg, - xr.open_dataarray(Path(CONFIG["taw_file"])), - join="override", - exclude="T", -) -taw_max = taw.max() # Assumes that grid spacing is regular and cells are square. When we # generalize this, don't make those assumptions. @@ -470,10 +460,16 @@ def wat_bal_tile(tz, tx, ty): ) mymap_min = 0 - mymap_max = 8* (int(taw_max.values) // 8) + mymap_max = CONFIG["taw_max"] mycolormap = CMAPS["precip"] - taw_tile = taw.sel( + garbage, taw_tile = xr.align( + precip, + xr.open_dataarray(Path(CONFIG["taw_file"])), + join="override", + exclude="T", + ) + taw_tile = taw_tile.sel( X=slice(x_min - x_min % RESOLUTION, x_max + RESOLUTION - x_max % RESOLUTION), Y=slice(y_min - y_min % RESOLUTION, y_max + RESOLUTION - y_max % RESOLUTION), ).compute() @@ -513,7 +509,7 @@ def wat_bal_tile(tz, tx, ty): def set_colorbar( map_choice, ): - mymap_max = 8 * (int(taw_max.values) // 8) + mymap_max = CONFIG["taw_max"] return ( f"{CONFIG['map_text'][map_choice]['menu_label']} [{CONFIG['map_text'][map_choice]['units']}]", CMAPS["precip"].to_dash_leaflet(), From 8712ff08dd8f0b916dc9b8bbfb11098a1060a628 Mon Sep 17 00:00:00 2001 From: remic Date: Mon, 1 May 2023 09:44:36 -0400 Subject: [PATCH 3/4] cosmetics --- enacts/config-defaults.yaml | 1 + enacts/wat_bal/maproom_monit.py | 39 ++++++++++++++------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/enacts/config-defaults.yaml b/enacts/config-defaults.yaml index 2f8877fbf..364f35c46 100644 --- a/enacts/config-defaults.yaml +++ b/enacts/config-defaults.yaml @@ -102,3 +102,4 @@ wat_bal_monit: # menu_label: Crop Evapotranspiration # description: The map shows the crop evapotranspiration... # units: mm + diff --git a/enacts/wat_bal/maproom_monit.py b/enacts/wat_bal/maproom_monit.py index c663963e2..bc205d498 100644 --- a/enacts/wat_bal/maproom_monit.py +++ b/enacts/wat_bal/maproom_monit.py @@ -380,16 +380,16 @@ def wat_bal_plots( ) return error_fig if map_choice == "sm": - myts = sm + ts = sm elif map_choice == "drainage": - myts = drainage + ts = drainage elif map_choice == "et_crop": - myts = et_crop + ts = et_crop wat_bal_graph = pgo.Figure() wat_bal_graph.add_trace( pgo.Scatter( - x=myts["T"].dt.strftime("%-d %b %y"), - y=myts.values, + x=ts["T"].dt.strftime("%-d %b %y"), + y=ts.values, hovertemplate="%{y} on %{x}", name="", line=pgo.scatter.Line(color="blue"), @@ -459,10 +459,6 @@ def wat_bal_tile(tz, tx, ty): data=[kc_init, kc_veg, kc_mid, kc_late, kc_end], dims=["kc_periods"], coords=[kc_periods] ) - mymap_min = 0 - mymap_max = CONFIG["taw_max"] - mycolormap = CMAPS["precip"] - garbage, taw_tile = xr.align( precip, xr.open_dataarray(Path(CONFIG["taw_file"])), @@ -483,20 +479,19 @@ def wat_bal_tile(tz, tx, ty): planting_date=p_d, ) if map_choice == "sm": - mymap = sm + map = sm elif map_choice == "drainage": - mymap = drainage + map = drainage elif map_choice == "et_crop": - mymap = et_crop + map = et_crop else: raise Exception("can not enter here") - mymap = mymap.isel(T=-1) - mymap.attrs["colormap"] = mycolormap - mymap = mymap.rename(X="lon", Y="lat") - mymap.attrs["scale_min"] = mymap_min - mymap.attrs["scale_max"] = mymap_max - result = pingrid.tile(mymap, tx, ty, tz, clip_shape) - return result + map = map.isel(T=-1) + map.attrs["colormap"] = CMAPS["precip"] + map = map.rename(X="lon", Y="lat") + map.attrs["scale_min"] = 0 + map.attrs["scale_max"] = CONFIG["taw_max"] + return pingrid.tile(map, tx, ty, tz, clip_shape) @APP.callback( @@ -509,12 +504,12 @@ def wat_bal_tile(tz, tx, ty): def set_colorbar( map_choice, ): - mymap_max = CONFIG["taw_max"] + map_max = CONFIG["taw_max"] return ( f"{CONFIG['map_text'][map_choice]['menu_label']} [{CONFIG['map_text'][map_choice]['units']}]", CMAPS["precip"].to_dash_leaflet(), - mymap_max, - [i for i in range(0, mymap_max + 1) if i % int(mymap_max/8) == 0], + map_max, + [i for i in range(0, map_max + 1) if i % int(map_max/8) == 0], ) From f41d5b5cf365277beafff6a8d9507a0ef4a9ba05 Mon Sep 17 00:00:00 2001 From: remic Date: Tue, 2 May 2023 10:16:23 -0400 Subject: [PATCH 4/4] it's conventional to use the name _ (underscore) for a return value you don't intend to use --- enacts/wat_bal/maproom_monit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enacts/wat_bal/maproom_monit.py b/enacts/wat_bal/maproom_monit.py index bc205d498..e17917bcc 100644 --- a/enacts/wat_bal/maproom_monit.py +++ b/enacts/wat_bal/maproom_monit.py @@ -459,7 +459,7 @@ def wat_bal_tile(tz, tx, ty): data=[kc_init, kc_veg, kc_mid, kc_late, kc_end], dims=["kc_periods"], coords=[kc_periods] ) - garbage, taw_tile = xr.align( + _, taw_tile = xr.align( precip, xr.open_dataarray(Path(CONFIG["taw_file"])), join="override",