Browse Source

Added offset option to aggregation function. Fixed wrong exit value checks of r.mapcalc runs.
Better error handling when loading map data. Added patch from ticket https://trac.osgeo.org/grass/ticket/2281 to speedup
r.series runs for aggregation.


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@60173 15284696-431f-4ddb-bdfa-cd5b030d7da7

Soeren Gebbert 11 years ago
parent
commit
144fca5245

+ 12 - 5
lib/python/temporal/aggregation.py

@@ -102,7 +102,7 @@ def collect_map_names(sp, dbif, start, end, sampling):
 
 
 def aggregate_raster_maps(inputs, base, start, end, count, method,
-                          register_null, dbif):
+                          register_null, dbif,  offset=0):
     """!Aggregate a list of raster input maps with r.series
 
        @param inputs The names of the raster maps to be aggregated
@@ -117,12 +117,13 @@ def aggregate_raster_maps(inputs, base, start, end, count, method,
        @param register_null If true null maps will be registered in the space
                             time raster dataset, if false not
        @param dbif The temporal database interface to use
+       @param offset Offset to be added to the map counter to create the map ids
     """
 
     msgr = get_tgis_message_interface()
 
     msgr.verbose(_("Aggregate %s raster maps") % (len(inputs)))
-    output = "%s_%i" % (base, count)
+    output = "%s_%i" % (base, int(offset) + count)
 
     mapset = get_current_mapset()
     map_id = output + "@" + mapset
@@ -151,10 +152,16 @@ def aggregate_raster_maps(inputs, base, start, end, count, method,
         file.write(string)
 
     file.close()
+    
     # Run r.series
-    ret = core.run_command("r.series", flags="z", file=filename,
-                           output=output, overwrite=core.overwrite(),
-                           method=method)
+    if len(inputs) > 1000 :
+        ret = core.run_command("r.series", flags="z", file=filename,
+                               output=output, overwrite=core.overwrite(),
+                               method=method)
+    else:
+        ret = core.run_command("r.series", file=filename,
+                               output=output, overwrite=core.overwrite(),
+                               method=method)
 
     if ret != 0:
         dbif.close()

+ 6 - 7
lib/python/temporal/extract.py

@@ -152,7 +152,6 @@ def extract_dataset(input, output, type, where, expression, base, nprocs=1,
                     for proc in proc_list:
                         proc.join()
                         exitcodes += proc.exitcode
-
                     if exitcodes != 0:
                         dbif.close()
                         msgr.fatal(_("Error while computation"))
@@ -248,18 +247,18 @@ def extract_dataset(input, output, type, where, expression, base, nprocs=1,
 
 def run_mapcalc2d(expr):
     """Helper function to run r.mapcalc in parallel"""
-    return core.run_command("r.mapcalc", expression=expr,
-                            overwrite=core.overwrite(), quiet=True)
+    exit(core.run_command("r.mapcalc", expression=expr,
+                            overwrite=core.overwrite(), quiet=True))
 
 
 def run_mapcalc3d(expr):
     """Helper function to run r3.mapcalc in parallel"""
-    return core.run_command("r3.mapcalc", expression=expr,
-                            overwrite=core.overwrite(), quiet=True)
+    exit(core.run_command("r3.mapcalc", expression=expr,
+                            overwrite=core.overwrite(), quiet=True))
 
 
 def run_vector_extraction(input, output, layer, type, where):
     """Helper function to run r.mapcalc in parallel"""
-    return core.run_command("v.extract", input=input, output=output,
+    exit(core.run_command("v.extract", input=input, output=output,
                             layer=layer, type=type, where=where,
-                            overwrite=core.overwrite(), quiet=True)
+                            overwrite=core.overwrite(), quiet=True))

+ 4 - 4
lib/python/temporal/mapcalc.py

@@ -340,16 +340,16 @@ def dataset_mapcalculator(inputs, output, type, expression, base, method,
 
 def _run_mapcalc2d(expr):
     """Helper function to run r.mapcalc in parallel"""
-    return core.run_command("r.mapcalc", expression=expr,
-                            overwrite=core.overwrite(), quiet=True)
+    exit(core.run_command("r.mapcalc", expression=expr,
+                            overwrite=core.overwrite(), quiet=True))
 
 ###############################################################################
 
 
 def _run_mapcalc3d(expr):
     """Helper function to run r3.mapcalc in parallel"""
-    return core.run_command("r3.mapcalc", expression=expr,
-                            overwrite=core.overwrite(), quiet=True)
+    exit(core.run_command("r3.mapcalc", expression=expr,
+                            overwrite=core.overwrite(), quiet=True))
 
 ###############################################################################
 

+ 104 - 59
lib/python/temporal/space_time_datasets.py

@@ -286,8 +286,17 @@ class RasterDataset(AbstractMapDataset):
                                              self.get_mapset())
 
     def load(self):
-        """!Load all info from an existing raster map into the internal s
-           tructure"""
+        """!Load all info from an existing raster map into the internal structure
+            
+            This method checks first if the map exists, in case it exists
+            the metadata of the map is put into this object and True is returned.
+            
+            @return True is the map exists and the metadata was filled successfully
+                          and getting the data was successfull, False otherwise
+        """
+
+        if self.map_exists() is not True:
+            return False
 
         # Fill base information
         self.base.set_creator(str(getpass.getuser()))
@@ -295,27 +304,32 @@ class RasterDataset(AbstractMapDataset):
         kvp = self.ciface.read_raster_info(self.get_name(),
                                            self.get_mapset())
 
-        # Fill spatial extent
-        self.set_spatial_extent_from_values(north=kvp["north"],
-                                            south=kvp["south"],
-                                            east=kvp["east"],
-                                            west=kvp["west"])
+        if kvp:
+            # Fill spatial extent
+            self.set_spatial_extent_from_values(north=kvp["north"],
+                                                south=kvp["south"],
+                                                east=kvp["east"],
+                                                west=kvp["west"])
+
+            # Fill metadata
+            self.metadata.set_nsres(kvp["nsres"])
+            self.metadata.set_ewres(kvp["ewres"])
+            self.metadata.set_datatype(kvp["datatype"])
+            self.metadata.set_min(kvp["min"])
+            self.metadata.set_max(kvp["max"])
 
-        # Fill metadata
-        self.metadata.set_nsres(kvp["nsres"])
-        self.metadata.set_ewres(kvp["ewres"])
-        self.metadata.set_datatype(kvp["datatype"])
-        self.metadata.set_min(kvp["min"])
-        self.metadata.set_max(kvp["max"])
+            rows = int(kvp["rows"])
+            cols = int(kvp["cols"])
 
-        rows = int(kvp["rows"])
-        cols = int(kvp["cols"])
+            ncells = cols * rows
 
-        ncells = cols * rows
+            self.metadata.set_cols(cols)
+            self.metadata.set_rows(rows)
+            self.metadata.set_number_of_cells(ncells)
+            
+            return True
 
-        self.metadata.set_cols(cols)
-        self.metadata.set_rows(rows)
-        self.metadata.set_number_of_cells(ncells)
+        return False
 
 ###############################################################################
 
@@ -598,7 +612,17 @@ class Raster3DDataset(AbstractMapDataset):
                                                self.get_mapset())
 
     def load(self):
-        """!Load all info from an existing raster3d map into the internal structure"""
+        """!Load all info from an existing 3d raster map into the internal structure
+            
+            This method checks first if the map exists, in case it exists
+            the metadata of the map is put into this object and True is returned.
+            
+            @return True is the map exists and the metadata was filled successfully
+                          and getting the data was successfull, False otherwise
+        """
+
+        if self.map_exists() is not True:
+            return False
 
         # Fill base information
         self.base.set_creator(str(getpass.getuser()))
@@ -607,28 +631,33 @@ class Raster3DDataset(AbstractMapDataset):
         kvp = self.ciface.read_raster3d_info(self.get_name(),
                                            self.get_mapset())
 
-        self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
-                                east=kvp["east"], west=kvp["west"],
-                                top=kvp["top"], bottom=kvp["bottom"])
+        if kvp:
+            self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
+                                    east=kvp["east"], west=kvp["west"],
+                                    top=kvp["top"], bottom=kvp["bottom"])
 
-        # Fill metadata
-        self.metadata.set_nsres(kvp["nsres"])
-        self.metadata.set_ewres(kvp["ewres"])
-        self.metadata.set_tbres(kvp["tbres"])
-        self.metadata.set_datatype(kvp["datatype"])
-        self.metadata.set_min(kvp["min"])
-        self.metadata.set_max(kvp["max"])
+            # Fill metadata
+            self.metadata.set_nsres(kvp["nsres"])
+            self.metadata.set_ewres(kvp["ewres"])
+            self.metadata.set_tbres(kvp["tbres"])
+            self.metadata.set_datatype(kvp["datatype"])
+            self.metadata.set_min(kvp["min"])
+            self.metadata.set_max(kvp["max"])
 
-        rows = int(kvp["rows"])
-        cols = int(kvp["cols"])
-        depths = int(kvp["depths"])
+            rows = int(kvp["rows"])
+            cols = int(kvp["cols"])
+            depths = int(kvp["depths"])
 
-        ncells = cols * rows * depths
+            ncells = cols * rows * depths
 
-        self.metadata.set_cols(cols)
-        self.metadata.set_rows(rows)
-        self.metadata.set_depths(depths)
-        self.metadata.set_number_of_cells(ncells)
+            self.metadata.set_cols(cols)
+            self.metadata.set_rows(rows)
+            self.metadata.set_depths(depths)
+            self.metadata.set_number_of_cells(ncells)
+            
+            return True
+
+        return False
 
 ###############################################################################
 
@@ -869,8 +898,19 @@ class VectorDataset(AbstractMapDataset):
 
 
     def load(self):
-        """!Load all info from an existing vector map into the internal
-        structure"""
+
+        """!Load all info from an existing vector map into the internal structure
+            
+            This method checks first if the map exists, in case it exists
+            the metadata of the map is put into this object and True is returned.
+            
+            @return True is the map exists and the metadata was filled successfully
+                          and getting the data was successfull, False otherwise
+        """
+
+        if self.map_exists() is not True:
+            return False
+
 
         # Fill base information
         self.base.set_creator(str(getpass.getuser()))
@@ -880,25 +920,30 @@ class VectorDataset(AbstractMapDataset):
         kvp = self.ciface.read_vector_info(self.get_name(),
                                            self.get_mapset())
 
-        # Fill spatial extent
-        self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
-                                east=kvp["east"], west=kvp["west"],
-                                top=kvp["top"], bottom=kvp["bottom"])
-
-        # Fill metadata
-        self.metadata.set_3d_info(kvp["map3d"])
-        self.metadata.set_number_of_points(kvp["points"])
-        self.metadata.set_number_of_lines(kvp["lines"])
-        self.metadata.set_number_of_boundaries(kvp["boundaries"])
-        self.metadata.set_number_of_centroids(kvp["centroids"])
-        self.metadata.set_number_of_faces(kvp["faces"])
-        self.metadata.set_number_of_kernels(kvp["kernels"])
-        self.metadata.set_number_of_primitives(kvp["primitives"])
-        self.metadata.set_number_of_nodes(kvp["nodes"])
-        self.metadata.set_number_of_areas(kvp["areas"])
-        self.metadata.set_number_of_islands(kvp["islands"])
-        self.metadata.set_number_of_holes(kvp["holes"])
-        self.metadata.set_number_of_volumes(kvp["volumes"])
+        if kvp:
+            # Fill spatial extent
+            self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
+                                    east=kvp["east"], west=kvp["west"],
+                                    top=kvp["top"], bottom=kvp["bottom"])
+
+            # Fill metadata
+            self.metadata.set_3d_info(kvp["map3d"])
+            self.metadata.set_number_of_points(kvp["points"])
+            self.metadata.set_number_of_lines(kvp["lines"])
+            self.metadata.set_number_of_boundaries(kvp["boundaries"])
+            self.metadata.set_number_of_centroids(kvp["centroids"])
+            self.metadata.set_number_of_faces(kvp["faces"])
+            self.metadata.set_number_of_kernels(kvp["kernels"])
+            self.metadata.set_number_of_primitives(kvp["primitives"])
+            self.metadata.set_number_of_nodes(kvp["nodes"])
+            self.metadata.set_number_of_areas(kvp["areas"])
+            self.metadata.set_number_of_islands(kvp["islands"])
+            self.metadata.set_number_of_holes(kvp["holes"])
+            self.metadata.set_number_of_volumes(kvp["volumes"])
+            
+            return True
+
+        return False
 
 ###############################################################################