예제 #1
0
def update_attribute_geo_lut(atr_rdr, atr_lut, print_msg=True):
    '''Get attributes in geo coord from atr_rdr dict and atr_lut dict
    Inputs:
        atr_rdr : dict, attributes of file in radar coord
        atr_lut : dict, attributes of mapping transformation file
        print_msg : bool, print out message or not
    Output:
        atr : dict, attributes of output file in geo coord.
    '''

    # copy atr_rdr
    atr = dict()
    for key, value in atr_rdr.iteritems():
        atr[key] = str(value)

    atr['FILE_LENGTH'] = atr_lut['FILE_LENGTH']
    atr['WIDTH']   = atr_lut['WIDTH']
    atr['Y_FIRST'] = atr_lut['Y_FIRST']
    atr['X_FIRST'] = atr_lut['X_FIRST']
    atr['Y_STEP']  = atr_lut['Y_STEP']
    atr['X_STEP']  = atr_lut['X_STEP']
    try:    atr['Y_UNIT'] = atr_lut['Y_UNIT']
    except: atr['Y_UNIT'] = 'degrees'
    try:    atr['X_UNIT'] = atr_lut['X_UNIT']
    except: atr['X_UNIT'] = 'degrees'

    # Reference point from y/x to lat/lon
    if 'ref_y' in atr_rdr.keys() and 'ref_x' in atr_rdr.keys():
        ref_x_rdr = np.array(int(atr_rdr['ref_x']))
        ref_y_rdr = np.array(int(atr_rdr['ref_y']))
        trans_file = atr_lut['FILE_PATH']
        ref_lat, ref_lon = ut.radar2glob(ref_y_rdr, ref_x_rdr, trans_file, atr_rdr, print_msg=False)[0:2]
        if ~np.isnan(ref_lat) and ~np.isnan(ref_lon):
            ref_y = np.rint((ref_lat - float(atr['Y_FIRST'])) / float(atr['Y_STEP']))
            ref_x = np.rint((ref_lon - float(atr['X_FIRST'])) / float(atr['X_STEP']))
            atr['ref_lat'] = str(ref_lat)
            atr['ref_lon'] = str(ref_lon)
            atr['ref_y'] = str(int(ref_y))
            atr['ref_x'] = str(int(ref_x))
            if print_msg:
                print 'update ref_lat/lon/y/x'
        else:
            warnings.warn("original reference pixel is out of .trans file's coverage. Continue.")
            try: atr.pop('ref_y')
            except: pass
            try: atr.pop('ref_x')
            except: pass
            try: atr.pop('ref_lat')
            except: pass
            try: atr.pop('ref_lon')
            except: pass
    return atr
예제 #2
0
def bbox_radar2geo(pix_box, atr_rdr=dict(), transFile='geomap*.trans'):
    '''Calculate bounding box in lat/lon for file in geo coord, based on input radar/pixel box
    Inputs:
        pix_box   - tuple of 4 int, indicating the UL/LR x/y
        atr_rdr   - dict, attributes of file in radar coord
        transFile - string, path of transformation file, i.e. geomap_4rlks.trans
    Output:
        geo_box - tuple of 4 float, indicating the UL/LR lon/lat of the bounding box
    '''
    x = np.array([pix_box[0], pix_box[2], pix_box[0], pix_box[2]])
    y = np.array([pix_box[1], pix_box[1], pix_box[3], pix_box[3]])
    lat, lon, lat_res, lon_res = ut.radar2glob(y, x, transFile, atr_rdr)
    buf = 10 * (np.max([lat_res, lon_res]))
    geo_box = (np.min(lon) - buf, np.max(lat) + buf, np.max(lon) + buf,
               np.min(lat) - buf)
    return geo_box
예제 #3
0
def geocode_attribute(atr_rdr, atr_geo, transFile=None):
    '''Update attributes after geocoding'''
    atr = dict()
    for key, value in atr_geo.iteritems():
        atr[key] = str(value)
    for key, value in atr_rdr.iteritems():
        atr[key] = value
    atr['WIDTH'] = atr_geo['WIDTH']
    atr['FILE_LENGTH'] = atr_geo['FILE_LENGTH']

    # Reference point from y/x to lat/lon
    if transFile and ('ref_x' and 'ref_y' in atr_rdr.keys()):
        ref_x = np.array(int(atr_rdr['ref_x']))
        ref_y = np.array(int(atr_rdr['ref_y']))
        ref_lat, ref_lon = ut.radar2glob(ref_y, ref_x, transFile, atr_rdr)[0:2]
        atr['ref_lat'] = ref_lat
        atr['ref_lon'] = ref_lon

    return atr
예제 #4
0
def main(argv):
    if len(sys.argv) < 3:
        usage(); sys.exit(1)

    y = int(argv[0])
    x = int(argv[1])
    print 'input radar coord: y/azimuth=%d, x/range=%d' % (y, x)

    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)

    lat, lon = ut.radar2glob(np.array(y), np.array(x), trans_file, atr_rdr)[0:2]
    print 'corresponding geo coord: lat=%.4f, lon=%.4f' % (lat, lon)

    return
예제 #5
0
def bbox_radar2geo(pix_box, atr_rdr=dict(), lookupFile=None):
    '''Calculate bounding box in lat/lon for file in geo coord, based on input radar/pixel box
    Inputs:
        pix_box   - tuple of 4 int, indicating the UL/LR x/y
        atr_rdr   - dict, attributes of file in radar coord
        lookupFile - string, path of transformation file, i.e. geomap_4rlks.trans
    Output:
        geo_box - tuple of 4 float, indicating the UL/LR lon/lat of the bounding box
    '''
    x = np.array([pix_box[0], pix_box[2], pix_box[0], pix_box[2]])
    y = np.array([pix_box[1], pix_box[1], pix_box[3], pix_box[3]])
    if 'Y_FIRST' in atr_rdr.keys():
        lat = coord_radar2geo(y, atr_rdr, 'y')
        lon = coord_radar2geo(x, atr_rdr, 'x')
        geo_box = (lon[0], lat[0], lon[1], lat[2])
    else:
        lat, lon, lat_res, lon_res = ut.radar2glob(y, x, lookupFile, atr_rdr)
        buf = 2 * (np.max(np.abs([lat_res, lon_res])))
        geo_box = (np.min(lon) - buf, np.max(lat) + buf, np.max(lon) + buf,
                   np.min(lat) - buf)
    return geo_box
예제 #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'
예제 #7
0
파일: pysarApp.py 프로젝트: wuweh/PySAR
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'