# Path: pyroSAR/gamma/auxil.py
# def process(cmd, outdir=None, logfile=None, logpath=None, inlist=None, void=True, shellscript=None):
#     """
#     wrapper function to execute GAMMA commands via module :mod:`subprocess`
#     
#     Parameters
#     ----------
#     cmd: list[str]
#         the command line arguments
#     outdir: str
#         the directory to execute the command in
#     logfile: str
#         a file to write the command log to; overrides parameter logpath
#     logpath: str
#         a directory to write logfiles to; the file will be named {GAMMA command}.log, e.g. gc_map.log;
#         is overridden by parameter logfile
#     inlist: list
#         a list of values, which is passed as interactive inputs via stdin
#     void: bool
#         return the stdout and stderr messages?
#     shellscript: str
#         a file to write the GAMMA commands to in shell format
#     
#     Returns
#     -------
#     tuple of str or None
#         the stdout and stderr messages if void is False, otherwise None
#     """
#     if logfile is not None:
#         log = logfile
#     else:
#         log = os.path.join(logpath, os.path.basename(cmd[0]) + '.log') if logpath else None
#     gamma_home = ExamineGamma().home
#     if shellscript is not None:
#         if not os.path.isfile(shellscript):
#             # create an empty file
#             with open(shellscript, 'w') as init:
#                 pass
#         line = ' '.join([str(x) for x in dissolve(cmd)])
#         if inlist is not None:
#             line += ' <<< $"{}"'.format('\n'.join([str(x) for x in inlist]) + '\n')
#         with open(shellscript, 'r+') as sh:
#             if outdir is not None:
#                 content = sh.read()
#                 sh.seek(0)
#                 is_new = re.search('this script was created automatically by pyroSAR', content) is None
#                 if is_new:
#                     ts = datetime.now().strftime('%a %b %d %H:%M:%S %Y')
#                     sh.write('# this script was created automatically by pyroSAR on {}\n\n'.format(ts))
#                     sh.write('export base={}\n'.format(outdir))
#                     sh.write('export GAMMA_HOME={}\n\n'.format(gamma_home))
#                     sh.write(content)
#                 line = line.replace(outdir, '$base').replace(gamma_home, '$GAMMA_HOME')
#             sh.seek(0, 2)  # set pointer to the end of the file
#             sh.write(line + '\n\n')
#     
#     # create an environment containing the locations of all GAMMA submodules to be passed to the subprocess calls
#     gammaenv = os.environ.copy()
#     gammaenv['GAMMA_HOME'] = gamma_home
#     out, err = run([ExamineGamma().gdal_config, '--datadir'], void=False)
#     gammaenv['GDAL_DATA'] = out.strip()
#     for module in ['DIFF', 'DISP', 'IPTA', 'ISP', 'LAT']:
#         loc = os.path.join(gammaenv['GAMMA_HOME'], module)
#         if os.path.isdir(loc):
#             gammaenv[module + '_HOME'] = loc
#             for submodule in ['bin', 'scripts']:
#                 subloc = os.path.join(loc, submodule)
#                 if os.path.isdir(subloc):
#                     gammaenv['PATH'] += os.pathsep + subloc
#     
#     # execute the command
#     out, err = run(cmd, outdir=outdir, logfile=log, inlist=inlist, void=False, errorpass=True, env=gammaenv)
#     gammaErrorHandler(out, err)
#     if not void:
#         return out, err


from pyroSAR.gamma.auxil import process
    process(['/usr/local/GAMMA_SOFTWARE-20180703/ISP/bin/dcomp_sirc_quad', infile, outfile, samples, parameter, loff,
             nlines], logpath=logpath, outdir=outdir, shellscript=shellscript)


def DELFT_vec2(SLC_par, DELFT_dir, nstate='-', interval='-', ODR='-', logpath=None, outdir=None, shellscript=None):
    """
    | Extract and interpolate DELFT ERS-1, ERS-2, and ENVISAT state vectors
    | Copyright 2012, Gamma Remote Sensing, v2.6 clw 24-Oct-2012

    Parameters
    ----------
    SLC_par:
        (input) ISP image parameter file
    DELFT_dir:
        directory containing Delft orbit arclist and ODR files for ERS-1, ERS-2 or ENVISAT
            * NOTE: enter . for current directory

    nstate:
        number of state vectors to generate (enter - for default (>= 15)
    interval:
        time interval between state vectors in the ISP image parameter file (s) (default: 10.0)
    ODR:
        ODR file to use (include path) rather than ODR file determined from the Delft orbit arclist
    logpath: str or None
        a directory to write command logfiles to
    outdir: str or None
        the directory to execute the command in
    shellscript: str or None
        a file to write the Gamma commands to in shell format
    """

# Path: pyroSAR/gamma/auxil.py
# def process(cmd, outdir=None, logfile=None, logpath=None, inlist=None, void=True, shellscript=None):
#     """
#     wrapper function to execute GAMMA commands via module :mod:`subprocess`
#     
#     Parameters
#     ----------
#     cmd: list[str]
#         the command line arguments
#     outdir: str
#         the directory to execute the command in
#     logfile: str
#         a file to write the command log to; overrides parameter logpath
#     logpath: str
#         a directory to write logfiles to; the file will be named {GAMMA command}.log, e.g. gc_map.log;
#         is overridden by parameter logfile
#     inlist: list
#         a list of values, which is passed as interactive inputs via stdin
#     void: bool
#         return the stdout and stderr messages?
#     shellscript: str
#         a file to write the GAMMA commands to in shell format
#     
#     Returns
#     -------
#     tuple of str or None
#         the stdout and stderr messages if void is False, otherwise None
#     """
#     if logfile is not None:
#         log = logfile
#     else:
#         log = os.path.join(logpath, os.path.basename(cmd[0]) + '.log') if logpath else None
#     gamma_home = ExamineGamma().home
#     if shellscript is not None:
#         if not os.path.isfile(shellscript):
#             # create an empty file
#             with open(shellscript, 'w') as init:
#                 pass
#         line = ' '.join([str(x) for x in dissolve(cmd)])
#         if inlist is not None:
#             line += ' <<< $"{}"'.format('\n'.join([str(x) for x in inlist]) + '\n')
#         with open(shellscript, 'r+') as sh:
#             if outdir is not None:
#                 content = sh.read()
#                 sh.seek(0)
#                 is_new = re.search('this script was created automatically by pyroSAR', content) is None
#                 if is_new:
#                     ts = datetime.now().strftime('%a %b %d %H:%M:%S %Y')
#                     sh.write('# this script was created automatically by pyroSAR on {}\n\n'.format(ts))
#                     sh.write('export base={}\n'.format(outdir))
#                     sh.write('export GAMMA_HOME={}\n\n'.format(gamma_home))
#                     sh.write(content)
#                 line = line.replace(outdir, '$base').replace(gamma_home, '$GAMMA_HOME')
#             sh.seek(0, 2)  # set pointer to the end of the file
#             sh.write(line + '\n\n')
#     
#     # create an environment containing the locations of all GAMMA submodules to be passed to the subprocess calls
#     gammaenv = os.environ.copy()
#     gammaenv['GAMMA_HOME'] = gamma_home
#     out, err = run([ExamineGamma().gdal_config, '--datadir'], void=False)
#     gammaenv['GDAL_DATA'] = out.strip()
#     for module in ['DIFF', 'DISP', 'IPTA', 'ISP', 'LAT']:
#         loc = os.path.join(gammaenv['GAMMA_HOME'], module)
#         if os.path.isdir(loc):
#             gammaenv[module + '_HOME'] = loc
#             for submodule in ['bin', 'scripts']:
#                 subloc = os.path.join(loc, submodule)
#                 if os.path.isdir(subloc):
#                     gammaenv['PATH'] += os.pathsep + subloc
#     
#     # execute the command
#     out, err = run(cmd, outdir=outdir, logfile=log, inlist=inlist, void=False, errorpass=True, env=gammaenv)
#     gammaErrorHandler(out, err)
#     if not void:
#         return out, err


# Path: pyroSAR/gamma/parser_demo.py
from pyroSAR.gamma.auxil import process

    process(['/usr/local/GAMMA_SOFTWARE-20180703/ISP/bin/dcomp_sirc_quad', infile, outfile, samples, parameter, loff,
             nlines], logpath=logpath, outdir=outdir, shellscript=shellscript)


def DELFT_vec2(SLC_par, DELFT_dir, nstate='-', interval='-', ODR='-', logpath=None, outdir=None, shellscript=None):
    """
    | Extract and interpolate DELFT ERS-1, ERS-2, and ENVISAT state vectors
    | Copyright 2012, Gamma Remote Sensing, v2.6 clw 24-Oct-2012

    Parameters
    ----------
    SLC_par:
        (input) ISP image parameter file
    DELFT_dir:
        directory containing Delft orbit arclist and ODR files for ERS-1, ERS-2 or ENVISAT
            * NOTE: enter . for current directory

    nstate:
        number of state vectors to generate (enter - for default (>= 15)
    interval:
        time interval between state vectors in the ISP image parameter file (s) (default: 10.0)
    ODR:
        ODR file to use (include path) rather than ODR file determined from the Delft orbit arclist
    logpath: str or None
        a directory to write command logfiles to
    outdir: str or None
        the directory to execute the command in
    shellscript: str or None
        a file to write the Gamma commands to in shell format
    """

    process(['/usr/local/GAMMA_SOFTWARE-20180703/ISP/bin/DELFT_vec2', SLC_par, DELFT_dir, nstate, interval, ODR],