Skip to content

Commit

Permalink
Merge pull request #5 from UW-SASWE/sm-main
Browse files Browse the repository at this point in the history
Added option to run RAT without vic spin-up
  • Loading branch information
SanchitMinocha authored Feb 1, 2023
2 parents 7bae092 + 473c02c commit a85f1d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
33 changes: 21 additions & 12 deletions src/rat/rat_basin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
rout_init_state_save_date = config['BASIN']['end']+datetime.timedelta(days=1)

if(not config['BASIN']['first_run']):
config['BASIN']['vic_init_state_date'] = datetime.datetime.combine(config['BASIN']['vic_init_state_date'], datetime.time.min)
if(config['BASIN'].get('vic_init_state_date')):
config['BASIN']['vic_init_state_date'] = datetime.datetime.combine(config['BASIN']['vic_init_state_date'], datetime.time.min)

# Changing start date if running RAT for first time for the particular basin to give VIC and MetSim to have their spin off periods
if(config['BASIN']['first_run']):
user_given_start = config['BASIN']['start']
config['BASIN']['start'] = user_given_start-datetime.timedelta(days=800) # Running RAT for extra 800 days before the user-given start date for VIC to give reliable results starting from user-given start date
data_download_start = config['BASIN']['start']-datetime.timedelta(days=90) # Downloading 90 days of extra meteorological data for MetSim to prepare it's initial state
vic_init_state_date = None # No initial state of VIC is present as running RAT for first time in this basin
else:
elif(config['BASIN'].get('vic_init_state_date')):
data_download_start = config['BASIN']['start'] # Downloading data from the same date as we want to run RAT from
vic_init_state_date = config['BASIN']['vic_init_state_date'] # Date of which initial state of VIC for the particular basin exists
else:
data_download_start = config['BASIN']['start']-datetime.timedelta(days=90) # Downloading 90 days of extra meteorological data for MetSim to prepare it's initial state
vic_init_state_date = None # No initial state of VIC is present as running RAT for first time in this basin

# Defining logger
log = init_logger(
Expand Down Expand Up @@ -169,7 +173,7 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
# Creating routing parameter directory
rout_param_dir = create_directory(os.path.join(basin_data_dir,'ro','pars',''), True)
# Defining path and name for basin flow direction file
basin_flow_dir_file = os.path.join(rout_param_dir,'fl')
basin_flow_dir_file = os.path.join(rout_param_dir,'fl.asc')
# Defining Basin station latlon file path
if (config['ROUTING']['station_global_data']):
basin_station_latlon_file = os.path.join(rout_param_dir,'basin_station_latlon.csv')
Expand Down Expand Up @@ -205,7 +209,7 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
evap_savedir = create_directory(os.path.join(basin_data_dir,'rat_outputs', "Evaporation"), True)
dels_savedir = create_directory(os.path.join(basin_data_dir,'rat_outputs', "dels"), True)
outflow_savedir = create_directory(os.path.join(basin_data_dir,'rat_outputs', "rat_outflow"),True)
web_dir_path = create_directory(os.path.join(basin_data_dir,'web_format_data',''),True)
final_output_path = create_directory(os.path.join(basin_data_dir,'final_outputs',''),True)
## End of defining paths for storing post-processed data and webformat data
#----------- Paths Necessary for running of Post-Processing-----------#

Expand Down Expand Up @@ -382,8 +386,11 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
if(config['BASIN']['first_run']):
vic.disagg_results(rout_input_state_file=p.vic_result_file) # If first run, use vic result file
rout_input_state_start_date = config['BASIN']['start'] # Start date will become same as vic result file
else:
elif(config['BASIN'].get('vic_init_state_date')): # If vic_state file exists
vic.disagg_results(rout_input_state_file=rout_input_state_file) # If not first run, use rout input state file
else:
vic.disagg_results(rout_input_state_file=p.vic_result_file) # If first run is false and vic state file does not exist, use vic result file
rout_input_state_start_date = config['BASIN']['start'] # Start date will become same as vic result file
vic_startdate = p.vic_startdate
vic_enddate = p.vic_enddate
VIC_STATUS=1 #Vic run successfully
Expand Down Expand Up @@ -427,10 +434,12 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
### Extracting routing start date ###
if(config['BASIN']['first_run']):
rout_input_state_start_date = config['BASIN']['start']
else:
elif(config['BASIN'].get('vic_init_state_date')):
rout_state_data = xr.open_dataset(rout_input_state_file).load()
rout_input_state_start_date = rout_state_data.time[0].values.astype('datetime64[us]').astype(datetime.datetime)
rout_state_data.close()
else:
rout_input_state_start_date = config['BASIN']['start']
### Extracted routing start date ###
#------------- Routing Begins and Pre processing for Mass Balance --------------#
with RouteParameterFile(
Expand Down Expand Up @@ -537,42 +546,42 @@ def rat(config, rat_logger, steps=[1,2,3,4,5,6,7,8,9,10,11,12,13]):
# Convert to format that is expected by the website and save in web_dir
## Surface Area
if(GEE_STATUS):
convert_sarea(sarea_savepath,web_dir_path)
convert_sarea(sarea_savepath,final_output_path)
rat_logger.info("Converted Surface Area to the Output Format.")
else:
rat_logger.info("Could not convert Surface Area to the Output Format as GEE run failed.")

## Inflow
if(ROUTING_STATUS):
convert_inflow(rout_inflow_dir, basin_reservoir_shpfile_path, reservoirs_gdf_column_dict, web_dir_path)
convert_inflow(rout_inflow_dir, basin_reservoir_shpfile_path, reservoirs_gdf_column_dict, final_output_path)
rat_logger.info("Converted Inflow to the Output Format.")
else:
rat_logger.info("Could not convert Inflow to the Output Format as Routing run failed.")

## Dels
if(DELS_STATUS):
convert_dels(dels_savedir, web_dir_path)
convert_dels(dels_savedir, final_output_path)
rat_logger.info("Converted ∆S to the Output Format.")
else:
rat_logger.info("Could not convert ∆S to the Output Format as GEE run failed.")

## Evaporation
if(EVAP_STATUS):
convert_evaporation(evap_savedir, web_dir_path)
convert_evaporation(evap_savedir, final_output_path)
rat_logger.info("Converted Evaporation to the Output Format.")
else:
rat_logger.info("Could not convert Evaporation to the Output Format as either GEE or VIC run failed.")

## Outflow
if(OUTFLOW_STATUS):
convert_outflow(outflow_savedir, web_dir_path)
convert_outflow(outflow_savedir, final_output_path)
rat_logger.info("Converted Outflow to the Output Format.")
else:
rat_logger.info("Could not convert Outflow to the Output Format as either GEE or Routing run failed resulting in failure of calculation of either Inflow, ∆S or Evaporation.")

## Altimeter
if(ALTIMETER_STATUS):
convert_altimeter(altimetry_savepath, web_dir_path)
convert_altimeter(altimetry_savepath, final_output_path)
rat_logger.info("Converted Extracted Height from Altimeter to the Output Format.")
else:
rat_logger.info("Could not convert Extracted Height from Altimeter to the Output Format as Altimeter Run failed.")
Expand Down
2 changes: 1 addition & 1 deletion src/rat/run_rat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ee

from rat.utils.logging import init_logger,close_logger
import ee_utils.ee_config as ee_configuration
import rat.ee_utils.ee_config as ee_configuration

#------------ Define Variables ------------#
def run_rat(config_fn):
Expand Down
8 changes: 4 additions & 4 deletions src/rat/utils/route_param_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _load_from_config(self):
f'run_{self.runname}'))
else:
if(self.intermediate_files):
self.workspace = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['major_basin_name'],'basins',
self.workspace = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['region_name'],'basins',
self.basin_name,'ro','rout_workspace',f'run_{self.runname}'))

## Route parameter file, this is where the parameter file will be saved
Expand All @@ -83,22 +83,22 @@ def _load_from_config(self):
self.route_param_path = os.path.relpath(config[self.config_section].get('route_param_file'),self.project_dir)
# Or storing it in route basin params dir and replace it from next cycle
else:
self.route_param_path = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['major_basin_name'],
self.route_param_path = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['region_name'],
'basins',self.basin_name,'rout_basin_params'),True)
self.route_param_path = os.path.relpath(os.path.join(self.route_param_path,'route_param.txt'),self.project_dir)

## flow direction file
self.params['flow_direction_file'] = self.basin_flow_direction_file

## output dir
self.params['output_dir'] = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['major_basin_name'],
self.params['output_dir'] = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['region_name'],
'basins',self.basin_name,'ro','ou'),True)

## stations
if (self.intermediate_files):
self.params['station'] = os.path.join(self.workspace, 'sta_xy.txt')
else:
self.params['station'] = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['major_basin_name'],
self.params['station'] = create_directory(os.path.join(config['GLOBAL']['data_dir'],config['BASIN']['region_name'],
'basins',self.basin_name,'ro','pars'),True)
self.params['station'] = os.path.join(self.params['station'],'sta_xy.txt')

Expand Down

0 comments on commit a85f1d6

Please sign in to comment.