[GRASS-SVN] r51479 - in grass/trunk: lib/python/temporal temporal/t.rast.aggregate temporal/t.rast.import temporal/t.support

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Apr 20 09:00:24 EDT 2012


Author: huhabla
Date: 2012-04-20 06:00:24 -0700 (Fri, 20 Apr 2012)
New Revision: 51479

Modified:
   grass/trunk/lib/python/temporal/core.py
   grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py
   grass/trunk/temporal/t.rast.import/t.rast.import.py
   grass/trunk/temporal/t.rast.import/test.t.rast.import.sh
   grass/trunk/temporal/t.support/t.support.py
Log:
Added ability to create a new location and import data into the new location to t.rast.import


Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py	2012-04-20 12:51:01 UTC (rev 51478)
+++ grass/trunk/lib/python/temporal/core.py	2012-04-20 13:00:24 UTC (rev 51479)
@@ -83,7 +83,7 @@
     """
     
     database = get_temporal_dbmi_init_string()
-
+    
     db_exists = False
 
     # Check if the database already exists
@@ -104,6 +104,8 @@
     if db_exists == True:
 	return
     
+    core.message(_("Create temporal database: %s"%(database)))
+    
     # Read all SQL scripts and templates
     map_tables_template_sql = open(os.path.join(get_sql_template_path(), "map_tables_template.sql"), 'r').read()
     raster_metadata_sql = open(os.path.join(get_sql_template_path(), "raster_metadata_table.sql"), 'r').read()

Modified: grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py
===================================================================
--- grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py	2012-04-20 12:51:01 UTC (rev 51478)
+++ grass/trunk/temporal/t.rast.aggregate/t.rast.aggregate.py	2012-04-20 13:00:24 UTC (rev 51479)
@@ -148,8 +148,7 @@
     next_start_time = first_start_time
 
     count = 0
-    proc_count = 0
-    proc_list = []
+    
     while next_start_time <= last_start_time:
         start = next_start_time
         if sp.is_time_absolute():

Modified: grass/trunk/temporal/t.rast.import/t.rast.import.py
===================================================================
--- grass/trunk/temporal/t.rast.import/t.rast.import.py	2012-04-20 12:51:01 UTC (rev 51478)
+++ grass/trunk/temporal/t.rast.import/t.rast.import.py	2012-04-20 13:00:24 UTC (rev 51479)
@@ -50,6 +50,14 @@
 #% multiple: no
 #%end
 
+#%option
+#% key: location
+#% type: string
+#% description: Create a new location and import the data into it. No not run this module in parallel or interrupt it when a new location should be created.
+#% required: no
+#% multiple: no
+#%end
+
 #%flag
 #% key: l
 #% description: Link the raster files using r.external
@@ -62,7 +70,7 @@
 
 #%flag
 #% key: o
-#% description: verride projection (use location's projection)
+#% description: override projection (use location's projection)
 #%end
 
 
@@ -73,6 +81,7 @@
 import tempfile
 import grass.script as grass
 import grass.temporal as tgis
+import time
 
 proj_file_name = "proj.txt"
 init_file_name = "init.txt"
@@ -88,18 +97,18 @@
     extrdir = options["extrdir"]
     title = options["title"]
     descr = options["description"]
+    location = options["location"]
     link = flags["l"]
     exp = flags["e"]
     overr = flags["o"]
 
-    # Make sure the temporal database exists
-    tgis.create_temporal_database()
+    grass.set_raise_on_error(True)
     
     # Check if input file and extraction directory exits
     if not os.path.exists(input): 
-        grass.fatal(_("Space time raster dataset archive <%s> not found.") % input)
+        grass.fatal(_("Space time raster dataset archive <%s> not found") % input)
     if not os.path.exists(extrdir): 
-        grass.fatal(_("Extraction directory <%s> not found.") % extrdir)
+        grass.fatal(_("Extraction directory <%s> not found") % extrdir)
 
     tar = tarfile.open(input)
     
@@ -107,126 +116,175 @@
     members = tar.getnames()
 
     if init_file_name not in members: 
-        grass.fatal(_("Unable to find init file <%s>.") % init_file_name)
+        grass.fatal(_("Unable to find init file <%s>") % init_file_name)
     if list_file_name not in members: 
-        grass.fatal(_("Unable to find list file <%s>.") % list_file_name)
+        grass.fatal(_("Unable to find list file <%s>") % list_file_name)
     if proj_file_name not in members: 
-        grass.fatal(_("Unable to find projection file <%s>.") % proj_file_name)
+        grass.fatal(_("Unable to find projection file <%s>") % proj_file_name)
 
     tar.extractall(path=extrdir)
     tar.close()
+    
+    # Save current working directory path
+    old_cwd = os.getcwd()
 
-    # Switch into the extraction directory and check for files
+    # Switch into the data directory
     os.chdir(extrdir)
+    
+    # Create a new location based on the projection information and switch into it
+    old_env = grass.gisenv()
+    if location:
+	try:
+	    proj4_string = open(proj_file_name, 'r').read()
+	    grass.create_location(dbase=old_env["GISDBASE"], 
+	                          location=location, 
+	                          proj4=proj4_string)
+	except:
+	    grass.fatal(_("Unable to create location %s") % location)
+	# Switch to the new created location
+	ret = grass.run_command("g.mapset", mapset="PERMANENT", 
+	                                    location=location, 
+	                                    gisdbase=old_env["GISDBASE"])
+	if ret != 0:                            
+	    grass.fatal(_("Unable to switch to location %s") % location)
+	# create default database connection
+	ret = grass.run_command("t.connect", flags="d")
+	if ret != 0:                            
+	    grass.fatal(_("Unable to create default temporal database in new location %s") % location)
 
-    fs = "|"
-    maplist = []
-    mapset =  grass.gisenv()["MAPSET"]
-    list_file = open(list_file_name, "r")
+    try:
+	# Make sure the temporal database exists
+	tgis.create_temporal_database()
+	
+	fs = "|"
+	maplist = []
+	mapset =  grass.gisenv()["MAPSET"]
+	list_file = open(list_file_name, "r")
 
-    # Read the map list from file
-    line_count = 0
-    while True:
-        line = list_file.readline()
-        if not line:
-            break
+	# Read the map list from file
+	line_count = 0
+	while True:
+	    line = list_file.readline()
+	    if not line:
+		break
 
-        line_list = line.split(fs)
+	    line_list = line.split(fs)
 
-        mapname = line_list[0].strip()
-        mapid = mapname + "@" + mapset
+	    mapname = line_list[0].strip()
+	    mapid = mapname + "@" + mapset
 
-        row = {}
-        row["name"] = mapname
-        row["id"] = mapid
-        row["start"] = line_list[1].strip()
-        row["end"] = line_list[2].strip()
+	    row = {}
+	    row["name"] = mapname
+	    row["id"] = mapid
+	    row["start"] = line_list[1].strip()
+	    row["end"] = line_list[2].strip()
 
-        maplist.append(row)
-        line_count += 1
+	    maplist.append(row)
+	    line_count += 1
 
-    list_file.close()
-    
-    # Check if geotiff files exists
-    for row in maplist:
-        filename = str(row["name"]) + ".tif"
-        if not os.path.exists(filename): 
-            grass.fatal(_("Unable to find geotiff raster file <%s> in archive.") % filename)
+	list_file.close()
+	
+	# Check if geotiff files exists
+	for row in maplist:
+	    filename = str(row["name"]) + ".tif"
+	    if not os.path.exists(filename): 
+		grass.fatal(_("Unable to find geotiff raster file <%s> in archive.") % filename)
 
-    # Read the init file
-    fs = "="
-    init = {}
-    init_file = open(init_file_name, "r")
-    while True:
-        line = init_file.readline()
-        if not line:
-            break
+	# Read the init file
+	fs = "="
+	init = {}
+	init_file = open(init_file_name, "r")
+	while True:
+	    line = init_file.readline()
+	    if not line:
+		break
 
-        kv = line.split(fs)
-        init[kv[0]] = kv[1].strip()
-    
-    init_file.close()
+	    kv = line.split(fs)
+	    init[kv[0]] = kv[1].strip()
+	
+	init_file.close()
 
-    if not init.has_key("temporal_type") or \
-       not init.has_key("semantic_type") or \
-       not init.has_key("number_of_maps"):
-        grass.fatal(_("Key words %s, %s or %s not found in init file.") % ("temporal_type", "semantic_type", "number_of_maps"))
+	if not init.has_key("temporal_type") or \
+	not init.has_key("semantic_type") or \
+	not init.has_key("number_of_maps"):
+	    grass.fatal(_("Key words %s, %s or %s not found in init file.") % ("temporal_type", "semantic_type", "number_of_maps"))
 
-    if line_count != int(init["number_of_maps"]):
-        grass.fatal(_("Number of maps mismatch in init and list file."))
+	if line_count != int(init["number_of_maps"]):
+	    grass.fatal(_("Number of maps mismatch in init and list file."))
 
-    # Check the space time dataset
+	# Check the space time dataset
 
-    id = output + "@" + mapset
+	id = output + "@" + mapset
 
-    sp = tgis.space_time_raster_dataset(id)
-    
-    if sp.is_in_db() and grass.overwrite() == False:
-        grass.fatal(_("Space time %s dataset <%s> is already in the database. Use the overwrite flag.") % name)
+	sp = tgis.space_time_raster_dataset(id)
+	
+	if sp.is_in_db() and grass.overwrite() == False:
+	    grass.fatal(_("Space time %s dataset <%s> is already in the database. Use the overwrite flag.") % name)
 
-    # Try to import/link the raster files
-    for row in maplist:
-        name = row["name"]
-        filename = str(row["name"]) + ".tif"
-        impflags=""
-        if overr:
-            impflags += "o"
-        if exp:
-            impflags += "e"
-        
-        if link:
-            ret = grass.run_command("r.external", input=filename, output=name, flags=impflags, overwrite=grass.overwrite())
-        else:
-            ret = grass.run_command("r.in.gdal", input=filename, output=name, flags=impflags, overwrite=grass.overwrite())
+	# Try to import/link the raster files
+	for row in maplist:
+	    name = row["name"]
+	    filename = str(row["name"]) + ".tif"
+	    impflags=""
+	    if overr:
+		impflags += "o"
+	    if exp or location:
+		impflags += "e"
+	    
+	    if link:
+		ret = grass.run_command("r.external", input=filename, 
+		                                      output=name, 
+		                                      flags=impflags, 
+		                                      overwrite=grass.overwrite())
+	    else:
+		ret = grass.run_command("r.in.gdal", input=filename, 
+						     output=name, 
+						     flags=impflags, 
+						     overwrite=grass.overwrite())
 
-        if ret != 0:
-            grass.fatal(_("Unable to import/link raster map <%s>.") % name)
-    
-        # Set the color rules if present
-        filename = str(row["name"]) + ".color"
-        if os.path.isfile(filename):
-            ret = grass.run_command("r.colors", map=name, rules=filename, overwrite=grass.overwrite())
+	    if ret != 0:
+		grass.fatal(_("Unable to import/link raster map <%s>.") % name)
+	
+	    # Set the color rules if present
+	    filename = str(row["name"]) + ".color"
+	    if os.path.isfile(filename):
+		ret = grass.run_command("r.colors", map=name, 
+						    rules=filename, 
+						    overwrite=grass.overwrite())
 
-            if ret != 0:
-                grass.fatal(_("Unable to set the color rules for raster map <%s>.") % name)
+		if ret != 0:
+		    grass.fatal(_("Unable to set the color rules for raster map <%s>.") % name)
 
-    # Create the space time raster dataset
-    if sp.is_in_db() and grass.overwrite() == True:
-        grass.info(_("Overwrite space time %s dataset <%s> and unregister all maps.") % (sp.get_new_map_instance(None).get_type(), name))
-        sp.delete()
-        sp = sp.get_new_instance(id)
+	# Create the space time raster dataset
+	if sp.is_in_db() and grass.overwrite() == True:
+	    grass.info(_("Overwrite space time %s dataset <%s> and unregister all maps.") % (sp.get_new_map_instance(None).get_type(), name))
+	    sp.delete()
+	    sp = sp.get_new_instance(id)
 
-    temporal_type = init["temporal_type"]
-    semantic_type = init["semantic_type"]
-    grass.verbose(_("Create space time %s dataset.") % sp.get_new_map_instance(None).get_type())
+	temporal_type = init["temporal_type"]
+	semantic_type = init["semantic_type"]
+	grass.verbose(_("Create space time %s dataset.") % sp.get_new_map_instance(None).get_type())
 
-    sp.set_initial_values(temporal_type=temporal_type, semantic_type=semantic_type, title=title, description=descr)
-    sp.insert()
+	sp.set_initial_values(temporal_type=temporal_type, semantic_type=semantic_type, title=title, description=descr)
+	sp.insert()
 
-    # register the raster maps
-    fs="|"
-    tgis.register_maps_in_space_time_dataset(type="rast", name=output, file=list_file_name, start="file", end="file", dbif=None, fs=fs)
+	# register the raster maps
+	fs="|"
+	tgis.register_maps_in_space_time_dataset(type="rast", name=output, file=list_file_name, start="file", end="file", dbif=None, fs=fs)
 
+	
+	os.chdir(old_cwd)
+    except:
+	raise
+    
+    # Make sure the location is switched back correctly
+    finally:
+	if location:
+	    # Switch to the old location
+	    ret = grass.run_command("g.mapset", mapset=old_env["MAPSET"], 
+						location=old_env["LOCATION_NAME"], 
+						gisdbase=old_env["GISDBASE"])
+    
 if __name__ == "__main__":
     options, flags = grass.parser()
     main()

Modified: grass/trunk/temporal/t.rast.import/test.t.rast.import.sh
===================================================================
--- grass/trunk/temporal/t.rast.import/test.t.rast.import.sh	2012-04-20 12:51:01 UTC (rev 51478)
+++ grass/trunk/temporal/t.rast.import/test.t.rast.import.sh	2012-04-20 13:00:24 UTC (rev 51479)
@@ -32,6 +32,14 @@
 t.register -i type=rast input=precip_abs1 file="${n1}" start="2001-01-01" increment="1 months"
 t.rast.export input=precip_abs1 output=strds_export.tar.bz2 compression=bzip2 workdir=test
 
+# Import the data into a new location
+t.rast.import --o location=new_test_1 input=strds_export.tar.bz2 output=precip_abs1 extrdir=test\
+              title="A test" description="Description of a test"
+
+t.rast.import --o location=new_test_2 input=strds_export.tar.bz2 output=precip_abs1 extrdir=test\
+          -l  title="A test" description="Description of a test"
+
+
 t.rast.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test\
           -oe title="A test" description="Description of a test"
 t.rast.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test\
@@ -41,8 +49,13 @@
 t.rast.import --o input=strds_export.tar.bz2 output=precip_abs1 extrdir=test\
           -l  title="A test" description="Description of a test"
 
+
 t.unregister type=rast maps=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
 t.remove type=strds input=precip_abs1
 g.remove rast=prec_1,prec_2,prec_3,prec_4,prec_5,prec_6
 rm -rf test
-rm strds_export.tar.bz2
\ No newline at end of file
+rm strds_export.tar.bz2
+# Remove the newly created locations
+eval `g.gisenv`
+rm -rf $GISDBASE/new_test_1
+rm -rf $GISDBASE/new_test_2

Modified: grass/trunk/temporal/t.support/t.support.py
===================================================================
--- grass/trunk/temporal/t.support/t.support.py	2012-04-20 12:51:01 UTC (rev 51478)
+++ grass/trunk/temporal/t.support/t.support.py	2012-04-20 13:00:24 UTC (rev 51479)
@@ -149,7 +149,7 @@
 		statement += map.delete(dbif=dbif, update=False, execute=False)
 	
 
-        # Execute the collected SQL statenents
+        # Execute the collected SQL statements
         dbif.execute_transaction(statement)
         
 	# Update the effected space time datasets



More information about the grass-commit mailing list