def test_absolute_time_3(self): """Test the registration of maps with absolute time. The timestamps are set using the C-Interface beforehand, so that the register function needs to read the timetsamp from the map metadata. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp( "register_map_1", tgis.get_current_mapset(), "1 Jan 2001 10:30:01" ) ciface.write_raster_timestamp( "register_map_2", tgis.get_current_mapset(), "1 Jan 2001 18:30:01" ) tgis.register_maps_in_space_time_dataset( type="raster", name=None, maps="register_map_1,register_map_2" ) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
def test_absolute_time_stvds_3(self): """Test the registration of maps with absolute time in a space time raster dataset. The timestamps are set via method arguments and with the c-interface. The timestamps of the method arguments should overwrite the time stamps set via the C-interface. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_vector_timestamp( "register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001" ) tgis.register_maps_in_space_time_dataset( type="vector", name=self.stvds_abs.get_name(), maps="register_map_1", start="2001-02-01", increment="1 day", interval=True, ) map = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 2, 1)) self.assertEqual(end, datetime.datetime(2001, 2, 2)) self.stvds_abs.select() start, end = self.stvds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 2, 1)) self.assertEqual(end, datetime.datetime(2001, 2, 2))
def test_relative_time_3(self): """Test the registration of maps with relative time. The timetsamps are set beforehand using the C-interface. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp( "register_map_1", tgis.get_current_mapset(), "1000000 seconds/1500000 seconds", ) ciface.write_raster_timestamp( "register_map_2", tgis.get_current_mapset(), "1500000 seconds/2000000 seconds", ) tgis.register_maps_in_space_time_dataset( type="raster", name=None, maps="register_map_1,register_map_2" ) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 1500000) self.assertEqual(unit, "seconds") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1500000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds")
def test_absolute_time_strds_1(self): """Test the registration of maps with absolute time in a space time raster dataset """ tgis.register_maps_in_space_time_dataset( type="raster", name=self.strds_abs.get_name(), maps="register_map_1,register_map_2", start="2001-01-01", increment="1 day", interval=True, ) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) self.strds_abs.select() start, end = self.strds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 3))
def test_relative_time_strds_2(self): """Test the registration of maps with relative time in a space time raster dataset. The timetsamps are set for the maps using the C-interface before registration. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1000000 seconds/1500000 seconds") ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1500000 seconds/2000000 seconds") tgis.register_maps_in_space_time_dataset(type="raster", name=self.strds_rel.get_name(), maps="register_map_1,register_map_2") map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 1500000) self.assertEqual(unit, "seconds") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1500000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds") self.strds_rel.select() start, end, unit = self.strds_rel.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds")
def test_absolute_time_strds_4(self): """Test the registration of maps with absolute time in a space time raster dataset. The timestamps are set via method arguments and with the c-interface. The timestamps of the method arguments should overwrite the time stamps set via the C-interface. The C-interface sets relative time stamps. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 day") tgis.register_maps_in_space_time_dataset(type="raster", name=self.strds_abs.get_name(), maps="register_map_1", start="2001-02-01", increment="1 day", interval=True) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 2, 1)) self.assertEqual(end, datetime.datetime(2001, 2, 2)) self.strds_abs.select() start, end = self.strds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 2, 1)) self.assertEqual(end, datetime.datetime(2001, 2, 2))
def test_relative_time_strds_1(self): """Test the registration of maps with relative time in a space time raster dataset """ tgis.register_maps_in_space_time_dataset(type="raster", name=self.strds_rel.get_name(), maps="register_map_1,register_map_2", start=0, increment=1, unit="day", interval=True) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 0) self.assertEqual(end, 1) self.assertEqual(unit, "day") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1) self.assertEqual(end, 2) self.assertEqual(unit, "day") self.strds_rel.select() start, end, unit = self.strds_rel.get_relative_time() self.assertEqual(start, 0) self.assertEqual(end, 2) self.assertEqual(unit, "day")
def test_absolute_time_strds_2(self): """Test the registration of maps with absolute time in a space time raster dataset. The timestamps are set using the C-Interface beforehand, so that the register function needs to read the timetsamp from the map metadata. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001") ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "2 Jan 2001/3 Jan 2001") tgis.register_maps_in_space_time_dataset(type="raster", name=self.strds_abs.get_name(), maps="register_map_1,register_map_2") map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) self.strds_abs.select() start, end = self.strds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 3))
def test_relative_time_2(self): """Test the registration of maps with relative time""" tgis.register_maps_in_space_time_dataset( type="raster", name=None, maps="register_map_1,register_map_2", start=1000000, increment=500000, unit="seconds", interval=True, ) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 1500000) self.assertEqual(unit, "seconds") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1500000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds")
def test_relative_time_strds_1(self): """Test the registration of maps with relative time in a space time raster dataset """ tgis.register_maps_in_space_time_dataset( type="raster", name=self.strds_rel.get_name(), maps="register_map_1,register_map_2", start=0, increment=1, unit="day", interval=True, ) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 0) self.assertEqual(end, 1) self.assertEqual(unit, "day") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1) self.assertEqual(end, 2) self.assertEqual(unit, "day") self.strds_rel.select() start, end, unit = self.strds_rel.get_relative_time() self.assertEqual(start, 0) self.assertEqual(end, 2) self.assertEqual(unit, "day")
def test_absolute_time_2(self): """Register vector maps in the temporal database and in addition in a stvds using the object method deleting empty maps :return: """ tgis.register_maps_in_space_time_dataset( type="vector", name=None, maps="register_map_1,register_map_2,register_map_empty", start="2001-01-01", increment="1 day", interval=True, ) map_1 = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset()) map_1.select() start, end = map_1.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map_2 = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset()) map_2.select() start, end = map_2.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) map_3 = tgis.VectorDataset("register_map_empty@" + tgis.get_current_mapset()) map_3.select() start, end = map_3.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 3)) self.assertEqual(end, datetime.datetime(2001, 1, 4)) map_list = [map_1, map_2, map_3] tgis.register_map_object_list( type="vector", map_list=map_list, output_stds=self.stvds_abs, delete_empty=True, ) self.stvds_abs.select() start, end = self.stvds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) map_3 = tgis.VectorDataset("register_map_empty@" + tgis.get_current_mapset()) self.assertEqual(map_3.map_exists(), False)
def test_absolute_time_2(self): """Test the registration of maps with absolute time """ tgis.register_maps_in_space_time_dataset(type="raster", name=None, maps="register_map_1,register_map_2", start="2001-01-01 10:30:01", increment="8 hours", interval=False) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
def setUp(self): """Create the space time raster dataset""" self.strds_abs = tgis.open_new_stds( name="register_test_abs", type="strds", temporaltype="absolute", title="Test strds", descr="Test strds", semantic="field", overwrite=True, ) tgis.register_maps_in_space_time_dataset( type="raster", name=self.strds_abs.get_name(), maps="register_map_1,register_map_2", start="2001-01-01", increment="1 day", interval=True, ) self.currmapset = tgis.get_current_mapset() self.newmapset = "test_temporal_register_mapset_access" # create and switch to new mapset self.runModule( "g.mapset", mapset=self.newmapset, flags="c", quiet=True, ) # add old mapset to search path self.runModule( "g.mapsets", mapset=self.currmapset, operation="add", quiet=True, ) self.runModule( "g.mapsets", flags="p", verbose=True, ) tgis.stop_subprocesses() tgis.init() self.assertNotEqual(self.currmapset, tgis.get_current_mapset())
def test_absolute_time_2(self): """Test the registration of maps with absolute time using register_maps_in_space_time_dataset() and register_map_object_list() with empty map deletion """ tgis.register_maps_in_space_time_dataset( type="raster", name=None, maps="register_map_1,register_map_2,register_map_null", start="2001-01-01 10:30:01", increment="8 hours", interval=False, ) map_1 = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map_1.select() start, end = map_1.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) map_2 = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map_2.select() start, end = map_2.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1)) map_3 = tgis.RasterDataset("register_map_null@" + tgis.get_current_mapset()) map_3.select() start, end = map_3.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2, 2, 30, 1)) map_list = [map_1, map_2, map_3] tgis.register_map_object_list( type="raster", map_list=map_list, output_stds=self.strds_abs, delete_empty=True, ) self.strds_abs.select() start, end = self.strds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 1, 18, 30, 1)) map_3 = tgis.VectorDataset("register_map_null@" + tgis.get_current_mapset()) self.assertEqual(map_3.map_exists(), False)
def test_absolute_time_1(self): """!Test the registration of maps with absolute time """ tgis.register_maps_in_space_time_dataset(type="rast", name=None, maps="register_map_1,register_map_2", start="2001-01-01", increment="1 day", interval=True) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3))
def test_absolute_time_2(self): """!Test the registration of maps with absolute time """ tgis.register_maps_in_space_time_dataset( type="rast", name=None, maps="register_map_1,register_map_2", start="2001-01-01 10:30:01", increment="8 hours", interval=False) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
def test_relative_time_2(self): """Test the registration of maps with relative time """ tgis.register_maps_in_space_time_dataset(type="raster", name=None, maps="register_map_1,register_map_2", start=1000000, increment=500000, unit="seconds", interval=True) map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 1500000) self.assertEqual(unit, "seconds") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1500000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds")
def test_absolute_time_1(self): """Test the registration of maps with absolute time using register_maps_in_space_time_dataset() and register_map_object_list() """ tgis.register_maps_in_space_time_dataset( type="raster", name=None, maps="register_map_1,register_map_2", start="2001-01-01", increment="1 day", interval=True, ) map_1 = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map_1.select() start, end = map_1.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map_2 = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map_2.select() start, end = map_2.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) map_list = [map_1, map_2] tgis.register_map_object_list( type="raster", map_list=map_list, output_stds=self.strds_abs, delete_empty=False, ) self.strds_abs.select() start, end = self.strds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 3))
def test_absolute_time_3(self): """!Test the registration of maps with absolute time. The timestamps are set using the C-Interface beforehand, so that the register function needs to read the timetsamp from the map metadata. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1 Jan 2001 10:30:01") ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1 Jan 2001 18:30:01") tgis.register_maps_in_space_time_dataset(type="rast", name=None, maps="register_map_1,register_map_2") map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 10, 30, 1)) map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1, 18, 30, 1))
def test_relative_time_strds_2(self): """!Test the registration of maps with relative time in a space time raster dataset. The timetsamps are set for the maps using the C-interface before registration. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_raster_timestamp("register_map_1", tgis.get_current_mapset(), "1000000 seconds/1500000 seconds") ciface.write_raster_timestamp("register_map_2", tgis.get_current_mapset(), "1500000 seconds/2000000 seconds") tgis.register_maps_in_space_time_dataset( type="rast", name=self.strds_rel.get_name(), maps="register_map_1,register_map_2") map = tgis.RasterDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 1500000) self.assertEqual(unit, "seconds") map = tgis.RasterDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end, unit = map.get_relative_time() self.assertEqual(start, 1500000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds") self.strds_rel.select() start, end, unit = self.strds_rel.get_relative_time() self.assertEqual(start, 1000000) self.assertEqual(end, 2000000) self.assertEqual(unit, "seconds")
def test_absolute_time_stvds_2(self): """Test the registration of maps with absolute time in a space time raster dataset. The timestamps are set using the C-Interface beforehand, so that the register function needs to read the timetsamp from the map metadata. """ ciface = tgis.get_tgis_c_library_interface() ciface.write_vector_timestamp( "register_map_1", tgis.get_current_mapset(), "1 Jan 2001/2 Jan 2001" ) ciface.write_vector_timestamp( "register_map_2", tgis.get_current_mapset(), "2 Jan 2001/3 Jan 2001" ) tgis.register_maps_in_space_time_dataset( type="vector", name=self.stvds_abs.get_name(), maps="register_map_1,register_map_2", ) map = tgis.VectorDataset("register_map_1@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 2)) map = tgis.VectorDataset("register_map_2@" + tgis.get_current_mapset()) map.select() start, end = map.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 2)) self.assertEqual(end, datetime.datetime(2001, 1, 3)) self.stvds_abs.select() start, end = self.stvds_abs.get_absolute_time() self.assertEqual(start, datetime.datetime(2001, 1, 1)) self.assertEqual(end, datetime.datetime(2001, 1, 3))
def test_1(self): """Perform several unregistration operations""" # Prepare some strings for the tests new_line = os.linesep a = ["a1", "a2", "a3", "a4", "a5", "a6"] al = new_line.join(a) al += new_line a123 = new_line.join(a[:3]) a123 += new_line a456 = new_line.join(a[3:]) a456 += new_line # Unregister maps a1, a2 and a3 from STRDS A self.assertModule("t.unregister", input="A", maps="a1,a2,a3") lister = SimpleModule("t.rast.list", input="A", columns="name", flags="s") self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Check if all maps are present in STRDS B lister = SimpleModule("t.rast.list", input="B", columns="name", flags="s") self.runModule(lister) self.assertEqual(al, lister.outputs.stdout) # Check if maps a1, a2 and a3 are still present in the temporal database lister = SimpleModule( "t.list", type="raster", columns="name", where= 'mapset = "%s" AND (name = "a1" OR name = "a2" OR name = "a3")' % (tgis.get_current_mapset())) self.runModule(lister) self.assertEqual(a123, lister.outputs.stdout) # Unregister maps a1, a2 and a3 from the temporal database self.assertModule("t.unregister", maps="a1,a2,a3") lister = SimpleModule( "t.list", type="raster", columns="name", where= 'mapset = "%s" AND (name = "a1" OR name = "a2" OR name = "a3")' % (tgis.get_current_mapset())) self.runModule(lister) self.assertEqual("", lister.outputs.stdout) # Check that masp a1, a2 and a3 are not present in STRDS B lister = SimpleModule("t.rast.list", input="B", columns="name", flags="s") self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Remove STRDS A and B and check if maps a4, a5 and a6 are still in the temporal database self.assertModule("t.remove", type="strds", inputs="A,B") lister = SimpleModule( "t.list", type="raster", columns="name", where= 'mapset = "%s" AND (name = "a4" OR name = "a5" OR name = "a6")' % (tgis.get_current_mapset())) self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Unregister maps a4, a5 and a6 from the temporal database self.assertModule("t.unregister", maps="a4,a5,a6") lister = SimpleModule( "t.list", type="raster", columns="name", where= 'mapset = "%s" AND (name = "a4" OR name = "a5" OR name = "a6")' % (tgis.get_current_mapset())) self.runModule(lister) self.assertEqual("", lister.outputs.stdout)
def main(): # lazy imports import grass.temporal as tgis from grass.pygrass.modules import Module # Get the options input = options["input"] output = options["output"] start = options["start"] stop = options["stop"] base = options["basename"] cycle = options["cycle"] lower = options["lower"] upper = options["upper"] offset = options["offset"] limits = options["limits"] shift = options["shift"] scale = options["scale"] method = options["method"] granularity = options["granularity"] register_null = flags["n"] reverse = flags["r"] time_suffix = options["suffix"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = tgis.get_current_mapset() if input.find("@") >= 0: id = input else: id = input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if input_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (id)) input_strds.select(dbif) if output.find("@") >= 0: out_id = output else: out_id = output + "@" + mapset # The output space time raster dataset output_strds = tgis.SpaceTimeRasterDataset(out_id) if output_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal( _("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % out_id) if tgis.check_granularity_string(granularity, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid granularity")) if tgis.check_granularity_string(cycle, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid cycle")) if offset: if tgis.check_granularity_string( offset, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid offset")) # The lower threshold space time raster dataset if lower: if not range: dbif.close() grass.fatal( _("You need to set the range to compute the occurrence" " space time raster dataset")) if lower.find("@") >= 0: lower_id = lower else: lower_id = lower + "@" + mapset lower_strds = tgis.SpaceTimeRasterDataset(lower_id) if lower_strds.is_in_db() == False: dbif.close() grass.fatal( _("Space time raster dataset <%s> not found") % (lower_strds.get_id())) if lower_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal( _("Temporal type of input strds and lower strds must be equal") ) lower_strds.select(dbif) # The upper threshold space time raster dataset if upper: if not lower: dbif.close() grass.fatal( _("The upper option works only in conjunction with the lower option" )) if upper.find("@") >= 0: upper = upper else: upper_id = upper + "@" + mapset upper_strds = tgis.SpaceTimeRasterDataset(upper_id) if upper_strds.is_in_db() == False: dbif.close() grass.fatal( _("Space time raster dataset <%s> not found") % (upper_strds.get_id())) if upper_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal( _("Temporal type of input strds and upper strds must be equal") ) upper_strds.select(dbif) input_strds_start, input_strds_end = input_strds.get_temporal_extent_as_tuple( ) if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) if stop: stop = tgis.string_to_datetime(stop) else: stop = input_strds_end start = tgis.adjust_datetime_to_granularity(start, granularity) else: start = int(start) if stop: stop = int(stop) else: stop = input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) else: end = start + cycle limit_relations = [ "EQUALS", "DURING", "OVERLAPS", "OVERLAPPING", "CONTAINS" ] count = 1 output_maps = [] while input_strds_end > start and stop > start: # Make sure that the cyclic computation will stop at the correct time if stop and end > stop: end = stop where = "start_time >= \'%s\' AND start_time < \'%s\'" % (str(start), str(end)) input_maps = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) grass.message(_("Processing cycle %s - %s" % (str(start), str(end)))) if len(input_maps) == 0: continue # Lets create a dummy list of maps with granularity conform intervals gran_list = [] gran_list_low = [] gran_list_up = [] gran_start = start while gran_start < end: map = input_strds.get_new_map_instance("%i@%i" % (count, count)) if input_strds.is_time_absolute(): gran_end = tgis.increment_datetime_by_string( gran_start, granularity) map.set_absolute_time(gran_start, gran_end) gran_start = tgis.increment_datetime_by_string( gran_start, granularity) else: gran_end = gran_start + granularity map.set_relative_time(gran_start, gran_end, input_strds.get_relative_time_unit()) gran_start = gran_start + granularity gran_list.append(copy(map)) gran_list_low.append(copy(map)) gran_list_up.append(copy(map)) # Lists to compute the topology with upper and lower datasets # Create the topology between the granularity conform list and all maps # of the current cycle gran_topo = tgis.SpatioTemporalTopologyBuilder() gran_topo.build(gran_list, input_maps) if lower: lower_maps = lower_strds.get_registered_maps_as_objects(dbif=dbif) gran_lower_topo = tgis.SpatioTemporalTopologyBuilder() gran_lower_topo.build(gran_list_low, lower_maps) if upper: upper_maps = upper_strds.get_registered_maps_as_objects(dbif=dbif) gran_upper_topo = tgis.SpatioTemporalTopologyBuilder() gran_upper_topo.build(gran_list_up, upper_maps) old_map_name = None # Aggregate num_maps = len(gran_list) for i in range(num_maps): if reverse: map = gran_list[num_maps - i - 1] else: map = gran_list[i] # Select input maps based on temporal topology relations input_maps = [] if map.get_equal(): input_maps += map.get_equal() elif map.get_contains(): input_maps += map.get_contains() elif map.get_overlaps(): input_maps += map.get_overlaps() elif map.get_overlapped(): input_maps += map.get_overlapped() elif map.get_during(): input_maps += map.get_during() # Check input maps if len(input_maps) == 0: continue # New output map if input_strds.get_temporal_type( ) == 'absolute' and time_suffix == 'gran': suffix = tgis.create_suffix_from_datetime( map.temporal_extent.get_start_time(), input_strds.get_granularity()) output_map_name = "{ba}_{su}".format(ba=base, su=suffix) elif input_strds.get_temporal_type( ) == 'absolute' and time_suffix == 'time': suffix = tgis.create_time_suffix(map) output_map_name = "{ba}_{su}".format(ba=base, su=suffix) else: output_map_name = tgis.create_numeric_suffix( base, count, time_suffix) output_map_id = map.build_id(output_map_name, mapset) output_map = input_strds.get_new_map_instance(output_map_id) # Check if new map is in the temporal database if output_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry output_map.delete(dbif) output_map = input_strds.get_new_map_instance( output_map_id) else: grass.fatal( _("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (output_map.get_map_id())) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): output_map.set_absolute_time(map_start, map_end) else: output_map.set_relative_time(map_start, map_end, map.get_relative_time_unit()) limits_vals = limits.split(",") limits_lower = float(limits_vals[0]) limits_upper = float(limits_vals[1]) lower_map_name = None if lower: relations = gran_list_low[i].get_temporal_relations() for relation in limit_relations: if relation in relations: lower_map_name = str(relations[relation][0].get_id()) break upper_map_name = None if upper: relations = gran_list_up[i].get_temporal_relations() for relation in limit_relations: if relation in relations: upper_map_name = str(relations[relation][0].get_id()) break input_map_names = [] for input_map in input_maps: input_map_names.append(input_map.get_id()) # Set up the module accmod = Module("r.series.accumulate", input=input_map_names, output=output_map_name, run_=False) if old_map_name: accmod.inputs["basemap"].value = old_map_name if lower_map_name: accmod.inputs["lower"].value = lower_map_name if upper_map_name: accmod.inputs["upper"].value = upper_map_name accmod.inputs["limits"].value = (limits_lower, limits_upper) if shift: accmod.inputs["shift"].value = float(shift) if scale: accmod.inputs["scale"].value = float(scale) if method: accmod.inputs["method"].value = method print(accmod) accmod.run() if accmod.popen.returncode != 0: dbif.close() grass.fatal(_("Error running r.series.accumulate")) output_maps.append(output_map) old_map_name = output_map_name count += 1 # Increment the cycle start = end if input_strds.is_time_absolute(): start = end if offset: start = tgis.increment_datetime_by_string(end, offset) end = tgis.increment_datetime_by_string(start, cycle) else: if offset: start = end + offset end = start + cycle # Insert the maps into the output space time dataset if output_strds.is_in_db(dbif): if grass.overwrite(): output_strds.delete(dbif) output_strds = input_strds.get_new_instance(out_id) temporal_type, semantic_type, title, description = input_strds.get_initial_values( ) output_strds.set_initial_values(temporal_type, semantic_type, title, description) output_strds.insert(dbif) empty_maps = [] # Register the maps in the database count = 0 for output_map in output_maps: count += 1 if count % 10 == 0: grass.percent(count, len(output_maps), 1) # Read the raster map data output_map.load() # In case of a empty map continue, do not register empty maps if not register_null: if output_map.metadata.get_min() is None and \ output_map.metadata.get_max() is None: empty_maps.append(output_map) continue # Insert map in temporal database output_map.insert(dbif) output_strds.register_map(output_map, dbif) # Update the spatio-temporal extent and the metadata table entries output_strds.update_from_registered_maps(dbif) grass.percent(1, 1, 1) dbif.close() # Remove empty maps if len(empty_maps) > 0: for map in empty_maps: grass.run_command("g.remove", flags='f', type="raster", name=map.get_name(), quiet=True)
def main(): # Get the options input = options["input"] base = options["basename"] where = options["where"] nprocs = options["nprocs"] mapset = grass.gisenv()["MAPSET"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds") maps = sp.get_registered_maps_as_objects_with_gaps(where, dbif) num = len(maps) # Configure the r.to.vect module gapfill_module = pymod.Module( "r.series.interp", overwrite=grass.overwrite(), quiet=True, run_=False, finish_=False, ) process_queue = pymod.ParallelModuleQueue(int(nprocs)) gap_list = [] overwrite_flags = {} # Identify all gaps and create new names count = 0 for _map in maps: if _map.get_id() is None: count += 1 _id = "%s_%d@%s" % (base, num + count, mapset) _map.set_id(_id) gap_list.append(_map) if len(gap_list) == 0: grass.message(_("No gaps found")) return # Build the temporal topology tb = tgis.SpatioTemporalTopologyBuilder() tb.build(maps) # Do some checks before computation for _map in gap_list: if not _map.get_precedes() or not _map.get_follows(): grass.fatal( _("Unable to determine successor " "and predecessor of a gap.")) if len(_map.get_precedes()) > 1: grass.warning( _("More than one successor of the gap found. " "Using the first found.")) if len(_map.get_follows()) > 1: grass.warning( _("More than one predecessor of the gap found. " "Using the first found.")) # Interpolate the maps using parallel processing result_list = [] for _map in gap_list: predecessor = _map.get_follows()[0] successor = _map.get_precedes()[0] gran = sp.get_granularity() tmpval, start = predecessor.get_temporal_extent_as_tuple() end, tmpval = successor.get_temporal_extent_as_tuple() # Now resample the gap map_matrix = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity( (_map, ), start, end, gran) map_names = [] map_positions = [] increment = 1.0 / (len(map_matrix) + 1.0) position = increment count = 0 for intp_list in map_matrix: new_map = intp_list[0] count += 1 new_id = "%s_%i@%s" % (_map.get_name(), count, tgis.get_current_mapset()) new_map.set_id(new_id) overwrite_flags[new_id] = False if new_map.map_exists() or new_map.is_in_db(dbif): if not grass.overwrite: grass.fatal( _("Map with name <%s> already exists. " "Please use another base name." % (_id))) else: if new_map.is_in_db(dbif): overwrite_flags[new_id] = True map_names.append(new_map.get_name()) map_positions.append(position) position += increment result_list.append(new_map) mod = copy.deepcopy(gapfill_module) mod(input=(predecessor.get_map_id(), successor.get_map_id()), datapos=(0, 1), output=map_names, samplingpos=map_positions) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) # Wait for unfinished processes process_queue.wait() # Insert new interpolated maps in temporal database and dataset for _map in result_list: id = _map.get_id() if overwrite_flags[id] == True: if _map.is_time_absolute(): start, end = _map.get_absolute_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_absolute_time(start, end) else: start, end, unit = _map.get_relative_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_relative_time(start, end, unit) _map.load() _map.insert(dbif) sp.register_map(_map, dbif) sp.update_from_registered_maps(dbif) sp.update_command_string(dbif=dbif) dbif.close()
def main(): # Get the options input = options["input"] base = options["basename"] where = options["where"] nprocs = options["nprocs"] mapset = grass.gisenv()["MAPSET"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds") maps = sp.get_registered_maps_as_objects_with_gaps(where, dbif) num = len(maps) # Configure the r.to.vect module gapfill_module = pymod.Module("r.series.interp", overwrite=grass.overwrite(), quiet=True, run_=False, finish_=False,) process_queue = pymod.ParallelModuleQueue(int(nprocs)) gap_list = [] overwrite_flags = {} # Identify all gaps and create new names count = 0 for _map in maps: if _map.get_id() is None: count += 1 _id = "%s_%d@%s" % (base, num + count, mapset) _map.set_id(_id) gap_list.append(_map) if len(gap_list) == 0: grass.message(_("No gaps found")) return # Build the temporal topology tb = tgis.SpatioTemporalTopologyBuilder() tb.build(maps) # Do some checks before computation for _map in gap_list: if not _map.get_precedes() or not _map.get_follows(): grass.fatal(_("Unable to determine successor " "and predecessor of a gap.")) if len(_map.get_precedes()) > 1: grass.warning(_("More than one successor of the gap found. " "Using the first found.")) if len(_map.get_follows()) > 1: grass.warning(_("More than one predecessor of the gap found. " "Using the first found.")) # Interpolate the maps using parallel processing result_list = [] for _map in gap_list: predecessor = _map.get_follows()[0] successor = _map.get_precedes()[0] gran = sp.get_granularity() tmpval, start = predecessor.get_temporal_extent_as_tuple() end, tmpval = successor.get_temporal_extent_as_tuple() # Now resample the gap map_matrix = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity((_map, ),start, end, gran) map_names = [] map_positions = [] increment = 1.0/ (len(map_matrix) + 1.0) position = increment count = 0 for intp_list in map_matrix: new_map = intp_list[0] count += 1 new_id = "%s_%i@%s"%(_map.get_name(), count, tgis.get_current_mapset()) new_map.set_id(new_id) overwrite_flags[new_id] = False if new_map.map_exists() or new_map.is_in_db(dbif): if not grass.overwrite: grass.fatal(_("Map with name <%s> already exists. " "Please use another base name." % (_id))) else: if new_map.is_in_db(dbif): overwrite_flags[new_id] = True map_names.append(new_map.get_name()) map_positions.append(position) position += increment result_list.append(new_map) mod = copy.deepcopy(gapfill_module) mod(input=(predecessor.get_map_id(), successor.get_map_id()), datapos=(0, 1), output=map_names, samplingpos=map_positions) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) # Wait for unfinished processes process_queue.wait() # Insert new interpolated maps in temporal database and dataset for _map in result_list: id = _map.get_id() if overwrite_flags[id] == True: if _map.is_time_absolute(): start, end = _map.get_absolute_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_absolute_time(start, end) else: start, end, unit = _map.get_relative_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_relative_time(start, end, unit) _map.load() _map.insert(dbif) sp.register_map(_map, dbif) sp.update_from_registered_maps(dbif) sp.update_command_string(dbif=dbif) dbif.close()
def test_1(self): """Perform several unregistration operations""" # Prepare some strings for the tests new_line = os.linesep a = ["a1","a2","a3","a4","a5","a6"] al = new_line.join(a) al += new_line a123 = new_line.join(a[:3]) a123 += new_line a456 = new_line.join(a[3:]) a456 += new_line # Unregister maps a1, a2 and a3 from STRDS A self.assertModule("t.unregister", input="A", maps="a1,a2,a3") lister = SimpleModule("t.rast.list", input="A", columns="name", flags="s") self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Check if all maps are present in STRDS B lister = SimpleModule("t.rast.list", input="B", columns="name", flags="s") self.runModule(lister) self.assertEqual(al, lister.outputs.stdout) # Check if maps a1, a2 and a3 are still present in the temporal database lister = SimpleModule("t.list", type="raster", columns="name", where='mapset = "%s" AND (name = "a1" OR name = "a2" OR name = "a3")'%(tgis.get_current_mapset())) self.runModule(lister) self.assertEqual(a123, lister.outputs.stdout) # Unregister maps a1, a2 and a3 from the temporal database self.assertModule("t.unregister", maps="a1,a2,a3") lister = SimpleModule("t.list", type="raster", columns="name", where='mapset = "%s" AND (name = "a1" OR name = "a2" OR name = "a3")'%(tgis.get_current_mapset())) self.runModule(lister) self.assertEqual("", lister.outputs.stdout) # Check that masp a1, a2 and a3 are not present in STRDS B lister = SimpleModule("t.rast.list", input="B", columns="name", flags="s") self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Remove STRDS A and B and check if maps a4, a5 and a6 are still in the temporal database self.assertModule("t.remove", type="strds", inputs="A,B") lister = SimpleModule("t.list", type="raster", columns="name", where='mapset = "%s" AND (name = "a4" OR name = "a5" OR name = "a6")'%(tgis.get_current_mapset())) self.runModule(lister) self.assertEqual(a456, lister.outputs.stdout) # Unregister maps a4, a5 and a6 from the temporal database self.assertModule("t.unregister", maps="a4,a5,a6") lister = SimpleModule("t.list", type="raster", columns="name", where='mapset = "%s" AND (name = "a4" OR name = "a5" OR name = "a6")'%(tgis.get_current_mapset())) self.runModule(lister) self.assertEqual("", lister.outputs.stdout)
def main(): # Get the options input = options["input"] start = options["start"] stop = options["stop"] base = options["basename"] cycle = options["cycle"] offset = options["offset"] minimum = options["minimum"] maximum = options["maximum"] occurrence = options["occurrence"] range = options["range"] indicator = options["indicator"] staend = options["staend"] register_null = flags["n"] reverse = flags["r"] grass.set_raise_on_error(True) # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = tgis.get_current_mapset() if input.find("@") >= 0: id = input else: id = input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if input_strds.is_in_db() == False: dbif.close() grass.fatal( _("Space time %s dataset <%s> not found") % (input_strds.get_output_map_instance(None).get_type(), id)) input_strds.select(dbif) dummy = input_strds.get_new_map_instance(None) # The occurrence space time raster dataset if occurrence: if not minimum or not maximum: if not range: dbif.close() grass.fatal( _("You need to set the range to compute the occurrence" " space time raster dataset")) if occurrence.find("@") >= 0: occurrence_id = occurrence else: occurrence_id = occurrence + "@" + mapset occurrence_strds = tgis.SpaceTimeRasterDataset(occurrence_id) if occurrence_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal( _("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % occurrence_id) # The indicator space time raster dataset if indicator: if not occurrence: dbif.close() grass.fatal( _("You need to set the occurrence to compute the indicator" " space time raster dataset")) if not staend: dbif.close() grass.fatal( _("You need to set the staend options to compute the indicator" " space time raster dataset")) if indicator.find("@") >= 0: indicator = indicator else: indicator_id = indicator + "@" + mapset indicator_strds = tgis.SpaceTimeRasterDataset(indicator_id) if indicator_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal( _("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % indicator_id) staend = staend.split(",") indicator_start = int(staend[0]) indicator_mid = int(staend[1]) indicator_end = int(staend[2]) # The minimum threshold space time raster dataset minimum_strds = None if minimum: if minimum.find("@") >= 0: minimum_id = minimum else: minimum_id = minimum + "@" + mapset minimum_strds = tgis.SpaceTimeRasterDataset(minimum_id) if minimum_strds.is_in_db() == False: dbif.close() grass.fatal( _("Space time raster dataset <%s> not found") % (minimum_strds.get_id())) if minimum_strds.get_temporal_type() != input_strds.get_temporal_type( ): dbif.close() grass.fatal( _("Temporal type of input strds and minimum strds must be equal" )) minimum_strds.select(dbif) # The maximum threshold space time raster dataset maximum_strds = None if maximum: if maximum.find("@") >= 0: maximum_id = maximum else: maximum_id = maximum + "@" + mapset maximum_strds = tgis.SpaceTimeRasterDataset(maximum_id) if maximum_strds.is_in_db() == False: dbif.close() grass.fatal( _("Space time raster dataset <%s> not found") % (maximum_strds.get_id())) if maximum_strds.get_temporal_type() != input_strds.get_temporal_type( ): dbif.close() grass.fatal( _("Temporal type of input strds and maximum strds must be equal" )) maximum_strds.select(dbif) input_strds_start, input_strds_end = input_strds.get_temporal_extent_as_tuple( ) if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) if stop: stop = tgis.string_to_datetime(stop) else: stop = input_strds_end else: start = int(start) if stop: stop = int(stop) else: stop = input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) else: end = start + cycle count = 1 indi_count = 1 occurrence_maps = {} indicator_maps = {} while input_strds_end > start and stop > start: # Make sure that the cyclic computation will stop at the correct time if stop and end > stop: end = stop where = "start_time >= \'%s\' AND start_time < \'%s\'" % (str(start), str(end)) input_maps = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) print len(input_maps) input_topo = tgis.SpatioTemporalTopologyBuilder() input_topo.build(input_maps, input_maps) if len(input_maps) == 0: continue grass.message(_("Processing cycle %s - %s" % (str(start), str(end)))) count = compute_occurrence(occurrence_maps, input_strds, input_maps, start, base, count, mapset, where, reverse, range, minimum_strds, maximum_strds, dbif) # Indicator computation is based on the occurrence so we need to start it after # the occurrence cycle if indicator: num_maps = len(input_maps) for i in xrange(num_maps): if reverse: map = input_maps[num_maps - i - 1] else: map = input_maps[i] indicator_map_name = "%s_indicator_%i" % (base, indi_count) indicator_map_id = dummy.build_id(indicator_map_name, mapset) indicator_map = input_strds.get_new_map_instance( indicator_map_id) # Check if new map is in the temporal database if indicator_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry indicator_map.delete(dbif) indicator_map = input_strds.get_new_map_instance( indicator_map_id) else: grass.fatal( _("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (indicator_map.get_map_id())) curr_map = occurrence_maps[map.get_id()].get_name() # Reverse time if reverse: if i == 0: prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i" % (indicator_start) elif i > 0 and i < num_maps - 1: prev_map = occurrence_maps[ map.next().get_id()].get_name() next_map = occurrence_maps[ map.prev().get_id()].get_name() # In case the previous map is null() set null() or the start indicator subexpr1 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_start) # In case the previous map was not null() if the current map is null() set null() # if the current map is not null() and the next map is not null() set # intermediate indicator, if the next map is null set the end indicator subexpr2 = "if(isnull(%s), %i, %i)" % ( next_map, indicator_end, indicator_mid) subexpr3 = "if(isnull(%s), null(), %s)" % (curr_map, subexpr2) expression = "%s = if(isnull(%s), %s, %s)" % ( indicator_map_name, prev_map, subexpr1, subexpr3) else: prev_map = occurrence_maps[ map.next().get_id()].get_name() subexpr1 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_start) subexpr3 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_mid) else: if i == 0: prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i" % (indicator_start) elif i > 0 and i < num_maps - 1: prev_map = occurrence_maps[ map.prev().get_id()].get_name() next_map = occurrence_maps[ map.next().get_id()].get_name() # In case the previous map is null() set null() or the start indicator subexpr1 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_start) # In case the previous map was not null() if the current map is null() set null() # if the current map is not null() and the next map is not null() set # intermediate indicator, if the next map is null set the end indicator subexpr2 = "if(isnull(%s), %i, %i)" % ( next_map, indicator_end, indicator_mid) subexpr3 = "if(isnull(%s), null(), %s)" % (curr_map, subexpr2) expression = "%s = if(isnull(%s), %s, %s)" % ( indicator_map_name, prev_map, subexpr1, subexpr3) else: prev_map = occurrence_maps[ map.prev().get_id()].get_name() subexpr1 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_start) subexpr3 = "if(isnull(%s), null(), %i)" % ( curr_map, indicator_mid) expression = "%s = if(isnull(%s), %s, %s)" % ( indicator_map_name, prev_map, subexpr1, subexpr3) print expression grass.mapcalc(expression, overwrite=True) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): indicator_map.set_absolute_time(map_start, map_end) else: indicator_map.set_relative_time( map_start, map_end, map.get_relative_time_unit()) indicator_maps[map.get_id()] = indicator_map indi_count += 1 # Increment the cycle start = end if input_strds.is_time_absolute(): start = end if offset: start = tgis.increment_datetime_by_string(end, offset) end = tgis.increment_datetime_by_string(start, cycle) else: if offset: start = end + offset end = start + cycle empty_maps = [] create_strds_register_maps(input_strds, occurrence_strds, occurrence_maps, register_null, empty_maps, dbif) if indicator: create_strds_register_maps(input_strds, indicator_strds, indicator_maps, register_null, empty_maps, dbif) dbif.close() # Remove empty maps if len(empty_maps) > 0: for map in empty_maps: grass.run_command("g.remove", flags='f', type="rast", pattern=map.get_name(), quiet=True)
def main(): # Get the options input = options["input"] output = options["output"] start = options["start"] stop = options["stop"] base = options["basename"] cycle = options["cycle"] lower = options["lower"] upper = options["upper"] offset = options["offset"] limits = options["limits"] shift = options["shift"] scale = options["scale"] method = options["method"] granularity = options["granularity"] register_null = flags["n"] reverse = flags["r"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = tgis.get_current_mapset() if input.find("@") >= 0: id = input else: id = input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if input_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (id)) input_strds.select(dbif) if output.find("@") >= 0: out_id = output else: out_id = output + "@" + mapset # The output space time raster dataset output_strds = tgis.SpaceTimeRasterDataset(out_id) if output_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal(_("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % out_id) if tgis.check_granularity_string(granularity, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid granularity")) if tgis.check_granularity_string(cycle, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid cycle")) if offset: if tgis.check_granularity_string(offset, input_strds.get_temporal_type()) == False: dbif.close() grass.fatal(_("Invalid offset")) # The lower threshold space time raster dataset if lower: if not range: dbif.close() grass.fatal(_("You need to set the range to compute the occurrence" " space time raster dataset")) if lower.find("@") >= 0: lower_id = lower else: lower_id = lower + "@" + mapset lower_strds = tgis.SpaceTimeRasterDataset(lower_id) if lower_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (lower_strds.get_id())) if lower_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal(_("Temporal type of input strds and lower strds must be equal")) lower_strds.select(dbif) # The upper threshold space time raster dataset if upper: if not lower: dbif.close() grass.fatal(_("The upper option works only in conjunction with the lower option")) if upper.find("@") >= 0: upper = upper else: upper_id = upper + "@" + mapset upper_strds = tgis.SpaceTimeRasterDataset(upper_id) if upper_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (upper_strds.get_id())) if upper_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal(_("Temporal type of input strds and upper strds must be equal")) upper_strds.select(dbif) input_strds_start, input_strds_end = input_strds.get_temporal_extent_as_tuple() if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) if stop: stop = tgis.string_to_datetime(stop) else: stop = input_strds_end start = tgis.adjust_datetime_to_granularity(start, granularity) else: start = int(start) if stop: stop = int(stop) else: stop = input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) else: end = start + cycle limit_relations = ["EQUALS", "DURING", "OVERLAPS", "OVERLAPPING", "CONTAINS"] count = 1 output_maps = [] while input_strds_end > start and stop > start: # Make sure that the cyclic computation will stop at the correct time if stop and end > stop: end = stop where = "start_time >= \'%s\' AND start_time < \'%s\'"%(str(start), str(end)) input_maps = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) grass.message(_("Processing cycle %s - %s"%(str(start), str(end)))) if len(input_maps) == 0: continue # Lets create a dummy list of maps with granularity conform intervals gran_list = [] gran_list_low = [] gran_list_up = [] gran_start = start while gran_start < end: map = input_strds.get_new_map_instance("%i@%i"%(count, count)) if input_strds.is_time_absolute(): gran_end = tgis.increment_datetime_by_string(gran_start, granularity) map.set_absolute_time(gran_start, gran_end) gran_start = tgis.increment_datetime_by_string(gran_start, granularity) else: gran_end = gran_start + granularity map.set_relative_time(gran_start, gran_end, input_strds.get_relative_time_unit()) gran_start = gran_start + granularity gran_list.append(copy(map)) gran_list_low.append(copy(map)) gran_list_up.append(copy(map)) # Lists to compute the topology with upper and lower datasets # Create the topology between the granularity conform list and all maps # of the current cycle gran_topo = tgis.SpatioTemporalTopologyBuilder() gran_topo.build(gran_list, input_maps) if lower: lower_maps = lower_strds.get_registered_maps_as_objects(dbif=dbif) gran_lower_topo = tgis.SpatioTemporalTopologyBuilder() gran_lower_topo.build(gran_list_low, lower_maps) if upper: upper_maps = upper_strds.get_registered_maps_as_objects(dbif=dbif) gran_upper_topo = tgis.SpatioTemporalTopologyBuilder() gran_upper_topo.build(gran_list_up, upper_maps) old_map_name = None # Aggregate num_maps = len(gran_list) for i in xrange(num_maps): if reverse: map = gran_list[num_maps - i - 1] else: map = gran_list[i] # Select input maps based on temporal topology relations input_maps = [] if map.get_equal(): input_maps += map.get_equal() elif map.get_contains(): input_maps += map.get_contains() elif map.get_overlaps(): input_maps += map.get_overlaps() elif map.get_overlapped(): input_maps += map.get_overlapped() elif map.get_during(): input_maps += map.get_during() # Check input maps if len(input_maps) == 0: continue # New output map output_map_name = "%s_%i" % (base, count) output_map_id = map.build_id(output_map_name, mapset) output_map = input_strds.get_new_map_instance(output_map_id) # Check if new map is in the temporal database if output_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry output_map.delete(dbif) output_map = input_strds.get_new_map_instance(output_map_id) else: grass.fatal(_("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (output_map.get_map_id())) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): output_map.set_absolute_time(map_start, map_end) else: output_map.set_relative_time(map_start, map_end, map.get_relative_time_unit()) limits_vals = limits.split(",") limits_lower = float(limits_vals[0]) limits_upper = float(limits_vals[1]) lower_map_name = None if lower: relations = gran_list_low[i].get_temporal_relations() for relation in limit_relations: if relation in relations: lower_map_name = str(relations[relation][0].get_id()) break upper_map_name = None if upper: relations = gran_list_up[i].get_temporal_relations() for relation in limit_relations: if relation in relations: upper_map_name = str(relations[relation][0].get_id()) break input_map_names = [] for input_map in input_maps: input_map_names.append(input_map.get_id()) # Set up the module accmod = Module("r.series.accumulate", input=input_map_names, output=output_map_name, run_=False) if old_map_name: accmod.inputs["basemap"].value = old_map_name if lower_map_name: accmod.inputs["lower"].value = lower_map_name if upper_map_name: accmod.inputs["upper"].value = upper_map_name accmod.inputs["limits"].value = (limits_lower, limits_upper) if shift: accmod.inputs["shift"].value = float(shift) if scale: accmod.inputs["scale"].value = float(scale) if method: accmod.inputs["method"].value = method print accmod accmod.run() if accmod.popen.returncode != 0: dbif.close() grass.fatal(_("Error running r.series.accumulate")) output_maps.append(output_map) old_map_name = output_map_name count += 1 # Increment the cycle start = end if input_strds.is_time_absolute(): start = end if offset: start = tgis.increment_datetime_by_string(end, offset) end = tgis.increment_datetime_by_string(start, cycle) else: if offset: start = end + offset end = start + cycle # Insert the maps into the output space time dataset if output_strds.is_in_db(dbif): if grass.overwrite(): output_strds.delete(dbif) output_strds = input_strds.get_new_instance(out_id) temporal_type, semantic_type, title, description = input_strds.get_initial_values() output_strds.set_initial_values(temporal_type, semantic_type, title, description) output_strds.insert(dbif) empty_maps = [] # Register the maps in the database count = 0 for output_map in output_maps: count += 1 if count%10 == 0: grass.percent(count, len(output_maps), 1) # Read the raster map data output_map.load() # In case of a empty map continue, do not register empty maps if not register_null: if output_map.metadata.get_min() is None and \ output_map.metadata.get_max() is None: empty_maps.append(output_map) continue # Insert map in temporal database output_map.insert(dbif) output_strds.register_map(output_map, dbif) # Update the spatio-temporal extent and the metadata table entries output_strds.update_from_registered_maps(dbif) grass.percent(1, 1, 1) dbif.close() # Remove empty maps if len(empty_maps) > 0: for map in empty_maps: grass.run_command("g.remove", flags='f', type="rast", pattern=map.get_name(), quiet=True)
def main(): # Get the options input = options["input"] start = options["start"] stop = options["stop"] base = options["basename"] cycle = options["cycle"] offset = options["offset"] minimum = options["minimum"] maximum = options["maximum"] occurrence = options["occurrence"] range = options["range"] indicator = options["indicator"] staend = options["staend"] register_null = flags["n"] reverse = flags["r"] grass.set_raise_on_error(True) # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = tgis.get_current_mapset() if input.find("@") >= 0: id = input else: id = input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if input_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time %s dataset <%s> not found") % ( input_strds.get_output_map_instance(None).get_type(), id)) input_strds.select(dbif) dummy = input_strds.get_new_map_instance(None) # The occurrence space time raster dataset if occurrence: if not minimum or not maximum: if not range: dbif.close() grass.fatal(_("You need to set the range to compute the occurrence" " space time raster dataset")) if occurrence.find("@") >= 0: occurrence_id = occurrence else: occurrence_id = occurrence + "@" + mapset occurrence_strds = tgis.SpaceTimeRasterDataset(occurrence_id) if occurrence_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal(_("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % occurrence_id) # The indicator space time raster dataset if indicator: if not occurrence: dbif.close() grass.fatal(_("You need to set the occurrence to compute the indicator" " space time raster dataset")) if not staend: dbif.close() grass.fatal(_("You need to set the staend options to compute the indicator" " space time raster dataset")) if indicator.find("@") >= 0: indicator = indicator else: indicator_id = indicator + "@" + mapset indicator_strds = tgis.SpaceTimeRasterDataset(indicator_id) if indicator_strds.is_in_db(dbif): if not grass.overwrite(): dbif.close() grass.fatal(_("Space time raster dataset <%s> is already in the " "database, use overwrite flag to overwrite") % indicator_id) staend = staend.split(",") indicator_start = int(staend[0]) indicator_mid = int(staend[1]) indicator_end = int(staend[2]) # The minimum threshold space time raster dataset minimum_strds = None if minimum: if minimum.find("@") >= 0: minimum_id = minimum else: minimum_id = minimum + "@" + mapset minimum_strds = tgis.SpaceTimeRasterDataset(minimum_id) if minimum_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (minimum_strds.get_id())) if minimum_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal(_("Temporal type of input strds and minimum strds must be equal")) minimum_strds.select(dbif) # The maximum threshold space time raster dataset maximum_strds = None if maximum: if maximum.find("@") >= 0: maximum_id = maximum else: maximum_id = maximum + "@" + mapset maximum_strds = tgis.SpaceTimeRasterDataset(maximum_id) if maximum_strds.is_in_db() == False: dbif.close() grass.fatal(_("Space time raster dataset <%s> not found") % (maximum_strds.get_id())) if maximum_strds.get_temporal_type() != input_strds.get_temporal_type(): dbif.close() grass.fatal(_("Temporal type of input strds and maximum strds must be equal")) maximum_strds.select(dbif) input_strds_start, input_strds_end = input_strds.get_temporal_extent_as_tuple() if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) if stop: stop = tgis.string_to_datetime(stop) else: stop = input_strds_end else: start = int(start) if stop: stop = int(stop) else: stop = input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) else: end = start + cycle count = 1 indi_count = 1 occurrence_maps = {} indicator_maps = {} while input_strds_end > start and stop > start: # Make sure that the cyclic computation will stop at the correct time if stop and end > stop: end = stop where = "start_time >= \'%s\' AND start_time < \'%s\'"%(str(start), str(end)) input_maps = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) print len(input_maps) input_topo = tgis.SpatioTemporalTopologyBuilder() input_topo.build(input_maps, input_maps) if len(input_maps) == 0: continue grass.message(_("Processing cycle %s - %s"%(str(start), str(end)))) count = compute_occurrence(occurrence_maps, input_strds, input_maps, start, base, count, mapset, where, reverse, range, minimum_strds, maximum_strds, dbif) # Indicator computation is based on the occurrence so we need to start it after # the occurrence cycle if indicator: num_maps = len(input_maps) for i in xrange(num_maps): if reverse: map = input_maps[num_maps - i - 1] else: map = input_maps[i] indicator_map_name = "%s_indicator_%i" % (base, indi_count) indicator_map_id = dummy.build_id(indicator_map_name, mapset) indicator_map = input_strds.get_new_map_instance(indicator_map_id) # Check if new map is in the temporal database if indicator_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry indicator_map.delete(dbif) indicator_map = input_strds.get_new_map_instance(indicator_map_id) else: grass.fatal(_("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (indicator_map.get_map_id())) curr_map = occurrence_maps[map.get_id()].get_name() # Reverse time if reverse: if i == 0: prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i"%(indicator_start) elif i > 0 and i < num_maps - 1: prev_map = occurrence_maps[map.next().get_id()].get_name() next_map = occurrence_maps[map.prev().get_id()].get_name() # In case the previous map is null() set null() or the start indicator subexpr1 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_start) # In case the previous map was not null() if the current map is null() set null() # if the current map is not null() and the next map is not null() set # intermediate indicator, if the next map is null set the end indicator subexpr2 = "if(isnull(%s), %i, %i)"%(next_map, indicator_end, indicator_mid) subexpr3 = "if(isnull(%s), null(), %s)"%(curr_map, subexpr2) expression = "%s = if(isnull(%s), %s, %s)"%(indicator_map_name, prev_map, subexpr1, subexpr3) else: prev_map = occurrence_maps[map.next().get_id()].get_name() subexpr1 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_start) subexpr3 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_mid) else: if i == 0: prev_map = curr_map subexpr1 = "null()" subexpr3 = "%i"%(indicator_start) elif i > 0 and i < num_maps - 1: prev_map = occurrence_maps[map.prev().get_id()].get_name() next_map = occurrence_maps[map.next().get_id()].get_name() # In case the previous map is null() set null() or the start indicator subexpr1 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_start) # In case the previous map was not null() if the current map is null() set null() # if the current map is not null() and the next map is not null() set # intermediate indicator, if the next map is null set the end indicator subexpr2 = "if(isnull(%s), %i, %i)"%(next_map, indicator_end, indicator_mid) subexpr3 = "if(isnull(%s), null(), %s)"%(curr_map, subexpr2) expression = "%s = if(isnull(%s), %s, %s)"%(indicator_map_name, prev_map, subexpr1, subexpr3) else: prev_map = occurrence_maps[map.prev().get_id()].get_name() subexpr1 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_start) subexpr3 = "if(isnull(%s), null(), %i)"%(curr_map, indicator_mid) expression = "%s = if(isnull(%s), %s, %s)"%(indicator_map_name, prev_map, subexpr1, subexpr3) print expression grass.mapcalc(expression, overwrite=True) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): indicator_map.set_absolute_time(map_start, map_end) else: indicator_map.set_relative_time(map_start, map_end, map.get_relative_time_unit()) indicator_maps[map.get_id()] = indicator_map indi_count += 1 # Increment the cycle start = end if input_strds.is_time_absolute(): start = end if offset: start = tgis.increment_datetime_by_string(end, offset) end = tgis.increment_datetime_by_string(start, cycle) else: if offset: start = end + offset end = start + cycle empty_maps = [] create_strds_register_maps(input_strds, occurrence_strds, occurrence_maps, register_null, empty_maps, dbif) if indicator: create_strds_register_maps(input_strds, indicator_strds, indicator_maps, register_null, empty_maps, dbif) dbif.close() # Remove empty maps if len(empty_maps) > 0: for map in empty_maps: grass.run_command("g.remove", flags='f', type="rast", pattern=map.get_name(), quiet=True)