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 cf3b1677 authored by Julien Veyssier's avatar Julien Veyssier
Browse files

use pipes for all r.reclass that used to read files


Signed-off-by: default avatarJulien Veyssier <eneiluj@posteo.net>
parent f484f252
......@@ -173,27 +173,28 @@ def processSubbasin(params):
df1 = pd.read_csv(stio1, header=None, dtype={0: int, 1: int}, sep=' ')
df2 = pd.read_csv(stio2, header=None, dtype={0: int}, sep=' ')
df3 = df1.merge(df2, on=0, sort=True)
out3Path = os.path.join(tmpPath, 'out3_proc%s' % processN)
with open(out3Path, 'w') as o3:
for index, row in df3.iterrows():
o3.write('%s = %s %s\n' % (row[0], row[0], row[1]))
#out3Path = os.path.join(tmpPath, 'out3_proc%s' % processN)
pReclass = grass.feed_command('r.reclass', input='newmap', output='test', rules='-', overwrite='True')
for index, row in df3.iterrows():
pReclass.stdin.write(encode('%s = %s %s\n' % (row[0], row[0], row[1])))
pReclass.stdin.close()
pReclass.wait()
#os.system('cat %s/out3_proc%s | r.reclass --o input=newmap output=test rules=-' % (tmpPath, processN))
grass.run_command('r.reclass',
input='newmap', output='test', rules=out3Path,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('g.region', raster='clumps', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('r.buffer', input='MASK', output='buf_mask', distances='%d'%buffer_distance,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('g.remove', flags='f', type='raster', name='MASK', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('g.region', zoom='buf_mask', stdout=DEVNULL, stderr=DEVNULL)
#os.system('echo "2 = 2 9999999" | r.reclass --o input=buf_mask output=buf_mask_new rules=-')
reclPath = os.path.join(tmpPath, 'reclBufMask%s' % processN)
with open(reclPath, 'w') as rp:
rp.write('2 = 2 9999999\n')
grass.run_command('r.reclass',
input='buf_mask', output='buf_mask_new', rules=reclPath,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
pReclass = grass.feed_command('r.reclass',
input='buf_mask', output='buf_mask_new', rules='-',
overwrite='True')
pReclass.stdin.write(encode('2 = 2 9999999\n'))
pReclass.stdin.close()
pReclass.wait()
grass.run_command('r.patch',
input='buf_mask_new,test', output='sum_buf', overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('r.mapcalc',
......@@ -228,14 +229,15 @@ def processSubbasin(params):
#os.system("r.stats -lnN input=map1 | tail -n +2 | awk '{print $1 \" = \" $2}' | r.reclass --o input=base output=base_new rules=-")
p = grass.pipe_command('r.stats', quiet=True, flags='lnN', input='map1')
pReclass = grass.feed_command('r.reclass', overwrite=True, input='base', output='base_new', rules='-')
# skip first line "0 0"
l = p.stdout.readline()
reclassNewPath = os.path.join(tmpPath, 'reclassNew%s' % processN)
with open(reclassNewPath, 'w') as recl:
for l in p.stdout:
lSpl = l.strip().split()
recl.write('%s = %s\n' % (int(lSpl[0]), int(lSpl[1])))
grass.run_command('r.reclass', overwrite=True, input='base', output='base_new', rules=reclassNewPath)
for l in p.stdout:
lSpl = decode(l).rstrip(os.linesep).split()
pReclass.stdin.write(encode('%s = %s\n' % (lSpl[0], lSpl[1])))
p.wait()
pReclass.stdin.close()
pReclass.wait()
grass.run_command('r.patch',
input='base_new,newmap3', output='newout', overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
......@@ -249,22 +251,23 @@ def processSubbasin(params):
#os.system("r.stats -cnN input=newmap3 | awk '{print $1 \" = \" $1 \" \" $2}' | r.reclass --o input=newmap3 output=newout_sz rules=-")
p = grass.pipe_command('r.stats', flags='cnN', input='newmap3', quiet=True)
reclassNewSzPath = os.path.join(tmpPath, 'reclassNewSz%s' % processN)
with open(reclassNewSzPath, 'w') as recl:
for l in p.stdout:
lSpl = l.strip().split()
recl.write('%s = %s %s\n' % (int(lSpl[0]), int(lSpl[0]), int(lSpl[1])))
grass.run_command('r.reclass', overwrite=True, input='newmap3', output='newout_sz', rules=reclassNewSzPath)
pReclass = grass.feed_command('r.reclass', overwrite=True, input='newmap3', output='newout_sz', rules='-')
for l in p.stdout:
lSpl = decode(l).rstrip(os.linesep).split()
pReclass.stdin.write(encode('%s = %s %s\n' % (lSpl[0], lSpl[0], lSpl[1])))
p.wait()
pReclass.stdin.close()
pReclass.wait()
reclass('newmap3', 'newmap_sz2', sz)
grass.run_command('r.buffer', input='newmap_sz2', output='buffer', distances='%d'%buffer_distance,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
#os.system('echo "1 thru 2 = 1" | r.reclass --o input=buffer output=buffer_new rules=-')
rulz = os.path.join(tmpPath, 'reclTmp')
with open(rulz, 'w') as rct:
rct.write('1 thru 2 = 1\n')
grass.run_command('r.reclass', overwrite=True, input='buffer', output='buffer_new', rules=rulz)
pReclass = grass.feed_command('r.reclass', overwrite=True, input='buffer', output='buffer_new', rules='-')
pReclass.stdin.write(encode('1 thru 2 = 1\n'))
pReclass.stdin.close()
pReclass.wait()
grass.run_command('r.mapcalc', expression='b1=if((buffer_new==1&&isnull(newmap_sz2)),newmap_sz2[-1,0],null())', overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
grass.run_command('r.mapcalc', expression='b2=if((buffer_new==1&&isnull(newmap_sz2)),newmap_sz2[1,0],null())', overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
......@@ -479,25 +482,23 @@ def main(parms_file, nbProc, generator=False):
stdout=DEVNULL, stderr=DEVNULL
)
# generate reclass rules
hrutmpLines = grass.read_command('r.stats', flags='l', input='hrus_tmp_wrong').decode('utf-8').strip().split('\n')
reclassHrustmpPath = os.path.join(configFileDir, 'tmp', 'reclassHrustmp')
with open(reclassHrustmpPath, 'w') as reclassRules:
c = 1
for l in hrutmpLines:
spl = l.split()
cat = spl[0]
label = ' '.join(spl[1:])
# used to be this but does not work in grass74 because result of r.cross is slightly different than grass >= 76...
#if 'NULL' in l:
if not l.startswith('*') and ('NULL' in l or 'no data' in l or l.strip() == '0'):
reclassRules.write('%s = 0\n' % (cat))
else:
reclassRules.write('%s = %s %s\n' % (cat, c, label))
c += 1
grass.run_command('r.reclass',
input='hrus_tmp_wrong', output='hrus_tmp', rules=reclassHrustmpPath,
overwrite='True', stdout=DEVNULL, stderr=DEVNULL)
p = grass.pipe_command('r.stats', flags='l', input='hrus_tmp_wrong')
pReclass = grass.feed_command('r.reclass', input='hrus_tmp_wrong', output='hrus_tmp', rules='-', overwrite='True')
c = 1
for l in p.stdout:
spl = decode(l).rstrip(os.linesep).split()
cat = spl[0]
label = ' '.join(spl[1:])
# used to be this but does not work in grass74 because result of r.cross is slightly different than grass >= 76...
#if 'NULL' in l:
if not l.startswith('*') and ('NULL' in l or 'no data' in l or l.strip() == '0'):
pReclass.stdin.write(encode('%s = 0\n' % (cat)))
else:
pReclass.stdin.write(encode('%s = %s %s\n' % (cat, c, label)))
c += 1
p.wait()
pReclass.stdin.close()
pReclass.wait()
print('----------------------------- Saving the raw cross product')
grass.run_command('r.out.gdal',
......
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