Skip to content

Commit e28742f

Browse files
committed
add vertical mean ts
1 parent 758b2d4 commit e28742f

1 file changed

Lines changed: 105 additions & 65 deletions

File tree

oceanval/summarize.py

Lines changed: 105 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import random
1212
import pandas as pd
1313
import re
14+
import dill
1415

1516
from shutil import copyfile
1617
from oceanval.session import session_info
@@ -512,7 +513,6 @@ def summarize(
512513
# Check if any summaries have been defined
513514
if len(summaries.keys) == 0:
514515
raise ValueError("You do not appear to have defined any summary variables! Use add_summary() to define them.")
515-
516516

517517
# Create output directory
518518
summary_dir = os.path.join(out_dir, "oceanval_summaries")
@@ -527,10 +527,11 @@ def summarize(
527527
)
528528
print("Variable mapping extraction complete.")
529529
print(all_df)
530-
x = input("Are you happy with the variable mappings shown above? Y/N: ")
531-
if x.lower() != "y":
532-
print("Exiting summarization. Please redefine your summary variables as needed.")
533-
return
530+
if ask:
531+
x = input("Are you happy with the variable mappings shown above? Y/N: ")
532+
if x.lower() != "y":
533+
print("Exiting summarization. Please redefine your summary variables as needed.")
534+
return
534535

535536
patterns = list(set(all_df.pattern))
536537
times_dict = dict()
@@ -642,14 +643,20 @@ def summarize(
642643
# Apply depth range if specified
643644
ds.merge("time")
644645
ds.tmean("year")
646+
short_title = summaries[var_name].short_title
647+
ds.set_longnames({model_var: short_title})
645648
ds.run()
646649

650+
647651
# now do the climatology
648652
clim_years = summaries[var_name].climatology_years
649-
ds_clim = ds.copy()
650-
ds_clim.subset(years=range(clim_years[0], clim_years[1] + 1))
651-
ds_clim.top()
652-
ds_clim.tmean()
653+
# do this quietly
654+
with warnings.catch_warnings(record=True) as w:
655+
ds_clim = ds.copy()
656+
ds_clim.subset(years=range(clim_years[0], clim_years[1] + 1))
657+
ds_clim.top()
658+
ds_clim.tmean()
659+
ds_clim.run()
653660

654661
# Prepare output filename
655662
out_file = f"{summary_dir}/data/{var_name}/{var_name}_surface_climatology.nc"
@@ -670,79 +677,105 @@ def summarize(
670677
trends = trend_info is not None
671678
# now figure out if vertical mean is neeed
672679
if summaries[var_name].vertical_mean:
673-
ds_vertmean = ds.copy()
674-
ds_vertmean.subset(years=clim_years)
675-
ds_vertmean.tmean()
676-
thickness = session_info["ds_thickness"]
677-
if thickness is None:
678-
ds_vertmean.vertical_mean(fixed = True)
679-
else:
680-
ds_vertmean.vertical_mean(thickness = ds_cell_thickness)
681-
# climatological years
682-
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalmean_climatology.nc"
683-
os.makedirs(os.path.dirname(out_file), exist_ok=True)
684-
if os.path.exists(out_file):
685-
os.remove(out_file)
686-
ds_vertmean.to_nc(out_file, overwrite=True, zip=True)
687-
print(f" Saved: {out_file}")
680+
# quietly
681+
with warnings.catch_warnings(record=True) as w:
682+
ds_vertmean = ds.copy()
683+
ds_vertmean.subset(years=clim_years)
684+
ds_vertmean.tmean()
685+
thickness = session_info["ds_thickness"]
686+
if thickness is None:
687+
ds_vertmean.vertical_mean(fixed = True)
688+
else:
689+
ds_vertmean.vertical_mean(thickness = ds_cell_thickness)
690+
# climatological years
691+
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalmean_climatology.nc"
692+
os.makedirs(os.path.dirname(out_file), exist_ok=True)
693+
if os.path.exists(out_file):
694+
os.remove(out_file)
695+
ds_vertmean.to_nc(out_file, overwrite=True, zip=True)
696+
print(f" Saved: {out_file}")
697+
698+
# do trends if needed
699+
if trends:
700+
with warnings.catch_warnings(record=True) as w:
701+
ds_trends = ds.copy()
702+
period = trend_info["period"]
703+
ds_trends.subset(years = range(period[0], period[1] + 1))
704+
window = trend_info["window"]
705+
if window != 1:
706+
ds_trends.rolling_mean(window)
707+
if thickness is None:
708+
ds_trends.vertical_mean(fixed = True)
709+
else:
710+
ds_trends.vertical_mean(thickness = ds_cell_thickness)
711+
ds_trends.spatial_mean()
712+
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalmean_spatialmeantimeseries.nc"
713+
os.makedirs(os.path.dirname(out_file), exist_ok = True)
714+
if os.path.exists(out_file):
715+
os.remove(out_file)
716+
717+
ds_trends.to_nc(out_file, overwrite=True, zip=True)
688718

689719
# now do vertical integration if needed
690720
if summaries[var_name].vertical_integration:
691-
ds_vertint = ds.copy()
692-
ds_vertint.subset(years=clim_years)
693-
ds_vertint.tmean()
694-
thickness = session_info["ds_thickness"]
695-
if thickness is None:
696-
ds_vertint.vertical_integration(fixed = True)
697-
else:
698-
ds_vertint.vertical_integration(thickness = ds_cell_thickness)
699-
# climatological years
700-
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalintegrated_climatology.nc"
701-
os.makedirs(os.path.dirname(out_file), exist_ok=True)
702-
if os.path.exists(out_file):
703-
os.remove(out_file)
704-
ds_vertint.to_nc(out_file, overwrite=True, zip=True)
705-
print(f" Saved: {out_file}")
721+
with warnings.catch_warnings(record=True) as w:
722+
ds_vertint = ds.copy()
723+
ds_vertint.subset(years=clim_years)
724+
ds_vertint.tmean()
725+
thickness = session_info["ds_thickness"]
726+
if thickness is None:
727+
ds_vertint.vertical_integration(fixed = True)
728+
else:
729+
ds_vertint.vertical_integration(thickness = ds_cell_thickness)
730+
# climatological years
731+
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalintegrated_climatology.nc"
732+
os.makedirs(os.path.dirname(out_file), exist_ok=True)
733+
if os.path.exists(out_file):
734+
os.remove(out_file)
735+
ds_vertint.to_nc(out_file, overwrite=True, zip=True)
736+
print(f" Saved: {out_file}")
706737

707738
# now do the spatial mean timeseries
708739
if trends:
740+
with warnings.catch_warnings(record=True) as w:
741+
ds_trends = ds.copy()
742+
period = trend_info["period"]
743+
ds_trends.subset(years = range(period[0], period[1] + 1))
744+
window = trend_info["window"]
745+
if window != 1:
746+
ds_trends.rolling_mean(window)
747+
if thickness is None:
748+
ds_trends.vertical_integration(fixed = True)
749+
else:
750+
ds_trends.vertical_integration(thickness = ds_cell_thickness)
751+
ds_trends.spatial_sum(by_area = True)
752+
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalintegrated_spatialsumtimeseries.nc"
753+
os.makedirs(os.path.dirname(out_file), exist_ok = True)
754+
if os.path.exists(out_file):
755+
os.remove(out_file)
756+
757+
ds_trends.to_nc(out_file, overwrite=True, zip=True)
758+
759+
if trends:
760+
with warnings.catch_warnings(record=True) as w:
709761
ds_trends = ds.copy()
762+
ds_trends.top()
763+
ds_trends.spatial_mean()
764+
# find out the window
765+
trend_info = summaries[var_name].trends
710766
period = trend_info["period"]
711767
ds_trends.subset(years = range(period[0], period[1] + 1))
712768
window = trend_info["window"]
713769
if window != 1:
714770
ds_trends.rolling_mean(window)
715-
if thickness is None:
716-
ds_trends.vertical_integration(fixed = True)
717-
else:
718-
ds_trends.vertical_integration(thickness = ds_cell_thickness)
719-
ds_trends.spatial_sum(by_area = True)
720-
out_file = f"{summary_dir}/data/{var_name}/{var_name}_verticalintegrated_spatialsumtimeseries.nc"
721-
os.makedirs(os.path.dirname(out_file), exist_ok = True)
771+
# save it
772+
out_file = f"{summary_dir}/data/{var_name}/{var_name}_surface_spatialmeantimeseries.nc"
773+
os.makedirs(os.path.dirname(out_file), exist_ok=True)
722774
if os.path.exists(out_file):
723775
os.remove(out_file)
724776

725777
ds_trends.to_nc(out_file, overwrite=True, zip=True)
726778

727-
if trends:
728-
ds_trends = ds.copy()
729-
ds_trends.top()
730-
ds_trends.spatial_mean()
731-
# find out the window
732-
trend_info = summaries[var_name].trends
733-
period = trend_info["period"]
734-
ds_trends.subset(years = range(period[0], period[1] + 1))
735-
window = trend_info["window"]
736-
if window != 1:
737-
ds_trends.rolling_mean(window)
738-
# save it
739-
out_file = f"{summary_dir}/data/{var_name}/{var_name}_surface_spatialmeantimeseries.nc"
740-
os.makedirs(os.path.dirname(out_file), exist_ok=True)
741-
if os.path.exists(out_file):
742-
os.remove(out_file)
743-
744-
ds_trends.to_nc(out_file, overwrite=True, zip=True)
745-
746779

747780

748781
# Save summaries configuration
@@ -812,11 +845,18 @@ def summarize(
812845
# copy file
813846
copyfile(file1, vv_out)
814847

848+
ff = f"{summary_dir}/data/{vv}/summaries_config.pkl"
849+
with open(ff, "rb") as f:
850+
ff_dict = dill.load(f)
851+
short_title = ff_dict[vv].short_title
852+
# read this in
815853
# read vv_out in and do some replacing
816854
with open(vv_out, "r") as file:
817855
filedata = file.read()
818856
# Replace the target string
819857
filedata = filedata.replace("VARIABLE_NAME", vv)
858+
filedata = filedata.replace("SHORT_TITLE", short_title)
859+
820860

821861
# write
822862
with open(vv_out, "w") as file:

0 commit comments

Comments
 (0)