示例#1
0
 def open_access(self):
     """
     Allow to open cube access with a ``with`` statement.
     Note: move this code into Cube later!
     :return: CubeDataAccess instance
     """
     cube = Cube.open(CUBE_DIR)
     data = cube.data
     yield data
     data.close()
示例#2
0
 def open_access(self):
     """
     Allow to open cube access with a ``with`` statement.
     Note: move this code into Cube later!
     :return: CubeDataAccess instance
     """
     cube = Cube.open(CUBE_DIR)
     data = cube.data
     yield data
     data.close()
示例#3
0
 def setUpClass(cls):
     _del_cube_dir()
     cube = Cube.create(CUBE_DIR, CubeConfig(spatial_res=1.0,
                                             start_time=datetime(2005, 1, 1),
                                             end_time=datetime(2005, 3, 1),
                                             grid_width=360, grid_height=180,
                                             compression=True))
     try:
         cube.update(TestCubeSourceProvider(cube.config, var='a_var'))
         cube.update(TestCubeSourceProvider(cube.config, var='b_var'))
         cube.update(TestCubeSourceProvider(cube.config, var='c_var'))
     finally:
         cube.close()
示例#4
0
 def setUpClass(cls):
     _del_cube_dir()
     cube = Cube.create(
         CUBE_DIR,
         CubeConfig(spatial_res=1.0,
                    start_time=datetime(2005, 1, 1),
                    end_time=datetime(2005, 3, 1),
                    grid_width=360,
                    grid_height=180,
                    compression=True))
     try:
         cube.update(TestCubeSourceProvider(cube.config, var='a_var'))
         cube.update(TestCubeSourceProvider(cube.config, var='b_var'))
         cube.update(TestCubeSourceProvider(cube.config, var='c_var'))
     finally:
         cube.close()
示例#5
0
    def test_update(self):
        cube = Cube.create(CUBE_DIR, CubeConfig())
        self.assertTrue(os.path.exists(CUBE_DIR))
        self.assertTrue(os.path.exists(CUBE_DIR + '/CHANGELOG'))

        provider = CubeSourceProviderMock(cube.config,
                                          start_time=datetime(2001, 1, 1),
                                          end_time=datetime(2001, 2, 1))
        cube.update(provider)

        self.assertEqual(
            [(datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 9, 0, 0)),
             (datetime(2001, 1, 9, 0, 0), datetime(2001, 1, 17, 0, 0)),
             (datetime(2001, 1, 17, 0, 0), datetime(2001, 1, 25, 0, 0)),
             (datetime(2001, 1, 25, 0, 0), datetime(2001, 2, 2, 0, 0))],
            provider.trace)

        self.assertTrue(os.path.exists(CUBE_DIR + "/cube.config"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2001_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2001_FAPAR.nc"))

        cube.close()

        with self.assertRaises(IOError):
            cube.update(provider)

        cube2 = Cube.open(CUBE_DIR)
        self.assertEqual(cube.config.spatial_res, cube2.config.spatial_res)
        self.assertEqual(cube.config.temporal_res, cube2.config.temporal_res)
        self.assertEqual(cube.config.file_format, cube2.config.file_format)
        self.assertEqual(cube.config.compression, cube2.config.compression)

        provider = CubeSourceProviderMock(cube2.config,
                                          start_time=datetime(2006, 12, 15),
                                          end_time=datetime(2007, 1, 15))
        cube2.update(provider)
        self.assertEqual(
            [
                (datetime(2006, 12, 11, 0, 0), datetime(2006, 12, 19, 0,
                                                        0)),  # 8 days
                (datetime(2006, 12, 19, 0, 0), datetime(2006, 12, 27, 0,
                                                        0)),  # 8 days
                (datetime(2006, 12, 27, 0, 0), datetime(2007, 1, 1, 0,
                                                        0)),  # 8 days
                (datetime(2007, 1, 1, 0, 0), datetime(2007, 1, 9, 0,
                                                      0)),  # 8 days
                (datetime(2007, 1, 9, 0, 0), datetime(2007, 1, 17, 0, 0))
            ],  # 8 days
            provider.trace)

        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2006_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2007_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2006_FAPAR.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2007_FAPAR.nc"))
        from xarray import Variable
        try:
            data = cube2.data
            self.assertIsNotNone(data)
            self.assertEqual((2, 11 * 46, 720, 1440), data.shape)
            self.assertEquals(['FAPAR', 'LAI'], data.variable_names)

            lai_var = data.variable('LAI')
            self.assert_cf_conformant_time_info(data, 'LAI')
            self.assert_cf_conformant_geospatial_info(data, 'LAI')
            self.assertIsNotNone(lai_var)
            self.assertIs(lai_var, data['LAI'])
            self.assertIs(lai_var, data[1])
            self.assertIsInstance(lai_var, Variable)
            self.assertIs(data.variable(1), data[1])
            array = lai_var[:, :, :]
            self.assertEqual(array.shape, (138, 720, 1440))
            scalar = lai_var[3, 320, 720]
            self.assertEqual(scalar.values, np.array(0.14, dtype=np.float32))

            fapar_var = data.variable('FAPAR')
            self.assert_cf_conformant_time_info(data, 'FAPAR')
            self.assert_cf_conformant_geospatial_info(data, 'FAPAR')
            self.assertIsNotNone(fapar_var)
            self.assertIs(fapar_var, data['FAPAR'])
            self.assertIs(fapar_var, data[0])
            self.assertIs(data.variable(0), data[0])
            array = fapar_var[:, :, :]
            self.assertEqual(array.shape, (138, 720, 1440))
            scalar = fapar_var[3, 320, 720]
            self.assertEqual(scalar.values, np.array(0.62, dtype=np.float32))

            result = data.get('FAPAR',
                              [datetime(2001, 1, 1),
                               datetime(2001, 2, 1)], [-90, 90], [-180, +180])
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0].shape, (4, 720, 1440))

            result = data.get(['FAPAR', 'LAI'],
                              [datetime(2001, 1, 1),
                               datetime(2001, 2, 1)], [50.0, 60.0],
                              [10.0, 30.0])
            self.assertEqual(2, len(result))
            self.assertEqual(result[0].shape, (4, 40, 80))
            self.assertEqual(result[1].shape, (4, 40, 80))

            result, = data.get(1, datetime(2001, 1, 20), 0, 0)
            self.assertEqual(result.shape, ())
            self.assertEqual(result.values, np.array(0.13, dtype=np.float32))

            result, = data.get(0, datetime(2001, 1, 20), -12.6, 5.9)
            self.assertEqual(result.shape, ())
            self.assertEqual(result, np.array(0.615, dtype=np.float32))

            result = data.get((1, 0), datetime(2001, 1, 20), 53.4, 13.1)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0].shape, ())
            self.assertEqual(result[1].shape, ())
            self.assertEqual(result[0].values, np.array(0.13,
                                                        dtype=np.float32))
            self.assertEqual(result[1].values, np.array(0.615,
                                                        dtype=np.float32))
        finally:
            cube2.close()
示例#6
0
    def test_update(self):
        cube = Cube.create(CUBE_DIR, CubeConfig())
        self.assertTrue(os.path.exists(CUBE_DIR))
        self.assertTrue(os.path.exists(CUBE_DIR + '/CHANGELOG'))

        provider = CubeSourceProviderMock(cube.config, start_time=datetime(2001, 1, 1), end_time=datetime(2001, 2, 1))
        cube.update(provider)

        self.assertEqual([(datetime(2001, 1, 1, 0, 0), datetime(2001, 1, 9, 0, 0)),
                          (datetime(2001, 1, 9, 0, 0), datetime(2001, 1, 17, 0, 0)),
                          (datetime(2001, 1, 17, 0, 0), datetime(2001, 1, 25, 0, 0)),
                          (datetime(2001, 1, 25, 0, 0), datetime(2001, 2, 2, 0, 0))],
                         provider.trace)

        self.assertTrue(os.path.exists(CUBE_DIR + "/cube.config"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2001_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2001_FAPAR.nc"))

        cube.close()

        with self.assertRaises(IOError):
            cube.update(provider)

        cube2 = Cube.open(CUBE_DIR)
        self.assertEqual(cube.config.spatial_res, cube2.config.spatial_res)
        self.assertEqual(cube.config.temporal_res, cube2.config.temporal_res)
        self.assertEqual(cube.config.file_format, cube2.config.file_format)
        self.assertEqual(cube.config.compression, cube2.config.compression)

        provider = CubeSourceProviderMock(cube2.config,
                                          start_time=datetime(2006, 12, 15),
                                          end_time=datetime(2007, 1, 15))
        cube2.update(provider)
        self.assertEqual([(datetime(2006, 12, 11, 0, 0), datetime(2006, 12, 19, 0, 0)),  # 8 days
                          (datetime(2006, 12, 19, 0, 0), datetime(2006, 12, 27, 0, 0)),  # 8 days
                          (datetime(2006, 12, 27, 0, 0), datetime(2007, 1, 1, 0, 0)),  # 8 days
                          (datetime(2007, 1, 1, 0, 0), datetime(2007, 1, 9, 0, 0)),  # 8 days
                          (datetime(2007, 1, 9, 0, 0), datetime(2007, 1, 17, 0, 0))],  # 8 days
                         provider.trace)

        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2006_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/LAI/2007_LAI.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2006_FAPAR.nc"))
        self.assertTrue(os.path.exists(CUBE_DIR + "/data/FAPAR/2007_FAPAR.nc"))
        from xarray import Variable
        try:
            data = cube2.data
            self.assertIsNotNone(data)
            self.assertEqual((2, 10 * 46, 720, 1440), data.shape)
            self.assertEquals(['FAPAR', 'LAI'], data.variable_names)

            lai_var = data.variable('LAI')
            self.assert_cf_conformant_time_info(data, 'LAI')
            self.assert_cf_conformant_geospatial_info(data, 'LAI')
            self.assertIsNotNone(lai_var)
            self.assertIs(lai_var, data['LAI'])
            self.assertIs(lai_var, data[1])
            self.assertIsInstance(lai_var, Variable)
            self.assertIs(data.variable(1), data[1])
            array = lai_var[:, :, :]
            self.assertEqual(array.shape, (138, 720, 1440))
            scalar = lai_var[3, 320, 720]
            self.assertEqual(scalar.values, np.array(0.14, dtype=np.float32))

            fapar_var = data.variable('FAPAR')
            self.assert_cf_conformant_time_info(data, 'FAPAR')
            self.assert_cf_conformant_geospatial_info(data, 'FAPAR')
            self.assertIsNotNone(fapar_var)
            self.assertIs(fapar_var, data['FAPAR'])
            self.assertIs(fapar_var, data[0])
            self.assertIs(data.variable(0), data[0])
            array = fapar_var[:, :, :]
            self.assertEqual(array.shape, (138, 720, 1440))
            scalar = fapar_var[3, 320, 720]
            self.assertEqual(scalar.values, np.array(0.62, dtype=np.float32))

            result = data.get('FAPAR',
                              [datetime(2001, 1, 1), datetime(2001, 2, 1)],
                              [-90, 90],
                              [-180, +180])
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0].shape, (4, 720, 1440))

            result = data.get(['FAPAR', 'LAI'],
                              [datetime(2001, 1, 1), datetime(2001, 2, 1)],
                              [50.0, 60.0],
                              [10.0, 30.0])
            self.assertEqual(2, len(result))
            self.assertEqual(result[0].shape, (4, 40, 80))
            self.assertEqual(result[1].shape, (4, 40, 80))

            result, = data.get(1,
                               datetime(2001, 1, 20),
                               0,
                               0)
            self.assertEqual(result.shape, ())
            self.assertEqual(result.values, np.array(0.13, dtype=np.float32))

            result, = data.get(0,
                               datetime(2001, 1, 20),
                               -12.6,
                               5.9)
            self.assertEqual(result.shape, ())
            self.assertEqual(result, np.array(0.615, dtype=np.float32))

            result = data.get((1, 0),
                              datetime(2001, 1, 20),
                              53.4,
                              13.1)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0].shape, ())
            self.assertEqual(result[1].shape, ())
            self.assertEqual(result[0].values, np.array(0.13, dtype=np.float32))
            self.assertEqual(result[1].values, np.array(0.615, dtype=np.float32))
        finally:
            cube2.close()