Skip to content

Commit e440d1a

Browse files
Merge pull request #413 from johntruckenbrodt/bugfix/gamma_shellscript
gamma: shellscript improvements
2 parents 54a60e7 + be3e550 commit e440d1a

File tree

3 files changed

+69
-45
lines changed

3 files changed

+69
-45
lines changed

pyroSAR/gamma/auxil.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ class Namespace(object):
285285
>>> print(n.pix_geo)
286286
'/path/S1A__IW___A_20180829T170631_pix_geo'
287287
"""
288+
288289
def __init__(self, directory, basename):
289290
self.__base = basename
290291
self.__outdir = directory
@@ -393,32 +394,40 @@ def par2hdr(parfile, hdrfile, modifications=None, nodata=None):
393394
hdr(items, hdrfile)
394395

395396

396-
def process(cmd, outdir=None, logfile=None, logpath=None,
397-
inlist=None, void=True, shellscript=None):
397+
def process(
398+
cmd: list[str],
399+
outdir: str | None = None,
400+
logfile: str | None = None,
401+
logpath: str | None = None,
402+
inlist: list[str] | None = None,
403+
void: bool = True,
404+
shellscript: str | None = None
405+
) -> tuple[str, str] | None:
398406
"""
399407
wrapper function to execute GAMMA commands via module :mod:`subprocess`
400408
401409
Parameters
402410
----------
403-
cmd: list[str]
404-
the command line arguments
405-
outdir: str
406-
the directory to execute the command in
407-
logfile: str
408-
a file to write the command log to; overrides parameter logpath
409-
logpath: str
410-
a directory to write logfiles to; the file will be named {GAMMA command}.log, e.g. gc_map.log;
411-
is overridden by parameter logfile
412-
inlist: list
413-
a list of values, which is passed as interactive inputs via stdin
414-
void: bool
415-
return the stdout and stderr messages?
416-
shellscript: str
417-
a file to write the GAMMA commands to in shell format
411+
cmd:
412+
The command line arguments.
413+
outdir:
414+
The directory to execute the command in. This directory is also set
415+
as environment variable in `shellscript`.
416+
logfile:
417+
A file to write the command log to. Overrides parameter `logpath`.
418+
logpath:
419+
A directory to write logfiles to. The file will be named
420+
{GAMMA command}.log, e.g. gc_map.log.
421+
Overrides parameter `logfile`.
422+
inlist:
423+
A list of values, which is passed as interactive inputs via `stdin`.
424+
void:
425+
Return the `stdout` and `stderr` messages?
426+
shellscript:
427+
A file to write the GAMMA commands to in shell format.
418428
419429
Returns
420430
-------
421-
tuple of str or None
422431
the stdout and stderr messages if void is False, otherwise None
423432
"""
424433
if logfile is not None:
@@ -435,17 +444,21 @@ def process(cmd, outdir=None, logfile=None, logpath=None,
435444
if inlist is not None:
436445
line += ' <<< $"{}"'.format('\n'.join([str(x) for x in inlist]) + '\n')
437446
with open(shellscript, 'r+') as sh:
447+
content = sh.read()
448+
sh.seek(0)
449+
disclaimer = 'This script was created automatically by pyroSAR'
450+
is_new = re.search(disclaimer, content) is None
451+
if is_new:
452+
ts = datetime.now().strftime('%a %b %d %H:%M:%S %Y')
453+
sh.write(f'# {disclaimer} on {ts}\n\n')
454+
sh.write('GAMMA_HOME={}\n\n'.format(gamma_home))
455+
sh.write(content)
456+
line = line.replace(gamma_home, '$GAMMA_HOME')
438457
if outdir is not None:
439-
content = sh.read()
440-
sh.seek(0)
441-
is_new = re.search('this script was created automatically by pyroSAR', content) is None
442-
if is_new:
443-
ts = datetime.now().strftime('%a %b %d %H:%M:%S %Y')
444-
sh.write('# this script was created automatically by pyroSAR on {}\n\n'.format(ts))
445-
sh.write('export base={}\n'.format(outdir))
446-
sh.write('export GAMMA_HOME={}\n\n'.format(gamma_home))
447-
sh.write(content)
448-
line = line.replace(outdir, '$base').replace(gamma_home, '$GAMMA_HOME')
458+
line = line.replace(outdir, '$OUTDIR')
459+
outdirs = re.findall('OUTDIR=(.*)\n', content)
460+
if len(outdirs) == 0 or outdir != outdirs[-1]:
461+
line = f"OUTDIR={outdir}\n\n{line}"
449462
sh.seek(0, 2) # set pointer to the end of the file
450463
sh.write(line + '\n\n')
451464

pyroSAR/gamma/dem.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
###############################################################################
22
# preparation of DEM data for use in GAMMA
33

4-
# Copyright (c) 2014-2022, the pyroSAR Developers.
4+
# Copyright (c) 2014-2026, the pyroSAR Developers.
55

66
# This file is part of the pyroSAR Project. It is subject to the
77
# license terms in the LICENSE.txt file found in the top-level
@@ -299,30 +299,36 @@ def dem_autocreate(geometry, demType, outfile, buffer=None, t_srs=4326, tr=None,
299299
shutil.rmtree(tmpdir)
300300

301301

302-
def dem_import(src, dst, geoid=None, logpath=None, outdir=None):
302+
def dem_import(
303+
src: str,
304+
dst: str,
305+
geoid: str | None = None,
306+
logpath: str | None = None,
307+
outdir: str | None = None,
308+
shellscript: str | None = None
309+
) -> None:
303310
"""
304-
convert an existing DEM in GDAL-readable format to GAMMA format including optional geoid-ellipsoid conversion.
311+
convert an existing DEM in GDAL-readable format to GAMMA
312+
format including optional geoid-ellipsoid conversion.
305313
306314
Parameters
307315
----------
308-
src: str
316+
src:
309317
the input DEM
310-
dst: str
318+
dst:
311319
the output DEM
312-
geoid: str or None
320+
geoid:
313321
the geoid height reference of `src`; supported options:
314322
315323
- 'EGM96'
316324
- 'EGM2008'
317325
- None: assume WGS84 ellipsoid heights and do not convert heights
318-
logpath: str or None
326+
logpath:
319327
a directory to write logfiles to
320-
outdir: str or None
328+
outdir:
321329
the directory to execute the command in
322-
323-
Returns
324-
-------
325-
330+
shellscript:
331+
a file to write the GAMMA commands to in shell format
326332
"""
327333
with raster.Raster(src) as ras:
328334
epsg = ras.epsg
@@ -349,25 +355,30 @@ def dem_import(src, dst, geoid=None, logpath=None, outdir=None):
349355
gflg=gflg,
350356
geoid='-',
351357
logpath=logpath,
352-
outdir=outdir)
358+
outdir=outdir,
359+
shellscript=shellscript)
353360
else:
354361
# new approach enabling an arbitrary target CRS EPSG code
355362
diff.create_dem_par(DEM_par=dst_base + '.par',
356363
inlist=[''] * 9,
357-
EPSG=epsg)
364+
EPSG=epsg,
365+
logpath=logpath,
366+
outdir=outdir,
367+
shellscript=shellscript)
358368
dem_import_pars = {'input_DEM': src,
359369
'DEM': dst,
360370
'DEM_par': dst_base + '.par',
361371
'logpath': logpath,
362-
'outdir': outdir}
372+
'outdir': outdir,
373+
'shellscript': shellscript}
363374
if gflg == 2:
364375
home = ExamineGamma().home
365376
if geoid == 'EGM96':
366377
geoid_file = os.path.join(home, 'DIFF', 'scripts', 'egm96.dem')
367378
elif geoid == 'EGM2008':
368379
geoid_file = os.path.join(home, 'DIFF', 'scripts', 'egm2008-5.dem')
369380
else:
370-
raise RuntimeError('conversion of {} geoid is not supported by GAMMA'.format(geoid))
381+
raise RuntimeError(f"conversion of '{geoid}' geoid is not supported by GAMMA")
371382
dem_import_pars['geoid'] = geoid_file
372383
dem_import_pars['geoid_par'] = geoid_file + '_par'
373384

pyroSAR/gamma/error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def gammaErrorHandler(returncode: int, out: str, err: str) -> None:
8181
else:
8282
return
8383
else:
84-
err_out = f'failed with return code {returncode}'
84+
err_out = f'{err}\nfailed with return code {returncode}'
8585
if returncode < 0:
8686
# handle signal kills like SIGSEGV (segmentation fault)
8787
sig = signal.Signals(-returncode)

0 commit comments

Comments
 (0)