Ejemplo n.º 1
0
 def checkMetadata(self, *args, **kwargs):
     """ Check to make sure the project directory has the necessary metadata to run this command.
     
         @note Concrete commands must call this super class method in their own
         implementation of checkMetadata(), and must call their implementation
         near the beginning of their run method.
     
     """
     self.studyArea = RHESSysMetadata.readStudyAreaEntries(self.context)
     self.metadata = RHESSysMetadata.readRHESSysEntries(self.context)
Ejemplo n.º 2
0
 def test_delete(self):
     RHESSysMetadata.writeRHESSysEntry(self.context, "key1", "value_one")
     RHESSysMetadata.writeRHESSysEntry(self.context, "key2", "value_two")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(metadata["key1"] == "value_one")
     RHESSysMetadata.deleteRHESSysEntry(self.context, "key1")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(not 'key1' in metadata)
     # Delete and empty entry
     RHESSysMetadata.deleteRHESSysEntry(self.context, "not_in_store")
Ejemplo n.º 3
0
 def checkMetadata(self, *args, **kwargs):
     """ Check to make sure the project directory has the necessary metadata to run this command.
     
         @note Concrete commands must call this super class method in their own
         implementation of checkMetadata(), and must call their implementation
         near the beginning of their run method.
     
     """
     self.studyArea = RHESSysMetadata.readStudyAreaEntries(self.context)
     self.metadata = RHESSysMetadata.readRHESSysEntries(self.context)
Ejemplo n.º 4
0
 def test_delete(self):
     RHESSysMetadata.writeRHESSysEntry(self.context, "key1", "value_one")
     RHESSysMetadata.writeRHESSysEntry(self.context, "key2", "value_two")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(metadata["key1"] == "value_one")    
     RHESSysMetadata.deleteRHESSysEntry(self.context, "key1")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(not 'key1' in metadata)
     # Delete and empty entry
     RHESSysMetadata.deleteRHESSysEntry(self.context, "not_in_store")
     
Ejemplo n.º 5
0
 def __init__(self, projectDir, configFile=None):
     """ Constructor for Context class
     
         @param projectDir Path of the project whose metadata store is to be read from
         @param configFile Path of rhessysworkflow configuration file to use.  If none,
         will attempt to read configuration from a file named in the environment
         variable ecohydrolib.context.Context.CONFIG_FILE_ENV
         
         @raise WorkflowVersionError if a version already exists in the 
         metadata store and is different than RHESSysMetadata._workflowVersion
     """
     super(Context, self).__init__(projectDir, configFile)
     RHESSysMetadata.checkWorkflowVersion(projectDir)
Ejemplo n.º 6
0
 def __init__(self, projectDir, configFile=None):
     """ Constructor for Context class
     
         @param projectDir Path of the project whose metadata store is to be read from
         @param configFile Path of rhessysworkflow configuration file to use.  If none,
         will attempt to read configuration from a file named in the environment
         variable ecohydrolib.context.Context.CONFIG_FILE_ENV
         
         @raise WorkflowVersionError if a version already exists in the 
         metadata store and is different than RHESSysMetadata._workflowVersion
     """
     super(Context, self).__init__(projectDir, configFile)
     RHESSysMetadata.checkWorkflowVersion(projectDir)
Ejemplo n.º 7
0
    def checkMetadata(self, *args, **kwargs):
        """ Check to make sure the project directory has the necessary metadata to run this command.
        
            @note Concrete commands must call this super class method in their own
            implementation of checkMetadata(), and must call their implementation
            near the beginning of their run method.
        
        """
        super(GrassCommand, self).checkMetadata()
        self.grassMetadata = RHESSysMetadata.readGRASSEntries(self.context)

        if not 'grass_dbase' in self.metadata:
            raise MetadataException(
                "Metadata in project directory %s does not contain a GRASS Dbase"
                % (self.context.projectDir, ))
        if not 'grass_location' in self.metadata:
            raise MetadataException(
                "Metadata in project directory %s does not contain a GRASS location"
                % (self.context.projectDir, ))
        if not 'grass_mapset' in self.metadata:
            raise MetadataException(
                "Metadata in project directory %s does not contain a GRASS mapset"
                % (self.context.projectDir, ))

        self.setupGrassEnv()
Ejemplo n.º 8
0
 def checkMetadata(self, *args, **kwargs):
     """ Check to make sure the project directory has the necessary metadata to run this command.
     
         @note Concrete commands must call this super class method in their own
         implementation of checkMetadata(), and must call their implementation
         near the beginning of their run method.
     
     """
     super(GrassCommand, self).checkMetadata()
     self.grassMetadata = RHESSysMetadata.readGRASSEntries(self.context)
     
     if not 'grass_dbase' in self.metadata:
         raise MetadataException("Metadata in project directory %s does not contain a GRASS Dbase" % (self.context.projectDir,)) 
     if not 'grass_location' in self.metadata:
         raise MetadataException("Metadata in project directory %s does not contain a GRASS location" % (self.context.projectDir,)) 
     if not 'grass_mapset' in self.metadata:
         raise MetadataException("Metadata in project directory %s does not contain a GRASS mapset" % (self.context.projectDir,))
     
     self.setupGrassEnv()
parser.add_argument('-t', '--threshold', dest='threshold', required=True, type=int,
                    help='Minimum size (in cells the size of the DEM resolution) of watershed sub-basins')
parser.add_argument('-s', '--streamThreshold', dest='streamThreshold', required=False, type=float,
                    help='Threshold to pass to r.findtheriver for distinguishing stream from non-stream pixels')
parser.add_argument('-w', '--streamWindow', dest='streamWindow', required=False, type=positive_odd_integer,
                    help='Stream search window, must be a postive, odd integer')
parser.add_argument('-a', '--areaEstimate', dest='areaEstimate', required=False, type=float,
                    help='Estimated area, in sq. km, of watershed to be delineated.  A warning message will be displayed if the delineated basin area is not close to estimated area.')
parser.add_argument('--ignoreBurnedDEM', dest='ignoreBurnedDEM', action='store_true', required=False,
                    help='Ignore stream burned DEM, if present. Default DEM raster will be used for all operations. If not specified and if stream burned raster is present, stream burned DEM will be used for calculating flow direction maps.')
parser.add_argument('--multiflowdirection', dest='multiflowdirection', action='store_true', required=False,
                    help='Use multiflow direction to determine watershed boundaries.')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS Dbase" % (context.projectDir,)) 
if not 'grass_location' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS location" % (context.projectDir,)) 
if not 'grass_mapset' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS mapset" % (context.projectDir,))
    dest='ignoreBurnedDEM',
    action='store_true',
    required=False,
    help=
    'Ignore stream burned DEM, if present. Default DEM raster will be used for all operations. If not specified and if stream burned raster is present, stream burned DEM will be used for calculating flow direction maps.'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS Dbase" %
        (context.projectDir, ))
if not 'grass_location' in metadata:
    sys.exit(
    "-m",
    "--mapset",
    dest="mapset",
    required=False,
    help="Name of the new GRASS mapset where study area data are to be imported.",
)
parser.add_argument("-d", "--description", dest="description", required=True, help="Description for new location")
parser.add_argument(
    "--overwrite",
    dest="overwrite",
    action="store_true",
    required=False,
    help="Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.",
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)
paths = RHESSysPaths(args.projectDir)

if not args.grassDbase:
    dbase = "GRASSData"
else:
    dbase = args.grassDbase
grassDbase = os.path.join(context.projectDir, dbase)
location = None
if args.location:
from rhessysworkflows.metadata import RHESSysMetadata
from rhessysworkflows.rhessys import RHESSysPaths

# Handle command line options
parser = argparse.ArgumentParser(description='Generate soil texture map for dataset in GRASS GIS')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-b', '--buildPrototypeRules', dest='buildPrototypeRules', required=False, action='store_true',
                    help='Write prototype soil reclass rules to the project directory. You must edit these rules to match the classes in your custom soils data')
group.add_argument('-r', '--ruleDir', dest='ruleDir', required=False,
                    help="The directory where soils reclass rule can be found; must contain the file %s" % (str(RHESSysMetadata.SOILS_RULES),) )
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS Dbase" % (context.projectDir,)) 
if not 'grass_location' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS location" % (context.projectDir,)) 
if not 'grass_mapset' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS mapset" % (context.projectDir,))
    '--projectDir',
    dest='projectDir',
    required=True,
    help=
    'The directory to which metadata, intermediate, and final files should be saved'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS Dbase" %
        (context.projectDir, ))
if not 'grass_location' in metadata:
    sys.exit(
Ejemplo n.º 14
0
 def test_empty_read(self):
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(len(metadata) == 0)
Ejemplo n.º 15
0
    dest='defonly',
    required=False,
    action='store_true',
    help=
    'Only generate soil definition files, do not try to create soil texture map.  Map must already exist.'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

soil_rast = 'soil_texture'

# Check for necessary information in metadata
manifest = RHESSysMetadata.readManifestEntries(context)
if not args.defonly:
    if not 'soil_raster_pctsand' in manifest:
        sys.exit(
            "Metadata in project directory %s does not contain a soil_raster_pctsand raster"
parser = argparse.ArgumentParser(description='Generate landcover maps in GRASS GIS')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-k', '--generateKnownRules', dest='generateKnownRules', required=False, action='store_true',
                    help="Generate rules for known landcover type; known types include: %s" % (str(RHESSysMetadata.KNOWN_LC_TYPES), ) )
group.add_argument('-b', '--buildPrototypeRules', dest='buildPrototypeRules', required=False, action='store_true',
                    help='Write prototype landcover reclass rules to the project directory. You must edit these rules to match the classes in your own landcover data')
group.add_argument('-r', '--ruleDir', dest='ruleDir', required=False,
                    help="The directory where landcover reclass rules can be found; must contain these files %s" % (str(RHESSysMetadata.LC_RULES),) )
parser.add_argument('-l', '--includeLaiRules', dest='includeLaiRules', required=False, action='store_true',
                    help='Make LAI map')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
manifest = RHESSysMetadata.readManifestEntries(context)

paths = RHESSysPaths(args.projectDir, metadata['rhessys_dir'])

# Get path of place to store reclass rules
Ejemplo n.º 17
0
from rhessysworkflows.rhessys import readParameterFile

# Handle command line options
parser = argparse.ArgumentParser(description='Create RHESSys world template used to create initial world file')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-c', '--climateStation', dest='climateStation', required=False,
                     help='The climate station to associate with the worldfile.  Must be one of the climate stations specified in the "climate_stations" key in the "rhessys" section of the metadata')
parser.add_argument('--aspectMinSlopeOne', dest='aspectMinSlopeOne', action='store_true', required=False,
                    help='Use slope map with a minimum value of 1.0 to be used for calculating spherical average aspect.  Needed for areas of low slope due to limitations of RHESSys grass2world, which truncates slopes <1 to 0.0, which causes spherical average of aspect to equal NaN.')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                    help='Print detailed information about what the program is doing')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'dem_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a DEM raster in a GRASS mapset" % (context.projectDir,)) 
if not 'soil_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a soil raster in a GRASS mapset" % (context.projectDir,))
if not 'patch_rast' in grassMetadata:
Ejemplo n.º 18
0
    '--sourceDir',
    dest='sourceDir',
    required=True,
    help=
    'The directory from which climate data should be copied. Assumes climate base stations are stored in files ending in ".base", with climate data files of the form ".VARNAME" (e.g. ".tmin")'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing climate stations and datasets; If specified, will delete existing data before importing new data.  If not specified, new data will be added to existing data.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
paths = RHESSysPaths(args.projectDir, metadata['rhessys_dir'])

if not os.access(args.sourceDir, os.R_OK):
    sys.exit(
        "The specified path of the climate data source directory, %s, is not readable"
        % (args.sourceDir, ))
from ecohydrolib.grasslib import *

from rhessysworkflows.context import Context
from rhessysworkflows.metadata import RHESSysMetadata
from rhessysworkflows.rhessys import RHESSysPaths

# Handle command line options
parser = argparse.ArgumentParser(description='Generate soil texture map for dataset in GRASS GIS')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS Dbase" % (context.projectDir,)) 
if not 'grass_location' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS location" % (context.projectDir,)) 
if not 'grass_mapset' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS mapset" % (context.projectDir,))
    '--branch',
    dest='branch',
    required=False,
    help=
    'Use source code from the specified branch of the RHESSys source; applies only when code is cloned from Git repository (i.e. -s not specified)'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing source code in the project directory; If specified, will delete existing code before importing new code.  If not specified, new code will be added to existing code.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

gisBase = context.config.get('GRASS', 'GISBASE')
gitPath = context.config.get('RHESSYS', 'PATH_OF_GIT')
makePath = context.config.get('RHESSYS', 'PATH_OF_MAKE')

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'rhessys_dir' in metadata:
    sys.exit(
Ejemplo n.º 21
0
    help='Tell RHESSys to output at the zone spatial level')
outputType.add_argument(
    '--patch',
    dest='outputType',
    action='store_const',
    const='-p',
    help='Tell RHESSys to output at the patch spatial level')
outputType.add_argument(
    '--canopy',
    dest='outputType',
    action='store_const',
    const='-c',
    help='Tell RHESSys to output at the canopy stratum spatial level')
parser.add_argument('args', nargs=argparse.REMAINDER)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
try:
    configFile = args.configfile
except AttributeError:
    pass

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'rhessys_dir' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a RHESSys directory"
        % (context.projectDir, ))
Ejemplo n.º 22
0
    def run(self, *args, **kwargs):
        """ Create flow tables for multiple worldfiles

        Arguments:
        scenario_id -- int    ID of the GI Notebook scenario whose GI instances are to be parameterized.
        auth_token -- string    Authorization token to use for authenticating to the GI Notebook.
        host -- string    Hostname of GI Notebook server. Default: None.
        api_root -- string    The root of the API URL to use. Default: None.
        use_HTTPS -- boolean    Use HTTPS for communication with the GI Notebook.
        force -- boolean        Force overwrite of existing scenario output. Default: False.
        verbose -- boolean    Produce verbose output. Default: False.
        """
        scenario_id = kwargs.get('scenario_id')
        if scenario_id is None:
            raise RunException('Scenario ID was not specified.')
        auth_token = kwargs.get('auth_token')
        if auth_token is None:
            raise RunException('Authorization token was not specified.')
        host = kwargs.get('host', DEFAULT_HOSTNAME)
        api_root = kwargs.get('api_path', DEFAULT_API_ROOT)
        use_HTTPS = kwargs.get('use_HTTPS', False)
        force = kwargs.get('force', False)
        verbose = kwargs.get('verbose', False)

        self.checkMetadata()
        self.param_const, self.param_db = self._init_paramdb()
        self.paths = RHESSysPaths(self.context.projectDir,
                                  self.metadata['rhessys_dir'])

        gi_scenario_base = 'gi_scenario'
        gi_scenario_data = "{0}.geojson".format(gi_scenario_base)
        scenario_geojson_path = os.path.join(self.context.projectDir,
                                             gi_scenario_data)
        gi_scenario_soils_base = "{0}_wsoils".format(gi_scenario_base)
        gi_scenario_soils = "{0}.geojson".format(gi_scenario_soils_base)
        scenario_soils_geojson_path = os.path.join(self.context.projectDir,
                                                   gi_scenario_soils)
        gi_scenario_landuse_base = "{0}_landuse".format(gi_scenario_base)
        gi_scenario_landuse = "{0}.geojson".format(gi_scenario_landuse_base)
        scenario_landuse_geojson_path = os.path.join(self.context.projectDir,
                                                     gi_scenario_landuse)
        gi_scenario_data_key = 'gi_scenario_data'
        gi_scenario_soils_data_key = "{0}_soils".format(gi_scenario_data_key)
        gi_scenario_landuse_data_key = "{0}_landuse".format(
            gi_scenario_data_key)
        if gi_scenario_data_key in self.metadata:
            if verbose:
                self.outfp.write('Existing GI scenario found.\n')
            if force:
                if verbose:
                    self.outfp.write(
                        'Force option specified, overwriting existing GI scenario.\n'
                    )
                if os.path.exists(scenario_geojson_path):
                    os.unlink(scenario_geojson_path)
                if os.path.exists(scenario_soils_geojson_path):
                    os.unlink(scenario_soils_geojson_path)
                if os.path.exists(scenario_landuse_geojson_path):
                    os.unlink(scenario_landuse_geojson_path)
            else:
                raise RunException('Exiting.  Use force option to overwrite.')

        if verbose:
            output = None
        else:
            output = open('/dev/null')
        try:
            # Connect to GI Notebook and fetch GI instance information (in GeoJSON format)
            # for this scenario ID.
            if verbose:
                self.outfp.write(
                    "\nDownloading GI scenario {0} from GI database...\n".
                    format(scenario_id))
            nb = GINotebook(hostname=host,
                            api_root=api_root,
                            use_https=use_HTTPS,
                            auth_token=auth_token)
            scenario = nb.get_scenario(scenario_id)
            scenario_geojson = scenario.get_instances_as_geojson(indent=2,
                                                                 shorten=True)
            (gi_scenario_data_wgs84, scenario_geojson_wgs84_path), (gi_scenario_data, scenario_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson, gi_scenario_base, verbose=verbose, output=output)

            # Filter out instances that do not contain soils data
            gi_scenario_soils_base = "{0}_wsoils".format(gi_scenario_base)
            scenario_geojson_wsoils = scenario.get_instances_as_geojson(
                indent=2,
                shorten=True,
                filter=lambda a: a.get('e_1_pedid') is not None)
            (gi_scenario_soils_wgs84, scenario_soils_geojson_wgs84_path), (gi_scenario_soils, scenario_soils_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson_wsoils, gi_scenario_soils_base,
                                                  verbose=verbose, output=output)

            # Import scenario GeoJSON into GRASS
            self._import_vector_into_grass(scenario_geojson_path,
                                           gi_scenario_data_key,
                                           force=force,
                                           verbose=verbose,
                                           output=output)

            # Import scenario (instances with soils data) GeoJSON into GRASS
            self._import_vector_into_grass(scenario_soils_geojson_path,
                                           gi_scenario_soils_data_key,
                                           force=force,
                                           verbose=verbose,
                                           output=output)

            # Generate raster layers from vector-based GI Scenario
            # Raster for updating soil type
            self._rasterize(gi_scenario_soils_data_key,
                            gi_scenario_soils_data_key,
                            column='e_1_pedid',
                            labelcolumn='e_1_pednm',
                            rast_title='GI soil types',
                            verbose=verbose,
                            force=force,
                            redir_fp=output)
            # Raster for updating stratum type
            gi_scenario_strata = "gi_scenario_strata"
            self._rasterize(gi_scenario_data_key,
                            gi_scenario_strata,
                            column='e_1_vegid',
                            labelcolumn='e_1_vegnm',
                            rast_title='GI vegetation types',
                            verbose=verbose,
                            force=force,
                            redir_fp=output)

            # Raster for updating land use
            # Filter out instances that are not rain gardens (i.e. the only GI type for which we currently have a
            #   land use).
            scenario_geojson_landuse = scenario.get_instances_as_geojson(
                indent=2,
                shorten=True,
                filter=lambda a: a.get('type', '') == 'Rain Garden')
            (gi_scenario_landuse_wgs84, scenario_landuse_geojson_wgs84_path), \
            (gi_scenario_landuse, scenario_landuse_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson_landuse, gi_scenario_landuse_base,
                                                  verbose=verbose, output=output)
            # Import land use (i.e. instances that are rain gardens) GeoJSON into GRASS
            self._import_vector_into_grass(scenario_landuse_geojson_path,
                                           gi_scenario_landuse_data_key,
                                           force=force,
                                           verbose=verbose,
                                           output=output)

            # Search for raster value for rain gardens in RHESSys parameter DB
            rg_name = 'raingarden'
            rg_found = self.param_db.search(
                self.param_const.SEARCH_TYPE_HIERARCHICAL, 'landuse', rg_name,
                None, None, None, None, None, None, None, None)
            if not rg_found:
                raise RunException(
                    "Unable to find raingarden landuse class in parameter database"
                )

            rg_id = [c[1][2] for c in self.param_db.classes.iteritems()][0]

            # Generate raster layer from vector-based GI Scenario
            # Raster for updating landuse type
            self._rasterize_single_value(gi_scenario_landuse_data_key,
                                         gi_scenario_landuse_data_key,
                                         value=rg_id,
                                         label=rg_name,
                                         rast_title='GI landuse types',
                                         verbose=verbose,
                                         force=force,
                                         redir_fp=output)

            # Write out updated landuse, stratum, and soil rasters and parameter definitions
            # Backup landuse raster
            self._backup_raster(self.grassMetadata['landuse_rast'])
            # Update landuse raster
            self._update_raster(self.grassMetadata['landuse_rast'],
                                gi_scenario_landuse_data_key)
            # Generate parameter definition file for landuse raster
            self._generate_parameter_definitions_for_raster(
                self.grassMetadata['landuse_rast'], 'landuse', verbose=verbose)
            # Backup stratum raster
            self._backup_raster(self.grassMetadata['stratum_rast'])
            # Update stratum raster
            self._update_raster(self.grassMetadata['stratum_rast'],
                                gi_scenario_strata)
            # Generate parameter definition file for stratum raster
            self._generate_parameter_definitions_for_raster(
                self.grassMetadata['stratum_rast'], 'stratum', verbose=verbose)
            # Backup soils raster
            self._backup_raster(self.grassMetadata['soil_rast'])
            # Update soils raster
            self._update_raster(self.grassMetadata['soil_rast'],
                                gi_scenario_soils_data_key)
            # Generate parameter definition file for soil raster
            self._generate_parameter_definitions_for_raster(
                self.grassMetadata['soil_rast'], 'soil', verbose=verbose)
            # Write metadata
            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_rast".format(gi_scenario_landuse_data_key),
                gi_scenario_landuse_data_key)
            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_rast".format(gi_scenario_soils_data_key),
                gi_scenario_soils_data_key)
            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_rast".format(gi_scenario_strata),
                gi_scenario_strata)

            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_vect".format(gi_scenario_data_key),
                gi_scenario_data_key)
            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_vect".format(gi_scenario_soils_data_key),
                gi_scenario_soils_data_key)
            RHESSysMetadata.writeGRASSEntry(
                self.context, "{0}_vect".format(gi_scenario_landuse_data_key),
                gi_scenario_landuse_data_key)
            RHESSysMetadata.writeRHESSysEntry(self.context,
                                              gi_scenario_data_key,
                                              gi_scenario_data)

            if verbose:
                self.outfp.write('\n\nFinished parameterizing GI.\n')

            # Write processing history
            RHESSysMetadata.appendProcessingHistoryItem(
                self.context, RHESSysMetadata.getCommandLine())

        finally:
            if output:
                output.close()
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-t', '--type', dest='types', required=True, nargs='+', choices=typeChoices,
                    help='The type of raster dataset to import, or all if all types should be imported.')
parser.add_argument('-m', '--method', dest='method', required=True, 
                    nargs='+', choices=RESAMPLE_METHODS,
                    help='The method to use to resample each raster.')
parser.add_argument('--integer', dest='integer', required=False, action='store_true',
                    help='Transform raster to integer on import. Can only be used when importing a single raster type.')
parser.add_argument('--multiplier', dest='multiplier', required=False, type=int, default=1000,
                    help='Multiplier to use when tranforming raster values to integer')                    
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

if len(args.types) != len(args.method):
    sys.exit(textwrap.fill("%d raster type(s) specified for import, but %d resample method(s) supplied. Please specify a resample method for each raster type" % \
             (len(args.types), len(args.method)) ) )

if 'all' in args.types:
    typeList = RHESSysMetadata.RASTER_TYPES
    methodList = [args.method[0] for t in typeList]
else:
from rhessysworkflows.rhessys import RHESSysPaths

STATION_FILE_EXTENSION = 'base'

# Handle command line options
parser = argparse.ArgumentParser(description='Import RHESSys climate data into project directory')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-s', '--sourceDir', dest='sourceDir', required=True,
                    help='The directory from which climate data should be copied. Assumes climate base stations are stored in files ending in ".base", with climate data files of the form ".VARNAME" (e.g. ".tmin")')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing climate stations and datasets; If specified, will delete existing data before importing new data.  If not specified, new data will be added to existing data.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
paths = RHESSysPaths(args.projectDir, metadata['rhessys_dir'])

if not os.access(args.sourceDir, os.R_OK):
    sys.exit("The specified path of the climate data source directory, %s, is not readable" % (args.sourceDir,) )
if not os.path.isdir(args.sourceDir):
    sys.exit("The specified path of the climate data source directory, %s, is not a directory" % (args.sourceDir,) )
Ejemplo n.º 25
0
)
parser.add_argument('-b',
                    '--basestationsFile',
                    dest='basestationsFile',
                    required=True,
                    help='Text file of the form: id|easting|northing|name')
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

if not os.path.isfile(args.basestationsFile):
    sys.exit("Specified base stations file '%s' is not a file" %
             (args.basestationsFile, ))
if not os.access(args.basestationsFile, os.R_OK):
    sys.exit("Specified base stations file '%s' is not readable" %
             (args.basestationsFile, ))

# Check for necessary information in metadata
Ejemplo n.º 26
0
    def run(self, *args, **kwargs):
        """ Multiple worldfiles, one worldfile for each subbasin delineated. 
        
        Arguments:
        verbose -- boolean    Produce verbose output. Default: False.
        """
        verbose = kwargs.get('verbose', False)
        
        self.checkMetadata()
        
        rhessysDir = self.metadata['rhessys_dir']
        self.paths = RHESSysPaths(self.context.projectDir, rhessysDir)
        
        templateFilename = os.path.basename(self.metadata['template'])
        templateFilepath = os.path.join(self.context.projectDir, self.metadata['template'])
        
        g2wPath = os.path.join(self.context.projectDir, self.metadata['g2w_bin'])
        
        # Make sure g2w can find rat
        g2wEnv = dict(os.environ)
        g2wEnv['PATH'] = self.paths.RHESSYS_BIN + os.pathsep + g2wEnv['PATH']
        
        # Make sure region is properly set
        demRast = self.grassMetadata['dem_rast']
        result = self.grassLib.script.run_command('g.region', rast=demRast)
        if result != 0:
            raise RunException("g.region failed to set region to DEM, returning {0}".format(result))
        
        # Mask subbasin to basin
        basin_rast = self.grassMetadata['basin_rast']
        result = self.grassLib.script.run_command('r.mask', flags='o', input=basin_rast, maskcats='1',
                                                  quiet=True)
        if result != 0:
            sys.exit("r.mask failed to set mask to basin, returning %s" % (result,))
        subbasin_raster = self.grassMetadata['subbasins_rast']
        subbasin_mask = "{0}_mask".format(subbasin_raster)
        mapcalc_input = "{subbasin_mask}={subbasins}".format(subbasin_mask=subbasin_mask,
                                                             subbasins=subbasin_raster)
        result = self.grassLib.script.write_command('r.mapcalc',
                                                    stdin=mapcalc_input,
                                                    stdout=PIPE,
                                                    stderr=PIPE)
        if result != 0:
            raise RunException("r.mapcalc failed to generate masked subbasin map {0}, input: {1}".format(subbasin_raster,
                                                                                                         mapcalc_input))
        
        # Get list of subbasins
        result = self.grassLib.script.read_command('r.stats', flags='n', input=subbasin_raster, quiet=True)
        if result is None or result == '':
            raise RunException("Error reading subbasin map {0}".format(subbasin_raster))
             
        subbasins = result.split()
        subbasin_masks = []
        worldfiles = []
        for subbasin in subbasins:
            # Remove mask
            result = self.grassLib.script.run_command('r.mask', flags='r', quiet=True)
            if result != 0:
                raise RunException("r.mask failed to remove mask")
            
            # Make a mask layer for the sub-basin
            mask_name = "subbasin_{0}".format(subbasin)
            subbasin_masks.append(mask_name)
            result = self.grassLib.script.write_command('r.mapcalc',
                                                        stdin="{mask_name}={subbasins} == {subbasin_number}".format(mask_name=mask_name,
                                                                                                                    subbasins=subbasin_mask,
                                                                                                                    subbasin_number=subbasin),
                                                        stdout=PIPE,
                                                        stderr=PIPE)
            if result != 0:
                raise RunException("r.mapcalc failed to generate mask for subbasin {0}".format(subbasin))
        
            # Mask to the sub-basin
            result = self.grassLib.script.run_command('r.mask', flags='o', input=mask_name, maskcats='1',
                                                      quiet=True)
            if result != 0:
                raise RunException("r.mask failed to set mask to sub-basin {0}, returning {1}".format(mask_name,
                                                                                                      result))
         
            worldfileName = "world_subbasin_{0}_init".format(subbasin)
            worldfilePath = os.path.join(self.paths.RHESSYS_WORLD, worldfileName)
            worldfiles.append(worldfilePath)
            g2wCommand = "{g2w} -t {template} -w {worldfile}".format(g2w=g2wPath, 
                                                                     template=templateFilepath, 
                                                                     worldfile=worldfilePath)
            
            if verbose:
                self.outfp.write("{0}\n".format(g2wCommand))
                self.outfp.write("\nRunning grass2world from {0}...".format(self.paths.RHESSYS_BIN))
                self.outfp.flush()

            cmdArgs = g2wCommand.split()
            process = Popen(cmdArgs, cwd=self.paths.RHESSYS_BIN, env=g2wEnv, 
                            stdout=PIPE, stderr=PIPE)
            (process_stdout, process_stderr) = process.communicate()
            if process.returncode != 0:
                raise RunException("grass2world failed, returning {0}".format(process.returncode))
     
            if verbose:
                self.outfp.write(process_stdout)
                self.outfp.write(process_stderr)
         
        # Remove mask
        result = self.grassLib.script.run_command('r.mask', flags='r', quiet=True)
        if result != 0:
            raise RunException("r.mask failed to remove mask") 
         
        # Write metadata
        RHESSysMetadata.writeRHESSysEntry(self.context, 'worldfiles_init', 
                                          RHESSysMetadata.VALUE_DELIM.join([self.paths.relpath(w) for w in worldfiles]))
        RHESSysMetadata.writeRHESSysEntry(self.context, 'subbasin_masks', 
                                          RHESSysMetadata.VALUE_DELIM.join([m for m in subbasin_masks]))

        if verbose:
            self.outfp.write('\n\nFinished creating worldfiles\n')

        # Write processing history
        RHESSysMetadata.appendProcessingHistoryItem(self.context, RHESSysMetadata.getCommandLine())
    '--multiplier',
    dest='multiplier',
    required=False,
    type=int,
    default=1000,
    help='Multiplier to use when tranforming raster values to integer')
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

if len(args.types) != len(args.method):
    sys.exit(textwrap.fill("%d raster type(s) specified for import, but %d resample method(s) supplied. Please specify a resample method for each raster type" % \
             (len(args.types), len(args.method)) ) )

if 'all' in args.types:
    typeList = RHESSysMetadata.RASTER_TYPES
    methodList = [args.method[0] for t in typeList]
else:
Ejemplo n.º 28
0
parser.add_argument(
    '--ignoreBurnedDEM',
    dest='ignoreBurnedDEM',
    action='store_true',
    required=False,
    help=
    'Ignore stream burned DEM, if present. Default DEM raster will be used for all operations. If not specified and if stream burned raster is present, stream burned DEM will be used for generating the flow table.'
)
parser.add_argument(
    '-v',
    '--verbose',
    dest='verbose',
    action='store_true',
    help='Print detailed information about what the program is doing')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
metadata = RHESSysMetadata.readRHESSysEntries(context)

if not 'dem_res_x' in studyArea:
    sys.exit(
        "Metadata in project directory %s does not contain a DEM x resolution"
Ejemplo n.º 29
0
                    help='Names of maps to be created.')
parser.add_argument('--mapcolorstyle', required=False, default='grey1.0',
                    help='Color map style to pass to r.colors, used for zonal stats map.')
args = parser.parse_args()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

if not len(args.rhessysOutFile) == len(args.mapNames):
    sys.exit("Number of RHESSys output files must match the number of map names")

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS Dbase" % (context.projectDir,)) 
if not 'grass_location' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS location" % (context.projectDir,)) 
if not 'grass_mapset' in metadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS mapset" % (context.projectDir,))

patchFilepaths = []
for outfile in args.rhessysOutFile:
    if not os.path.isfile(outfile) or not os.access(outfile, os.R_OK):
        sys.exit("Unable to read RHESSys output file %s" % (outfile,))
    patchFilepaths.append( os.path.abspath(outfile) ) 

# Determine output variables
variables = ['patchID']
Ejemplo n.º 30
0
 def test_write_and_read(self):
     RHESSysMetadata.writeRHESSysEntry(self.context, "key1", "value_one")
     RHESSysMetadata.writeRHESSysEntry(self.context, "key2", "value_two")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(metadata["key1"] == "value_one")
Ejemplo n.º 31
0
from rhessysworkflows.rhessys import datetimeToString
from rhessysworkflows.rhessys import generateCommandString
from rhessysworkflows.worldfileio import getClimateBaseStationFilenames
from rhessysworkflows.climateio import getStartAndEndDateForClimateStation

# Handle command line options
parser = argparse.ArgumentParser(description='Run lairead utility to initializes vegetation carbon stores. Will: (1) run lairead to ' +
                                             'produce a redefine worldfile; (2) run RHESSys simulation for 3-days to generate base worldfile')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                    help='Print detailed information about what the program is doing')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'basin_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS dataset with a basin raster" % (context.projectDir,))
if not 'dem_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS dataset with a DEM raster" % (context.projectDir,))
if not 'hillslope_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS dataset with a hillslope raster" % (context.projectDir,))  
Ejemplo n.º 32
0
parser = argparse.ArgumentParser(description='Generate patch maps or in GRASS location associated with the project directory.')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-t', '--patchType', dest='patchType', required=True, choices=['grid', 'clump'],
                    help='Type of patch to be generated: uniform grid or clumps based on elevation')
parser.add_argument('-c', '--clumpMap', dest='clumpMap', required=False, default='elevation', choices=['elevation', 'wetness_index'],
                    help='Type of patch to be generated: uniform grid or clumps based on elevation')
parser.add_argument('-z', '--forceZone', dest='forceZone', required=False, action='store_true',
                    help='Use patch map as zone map even if zone map is already defined. ' +
                    ' By default if a zone map is present, this script will not set the patch map as the zone map.')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
if not 'dem_rows' in studyArea:
    sys.exit("Metadata in project directory %s does not contain DEM rows entry" % (context.projectDir,))
    
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'basin_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS dataset with a basin raster" % (context.projectDir,))
Ejemplo n.º 33
0
 def run(self, *args, **kwargs):
     """ Run lairead for multiple worldfiles 
     
     Arguments:
     topmodel -- boolean   Whether to run RHESSys in TOPMODEL model. Default: False.
     verbose -- boolean    Produce verbose output. Default: False.
     """
     verbose = kwargs.get('verbose', False)
     topmodel = kwargs.get('topmodel', False)
     
     self.checkMetadata(topmodel=topmodel)
     
     rhessysDir = self.metadata['rhessys_dir']
     self.paths = RHESSysPaths(self.context.projectDir, rhessysDir)
     rhessysBinPath = os.path.join(self.context.projectDir, self.metadata['rhessys_bin'])
     
     # Make sure region is properly set
     demRast = self.grassMetadata['dem_rast']
     result = self.grassLib.script.run_command('g.region', rast=demRast)
     if result != 0:
         raise RunException("g.region failed to set region to DEM, returning {0}".format(result))
     
     # Run lairead for each worldfile
     lairead_tecfiles = []
     final_worldfiles = []
     worldfiles = self.metadata['worldfiles_init'].split(RHESSysMetadata.VALUE_DELIM)
     masks = self.metadata['subbasin_masks'].split(RHESSysMetadata.VALUE_DELIM)
     
     surfaceFlowtables = subsurfaceFlowtables = None
     if not topmodel:
         surfaceFlowtables = self.metadata['surface_flowtables'].split(RHESSysMetadata.VALUE_DELIM)
         subsurfaceFlowtables = self.metadata['subsurface_flowtables'].split(RHESSysMetadata.VALUE_DELIM)
     
     for (i, worldfile) in enumerate(worldfiles):
         worldfilePath = os.path.join(self.context.projectDir, worldfile)
         worldfileDir = os.path.dirname(worldfilePath)
         
         surfaceFlowtable = subsurfaceFlowtable = None
         if not topmodel:
             surfaceFlowtable = surfaceFlowtables[i] # Assumption: worldfiles and flowtables lists are in the same order
             subsurfaceFlowtable = subsurfaceFlowtables[i] # Assumption: worldfiles and flowtables lists are in the same order
             
         # Mask to correct mask
         mask = masks[i] # Assumption: worldfiles and masks lists are in the same order
         result = self.grassLib.script.run_command('r.mask', flags='o', input=mask, maskcats='1',
                                                   quiet=True)
         if result != 0:
             raise RunException("r.mask failed to set mask to sub-basin {0}, returning {1}".format(mask,
                                                                                                   result))
         ## 1. Determine legal simulation start and date from climate data 
         # Read first climate station from worldfile
         header = "{0}.hdr".format(worldfile)
         headerPath = os.path.join(self.context.projectDir, header)
         stations = getClimateBaseStationFilenames(headerPath)
         if not len(stations) > 0:
             raise RunException("No climate stations found in worldfile header {0}".format(headerPath))
         firstStationPath = os.path.normpath( os.path.join(self.paths.RHESSYS_DIR, stations[0]) )
         if verbose:
             self.outfp.write("First climate station in worldfile: %s\n" % (firstStationPath,) )
         
         # Read climate timeseries for start and end date, write to metadata
         (startDate, endDate) = getStartAndEndDateForClimateStation(firstStationPath, self.paths)
         if verbose:
             self.outfp.write("start date: %s, end date: %s\n" % ( str(startDate), str(endDate) ) )
         fourDays = datetime.timedelta(days=4)
         if endDate - startDate < fourDays:
             raise RunException("Climate time-series defined by station %s is too short to run lairead (less than four-days long)" %
                                (firstStationPath,) )
         
         ## 2. Run LAI read to generate redefine worldfile
         tecDurRedef = datetime.timedelta(days=1)
         tecRedef = startDate + tecDurRedef
         laireadPath = os.path.join(self.context.projectDir, self.metadata['lairead_bin'])
         oldWorldPath = os.path.join(self.context.projectDir, worldfile)
         redefWorldName = "%s.Y%dM%dD%dH%d" % \
             (worldfile, tecRedef.year, tecRedef.month, tecRedef.day, tecRedef.hour)
         redefWorldPath = os.path.join(self.context.projectDir, redefWorldName)
         allomPath = os.path.join(self.context.projectDir, self.metadata['allometric_table'])
         
         if verbose:
             self.outfp.write("\nRunning lairead for subbasin {0}...".format(mask))
             
         p = self.grassLib.script.pipe_command(laireadPath, old=oldWorldPath, redef=redefWorldPath,
                                               allom=allomPath, lai=self.grassMetadata['lai_rast'],
                                               vegid=self.grassMetadata['stratum_rast'],
                                               zone=self.grassMetadata['zone_rast'],
                                               hill=self.grassMetadata['hillslope_rast'],
                                               patch=self.grassMetadata['patch_rast'],
                                               mask=mask)
         (stdoutStr, stderrStr) = p.communicate() 
         result = p.returncode
         if result != 0:
             self.outfp.write(stdoutStr)
             raise RunException("\nlairead failed, returning %s" % (result,))
         
         if verbose:
             self.outfp.write('done\n')
                     
         ## 3. Write TEC file for redefining the initial flow table
         ##    Redefine on the second day of the simulation, write output
         ##    on the third day
         tecName = "tec.lairead_{0}".format(mask)
         tecPath = os.path.join(self.paths.RHESSYS_TEC, tecName)
         tecDurOutput = datetime.timedelta(days=2)
         tecOutput = startDate + tecDurOutput
         
         f = open(tecPath, 'w')
         f.write("%s redefine_world%s" % 
                 (datetimeToString(tecRedef), os.linesep) )
         f.write("%s output_current_state%s" %
                 (datetimeToString(tecOutput), os.linesep) )
         f.close()
         lairead_tecfiles.append(tecPath)
         
         ## 4. Run RHESSys for the first 4 legal days with redefine TEC
         rhessysStart = startDate
         rhessysDur = datetime.timedelta(days=3)
         rhessysEnd = startDate + rhessysDur
         surfaceFlowtablePath = subSurfaceFlowtablePath = None
         if not topmodel:
             surfaceFlowtablePath = os.path.join(self.context.projectDir, surfaceFlowtable)
             subsurfaceFlowtablePath = os.path.join(self.context.projectDir, subsurfaceFlowtable)
         
         rhessysCmd = generateCommandString(rhessysBinPath, None,
                                            rhessysStart, rhessysEnd,
                                            tecPath, oldWorldPath,
                                            surfaceFlowtablePath, subsurfaceFlowtablePath)
         if verbose:
             self.outfp.write('\nRunning RHESSys to redefine worldfile with vegetation carbon stores...\n')
             self.outfp.write(rhessysCmd)
             self.outfp.write('\n')
         
         cmdArgs = rhessysCmd.split()
         process = Popen(cmdArgs, cwd=self.paths.RHESSYS_DIR, stdout=PIPE, stderr=PIPE)
         (process_stdout, process_stderr) = process.communicate()
         if verbose:
             self.outfp.write(process_stdout)
             self.outfp.write(process_stderr)
         if process.returncode != 0:
             raise RunException("\n\nRHESSys failed, returning %s" % (process.returncode,) )
         
         if verbose:    
             sys.stdout.write('done\n')
             
         ## 5. Rename redefine worldfile, write to metadata
         outputWorldName = "%s.Y%dM%dD%dH%d.state" % \
             (worldfile, tecOutput.year, tecOutput.month, tecOutput.day, tecOutput.hour)
         outputWorldPath = os.path.join(self.context.projectDir, outputWorldName)
         
         if not os.access(outputWorldPath, os.W_OK):
             raise RunException("Unable to find redefined worldfile %s" % (outputWorldPath,) )
             
         newWorldName = "world_{0}".format(mask)
         newWorldPath = os.path.join(self.paths.RHESSYS_WORLD, newWorldName)
         
         shutil.move(outputWorldPath, newWorldPath)
         if not os.path.exists(newWorldPath):
             raise RunException("Failed to copy redefined worldfile %s to %s" % (outputWorldPath, newWorldPath) )
         
         final_worldfiles.append(newWorldPath)
         
         # Copy world file header from init worldfile to final world file
         newHeader = "%s.hdr" % (newWorldName,)
         newHeaderPath = os.path.join(self.paths.RHESSYS_WORLD, newHeader)
         shutil.copyfile(headerPath, newHeaderPath)
         
     if verbose:    
         sys.stdout.write('\n\nSuccessfully used lairead to initialize vegetation carbon stores.\n')
     
     # Write metadata    
     RHESSysMetadata.writeRHESSysEntry(self.context, 'worldfiles',
                                       RHESSysMetadata.VALUE_DELIM.join([self.paths.relpath(w) for w in final_worldfiles]))
     RHESSysMetadata.writeRHESSysEntry(self.context, 'lairead_tecfiles', 
                                       RHESSysMetadata.VALUE_DELIM.join([self.paths.relpath(t) for t in lairead_tecfiles]))
     RHESSysMetadata.writeRHESSysEntry(self.context, 'lairead_mode_topmodel', str(topmodel))
         
     # Write processing history
     RHESSysMetadata.appendProcessingHistoryItem(self.context, RHESSysMetadata.getCommandLine())
Ejemplo n.º 34
0
parser.add_argument(
    '-p',
    '--projectDir',
    dest='projectDir',
    required=True,
    help=
    'The directory to which metadata, intermediate, and final files should be saved'
)
parser.add_argument(
    '-v',
    '--verbose',
    dest='verbose',
    action='store_true',
    help='Print detailed information about what the program is doing')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'basin_rast' in grassMetadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS dataset with a basin raster"
        % (context.projectDir, ))
if not 'dem_rast' in grassMetadata:
    sys.exit(
Ejemplo n.º 35
0
    dest='buildPrototypeRules',
    required=False,
    action='store_true',
    help=
    'Write prototype soil reclass rules to the project directory. You must edit these rules to match the classes in your custom soils data'
)
group.add_argument(
    '-r',
    '--ruleDir',
    dest='ruleDir',
    required=False,
    help=
    "The directory where soils reclass rule can be found; must contain the file %s"
    % (str(RHESSysMetadata.SOILS_RULES), ))
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS Dbase" %
        (context.projectDir, ))
if not 'grass_location' in metadata:
    sys.exit(
from rhessysworkflows.context import Context
from rhessysworkflows.metadata import RHESSysMetadata
from rhessysworkflows.rhessys import RHESSysPaths

# Handle command line options
parser = argparse.ArgumentParser(description='Generate soil texture map for dataset in GRASS GIS')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('--defonly', dest='defonly', required=False, action='store_true',
                    help='Only generate soil definition files, do not try to create soil texture map.  Map must already exist.')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

soil_rast = 'soil_texture'

# Check for necessary information in metadata
manifest = RHESSysMetadata.readManifestEntries(context)
if not args.defonly:
    if not 'soil_raster_pctsand' in manifest:
        sys.exit("Metadata in project directory %s does not contain a soil_raster_pctsand raster" % (context.projectDir,))
    if not 'soil_raster_pctclay' in manifest:
Ejemplo n.º 37
0
    def run(self, *args, **kwargs):
        """ Create flow tables for multiple worldfiles

        Arguments:
        scenario_id -- int    ID of the GI Notebook scenario whose GI instances are to be parameterized.
        auth_token -- string    Authorization token to use for authenticating to the GI Notebook.
        host -- string    Hostname of GI Notebook server. Default: None.
        api_root -- string    The root of the API URL to use. Default: None.
        use_HTTPS -- boolean    Use HTTPS for communication with the GI Notebook.
        force -- boolean        Force overwrite of existing scenario output. Default: False.
        verbose -- boolean    Produce verbose output. Default: False.
        """
        scenario_id = kwargs.get('scenario_id')
        if scenario_id is None:
            raise RunException('Scenario ID was not specified.')
        auth_token = kwargs.get('auth_token')
        if auth_token is None:
            raise RunException('Authorization token was not specified.')
        host = kwargs.get('host', DEFAULT_HOSTNAME)
        api_root = kwargs.get('api_path', DEFAULT_API_ROOT)
        use_HTTPS = kwargs.get('use_HTTPS', False)
        force = kwargs.get('force', False)
        verbose = kwargs.get('verbose', False)

        self.checkMetadata()
        self.param_const, self.param_db = self._init_paramdb()
        self.paths = RHESSysPaths(self.context.projectDir, self.metadata['rhessys_dir'])

        gi_scenario_base = 'gi_scenario'
        gi_scenario_data = "{0}.geojson".format(gi_scenario_base)
        scenario_geojson_path = os.path.join(self.context.projectDir, gi_scenario_data)
        gi_scenario_soils_base = "{0}_wsoils".format(gi_scenario_base)
        gi_scenario_soils = "{0}.geojson".format(gi_scenario_soils_base)
        scenario_soils_geojson_path = os.path.join(self.context.projectDir, gi_scenario_soils)
        gi_scenario_landuse_base = "{0}_landuse".format(gi_scenario_base)
        gi_scenario_landuse = "{0}.geojson".format(gi_scenario_landuse_base)
        scenario_landuse_geojson_path = os.path.join(self.context.projectDir, gi_scenario_landuse)
        gi_scenario_data_key = 'gi_scenario_data'
        gi_scenario_soils_data_key = "{0}_soils".format(gi_scenario_data_key)
        gi_scenario_landuse_data_key = "{0}_landuse".format(gi_scenario_data_key)
        if gi_scenario_data_key in self.metadata:
            if verbose:
                self.outfp.write('Existing GI scenario found.\n')
            if force:
                if verbose:
                    self.outfp.write('Force option specified, overwriting existing GI scenario.\n')
                if os.path.exists(scenario_geojson_path):
                    os.unlink(scenario_geojson_path)
                if os.path.exists(scenario_soils_geojson_path):
                    os.unlink(scenario_soils_geojson_path)
                if os.path.exists(scenario_landuse_geojson_path):
                    os.unlink(scenario_landuse_geojson_path)
            else:
                raise RunException('Exiting.  Use force option to overwrite.')

        if verbose:
            output = None
        else:
            output = open('/dev/null')
        try:
            # Connect to GI Notebook and fetch GI instance information (in GeoJSON format)
            # for this scenario ID.
            if verbose:
                self.outfp.write("\nDownloading GI scenario {0} from GI database...\n".format(scenario_id))
            nb = GINotebook(hostname=host,
                            api_root=api_root,
                            use_https=use_HTTPS, auth_token=auth_token)
            scenario = nb.get_scenario(scenario_id)
            scenario_geojson = scenario.get_instances_as_geojson(indent=2, shorten=True)
            (gi_scenario_data_wgs84, scenario_geojson_wgs84_path), (gi_scenario_data, scenario_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson, gi_scenario_base, verbose=verbose, output=output)

            # Filter out instances that do not contain soils data
            gi_scenario_soils_base = "{0}_wsoils".format(gi_scenario_base)
            scenario_geojson_wsoils = scenario.get_instances_as_geojson(indent=2, shorten=True,
                                                                        filter=lambda a: a.get('e_1_pedid') is not None)
            (gi_scenario_soils_wgs84, scenario_soils_geojson_wgs84_path), (gi_scenario_soils, scenario_soils_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson_wsoils, gi_scenario_soils_base,
                                                  verbose=verbose, output=output)

            # Import scenario GeoJSON into GRASS
            self._import_vector_into_grass(scenario_geojson_path, gi_scenario_data_key,
                                           force=force, verbose=verbose, output=output)

            # Import scenario (instances with soils data) GeoJSON into GRASS
            self._import_vector_into_grass(scenario_soils_geojson_path, gi_scenario_soils_data_key,
                                           force=force, verbose=verbose, output=output)

            # Generate raster layers from vector-based GI Scenario
            # Raster for updating soil type
            self._rasterize(gi_scenario_soils_data_key, gi_scenario_soils_data_key,
                            column='e_1_pedid', labelcolumn='e_1_pednm',
                            rast_title='GI soil types',
                            verbose=verbose,
                            force=force,
                            redir_fp=output)
            # Raster for updating stratum type
            gi_scenario_strata = "gi_scenario_strata"
            self._rasterize(gi_scenario_data_key, gi_scenario_strata,
                            column='e_1_vegid', labelcolumn='e_1_vegnm',
                            rast_title='GI vegetation types',
                            verbose=verbose,
                            force=force,
                            redir_fp=output)

            # Raster for updating land use
            # Filter out instances that are not rain gardens (i.e. the only GI type for which we currently have a
            #   land use).
            scenario_geojson_landuse = scenario.get_instances_as_geojson(indent=2, shorten=True,
                                                                         filter=lambda a: a.get('type',
                                                                                                '') == 'Rain Garden')
            (gi_scenario_landuse_wgs84, scenario_landuse_geojson_wgs84_path), \
            (gi_scenario_landuse, scenario_landuse_geojson_path) = \
                self._write_geojson_and_reproject(scenario_geojson_landuse, gi_scenario_landuse_base,
                                                  verbose=verbose, output=output)
            # Import land use (i.e. instances that are rain gardens) GeoJSON into GRASS
            self._import_vector_into_grass(scenario_landuse_geojson_path, gi_scenario_landuse_data_key,
                                           force=force, verbose=verbose, output=output)

            # Search for raster value for rain gardens in RHESSys parameter DB
            rg_name = 'raingarden'
            rg_found = self.param_db.search(self.param_const.SEARCH_TYPE_HIERARCHICAL, 'landuse', rg_name,
                                            None, None, None, None, None, None, None, None)
            if not rg_found:
                raise RunException("Unable to find raingarden landuse class in parameter database")

            rg_id = [c[1][2] for c in self.param_db.classes.iteritems()][0]

            # Generate raster layer from vector-based GI Scenario
            # Raster for updating landuse type
            self._rasterize_single_value(gi_scenario_landuse_data_key, gi_scenario_landuse_data_key,
                                         value=rg_id, label=rg_name,
                                         rast_title='GI landuse types',
                                         verbose=verbose,
                                         force=force,
                                         redir_fp=output)

            # Write out updated landuse, stratum, and soil rasters and parameter definitions
            # Backup landuse raster
            self._backup_raster(self.grassMetadata['landuse_rast'])
            # Update landuse raster
            self._update_raster(self.grassMetadata['landuse_rast'], gi_scenario_landuse_data_key)
            # Generate parameter definition file for landuse raster
            self._generate_parameter_definitions_for_raster(self.grassMetadata['landuse_rast'], 'landuse',
                                                            verbose=verbose)
            # Backup stratum raster
            self._backup_raster(self.grassMetadata['stratum_rast'])
            # Update stratum raster
            self._update_raster(self.grassMetadata['stratum_rast'], gi_scenario_strata)
            # Generate parameter definition file for stratum raster
            self._generate_parameter_definitions_for_raster(self.grassMetadata['stratum_rast'], 'stratum',
                                                            verbose=verbose)
            # Backup soils raster
            self._backup_raster(self.grassMetadata['soil_rast'])
            # Update soils raster
            self._update_raster(self.grassMetadata['soil_rast'], gi_scenario_soils_data_key)
            # Generate parameter definition file for soil raster
            self._generate_parameter_definitions_for_raster(self.grassMetadata['soil_rast'], 'soil',
                                                            verbose=verbose)
            # Write metadata
            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_rast".format(gi_scenario_landuse_data_key),
                                            gi_scenario_landuse_data_key)
            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_rast".format(gi_scenario_soils_data_key),
                                            gi_scenario_soils_data_key)
            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_rast".format(gi_scenario_strata), gi_scenario_strata)

            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_vect".format(gi_scenario_data_key), gi_scenario_data_key)
            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_vect".format(gi_scenario_soils_data_key),
                                            gi_scenario_soils_data_key)
            RHESSysMetadata.writeGRASSEntry(self.context, "{0}_vect".format(gi_scenario_landuse_data_key),
                                            gi_scenario_landuse_data_key)
            RHESSysMetadata.writeRHESSysEntry(self.context, gi_scenario_data_key, gi_scenario_data)

            if verbose:
                self.outfp.write('\n\nFinished parameterizing GI.\n')

            # Write processing history
            RHESSysMetadata.appendProcessingHistoryItem(self.context, RHESSysMetadata.getCommandLine())

        finally:
            if output:
                output.close()
from rhessysworkflows.context import Context
from rhessysworkflows.metadata import RHESSysMetadata
from rhessysworkflows.rhessys import RHESSysPaths

# Handle command line options
parser = argparse.ArgumentParser(description='Generate climate base station raster map in GRASS for a list of base stations using Thiessen polygons.')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-b', '--basestationsFile', dest='basestationsFile', required=True,
                    help='Text file of the form: id|easting|northing|name')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

if not os.path.isfile(args.basestationsFile):
    sys.exit("Specified base stations file '%s' is not a file" % (args.basestationsFile,) )
if not os.access(args.basestationsFile, os.R_OK):
    sys.exit("Specified base stations file '%s' is not readable" % (args.basestationsFile,) )

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
Ejemplo n.º 39
0
    def run(self, *args, **kwargs):
        """ Multiple worldfiles, one worldfile for each subbasin delineated. 
        
        Arguments:
        verbose -- boolean    Produce verbose output. Default: False.
        """
        verbose = kwargs.get('verbose', False)

        self.checkMetadata()

        rhessysDir = self.metadata['rhessys_dir']
        self.paths = RHESSysPaths(self.context.projectDir, rhessysDir)

        templateFilename = os.path.basename(self.metadata['template'])
        templateFilepath = os.path.join(self.context.projectDir,
                                        self.metadata['template'])

        g2wPath = os.path.join(self.context.projectDir,
                               self.metadata['g2w_bin'])

        # Make sure g2w can find rat
        g2wEnv = dict(os.environ)
        g2wEnv['PATH'] = self.paths.RHESSYS_BIN + os.pathsep + g2wEnv['PATH']

        # Make sure region is properly set
        demRast = self.grassMetadata['dem_rast']
        result = self.grassLib.script.run_command('g.region', rast=demRast)
        if result != 0:
            raise RunException(
                "g.region failed to set region to DEM, returning {0}".format(
                    result))

        # Mask subbasin to basin
        basin_rast = self.grassMetadata['basin_rast']
        result = self.grassLib.script.run_command('r.mask',
                                                  flags='o',
                                                  input=basin_rast,
                                                  maskcats='1',
                                                  quiet=True)
        if result != 0:
            sys.exit("r.mask failed to set mask to basin, returning %s" %
                     (result, ))
        subbasin_raster = self.grassMetadata['subbasins_rast']
        subbasin_mask = "{0}_mask".format(subbasin_raster)
        mapcalc_input = "{subbasin_mask}={subbasins}".format(
            subbasin_mask=subbasin_mask, subbasins=subbasin_raster)
        result = self.grassLib.script.write_command('r.mapcalc',
                                                    stdin=mapcalc_input,
                                                    stdout=PIPE,
                                                    stderr=PIPE)
        if result != 0:
            raise RunException(
                "r.mapcalc failed to generate masked subbasin map {0}, input: {1}"
                .format(subbasin_raster, mapcalc_input))

        # Get list of subbasins
        result = self.grassLib.script.read_command('r.stats',
                                                   flags='n',
                                                   input=subbasin_raster,
                                                   quiet=True)
        if result is None or result == '':
            raise RunException(
                "Error reading subbasin map {0}".format(subbasin_raster))

        subbasins = result.split()
        subbasin_masks = []
        worldfiles = []
        for subbasin in subbasins:
            # Remove mask
            result = self.grassLib.script.run_command('r.mask',
                                                      flags='r',
                                                      quiet=True)
            if result != 0:
                raise RunException("r.mask failed to remove mask")

            # Make a mask layer for the sub-basin
            mask_name = "subbasin_{0}".format(subbasin)
            subbasin_masks.append(mask_name)
            result = self.grassLib.script.write_command(
                'r.mapcalc',
                stdin="{mask_name}={subbasins} == {subbasin_number}".format(
                    mask_name=mask_name,
                    subbasins=subbasin_mask,
                    subbasin_number=subbasin),
                stdout=PIPE,
                stderr=PIPE)
            if result != 0:
                raise RunException(
                    "r.mapcalc failed to generate mask for subbasin {0}".
                    format(subbasin))

            # Mask to the sub-basin
            result = self.grassLib.script.run_command('r.mask',
                                                      flags='o',
                                                      input=mask_name,
                                                      maskcats='1',
                                                      quiet=True)
            if result != 0:
                raise RunException(
                    "r.mask failed to set mask to sub-basin {0}, returning {1}"
                    .format(mask_name, result))

            worldfileName = "world_subbasin_{0}_init".format(subbasin)
            worldfilePath = os.path.join(self.paths.RHESSYS_WORLD,
                                         worldfileName)
            worldfiles.append(worldfilePath)
            g2wCommand = "{g2w} -t {template} -w {worldfile}".format(
                g2w=g2wPath,
                template=templateFilepath,
                worldfile=worldfilePath)

            if verbose:
                self.outfp.write("{0}\n".format(g2wCommand))
                self.outfp.write("\nRunning grass2world from {0}...".format(
                    self.paths.RHESSYS_BIN))
                self.outfp.flush()

            cmdArgs = g2wCommand.split()
            process = Popen(cmdArgs,
                            cwd=self.paths.RHESSYS_BIN,
                            env=g2wEnv,
                            stdout=PIPE,
                            stderr=PIPE)
            (process_stdout, process_stderr) = process.communicate()
            if process.returncode != 0:
                raise RunException("grass2world failed, returning {0}".format(
                    process.returncode))

            if verbose:
                self.outfp.write(process_stdout)
                self.outfp.write(process_stderr)

        # Remove mask
        result = self.grassLib.script.run_command('r.mask',
                                                  flags='r',
                                                  quiet=True)
        if result != 0:
            raise RunException("r.mask failed to remove mask")

        # Write metadata
        RHESSysMetadata.writeRHESSysEntry(
            self.context, 'worldfiles_init',
            RHESSysMetadata.VALUE_DELIM.join(
                [self.paths.relpath(w) for w in worldfiles]))
        RHESSysMetadata.writeRHESSysEntry(
            self.context, 'subbasin_masks',
            RHESSysMetadata.VALUE_DELIM.join([m for m in subbasin_masks]))

        if verbose:
            self.outfp.write('\n\nFinished creating worldfiles\n')

        # Write processing history
        RHESSysMetadata.appendProcessingHistoryItem(
            self.context, RHESSysMetadata.getCommandLine())
Ejemplo n.º 40
0
                         'If one flow table is supplied, it will be used for subsurface and surface routing.  ' +
                         'If two flow tables are supplied the first will be use for subsurface routing, the second for surface.')
outputType = parser.add_mutually_exclusive_group(required=True)
outputType.add_argument('--basin', dest='outputType', action='store_const', const='-b',
                        help='Tell RHESSys to output at the basin spatial level')
outputType.add_argument('--hillslope', dest='outputType', action='store_const', const='-h',
                        help='Tell RHESSys to output at the hillslope spatial level')
outputType.add_argument('--zone', dest='outputType', action='store_const', const='-z',
                        help='Tell RHESSys to output at the zone spatial level')
outputType.add_argument('--patch', dest='outputType', action='store_const', const='-p',
                        help='Tell RHESSys to output at the patch spatial level')
outputType.add_argument('--canopy', dest='outputType', action='store_const', const='-c',
                        help='Tell RHESSys to output at the canopy stratum spatial level')
parser.add_argument('args', nargs=argparse.REMAINDER)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
try:
    configFile = args.configfile
except AttributeError:
    pass

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'rhessys_dir' in metadata:
    sys.exit("Metadata in project directory %s does not contain a RHESSys directory" % (context.projectDir,))
if not 'rhessys_bin' in metadata:
    sys.exit("Metadata in project directory %s does not contain a RHESSys binary" % (context.projectDir,))
Ejemplo n.º 41
0
 def test_empty_read(self):
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(len(metadata) == 0)
    'Name of the new GRASS mapset where study area data are to be imported.')
parser.add_argument('-d',
                    '--description',
                    dest='description',
                    required=True,
                    help='Description for new location')
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)
paths = RHESSysPaths(args.projectDir)

if not args.grassDbase:
    dbase = 'GRASSData'
else:
    dbase = args.grassDbase
grassDbase = os.path.join(context.projectDir, dbase)
location = None
if args.location:
Ejemplo n.º 43
0
parser = argparse.ArgumentParser(description='Import RHESSys source code into project directory')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file.')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-s', '--sourceDir', dest='sourceDir', required=False,
                    help='The directory from which RHESys source should be copied. NOTE: will delete any sources already in the project directory')
group = parser.add_mutually_exclusive_group()
group.add_argument('-t', '--tag', dest='tag', required=False,
                    help='Use source code from the specified tagged version of RHESSys; applies only when code is cloned from Git repository (i.e. -s not specified)')
group.add_argument('-b', '--branch', dest='branch', required=False,
                    help='Use source code from the specified branch of the RHESSys source; applies only when code is cloned from Git repository (i.e. -s not specified)')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing source code in the project directory; If specified, will delete existing code before importing new code.  If not specified, new code will be added to existing code.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

gisBase = context.config.get('GRASS', 'GISBASE')
gitPath = context.config.get('RHESSYS', 'PATH_OF_GIT')
makePath = context.config.get('RHESSYS', 'PATH_OF_MAKE')

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'rhessys_dir' in metadata:
    sys.exit("Metadata in project directory %s does not contain a RHESSys directory" % (context.projectDir,))
Ejemplo n.º 44
0
 def test_write_and_read(self):
     RHESSysMetadata.writeRHESSysEntry(self.context, "key1", "value_one")
     RHESSysMetadata.writeRHESSysEntry(self.context, "key2", "value_two")
     metadata = RHESSysMetadata.readRHESSysEntries(self.context)
     self.assertTrue(metadata["key1"] == "value_one")
Ejemplo n.º 45
0
    def run(self, *args, **kwargs):
        """ Create flow tables for multiple worldfiles 
        
        Arguments:
        routeRoads -- boolean    Whether road routing should be enabled in createflowpaths. Default: False.
        routeRoofs -- boolean    Whether roof routing should be enabled in createflowpaths. Default: False.
        ignoreBurnedDEM -- boolean    If true, use the base DEM when running createflowpaths. 
                                      If false, use the stream-burned DEM (if present).  Default: False.
        force -- boolean        Whether to force createflowpaths to run if DEM X resolution != Y resolution. Default: False.
        verbose -- boolean    Produce verbose output. Default: False.
        """
        routeRoads = kwargs.get('routeRoads', False)
        routeRoofs = kwargs.get('routeRoofs', False)
        force = kwargs.get('force', False)
        ignoreBurnedDEM = kwargs.get('ignoreBurnedDEM', False)
        verbose = kwargs.get('verbose', False)
        
        self.checkMetadata(routeRoads=routeRoads, 
                           routeRoofs=routeRoofs)
        
        rhessysDir = self.metadata['rhessys_dir']
        self.paths = RHESSysPaths(self.context.projectDir, rhessysDir)
        
        demResX = float(self.studyArea['dem_res_x'])
        demResY = float(self.studyArea['dem_res_y'])
        if demResX != demResY:
            self.outfp.write("DEM x resolution (%f) does not match y resolution (%f)" %
                             (demResX, demResY) )
            if not force:
                raise RunException('Exiting.  Use force option to override')
        
        # Determine DEM raster to use
        demRast = self.grassMetadata['dem_rast']
        if ('stream_burned_dem_rast' in self.grassMetadata) and (not ignoreBurnedDEM):
            demRast = self.grassMetadata['stream_burned_dem_rast']
        self.outfp.write("Using raster named '%s' to calculate flow direction map\n" % (demRast,) )
        
        # Make sure region is properly set
        demRast = self.grassMetadata['dem_rast']
        result = self.grassLib.script.run_command('g.region', rast=demRast)
        if result != 0:
            raise RunException("g.region failed to set region to DEM, returning {0}".format(result))
        
        # Get paths for CF binary and template
        cfPath = os.path.join(self.context.projectDir, self.metadata['cf_bin'])
        templatePath = os.path.join(self.context.projectDir, self.metadata['template'])
        if verbose:
            self.outfp.write(self.templatePath)
            self.outfp.write('\n')
        
        # Make output file paths and file name templates
        flowTableNameBase = "world_{mask}"
        flowOutpath = os.path.join(self.paths.RHESSYS_FLOW, flowTableNameBase)
        cfOutpath = os.path.join(self.paths.RHESSYS_FLOW, "cf_{mask}.out")
        
        roads = None
        if routeRoads:
            roads = self.grassMetadata['roads_rast']
        else:
            roads = self.grassMetadata['zero_rast']
        
        roofs = None
        impervious = None
        
        # These filenames are only for metadata
        if routeRoofs:
            roofs = self.grassMetadata['roof_connectivity_rast']
            impervious = self.grassMetadata['impervious_rast']
            surfaceFlowtableTemplate = "world_{mask}_surface.flow"
            subsurfaceFlowtableTemplate = "world_{mask}_subsurface.flow"
        else:
            surfaceFlowtableTemplate = subsurfaceFlowtableTemplate = "world_{mask}.flow"
        
        # Make flowtable for each masked region
        if verbose:
            self.outfp.write('Running createflowpaths (this may take a few minutes)...')
            self.outfp.flush()
        
        surfaceFlowtables = []
        subsurfaceFlowtables = []
        masks = self.metadata['subbasin_masks'].split(RHESSysMetadata.VALUE_DELIM)
        for mask in masks:
            result = self.grassLib.script.run_command('r.mask', flags='o', input=mask, maskcats='1',
                                                      quiet=True)
            if result != 0:
                raise RunException("r.mask failed to set mask to sub-basin {0}, returning {1}".format(mask,
                                                                                                      result))
            # Run CF
            p = self.grassLib.script.pipe_command(cfPath, out=flowOutpath.format(mask=mask), 
                                                  template=templatePath, dem=demRast, 
                                                  slope=self.grassMetadata['slope_rast'],
                                                  stream=self.grassMetadata['streams_rast'],
                                                  road=roads, roof=roofs, impervious=impervious,
                                                  cellsize=demResX)
            (pStdout, pStderr) = p.communicate()
            
            if verbose:
                self.outfp.write("CF output:\n")
                self.outfp.write(pStdout)
                if pStderr:
                    self.outfp.write(pStderr)
            
            if p.returncode != 0:
                raise RunException("createflowpaths failed, returning %s" % ( str(p.returncode),))
            
            # Write cf output to project directory
            cfOut = open(cfOutpath.format(mask=mask), 'w')
            cfOut.write(pStdout)
            if pStderr:
                cfOut.write("\n\nStandard error output:\n\n")
                cfOut.write(pStderr)
            cfOut.close()
            
            surfFlow = os.path.join(self.paths.RHESSYS_FLOW, surfaceFlowtableTemplate.format(mask=mask))
            surfaceFlowtables.append(surfFlow)
            subsurfFlow = os.path.join(self.paths.RHESSYS_FLOW, subsurfaceFlowtableTemplate.format(mask=mask))
            subsurfaceFlowtables.append(subsurfFlow)
        
        # Remove mask
        result = self.grassLib.script.run_command('r.mask', flags='r', quiet=True)
        if result != 0:
            raise RunException("r.mask failed to remove mask") 
            
        # Write metadata
        cfCmd = "%s out=%s template=%s dem=%s slope=%s stream=%s road=%s roof=%s impervious=%s cellsize=%s" % \
        (cfPath, flowOutpath, templatePath, demRast, self.grassMetadata['slope_rast'],
         self.grassMetadata['streams_rast'], roads, roofs, impervious, demResX)
        RHESSysMetadata.writeRHESSysEntry(self.context, 'flowtable_cmd', cfCmd)
        RHESSysMetadata.writeRHESSysEntry(self.context, 'surface_flowtables', 
                                          RHESSysMetadata.VALUE_DELIM.join([self.paths.relpath(s) for s in surfaceFlowtables]) )
        RHESSysMetadata.writeRHESSysEntry(self.context, 'subsurface_flowtables', 
                                          RHESSysMetadata.VALUE_DELIM.join([self.paths.relpath(s) for s in subsurfaceFlowtables]) )

        if verbose:
            self.outfp.write('\n\nFinished creating flow tables\n')
            
        # Write processing history
        RHESSysMetadata.appendProcessingHistoryItem(self.context, RHESSysMetadata.getCommandLine())
         
Ejemplo n.º 46
0
    def run(self, *args, **kwargs):
        """ Create flow tables for multiple worldfiles 
        
        Arguments:
        routeRoads -- boolean    Whether road routing should be enabled in createflowpaths. Default: False.
        routeRoofs -- boolean    Whether roof routing should be enabled in createflowpaths. Default: False.
        ignoreBurnedDEM -- boolean    If true, use the base DEM when running createflowpaths. 
                                      If false, use the stream-burned DEM (if present).  Default: False.
        force -- boolean        Whether to force createflowpaths to run if DEM X resolution != Y resolution. Default: False.
        verbose -- boolean    Produce verbose output. Default: False.
        """
        routeRoads = kwargs.get('routeRoads', False)
        routeRoofs = kwargs.get('routeRoofs', False)
        force = kwargs.get('force', False)
        ignoreBurnedDEM = kwargs.get('ignoreBurnedDEM', False)
        verbose = kwargs.get('verbose', False)

        self.checkMetadata(routeRoads=routeRoads, routeRoofs=routeRoofs)

        rhessysDir = self.metadata['rhessys_dir']
        self.paths = RHESSysPaths(self.context.projectDir, rhessysDir)

        demResX = float(self.studyArea['dem_res_x'])
        demResY = float(self.studyArea['dem_res_y'])
        if demResX != demResY:
            self.outfp.write(
                "DEM x resolution (%f) does not match y resolution (%f)" %
                (demResX, demResY))
            if not force:
                raise RunException('Exiting.  Use force option to override')

        # Determine DEM raster to use
        demRast = self.grassMetadata['dem_rast']
        if ('stream_burned_dem_rast'
                in self.grassMetadata) and (not ignoreBurnedDEM):
            demRast = self.grassMetadata['stream_burned_dem_rast']
        self.outfp.write(
            "Using raster named '%s' to calculate flow direction map\n" %
            (demRast, ))

        # Make sure region is properly set
        demRast = self.grassMetadata['dem_rast']
        result = self.grassLib.script.run_command('g.region', rast=demRast)
        if result != 0:
            raise RunException(
                "g.region failed to set region to DEM, returning {0}".format(
                    result))

        # Get paths for CF binary and template
        cfPath = os.path.join(self.context.projectDir, self.metadata['cf_bin'])
        templatePath = os.path.join(self.context.projectDir,
                                    self.metadata['template'])
        if verbose:
            self.outfp.write(self.templatePath)
            self.outfp.write('\n')

        # Make output file paths and file name templates
        flowTableNameBase = "world_{mask}"
        flowOutpath = os.path.join(self.paths.RHESSYS_FLOW, flowTableNameBase)
        cfOutpath = os.path.join(self.paths.RHESSYS_FLOW, "cf_{mask}.out")

        roads = None
        if routeRoads:
            roads = self.grassMetadata['roads_rast']
        else:
            roads = self.grassMetadata['zero_rast']

        roofs = None
        impervious = None

        # These filenames are only for metadata
        if routeRoofs:
            roofs = self.grassMetadata['roof_connectivity_rast']
            impervious = self.grassMetadata['impervious_rast']
            surfaceFlowtableTemplate = "world_{mask}_surface.flow"
            subsurfaceFlowtableTemplate = "world_{mask}_subsurface.flow"
        else:
            surfaceFlowtableTemplate = subsurfaceFlowtableTemplate = "world_{mask}.flow"

        # Make flowtable for each masked region
        if verbose:
            self.outfp.write(
                'Running createflowpaths (this may take a few minutes)...')
            self.outfp.flush()

        surfaceFlowtables = []
        subsurfaceFlowtables = []
        masks = self.metadata['subbasin_masks'].split(
            RHESSysMetadata.VALUE_DELIM)
        for mask in masks:
            result = self.grassLib.script.run_command('r.mask',
                                                      flags='o',
                                                      input=mask,
                                                      maskcats='1',
                                                      quiet=True)
            if result != 0:
                raise RunException(
                    "r.mask failed to set mask to sub-basin {0}, returning {1}"
                    .format(mask, result))
            # Run CF
            p = self.grassLib.script.pipe_command(
                cfPath,
                out=flowOutpath.format(mask=mask),
                template=templatePath,
                dem=demRast,
                slope=self.grassMetadata['slope_rast'],
                stream=self.grassMetadata['streams_rast'],
                road=roads,
                roof=roofs,
                impervious=impervious,
                cellsize=demResX)
            (pStdout, pStderr) = p.communicate()

            if verbose:
                self.outfp.write("CF output:\n")
                self.outfp.write(pStdout)
                if pStderr:
                    self.outfp.write(pStderr)

            if p.returncode != 0:
                raise RunException("createflowpaths failed, returning %s" %
                                   (str(p.returncode), ))

            # Write cf output to project directory
            cfOut = open(cfOutpath.format(mask=mask), 'w')
            cfOut.write(pStdout)
            if pStderr:
                cfOut.write("\n\nStandard error output:\n\n")
                cfOut.write(pStderr)
            cfOut.close()

            surfFlow = os.path.join(self.paths.RHESSYS_FLOW,
                                    surfaceFlowtableTemplate.format(mask=mask))
            surfaceFlowtables.append(surfFlow)
            subsurfFlow = os.path.join(
                self.paths.RHESSYS_FLOW,
                subsurfaceFlowtableTemplate.format(mask=mask))
            subsurfaceFlowtables.append(subsurfFlow)

        # Remove mask
        result = self.grassLib.script.run_command('r.mask',
                                                  flags='r',
                                                  quiet=True)
        if result != 0:
            raise RunException("r.mask failed to remove mask")

        # Write metadata
        cfCmd = "%s out=%s template=%s dem=%s slope=%s stream=%s road=%s roof=%s impervious=%s cellsize=%s" % \
        (cfPath, flowOutpath, templatePath, demRast, self.grassMetadata['slope_rast'],
         self.grassMetadata['streams_rast'], roads, roofs, impervious, demResX)
        RHESSysMetadata.writeRHESSysEntry(self.context, 'flowtable_cmd', cfCmd)
        RHESSysMetadata.writeRHESSysEntry(
            self.context, 'surface_flowtables',
            RHESSysMetadata.VALUE_DELIM.join(
                [self.paths.relpath(s) for s in surfaceFlowtables]))
        RHESSysMetadata.writeRHESSysEntry(
            self.context, 'subsurface_flowtables',
            RHESSysMetadata.VALUE_DELIM.join(
                [self.paths.relpath(s) for s in subsurfaceFlowtables]))

        if verbose:
            self.outfp.write('\n\nFinished creating flow tables\n')

        # Write processing history
        RHESSysMetadata.appendProcessingHistoryItem(
            self.context, RHESSysMetadata.getCommandLine())
# Handle command line options
parser = argparse.ArgumentParser(description='Generate landcover maps in GRASS GIS')
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('-l', '--makeLaiMap', dest='makeLaiMap', required=False, action='store_true',
                    help='Make LAI map')
parser.add_argument('--skipRoads', dest='skipRoads', required=False, action='store_true', default=False,
                    help='Do not make roads map')
parser.add_argument('--defonly', dest='defonly', required=False, action='store_true',
                    help='Only generate landuse and stratum definition files, do not try to create maps.  Maps must already exist.')
parser.add_argument('--overwrite', dest='overwrite', action='store_true', required=False,
                    help='Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'landcover_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a GRASS dataset with a landcover raster" % (context.projectDir,))
if not 'dem_rast' in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a DEM raster in a GRASS mapset" % (context.projectDir,)) 

metadata = RHESSysMetadata.readRHESSysEntries(context)
Ejemplo n.º 48
0
parser.add_argument('-i', '--configfile', dest='configfile', required=False,
                    help='The configuration file. Must define section "GRASS" and option "GISBASE"')
parser.add_argument('-p', '--projectDir', dest='projectDir', required=True,
                    help='The directory to which metadata, intermediate, and final files should be saved')
parser.add_argument('--routeRoads', dest='routeRoads', required=False, action='store_true',
                    help='Tell createflowpaths to route flow from roads to the nearest stream pixel (requires roads_rast to be defined in metadata)')
parser.add_argument('--routeRoofs', dest='routeRoofs', required=False, action='store_true',
                    help='Tell createflowpaths to route flow from roof tops based on roof top connectivity to nearest impervious surface (requires roof_connectivity_rast and impervious_rast to be defined in metadata)')
parser.add_argument('-f', '--force', dest='force', action='store_true',
                    help='Run createflowpaths even if DEM x resolution does not match y resolution')
parser.add_argument('--ignoreBurnedDEM', dest='ignoreBurnedDEM', action='store_true', required=False,
                    help='Ignore stream burned DEM, if present. Default DEM raster will be used for all operations. If not specified and if stream burned raster is present, stream burned DEM will be used for generating the flow table.')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
                    help='Print detailed information about what the program is doing')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

# Check for necessary information in metadata
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
metadata = RHESSysMetadata.readRHESSysEntries(context)

if not 'dem_res_x' in studyArea:
    sys.exit("Metadata in project directory %s does not contain a DEM x resolution" % (context.projectDir,))
if not 'dem_res_y' in studyArea:
group.add_argument(
    '-r',
    '--ruleDir',
    dest='ruleDir',
    required=False,
    help=
    "The directory where landcover reclass rules can be found; should contain these files %s"
    % (str(RHESSysMetadata.LC_RULES), ))
parser.add_argument('-l',
                    '--includeLaiRules',
                    dest='includeLaiRules',
                    required=False,
                    action='store_true',
                    help='Make LAI map')
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
manifest = RHESSysMetadata.readManifestEntries(context)

paths = RHESSysPaths(args.projectDir, metadata['rhessys_dir'])

# Get path of place to store reclass rules
Ejemplo n.º 50
0
    type=float,
    help=
    'Rescale raster values of 0 to args.resample to 0 to 255 in output images.'
)
args = parser.parse_args()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

ffmpegPath = context.config.get('RHESSYS', 'PATH_OF_FFMPEG')

# Check for necessary information in metadata
metadata = RHESSysMetadata.readRHESSysEntries(context)
if not 'grass_dbase' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS Dbase" %
        (context.projectDir, ))
if not 'grass_location' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS location" %
        (context.projectDir, ))
if not 'grass_mapset' in metadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS mapset" %
        (context.projectDir, ))

if not os.path.isfile(args.rhessysOutFile) or not os.access(
        args.rhessysOutFile, os.R_OK):
Ejemplo n.º 51
0
    dest='forceZone',
    required=False,
    action='store_true',
    help='Use patch map as zone map even if zone map is already defined. ' +
    ' By default if a zone map is present, this script will not set the patch map as the zone map.'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
studyArea = RHESSysMetadata.readStudyAreaEntries(context)
if not 'dem_rows' in studyArea:
    sys.exit(
        "Metadata in project directory %s does not contain DEM rows entry" %
        (context.projectDir, ))

grassMetadata = RHESSysMetadata.readGRASSEntries(context)
    dest='defonly',
    required=False,
    action='store_true',
    help=
    'Only generate landuse and stratum definition files, do not try to create maps.  Maps must already exist.'
)
parser.add_argument(
    '--overwrite',
    dest='overwrite',
    action='store_true',
    required=False,
    help=
    'Overwrite existing datasets in the GRASS mapset.  If not specified, program will halt if a dataset already exists.'
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not 'landcover_rast' in grassMetadata:
    sys.exit(
        "Metadata in project directory %s does not contain a GRASS dataset with a landcover raster"
        % (context.projectDir, ))
if not 'dem_rast' in grassMetadata:
    sys.exit(
Ejemplo n.º 53
0
parser.add_argument(
    "-p",
    "--projectDir",
    dest="projectDir",
    required=True,
    help="The directory to which metadata, intermediate, and final files should be saved",
)
parser.add_argument(
    "-v",
    "--verbose",
    dest="verbose",
    action="store_true",
    help="Print detailed information about what the program is doing",
)
args = parser.parse_args()
cmdline = RHESSysMetadata.getCommandLine()

configFile = None
if args.configfile:
    configFile = args.configfile

context = Context(args.projectDir, configFile)

# Check for necessary information in metadata
grassMetadata = RHESSysMetadata.readGRASSEntries(context)
if not "dem_rast" in grassMetadata:
    sys.exit("Metadata in project directory %s does not contain a DEM raster in a GRASS mapset" % (context.projectDir,))
if not "soil_rast" in grassMetadata:
    sys.exit(
        "Metadata in project directory %s does not contain a soil raster in a GRASS mapset" % (context.projectDir,)
    )