def run_clark2009(catchment, output_dem, hydro_year_to_take, met_inp_folder, catchment_shp_folder): """ wrapper to call the clark 2009 snow model for given area :param catchment: string giving catchment area to run model on :param output_dem: string identifying the grid to run model on :param hydro_year_to_take: integer specifying the hydrological year to run model over. 2001 = 1/4/2000 to 31/3/2001 :return: st_swe, st_melt, st_acc, out_dt, mask. daily grids of SWE at day's end, total melt and accumulation over the previous day, and datetimes of ouput """ print('loading met data') data_id = '{}_{}'.format(catchment, output_dem) inp_met = nc.Dataset( met_inp_folder + '/met_inp_{}_hy{}.nc'.format(data_id, hydro_year_to_take), 'r') inp_dt = nc.num2date(inp_met.variables['time'][:], inp_met.variables['time'].units) inp_doy = convert_date_hydro_DOY(inp_dt) inp_hourdec = [datt.hour for datt in inp_dt] inp_ta = inp_met.variables['temperature'][:] inp_precip = inp_met.variables['precipitation'][:] print('met data loaded') mask = create_mask_from_shpfile( inp_met.variables['lat'][:], inp_met.variables['lon'][:], catchment_shp_folder + '/{}.shp'.format(catchment)) # TODO: think about masking input data to speed up print('starting snow model') # start with no snow. # call main function once hourly/sub-hourly temp and precip data available. st_swe, st_melt, st_acc = snow_main_simple(inp_ta, inp_precip, inp_doy, inp_hourdec, dtstep=3600) out_dt = np.asarray( make_regular_timeseries(inp_dt[0], inp_dt[-1] + dt.timedelta(days=1), 86400)) print('snow model finished') # mask out values outside of catchment st_swe[:, mask == False] = np.nan st_melt[:, mask == False] = np.nan st_acc[:, mask == False] = np.nan return st_swe, st_melt, st_acc, out_dt, mask
# set up input and output DEM for processing # output DEM nztm_dem, x_centres, y_centres, lat_array, lon_array = setup_nztm_dem( dem_file, origin=origin) data_id = '{}_{}'.format(catchment, output_dem) # name to identify the output data if mask_dem == True: # Get the masks for the individual regions of interest if mask_created == True: # load precalculated mask mask = np.load(mask_folder + '/{}_{}.npy'.format(catchment, output_dem)) if origin == 'topleft': mask = np.flipud(mask) else: # create mask and save to npy file # masks = get_masks() #TODO set up for multiple masks mask = create_mask_from_shpfile(lat_array, lon_array, mask_shpfile) if origin == 'topleft': # flip to save as bottom left mask = np.flipud(mask) np.save(mask_folder + '/{}_{}.npy'.format(catchment, output_dem), mask) if origin == 'topleft': # flip back to topleft mask = np.flipud(mask) # Trim down the number of latitudes requested so it all stays in memory lats, lons, elev, northings, eastings = trim_lat_lon_bounds( mask, lat_array, lon_array, nztm_dem, y_centres, x_centres) _, _, trimmed_mask, _, _ = trim_lat_lon_bounds(mask, lat_array, lon_array, mask.copy(), y_centres, x_centres) else: mask = None
if which_model == 'clark2009': config['tmelt'] = 273.16 if which_model == 'dsc_snow': config['tmelt'] = 274.16 for hydro_year_to_take in hydro_years_to_take: # run model and return timeseries of daily swe, acc and melt. met_infile = met_data_folder + '/met_inp_{}_{}_hy{}.nc'.format( catchment, output_dem, hydro_year_to_take) st_swe, st_melt, st_acc = snow_main(met_infile, which_melt=which_model, **config) # create the extra variables required by catchment_evaluation inp_met = nc.Dataset(met_infile, 'r') inp_dt = nc.num2date(inp_met.variables['time'][:], inp_met.variables['time'].units) out_dt = np.asarray( make_regular_timeseries(inp_dt[0], inp_dt[-1], 86400)) mask = create_mask_from_shpfile( inp_met.variables['lat'][:], inp_met.variables['lon'][:], catchment_shp_folder + '/{}.shp'.format(catchment)) pickle.dump( [ st_swe.astype(dtype=np.float32), st_melt.astype(dtype=np.float32), st_acc.astype(dtype=np.float32), out_dt, mask, config ], open( output_folder + '/{}_{}_hy{}_{}.pkl'.format( catchment, output_dem, hydro_year_to_take, which_model), 'wb'), -1)