Esempio n. 1
0
def call_ssara_dem(inps, cwd):
    print('DEM generation using SSARA')
    sys.stdout.flush()
    out_file = 'ssara_dem.log'

    # need to refactor so that Josh's dataset_template will be used throughout
    ssaraopt_string = inps.ssaraopt
    ssaraopt_list = ssaraopt_string.split(' ')
    ssaraopt_list = add_polygon_to_ssaraopt(dataset_template=inps.template,
                                            ssaraopt=ssaraopt_list.copy(),
                                            delta_lat=0)
    ssaraopt_string = ' '.join(ssaraopt_list)

    out_file = 'out_ssara_dem'
    command = 'ssara_federated_query.py {ssaraopt} --dem '.format(
        ssaraopt=ssaraopt_string)
    message_rsmas.log(os.getcwd(), command)
    command = '(' + command + ' | tee ' + out_file + '.o) 3>&1 1>&2 2>&3 | tee ' + out_file + '.e'
    print('command currently executing: ' + command)
    sys.stdout.flush()
    if os.getenv('DOWNLOADHOST') == 'local':
        print('Command: ' + command)
        sys.stdout.flush()
        try:
            proc = subprocess.Popen(command,
                                    stderr=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    shell=True,
                                    universal_newlines=True)
            error, output = proc.communicate(
            )  # FA 8/19 error, output works better here than output, error. Could be that stderr and stdout are switched .
            if proc.returncode is not 0:
                raise Exception(
                    'ERROR starting dem.py subprocess'
                )  # FA 8/19: I don't think this happens, errors are is output
        except subprocess.CalledProcessError as exc:
            print("Command failed. Exit code, StdErr:", exc.returncode,
                  exc.output)
            sys.exit('Error produced by ssara_federated_query.py')
        else:
            if not 'Downloading DEM' in output:
                os.chdir('..')
                shutil.rmtree('DEM')
                sys.exit('Error in dem.py: Tiles are missing. Ocean???')
    else:
        dem_dir = os.getcwd()
        ssh_command_list = ['s.bgood', 'cd {0}'.format(cwd), command]
        host = os.getenv('DOWNLOADHOST')
        status = ssh_with_commands(host, ssh_command_list)
        print('status from ssh_with_commands:' + str(status))
        sys.stdout.flush()

    print('Done downloading dem.grd')
    sys.stdout.flush()
    grd_to_envi_and_vrt()
    grd_to_xml(cwd)
Esempio n. 2
0
def main(iargs=None):

    # set defaults: ssara=True is set in dem_parser, use custom_pemp[late field if given
    inps = cmd_line_parse(iargs, script='dem_rsmas')

    if not iargs is None:
        message_rsmas.log(inps.work_dir, os.path.basename(__file__) + ' ' + ' '.join(iargs[:]))
    else:
        message_rsmas.log(inps.work_dir, os.path.basename(__file__) + ' ' + ' '.join(sys.argv[1::]))

    if not inps.flag_boundingBox and not inps.flag_ssara:
        if 'demMethod' in list(inps.template.keys()):
            if inps.template['demMethod'] == 'ssara':
                inps.flag_ssara = True
                inps.flag_boundingBox = False
            if inps.template['demMethod'] == 'boundingBox':
                inps.flag_ssara = False
                inps.flag_boundingBox = True
    elif inps.flag_boundingBox:
        inps.flag_ssara = False
    else:
        inps.flag_ssara = True

    dem_dir = make_dem_dir(inps.work_dir)

    if dem_dir:

        if inps.flag_ssara:

            call_ssara_dem(inps, dem_dir)

            print('You have finished SSARA!')
        elif inps.flag_boundingBox:
            print('DEM generation using ISCE')
            bbox = inps.template['topsStack.boundingBox'].strip("'")
            bbox = [val for val in bbox.split()]
            south = bbox[0]
            north = bbox[1]
            west = bbox[2]
            east = bbox[3].split('\'')[0]

            south = math.floor(float(south) - 0.5)
            north = math.ceil(float(north) + 0.5)
            west = math.floor(float(west) - 0.5)
            east = math.ceil(float(east) + 0.5 )

            demBbox = str(int(south)) + ' ' + str(int(north)) + ' ' + str(int(west)) + ' ' + str(int(east))
            command = 'dem.py -a stitch -b ' + demBbox + ' -c -u https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/'
            message_rsmas.log(os.getcwd(), command)

            if os.getenv('DOWNLOADHOST') == 'local':
                try:
                    proc = subprocess.Popen(command,  stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
                    output, error = proc.communicate()
                    if proc.returncode is not 0:
                        raise Exception('ERROR starting dem.py subprocess')   # FA 8/19: I don't think this happens, errors are is output
                except subprocess.CalledProcessError as exc:
                    print("Command failed. Exit code, StdErr:", exc.returncode, exc.output)
                    sys.exit('Error produced by dem.py')
                else:
                    if 'Could not create a stitched DEM. Some tiles are missing' in output:
                        os.chdir('..')
                        shutil.rmtree('DEM')
                        sys.exit('Error in dem.py: Tiles are missing. Ocean???')

            else:
                dem_dir = os.getcwd()
                ssh_command_list = ['s.bgood', 'cd {0}'.format(dem_dir), command]
                host = os.getenv('DOWNLOADHOST')
                try:
                    status = ssh_with_commands(host, ssh_command_list)
                except subprocess.CalledProcessError as exc:
                    print("Command failed. Exit code, StdErr:", exc.returncode, exc.output)
                    sys.exit('Error produced by dem.py using ' + host)

            #print('Exit status from dem.py: {0}'.format(status))

            xmlFile = glob.glob('demLat_*.wgs84.xml')[0]

            fin = open(xmlFile, 'r')
            fout = open("tmp.txt", "wt")
            for line in fin:
                fout.write(line.replace('demLat', dem_dir + '/demLat'))
            fin.close()
            fout.close()
            os.rename('tmp.txt', xmlFile)

        else:
            sys.ext('Error unspported demMethod option: ' + inps.template['topsStack.demMethod'])

        print('\n###############################################')
        print('End of dem_rsmas.py')
        print('################################################\n')

    return None