Пример #1
0
def main(argv):
    if len(sys.argv) < 3:
        usage()
        sys.exit(1)

    lat = float(argv[0])
    lon = float(argv[1])

    try:
        trans_file = argv[2]
    except:
        trans_file = ut.get_lookup_file()

    try:
        radar_file = argv[3]
    except:
        radar_file = 'unwrapIfgram.h5'

    atr_rdr = readfile.read_attribute(radar_file)

    print 'input geo coord: lat=%.4f, lon=%.4f' % (lat, lon)

    y, x = ut.glob2radar(np.array(lat), np.array(lon), trans_file,
                         atr_rdr)[0:2]
    print 'corresponding radar coord: y=%d, x=%d' % (y, x)

    return
Пример #2
0
def bbox_geo2radar(geo_box, atr_rdr=dict(), transFile='geomap*.trans'):
    '''Calculate bounding box in x/y for file in radar coord, based on input geo box.
    Inputs:
        geo_box   - tuple of 4 float, indicating the UL/LR lon/lat 
        atr_rdr   - dict, attributes of file in radar coord
        transFile - string, path of transformation file, i.e. geomap_4rlks.trans
    Output:
        pix_box - tuple of 4 int, indicating the UL/LR x/y of the bounding box in radar coord
                  for the corresponding lat/lon coverage.
    '''
    lat = np.array([geo_box[3], geo_box[3], geo_box[1], geo_box[1]])
    lon = np.array([geo_box[0], geo_box[2], geo_box[0], geo_box[2]])
    y, x, y_res, x_res = ut.glob2radar(lat, lon, transFile, atr_rdr)
    buf = 10 * (np.max([x_res, y_res]))
    pix_box = (np.min(x) - buf, np.min(y) - buf, np.max(x) + buf,
               np.max(y) + buf)
    return pix_box
Пример #3
0
def bbox_geo2radar(geo_box, atr_rdr=dict(), lookupFile=None):
    '''Calculate bounding box in x/y for file in radar coord, based on input geo box.
    Inputs:
        geo_box   - tuple of 4 float, indicating the UL/LR lon/lat 
        atr_rdr   - dict, attributes of file in radar coord
        lookupFile - string, path of transformation file, i.e. geomap_4rlks.trans
    Output:
        pix_box - tuple of 4 int, indicating the UL/LR x/y of the bounding box in radar coord
                  for the corresponding lat/lon coverage.
    '''
    lat = np.array([geo_box[3], geo_box[3], geo_box[1], geo_box[1]])
    lon = np.array([geo_box[0], geo_box[2], geo_box[0], geo_box[2]])
    if 'Y_FIRST' in atr_rdr.keys():
        y = coord_geo2radar(lat, atr_rdr, 'lat')
        x = coord_geo2radar(lon, atr_rdr, 'lon')
        pix_box = (x[0], y[2], x[1], y[0])
    else:
        y, x, y_res, x_res = ut.glob2radar(lat, lon, lookupFile, atr_rdr)
        buf = 2 * (np.max(np.abs([x_res, y_res])))
        pix_box = (np.min(x) - buf, np.min(y) - buf, np.max(x) + buf,
                   np.max(y) + buf)
    return pix_box
Пример #4
0
def main(argv):
    inps = cmdLineParse()
    inps.file = ut.get_file_list(inps.file)

    atr = readfile.read_attribute(inps.file[0])
    length = int(atr['FILE_LENGTH'])
    width = int(atr['WIDTH'])

    if inps.reset:
        print '----------------------------------------------------------------------------'
        for file in inps.file:
            remove_reference_pixel(file)
        return

    ##### Check Input Coordinates
    # Read ref_y/x/lat/lon from reference/template
    # priority: Direct Input > Reference File > Template File
    if inps.template_file:
        print 'reading reference info from template: ' + inps.template_file
        inps = read_seed_template2inps(inps.template_file, inps)
    if inps.reference_file:
        print 'reading reference info from reference: ' + inps.reference_file
        inps = read_seed_reference2inps(inps.reference_file, inps)

    ## Do not use ref_lat/lon input for file in radar-coord
    #if not 'X_FIRST' in atr.keys() and (inps.ref_lat or inps.ref_lon):
    #    print 'Lat/lon reference input is disabled for file in radar coord.'
    #    inps.ref_lat = None
    #    inps.ref_lon = None

    # Convert ref_lat/lon to ref_y/x
    if inps.ref_lat and inps.ref_lon:
        if 'X_FIRST' in atr.keys():
            inps.ref_y = subset.coord_geo2radar(inps.ref_lat, atr, 'lat')
            inps.ref_x = subset.coord_geo2radar(inps.ref_lon, atr, 'lon')
        else:
            # Convert lat/lon to az/rg for radar coord file using geomap*.trans file
            inps.ref_y, inps.ref_x = ut.glob2radar(np.array(inps.ref_lat), np.array(inps.ref_lon),\
                                                   inps.trans_file, atr)[0:2]
        print 'Input reference point in lat/lon: ' + str(
            [inps.ref_lat, inps.ref_lon])
    print 'Input reference point in   y/x  : ' + str([inps.ref_y, inps.ref_x])

    # Do not use ref_y/x outside of data coverage
    if (inps.ref_y and inps.ref_x
            and not (0 <= inps.ref_y <= length and 0 <= inps.ref_x <= width)):
        inps.ref_y = None
        inps.ref_x = None
        print 'WARNING: input reference point is OUT of data coverage!'
        print 'Continue with other method to select reference point.'

    # Do not use ref_y/x in masked out area
    if inps.ref_y and inps.ref_x and inps.mask_file:
        print 'mask: ' + inps.mask_file
        mask = readfile.read(inps.mask_file)[0]
        if mask[inps.ref_y, inps.ref_x] == 0:
            inps.ref_y = None
            inps.ref_x = None
            print 'WARNING: input reference point is in masked OUT area!'
            print 'Continue with other method to select reference point.'

    ##### Select method
    if inps.ref_y and inps.ref_x:
        inps.method = 'input-coord'
    elif inps.coherence_file:
        if os.path.isfile(inps.coherence_file):
            inps.method = 'max-coherence'
        else:
            inps.coherence_file = None

    if inps.method == 'manual':
        inps.parallel = False
        print 'Parallel processing is disabled for manual seeding method.'

    ##### Seeding file by file
    # check outfile and parallel option
    if inps.parallel:
        num_cores, inps.parallel, Parallel, delayed = ut.check_parallel(
            len(inps.file))

    if len(inps.file) == 1:
        seed_file_inps(inps.file[0], inps, inps.outfile)

    elif inps.parallel:
        #num_cores = min(multiprocessing.cpu_count(), len(inps.file))
        #print 'parallel processing using %d cores ...'%(num_cores)
        Parallel(n_jobs=num_cores)(delayed(seed_file_inps)(file, inps)
                                   for file in inps.file)
    else:
        for File in inps.file:
            seed_file_inps(File, inps)

    print 'Done.'
    return
Пример #5
0
def main(argv):
  start = time.time()

  try:     templateFile = argv[1]
  except:  Usage(); sys.exit(1)

  ###########  Path  ############
  projectName = os.path.basename(templateFile).partition('.')[0]
  try:     tssarProjectDir = os.getenv('TSSARDIR') +'/'+projectName
  except:  tssarProjectDir = os.getenv('SCRATCHDIR') + '/' + projectName + "/TSSAR"     # FA 7/2015: adopted for new directory structure
  print "\nprojectName: ", projectName
  print "QQ " + tssarProjectDir
  if not os.path.isdir(tssarProjectDir): os.mkdir(tssarProjectDir)
  os.chdir(tssarProjectDir)

  ##########  Initial File Name  ########
  import h5py
  import pysar._pysar_utilities as ut
  import pysar._readfile as readfile
  template = readfile.read_template(templateFile)

  igramFile = 'LoadedData.h5'
  if os.path.isfile('Modified_'+igramFile):  igramFile = 'Modified_'+igramFile
  corFile   = 'Coherence.h5'
  if os.path.isfile('Modified_'+corFile):    corFile   = 'Modified_'+corFile
  maskFile  = 'Mask.h5'
  if os.path.isfile('Modified_'+maskFile):   maskFile  = 'Modified_'+maskFile

#########################################
# Loading Data
#########################################
  print '\n**********  Loading Data  *****************************'
  if os.path.isfile(igramFile):
    print igramFile + ' already exists.'
  else:
    loadCmd='load_data.py ' + templateFile 
    print loadCmd
    os.system(loadCmd)
    #copyDemCmd='copy_dem_trans.py ' + templateFile
    #print copyDemCmd
    #os.system(copyDemCmd)

  if not os.path.isfile(igramFile): sys.exit('\nERROR: No interferograms file found!\n')

  ##########  Initial File Name - 2  ####
  try:  demGeoFile = find_filename(template, 'pysar.dem.geoCoord')
  except: print '\nWARNING:\n    No geo coded DEM found! Might be a problem in tropospheric delay / orbital error correction!\n'
  try:  demRdrFile = find_filename(template, 'pysar.dem.radarCoord')
  except: print '\nWARNING:\n    No radar coded DEM found! Will be a problem in tropospheric delay / orbital error correction!\n'
  try:  geomapFile = find_filename(template, 'pysar.geomap')
  except: print '\nWARNING:\n    No geomap*.trans file found! Will be a problem in geocoding!\n'


#########################################
# Check the subset (Optional)
#########################################
  print '\n**********  Subseting  ********************************'

  if   'pysar.subset.yx' in template.keys():
    print 'subseting data with y/x input...'
    subset = template['pysar.subset.yx'].split(',')

    print 'calculating bounding box in geo coordinate.'
    import numpy as np
    ysub = [float(i) for i in subset[0].split(':')];  ysub.sort()
    xsub = [float(i) for i in subset[1].split(':')];  xsub.sort()
    x = np.array([xsub[0],xsub[1],xsub[0],xsub[1]])
    y = np.array([ysub[0],ysub[0],ysub[1],ysub[1]])
    lat,lon,lat_res,lon_res = ut.radar2glob(x,y,igramFile,1)
    buf = 10*(np.max([lat_res,lon_res]))
    latsub = str(np.min(lat)-buf)+':'+str(np.max(lat)+buf)
    lonsub = str(np.min(lon)-buf)+':'+str(np.max(lon)+buf)
    print '    subset in y - '+subset[0]+'\n    subset in x - '+subset[1]
    print '    subset in lat - '+latsub +'\n    subset in lon - '+lonsub

    ## subset radar coded file
    igramFile  = check_subset(igramFile, subset, option='yx')
    maskFile   = check_subset(maskFile,  subset, option='yx')
    try:    demRdrFile = check_subset(demRdrFile,subset, option='yx')
    except: pass
    ## subset geo coded file
    try:    demGeoFile = check_subset(demGeoFile,[latsub,lonsub], option='lalo')
    except: pass
    try:    geomapFile = check_subset(geomapFile,[latsub,lonsub], option='lalo')
    except: pass

  elif 'pysar.subset.lalo' in template.keys():
    print 'subseting data with lat/lon input...'
    subset= template['pysar.subset.lalo'].split(',')

    print 'calculate bounding box in radar coordinate.'
    import numpy as np
    latsub = [float(i) for i in subset[0].split(':')];  latsub.sort()
    lonsub = [float(i) for i in subset[1].split(':')];  lonsub.sort()
    lat = np.array([latsub[0],latsub[0],latsub[1],latsub[1]])
    lon = np.array([lonsub[0],lonsub[1],lonsub[0],lonsub[1]])
    x,y,x_res,y_res = ut.glob2radar(lat,lon)
    buf = 10*(np.max([x_res,y_res]))
    xsub = str(np.min(x)-buf)+':'+str(np.max(x)+buf)
    ysub = str(np.min(y)-buf)+':'+str(np.max(y)+buf)
    print '    subset in lat - '+subset[0]+'\n    subset in lon - '+subset[1]
    print '    subset in y - '  +ysub     +'\n    subset in x - '+xsub

    ## subset geo coded files
    try:    demGeoFile = check_subset(demGeoFile, subset, option='lalo')
    except: pass
    try:    geomapFile = check_subset(geomapFile, subset, option='lalo')
    except: pass
    ## subset radar coded files
    igramFile  = check_subset(igramFile, [ysub,xsub], option='yx')
    maskFile   = check_subset(maskFile,  [ysub,xsub], option='yx')
    try:    demRdrFile = check_subset(demRdrFile,[ysub,xsub], option='yx')
    except: pass

  else: print 'No Subset selected. Processing the whole area'


  #try:
  #  subset= template['pysar.subset.yx'].split(',')
  #  print 'subset in y - '+str(subset[0])
  #  print 'subset in x - '+str(subset[1])
  #  igramFile  = check_subset(igramFile, subset)
  #  corFile    = check_subset(corFile,   subset)
  #  maskFile   = check_subset(maskFile,  subset)
  #  demRdrFile = check_subset(demRdrFile,subset)
  #  demGeoFile = check_subset(demGeoFile,subset)
  #  #geomapFile = check_subset(geomapFile,subset)
  #except:  print   'No Subset selected. Processing the whole area'


#########################################
# Referencing Interferograms
#########################################
  print '\n**********  Referencing Interferograms  ***************'
  if os.path.isfile('Seeded_'+igramFile):
    igramFile = 'Seeded_'+igramFile
    print igramFile + ' already exists.'
  else:
    print 'referncing all interferograms to the same pixel.'
    if 'pysar.seed.lalo' in template.keys():
       'Checking lat/lon refernce point' 
       lat= template['pysar.seed.lalo'].split(',')[0]
       lon= template['pysar.seed.lalo'].split(',')[1]
       seedCmd= 'seed_data.py -f ' + igramFile + ' -l ' +lat+ ' -L '+lon
    elif 'pysar.seed.yx' in template.keys():
       'Checking y/x reference point'
       y= template['pysar.seed.yx'].split(',')[0]
       x= template['pysar.seed.yx'].split(',')[1]
       seedCmd= 'seed_data.py -f ' + igramFile + ' -y ' +y+ ' -x '+x
    else: 
       seedCmd= 'seed_data.py -f ' + igramFile

    igramFile = 'Seeded_'+igramFile
    print seedCmd  
    os.system(seedCmd)


############################################
# Unwrapping Error Correction (Optional)
#    based on the consistency of triplets
#    of interferograms  
############################################
  print '\n**********  Unwrapping Error Correction  **************'
  outName = igramFile.split('.')[0]+'_unwCor.h5'
  try:
    template['pysar.unwrapError']
    if template['pysar.unwrapError'] in ('Y','y','yes','Yes','YES'):
      if os.path.isfile(outName):
        igramFile = outName
        print igramFile+' exists.'
      else:
        print 'This might take a while depending on the size of your data set!'
        unwCmd='unwrap_error.py '+igramFile
        os.system(unwCmd)
        igramFile = outName
    else:  print 'No unwrapping error correction.'
  except:  print 'No unwrapping error correction.'


#########################################
# Inversion of Interferograms
########################################
  print '\n**********  Time Series Inversion  ********************'
  if os.path.isfile('timeseries.h5'):
     print 'timeseries.h5 already exists, inversion is not needed.'
  else:
     invertCmd = 'igram_inversion.py '+ igramFile 
     print invertCmd
     os.system(invertCmd)
  timeseriesFile='timeseries.h5'


##############################################
# Temporal Coherence: 
#   A parameter to evaluate the consistency 
#   of timeseries with the interferograms
##############################################
  print '\n**********  Generate Temporal Coherence file  *********'
  if os.path.isfile('temporal_coherence.h5'):
     print 'temporal_coherence.h5 already exists.'
  else:
     tempcohCmd='temporal_coherence.py '+igramFile+' '+timeseriesFile
     print tempcohCmd
     os.system(tempcohCmd)
  tempCohFile = 'temporal_coherence.h5'


##############################################
# Update Mask based on temporal coherence (Optional)
##############################################
  print '\n**********  Update Mask based on Temporal Coherence  **'
  outName = maskFile.split('.')[0]+'_tempCoh.h5'
  try:
     template['pysar.mask']
     if template['pysar.mask'] in ('Y','yes','Yes','YES','y'):
        if os.path.isfile(outName):
           maskFile = outName
           print maskFile+' already exists.'
        else:
           try:    cohT = template['pysar.mask.threshold']
           except: cohT = '0.7'
           maskCmd='generate_mask.py -f '+tempCohFile+' -m '+ cohT +' -M 1.0 -o '+outName
           print maskCmd
           os.system(maskCmd)
           maskFile = outName
     else:  print 'No mask update from temporal coherence'
  except:   print 'No mask update from temporal coherence'


##############################################
# Incident Angle
##############################################
  print '\n**********  Generate Incident Angle file  *************'
  if os.path.isfile('incidence_angle.h5'):
     print 'incidence_angle.h5 already exists.'
  else:
     inciCmd = 'incidence_angle.py -f '+timeseriesFile
     print inciCmd
     os.system(inciCmd)


##############################################
# LOD (Local Oscillator Drift) Correction
#   when Satellite is Envisat and
#   Coordinate system is radar
##############################################
  print '\n**********  Local Oscillator Drift correction  ********'
  rdr_or_geo = ut.radar_or_geo(timeseriesFile)
  outName = timeseriesFile.split('.')[0]+'_LODcor.h5'
  if os.path.isfile(outName):
     timeseriesFile = outName
     print timeseriesFile+' already exists.'
  else:
    h5file   = h5py.File(timeseriesFile,'r')
    platform = h5file['timeseries'].attrs['PLATFORM']
    if platform == 'ENVISAT':
       if rdr_or_geo == 'radar':
          LODcmd='lod.py '+timeseriesFile
          print LODcmd
          os.system(LODcmd)
          timeseriesFile = outName
       else: print 'Cannot correct LOD for geocoded data.'
    else: print 'No need of LOD correction for '+platform
    h5file.close()


##############################################
# Tropospheric Delay Correction (Optional)
##############################################
  print '\n**********  Tropospheric Correction  ******************'
  try:
     if (template['pysar.troposphericDelay'] in ('Y','y','yes','Yes','YES')) and\
         template['pysar.orbitError.method'] in ['BaseTropCor','basetropcor','base_trop_cor']:
        print '''
   +++++++++++++++++++++++++++++++++++++++++++++++++++
   WARNING:
       Orbital error correction was BaseTropCor.
       Tropospheric correction was already applied simultaneous with baseline error correction.
       Tropospheric correction can not be applied again.
       To apply the tropospheric correction separated from baseline error correction, \
          choose other existing options for orbital error correction.
    +++++++++++++++++++++++++++++++++++++++++++++++++++      
        '''
        template['pysar.troposphericDelay']='no'
  except:  print 'Checking the tropospheric delay correction ...'

  if template['pysar.troposphericDelay'] in ('Y','y','yes','Yes','YES'):     
     if   rdr_or_geo == 'radar':  demFile = demRdrFile
     elif rdr_or_geo == 'geo':    demFile = demGeoFile

     if not os.path.isfile(demFile):
        print '++++++++++++++++++++++++++++++++++++++++++++++'
        print 'ERROR:'
        print '    DEM file was not found!'
        print '    Continue without tropospheric correction ...'
        print '++++++++++++++++++++++++++++++++++++++++++++++'
     else:
        if template['pysar.troposphericDelay.method'] in ['height-correlation','height_correlation',\
                                                          'Height-Correlation','Height_Correlation']:
           print 'tropospheric delay correction with height-correlation approach'
           outName = timeseriesFile.split('.')[0]+'_tropCor.h5'
           if os.path.isfile(outName):
              timeseriesFile = outName
              print timeseriesFile+' already exists.'
           else:
              try:     poly_order = template['pysar.troposphericDelay.polyOrder']
              except:  poly_order = '1';  print 'Deafult polynomial order for troposphreic correction = 1'
              cmdTrop = 'tropcor_phase_elevation.py'+' -f '+timeseriesFile+' -d '+demFile+' -p '+str(poly_order)
              print cmdTrop
              os.system(cmdTrop)
              timeseriesFile = outName

        elif template['pysar.troposphericDelay.method'] in ['pyaps','PyAPS','PYAPS']:
           print 'Atmospheric correction using Numerical Weather Models (using PyAPS software)'
           print 'reading DEM, source of NWM and acquisition time from template file'
           source_of_NWM = template['pysar.troposphericDelay.weatherModel']
           print 'Numerical Weather Model: '+source_of_NWM
           outName = timeseriesFile.split('.')[0]+'_'+source_of_NWM+'.h5'
           if os.path.isfile(outName):
              timeseriesFile = outName
              print timeseriesFile+' already exists.'
           else:
              acquisition_time = template['pysar.acquisitionTime']
              print 'acquisition time: '+acquisition_time
              cmdTrop = 'tropcor_pyaps.py -f '+timeseriesFile+' -d '+demFile+' -s '+source_of_NWM+\
                                        ' -h '+acquisition_time+' -i incidence_angle.h5'
              print cmdTrop
              os.system(cmdTrop)
              #subprocess.Popen(cmdTrop).wait()
              timeseriesFile = outName
        else:  print 'ERROR: Unrecognized atmospheric correction method: '+template['pysar.trop.method']
  else:  print 'No atmospheric delay correction.'


##############################################
# Topographic (DEM) Residuals Correction (Optional)
##############################################
  print '\n**********  Topographic (DEM) Error correction  *******'
  outName = timeseriesFile.split('.')[0]+'_demCor.h5'
  try:
     template['pysar.topoError']
     if template['pysar.topoError'] in ('Y','yes','Yes','YES','y'):
        if os.path.isfile(outName):
           timeseriesFile = outName
           print timeseriesFile+' already exists.'
        else:
           print 'Correcting topographic residuals'
           topoCmd='dem_error.py '+ timeseriesFile +' '+ igramFile
           print topoCmd
           os.system(topoCmd)
           timeseriesFile = outName
     else:  print 'No correction for topographic residuals.'
  except:   print 'No correction for topographic residuals.'


##############################################
# Orbit Correction (Optional)
##############################################
  print '\n**********  Orbital Correction  ***********************'
  try:
     template['pysar.orbitError']
     if template['pysar.orbitError'] in ('Y','yes','Yes','YES','y'):
        try:
           orbit_error_method=template['pysar.orbitError.method']
           print 'orbit error correction method : '+orbit_error_method
           outName = timeseriesFile.split('.')[0]+'_'+orbit_error_method+'.h5'
           if os.path.isfile(outName):
              timeseriesFile = outName
              print timeseriesFile+' already exists.'

           else:
              if orbit_error_method in [    'plane',     'plane_range',     'plane_azimuth',\
                                        'quadratic', 'quadratic_range', 'quadratic_azimuth']:
                 orbitCmd='remove_plane.py '+timeseriesFile+' '+orbit_error_method #+ ' Mask.h5'
                 print orbitCmd
                 os.system(orbitCmd)
                 timeseriesFile = outName

              elif orbit_error_method in ['baselineCor','BaselineCor']:
                 try:
                    h5file=h5py.File(timeseriesFile,'r')
                    daz=float(h5file['timeseries'].attrs['AZIMUTH_PIXEL_SIZE'])
                    orbitCmd='baseline_error.py ' +timeseriesFile #+ ' Mask.h5'
                    print orbitCmd
                    os.system(orbitCmd)
                    timeseriesFile = outName
                 except:
                    print 'WARNING!'
                    print 'Skipping orbital error correction.'
                    print 'baselineCor method can only be applied in radar coordinate'

              elif orbit_error_method in ['BaseTropCor','basetropcor','base_trop_cor']:
                 try:
                    h5file=h5py.File(timeseriesFile,'r')
                    daz=float(h5file['timeseries'].attrs['AZIMUTH_PIXEL_SIZE']) 
                    print 'Joint estimation of Baseline error and tropospheric delay [height-correlation approach]'
                    if   rdr_or_geo == 'radar':  demFile = demRdrFile
                    elif rdr_or_geo == 'geo':    demFile = demGeoFile
                    try:     poly_order = template['pysar.trop.poly_order']
                    except:  poly_order = 1;  print 'Deafult polynomial order for troposphreic correction = 1'
                    orbitCmd='baseline_trop.py '+timeseriesFile+' '+ demFile +' '+ str(poly_order) +' range_and_azimuth'
                    print orbitCmd
                    os.system(orbitCmd)
                    timeseriesFile = outName
                 except:
                    print 'WARNING!'
                    print 'Skipping orbital error correction.'
                    print 'baselineCor method can only be applied in radar coordinate'

              else:
                 print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++'
                 print 'WARNING!'
                 print 'Orbital error correction method was not recognized!'
                 print 'Possible options are:'
                 print '    quadratic, plane, quardatic_range, quadratic_azimiuth'
                 print '    plane_range, plane_azimuth,baselineCor,BaseTropCor'
                 print 'Continue without orbital errors correction...'
                 print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++'
        except:  print 'No orbital errors correction.'
     else:       print 'No orbital errors correction.'
  except:        print 'No orbital errors correction.'


#############################################
# Velocity and rmse maps
#############################################
  print '\n**********  Velocity estimation  **********************'
  if os.path.isfile('velocity.h5'):
    print 'velocity.h5 file already exists.'
  else:
    velCmd='timeseries2velocity.py '+timeseriesFile
    print velCmd
    os.system(velCmd)
  velocityFile = 'velocity.h5'


############################################
# Geocoding (Optional)
############################################
  print '\n**********  Geocoding  ********************************'
  try:
     template['pysar.geocode']
     if template['pysar.geocode'] in ('Y','y','yes','Yes','YES'):
        #geoTsFile      = check_geocode(timeseriesFile,geomapFile)
        geoVelocityFile = check_geocode(velocityFile,  geomapFile)
        geoMaskFile     = check_geocode(maskFile,      geomapFile)
     else:  print 'No geocoding applied'
  except:   print 'No geocoding applied'


#############################################
# Masking (Optional)
#############################################
  print '\n**********  Masking Velocity  *************************'
  velocityFile    = check_mask(   velocityFile,    maskFile)
  try:    geoVelocityFile = check_mask(geoVelocityFile, geoMaskFile)
  except: pass

#############################################
#                PySAR v1.0                 #
#############################################
  s = time.time()-start;  m, s = divmod(s, 60);  h, m = divmod(m, 60)
  print '\nTime used: %02d hours %02d mins %02d secs' % (h, m, s)
  print '\n###############################################'
  print 'End of PySAR processing!'
  print '################################################\n'
Пример #6
0
def main(argv):
    start = time.time()

    try:
        templateFile = argv[1]
    except:
        Usage()
        sys.exit(1)

    ###########  Path  ############
    projectName = os.path.basename(templateFile).partition('.')[0]
    try:
        tssarProjectDir = os.getenv('TSSARDIR') + '/' + projectName
    except:
        tssarProjectDir = os.getenv(
            'SCRATCHDIR'
        ) + '/' + projectName + "/TSSAR"  # FA 7/2015: adopted for new directory structure
    print "\nprojectName: ", projectName
    print "QQ " + tssarProjectDir
    if not os.path.isdir(tssarProjectDir): os.mkdir(tssarProjectDir)
    os.chdir(tssarProjectDir)

    ##########  Initial File Name  ########
    import h5py
    import pysar._pysar_utilities as ut
    import pysar._readfile as readfile
    template = readfile.read_template(templateFile)

    igramFile = 'LoadedData.h5'
    if os.path.isfile('Modified_' + igramFile):
        igramFile = 'Modified_' + igramFile
    corFile = 'Coherence.h5'
    if os.path.isfile('Modified_' + corFile): corFile = 'Modified_' + corFile
    maskFile = 'Mask.h5'
    if os.path.isfile('Modified_' + maskFile):
        maskFile = 'Modified_' + maskFile

    #########################################
    # Loading Data
    #########################################
    print '\n**********  Loading Data  *****************************'
    if os.path.isfile(igramFile):
        print igramFile + ' already exists.'
    else:
        loadCmd = 'load_data.py ' + templateFile
        print loadCmd
        os.system(loadCmd)
        #copyDemCmd='copy_dem_trans.py ' + templateFile
        #print copyDemCmd
        #os.system(copyDemCmd)

    if not os.path.isfile(igramFile):
        sys.exit('\nERROR: No interferograms file found!\n')

    ##########  Initial File Name - 2  ####
    try:
        demGeoFile = find_filename(template, 'pysar.dem.geoCoord')
    except:
        print '\nWARNING:\n    No geo coded DEM found! Might be a problem in tropospheric delay / orbital error correction!\n'
    try:
        demRdrFile = find_filename(template, 'pysar.dem.radarCoord')
    except:
        print '\nWARNING:\n    No radar coded DEM found! Will be a problem in tropospheric delay / orbital error correction!\n'
    try:
        geomapFile = find_filename(template, 'pysar.geomap')
    except:
        print '\nWARNING:\n    No geomap*.trans file found! Will be a problem in geocoding!\n'

    #########################################
    # Check the subset (Optional)
    #########################################
    print '\n**********  Subseting  ********************************'

    if 'pysar.subset.yx' in template.keys():
        print 'subseting data with y/x input...'
        subset = template['pysar.subset.yx'].split(',')

        print 'calculating bounding box in geo coordinate.'
        import numpy as np
        ysub = [float(i) for i in subset[0].split(':')]
        ysub.sort()
        xsub = [float(i) for i in subset[1].split(':')]
        xsub.sort()
        x = np.array([xsub[0], xsub[1], xsub[0], xsub[1]])
        y = np.array([ysub[0], ysub[0], ysub[1], ysub[1]])
        lat, lon, lat_res, lon_res = ut.radar2glob(x, y, igramFile, 1)
        buf = 10 * (np.max([lat_res, lon_res]))
        latsub = str(np.min(lat) - buf) + ':' + str(np.max(lat) + buf)
        lonsub = str(np.min(lon) - buf) + ':' + str(np.max(lon) + buf)
        print '    subset in y - ' + subset[
            0] + '\n    subset in x - ' + subset[1]
        print '    subset in lat - ' + latsub + '\n    subset in lon - ' + lonsub

        ## subset radar coded file
        igramFile = check_subset(igramFile, subset, option='yx')
        maskFile = check_subset(maskFile, subset, option='yx')
        try:
            demRdrFile = check_subset(demRdrFile, subset, option='yx')
        except:
            pass
        ## subset geo coded file
        try:
            demGeoFile = check_subset(demGeoFile, [latsub, lonsub],
                                      option='lalo')
        except:
            pass
        try:
            geomapFile = check_subset(geomapFile, [latsub, lonsub],
                                      option='lalo')
        except:
            pass

    elif 'pysar.subset.lalo' in template.keys():
        print 'subseting data with lat/lon input...'
        subset = template['pysar.subset.lalo'].split(',')

        print 'calculate bounding box in radar coordinate.'
        import numpy as np
        latsub = [float(i) for i in subset[0].split(':')]
        latsub.sort()
        lonsub = [float(i) for i in subset[1].split(':')]
        lonsub.sort()
        lat = np.array([latsub[0], latsub[0], latsub[1], latsub[1]])
        lon = np.array([lonsub[0], lonsub[1], lonsub[0], lonsub[1]])
        x, y, x_res, y_res = ut.glob2radar(lat, lon)
        buf = 10 * (np.max([x_res, y_res]))
        xsub = str(np.min(x) - buf) + ':' + str(np.max(x) + buf)
        ysub = str(np.min(y) - buf) + ':' + str(np.max(y) + buf)
        print '    subset in lat - ' + subset[
            0] + '\n    subset in lon - ' + subset[1]
        print '    subset in y - ' + ysub + '\n    subset in x - ' + xsub

        ## subset geo coded files
        try:
            demGeoFile = check_subset(demGeoFile, subset, option='lalo')
        except:
            pass
        try:
            geomapFile = check_subset(geomapFile, subset, option='lalo')
        except:
            pass
        ## subset radar coded files
        igramFile = check_subset(igramFile, [ysub, xsub], option='yx')
        maskFile = check_subset(maskFile, [ysub, xsub], option='yx')
        try:
            demRdrFile = check_subset(demRdrFile, [ysub, xsub], option='yx')
        except:
            pass

    else:
        print 'No Subset selected. Processing the whole area'

    #try:
    #  subset= template['pysar.subset.yx'].split(',')
    #  print 'subset in y - '+str(subset[0])
    #  print 'subset in x - '+str(subset[1])
    #  igramFile  = check_subset(igramFile, subset)
    #  corFile    = check_subset(corFile,   subset)
    #  maskFile   = check_subset(maskFile,  subset)
    #  demRdrFile = check_subset(demRdrFile,subset)
    #  demGeoFile = check_subset(demGeoFile,subset)
    #  #geomapFile = check_subset(geomapFile,subset)
    #except:  print   'No Subset selected. Processing the whole area'

    #########################################
    # Referencing Interferograms
    #########################################
    print '\n**********  Referencing Interferograms  ***************'
    if os.path.isfile('Seeded_' + igramFile):
        igramFile = 'Seeded_' + igramFile
        print igramFile + ' already exists.'
    else:
        print 'referncing all interferograms to the same pixel.'
        if 'pysar.seed.lalo' in template.keys():
            'Checking lat/lon refernce point'
            lat = template['pysar.seed.lalo'].split(',')[0]
            lon = template['pysar.seed.lalo'].split(',')[1]
            seedCmd = 'seed_data.py -f ' + igramFile + ' -l ' + lat + ' -L ' + lon
        elif 'pysar.seed.yx' in template.keys():
            'Checking y/x reference point'
            y = template['pysar.seed.yx'].split(',')[0]
            x = template['pysar.seed.yx'].split(',')[1]
            seedCmd = 'seed_data.py -f ' + igramFile + ' -y ' + y + ' -x ' + x
        else:
            seedCmd = 'seed_data.py -f ' + igramFile

        igramFile = 'Seeded_' + igramFile
        print seedCmd
        os.system(seedCmd)

############################################
# Unwrapping Error Correction (Optional)
#    based on the consistency of triplets
#    of interferograms
############################################
    print '\n**********  Unwrapping Error Correction  **************'
    outName = igramFile.split('.')[0] + '_unwCor.h5'
    try:
        template['pysar.unwrapError']
        if template['pysar.unwrapError'] in ('Y', 'y', 'yes', 'Yes', 'YES'):
            if os.path.isfile(outName):
                igramFile = outName
                print igramFile + ' exists.'
            else:
                print 'This might take a while depending on the size of your data set!'
                unwCmd = 'unwrap_error.py ' + igramFile
                os.system(unwCmd)
                igramFile = outName
        else:
            print 'No unwrapping error correction.'
    except:
        print 'No unwrapping error correction.'

    #########################################
    # Inversion of Interferograms
    ########################################
    print '\n**********  Time Series Inversion  ********************'
    if os.path.isfile('timeseries.h5'):
        print 'timeseries.h5 already exists, inversion is not needed.'
    else:
        invertCmd = 'igram_inversion.py ' + igramFile
        print invertCmd
        os.system(invertCmd)
    timeseriesFile = 'timeseries.h5'

    ##############################################
    # Temporal Coherence:
    #   A parameter to evaluate the consistency
    #   of timeseries with the interferograms
    ##############################################
    print '\n**********  Generate Temporal Coherence file  *********'
    if os.path.isfile('temporal_coherence.h5'):
        print 'temporal_coherence.h5 already exists.'
    else:
        tempcohCmd = 'temporal_coherence.py ' + igramFile + ' ' + timeseriesFile
        print tempcohCmd
        os.system(tempcohCmd)
    tempCohFile = 'temporal_coherence.h5'

    ##############################################
    # Update Mask based on temporal coherence (Optional)
    ##############################################
    print '\n**********  Update Mask based on Temporal Coherence  **'
    outName = maskFile.split('.')[0] + '_tempCoh.h5'
    try:
        template['pysar.mask']
        if template['pysar.mask'] in ('Y', 'yes', 'Yes', 'YES', 'y'):
            if os.path.isfile(outName):
                maskFile = outName
                print maskFile + ' already exists.'
            else:
                try:
                    cohT = template['pysar.mask.threshold']
                except:
                    cohT = '0.7'
                maskCmd = 'generate_mask.py -f ' + tempCohFile + ' -m ' + cohT + ' -M 1.0 -o ' + outName
                print maskCmd
                os.system(maskCmd)
                maskFile = outName
        else:
            print 'No mask update from temporal coherence'
    except:
        print 'No mask update from temporal coherence'

    ##############################################
    # Incident Angle
    ##############################################
    print '\n**********  Generate Incident Angle file  *************'
    if os.path.isfile('incidence_angle.h5'):
        print 'incidence_angle.h5 already exists.'
    else:
        inciCmd = 'incidence_angle.py -f ' + timeseriesFile
        print inciCmd
        os.system(inciCmd)

##############################################
# LOD (Local Oscillator Drift) Correction
#   when Satellite is Envisat and
#   Coordinate system is radar
##############################################
    print '\n**********  Local Oscillator Drift correction  ********'
    rdr_or_geo = ut.radar_or_geo(timeseriesFile)
    outName = timeseriesFile.split('.')[0] + '_LODcor.h5'
    if os.path.isfile(outName):
        timeseriesFile = outName
        print timeseriesFile + ' already exists.'
    else:
        h5file = h5py.File(timeseriesFile, 'r')
        platform = h5file['timeseries'].attrs['PLATFORM']
        if platform == 'ENVISAT':
            if rdr_or_geo == 'radar':
                LODcmd = 'lod.py ' + timeseriesFile
                print LODcmd
                os.system(LODcmd)
                timeseriesFile = outName
            else:
                print 'Cannot correct LOD for geocoded data.'
        else:
            print 'No need of LOD correction for ' + platform
        h5file.close()


##############################################
# Tropospheric Delay Correction (Optional)
##############################################
    print '\n**********  Tropospheric Correction  ******************'
    try:
        if (template['pysar.troposphericDelay'] in ('Y','y','yes','Yes','YES')) and\
            template['pysar.orbitError.method'] in ['BaseTropCor','basetropcor','base_trop_cor']:
            print '''
   +++++++++++++++++++++++++++++++++++++++++++++++++++
   WARNING:
       Orbital error correction was BaseTropCor.
       Tropospheric correction was already applied simultaneous with baseline error correction.
       Tropospheric correction can not be applied again.
       To apply the tropospheric correction separated from baseline error correction, \
          choose other existing options for orbital error correction.
    +++++++++++++++++++++++++++++++++++++++++++++++++++      
        '''
            template['pysar.troposphericDelay'] = 'no'
    except:
        print 'Checking the tropospheric delay correction ...'

    if template['pysar.troposphericDelay'] in ('Y', 'y', 'yes', 'Yes', 'YES'):
        if rdr_or_geo == 'radar': demFile = demRdrFile
        elif rdr_or_geo == 'geo': demFile = demGeoFile

        if not os.path.isfile(demFile):
            print '++++++++++++++++++++++++++++++++++++++++++++++'
            print 'ERROR:'
            print '    DEM file was not found!'
            print '    Continue without tropospheric correction ...'
            print '++++++++++++++++++++++++++++++++++++++++++++++'
        else:
            if template['pysar.troposphericDelay.method'] in ['height-correlation','height_correlation',\
                                                              'Height-Correlation','Height_Correlation']:
                print 'tropospheric delay correction with height-correlation approach'
                outName = timeseriesFile.split('.')[0] + '_tropCor.h5'
                if os.path.isfile(outName):
                    timeseriesFile = outName
                    print timeseriesFile + ' already exists.'
                else:
                    try:
                        poly_order = template[
                            'pysar.troposphericDelay.polyOrder']
                    except:
                        poly_order = '1'
                        print 'Deafult polynomial order for troposphreic correction = 1'
                    cmdTrop = 'tropcor_phase_elevation.py' + ' -f ' + timeseriesFile + ' -d ' + demFile + ' -p ' + str(
                        poly_order)
                    print cmdTrop
                    os.system(cmdTrop)
                    timeseriesFile = outName

            elif template['pysar.troposphericDelay.method'] in [
                    'pyaps', 'PyAPS', 'PYAPS'
            ]:
                print 'Atmospheric correction using Numerical Weather Models (using PyAPS software)'
                print 'reading DEM, source of NWM and acquisition time from template file'
                source_of_NWM = template[
                    'pysar.troposphericDelay.weatherModel']
                print 'Numerical Weather Model: ' + source_of_NWM
                outName = timeseriesFile.split(
                    '.')[0] + '_' + source_of_NWM + '.h5'
                if os.path.isfile(outName):
                    timeseriesFile = outName
                    print timeseriesFile + ' already exists.'
                else:
                    acquisition_time = template['pysar.acquisitionTime']
                    print 'acquisition time: ' + acquisition_time
                    cmdTrop = 'tropcor_pyaps.py -f '+timeseriesFile+' -d '+demFile+' -s '+source_of_NWM+\
                                              ' -h '+acquisition_time+' -i incidence_angle.h5'
                    print cmdTrop
                    os.system(cmdTrop)
                    #subprocess.Popen(cmdTrop).wait()
                    timeseriesFile = outName
            else:
                print 'ERROR: Unrecognized atmospheric correction method: ' + template[
                    'pysar.trop.method']
    else:
        print 'No atmospheric delay correction.'

    ##############################################
    # Topographic (DEM) Residuals Correction (Optional)
    ##############################################
    print '\n**********  Topographic (DEM) Error correction  *******'
    outName = timeseriesFile.split('.')[0] + '_demCor.h5'
    try:
        template['pysar.topoError']
        if template['pysar.topoError'] in ('Y', 'yes', 'Yes', 'YES', 'y'):
            if os.path.isfile(outName):
                timeseriesFile = outName
                print timeseriesFile + ' already exists.'
            else:
                print 'Correcting topographic residuals'
                topoCmd = 'dem_error.py ' + timeseriesFile + ' ' + igramFile
                print topoCmd
                os.system(topoCmd)
                timeseriesFile = outName
        else:
            print 'No correction for topographic residuals.'
    except:
        print 'No correction for topographic residuals.'

    ##############################################
    # Orbit Correction (Optional)
    ##############################################
    print '\n**********  Orbital Correction  ***********************'
    try:
        template['pysar.orbitError']
        if template['pysar.orbitError'] in ('Y', 'yes', 'Yes', 'YES', 'y'):
            try:
                orbit_error_method = template['pysar.orbitError.method']
                print 'orbit error correction method : ' + orbit_error_method
                outName = timeseriesFile.split(
                    '.')[0] + '_' + orbit_error_method + '.h5'
                if os.path.isfile(outName):
                    timeseriesFile = outName
                    print timeseriesFile + ' already exists.'

                else:
                    if orbit_error_method in [    'plane',     'plane_range',     'plane_azimuth',\
                                              'quadratic', 'quadratic_range', 'quadratic_azimuth']:
                        orbitCmd = 'remove_plane.py ' + timeseriesFile + ' ' + orbit_error_method  #+ ' Mask.h5'
                        print orbitCmd
                        os.system(orbitCmd)
                        timeseriesFile = outName

                    elif orbit_error_method in ['baselineCor', 'BaselineCor']:
                        try:
                            h5file = h5py.File(timeseriesFile, 'r')
                            daz = float(h5file['timeseries'].
                                        attrs['AZIMUTH_PIXEL_SIZE'])
                            orbitCmd = 'baseline_error.py ' + timeseriesFile  #+ ' Mask.h5'
                            print orbitCmd
                            os.system(orbitCmd)
                            timeseriesFile = outName
                        except:
                            print 'WARNING!'
                            print 'Skipping orbital error correction.'
                            print 'baselineCor method can only be applied in radar coordinate'

                    elif orbit_error_method in [
                            'BaseTropCor', 'basetropcor', 'base_trop_cor'
                    ]:
                        try:
                            h5file = h5py.File(timeseriesFile, 'r')
                            daz = float(h5file['timeseries'].
                                        attrs['AZIMUTH_PIXEL_SIZE'])
                            print 'Joint estimation of Baseline error and tropospheric delay [height-correlation approach]'
                            if rdr_or_geo == 'radar': demFile = demRdrFile
                            elif rdr_or_geo == 'geo': demFile = demGeoFile
                            try:
                                poly_order = template['pysar.trop.poly_order']
                            except:
                                poly_order = 1
                                print 'Deafult polynomial order for troposphreic correction = 1'
                            orbitCmd = 'baseline_trop.py ' + timeseriesFile + ' ' + demFile + ' ' + str(
                                poly_order) + ' range_and_azimuth'
                            print orbitCmd
                            os.system(orbitCmd)
                            timeseriesFile = outName
                        except:
                            print 'WARNING!'
                            print 'Skipping orbital error correction.'
                            print 'baselineCor method can only be applied in radar coordinate'

                    else:
                        print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++'
                        print 'WARNING!'
                        print 'Orbital error correction method was not recognized!'
                        print 'Possible options are:'
                        print '    quadratic, plane, quardatic_range, quadratic_azimiuth'
                        print '    plane_range, plane_azimuth,baselineCor,BaseTropCor'
                        print 'Continue without orbital errors correction...'
                        print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++'
            except:
                print 'No orbital errors correction.'
        else:
            print 'No orbital errors correction.'
    except:
        print 'No orbital errors correction.'

    #############################################
    # Velocity and rmse maps
    #############################################
    print '\n**********  Velocity estimation  **********************'
    if os.path.isfile('velocity.h5'):
        print 'velocity.h5 file already exists.'
    else:
        velCmd = 'timeseries2velocity.py ' + timeseriesFile
        print velCmd
        os.system(velCmd)
    velocityFile = 'velocity.h5'

    ############################################
    # Geocoding (Optional)
    ############################################
    print '\n**********  Geocoding  ********************************'
    try:
        template['pysar.geocode']
        if template['pysar.geocode'] in ('Y', 'y', 'yes', 'Yes', 'YES'):
            #geoTsFile      = check_geocode(timeseriesFile,geomapFile)
            geoVelocityFile = check_geocode(velocityFile, geomapFile)
            geoMaskFile = check_geocode(maskFile, geomapFile)
        else:
            print 'No geocoding applied'
    except:
        print 'No geocoding applied'

    #############################################
    # Masking (Optional)
    #############################################
    print '\n**********  Masking Velocity  *************************'
    velocityFile = check_mask(velocityFile, maskFile)
    try:
        geoVelocityFile = check_mask(geoVelocityFile, geoMaskFile)
    except:
        pass

    #############################################
    #                PySAR v1.0                 #
    #############################################
    s = time.time() - start
    m, s = divmod(s, 60)
    h, m = divmod(m, 60)
    print '\nTime used: %02d hours %02d mins %02d secs' % (h, m, s)
    print '\n###############################################'
    print 'End of PySAR processing!'
    print '################################################\n'