@@ -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
0 commit comments