[GRASS-SVN] r60173 - grass/trunk/lib/python/temporal

svn_grass at osgeo.org svn_grass at osgeo.org
Sat May 10 17:32:03 PDT 2014


Author: huhabla
Date: 2014-05-10 17:32:02 -0700 (Sat, 10 May 2014)
New Revision: 60173

Modified:
   grass/trunk/lib/python/temporal/aggregation.py
   grass/trunk/lib/python/temporal/extract.py
   grass/trunk/lib/python/temporal/mapcalc.py
   grass/trunk/lib/python/temporal/space_time_datasets.py
Log:
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 #2281 to speedup
r.series runs for aggregation.


Modified: grass/trunk/lib/python/temporal/aggregation.py
===================================================================
--- grass/trunk/lib/python/temporal/aggregation.py	2014-05-10 22:57:28 UTC (rev 60172)
+++ grass/trunk/lib/python/temporal/aggregation.py	2014-05-11 00:32:02 UTC (rev 60173)
@@ -102,7 +102,7 @@
 
 
 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 @@
        @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 @@
         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()

Modified: grass/trunk/lib/python/temporal/extract.py
===================================================================
--- grass/trunk/lib/python/temporal/extract.py	2014-05-10 22:57:28 UTC (rev 60172)
+++ grass/trunk/lib/python/temporal/extract.py	2014-05-11 00:32:02 UTC (rev 60173)
@@ -152,7 +152,6 @@
                     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 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))

Modified: grass/trunk/lib/python/temporal/mapcalc.py
===================================================================
--- grass/trunk/lib/python/temporal/mapcalc.py	2014-05-10 22:57:28 UTC (rev 60172)
+++ grass/trunk/lib/python/temporal/mapcalc.py	2014-05-11 00:32:02 UTC (rev 60173)
@@ -340,16 +340,16 @@
 
 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))
 
 ###############################################################################
 

Modified: grass/trunk/lib/python/temporal/space_time_datasets.py
===================================================================
--- grass/trunk/lib/python/temporal/space_time_datasets.py	2014-05-10 22:57:28 UTC (rev 60172)
+++ grass/trunk/lib/python/temporal/space_time_datasets.py	2014-05-11 00:32:02 UTC (rev 60173)
@@ -286,37 +286,51 @@
                                              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()))
 
         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)
+            self.metadata.set_cols(cols)
+            self.metadata.set_rows(rows)
+            self.metadata.set_number_of_cells(ncells)
+            
+            return True
 
+        return False
+
 ###############################################################################
 
 class Raster3DDataset(AbstractMapDataset):
@@ -598,8 +612,18 @@
                                                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,29 +631,34 @@
         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
+
 ###############################################################################
 
 class VectorDataset(AbstractMapDataset):
@@ -869,9 +898,20 @@
 
 
     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,26 +920,31 @@
         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"])
+        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"])
+            # 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
+
 ###############################################################################
 
 class SpaceTimeRasterDataset(AbstractSpaceTimeDataset):



More information about the grass-commit mailing list