Exemplo n.º 1
0
def main(argv):

    DelayType = 'comb'

    try:
        opts, args = getopt.getopt(argv, "f:d:s:h:D:i:")
    except getopt.GetoptError:
        Usage()
        sys.exit(1)

    for opt, arg in opts:
        if opt == '-f': timeSeriesFile = arg
        elif opt == '-d': demFile = arg
        elif opt == '-s': atmSource = arg
        elif opt == '-h': hr = arg
        elif opt == '-D': DelayType = arg
        elif opt == '-i': inc_angle = arg

    try:
        timeSeriesFile
        demFile
    except:
        Usage()
        sys.exit(1)

    demFile = ut.check_variable_name(demFile)
    demCoord = ut.radar_or_geo(demFile)

    h5timeseries = h5py.File(timeSeriesFile)
    yref = h5timeseries['timeseries'].attrs['ref_y']
    xref = h5timeseries['timeseries'].attrs['ref_x']

    ###############################################################
    #incidence angle to map the zenith delay to the slant delay

    try:
        inc_angle
    except:
        print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
        print 'WARNING:'
        print 'incedence angle is not specified >>>> Average look angle is used ... '
        print 'For more precise results use input option -i to introduce the incidence angle file or average incodence angle'
        print '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
        input_inc_angle = 'None'

        wavelength = float(h5timeseries['timeseries'].attrs['WAVELENGTH'])
        inc_angle1 = float(h5timeseries['timeseries'].attrs['LOOK_REF1'])
        inc_angle2 = float(h5timeseries['timeseries'].attrs['LOOK_REF2'])
        inc_angle = (inc_angle1 + inc_angle2) / 2.0
        print '*******************************************************************************'
        print 'Near Look Angle: ' + str(inc_angle1)
        print 'Far  Look Angle:' + str(inc_angle2)
        print 'Average Look Angle (used in pyaps to calculate delay): ' + str(
            inc_angle)
        print 'Acquisition time is : ' + hr
        print '*******************************************************************************'
        inc_angle = str(inc_angle)

    if os.path.isfile(inc_angle):
        incidenceFile = inc_angle
        h5incidence = h5py.File(incidenceFile, 'r')
        iset = h5incidence['mask'].get('mask')
        inc_angle = iset[0:iset.shape[0], 0:iset.shape[1]]
        h5incidence.close()
    else:
        inc_angle = float(inc_angle)
        print 'incidence angle = ' + str(inc_angle)

    inc_angle = inc_angle * pi / 180.0
    ################################################################

    dateList = h5timeseries['timeseries'].keys()

    if atmSource in ['ecmwf', 'ECMWF']:
        gribSource = 'ECMWF'
        if not os.path.isdir('ECMWF'):
            print 'making directory: ECMWF'
            os.mkdir('ECMWF')

        ecmwf_file = []
        for d in dateList:
            ecm = './ECMWF/ERA-Int_' + d + '_' + hr + '.grb'
            ecmwf_file.append(ecm)
            print[d]
            if not os.path.isfile(ecm):
                pa.ECMWFdload([d], hr, './ECMWF/')
            else:
                print ecm + ' already exists.'

    elif atmSource in ['narr', 'NARR']:
        gribSource = 'NARR'
        if not os.path.isdir('NARR'):
            print 'making directory: NARR'
            os.mkdir('NARR')

        ecmwf_file = []
        for d in dateList:
            ecm = './NARR/narr-a_221_' + d + '_' + hr + '00_000.grb'
            ecmwf_file.append(ecm)
            print[d]
            if not os.path.isfile(ecm):
                pa.NARRdload([d], hr, './NARR/')
            else:
                print ecm + ' already exists.'

    elif atmSource in ['era', 'ERA']:
        gribSource = 'ERA'
        if not os.path.isdir('ERA'):
            print 'making directory: ERA'
            os.mkdir('ERA')

        ecmwf_file = []
        for d in dateList:
            ecm = './ERA/ERA_' + d + '_' + hr + '.grb'
            ecmwf_file.append(ecm)
            print[d]
            if not os.path.isfile(ecm):
                pa.ERAdload([d], hr, './ERA/')
            else:
                print ecm + ' already exists.'

    elif atmSource in ['merra', 'MERRA']:
        gribSource = 'MERRA'
        if not os.path.isdir('MERRA'):
            print 'making directory: MERRA'
            os.mkdir('MERRA')

        ecmwf_file = []
        for d in dateList:
            ecm = './MERRA/merra-' + d + '-' + hr + '.hdf'
            ecmwf_file.append(ecm)
            print[d]
            if not os.path.isfile(ecm):
                pa.MERRAdload([d], hr, './MERRA/')
            else:
                print ecm + ' already exists.'

    else:
        Usage()
        sys.exit(1)

    print '*******************************************************************************'
    print 'Calcualting delay for each epoch.'
    h5phsName = atmSource + '.h5'
    h5phs = h5py.File(h5phsName, 'w')
    outName = timeSeriesFile.replace(
        '.h5', '_') + atmSource + '.h5'  #Yunjun, Feb 15, 2015
    #outName=timeSeriesFile.replace('.h5','')+'_tropCorPyAPS.h5'
    h5apsCor = h5py.File(outName, 'w')
    group = h5apsCor.create_group('timeseries')
    group_phs = h5phs.create_group('timeseries')

    #if 'X_FIRST' in  h5timeseries['timeseries'].attrs.keys():
    #   demCoord='geo'
    #   print 'The coordinate system is : Geo'
    #else:
    #   demCoord='radar'
    #   print 'The coordinate system is : radar'

    print ecmwf_file[0]
    if demCoord == 'radar':
        aps1 = pa.PyAPS_rdr(str(ecmwf_file[0]),
                            demFile,
                            grib=gribSource,
                            verb=True,
                            Del=DelayType)
    else:
        aps1 = pa.PyAPS_geo(str(ecmwf_file[0]),
                            demFile,
                            grib=gribSource,
                            verb=True,
                            Del=DelayType)

    phs1 = zeros((aps1.ny, aps1.nx))
    aps1.getdelay(phs1)
    phs1 = (phs1 - phs1[yref, xref]) / cos(inc_angle)
    dset = group.create_dataset(dateList[0],
                                data=phs1 - phs1,
                                compression='gzip')

    for i in range(1, len(ecmwf_file)):
        ecm = ecmwf_file[i]
        print ecm
        if demCoord == 'radar':
            aps = pa.PyAPS_rdr(str(ecm),
                               demFile,
                               grib=gribSource,
                               verb=True,
                               Del=DelayType)
        else:
            aps = pa.PyAPS_geo(str(ecm),
                               demFile,
                               grib=gribSource,
                               verb=True,
                               Del=DelayType)
        phs = zeros((aps.ny, aps.nx))
        aps.getdelay(phs)
        phs = (phs - phs[yref, xref]) / cos(inc_angle)
        phs = phs - phs1
        dset = group_phs.create_dataset(dateList[i],
                                        data=phs,
                                        compression='gzip')
        dset1 = h5timeseries['timeseries'].get(dateList[i])
        data1 = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
        dset = group.create_dataset(dateList[i],
                                    data=data1 + phs,
                                    compression='gzip')

    for key, value in h5timeseries['timeseries'].attrs.iteritems():
        group.attrs[key] = value
        group_phs.attrs[key] = value

    dset1 = h5timeseries['mask'].get('mask')
    Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
    group = h5apsCor.create_group('mask')
    dset = group.create_dataset('mask', data=Mask, compression='gzip')
Exemplo n.º 2
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'
Exemplo n.º 3
0
def main(argv):

    DelayType='comb'

    try:
      opts, args = getopt.getopt(argv,"f:d:s:h:D:i:")
    except getopt.GetoptError:
      Usage() ; sys.exit(1)

    for opt,arg in opts:
      if   opt == '-f':        timeSeriesFile = arg
      elif opt == '-d':        demFile   = arg
      elif opt == '-s':        atmSource = arg
      elif opt == '-h':        hr        = arg
      elif opt == '-D':        DelayType = arg
      elif opt == '-i':        inc_angle = arg
     
    try:
      timeSeriesFile
      demFile
    except:
      Usage() ; sys.exit(1)

    demFile  = ut.check_variable_name(demFile)
    demCoord = ut.radar_or_geo(demFile)

    h5timeseries = h5py.File(timeSeriesFile)
    yref=h5timeseries['timeseries'].attrs['ref_y']
    xref=h5timeseries['timeseries'].attrs['ref_x']

###############################################################
#incidence angle to map the zenith delay to the slant delay

    try:
       inc_angle
    except:
       print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
       print 'WARNING:'
       print 'incedence angle is not specified >>>> Average look angle is used ... '
       print 'For more precise results use input option -i to introduce the incidence angle file or average incodence angle'
       print '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
       input_inc_angle='None'

       wavelength=float(h5timeseries['timeseries'].attrs['WAVELENGTH'])
       inc_angle1=float(h5timeseries['timeseries'].attrs['LOOK_REF1'])
       inc_angle2=float(h5timeseries['timeseries'].attrs['LOOK_REF2'])
       inc_angle=(inc_angle1+inc_angle2)/2.0
       print '*******************************************************************************'
       print 'Near Look Angle: ' + str(inc_angle1)
       print 'Far  Look Angle:' + str(inc_angle2)
       print 'Average Look Angle (used in pyaps to calculate delay): ' + str(inc_angle)
       print 'Acquisition time is : '+ hr
       print '*******************************************************************************'
       inc_angle=str(inc_angle)

    if os.path.isfile(inc_angle):
       incidenceFile=inc_angle       
       h5incidence=h5py.File(incidenceFile,'r')
       iset=h5incidence['mask'].get('mask')
       inc_angle=iset[0:iset.shape[0],0:iset.shape[1]]
       h5incidence.close()
    else:
       inc_angle=float(inc_angle)
       print 'incidence angle = '+ str(inc_angle)

    inc_angle=inc_angle*pi/180.0
################################################################
    
    dateList = h5timeseries['timeseries'].keys()

    if atmSource in ['ecmwf','ECMWF']:
       gribSource='ECMWF'
       if not os.path.isdir('ECMWF'):
          print 'making directory: ECMWF'
          os.mkdir('ECMWF')

       ecmwf_file=[]
       for d in dateList:
          ecm='./ECMWF/ERA-Int_'+d+'_'+hr+'.grb'
          ecmwf_file.append(ecm)
          print [d]
          if not os.path.isfile(ecm):
             pa.ECMWFdload([d],hr,'./ECMWF/')
          else:
             print ecm + ' already exists.'

    elif atmSource in ['narr','NARR']:
       gribSource='NARR'
       if not os.path.isdir('NARR'):
          print 'making directory: NARR'
          os.mkdir('NARR')

       ecmwf_file=[]
       for d in dateList:
          ecm='./NARR/narr-a_221_'+d+'_'+hr+'00_000.grb'
          ecmwf_file.append(ecm)
          print [d]
          if not os.path.isfile(ecm):
             pa.NARRdload([d],hr,'./NARR/')
          else:
             print ecm + ' already exists.'

    elif atmSource in ['era','ERA']:
       gribSource='ERA'
       if not os.path.isdir('ERA'):
          print 'making directory: ERA'
          os.mkdir('ERA')

       ecmwf_file=[]
       for d in dateList:
          ecm='./ERA/ERA_'+d+'_'+hr+'.grb'
          ecmwf_file.append(ecm)
          print [d]
          if not os.path.isfile(ecm):
             pa.ERAdload([d],hr,'./ERA/')
          else:
             print ecm + ' already exists.'

    elif atmSource in ['merra','MERRA']:
       gribSource='MERRA'
       if not os.path.isdir('MERRA'):
          print 'making directory: MERRA'
          os.mkdir('MERRA')

       ecmwf_file=[]
       for d in dateList:
          ecm='./MERRA/merra-'+d+'-'+hr+'.hdf'
          ecmwf_file.append(ecm)
          print [d]
          if not os.path.isfile(ecm):
             pa.MERRAdload([d],hr,'./MERRA/')
          else:
             print ecm + ' already exists.'

    else:
       Usage();sys.exit(1)

    print '*******************************************************************************'
    print 'Calcualting delay for each epoch.'
    h5phsName=atmSource + '.h5'
    h5phs=h5py.File(h5phsName,'w')
    outName=timeSeriesFile.replace('.h5','_') + atmSource + '.h5'	#Yunjun, Feb 15, 2015
    #outName=timeSeriesFile.replace('.h5','')+'_tropCorPyAPS.h5' 
    h5apsCor=h5py.File(outName,'w')
    group=h5apsCor.create_group('timeseries')
    group_phs=h5phs.create_group('timeseries')

    #if 'X_FIRST' in  h5timeseries['timeseries'].attrs.keys():
    #   demCoord='geo'
    #   print 'The coordinate system is : Geo'
    #else:
    #   demCoord='radar'  
    #   print 'The coordinate system is : radar'      

    print ecmwf_file[0]
    if demCoord=='radar':  aps1 = pa.PyAPS_rdr(str(ecmwf_file[0]),demFile,grib=gribSource,verb=True,Del=DelayType)
    else:                  aps1 = pa.PyAPS_geo(str(ecmwf_file[0]),demFile,grib=gribSource,verb=True,Del=DelayType)

    phs1 = zeros((aps1.ny,aps1.nx))
    aps1.getdelay(phs1)     
    phs1=(phs1 - phs1[yref,xref])/cos(inc_angle)   
    dset = group.create_dataset(dateList[0], data= phs1- phs1, compression='gzip')
    
    for i in range(1,len(ecmwf_file)):
       ecm=ecmwf_file[i] 
       print ecm
       if demCoord=='radar':  aps = pa.PyAPS_rdr(str(ecm),demFile,grib=gribSource,verb=True,Del=DelayType)   
       else:                  aps = pa.PyAPS_geo(str(ecm),demFile,grib=gribSource,verb=True,Del=DelayType)
       phs = zeros((aps.ny,aps.nx))
       aps.getdelay(phs)
       phs=(phs - phs[yref,xref])/cos(inc_angle)
       phs=phs-phs1
       dset  = group_phs.create_dataset(dateList[i], data= phs, compression='gzip')
       dset1 = h5timeseries['timeseries'].get(dateList[i])
       data1 = dset1[0:dset1.shape[0],0:dset1.shape[1]]
       dset  = group.create_dataset(dateList[i], data= data1+phs, compression='gzip')

    for key,value in h5timeseries['timeseries'].attrs.iteritems():
      group.attrs[key] = value
      group_phs.attrs[key] = value

    dset1 = h5timeseries['mask'].get('mask')
    Mask  = dset1[0:dset1.shape[0],0:dset1.shape[1]]
    group = h5apsCor.create_group('mask')
    dset  = group.create_dataset('mask', data=Mask, compression='gzip')
Exemplo n.º 4
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'