From fe0f19665cc499ed9afe19bc50c2ecb99b78385b Mon Sep 17 00:00:00 2001 From: Mike Bedington Date: Mon, 12 Nov 2018 15:55:20 +0000 Subject: [PATCH] Tidy up river update function --- .../file/update_river_data.py | 103 +++++++++--------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/app/update_river_model/file/update_river_data.py b/app/update_river_model/file/update_river_data.py index 2ee2e41..524941a 100644 --- a/app/update_river_model/file/update_river_data.py +++ b/app/update_river_model/file/update_river_data.py @@ -14,62 +14,63 @@ no_miss_loops = 4 # Load the river model with open('river_model.pk1','rb') as f: - river_dict = pk.load(f) + river_dict = pk.load(f) start_date = end_date for this_river in river_dict.values(): - if hasattr(this_river, 'catchment_precipitation'): - this_river_update = np.max(this_river.catchment_precipitation[0]) - if this_river_update < start_date: - start_date = this_river_update + if hasattr(this_river, 'catchment_precipitation'): + this_river_update = np.max(this_river.catchment_precipitation[0]) + if this_river_update < start_date: + start_date = this_river_update if start_date == end_date: - print('Already up to date') - -else: - - missing_dates = np.asarray([start_date + dt.timedelta(days=int(this_ind)) for this_ind in np.arange(0, (end_date - start_date).days + 1)]) - - for this_missing_loop in np.arange(0, no_miss_loops): - new_missing_dates = [] - for this_date in missing_dates: - if this_missing_loop > 0: - print('Trying again to fill for {}'.format(this_date)) - else: - print(this_date) - this_date_m1 = this_date - dt.timedelta(days=int(this_missing_loop)) - this_date_str = this_date_m1.strftime('%Y%m%d') - potential_files = gb.glob('{}/{}*_forecast/wrfout_d03*'.format(wrf_forecast_out_dir, this_date_str)) - - try: - this_wrf_nc = nc.Dataset(potential_files[-1], 'r') - wrf_date_str_raw = this_wrf_nc.variables['Times'][:] - wrf_date_str = np.asarray([b''.join(this_str) for this_str in wrf_date_str_raw]) - wrf_dt = np.asarray([dt.datetime.strptime(this_str.decode('utf-8'),'%Y-%m-%d_%H:%M:%S') for this_str in wrf_date_str]) - wrf_dt_date = np.asarray([this_dt.date() for this_dt in wrf_dt]) - - date_match = wrf_dt_date == this_date.date() - - forecast_data = {'times': wrf_dt[date_match], 'RAINNC': this_wrf_nc.variables['RAINNC'][date_match,:,:], - 'T2': this_wrf_nc.variables['T2'][date_match,:,:]} - this_wrf_nc.close() - - for this_river_name, this_river in river_dict.items(): - if hasattr(this_river, 'addToSeries'): - this_rain = np.sum(np.sum(forecast_data['RAINNC']*this_river.wrf_catchment_factors, axis=2), axis=1) - this_river.addToSeries('catchment_precipitation', this_rain, forecast_data['times'], override=True) - - this_temp = np.zeros(len(forecast_data['times'])) - for i in range(0, len(forecast_data['times'])): - this_temp[i] = np.average(forecast_data['T2'][i,:,:], weights=this_river.wrf_catchment_factors) - this_river.addToSeries('catchment_temp', this_temp, forecast_data['times'], override=True) - - except: - new_missing_dates.append(this_date) - missing_dates = new_missing_dates[:] - - with open('river_model.pk1','wb') as f: - pk.dump(river_dict, f, pk.HIGHEST_PROTOCOL) + print('Already up to date') + +else: + + missing_dates = np.asarray([start_date + dt.timedelta(days=int(this_ind)) for this_ind in np.arange(0, (end_date - start_date).days + 1)]) + + for this_missing_loop in np.arange(0, no_miss_loops): + new_missing_dates = [] + for this_date in missing_dates: + if this_missing_loop > 0: + print('Trying again to fill for {}'.format(this_date)) + else: + print(this_date) + this_date_m1 = this_date - dt.timedelta(days=int(this_missing_loop)) + this_date_str = this_date_m1.strftime('%Y%m%d') + potential_files = gb.glob('{}/{}*_forecast/wrfout_d03*'.format(wrf_forecast_out_dir, this_date_str)) + + try: + this_wrf_nc = nc.Dataset(potential_files[-1], 'r') + wrf_date_str_raw = this_wrf_nc.variables['Times'][:] + wrf_date_str = np.asarray([b''.join(this_str) for this_str in wrf_date_str_raw]) + wrf_dt = np.asarray([dt.datetime.strptime(this_str.decode('utf-8'),'%Y-%m-%d_%H:%M:%S') for this_str in wrf_date_str]) + wrf_dt_date = np.asarray([this_dt.date() for this_dt in wrf_dt]) + + date_match = wrf_dt_date == this_date.date() + + forecast_data = {'times': wrf_dt[date_match], 'RAINNC': this_wrf_nc.variables['RAINNC'][date_match,:,:], + 'T2': this_wrf_nc.variables['T2'][date_match,:,:]} + this_wrf_nc.close() + + for this_river_name, this_river in river_dict.items(): + if hasattr(this_river, 'addToSeries'): + this_rain = np.sum(np.sum(forecast_data['RAINNC']*this_river.wrf_catchment_factors, axis=2), axis=1) + this_river.addToSeries('catchment_precipitation', this_rain, forecast_data['times'], override=True) + + this_temp = np.zeros(len(forecast_data['times'])) + for i in range(0, len(forecast_data['times'])): + this_temp[i] = np.average(forecast_data['T2'][i,:,:], weights=this_river.wrf_catchment_factors) + this_river.addToSeries('catchment_temp', this_temp, forecast_data['times'], override=True) + if hasattr(this_river, 'river_obj'): + this_river.catchment_precipitation = this_river.river_obj.catchment_precipitation + except: + new_missing_dates.append(this_date) + missing_dates = new_missing_dates[:] + + with open('river_model.pk1','wb') as f: + pk.dump(river_dict, f, pk.HIGHEST_PROTOCOL) -- GitLab