Skip to content

Commit

Permalink
Merge pull request #33 from adehecq/amaury
Browse files Browse the repository at this point in the history
Few updates for first RAGMAC submission
  • Loading branch information
adehecq authored Feb 3, 2022
2 parents 922c3bb + 0847bf7 commit 53c5827
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
12 changes: 9 additions & 3 deletions ragmac_xdem/mass_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def fill_ddem(
return filled_ddem


def fill_ddem_local_hypso(ddem, ref_dem, roi_mask, plot=True, outfig=None):
def fill_ddem_local_hypso(ddem, ref_dem, roi_mask, roi_outlines, plot=True, outfig=None):
"""
Function to fill gaps in ddems using a local hypsometric approach.
"""
Expand Down Expand Up @@ -159,13 +159,19 @@ def fill_ddem_local_hypso(ddem, ref_dem, roi_mask, plot=True, outfig=None):
ax3.tick_params(axis="x", colors=p3.patches[0].get_facecolor(), **tkw)

# ddem before interpolation
bounds = roi_outlines.bounds
pad = 2e3
ax2 = plt.subplot(132)
ddem.show(ax=ax2, cmap="coolwarm_r", vmin=-50, vmax=50)
roi_outlines.ds.plot(ax=ax2, facecolor="none", edgecolor="k", zorder=2)
ddem.show(ax=ax2, cmap="coolwarm_r", vmin=-50, vmax=50, zorder=1)
plt.xlim(bounds.left - pad, bounds.right + pad)
plt.ylim(bounds.bottom - pad, bounds.top + pad)
plt.title("dDEM before interpolation")

# ddem before interpolation
ax3 = plt.subplot(133, sharex=ax2, sharey=ax2)
ddem_filled.show(ax=ax3, cmap="coolwarm_r", vmin=-50, vmax=50)
roi_outlines.ds.plot(ax=ax3, facecolor="none", edgecolor="k", zorder=2)
ddem_filled.show(ax=ax3, cmap="coolwarm_r", vmin=-50, vmax=50, zorder=1)
plt.title("dDEM after interpolation")

if outfig is None:
Expand Down
7 changes: 4 additions & 3 deletions ragmac_xdem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def best_dem_cover(dem_path_list: list, init_stats: pd.Series) -> list[str, floa
# Select highest ROI coverage
best = stats_subset.sort_values(by="roi_cover_orig").iloc[-1]

return best.dem_path, best.roi_cover_orig
return best.dem_path, best.roi_cover_orig, best.dem_date


def list_pairs(validation_dates):
Expand Down Expand Up @@ -263,8 +263,9 @@ def dems_selection(
else:
assert init_stats is not None, "`init_stats` must be provided for mode 'best'"
final_dem_list = []
for group in output_list:
selected_dem, _ = best_dem_cover(group, init_stats)
for date, group in zip(validation_dates, output_list):
selected_dem, _, dem_date = best_dem_cover(group, init_stats)
print(f"For date {date}, selected DEM from date {dem_date}")
final_dem_list.append(
[
selected_dem,
Expand Down
18 changes: 13 additions & 5 deletions scripts/main_experiment1.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@

# -- Select different processing modes -- #
if args.mode == "median":
selection_opts = {"mode": "close", "dt": 365, "months": [8, 9, 10]}
selection_opts = {"mode": "close", "dt": 400, "months": [8, 9, 10]}
merge_opts = {"mode": "median"}
outdir = os.path.join(exp["processed_data"]["directory"], "results_median")
downsampling = 1
method = "DEMdiff"
method = "DEMdiff_median"
elif args.mode == "best":
selection_opts = {"mode": "best", "dt": 400, "months": [8, 9, 10]}
merge_opts = {"mode": "median"}
outdir = os.path.join(exp["processed_data"]["directory"], "results_best")
downsampling = 1
method = "DEMdiff_best"
elif args.mode == "shean":
selection_opts = {"mode": "subperiod", "dt": 365}
downsampling = 1
Expand All @@ -91,7 +97,7 @@
outdir = os.path.join(exp["processed_data"]["directory"], "results_knuth")
method = "TimeSeries2"
else:
raise ValueError("`mode` must be either of 'median','shean' or 'knuth'")
raise ValueError("`mode` must be either of 'median', 'best', 'shean' or 'knuth'")

# create output directories
if not os.path.exists(coreg_dir):
Expand All @@ -111,6 +117,8 @@
# -- Select DEMs to be processed -- #
print("\n### DEMs selection ###")
validation_dates = exp["validation_dates"]
if selection_opts["mode"] == 'best':
selection_opts["init_stats"] = init_stats
groups = utils.dems_selection(dems_files, validation_dates=validation_dates, **selection_opts)

# -- Postprocess DEMs i.e. coregister, filter etc -- #
Expand All @@ -126,7 +134,7 @@
plot=True,
method="mp",
)
print(f"--> Coregistered DEMs saved in {outdir}")
print(f"--> Coregistered DEMs saved in {coreg_dir}")

# Temporarily downsample DEM for speeding-up testing
if downsampling > 1:
Expand Down Expand Up @@ -170,7 +178,7 @@
print(pair_id)
fig_fn = os.path.join(outdir, f"{pair_id}_mb_fig.png")
ddem_filled, ddem_bins = mb.fill_ddem_local_hypso(
ddems[pair_id], ref_dem, roi_mask, plot=True, outfig=fig_fn
ddems[pair_id], ref_dem, roi_mask, roi_outlines, plot=True, outfig=fig_fn
)
ddems_filled[pair_id] = ddem_filled

Expand Down
14 changes: 11 additions & 3 deletions scripts/main_experiment2.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@

# -- Select different processing modes -- #
if args.mode == "median":
selection_opts = {"mode": "close", "dt": 365, "months": [8, 9, 10]}
selection_opts = {"mode": "close", "dt": 400, "months": [8, 9, 10]}
merge_opts = {"mode": "median"}
outdir = os.path.join(baltoro_paths["processed_data"]["directory"], "results_median")
downsampling = 1
method = "DEMdiff"
method = "DEMdiff_median"
elif args.mode == "best":
selection_opts = {"mode": "best", "dt": 400, "months": [8, 9, 10]}
merge_opts = {"mode": "median"}
outdir = os.path.join(baltoro_paths["processed_data"]["directory"], "results_best")
downsampling = 1
method = "DEMdiff_best"
elif args.mode == "shean":
selection_opts = {"mode": "subperiod", "dt": 365}
downsampling = 1
Expand Down Expand Up @@ -106,6 +112,8 @@
# -- Select DEMs to be processed -- #
print("\n### DEMs selection ###")
validation_dates = baltoro_paths["validation_dates"]
if selection_opts["mode"] == 'best':
selection_opts["init_stats"] = init_stats
groups = utils.dems_selection(dems_files, validation_dates=validation_dates, **selection_opts)

# -- Postprocess DEMs i.e. coregister, filter etc -- #
Expand Down Expand Up @@ -167,7 +175,7 @@
print(pair_id)
fig_fn = os.path.join(outdir, f"{pair_id}_mb_fig.png")
ddem_filled, ddem_bins = mb.fill_ddem_local_hypso(
ddems[pair_id], ref_dem, roi_mask, plot=True, outfig=fig_fn
ddems[pair_id], ref_dem, roi_mask, roi_outlines, plot=True, outfig=fig_fn
)
ddems_filled[pair_id] = ddem_filled

Expand Down

0 comments on commit 53c5827

Please sign in to comment.