En raison du déménagement des baies serveurs, les services gitlab.irstea.fr et mattermost.irstea.fr seront interrompus le samedi 2 octobre 2021 au matin. Ils devraient revenir à la normale dans la journée.

Unverified Commit a05bf3a5 authored by Julien Veyssier's avatar Julien Veyssier
Browse files

use function wrappers around grass calls to add option needed by windows


Signed-off-by: default avatarJulien Veyssier <eneiluj@posteo.net>
parent 69e355f0
......@@ -163,9 +163,9 @@ def main(parms_file):
cutting_raster(layer, layer_cut, xmin_slect, ymax_slect, xmax_slect, ymin_slect)
dem_wk = 'dem_wk'
grass.run_command('g.proj', flags='c', georef=dem_cut, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass.run_command('r.in.gdal', flags='o', input=dem_cut, output=dem_wk, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass.run_command('g.region', flags='sp', raster=dem_wk, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('g.proj', flags='c', georef=dem_cut, stdout=DEVNULL, stderr=DEVNULL)
grass_run_command('r.in.gdal', flags='o', input=dem_cut, output=dem_wk, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
grass_run_command('g.region', flags='sp', raster=dem_wk, stdout=DEVNULL, stderr=DEVNULL)
# Fill the DEM (depressionless)
dem_filled = elev = dem_wk
......@@ -178,28 +178,28 @@ def main(parms_file):
areas = 1
while (areas > 0):
grass.run_command('r.fill.dir', input=elev, output=dem_filled, direction=dir_temp, areas=unfilled_areas,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
areas = int(grass.parse_command('r.info', flags='r', map=unfilled_areas, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())['max'])
grass_run_command('r.fill.dir', input=elev, output=dem_filled, direction=dir_temp, areas=unfilled_areas,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
areas = int(grass_parse_command('r.info', flags='r', map=unfilled_areas, stdout=DEVNULL, stderr=DEVNULL)['max'])
elev = dem_filled
# Slope and aspect derivation
print('---------- HRU-delin Step 1 : Derivating layers from MNT')
dem_slope = 'dem_slope'
dem_aspect = 'dem_aspect'
grass.run_command('r.slope.aspect', elevation=dem_filled, slope=dem_slope, aspect=dem_aspect,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.slope.aspect', elevation=dem_filled, slope=dem_slope, aspect=dem_aspect,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
# Reclassifying rasters
# DEM
dem_rcl = 'dem_rcl'
if (parms.get('reclass_dem', 'rules_auto_dem')) == 'yes':
pRecode = grass.feed_command('r.recode', input=dem_filled, output=dem_rcl, rules='-', quiet=True, startupinfo=getSi())
pRecode = grass_feed_command('r.recode', input=dem_filled, output=dem_rcl, rules='-', quiet=True)
step_reclass = int(parms.get('reclass_dem', 'step_dem'))
alti_min = float(grass.parse_command('r.info', flags='r', map=dem_filled, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())['min'])
alti_max = float(grass.parse_command('r.info', flags='r', map=dem_filled, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())['max'])
alti_min = float(grass_parse_command('r.info', flags='r', map=dem_filled, stdout=DEVNULL, stderr=DEVNULL)['min'])
alti_max = float(grass_parse_command('r.info', flags='r', map=dem_filled, stdout=DEVNULL, stderr=DEVNULL)['max'])
# just a copy, still do the pipe
recl_dem_file = os.path.join(directory_out, 'reclass_rules_dem')
......@@ -228,7 +228,7 @@ def main(parms_file):
if not os.path.isfile(recl_dem_file):
sys.exit('------------> File reclass_rules_dem is missing')
# r.recode does a better job with grass7, values close to interval limits are now correctly reclassified
grass.run_command('r.recode', input=dem_filled, output=dem_rcl, rules=recl_dem_file, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.recode', input=dem_filled, output=dem_rcl, rules=recl_dem_file, stdout=DEVNULL, stderr=DEVNULL)
# Save the rules to directory_out
recl_dem_name = os.path.basename(recl_dem_file)
shutil.copyfile(recl_dem_file, os.path.join(directory_out, recl_dem_name))
......@@ -237,12 +237,12 @@ def main(parms_file):
slp_rcl = 'slp_rcl'
if (parms.get('reclass_slope', 'rules_auto_slope')) == 'yes':
pRecode = grass.feed_command('r.recode', input=dem_slope, output=slp_rcl, rules='-', quiet=True, startupinfo=getSi())
pRecode = grass_feed_command('r.recode', input=dem_slope, output=slp_rcl, rules='-', quiet=True)
recl_slope_file = os.path.join(directory_out, 'reclass_rules_slope')
default_recl_slope_file = os.path.join(DATA_PATH, 'reclass_default_rules_slope')
slp_min = float(grass.parse_command('r.info', flags='r', map=dem_slope, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())['min'])
slp_max = float(grass.parse_command('r.info', flags='r', map=dem_slope, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())['max'])
slp_min = float(grass_parse_command('r.info', flags='r', map=dem_slope, stdout=DEVNULL, stderr=DEVNULL)['min'])
slp_max = float(grass_parse_command('r.info', flags='r', map=dem_slope, stdout=DEVNULL, stderr=DEVNULL)['max'])
with open(default_recl_slope_file, 'r') as default_slp_rules:
slp_class = default_slp_rules.read()
max_slp = str(slp_max)
......@@ -260,7 +260,7 @@ def main(parms_file):
if not os.path.isfile(recl_slope_file):
sys.exit('------------> file reclass_rules_slope is missing')
# improve some subprocess.Popen => run_command with rules=FILE
grass.run_command('r.recode', input=dem_slope, output=slp_rcl, rules=recl_slope_file, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.recode', input=dem_slope, output=slp_rcl, rules=recl_slope_file, stdout=DEVNULL, stderr=DEVNULL)
recl_slope_name = os.path.basename(recl_slope_file)
shutil.copyfile(recl_slope_file, os.path.join(directory_out, recl_slope_name))
......@@ -276,7 +276,7 @@ def main(parms_file):
shutil.copyfile(user_aspect, recl_aspect_file)
asp_rcl = 'asp_rcl'
grass.run_command('r.recode', input=dem_aspect, output=asp_rcl, rules=recl_aspect_file, stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.recode', input=dem_aspect, output=asp_rcl, rules=recl_aspect_file, stdout=DEVNULL, stderr=DEVNULL)
# Oriented flows generation
print('---------- HRU-delin Step 1 : Generating Oriented Flows')
......@@ -286,7 +286,7 @@ def main(parms_file):
halfbasins='halfbasins'
accum = 'accum_wk'
drain = 'drain_wk'
grass.run_command('r.watershed',
grass_run_command('r.watershed',
flags='sb',
elevation=dem_filled,
stream=streams,
......@@ -296,50 +296,50 @@ def main(parms_file):
drainage=drain,
threshold=min_size,
overwrite='True',
stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi()
stdout=DEVNULL, stderr=DEVNULL
)
# Save rasters
print('---------- HRU-delin Step 1 : Saving the rasters')
raster_out = os.path.join(directory_out, 'step1_dem_filled.tif')
grass.run_command('r.out.gdal', input=dem_filled, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=dem_filled, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_dem_reclass.tif')
grass.run_command('r.out.gdal', input=dem_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=dem_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_slope.tif')
grass.run_command('r.out.gdal', input=dem_slope, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=dem_slope, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_aspect.tif')
grass.run_command('r.out.gdal', input=dem_aspect, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=dem_aspect, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_slope_reclass.tif')
grass.run_command('r.out.gdal', input=slp_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=slp_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_aspect_reclass.tif')
grass.run_command('r.out.gdal', input=asp_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=asp_rcl, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_streams.tif')
grass.run_command('r.out.gdal', input=streams, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=streams, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_drain.tif')
grass.run_command('r.out.gdal', input=drain, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=drain, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_accum.tif')
grass.run_command('r.out.gdal', input=accum, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=accum, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_subbasins.tif')
grass.run_command('r.out.gdal', input=subbasins, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=subbasins, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
raster_out = os.path.join(directory_out, 'step1_halfbasins.tif')
grass.run_command('r.out.gdal', input=halfbasins, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL, startupinfo=getSi())
grass_run_command('r.out.gdal', input=halfbasins, output=raster_out, overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
print('---------- HRU-delin Step 1 ended ---------------------------------------------')
if __name__ == '__main__':
from progressColors import *
from parallel import buildGrassEnv
from ogr2ogr import main as ogr2ogrMain
from utils import getSi
from utils import grass_run_command, grass_parse_command, grass_feed_command
try:
from tqdm import tqdm
except Exception as e:
......@@ -364,4 +364,4 @@ else:
from .progressColors import *
from .parallel import buildGrassEnv
from .ogr2ogr import main as ogr2ogrMain
from .utils import getSi
\ No newline at end of file
from .utils import grass_run_command, grass_parse_command, grass_feed_command
\ No newline at end of file
# coding: utf-8
import pandas as pd
import grass.script as grass
import subprocess
import platform
isWindows = (platform.system() == 'Windows')
def myJoin(f1Path, f2Path, resultPath):
df1 = pd.read_table(f1Path, delim_whitespace=True)
......@@ -7,9 +11,7 @@ def myJoin(f1Path, f2Path, resultPath):
merge = pd.merge(df1, df2)
merge.to_csv(resultPath, header=True, index=False, sep=' ')
import subprocess
import platform
if platform.system() == 'Windows':
if isWindows:
def getSi():
si = None
if hasattr(subprocess, 'STARTUPINFO'):
......@@ -18,4 +20,16 @@ if platform.system() == 'Windows':
return si
else:
def getSi():
return None
\ No newline at end of file
return None
def grass_run_command(*args, **kwargs):
kwargs['startupinfo'] = getSi()
return grass.run_command(*args, **kwargs)
def grass_parse_command(*args, **kwargs):
kwargs['startupinfo'] = getSi()
return grass.parse_command(*args, **kwargs)
def grass_feed_command(*args, **kwargs):
kwargs['startupinfo'] = getSi()
return grass.feed_command(*args, **kwargs)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment