diff --git a/src/raster/r.maxent.predict/r.maxent.predict.html b/src/raster/r.maxent.predict/r.maxent.predict.html index 06bedf6751..618cea4a48 100644 --- a/src/raster/r.maxent.predict/r.maxent.predict.html +++ b/src/raster/r.maxent.predict/r.maxent.predict.html @@ -197,7 +197,9 @@

3: Create a prediction layer

 r.maxent.predict
-  lambda=output_model1/Bradypus_tridactylus.lambdas raster=bio02_ssp585,bio03_ssp585,bio08_ssp585,bio09_ssp585,bio13_ssp585,bio15_ssp585,bio17_ssp585,sa_eco_l2@current variables=bio02,bio03,bio08,bio09,bio13,bio15,bio17,landuse \
+  lambda=output_model1/Bradypus_tridactylus.lambdas \
+  rasters=bio02_ssp585,bio03_ssp585,bio08_ssp585,bio09_ssp585,bio13_ssp585,bio15_ssp585,bio17_ssp585,sa_eco_l2 \
+  variables=bio02,bio03,bio08,bio09,bio13,bio15,bio17,landuse \
   output=model_1_ssp585
 
@@ -207,7 +209,8 @@

3: Create a prediction layer

model_1_ssp585 (right map in the figure below). The results suggest the area with suitable conditions will increase under future climates compared the that under the current conditions (left map in -the figure below). Not sure that is realistic. +the figure below). This result is unexpected, and warrants further +investigation.

1 and current_mapset not in chlay_mapsets: + double_layers.append(layer) + return {"missing": missing_layers, "double": double_layers} def create_temp_name(prefix): @@ -270,56 +318,75 @@ def main(options, flags): ) gs.fatal(_(msg)) - # Get names of variables and corresponding layer names + # Create (or get) folder with environmental layers # ------------------------------------------------------------------ - gs.info(_("Check if the variable and layer names correspond\n")) - - if bool(options["alias_file"]): - with open(options["alias_file"]) as csv_file: - row_data = list(csv.reader(csv_file, delimiter=",")) - col_data = list(zip(*row_data)) - chlay = check_layers_exist(col_data[1]) - if len(chlay) > 0: - gs.message( + + projectionlayers = options["projectionlayers"] + if projectionlayers: + # The name of an existing folder with layers is provided + temp_directory = projectionlayers + else: + # Create temporary folder for the raster layers + temp_directory = gs.tempdir() + + # Get Get names of variables and corresponding layer names + if bool(options["alias_file"]): + with open(options["alias_file"]) as csv_file: + row_data = list(csv.reader(csv_file, delimiter=",")) + col_data = list(zip(*row_data)) + file_names = col_data[0] + layer_names = col_data[1] + else: + layer_names = options["rasters"].split(",") + if bool(options["variables"]): + file_names = options["variables"].split(",") + else: + file_names = [strip_mapset(x) for x in layer_names] + chlay = check_layers(layer_names) + if len(chlay["missing"]) > 0: + gs.fatal( _( "The layer(s) {} do not exist in the accessible mapsets".format( - ", ".join(chlay) + ", ".join(chlay["missing"]) + ) + ) + ) + if len(chlay["double"]) > 0: + gs.fatal( + _( + "There are layers with the name {} in multiple accessible mapsets, " + "none of which are in the current mapset." + "Add the mapset name to specify which or these layers should be used.".format( + ", ".join(chlay["double"]) ) ) ) - else: - file_names = col_data[0] - layer_names = col_data[1] - else: - layer_names = options["raster"].split(",") - if bool(options["variables"]): - file_names = options["variables"].split(",") - else: - file_names = [strip_mapset(x) for x in layer_names] - # Export raster layers to temporary directory - # ------------------------------------------------------------------ - gs.info(_("Export the raster layers as asci layers for use by Maxent\n")) + # Export raster layers to temporary directory + gs.info(_("Export the raster layers as asci layers for use by Maxent\n")) + loading_indicator = LoadingIndicator() + loading_indicator.start() - for n, layer_name in enumerate(layer_names): - dt = gs.parse_command("r.info", map=layer_name, flags="g")["datatype"] - if dt == "CELL": - datatype = "Int16" - nodataval = -9999 - else: - datatype = "" - nodataval = -9999999 - file_name = os.path.join(temp_directory, f"{file_names[n]}.asc") - gs.run_command( - "r.out.gdal", - input=layer_name, - output=file_name, - format="AAIGrid", - flags="c", - type=datatype, - nodata=nodataval, - quiet=True, - ) + for n, layer_name in enumerate(layer_names): + dt = gs.parse_command("r.info", map=layer_name, flags="g")["datatype"] + if dt == "CELL": + datatype = "Int16" + nodataval = -9999 + else: + datatype = "" + nodataval = -9999999 + file_name = os.path.join(temp_directory, f"{file_names[n]}.asc") + gs.run_command( + "r.out.gdal", + input=layer_name, + output=file_name, + format="AAIGrid", + flags="c", + type=datatype, + nodata=nodataval, + quiet=True, + ) + loading_indicator.stop() # Input parameters - building command line string # ------------------------------------------------------------------ @@ -332,7 +399,7 @@ def main(options, flags): "-cp", maxent_file, "density.Project", - options["lambda"], + options["lambdafile"], temp_directory, temp_file, ] @@ -344,26 +411,41 @@ def main(options, flags): # Run Maxent density.Project # ----------------------------------------------------------------- + gs.info(_("This may take some time ...")) with subprocess.Popen( maxent_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, + text=True, ) as process: # Capture and print stdout for stdout_line in process.stdout: gs.info(stdout_line) # Capture and print stderr for stderr_line in process.stderr: - gs.info(stderr_line) + gs.info(f"Warning/Error: {stderr_line}") + if "java.util.NoSuchElementException" in stderr_line: + missing_variables = ( + "Check variable names and path + names of input files" + ) # Check the return code process.wait() if process.returncode != 0: - gs.fatal(_("Maxent terminated with an error")) - + if missing_variables: + gs.fatal(missing_variables) + else: + gs.fatal(_("Maxent terminated with an error")) # ----------------------------------------------------------------- # Import the resulting layer in GRASS GIS # ----------------------------------------------------------------- + if not os.path.isfile(temp_file): + gs.fatal( + _( + "Maxent did not create an output raster for import in GRASS.\n" + "Check the error message(s) above." + ) + ) gs.info(_("Importing the predicted suitability layer in GRASS GIS\n")) gs.run_command( "r.in.gdal",