Esempio n. 1
0
    def test_caching(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file, None, 0.05)
        data.read_model('chl')
        self.assertTrue(hasattr(data, 'chl'))
        data.read_model('dox')
        self.assertTrue(hasattr(data, 'dox'))
        self.assertFalse(hasattr(data, 'chl'))

        data = Data(test_file, None, 2)
        data.read_model('chl')
        self.assertTrue(hasattr(data, 'chl'))
        data.read_model('dox')
        self.assertTrue(hasattr(data, 'dox'))
        self.assertTrue(hasattr(data, 'chl'))
Esempio n. 2
0
 def test_remove_empty_matchups(self):
     data = Data(self.path + 'resources/test_single_model_fill_values.nc')
     me = MatchupEngine(data)
     matchups = me.find_all_matchups()
     self.assertEqual(3, len(matchups))
     matchups = me.remove_empty_matchups(matchups)
     self.assertEqual(2, len(matchups))
Esempio n. 3
0
def load(filename, ref_filename=None, config=None):
    """
    Returns an abstraction view on the data from the given input file.
    @param filename: the source file.
    @param ref_filename: the file containing the reference data; if None, the reference data is assumed to be in the source file.
    @return: a 'Data' object.
    """
    if config is not None:
        max_cache_size = config.max_cache_size
    else:
        max_cache_size = None
    return Data(filename, ref_filename, max_cache_size)
Esempio n. 4
0
    def test_find_matchups_single_no_depth(self):
        data = Data(self.path + 'resources/test_without_depth.nc')
        config = Configuration(time_delta=10)
        me = MatchupEngine(data, config)

        reference_record = ReferenceRecord(0, 55.20123, 6.30048, 1261447205,
                                           None)
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertAlmostEqual(55.20123, matchup.reference_record.lat)
        self.assertAlmostEqual(6.30048, matchup.reference_record.lon)
        self.assertAlmostEqual(1261447205, matchup.reference_record.time)
        self.assertIsNone(matchup.reference_record.depth)
Esempio n. 5
0
 def test_data_works_with_split_files(self):
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test_without_records.nc"
     test_ref_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test_only_reference.nc"
     data = Data(test_file, test_ref_file)
     self.assertEqual(2, len(data.model_vars()))
     self.assertTrue('chl' in data.model_vars())
     self.assertTrue('sst' in data.model_vars())
     self.assertEqual(1, len(data.ref_vars()))
     self.assertTrue('chl_ref' in data.ref_vars())
     self.assertEqual(4, len(data.reference_coordinate_variables()))
Esempio n. 6
0
class MatchupEngine_test(TestCase):
    def setUp(self):
        self.path = os.path.dirname(os.path.realpath(__file__)) + '/../'
        self.data = Data(self.path + 'resources/test.nc')

    def tearDown(self):
        self.data.close()

    def test_find_pixel_positions_small_max_delta(self):
        me = MatchupEngine(self.data, Configuration())
        pixel_position = me.find_matchup_position(55.8, 6.0)
        np.assert_array_almost_equal((1, 0, 5.8, 55.2), pixel_position)

    def test_find_pixel_positions_huge_max_delta(self):
        me = MatchupEngine(self.data, Configuration())
        pixel_position = me.find_matchup_position(55.8, 6.0)
        np.assert_array_almost_equal((1, 0, 5.8, 55.2), pixel_position)

    def test_find_time_positions_huge_delta(self):
        me = MatchupEngine(self.data, Configuration(time_delta=100000))
        time_position = me.find_matchup_times(1261440250)[0]
        np.assert_array_almost_equal((0, 1261440000), time_position)

    def test_find_time_positions_small_delta(self):
        me = MatchupEngine(self.data, Configuration(time_delta=6))
        time_positions = me.find_matchup_times(1261447205)[0]
        np.assert_array_almost_equal((1, 1261447200), time_positions)

    def test_find_matchup(self):
        reference_record = ReferenceRecord(0, 55.3, 5.5, 1261440252, 0.0012)
        me = MatchupEngine(self.data, Configuration())
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertEqual(55.3, matchup.reference_record.lat)
        self.assertEqual(5.5, matchup.reference_record.lon)
        self.assertEqual(1261440252, matchup.reference_record.time)
        self.assertEqual(0.0012, matchup.reference_record.depth)
        self.assertAlmostEqual(0.1111,
                               matchup.get_model_value('chl', self.data), 5)
        self.assertAlmostEqual(1.1111,
                               matchup.get_model_value('sst', self.data), 5)
        self.assertAlmostEqual(0.1,
                               matchup.get_ref_value('chl_ref', self.data), 5)

    def test_find_matchups_single(self):
        reference_record = ReferenceRecord(0, 55.20123, 6.30048, 1261447205,
                                           0.0020015)
        config = Configuration(time_delta=10, depth_delta=0.0001)
        me = MatchupEngine(self.data, config)
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertAlmostEqual(55.20123, matchup.reference_record.lat)
        self.assertAlmostEqual(6.30048, matchup.reference_record.lon)
        self.assertAlmostEqual(1261447205, matchup.reference_record.time)
        self.assertAlmostEqual(0.0020015, matchup.reference_record.depth)

    def test_find_matchups_single_no_depth(self):
        data = Data(self.path + 'resources/test_without_depth.nc')
        config = Configuration(time_delta=10)
        me = MatchupEngine(data, config)

        reference_record = ReferenceRecord(0, 55.20123, 6.30048, 1261447205,
                                           None)
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertAlmostEqual(55.20123, matchup.reference_record.lat)
        self.assertAlmostEqual(6.30048, matchup.reference_record.lon)
        self.assertAlmostEqual(1261447205, matchup.reference_record.time)
        self.assertIsNone(matchup.reference_record.depth)

    def test_find_ref_coordinate_names(self):
        ref_coord_variable_names = ['lat_ref', 'ref_lon', 'reftime']
        lat, lon, time, depth = find_ref_coordinate_names(
            ref_coord_variable_names)
        self.assertEqual('lat_ref', lat)
        self.assertEqual('ref_lon', lon)
        self.assertEqual('reftime', time)
        self.assertEqual(None, depth)

    def test_find_all_matchups(self):
        me = MatchupEngine(self.data, Configuration())
        all_matchups = me.find_all_matchups()
        self.assertIsNotNone(all_matchups)
        expected_matchup_count = 3  #reference_records
        self.assertEqual(expected_matchup_count, len(all_matchups))
        matchup = all_matchups[0]
        self.assertAlmostEqual(55.21, matchup.reference_record.lat, 5)
        self.assertAlmostEqual(5.31, matchup.reference_record.lon, 5)
        self.assertEqual(1261440250, matchup.reference_record.time)
        self.assertAlmostEqual(0.0012, matchup.reference_record.depth, 5)

    def test_normalise(self):
        self.assertEqual(type(3), type(normalise(2.5, 3)))
        self.assertEqual(3, normalise(10.5, 3))
        self.assertEqual(1, normalise(0.5, 10))
        self.assertEqual(3, normalise(2.500001, 10))
        self.assertEqual(2, normalise(2.499999, 10))

    def test_remove_empty_matchups(self):
        data = Data(self.path + 'resources/test_single_model_fill_values.nc')
        me = MatchupEngine(data)
        matchups = me.find_all_matchups()
        self.assertEqual(3, len(matchups))
        matchups = me.remove_empty_matchups(matchups)
        self.assertEqual(2, len(matchups))

    def test_exclude_reference_records_with_out_of_bounds_lats(self):
        reference_record = ReferenceRecord(0, 54.1, 5.5, 1261440252, 0.0012)
        me = MatchupEngine(self.data, Configuration())
        matchups = me.find_matchups(reference_record)
        self.assertEqual(0, len(matchups))

    @unittest.skip('shall not run in production environment')
    def test_find_matchups_with_gridded_reference_variable(self):
        data = Data(self.path + 'resources/ogs_test_smaller.nc')
        me = MatchupEngine(data, Configuration())
        matchups = me.find_all_matchups()
        self.assertIsNotNone(matchups)
        self.assertEqual(6560, len(matchups))
Esempio n. 7
0
 def test_find_matchups_with_gridded_reference_variable(self):
     data = Data(self.path + 'resources/ogs_test_smaller.nc')
     me = MatchupEngine(data, Configuration())
     matchups = me.find_all_matchups()
     self.assertIsNotNone(matchups)
     self.assertEqual(6560, len(matchups))
Esempio n. 8
0
 def test_compute_variable_size(self):
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
     data = Data(test_file)
     self.assertAlmostEqual(0.0250244140625, data.compute_variable_size('chl'))
     self.assertAlmostEqual(0.0250244140625, data.compute_variable_size('dox'))
     self.assertAlmostEqual(0.00030517578125, data.compute_variable_size('longitude'))
Esempio n. 9
0
 def setUp(self):
     path = os.path.dirname(os.path.realpath(__file__)) + '/../'
     self.data = Data(path + 'resources/test.nc')
     self.config = Configuration(time_delta=86400, depth_delta=12, ddof=0, alpha=1, beta=1)
     self.me = MatchupEngine(self.data, self.config)
Esempio n. 10
0
class Processor_test(TestCase):

    def setUp(self):
        path = os.path.dirname(os.path.realpath(__file__)) + '/../'
        self.data = Data(path + 'resources/test.nc')
        self.config = Configuration(time_delta=86400, depth_delta=12, ddof=0, alpha=1, beta=1)
        self.me = MatchupEngine(self.data, self.config)


    def tearDown(self):
        self.data.close()


    def test_compute_statistics(self):
        model_values = np.array(range(1, 5, 1)) # [1, 2, 3, 4]
        ref_values = np.array([1.1, 2.2, 2.9, 3.7])
        stats = calculate_statistics(model_values=model_values, reference_values=ref_values, config=self.config)
        self.assertIsNone(stats['model_name'])
        self.assertIsNone(stats['ref_name'])
        self.assertAlmostEqual(0.192028, stats['unbiased_rmse'], 5)
        self.assertAlmostEqual(0.193649, stats['rmse'], 5)
        self.assertAlmostEqual(0.2010936411, stats['normalised_rmse'], 5)
        self.assertAlmostEqual(-1.0101, stats['pbias'], 5)
        self.assertAlmostEqual(0.025, stats['bias'], 5)
        self.assertAlmostEqual(0.99519, stats['corrcoeff'], 5)
        self.assertAlmostEqual(1.03521, stats['reliability_index'], 5)
        self.assertAlmostEqual(0.9588759, stats['model_efficiency'], 5)
        self.assertAlmostEqual(2.5, stats['mean'], 5)
        self.assertAlmostEqual(2.475, stats['ref_mean'], 5)
        self.assertAlmostEqual(1.11803, stats['stddev'], 5)
        self.assertAlmostEqual(0.954921, stats['ref_stddev'], 5)
        self.assertAlmostEqual(1.170808, stats['normalised_stddev'], 5)
        self.assertAlmostEqual(2.5, stats['median'], 5)
        self.assertAlmostEqual(2.55, stats['ref_median'], 5)
        self.assertAlmostEqual(3.7, stats['p90'], 5)
        self.assertAlmostEqual(3.46, stats['ref_p90'], 5)
        self.assertAlmostEqual(3.85, stats['p95'], 5)
        self.assertAlmostEqual(3.58, stats['ref_p95'], 5)
        self.assertAlmostEqual(1, stats['min'], 5)
        self.assertAlmostEqual(1.1, stats['ref_min'], 5)
        self.assertAlmostEqual(4, stats['max'], 5)
        self.assertAlmostEqual(3.7, stats['ref_max'], 5)

        self.assertAlmostEqual(stats['rmse'] ** 2, stats['bias'] ** 2 + stats['unbiased_rmse'] ** 2, 5)


    def test_compute_statistics_with_masked_values(self):
        model_values = ma.array(np.arange(1.0, 5.0, 1), mask=np.array([False, False, True, False])) # [1, 2, --, 4]
        ref_values = ma.array([1.1, 2.2, 2.9, 3.7])
        ref_values, model_values = utils.harmonise(ref_values, model_values)
        ref_values = ref_values.compressed()
        model_values = model_values.compressed()
        stats = calculate_statistics(model_values=model_values, reference_values=ref_values, config=self.config, model_name='kate', ref_name='ref')
        self.assertEqual('kate', stats['model_name'])
        self.assertEqual('ref', stats['ref_name'])
        self.assertAlmostEqual(0.216024, stats['unbiased_rmse'], 5)
        self.assertAlmostEqual(0.216024, stats['rmse'], 5)
        self.assertAlmostEqual(6.344131e-15, stats['pbias'], 5)
        self.assertAlmostEqual(0.0, stats['bias'], 5)
        self.assertAlmostEqual(0.99484975, stats['corrcoeff'], 5)
        self.assertAlmostEqual(1.039815, stats['reliability_index'], 5)
        self.assertAlmostEqual(0.9589041, stats['model_efficiency'], 5)
        self.assertAlmostEqual(2.33333, stats['mean'], 5)
        self.assertAlmostEqual(2.33333, stats['ref_mean'], 5)
        self.assertAlmostEqual(1.24722, stats['stddev'], 5)
        self.assertAlmostEqual(1.06562, stats['ref_stddev'], 5)
        self.assertAlmostEqual(1.17041, stats['normalised_stddev'], 5)
        self.assertAlmostEqual(2, stats['median'], 5)
        self.assertAlmostEqual(2.2, stats['ref_median'], 5)
        self.assertAlmostEqual(3.6, stats['p90'], 5)
        self.assertAlmostEqual(3.4, stats['ref_p90'], 5)
        self.assertAlmostEqual(3.8, stats['p95'], 5)
        self.assertAlmostEqual(3.55, stats['ref_p95'], 5)
        self.assertAlmostEqual(1, stats['min'], 5)
        self.assertAlmostEqual(1.1, stats['ref_min'], 5)
        self.assertAlmostEqual(4, stats['max'], 5)
        self.assertAlmostEqual(3.7, stats['ref_max'], 5)

        self.assertAlmostEqual(stats['rmse'] ** 2, stats['bias'] ** 2 + stats['unbiased_rmse'] ** 2, 5)


    def test_compute_statistics_with_extreme_model_values(self):
        model_values = np.array(range(1, 5, 1)) # [1, 2, 3, 4]
        ref_values = np.array([1, 1, 1, 1])
        stats = calculate_statistics(model_values=model_values, reference_values=ref_values, config=self.config)
        self.assertAlmostEqual(1.118034, stats['unbiased_rmse'], 5)
        self.assertAlmostEqual(1.870829, stats['rmse'], 5)
        self.assertAlmostEqual(-150, stats['pbias'], 5)
        self.assertAlmostEqual(1.5, stats['bias'], 5)
        self.assertTrue(np.isnan(stats['corrcoeff']))
        self.assertAlmostEqual(1.5106421, stats['reliability_index'], 5)
        self.assertTrue(np.isnan(stats['model_efficiency']))
        self.assertAlmostEqual(2.5, stats['mean'], 5)
        self.assertAlmostEqual(1, stats['ref_mean'], 5)
        self.assertAlmostEqual(1.11803, stats['stddev'], 5)
        self.assertAlmostEqual(0.0, stats['ref_stddev'], 5)
        self.assertTrue(np.isnan, stats['normalised_stddev'])
        self.assertAlmostEqual(2.5, stats['median'], 5)
        self.assertAlmostEqual(1, stats['ref_median'], 5)
        self.assertAlmostEqual(3.7, stats['p90'], 5)
        self.assertAlmostEqual(1, stats['ref_p90'], 5)
        self.assertAlmostEqual(3.85, stats['p95'], 5)
        self.assertAlmostEqual(1, stats['ref_p95'], 5)
        self.assertAlmostEqual(1, stats['min'], 5)
        self.assertAlmostEqual(1, stats['ref_min'], 5)
        self.assertAlmostEqual(4, stats['max'], 5)
        self.assertAlmostEqual(1, stats['ref_max'], 5)

        self.assertAlmostEqual(stats['rmse'] ** 2, stats['bias'] ** 2 + stats['unbiased_rmse'] ** 2, 5)


    def test_compute_statistics_with_extreme_reference_values(self):
        model_values = np.array([1, 1, 1, 1])
        ref_values = np.array([1.1, 2.2, 2.9, 3.7])
        stats = calculate_statistics(model_values=model_values, reference_values=ref_values, config=self.config)
        self.assertAlmostEqual(0.954921, stats['unbiased_rmse'], 5)
        self.assertAlmostEqual(1.757128, stats['rmse'], 5)
        self.assertAlmostEqual(59.595959, stats['pbias'], 5)
        self.assertAlmostEqual(-1.475, stats['bias'], 5)
        self.assertTrue(np.isnan(stats['corrcoeff']))
        self.assertAlmostEqual(1.49908579, stats['reliability_index'], 5)
        self.assertAlmostEqual(-2.38588, stats['model_efficiency'], 5)
        self.assertAlmostEqual(1.0, stats['mean'], 5)
        self.assertAlmostEqual(2.475, stats['ref_mean'], 5)
        self.assertAlmostEqual(0, stats['stddev'], 5)
        self.assertAlmostEqual(0.954921, stats['ref_stddev'], 5)
        self.assertAlmostEqual(0.0, stats['normalised_stddev'], 5)
        self.assertAlmostEqual(1, stats['median'], 5)
        self.assertAlmostEqual(2.545, stats['ref_median'], 2)
        self.assertAlmostEqual(1, stats['p90'], 5)
        self.assertAlmostEqual(3.46, stats['ref_p90'], 5)
        self.assertAlmostEqual(1, stats['p95'], 5)
        self.assertAlmostEqual(3.58, stats['ref_p95'], 2)
        self.assertAlmostEqual(1, stats['min'], 5)
        self.assertAlmostEqual(1.1, stats['ref_min'], 5)
        self.assertAlmostEqual(1, stats['max'], 5)
        self.assertAlmostEqual(3.7, stats['ref_max'], 5)

        self.assertAlmostEqual(stats['rmse'] ** 2, stats['bias'] ** 2 + stats['unbiased_rmse'] ** 2, 5)
Esempio n. 11
0
def main():
    parsed_args = parse_arguments(sys.argv[1:])
    config = Configuration(properties_file_name=parsed_args.config, target_dir=parsed_args.output_dir,
                           target_prefix=parsed_args.prefix)
    file_handler = setup_logging(config)
    if parsed_args.reference_file is not None:
        data = Data(parsed_args.path, parsed_args.reference_file, config.max_cache_size)
    else:
        data = Data(parsed_args.path, max_cache_size=config.max_cache_size)

    output = Output(config=config)

    matchups = None
    if data.has_one_dim_ref_var():
        me = MatchupEngine(data, config)
        matchups = me.find_all_matchups()
        if not matchups:
            logging.warning('No matchups found. System will exit.')
            exit(0)
        if config.remove_empty_matchups:
            matchups = me.remove_empty_matchups(matchups)

    if not os.name == 'nt':
        logging.debug('Memory after matchups have been found: %s' % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)

    matchup_count = 0 if matchups is None else len(matchups)
    collected_statistics = {}
    density_plot_files = []
    target_files = []
    density_plots = {}

    for (model_name, ref_name) in parsed_args.variable_mappings:
        unit = data.unit(model_name)
        is_gridded = len(data.get_reference_dimensions(ref_name)) > 1
        if is_gridded:
            reference_values, model_values = data.get_values(ref_name, model_name)
            matchup_count += ma.count(reference_values)
        else:
            reference_values, model_values = utils.extract_values(matchups, data, ref_name, model_name)
            reference_values, model_values = utils.harmonise(reference_values, model_values)
            logging.debug('Compressing ref-variable %s' % ref_name)
            reference_values = reference_values.compressed()
            logging.debug('Compressing model-variable %s' % model_name)
            model_values = model_values.compressed()

        logging.info('Calculating statistics for \'%s\' with \'%s\'' % (model_name, ref_name))
        stats = processor.calculate_statistics(model_values, reference_values, model_name, ref_name, unit, config)
        collected_statistics[(model_name, ref_name)] = stats

        if config.write_density_plots:
            axis_min = min(stats['min'], stats['ref_min'])
            axis_max = max(stats['p90'], stats['ref_p90'])
            logging.info('Creating density plot for \'%s\' and \'%s\'' % (model_name, ref_name))
            density_plots[model_name + ref_name] = output.density_plot(model_name, ref_name, model_values,
                                                                       reference_values, config.density_plot_log_scaled,
                                                                       None, axis_min, axis_max, data.unit(model_name))

    if not os.name == 'nt':
        logging.debug(
            'Memory after statistics have been computed: %s' % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)

    if config.write_csv:
        csv_target_file = '%s/%sstatistics.csv' % (parsed_args.output_dir, config.target_prefix)
        target_files.append(csv_target_file)
        output.csv(data, parsed_args.variable_mappings, collected_statistics, matchup_count, matchups=matchups, source_file=parsed_args.path, target_file=csv_target_file)
        logging.info('CSV output written to \'%s\'' % csv_target_file)
        if matchups is not None:
            matchup_filename = '%s_matchups.csv' % os.path.splitext(csv_target_file)[0]
            logging.info('Matchups written to \'%s\'' % matchup_filename)
            target_files.append(matchup_filename)

    taylor_target_files = []
    if config.write_taylor_diagrams:
        taylor_target_file = '%s/%staylor.png' % (parsed_args.output_dir, config.target_prefix)
        written_taylor_diagrams, d = output.taylor(list(collected_statistics.values()), taylor_target_file)
        del d
        if written_taylor_diagrams:
            for written_taylor_diagram in written_taylor_diagrams:
                logging.info('Taylor diagram written to \'%s\'' % written_taylor_diagram)
                target_files.append(written_taylor_diagram)
                taylor_target_files.append(written_taylor_diagram)

    if config.write_density_plots:
        for (model_name, ref_name) in parsed_args.variable_mappings:
            density_target = '%s/density-%s-%s.png' % (parsed_args.output_dir, model_name, ref_name)
            density_plot_files.append(density_target)
            target_files.append(density_target)
            output.write_density_plot(density_plots[model_name + ref_name], density_target)
            logging.info('Density plot written to \'%s\'' % density_target)

    target_diagram_file = None
    if config.write_target_diagram:
        target_diagram_file = '%s/%starget.png' % (parsed_args.output_dir, config.target_prefix)
        output.target_diagram(list(collected_statistics.values()), target_diagram_file)
        logging.info('Target diagram written to \'%s\'' % target_diagram_file)
        target_files.append(target_diagram_file)

    if config.write_xhtml:
        xml_target_file = '%s/%sreport.xml' % (parsed_args.output_dir, config.target_prefix)
        path = str(os.path.dirname(os.path.realpath(__file__))) + '/../resources/'
        xsl = path + 'analysis-summary.xsl'
        css = path + 'styleset.css'
        xsl_target = '%s/%s' % (parsed_args.output_dir, os.path.basename(xsl))
        css_target = '%s/%s' % (parsed_args.output_dir, os.path.basename(css))
        output.xhtml(list(collected_statistics.values()), matchup_count, matchups, data, xml_target_file, taylor_target_files,
                     target_diagram_file, density_plot_files)
        logging.info('XHTML report written to \'%s\'' % xml_target_file)
        shutil.copy(xsl, parsed_args.output_dir)
        logging.info('XHTML support file written to \'%s/%s\'' % (parsed_args.output_dir, 'analysis-summary.xsl'))
        shutil.copy(css, parsed_args.output_dir)
        logging.info('XHTML support file written to \'%s/%s\'' % (parsed_args.output_dir, 'styleset.xsl'))
        target_files.append(xml_target_file)
        target_files.append(xsl_target)
        target_files.append(css_target)

    if config.zip:
        create_zip(target_files, config, file_handler, parsed_args)

    logging.info('End of process')
Esempio n. 12
0
class Data_test(unittest.TestCase):

    def setUp(self):
        logging.basicConfig(level=logging.DEBUG)
        self.test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test.nc"
        self.data = Data(self.test_file)


    def tearDown(self):
        self.data.close()
        logging.basicConfig(level=logging.WARNING)


    def test_model_vars(self):
        model_vars = self.data.model_vars()
        assert_array_equal(np.array(['chl', 'sst']), model_vars)


    def test_ref_vars(self):
        ref_vars = self.data.ref_vars()
        assert_array_equal(np.array(['chl_ref']), ref_vars)


    def test_reference_records_count(self):
        self.assertEqual(3, self.data.reference_records_count({'record_num'}))


    def test_gridded_reference_records_count(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertEqual(41 * 80, data.reference_records_count({'latitude', 'longitude'}))


    def test_find_model_latitude_variable_name(self):
        self.assertEqual('lat', self.data.find_model_latitude_variable_name())
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertEqual('latitude', data.find_model_latitude_variable_name())


    def test_find_model_longitude_variable_name(self):
        self.assertEqual('lon', self.data.find_model_longitude_variable_name())
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertEqual('longitude', data.find_model_longitude_variable_name())


    def test_var_access(self):
        chl_data = self.data.read_model('chl', [0, 0, 0, 0])
        self.assertAlmostEqual(0.1111, chl_data)


    def test_data_is_read_only_once_full_variable(self):
        self.data.read_model('chl')
        chl_data = self.data.read_model('chl', [0, 0, 0, 0])
        self.assertAlmostEqual(0.1111, chl_data)

        self.data.__getattribute__('chl')[0][0][0][0] = 0.5
        chl_data = self.data.read_model('chl', [0, 0, 0, 0])
        self.assertAlmostEqual(0.5, chl_data)


    def test_data_works_with_split_files(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test_without_records.nc"
        test_ref_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test_only_reference.nc"
        data = Data(test_file, test_ref_file)
        self.assertEqual(2, len(data.model_vars()))
        self.assertTrue('chl' in data.model_vars())
        self.assertTrue('sst' in data.model_vars())
        self.assertEqual(1, len(data.ref_vars()))
        self.assertTrue('chl_ref' in data.ref_vars())
        self.assertEqual(4, len(data.reference_coordinate_variables()))


    def test_unit(self):
        self.assertEqual('milligram m-3', self.data.unit('chl'))
        self.assertEqual('kelvin', self.data.unit('sst'))
        self.assertEqual('degrees_east', self.data.unit('lon_ref'))
        self.assertIsNone(self.data.unit('var_without_unit'))
        self.assertRaises(ValueError, lambda: self.data.unit('toad_count'))


    def test_gridded_reference_data(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertEqual(1, len(data.ref_vars()))
        self.assertEqual('Ref_chl', data.ref_vars()[0])


    def test_caching(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file, None, 0.05)
        data.read_model('chl')
        self.assertTrue(hasattr(data, 'chl'))
        data.read_model('dox')
        self.assertTrue(hasattr(data, 'dox'))
        self.assertFalse(hasattr(data, 'chl'))

        data = Data(test_file, None, 2)
        data.read_model('chl')
        self.assertTrue(hasattr(data, 'chl'))
        data.read_model('dox')
        self.assertTrue(hasattr(data, 'dox'))
        self.assertTrue(hasattr(data, 'chl'))


    def test_compute_variable_size(self):
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertAlmostEqual(0.0250244140625, data.compute_variable_size('chl'))
        self.assertAlmostEqual(0.0250244140625, data.compute_variable_size('dox'))
        self.assertAlmostEqual(0.00030517578125, data.compute_variable_size('longitude'))

    def test_get_data(self):
        self.data.read_model('chl')
        array = self.data.get_data([0, 0, 0, 0], 'chl')
        assert_array_equal(np.array([0.1111], dtype='float32'), array)

        array = self.data.get_data([1, 0, 1, 2], 'chl')
        assert_array_equal(np.array([0.1223], dtype='float32'), array)
        array = self.data.get_data([1, 0, 1, 3], 'chl')
        assert_array_equal(np.array([0.2223], dtype='float32'), array)

    def test_has_gridded_ref_var(self):
        self.assertTrue(self.data.has_one_dim_ref_var())
        test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
        data = Data(test_file)
        self.assertFalse(data.has_one_dim_ref_var())


    def test_get_differing_dim_names(self):
        data = DataMock()
        dim_indices = data.get_differing_dim_names('model_0', 'ref_0')
        self.assertDictEqual({}, dim_indices)

        dim_indices = data.get_differing_dim_names('model_0', 'ref_1')
        self.assertDictEqual({'depth': 'depth_ref'}, dim_indices)

        dim_indices = data.get_differing_dim_names('model_0', 'ref_2')
        self.assertDictEqual({'time': 'time_ref', 'depth': 'depth_ref'}, dim_indices)

        self.assertRaises(ValueError, lambda : data.get_differing_dim_names('model_0', 'ref_3'))


    def test_get_slices(self):
        data = DataMock()
        model_values_slices, ref_values_slices = data.get_slices('model_0', 'ref_1')
        self.assertListEqual([slice(None), slice(3, 4), slice(None), slice(None)], model_values_slices)
        self.assertListEqual([slice(0, 5), slice(0, 1), slice(0, 5), slice(0, 10)], ref_values_slices)
Esempio n. 13
0
 def setUp(self):
     self.path = os.path.dirname(os.path.realpath(__file__)) + '/../'
     self.data = Data(self.path + 'resources/test.nc')
Esempio n. 14
0
 def test_gridded_reference_data(self):
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
     data = Data(test_file)
     self.assertEqual(1, len(data.ref_vars()))
     self.assertEqual('Ref_chl', data.ref_vars()[0])
Esempio n. 15
0
 def test_find_model_longitude_variable_name(self):
     self.assertEqual('lon', self.data.find_model_longitude_variable_name())
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
     data = Data(test_file)
     self.assertEqual('longitude', data.find_model_longitude_variable_name())
Esempio n. 16
0
 def test_gridded_reference_records_count(self):
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
     data = Data(test_file)
     self.assertEqual(41 * 80, data.reference_records_count({'latitude', 'longitude'}))
Esempio n. 17
0
 def setUp(self):
     logging.basicConfig(level=logging.DEBUG)
     self.test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/test.nc"
     self.data = Data(self.test_file)
Esempio n. 18
0
 def setUp(self):
     self.path = os.path.dirname(os.path.realpath(__file__)) + '/../'
     self.data = Data(self.path + 'resources/test.nc')
 def test_find_reference_records_no_depth(self):
     self.data = Data(self.path + 'resources/test_without_depth.nc')
     rrf = ReferenceRecordsFinder(self.data)
     reference_records = rrf.find_reference_records()
     self.assertEqual(3, len(reference_records))
Esempio n. 20
0
class MatchupEngine_test(TestCase):

    def setUp(self):
        self.path = os.path.dirname(os.path.realpath(__file__)) + '/../'
        self.data = Data(self.path + 'resources/test.nc')


    def tearDown(self):
        self.data.close()


    def test_find_pixel_positions_small_max_delta(self):
        me = MatchupEngine(self.data, Configuration())
        pixel_position = me.find_matchup_position(55.8, 6.0)
        np.assert_array_almost_equal((1, 0, 5.8, 55.2), pixel_position)


    def test_find_pixel_positions_huge_max_delta(self):
        me = MatchupEngine(self.data, Configuration())
        pixel_position = me.find_matchup_position(55.8, 6.0)
        np.assert_array_almost_equal((1, 0, 5.8, 55.2), pixel_position)


    def test_find_time_positions_huge_delta(self):
        me = MatchupEngine(self.data, Configuration(time_delta=100000))
        time_position = me.find_matchup_times(1261440250)[0]
        np.assert_array_almost_equal((0, 1261440000), time_position)


    def test_find_time_positions_small_delta(self):
        me = MatchupEngine(self.data, Configuration(time_delta=6))
        time_positions = me.find_matchup_times(1261447205)[0]
        np.assert_array_almost_equal((1, 1261447200), time_positions)


    def test_find_matchup(self):
        reference_record = ReferenceRecord(0, 55.3, 5.5, 1261440252, 0.0012)
        me = MatchupEngine(self.data, Configuration())
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertEqual(55.3, matchup.reference_record.lat)
        self.assertEqual(5.5, matchup.reference_record.lon)
        self.assertEqual(1261440252, matchup.reference_record.time)
        self.assertEqual(0.0012, matchup.reference_record.depth)
        self.assertAlmostEqual(0.1111, matchup.get_model_value('chl', self.data), 5)
        self.assertAlmostEqual(1.1111, matchup.get_model_value('sst', self.data), 5)
        self.assertAlmostEqual(0.1, matchup.get_ref_value('chl_ref', self.data), 5)


    def test_find_matchups_single(self):
        reference_record = ReferenceRecord(0, 55.20123, 6.30048, 1261447205, 0.0020015)
        config = Configuration(time_delta=10, depth_delta=0.0001)
        me = MatchupEngine(self.data, config)
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertAlmostEqual(55.20123, matchup.reference_record.lat)
        self.assertAlmostEqual(6.30048, matchup.reference_record.lon)
        self.assertAlmostEqual(1261447205, matchup.reference_record.time)
        self.assertAlmostEqual(0.0020015, matchup.reference_record.depth)


    def test_find_matchups_single_no_depth(self):
        data = Data(self.path + 'resources/test_without_depth.nc')
        config = Configuration(time_delta=10)
        me = MatchupEngine(data, config)

        reference_record = ReferenceRecord(0, 55.20123, 6.30048, 1261447205, None)
        matchups = me.find_matchups(reference_record)
        self.assertEqual(1, len(matchups))
        matchup = matchups[0]
        self.assertIsNotNone(matchup)
        self.assertAlmostEqual(55.20123, matchup.reference_record.lat)
        self.assertAlmostEqual(6.30048, matchup.reference_record.lon)
        self.assertAlmostEqual(1261447205, matchup.reference_record.time)
        self.assertIsNone(matchup.reference_record.depth)


    def test_find_ref_coordinate_names(self):
        ref_coord_variable_names = ['lat_ref', 'ref_lon', 'reftime']
        lat, lon, time, depth = find_ref_coordinate_names(ref_coord_variable_names)
        self.assertEqual('lat_ref', lat)
        self.assertEqual('ref_lon', lon)
        self.assertEqual('reftime', time)
        self.assertEqual(None, depth)


    def test_find_all_matchups(self):
        me = MatchupEngine(self.data, Configuration())
        all_matchups = me.find_all_matchups()
        self.assertIsNotNone(all_matchups)
        expected_matchup_count = 3 #reference_records
        self.assertEqual(expected_matchup_count, len(all_matchups))
        matchup = all_matchups[0]
        self.assertAlmostEqual(55.21, matchup.reference_record.lat, 5)
        self.assertAlmostEqual(5.31, matchup.reference_record.lon, 5)
        self.assertEqual(1261440250, matchup.reference_record.time)
        self.assertAlmostEqual(0.0012, matchup.reference_record.depth, 5)


    def test_normalise(self):
        self.assertEqual(type(3), type(normalise(2.5, 3)))
        self.assertEqual(3, normalise(10.5, 3))
        self.assertEqual(1, normalise(0.5, 10))
        self.assertEqual(3, normalise(2.500001, 10))
        self.assertEqual(2, normalise(2.499999, 10))


    def test_remove_empty_matchups(self):
        data = Data(self.path + 'resources/test_single_model_fill_values.nc')
        me = MatchupEngine(data)
        matchups = me.find_all_matchups()
        self.assertEqual(3, len(matchups))
        matchups = me.remove_empty_matchups(matchups)
        self.assertEqual(2, len(matchups))


    def test_exclude_reference_records_with_out_of_bounds_lats(self):
        reference_record = ReferenceRecord(0, 54.1, 5.5, 1261440252, 0.0012)
        me = MatchupEngine(self.data, Configuration())
        matchups = me.find_matchups(reference_record)
        self.assertEqual(0, len(matchups))


    @unittest.skip('shall not run in production environment')
    def test_find_matchups_with_gridded_reference_variable(self):
        data = Data(self.path + 'resources/ogs_test_smaller.nc')
        me = MatchupEngine(data, Configuration())
        matchups = me.find_all_matchups()
        self.assertIsNotNone(matchups)
        self.assertEqual(6560, len(matchups))
Esempio n. 21
0
 def test_has_gridded_ref_var(self):
     self.assertTrue(self.data.has_one_dim_ref_var())
     test_file = os.path.dirname(os.path.realpath(__file__)) + "/../resources/ogs_test_smaller.nc"
     data = Data(test_file)
     self.assertFalse(data.has_one_dim_ref_var())