def run_diags(self):
        ''' Runs the diagnostics. What did you think it did? '''
        self.obs_dict = self.load_obs_dict()
        self.regions_dict = self.create_regions_dict()

        for self.var_name_long in self.parameter.vars:
            self.var = self.var_name_long.split('_')[0]

            if self.var not in self.obs_dict:
                logging.getLogger("pcmdi_metrics").error(
                    'Variable %s not in obs_dict' % self.var)
                continue

            self.output_metric = OutputMetrics(self.parameter,
                                               self.var_name_long,
                                               self.obs_dict,
                                               sftlf=self.sftlf)

            for region in self.regions_dict[self.var]:
                self.region = self.create_region(region)
                # Need to add the region to the output dict now b/c
                # otherwise if done later, sometimes it's not added due to
                # premature break in the for loops for reference and test.
                self.output_metric.add_region(self.region)
                # Runs obs vs obs, obs vs model, or model vs model
                self.run_reference_and_test_comparison()
            self.output_metric.write_on_exit()
示例#2
0
    def run_reference_and_test_comparison(self):
        '''  Does the (obs or model) vs (obs or model) comparison. '''
        reference_data_set = self.parameter.reference_data_set
        test_data_set = self.parameter.test_data_set

        reference_data_set_is_obs = self.is_data_set_obs(reference_data_set)
        test_data_set_is_obs = self.is_data_set_obs(test_data_set)

        # If either the reference or test are obs, the data sets
        # themselves need to be modified.
        if reference_data_set_is_obs:
            reference_data_set = Observation.setup_obs_list_from_parameter(
                reference_data_set, self.obs_dict, self.var)
        if test_data_set_is_obs:
            test_data_set = Observation.setup_obs_list_from_parameter(
                test_data_set, self.obs_dict, self.var)

        if len(reference_data_set) == 0:  # We did not find any ref!!!
            raise RuntimeError("No reference dataset found!")

        # self.reference/self.test are either an obs or model
        for reference in reference_data_set:
            try:
                ref = self.determine_obs_or_model(reference_data_set_is_obs,
                                                  reference, self.parameter.reference_data_path)
            # TODO Make this a custom exception. This exception is for
            # when a model doesn't have sftlf for a given region
            except RuntimeError:
                continue

            for test in test_data_set:
                logging.getLogger("pcmdi_metrics").info("TEST DATA IS: {}".format(test))
                self.output_metric = OutputMetrics(self.parameter, self.var_name_long,
                                                   self.obs_dict, sftlf=self.sftlf)
                self.output_metric.add_region(self.region)
                try:
                    tst = self.determine_obs_or_model(test_data_set_is_obs,
                                                      test, self.parameter.test_data_path)
                    self.output_metric.obs_or_model = tst.obs_or_model
                # TODO Make this a custom exception. This exception is for
                # when a model doesn't have sftlf for a given region
                except RuntimeError:
                    continue
                except Exception as err:
                    logging.getLogger("pcmdi_metrics").info("Unexpected error: {e}".format(e=err))
                    break

                try:
                    self.output_metric.calculate_and_output_metrics(ref, tst)
                except RuntimeError:
                    continue
                except Exception as err:
                    err_msg = "Unexpected error in calculate output metrics: {e}".format(e=err)
                    logging.getLogger("pcmdi_metrics").info(err_msg)
                    break