import os import xarray as xr import numpy as np import pandas as pd import geopandas as gpd import json #import salem import oggm.cfg from oggm import utils, workflow, tasks, graphics from oggm.shop import gcm_climate import logging # Module logger log = logging.getLogger(__name__) from oggm import entity_task @entity_task(log) def run_hydro_w_spinup_merge(gdir, members=None): '''Runs dynamics spinup until ref_yr, but minimum until ye 2001. Then, historical climate until 2020. Finally the future projections for the three SSPs by using ISIMIP3b. The ref_area_yr is 2000. ''' # increasing ye until 2001 increases the runtime but probably there are not so many glaciers # with rgi_date <2000, we need to go until 2001, as this is the reference year for the area (also for the spinup!!!) # default: ye = gdir.rgi_date + 1 # ye has to be 2001 or later, if (gdir.rgi_date + 1)> 2001 ye=2001 if (gdir.rgi_date + 1)<2001 else gdir.rgi_date + 1 # `run_with_hydro` with run_dyanmic spinup wants to go until rgi date, so we just let it go until then # in case of HEF, it goes until 2004 (however, hydro stuff only until 2003) tasks.run_with_hydro(gdir, run_task=tasks.run_dynamic_spinup, store_monthly_hydro=True, ye=ye, # this is important ref_area_yr = 2000, output_filesuffix='_spinup_rgi_date') # start from rgi date (this happens automatically), in case of HEF, this is 2004 tasks.run_with_hydro(gdir, run_task=tasks.run_from_climate_data, climate_filename='climate_historical', ys=ye, store_monthly_hydro=True, init_model_filesuffix='_spinup_rgi_date', ref_geometry_filesuffix='_spinup_rgi_date', ref_area_yr = 2000, output_filesuffix='_historical_from_rgi_date_run') utils.merge_consecutive_run_outputs(gdir, input_filesuffix_1='_spinup_rgi_date', input_filesuffix_2='_historical_from_rgi_date_run', output_filesuffix=f'_hist_w_spinup_run', delete_input=True, ) # we will delete that later # only this part needs to be recomputed for every member and ssp... for member in members: for ssp in ['ssp126', 'ssp370', 'ssp585']: rid = f'_ISIMIP3b_{member}_{ssp}' tasks.run_with_hydro(gdir, run_task=tasks.run_from_climate_data, ys=2020, # this is important! Start from 2020 glacier # use gcm_data, not climate_historical climate_filename='gcm_data', # use the chosen scenario climate_input_filesuffix=rid, # we start from the previous run, init_model_filesuffix='_historical_from_rgi_date_run', # howver, the ref_geometry comes from the first run, # this is different to init_model_filesuffix as we want to copy the reference area from the spinup (as here the gdir is after ref_area_yr) ref_geometry_filesuffix='_spinup_rgi_date', ref_area_yr = 2000, # recognize the run for later output_filesuffix=f'_future_run{rid}_{member}', # add monthly diagnostics store_monthly_hydro=True); #this is a bit stupid, but there is no input_filesuffix_3, as far as I can see: utils.merge_consecutive_run_outputs(gdir, input_filesuffix_1='_hist_w_spinup_run', input_filesuffix_2=f'_future_run{rid}_{member}', output_filesuffix=f'_merged_{member}_{ssp}') os.remove(gdir.get_filepath('model_diagnostics', filesuffix=f'_future_run{rid}_{member}')) # we did not delete the input here because we need _hist_w_spinup_run for all the ssps # we just remove the hist_w_spinup_run at the end os.remove(gdir.get_filepath('model_diagnostics', filesuffix='_hist_w_spinup_run')) @entity_task(log) def run_hydro_from_2000_ref_area_2000(gdir): '''Runs historical hydro climate from 2000 until 2020. Finally the future projections for the three SSPs by using ISIMIP3b. The ref_area_yr is 2000. ''' tasks.run_with_hydro(gdir, run_task=tasks.run_from_climate_data, climate_filename='climate_historical', ys=2000, init_model_yr=2000, store_monthly_hydro=True, init_model_filesuffix='_spinup_historical', ref_geometry_filesuffix='_spinup_historical', ref_area_yr = 2000, output_filesuffix='_historical_from_2000_run') ### ISIMPI3b members = ['gfdl-esm4_r1i1p1f1', 'mpi-esm1-2-hr_r1i1p1f1', 'mri-esm2-0_r1i1p1f1', 'ipsl-cm6a-lr_r1i1p1f1', 'ukesm1-0-ll_r1i1p1f2' ] # only this part needs to be recomputed for every member and ssp... for member in members: for ssp in ['ssp126', 'ssp370', 'ssp585']: rid = f'_ISIMIP3b_{member}_{ssp}' tasks.run_with_hydro(gdir, run_task=tasks.run_from_climate_data, ys=2020, # this is important! Start from 2020 glacier # use gcm_data, not climate_historical climate_filename='gcm_data', # use the chosen scenario climate_input_filesuffix=rid, # we start from the previous run, init_model_filesuffix='_historical_from_2000_run', ref_geometry_filesuffix='_historical_from_2000_run', ref_area_yr = 2000, # recognize the run for later output_filesuffix=f'_future_run{rid}', # add monthly diagnostics store_monthly_hydro=True); utils.merge_consecutive_run_outputs(gdir, input_filesuffix_1='_historical_from_2000_run', input_filesuffix_2=f'_future_run{rid}', output_filesuffix=f'_merged_from_2000_run{rid}', delete_input=False, ) # we will delete that later ### CMIP6 gcms_cmip6 = pd.read_csv('/home/www/oggm/cmip6/all_gcm_list.csv', index_col=0) for gcm in gcms_cmip6.gcm.unique(): ##TEST: df1 = gcms_cmip6.loc[gcms_cmip6.gcm == gcm] for ssp in df1.ssp.unique(): df2 = df1.loc[df1.ssp == ssp] assert len(df2) == 2 ft = df2.loc[df2['var'] == 'tas'].iloc[0] rid = ft.fname.replace('_r1i1p1f1_tas.nc', '') rid='_CMIP6_' + rid tasks.run_with_hydro(gdir, run_task=tasks.run_from_climate_data, ys=2020, # this is important! Start from 2020 glacier # use gcm_data, not climate_historical climate_filename='gcm_data', # use the chosen scenario climate_input_filesuffix=rid, # we start from the previous run, init_model_filesuffix='_historical_from_2000_run', ref_geometry_filesuffix='_historical_from_2000_run', ref_area_yr = 2000, # recognize the run for later output_filesuffix=f'_future_run{rid}', # add monthly diagnostics store_monthly_hydro=True); utils.merge_consecutive_run_outputs(gdir, input_filesuffix_1='_historical_from_2000_run', input_filesuffix_2=f'_future_run{rid}', output_filesuffix=f'_merged_from_2000_run{rid}', delete_input=False, ) # we will delete that later