model_info = zip(model_name_list, model_plot_name_list) ci_method = os.environ['CI_METHOD'] grid = os.environ['VERIF_GRID'] event_equalization = os.environ['EVENT_EQUALIZATION'] met_version = os.environ['MET_VERSION'] logger = logging.getLogger(os.environ['LOGGING_FILENAME']) logger.setLevel(os.environ['LOGGING_LEVEL']) formatter = logging.Formatter( "%(asctime)s.%(msecs)03d (%(filename)s:%(lineno)d) " "%(levelname)s: %(message)s", "%m/%d %H:%M:%S") file_handler = logging.FileHandler(os.environ['LOGGING_FILENAME'], mode='a') file_handler.setFormatter(formatter) logger.addHandler(file_handler) plot_time_dates, expected_stat_file_dates = plot_util.get_date_arrays( plot_time, start_date_YYYYmmdd, end_date_YYYYmmdd, valid_time_info, init_time_info, lead) total_days = len(plot_time_dates) stat_file_base_columns = plot_util.get_stat_file_base_columns(met_version) logger.info("Reading in model data") for model in model_info: model_num = model_info.index(model) + 1 model_name = model[0] model_plot_name = model[1] model_data_now_index = pd.MultiIndex.from_product( [[model_plot_name], expected_stat_file_dates], names=['model_plot_name', 'dates']) model_stat_file = os.path.join( stat_file_input_dir_base, verif_case, verif_type, model_plot_name, plot_time + start_date_YYYYmmdd + "to" + end_date_YYYYmmdd + "_valid" +
def create_plots_precip(self, fcst_var_level_list, obs_var_level_list, fcst_var_thresh_list, obs_var_thresh_list, lead_list, plotting_scripts_dir): # Need to generate random numbers for # Monte Carlo sig. testing, all leads # and thresholds use same numbers if os.environ['CI_METHOD'] == 'EMC_MONTE_CARLO': ntests = 10000 plot_time = os.environ['PLOT_TIME'] start_date_YYYYmmdd = os.environ['START_DATE_YYYYmmdd'] end_date_YYYYmmdd = os.environ['END_DATE_YYYYmmdd'] valid_time_info = os.environ['VALID_TIME_INFO'].replace( '"', '').split(", ") init_time_info = os.environ['INIT_TIME_INFO'].replace( '"', '').split(", ") lead = lead_list[0] plot_time_dates, expected_stat_file_dates = plot_util.get_date_arrays( plot_time, start_date_YYYYmmdd, end_date_YYYYmmdd, valid_time_info, init_time_info, lead) total_days = len(plot_time_dates) randx_dir = os.path.join( os.environ['PLOTTING_OUT_DIR_FULL'], "data", plot_time + start_date_YYYYmmdd + "to" + end_date_YYYYmmdd + "_valid" + valid_time_info[0] + "to" + valid_time_info[-1] + "Z" + "_init" + init_time_info[0] + "to" + init_time_info[-1] + "Z") if not os.path.exists(randx_dir): os.makedirs(randx_dir) for model in os.environ['MODEL_PLOT_NAME_LIST'].split(' '): randx_file = os.path.join(randx_dir, model + '_randx.txt') randx = np.random.rand(ntests, total_days) np.savetxt(randx_file, randx, fmt='%0.10f') self.add_env_var('LEAD_LIST', ', '.join(lead_list)) self.add_env_var('FCST_VAR_THRESH_LIST', ', '.join(fcst_var_thresh_list)) self.add_env_var('OBS_VAR_THRESH_LIST', ', '.join(obs_var_thresh_list)) #time series plot for lead in lead_list: self.add_env_var('LEAD', lead) for vl in range(len(fcst_var_level_list)): self.add_env_var('FCST_VAR_LEVEL', fcst_var_level_list[vl]) self.add_env_var('OBS_VAR_LEVEL', obs_var_level_list[vl]) for vt in range(len(fcst_var_thresh_list)): self.add_env_var('FCST_VAR_THRESH', fcst_var_thresh_list[vt]) self.add_env_var('OBS_VAR_THRESH', obs_var_thresh_list[vt]) self.set_plotting_script( os.path.join(plotting_scripts_dir, "plot_time_series.py")) self.logger.debug("Running " + os.path.join( plotting_scripts_dir, "plot_time_series.py") + " with...") self.logger.debug("DATES: " + os.environ['PLOT_TIME'] + " " + os.environ['START_DATE_YYYYmmdd'] + " " + os.environ['END_DATE_YYYYmmdd']) self.logger.debug("VALID TIME INFO: " + os.environ['VALID_TIME_INFO']) self.logger.debug("INIT TIME INFO: " + os.environ['INIT_TIME_INFO']) self.logger.debug("FCST VAR: " + os.environ['FCST_VAR_NAME'] + " " + fcst_var_level_list[vl] + " " + fcst_var_thresh_list[vt] + " " + os.environ['FCST_VAR_EXTRA']) self.logger.debug("OBS VAR: " + os.environ['OBS_VAR_NAME'] + " " + obs_var_level_list[vl] + " " + obs_var_thresh_list[vt] + " " + os.environ['OBS_VAR_EXTRA']) self.logger.debug("INTERP: " + os.environ['INTERP']) self.logger.debug("REGION: " + os.environ["REGION"]) self.logger.debug("LEAD: " + lead) self.logger.debug("EVENT_EQUALIZATION: " + os.environ['EVENT_EQUALIZATION']) self.logger.debug("CI_METHOD: " + os.environ['CI_METHOD']) self.logger.debug("VERIF_GRID: " + os.environ['VERIF_GRID']) self.logger.debug("MODEL_NAME_LIST: " + os.environ['MODEL_NAME_LIST']) self.logger.debug("MODEL_PLOT_NAME_LIST: " + os.environ['MODEL_PLOT_NAME_LIST']) self.logger.debug("PLOT_STATS_LIST: " + os.environ['PLOT_STATS_LIST']) cmd = self.get_command() if cmd is None: self.logger.error( "ERROR: make_plots could not generate command for " + self.plotting_script) return self.build() self.clear() #lead mean plot for vl in range(len(fcst_var_level_list)): self.add_env_var('FCST_VAR_LEVEL', fcst_var_level_list[vl]) self.add_env_var('OBS_VAR_LEVEL', obs_var_level_list[vl]) for vt in range(len(fcst_var_thresh_list)): self.add_env_var('FCST_VAR_THRESH', fcst_var_thresh_list[vt]) self.add_env_var('OBS_VAR_THRESH', obs_var_thresh_list[vt]) self.set_plotting_script( os.path.join(plotting_scripts_dir, "plot_lead_mean.py")) self.logger.debug( "Running " + os.path.join(plotting_scripts_dir, "plot_lead_mean.py") + " with...") self.logger.debug("DATES: " + os.environ['PLOT_TIME'] + " " + os.environ['START_DATE_YYYYmmdd'] + " " + os.environ['END_DATE_YYYYmmdd']) self.logger.debug("VALID TIME INFO: " + os.environ['VALID_TIME_INFO']) self.logger.debug("INIT TIME INFO: " + os.environ['INIT_TIME_INFO']) self.logger.debug("FCST VAR: " + os.environ['FCST_VAR_NAME'] + " " + fcst_var_level_list[vl] + " " + fcst_var_thresh_list[vt] + " " + os.environ['FCST_VAR_EXTRA']) self.logger.debug("OBS VAR: " + os.environ['OBS_VAR_NAME'] + " " + obs_var_level_list[vl] + " " + obs_var_thresh_list[vt] + " " + os.environ['OBS_VAR_EXTRA']) self.logger.debug("INTERP: " + os.environ['INTERP']) self.logger.debug("REGION: " + os.environ["REGION"]) self.logger.debug("LEAD_LIST: " + os.environ["LEAD_LIST"]) self.logger.debug("EVENT_EQUALIZATION: " + os.environ['EVENT_EQUALIZATION']) self.logger.debug("CI_METHOD: " + os.environ['CI_METHOD']) self.logger.debug("VERIF_GRID: " + os.environ['VERIF_GRID']) self.logger.debug("MODEL_NAME_LIST: " + os.environ['MODEL_NAME_LIST']) self.logger.debug("MODEL_PLOT_NAME_LIST: " + os.environ['MODEL_PLOT_NAME_LIST']) self.logger.debug("PLOT_STATS_LIST: " + os.environ['PLOT_STATS_LIST']) cmd = self.get_command() if cmd is None: self.logger.error( "ERROR: make_plots could not generate command for " + self.plotting_script) return self.build() self.clear() #threshold mean plot for lead in lead_list: self.add_env_var('LEAD', lead) for vl in range(len(fcst_var_level_list)): self.add_env_var('FCST_VAR_LEVEL', fcst_var_level_list[vl]) self.add_env_var('OBS_VAR_LEVEL', obs_var_level_list[vl]) self.set_plotting_script( os.path.join(plotting_scripts_dir, "plot_threshold_mean.py")) self.logger.debug("Running " + os.path.join( plotting_scripts_dir, "plot_threshold_mean.py") + " with...") self.logger.debug("DATES: " + os.environ['PLOT_TIME'] + " " + os.environ['START_DATE_YYYYmmdd'] + " " + os.environ['END_DATE_YYYYmmdd']) self.logger.debug("VALID TIME INFO: " + os.environ['VALID_TIME_INFO']) self.logger.debug("INIT TIME INFO: " + os.environ['INIT_TIME_INFO']) self.logger.debug("FCST VAR: " + os.environ['FCST_VAR_NAME'] + " " + fcst_var_level_list[vl] + " " + os.environ["FCST_VAR_THRESH_LIST"] + " " + os.environ['FCST_VAR_EXTRA']) self.logger.debug("OBS VAR: " + os.environ['OBS_VAR_NAME'] + " " + obs_var_level_list[vl] + " " + os.environ["OBS_VAR_THRESH_LIST"] + " " + os.environ['OBS_VAR_EXTRA']) self.logger.debug("INTERP: " + os.environ['INTERP']) self.logger.debug("REGION: " + os.environ["REGION"]) self.logger.debug("LEAD: " + os.environ["LEAD"]) self.logger.debug("EVENT_EQUALIZATION: " + os.environ['EVENT_EQUALIZATION']) self.logger.debug("CI_METHOD: " + os.environ['CI_METHOD']) self.logger.debug("VERIF_GRID: " + os.environ['VERIF_GRID']) self.logger.debug("MODEL_NAME_LIST: " + os.environ['MODEL_NAME_LIST']) self.logger.debug("MODEL_PLOT_NAME_LIST: " + os.environ['MODEL_PLOT_NAME_LIST']) self.logger.debug("PLOT_STATS_LIST: " + os.environ['PLOT_STATS_LIST']) cmd = self.get_command() if cmd is None: self.logger.error( "ERROR: make_plots could not generate command for " + self.plotting_script) return self.build() self.clear() #threshhold by mean plot for vl in range(len(fcst_var_level_list)): self.add_env_var('FCST_VAR_LEVEL', fcst_var_level_list[vl]) self.add_env_var('OBS_VAR_LEVEL', obs_var_level_list[vl]) self.set_plotting_script( os.path.join(plotting_scripts_dir, "plot_threshold_by_lead.py")) self.logger.debug("Running " + os.path.join( plotting_scripts_dir, "plot_threshold_by_lead.py") + " with...") self.logger.debug("DATES: " + os.environ['PLOT_TIME'] + " " + os.environ['START_DATE_YYYYmmdd'] + " " + os.environ['END_DATE_YYYYmmdd']) self.logger.debug("VALID TIME INFO: " + os.environ['VALID_TIME_INFO']) self.logger.debug("INIT TIME INFO: " + os.environ['INIT_TIME_INFO']) self.logger.debug("FCST VAR: " + os.environ['FCST_VAR_NAME'] + " " + fcst_var_level_list[vl] + " " + os.environ["FCST_VAR_THRESH_LIST"] + " " + os.environ['FCST_VAR_EXTRA']) self.logger.debug("OBS VAR: " + os.environ['OBS_VAR_NAME'] + " " + obs_var_level_list[vl] + " " + os.environ["OBS_VAR_THRESH_LIST"] + " " + os.environ['OBS_VAR_EXTRA']) self.logger.debug("INTERP: " + os.environ['INTERP']) self.logger.debug("REGION: " + os.environ["REGION"]) self.logger.debug("LEAD_LIST: " + os.environ["LEAD_LIST"]) self.logger.debug("EVENT_EQUALIZATION: " + os.environ['EVENT_EQUALIZATION']) self.logger.debug("CI_METHOD: " + os.environ['CI_METHOD']) self.logger.debug("VERIF_GRID: " + os.environ['VERIF_GRID']) self.logger.debug("MODEL_NAME_LIST: " + os.environ['MODEL_NAME_LIST']) self.logger.debug("MODEL_PLOT_NAME_LIST: " + os.environ['MODEL_PLOT_NAME_LIST']) self.logger.debug("PLOT_STATS_LIST: " + os.environ['PLOT_STATS_LIST']) cmd = self.get_command() if cmd is None: self.logger.error( "ERROR: make_plots could not generate command for " + self.plotting_script) return self.build() self.clear()
base_name+='_desc'+desc if obs_lead != '': base_name+='_obs_lead'+obs_lead if interp_pnts != '': base_name+='_interp_pnts'+interp_pnts if cov_thresh != '': cov_thresh_symbol, cov_thresh_letter = plot_util.format_thresh( cov_thresh ) base_name+='_cov_thresh'+cov_thresh_letter.replace(',', '_') if alpha != '': base_name+='_alpha'+alpha # Set up expected date in MET .stat file and date plot information plot_time_dates, expected_stat_file_dates = plot_util.get_date_arrays( date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, fcst_lead ) total_dates = len(plot_time_dates) if len(plot_time_dates) == 0: logger.error("Date array constructed information from METplus " +"conf file has length of 0. Not enough information " +"was provided to build date information. Please check " +"provided VALID/INIT_BEG/END and " +"OBS/FCST_INIT/VALID_HOUR_LIST") exit(1) elif len(plot_time_dates) <= 3: date_tick_intvl = 1 elif len(plot_time_dates) > 3 and len(plot_time_dates) <= 10: date_tick_intvl = 2 elif len(plot_time_dates) > 10 and len(plot_time_dates) < 31:
def test_get_date_arrays(): # Independently test the creation of # the date arrays, one used for plotting # the other the expected dates in the # MET .stat file format # Test 1 date_type = 'VALID' date_beg = '20190101' date_end = '20190105' fcst_valid_hour = '000000' fcst_init_hour = '000000' obs_valid_hour = '' obs_init_hour = '' lead = '240000' date_base = datetime.datetime(2019, 1, 1) date_array = np.array( [date_base + datetime.timedelta(days=i) for i in range(5)]) expected_plot_time_dates = [] expected_expected_stat_file_dates = [] for date in date_array: dt = date.time() seconds = (dt.hour * 60 + dt.minute) * 60 + dt.second expected_plot_time_dates.append(date.toordinal() + seconds / 86400.) expected_expected_stat_file_dates.append( date.strftime('%Y%m%d_%H%M%S')) test_plot_time_dates, test_expected_stat_file_dates = ( plot_util.get_date_arrays(date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, lead)) assert (len(test_plot_time_dates) == len(expected_plot_time_dates)) for l in range(len(test_plot_time_dates)): assert (test_plot_time_dates[l] == expected_plot_time_dates[l]) assert (len(test_expected_stat_file_dates) == len( expected_expected_stat_file_dates)) for l in range(len(test_expected_stat_file_dates)): assert (test_expected_stat_file_dates[l] == expected_expected_stat_file_dates[l]) # Test 2 date_type = 'VALID' date_beg = '20190101' date_end = '20190105' fcst_valid_hour = '000000, 060000, 120000, 180000' fcst_init_hour = '000000, 060000, 120000, 180000' obs_valid_hour = '' obs_init_hour = '' lead = '480000' date_base = datetime.datetime(2019, 1, 1) date_array = np.array( [date_base + datetime.timedelta(hours=i) for i in range(0, 120, 6)]) expected_plot_time_dates = [] expected_expected_stat_file_dates = [] for date in date_array: dt = date.time() seconds = (dt.hour * 60 + dt.minute) * 60 + dt.second expected_plot_time_dates.append(date.toordinal() + seconds / 86400.) expected_expected_stat_file_dates.append( date.strftime('%Y%m%d_%H%M%S')) test_plot_time_dates, test_expected_stat_file_dates = ( plot_util.get_date_arrays(date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, lead)) assert (len(test_plot_time_dates) == len(expected_plot_time_dates)) for l in range(len(test_plot_time_dates)): assert (test_plot_time_dates[l] == expected_plot_time_dates[l]) assert (len(test_expected_stat_file_dates) == len( expected_expected_stat_file_dates)) for l in range(len(test_expected_stat_file_dates)): assert (test_expected_stat_file_dates[l] == expected_expected_stat_file_dates[l]) # Test 3 date_type = 'INIT' date_beg = '20190101' date_end = '20190105' fcst_valid_hour = '000000' fcst_init_hour = '000000' obs_valid_hour = '' obs_init_hour = '' lead = '360000' date_base = datetime.datetime(2019, 1, 1) date_array = np.array( [date_base + datetime.timedelta(days=i) for i in range(5)]) lead_hour_seconds = int(int(lead[:-4])) * 3600 lead_min_seconds = int(lead[-4:-2]) * 60 lead_seconds = int(lead[-2:]) lead_offset = datetime.timedelta(seconds=lead_hour_seconds + lead_min_seconds + lead_seconds) expected_plot_time_dates = [] expected_expected_stat_file_dates = [] for date in date_array: dt = date.time() seconds = (dt.hour * 60 + dt.minute) * 60 + dt.second expected_plot_time_dates.append(date.toordinal() + seconds / 86400.) expected_expected_stat_file_dates.append( (date + lead_offset).strftime('%Y%m%d_%H%M%S')) test_plot_time_dates, test_expected_stat_file_dates = ( plot_util.get_date_arrays(date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, lead)) assert (len(test_plot_time_dates) == len(expected_plot_time_dates)) for l in range(len(test_plot_time_dates)): assert (test_plot_time_dates[l] == expected_plot_time_dates[l]) assert (len(test_expected_stat_file_dates) == len( expected_expected_stat_file_dates)) for l in range(len(test_expected_stat_file_dates)): assert (test_expected_stat_file_dates[l] == expected_expected_stat_file_dates[l]) # Test 4 date_type = 'INIT' date_beg = '20190101' date_end = '20190105' fcst_valid_hour = '000000, 060000, 120000, 180000' fcst_init_hour = '000000, 060000, 120000, 180000' obs_valid_hour = '' obs_init_hour = '' lead = '120000' date_base = datetime.datetime(2019, 1, 1) date_array = np.array( [date_base + datetime.timedelta(hours=i) for i in range(0, 120, 6)]) lead_hour_seconds = int(int(lead[:-4])) * 3600 lead_min_seconds = int(lead[-4:-2]) * 60 lead_seconds = int(lead[-2:]) lead_offset = datetime.timedelta(seconds=lead_hour_seconds + lead_min_seconds + lead_seconds) expected_plot_time_dates = [] expected_expected_stat_file_dates = [] for date in date_array: dt = date.time() seconds = (dt.hour * 60 + dt.minute) * 60 + dt.second expected_plot_time_dates.append(date.toordinal() + seconds / 86400.) expected_expected_stat_file_dates.append( (date + lead_offset).strftime('%Y%m%d_%H%M%S')) test_plot_time_dates, test_expected_stat_file_dates = ( plot_util.get_date_arrays(date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, lead)) assert (len(test_plot_time_dates) == len(expected_plot_time_dates)) for l in range(len(test_plot_time_dates)): assert (test_plot_time_dates[l] == expected_plot_time_dates[l]) assert (len(test_expected_stat_file_dates) == len( expected_expected_stat_file_dates)) for l in range(len(test_expected_stat_file_dates)): assert (test_expected_stat_file_dates[l] == expected_expected_stat_file_dates[l])
if alpha != '': base_name += '_alpha' + alpha # Reading in model .stat files from stat_analysis logger.info("Reading in model data") for model_info in model_info_list: model_num = model_info_list.index(model_info) + 1 model_name = model_info[0] model_plot_name = model_info[1] model_obtype = model_info[2] for fl in range(len(fcst_leads)): fcst_lead = fcst_leads[fl] # Set up expected date in MET .stat file # and date plot information plot_time_dates, expected_stat_file_dates = ( plot_util.get_date_arrays(date_type, date_beg, date_end, fcst_valid_hour, fcst_init_hour, obs_valid_hour, obs_init_hour, fcst_lead)) total_dates = len(plot_time_dates) if len(plot_time_dates) == 0: logger.error("Date array constructed information from " + "METplus conf file has length of 0. Not enough " + "information was provided to build date " + "information. Please check provided " + "VALID/INIT_BEG/END and " + "OBS/FCST_INIT/VALID_HOUR_LIST") exit(1) elif len(plot_time_dates) <= 3: date_tick_intvl = 1 elif len(plot_time_dates) > 3 and len(plot_time_dates) <= 10: date_tick_intvl = 2 elif len(plot_time_dates) > 10 and len(plot_time_dates) < 31: