Exemplo n.º 1
0
    def test_get_ensemble(self):
        EPSG = 32633
        upper_left_x = 436100.0
        upper_left_y = 7417800.0
        nx = 74
        ny = 94
        dx = 1000.0
        dy = 1000.0
        # Period start
        year = 2015
        month = 7
        day = 26
        hour = 0
        n_hours = 30
        utc = api.Calendar()  # No offset gives Utc
        t0 = api.YMDhms(year, month, day, hour)
        period = api.UtcPeriod(utc.time(t0), utc.time(t0) + api.deltahours(n_hours))
        t_c = utc.time(t0) + api.deltahours(1)

        base_dir = path.join(shyftdata_dir, "netcdf", "arome")
        pattern = "fc*.nc"
        bbox = ([upper_left_x, upper_left_x + nx*dx,
                 upper_left_x + nx*dx, upper_left_x],
                [upper_left_y, upper_left_y,
                 upper_left_y - ny*dy, upper_left_y - ny*dy])
        try:
            repos = AromeDataRepository(EPSG, base_dir, filename=pattern, bounding_box=bbox)
            data_names = ("temperature", "wind_speed", "relative_humidity")
            ensemble = repos.get_forecast_ensemble(data_names, period, t_c, None)
            self.assertTrue(isinstance(ensemble, list))
            self.assertEqual(len(ensemble), 10)
        except AromeDataRepositoryError as adre:
            self.skipTest("(test inconclusive- missing arome-data {0})".format(adre))
Exemplo n.º 2
0
    def test_subsets(self):
        EPSG, bbox = self.arome_epsg_bbox
        # Period start
        year = 2015
        month = 8
        day = 24
        hour = 6
        n_hours = 30
        date_str = "{}{:02}{:02}_{:02}".format(year, month, day, hour)
        utc = api.Calendar()  # No offset gives Utc
        t0 = api.YMDhms(year, month, day, hour)
        period = api.UtcPeriod(utc.time(t0), utc.time(t0) + api.deltahours(n_hours))

        base_dir = path.join(shyftdata_dir, "repository", "arome_data_repository")
        filename = "arome_metcoop_red_default2_5km_{}.nc".format(date_str)

        data_names = ("temperature", "wind_speed", "precipitation", "relative_humidity", "radiation")
        allow_subset = False
        reader = AromeDataRepository(EPSG, base_dir, filename=filename,
                                     bounding_box=bbox, allow_subset=allow_subset)
        with self.assertRaises(AromeDataRepositoryError) as context:
            reader.get_timeseries(data_names, period, None)
        self.assertEqual("Could not find all data fields", context.exception.args[0])
        allow_subset = True
        reader = AromeDataRepository(EPSG, base_dir, filename=filename,
                                     bounding_box=bbox, allow_subset=allow_subset)
        try:
            sources = reader.get_timeseries(data_names, period, None)
        except AromeDataRepositoryError as e:
            self.fail("AromeDataRepository.get_timeseries(data_names, period, None) "
                      "raised AromeDataRepositoryError unexpectedly.")
        self.assertEqual(len(sources), len(data_names) - 1)
Exemplo n.º 3
0
    def test_get_forecast(self):
        # Period start
        year = 2015
        month = 8
        day = 24
        hour = 6
        n_hours = 65
        utc = api.Calendar()  # No offset gives Utc
        t0 = api.YMDhms(year, month, day, hour)
        period = api.UtcPeriod(utc.time(t0), utc.time(t0) + api.deltahours(n_hours))
        t_c1 = utc.time(t0) + api.deltahours(1)
        t_c2 = utc.time(t0) + api.deltahours(7)

        base_dir = path.join(shyftdata_dir, "repository", "arome_data_repository")
        pattern = "arome_metcoop*default2_5km_*.nc"
        EPSG, bbox = self.arome_epsg_bbox

        repos = AromeDataRepository(EPSG, base_dir, filename=pattern, bounding_box=bbox)
        data_names = ("temperature", "wind_speed", "precipitation", "relative_humidity")
        tc1_sources = repos.get_forecast(data_names, period, t_c1, None)
        tc2_sources = repos.get_forecast(data_names, period, t_c2, None)

        self.assertTrue(len(tc1_sources) == len(tc2_sources))
        self.assertTrue(set(tc1_sources) == set(data_names))
        self.assertTrue(tc1_sources["temperature"][0].ts.size() == n_hours + 1)

        tc1_precip = tc1_sources["precipitation"][0].ts
        tc2_precip = tc2_sources["precipitation"][0].ts

        self.assertEqual(tc1_precip.size(), n_hours)
        self.assertTrue(tc1_precip.time(0) != tc2_precip.time(0))
Exemplo n.º 4
0
    def test_tiny_bbox(self):
        EPSG, _ = self.arome_epsg_bbox

        x0 = 436250.0   # lower left
        y0 = 6823250.0  # lower right
        nx = 1
        ny = 1
        dx = 5.0
        dy = 5.0
        bbox = ([x0, x0 + nx*dx, x0 + nx*dx, x0], [y0, y0, y0 + ny*dy, y0 + ny*dy])
        print(bbox)
        
        # Period start
        year = 2015
        month = 8
        day = 24
        hour = 6
        n_hours = 30
        date_str = "{}{:02}{:02}_{:02}".format(year, month, day, hour)
        utc = api.Calendar()  # No offset gives Utc
        t0 = api.YMDhms(year, month, day, hour)
        period = api.UtcPeriod(utc.time(t0), utc.time(t0) + api.deltahours(n_hours))

        base_dir = path.join(shyftdata_dir, "repository", "arome_data_repository")
        filename = "arome_metcoop_red_default2_5km_{}.nc".format(date_str)
        reader = AromeDataRepository(EPSG, base_dir, filename=filename, 
                                     bounding_box=bbox, x_padding=0, y_padding=0)
        data_names = ("temperature", "wind_speed", "precipitation", "relative_humidity")
        try:
            tss = reader.get_timeseries(data_names, period, None)
        except AromeDataRepository as err:
            self.fail("reader.get_timeseries raised AromeDataRepositoryError('{}') "
                      "unexpectedly.".format(err.args[0]))
Exemplo n.º 5
0
 def test_create_region_environment(self):
     cal = api.Calendar()
     time_axis = api.Timeaxis(cal.time(api.YMDhms(2015, 1, 1, 0, 0, 0)), api.deltahours(1), 240)
     re = self.create_dummy_region_environment(time_axis, api.GeoPoint(1000, 1000, 100))
     self.assertIsNotNone(re)
     self.assertEqual(len(re.radiation), 1)
     self.assertAlmostEqual(re.radiation[0].ts.value(0), 300.0)
Exemplo n.º 6
0
 def test_wrong_forecast(self):
     with self.assertRaises(AromeDataRepositoryError) as context:
         utc = api.Calendar()  # No offset gives Utc
         t0 = api.YMDhms(2015, 12, 25, 18)
         period = api.UtcPeriod(utc.time(t0), utc.time(t0) + api.deltahours(30))
         ar1 = AromeDataRepository(32632, shyftdata_dir, filename="plain_wrong_*.nc")
         ar1.get_forecast(("temperature",), period, utc.time(t0), None)
     self.assertTrue(all(x in context.exception.args[0] for x in
                         ["No matches found for file_pattern = ", "and t_c = "]))
Exemplo n.º 7
0
    def test_get_dummy(self):
        """
        #Simple regression test of dummy variables from `utils.dummy_var`
        """
        EPSG, bbox, bpoly = self.senorge_epsg_bbox

        # Period start
        n_days = 5
        t0 = api.YMDhms(1957, 1)
        utc = api.Calendar()  # No offset gives Utc
        period = api.UtcPeriod(utc.time(t0),
                               utc.time(t0) + api.deltahours(n_days * 24))

        base_dir = path.join(shyftdata_dir, "repository",
                             "senorge_data_repository")
        f1 = "senorge_test.nc"

        senorge1 = SeNorgeDataRepository(EPSG,
                                         base_dir,
                                         filename=f1,
                                         allow_subset=True)
        senorge1_data_names = ("radiation", "temperature", "wind_speed")
        sources = senorge1.get_timeseries(senorge1_data_names,
                                          period,
                                          geo_location_criteria=bpoly)
        self.assertTrue(set(sources) == set(senorge1_data_names))
        r0 = sources["radiation"][0].ts
        p0 = sources["temperature"][0].ts
        self.assertTrue(p0.total_period().start <= period.start
                        and p0.total_period().end >= period.end)
        self.assertTrue(r0.time(0), period.start)

        t0 = api.YMDhms(1957, 1)
        period = api.UtcPeriod(utc.time(t0),
                               utc.time(t0) + api.deltahours(n_days * 23))
        sources = senorge1.get_timeseries(senorge1_data_names,
                                          period,
                                          geo_location_criteria=bpoly)
        r0 = sources["radiation"][0].ts
        p0 = sources["temperature"][0].ts
        self.assertTrue(p0.total_period().start <= period.start
                        and p0.total_period().end >= period.end)
        self.assertTrue(r0.time(0) == period.start)
        self.assertTrue(r0.time_axis.time(1) - r0.time_axis.time(0) == 86400)
Exemplo n.º 8
0
 def test_read_forecast(self):
     utc = api.Calendar()
     ds = SmGTsRepository(PROD, FC_PROD)
     nl = [
         u'/LTMS-Abisko........-T0000A5P_EC00_ENS',
         u'/LTMS-Abisko........-T0000A5P_EC00_E04',
         u'/Vikf-Tistel........-T0017A3P_MAN',
         u'/Vikf-Tistel........-T0000A5P_MAN'
     ]  #[u'/ICC-test-v9.2']
     t0 = utc.time(api.YMDhms(2015, 10, 1, 00, 00, 00))
     t1 = utc.time(api.YMDhms(2015, 10, 10, 00, 00, 00))
     p = api.UtcPeriod(t0, t1)
     fclist = ds.read_forecast(nl, p)
     self.assertIsNotNone(fclist)
     fc1 = fclist[u'/LTMS-Abisko........-T0000A5P_EC00_E04']
     fc1_v = [fc1.value(i) for i in range(fc1.size())]
     # test times here, left for manual inspection here fc1_t=[utc.to_string(fc1.time(i)) for i in range(fc1.size())]
     self.assertIsNotNone(fc1_v)
     #values as read from preprod smg:
     fc1_v_expected = [
         0.00, 0.33, 0.33, 0.33, 0.33, 0.33, 0.33, 0.08, 0.08, 0.08,
         0.08, 0.08, 0.08, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.11,
         0.11, 0.11, 0.11, 0.11, 0.11, 0.47, 0.47, 0.47, 0.47, 0.47,
         0.47, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.12, 0.12, 0.12,
         0.12, 0.12, 0.12, 0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.14,
         0.14, 0.14, 0.14, 0.14, 0.14, 0.02, 0.02, 0.02, 0.02, 0.02,
         0.02, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.00, 0.00, 0.00,
         0.00, 0.00, 0.00, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.10,
         0.10, 0.10, 0.10, 0.10, 0.10, 0.08, 0.08, 0.08, 0.08, 0.08,
         0.08, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.23, 0.23, 0.23,
         0.23, 0.23, 0.23, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.01,
         0.01, 0.01, 0.01, 0.01, 0.01, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
         0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.03,
         0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.06, 0.06,
         0.06, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.13, 0.13, 0.13,
         0.13, 0.13, 0.13, 0.10, 0.10, 0.10, 0.10
     ]
     [
         self.assertLess(
             fabs(fc1_v_expected[i] - fc1_v[i]), 0.01,
             "{}:{} !={}".format(i, fc1_v_expected[i], fc1_v[i]))
         for i in range(len(fc1_v_expected))
     ]
Exemplo n.º 9
0
def time_axis_from_dict(t_dict):
    utc = api.Calendar()

    sim_start = dt.datetime.strptime(t_dict['start_datetime'], "%Y-%m-%dT%H:%M:%S")
    utc_start = utc.time(
        api.YMDhms(sim_start.year, sim_start.month, sim_start.day, sim_start.hour, sim_start.minute, sim_start.second))
    tstep = t_dict['run_time_step']
    nstep = t_dict['number_of_steps']
    time_axis = api.TimeAxisFixedDeltaT(utc_start, tstep, nstep)

    return time_axis
Exemplo n.º 10
0
    def test_create_TargetSpecificationPts(self):
        t = api.TargetSpecificationPts();
        t.scale_factor = 1.0
        t.calc_mode = api.NASH_SUTCLIFFE
        t.calc_mode = api.KLING_GUPTA;
        t.s_r = 1.0  # KGEs scale-factors
        t.s_a = 2.0
        t.s_b = 3.0
        self.assertAlmostEqual(t.scale_factor, 1.0)
        # create a ts with some points
        cal = api.Calendar();
        start = cal.time(api.YMDhms(2015, 1, 1, 0, 0, 0))
        dt = api.deltahours(1)
        tsf = api.TsFactory();
        times = api.UtcTimeVector()
        times.push_back(start + 1 * dt);
        times.push_back(start + 3 * dt);
        times.push_back(start + 4 * dt)

        values = api.DoubleVector()
        values.push_back(1.0)
        values.push_back(3.0)
        values.push_back(np.nan)
        tsp = tsf.create_time_point_ts(api.UtcPeriod(start, start + 24 * dt), times, values)
        # convert it from a time-point ts( as returned from current smgrepository) to a fixed interval with timeaxis, needed by calibration
        tst = api.TsTransform()
        tsa = tst.to_average(start, dt, 24, tsp)
        # tsa2 = tst.to_average(start,dt,24,tsp,False)
        # tsa_staircase = tst.to_average_staircase(start,dt,24,tsp,False) # nans infects the complete interval to nan
        # tsa_staircase2 = tst.to_average_staircase(start,dt,24,tsp,True) # skip nans, nans are 0
        # stuff it into the target spec.
        # also show how to specify snow-calibration
        cids = api.IntVector([0, 2, 3])
        t2 = api.TargetSpecificationPts(tsa,cids, 0.7, api.KLING_GUPTA, 1.0, 1.0, 1.0, api.SNOW_COVERED_AREA)
        t2.catchment_property = api.SNOW_WATER_EQUIVALENT
        self.assertEqual(t2.catchment_property, api.SNOW_WATER_EQUIVALENT)
        self.assertIsNotNone(t2.catchment_indexes)
        for i in range(len(cids)):
            self.assertEqual(cids[i],t2.catchment_indexes[i])
        t.ts = tsa
        #TODO: Does not work, list of objects are not yet convertible tv = api.TargetSpecificationVector([t, t2])
        tv=api.TargetSpecificationVector()
        tv.append(t)
        tv.append(t2)
        # now verify we got something ok
        self.assertEqual(2, tv.size())
        self.assertAlmostEqual(tv[0].ts.value(1), 1.5)  # average value 0..1 ->0.5
        self.assertAlmostEqual(tv[0].ts.value(2), 2.5)  # average value 0..1 ->0.5
        self.assertAlmostEqual(tv[0].ts.value(3), 3.0)  # average value 0..1 ->0.5
        # and that the target vector now have its own copy of ts
        tsa.set(1, 3.0)
        self.assertAlmostEqual(tv[0].ts.value(1), 1.5)  # make sure the ts passed onto target spec, is a copy
        self.assertAlmostEqual(tsa.value(1), 3.0)  # and that we really did change the source