Exemple #1
0
def main(argv):

    try:
        file = argv[0]
        alks = float(argv[1])
        rlks = float(argv[2])
    except:
        Usage()
        sys.exit(1)

    ext = os.path.splitext(file)[1]

    outName = file.split('.')[0] + '_a' + str(int(alks)) + 'lks_r' + str(
        int(rlks)) + 'lks' + ext
    if ext == '.int' or ext == '.slc':
        a, p, r = readfile.read_complex64(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
        a, p, r = readfile.read_float32(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        writefile.write_float32(plks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.dem'):
        d, r = readfile.read_dem(file)
        dlks = multilook(d, alks, rlks)

        print 'writing ' + outName
        writefile.write_dem(dlks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext in ['.jpeg', 'jpg', 'png']:

        import Image
        im = Image.open(file)

        width = im.size[0] / int(rlks)
        height = im.size[1] / int(alks)

        imlks = im.resize((width, height), Image.NEAREST)
        print 'writing ' + outName
        imlks.save(outName)

        try:
            r = readfile.read_rsc_file(file + '.rsc')
        except:
            sys.exit(1)

        r['FILE_LENGTH'] = str(height)
        r['WIDTH'] = str(width)
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.h5'):

        h5file = h5py.File(file, 'r')
        # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
        h5file_lks = h5py.File(outName, 'w')

        if 'interferograms' in h5file.keys():
            print 'Multilooking the interferograms'
            gg = h5file_lks.create_group('interferograms')
            igramList = h5file['interferograms'].keys()
            for igram in igramList:
                print igram
                unw = h5file['interferograms'][igram].get(igram)
                unwlks = multilook(unw, alks, rlks)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unwlks,
                                            compression='gzip')
                for key, value in h5file['interferograms'][
                        igram].attrs.iteritems():
                    group.attrs[key] = value
                group.attrs['WIDTH'] = unwlks.shape[1]
                group.attrs['FILE_LENGTH'] = unwlks.shape[0]
                try:
                    group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP'])
                    group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP'])
                except:
                    group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float(
                        group.attrs['AZIMUTH_PIXEL_SIZE'])
                    group.attrs['RANGE_PIXEL_SIZE'] = rlks * float(
                        group.attrs['RANGE_PIXEL_SIZE'])

            dset1 = h5file['mask'].get('mask')
            mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            masklks = multilook(mask, alks, rlks)
            group = h5file_lks.create_group('mask')
            dset = group.create_dataset('mask',
                                        data=masklks,
                                        compression='gzip')

        elif 'timeseries' in h5file.keys():
            print 'Multilooking the time-series'
            group = h5file_lks.create_group('timeseries')
            dateList = h5file['timeseries'].keys()
            for d in dateList:
                print d
                unw = h5file['timeseries'].get(d)
                unwlks = multilook(unw, alks, rlks)
                dset = group.create_dataset(d, data=unwlks, compression='gzip')

            for key, value in h5file['timeseries'].attrs.iteritems():
                group.attrs[key] = value
            group.attrs['WIDTH'] = unwlks.shape[1]
            group.attrs['FILE_LENGTH'] = unwlks.shape[0]

            try:
                group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP'])
                group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP'])
            except:
                group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float(
                    group.attrs['AZIMUTH_PIXEL_SIZE'])
                group.attrs['RANGE_PIXEL_SIZE'] = rlks * float(
                    group.attrs['RANGE_PIXEL_SIZE'])

            try:
                dset1 = h5file['mask'].get('mask')
                Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                Masklks = multilook(Mask, alks, rlks)
                group = h5file_lks.create_group('mask')
                dset = group.create_dataset('mask',
                                            data=Masklks,
                                            compression='gzip')
            except:
                print 'Multilooked file does not include the maske'

        elif 'temporal_coherence' in h5file.keys(
        ) or 'velocity' in h5file.keys() or 'mask' in h5file.keys():
            k = h5file.keys()
            print 'multi looking the ' + k[0]

            group = h5file_lks.create_group(k[0])
            dset1 = h5file[k[0]].get(k[0])
            Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            Masklks = multilook(Mask, alks, rlks)
            dset = group.create_dataset(k[0], data=Masklks, compression='gzip')
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value

            try:
                group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP'])
                group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP'])
            except:
                group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float(
                    group.attrs['AZIMUTH_PIXEL_SIZE'])
                group.attrs['RANGE_PIXEL_SIZE'] = rlks * float(
                    group.attrs['RANGE_PIXEL_SIZE'])
        group.attrs['WIDTH'] = Masklks.shape[1]
        group.attrs['FILE_LENGTH'] = Masklks.shape[0]
        h5file.close()
        h5file_lks.close()
def main(argv):

    try:
        opts, args = getopt.getopt(argv, "h:f:t:p:")

    except getopt.GetoptError:
        Usage()
        sys.exit(1)

    if opts == []:
        Usage()
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            Usage()
            sys.exit()
        elif opt == "-f":
            file = arg
        elif opt == "-t":
            filtType = arg
        elif opt == "-p":
            par = arg

    #  try:
    #    file=argv[0]
    #    alks=float(argv[1])
    #    rlks=float(argv[2])
    #  except:
    #    Usage();sys.exit(1)

    ext = os.path.splitext(file)[1]
    outName = file.split(".")[0] + "_" + filtType + ext
    try:
        par
    except:
        par = []

    print "+++++++++++++++++++++++++++"
    print "Filter type : " + filtType
    print "parameters : " + str(par)
    print "+++++++++++++++++++++++++++"
    ###############################################
    if ext == ".int" or ext == ".slc":
        a, p, r = readfile.read_complex64(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        r["FILE_LENGTH"] = str(dlks.shape[0])
        r["WIDTH"] = str(dlks.shape[1])
        r["XMAX"] = str(int(r["WIDTH"]) - 1)
        r["YMAX"] = str(int(r["FILE_LENGTH"]) - 1)
        try:
            r["Y_STEP"] = str(float(r["Y_STEP"]) * alks)
            r["X_STEP"] = str(float(r["X_STEP"]) * rlks)
        except:
            Geo = 0

        f = open(outName + ".rsc", "w")
        for k in r.keys():
            f.write(k + "    " + r[k] + "\n")
        f.close()

    elif ext == ".unw" or ext == ".cor" or ext == ".hgt":
        a, p, r = readfile.read_float32(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        writefile.write_float32(plks, outName)

        r["FILE_LENGTH"] = str(dlks.shape[0])
        r["WIDTH"] = str(dlks.shape[1])
        r["XMAX"] = str(int(r["WIDTH"]) - 1)
        r["YMAX"] = str(int(r["FILE_LENGTH"]) - 1)

        try:
            r["Y_STEP"] = str(float(r["Y_STEP"]) * alks)
            r["X_STEP"] = str(float(r["X_STEP"]) * rlks)
        except:
            Geo = 0

        f = open(outName + ".rsc", "w")
        for k in r.keys():
            f.write(k + "    " + r[k] + "\n")
        f.close()

    elif ext == (".dem"):
        d, r = readfile.read_dem(file)
        dlks = multilook(d, alks, rlks)

        print "writing " + outName
        writefile.write_dem(dlks, outName)

        r["FILE_LENGTH"] = str(dlks.shape[0])
        r["WIDTH"] = str(dlks.shape[1])
        r["XMAX"] = str(int(r["WIDTH"]) - 1)
        r["YMAX"] = str(int(r["FILE_LENGTH"]) - 1)

        try:
            r["Y_STEP"] = str(float(r["Y_STEP"]) * alks)
            r["X_STEP"] = str(float(r["X_STEP"]) * rlks)
        except:
            Geo = 0

        f = open(outName + ".rsc", "w")
        for k in r.keys():
            f.write(k + "    " + r[k] + "\n")
        f.close()

    elif ext in [".jpeg", "jpg", "png"]:

        import Image

        im = Image.open(file)

        width = im.size[0] / int(rlks)
        height = im.size[1] / int(alks)

        imlks = im.resize((width, height), Image.NEAREST)
        print "writing " + outName
        imlks.save(outName)

        try:
            r = readfile.read_rsc_file(file + ".rsc")
        except:
            sys.exit(1)

        r["FILE_LENGTH"] = str(height)
        r["WIDTH"] = str(width)
        r["XMAX"] = str(int(r["WIDTH"]) - 1)
        r["YMAX"] = str(int(r["FILE_LENGTH"]) - 1)
        try:
            r["Y_STEP"] = str(float(r["Y_STEP"]) * alks)
            r["X_STEP"] = str(float(r["X_STEP"]) * rlks)
        except:
            Geo = 0

        f = open(outName + ".rsc", "w")
        for k in r.keys():
            f.write(k + "    " + r[k] + "\n")
        f.close()

    elif ext == (".h5"):

        h5file = h5py.File(file, "r")
        # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
        h5file_lks = h5py.File(outName, "w")

        if "interferograms" in h5file.keys():
            print "Filtering the interferograms in space"
            gg = h5file_lks.create_group("interferograms")
            igramList = h5file["interferograms"].keys()
            for igram in igramList:
                print igram
                unwSet = h5file["interferograms"][igram].get(igram)
                unw = unwSet[0 : unwSet.shape[0], 0 : unwSet.shape[1]]
                unw = filter(unw, filtType, par)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram, data=unw, compression="gzip")
                for key, value in h5file["interferograms"][igram].attrs.iteritems():
                    group.attrs[key] = value

            dset1 = h5file["mask"].get("mask")
            mask = dset1[0 : dset1.shape[0], 0 : dset1.shape[1]]
            group = h5file_lks.create_group("mask")
            dset = group.create_dataset("mask", data=mask, compression="gzip")

        elif "timeseries" in h5file.keys():
            print "Filtering the time-series"
            group = h5file_lks.create_group("timeseries")
            dateList = h5file["timeseries"].keys()
            for d in dateList:
                print d
                dset1 = h5file["timeseries"].get(d)
                data = dset1[0 : dset1.shape[0], 0 : dset1.shape[1]]
                data = filter(data, filtType, par)

                dset = group.create_dataset(d, data=data, compression="gzip")

            for key, value in h5file["timeseries"].attrs.iteritems():
                group.attrs[key] = value

            try:
                dset1 = h5file["mask"].get("mask")
                Mask = dset1[0 : dset1.shape[0], 0 : dset1.shape[1]]
                # Masklks=multilook(Mask,alks,rlks)
                group = h5file_lks.create_group("mask")
                dset = group.create_dataset("mask", data=Mask, compression="gzip")
            except:
                print "Filterd file does not include the maske"

        elif "temporal_coherence" in h5file.keys() or "velocity" in h5file.keys() or "mask" in h5file.keys():
            k = h5file.keys()
            print "filtering the " + k[0]

            group = h5file_lks.create_group(k[0])
            dset1 = h5file[k[0]].get(k[0])
            data = dset1[0 : dset1.shape[0], 0 : dset1.shape[1]]
            data = filter(data, filtType, par)
            dset = group.create_dataset(k[0], data=data, compression="gzip")
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value

        h5file.close()
        h5file_lks.close()
Exemple #3
0
def main(argv):

  #outName='subsetIgrams.h5'
  try:
      opts, args = getopt.getopt(argv,"h:f:x:y:o:l:L:")

  except getopt.GetoptError:
      print 'Error while getting args'
      Usage() ; sys.exit(1)

  for opt,arg in opts:
      if opt in ("-h","--help"):
        Usage()
        sys.exit()
      elif opt == '-f':
        File = arg
      elif opt=='-y':
        ysub=[int(i) for i in arg.split(':')]
        ysub.sort()
      elif opt=='-x':
        xsub = [int(i) for i in arg.split(':')]
        xsub.sort()
      elif opt=='-o':
        outName=arg
      elif opt=='-l':
        Latsub=[float(i) for i in arg.split(':')]
        Latsub.sort()
      elif opt=='-L':
        Lonsub = [float(i) for i in arg.split(':')]
        Lonsub.sort()


#####################################################

  try:
    File
    xsub
    ysub
  except:
    try:
      File
      Latsub
      Lonsub
    except:
      Usage();sys.exit(1)

  try:
    outName
  except:
    outName='subset_'+File

  ext = os.path.splitext(File)[1]

  if ext == '.h5':
    try:
      h5file=h5py.File(File,'r')
    except:
      Usage() ; sys.exit(1)
    k=h5file.keys()

    # convert LatLon to xy for geocoded file
    try:
      Latsub
      Lonsub
      if 'X_FIRST' in h5file[k[0]].attrs.keys():
        xsub=[0]*2
        ysub=[0]*2
        xsub[0]=int((Lonsub[0]-float(h5file[k[0]].attrs['X_FIRST']))/float(h5file[k[0]].attrs['X_STEP']))
        xsub[1]=int((Lonsub[1]-float(h5file[k[0]].attrs['X_FIRST']))/float(h5file[k[0]].attrs['X_STEP']))
        ysub[0]=int((Latsub[1]-float(h5file[k[0]].attrs['Y_FIRST']))/float(h5file[k[0]].attrs['Y_STEP']))
        ysub[1]=int((Latsub[0]-float(h5file[k[0]].attrs['Y_FIRST']))/float(h5file[k[0]].attrs['Y_STEP']))
        print 'Subseting geocoded',ext,' file with Latitude and Longitude...'
      elif 'X_FIRST' in h5file[k[0]][h5file[k[0]].keys()[0]].attrs.keys():	# for geocoded interferograms/coherence
        igramList=h5file[k[0]].keys()
        xsub=[0]*2
        ysub=[0]*2
        xsub[0]=int((Lonsub[0]-float(h5file[k[0]][igramList[0]].attrs['X_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['X_STEP']))
        xsub[1]=int((Lonsub[1]-float(h5file[k[0]][igramList[0]].attrs['X_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['X_STEP']))
        ysub[0]=int((Latsub[1]-float(h5file[k[0]][igramList[0]].attrs['Y_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['Y_STEP']))
        ysub[1]=int((Latsub[0]-float(h5file[k[0]][igramList[0]].attrs['Y_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['Y_STEP']))
        print 'Subseting geocoded',ext,' file with Latitude and Longitude...'
      else:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; sys.exit(1)
    except:
      Geo=0
#    k=h5file.keys()

    if 'interferograms' in k:
  
      igramList=h5file['interferograms'].keys()
      h5out=h5py.File(outName,'w')
      gg=h5out.create_group('interferograms')
      for igram in igramList:
        print igram
        dset1=h5file['interferograms'][igram].get(igram)
        group=gg.create_group(igram)
        dset=group.create_dataset(igram, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
        for key, value in h5file['interferograms'][igram].attrs.iteritems():
          group.attrs[key] = value
        group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0]
        group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1]
        group.attrs['subset_x0']=xsub[0]
        group.attrs['subset_x1']=xsub[1]
        group.attrs['subset_y0']=ysub[0]
        group.attrs['subset_y1']=ysub[1]

        if 'X_FIRST' in h5file['interferograms'][igram].attrs.keys():
          group.attrs['X_FIRST']=float(h5file['interferograms'][igram].attrs['X_FIRST']) + xsub[0]*float(h5file['interferograms'][igram].attrs['X_STEP'])
          group.attrs['Y_FIRST']=float(h5file['interferograms'][igram].attrs['Y_FIRST']) + ysub[0]*float(h5file['interferograms'][igram].attrs['Y_STEP'])  
 
      gm=h5out.create_group('mask')
      try:
        Mset=h5file['mask'].get('mask')
        dset=gm.create_dataset('mask', data=Mset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      except:
        print 'No group for mask found! It may cause problem in other processing steps.'

      try:    
        Cset=h5file['meanCoherence'].get('meanCoherence')                  
        gm=h5out.create_group('meanCoherence')
        dset=gm.create_dataset('meanCoherence', data=Cset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      except:
        print 'No average coherence found in the File'

    elif k[0] in ('coherence','wrapped'):
      corList=h5file[k[0]].keys()
      h5out=h5py.File(outName,'w')
      gg=h5out.create_group(k[0])
      for cor in corList:
        print cor
        dset1=h5file[k[0]][cor].get(cor)
        group=gg.create_group(cor)
        dset=group.create_dataset(cor, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
        for key, value in h5file[k[0]][cor].attrs.iteritems():
          group.attrs[key] = value
        group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0]
        group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1]
        group.attrs['subset_x0']=xsub[0]
        group.attrs['subset_x1']=xsub[1]
        group.attrs['subset_y0']=ysub[0]
        group.attrs['subset_y1']=ysub[1]

        if 'X_FIRST' in h5file[k[0]][cor].attrs.keys():
          group.attrs['X_FIRST']=float(h5file[k[0]][cor].attrs['X_FIRST']) + xsub[0]*float(h5file[k[0]][cor].attrs['X_STEP'])
          group.attrs['Y_FIRST']=float(h5file[k[0]][cor].attrs['Y_FIRST']) + ysub[0]*float(h5file[k[0]][cor].attrs['Y_STEP'])


    elif 'timeseries' in h5file.keys():
      
      dateList=h5file['timeseries'].keys()
      h5out=h5py.File(outName,'w')
      group=h5out.create_group('timeseries')
      for d in dateList:
        print d
        dset1=h5file['timeseries'].get(d)
        dset=group.create_dataset(d, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      for key, value in h5file['timeseries'].attrs.iteritems():
        group.attrs[key] = value
      group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0]
      group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1]
      group.attrs['subset_x0']=xsub[0]
      group.attrs['subset_x1']=xsub[1]
      group.attrs['subset_y0']=ysub[0]
      group.attrs['subset_y1']=ysub[1] 

      if 'X_FIRST' in h5file['timeseries'].attrs.keys():
          group.attrs['X_FIRST']=float(h5file['timeseries'].attrs['X_FIRST']) + xsub[0]*float(h5file['timeseries'].attrs['X_STEP'])
          group.attrs['Y_FIRST']=float(h5file['timeseries'].attrs['Y_FIRST']) + ysub[0]*float(h5file['timeseries'].attrs['Y_STEP'])
      h5file.close()
      h5out.close()

    elif 'temporal_coherence' in h5file.keys() or 'velocity' in h5file.keys() or 'mask' in h5file.keys() or 'rmse' in h5file.keys():
      print 'writing  >>>  ' +outName
      dset=h5file[k[0]].get(k[0]) 
      data=dset[ysub[0]:ysub[1],xsub[0]:xsub[1]]
      hfout=h5py.File(outName,'w')
      group= hfout.create_group(k[0])
      group.create_dataset(k[0],data=data,compression='gzip')
    
      for key,value in h5file[k[0]].attrs.iteritems():
         group.attrs[key]=value
      group.attrs['FILE_LENGTH']=data.shape[0]
      group.attrs['WIDTH']=data.shape[1]
      group.attrs['XMIN']=0
      group.attrs['XMAX']=data.shape[1]-1
      group.attrs['YMIN']=0
      group.attrs['YMAX']=data.shape[0]-1
      group.attrs['subset_x0']=xsub[0]
      group.attrs['subset_x1']=xsub[1]
      group.attrs['subset_y0']=ysub[0]
      group.attrs['subset_y1']=ysub[1]
      if 'X_FIRST' in h5file[k[0]].attrs.keys():
         group.attrs['X_FIRST']=float(h5file[k[0]].attrs['X_FIRST']) + xsub[0]*float(h5file[k[0]].attrs['X_STEP'])
         group.attrs['Y_FIRST']=float(h5file[k[0]].attrs['Y_FIRST']) + ysub[0]*float(h5file[k[0]].attrs['Y_STEP'])
      h5file.close()
      hfout.close()
 
  elif ext in ['.unw','.cor','.hgt']:
    a,p,r = readfile.read_float32(File)
  
    try:
      Latsub
      Lonsub
      try:
        r['X_FIRST']
        xsub=[0]*2
        ysub=[0]*2
        xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP']))
        xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP']))
        ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        print 'Subseting geocoded',ext,' file with Latitude and Longitude...'
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; sys.exit(1)
    except:
      Geo=0
  
    a=a[ysub[0]:ysub[1],xsub[0]:xsub[1]] 
    p=p[ysub[0]:ysub[1],xsub[0]:xsub[1]]

    print 'writing >>> '+outName
    writefile.write_float32(p,outName)
    
    r['FILE_LENGTH']=str(p.shape[0])
    r['WIDTH']=str(p.shape[1])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    r['subset_x0']=str(xsub[0]) 
    r['subset_x1']=str(xsub[1])
    r['subset_y0']=str(ysub[0]) 
    r['subset_y1']=str(ysub[1])
    try:
       r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP']))
       r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP']))
    except:
       Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()

  elif ext== '.dem':
    d,r = readfile.read_dem(File)

    try:
      Latsub
      Lonsub
#      print Latsub
      try:
        r['X_FIRST']
        xsub=[0]*2
        ysub=[0]*2
        xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP']))
        xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP']))
        ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        print 'Subseting',ext,' file with Latitude and Longitude...'
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; sys.exit(1)
    except:
      Geo=0

    d=d[ysub[0]:ysub[1],xsub[0]:xsub[1]]  

    print 'writing >>> '+outName
    writefile.write_dem(d,outName)
    
    r['FILE_LENGTH']=str(d.shape[0])
    r['WIDTH']=str(d.shape[1])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    r['subset_x0']=str(xsub[0]) 
    r['subset_x1']=str(xsub[1])
    r['subset_y0']=str(ysub[0]) 
    r['subset_y1']=str(ysub[1])

    try:
       r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP']))
       r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP']))
    except:
       Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()

  elif ext in ['.jpeg','jpg','png']:
    import Image
    im = Image.open(File)

    try:
      r=readfile.read_rsc_file(File+'.rsc')
    except:
      sys.exit(1)

    try:
      Latsub
      Lonsub
      try:
        r['X_FIRST']
        xsub=[0]*2
        ysub=[0]*2
        xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP']))
        xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP']))
        ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP']))
        print 'Subseting geocoded',ext,' file with Latitude and Longitude...'
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; sys.exit(1)
    except:
      Geo=0

    box = (xsub[0],ysub[0],xsub[1],ysub[1])
    output_img = im.crop(box)
    print 'writing >>> '+outName
    output_img.save(outName)
#    try:
#      r=readfile.read_rsc_file(File+'.rsc')
#    except:
#      sys.exit(1)
     
    r['FILE_LENGTH']=str(ysub[1]-ysub[0])
    r['WIDTH']=str(xsub[1]-xsub[0])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    r['subset_x0']=str(xsub[0]) 
    r['subset_x1']=str(xsub[1])
    r['subset_y0']=str(ysub[0]) 
    r['subset_y1']=str(ysub[1])
    try:
       r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP']))
       r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP']))
    except:
       Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()
Exemple #4
0
def main(argv):

    try:
        opts, args = getopt.getopt(argv, "h:f:t:p:")

    except getopt.GetoptError:
        Usage()
        sys.exit(1)

    if opts == []:
        Usage()
        sys.exit(1)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            Usage()
            sys.exit()
        elif opt == '-f':
            file = arg
        elif opt == '-t':
            filtType = arg
        elif opt == '-p':
            par = arg


#  try:
#    file=argv[0]
#    alks=float(argv[1])
#    rlks=float(argv[2])
#  except:
#    Usage();sys.exit(1)

    ext = os.path.splitext(file)[1]
    outName = file.split('.')[0] + '_' + filtType + ext
    try:
        par
    except:
        par = []

    print '+++++++++++++++++++++++++++'
    print 'Filter type : ' + filtType
    print 'parameters : ' + str(par)
    print '+++++++++++++++++++++++++++'
    ###############################################
    if ext == '.int' or ext == '.slc':
        a, p, r = readfile.read_complex64(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
        a, p, r = readfile.read_float32(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        writefile.write_float32(plks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.dem'):
        d, r = readfile.read_dem(file)
        dlks = multilook(d, alks, rlks)

        print 'writing ' + outName
        writefile.write_dem(dlks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext in ['.jpeg', 'jpg', 'png']:

        import Image
        im = Image.open(file)

        width = im.size[0] / int(rlks)
        height = im.size[1] / int(alks)

        imlks = im.resize((width, height), Image.NEAREST)
        print 'writing ' + outName
        imlks.save(outName)

        try:
            r = readfile.read_rsc_file(file + '.rsc')
        except:
            sys.exit(1)

        r['FILE_LENGTH'] = str(height)
        r['WIDTH'] = str(width)
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.h5'):

        h5file = h5py.File(file, 'r')
        # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
        h5file_lks = h5py.File(outName, 'w')

        if 'interferograms' in h5file.keys():
            print 'Filtering the interferograms in space'
            gg = h5file_lks.create_group('interferograms')
            igramList = h5file['interferograms'].keys()
            for igram in igramList:
                print igram
                unwSet = h5file['interferograms'][igram].get(igram)
                unw = unwSet[0:unwSet.shape[0], 0:unwSet.shape[1]]
                unw = filter(unw, filtType, par)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unw,
                                            compression='gzip')
                for key, value in h5file['interferograms'][
                        igram].attrs.iteritems():
                    group.attrs[key] = value

            dset1 = h5file['mask'].get('mask')
            mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            group = h5file_lks.create_group('mask')
            dset = group.create_dataset('mask', data=mask, compression='gzip')

        elif 'timeseries' in h5file.keys():
            print 'Filtering the time-series'
            group = h5file_lks.create_group('timeseries')
            dateList = h5file['timeseries'].keys()
            for d in dateList:
                print d
                dset1 = h5file['timeseries'].get(d)
                data = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                data = filter(data, filtType, par)

                dset = group.create_dataset(d, data=data, compression='gzip')

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

            try:
                dset1 = h5file['mask'].get('mask')
                Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                # Masklks=multilook(Mask,alks,rlks)
                group = h5file_lks.create_group('mask')
                dset = group.create_dataset('mask',
                                            data=Mask,
                                            compression='gzip')
            except:
                print 'Filterd file does not include the maske'

        elif 'temporal_coherence' in h5file.keys(
        ) or 'velocity' in h5file.keys() or 'mask' in h5file.keys():
            k = h5file.keys()
            print 'filtering the ' + k[0]

            group = h5file_lks.create_group(k[0])
            dset1 = h5file[k[0]].get(k[0])
            data = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            data = filter(data, filtType, par)
            dset = group.create_dataset(k[0], data=data, compression='gzip')
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value

        h5file.close()
        h5file_lks.close()
Exemple #5
0
def main(argv):

    #outName='subsetIgrams.h5'
    try:
        opts, args = getopt.getopt(argv, "h:f:x:y:o:l:L:")

    except getopt.GetoptError:
        print 'Error while getting args'
        Usage()
        sys.exit(1)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            Usage()
            sys.exit()
        elif opt == '-f':
            File = arg
        elif opt == '-y':
            ysub = [int(i) for i in arg.split(':')]
            ysub.sort()
        elif opt == '-x':
            xsub = [int(i) for i in arg.split(':')]
            xsub.sort()
        elif opt == '-o':
            outName = arg
        elif opt == '-l':
            Latsub = [float(i) for i in arg.split(':')]
            Latsub.sort()
        elif opt == '-L':
            Lonsub = [float(i) for i in arg.split(':')]
            Lonsub.sort()

#####################################################

    try:
        File
        xsub
        ysub
    except:
        try:
            File
            Latsub
            Lonsub
        except:
            Usage()
            sys.exit(1)

    try:
        outName
    except:
        outName = 'subset_' + File

    ext = os.path.splitext(File)[1]

    if ext == '.h5':
        try:
            h5file = h5py.File(File, 'r')
        except:
            Usage()
            sys.exit(1)
        k = h5file.keys()

        # convert LatLon to xy for geocoded file
        try:
            Latsub
            Lonsub
            if 'X_FIRST' in h5file[k[0]].attrs.keys():
                xsub = [0] * 2
                ysub = [0] * 2
                xsub[0] = int(
                    (Lonsub[0] - float(h5file[k[0]].attrs['X_FIRST'])) /
                    float(h5file[k[0]].attrs['X_STEP']))
                xsub[1] = int(
                    (Lonsub[1] - float(h5file[k[0]].attrs['X_FIRST'])) /
                    float(h5file[k[0]].attrs['X_STEP']))
                ysub[0] = int(
                    (Latsub[1] - float(h5file[k[0]].attrs['Y_FIRST'])) /
                    float(h5file[k[0]].attrs['Y_STEP']))
                ysub[1] = int(
                    (Latsub[0] - float(h5file[k[0]].attrs['Y_FIRST'])) /
                    float(h5file[k[0]].attrs['Y_STEP']))
                print 'Subseting geocoded', ext, ' file with Latitude and Longitude...'
            elif 'X_FIRST' in h5file[k[0]][h5file[k[0]].keys()[0]].attrs.keys(
            ):  # for geocoded interferograms/coherence
                igramList = h5file[k[0]].keys()
                xsub = [0] * 2
                ysub = [0] * 2
                xsub[0] = int(
                    (Lonsub[0] -
                     float(h5file[k[0]][igramList[0]].attrs['X_FIRST'])) /
                    float(h5file[k[0]][igramList[0]].attrs['X_STEP']))
                xsub[1] = int(
                    (Lonsub[1] -
                     float(h5file[k[0]][igramList[0]].attrs['X_FIRST'])) /
                    float(h5file[k[0]][igramList[0]].attrs['X_STEP']))
                ysub[0] = int(
                    (Latsub[1] -
                     float(h5file[k[0]][igramList[0]].attrs['Y_FIRST'])) /
                    float(h5file[k[0]][igramList[0]].attrs['Y_STEP']))
                ysub[1] = int(
                    (Latsub[0] -
                     float(h5file[k[0]][igramList[0]].attrs['Y_FIRST'])) /
                    float(h5file[k[0]][igramList[0]].attrs['Y_STEP']))
                print 'Subseting geocoded', ext, ' file with Latitude and Longitude...'
            else:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                sys.exit(1)
        except:
            Geo = 0


#    k=h5file.keys()

        if 'interferograms' in k:

            igramList = h5file['interferograms'].keys()
            h5out = h5py.File(outName, 'w')
            gg = h5out.create_group('interferograms')
            for igram in igramList:
                print igram
                dset1 = h5file['interferograms'][igram].get(igram)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=dset1[ysub[0]:ysub[1],
                                                       xsub[0]:xsub[1]],
                                            compression='gzip')
                for key, value in h5file['interferograms'][
                        igram].attrs.iteritems():
                    group.attrs[key] = value
                group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1],
                                                         xsub[0]:xsub[1]])[0]
                group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]])[1]
                group.attrs['subset_x0'] = xsub[0]
                group.attrs['subset_x1'] = xsub[1]
                group.attrs['subset_y0'] = ysub[0]
                group.attrs['subset_y1'] = ysub[1]

                if 'X_FIRST' in h5file['interferograms'][igram].attrs.keys():
                    group.attrs['X_FIRST'] = float(
                        h5file['interferograms']
                        [igram].attrs['X_FIRST']) + xsub[0] * float(
                            h5file['interferograms'][igram].attrs['X_STEP'])
                    group.attrs['Y_FIRST'] = float(
                        h5file['interferograms']
                        [igram].attrs['Y_FIRST']) + ysub[0] * float(
                            h5file['interferograms'][igram].attrs['Y_STEP'])

            gm = h5out.create_group('mask')
            try:
                Mset = h5file['mask'].get('mask')
                dset = gm.create_dataset('mask',
                                         data=Mset[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]],
                                         compression='gzip')
            except:
                print 'No group for mask found! It may cause problem in other processing steps.'

            try:
                Cset = h5file['meanCoherence'].get('meanCoherence')
                gm = h5out.create_group('meanCoherence')
                dset = gm.create_dataset('meanCoherence',
                                         data=Cset[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]],
                                         compression='gzip')
            except:
                print 'No average coherence found in the File'

        elif k[0] in ('coherence', 'wrapped'):
            corList = h5file[k[0]].keys()
            h5out = h5py.File(outName, 'w')
            gg = h5out.create_group(k[0])
            for cor in corList:
                print cor
                dset1 = h5file[k[0]][cor].get(cor)
                group = gg.create_group(cor)
                dset = group.create_dataset(cor,
                                            data=dset1[ysub[0]:ysub[1],
                                                       xsub[0]:xsub[1]],
                                            compression='gzip')
                for key, value in h5file[k[0]][cor].attrs.iteritems():
                    group.attrs[key] = value
                group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1],
                                                         xsub[0]:xsub[1]])[0]
                group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]])[1]
                group.attrs['subset_x0'] = xsub[0]
                group.attrs['subset_x1'] = xsub[1]
                group.attrs['subset_y0'] = ysub[0]
                group.attrs['subset_y1'] = ysub[1]

                if 'X_FIRST' in h5file[k[0]][cor].attrs.keys():
                    group.attrs['X_FIRST'] = float(
                        h5file[k[0]][cor].attrs['X_FIRST']) + xsub[0] * float(
                            h5file[k[0]][cor].attrs['X_STEP'])
                    group.attrs['Y_FIRST'] = float(
                        h5file[k[0]][cor].attrs['Y_FIRST']) + ysub[0] * float(
                            h5file[k[0]][cor].attrs['Y_STEP'])

        elif 'timeseries' in h5file.keys():

            dateList = h5file['timeseries'].keys()
            h5out = h5py.File(outName, 'w')
            group = h5out.create_group('timeseries')
            for d in dateList:
                print d
                dset1 = h5file['timeseries'].get(d)
                dset = group.create_dataset(d,
                                            data=dset1[ysub[0]:ysub[1],
                                                       xsub[0]:xsub[1]],
                                            compression='gzip')
            for key, value in h5file['timeseries'].attrs.iteritems():
                group.attrs[key] = value
            group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1],
                                                     xsub[0]:xsub[1]])[0]
            group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1],
                                               xsub[0]:xsub[1]])[1]
            group.attrs['subset_x0'] = xsub[0]
            group.attrs['subset_x1'] = xsub[1]
            group.attrs['subset_y0'] = ysub[0]
            group.attrs['subset_y1'] = ysub[1]

            if 'X_FIRST' in h5file['timeseries'].attrs.keys():
                group.attrs['X_FIRST'] = float(
                    h5file['timeseries'].attrs['X_FIRST']) + xsub[0] * float(
                        h5file['timeseries'].attrs['X_STEP'])
                group.attrs['Y_FIRST'] = float(
                    h5file['timeseries'].attrs['Y_FIRST']) + ysub[0] * float(
                        h5file['timeseries'].attrs['Y_STEP'])
            h5file.close()
            h5out.close()

        elif 'temporal_coherence' in h5file.keys(
        ) or 'velocity' in h5file.keys() or 'mask' in h5file.keys(
        ) or 'rmse' in h5file.keys():
            print 'writing  >>>  ' + outName
            dset = h5file[k[0]].get(k[0])
            data = dset[ysub[0]:ysub[1], xsub[0]:xsub[1]]
            hfout = h5py.File(outName, 'w')
            group = hfout.create_group(k[0])
            group.create_dataset(k[0], data=data, compression='gzip')

            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value
            group.attrs['FILE_LENGTH'] = data.shape[0]
            group.attrs['WIDTH'] = data.shape[1]
            group.attrs['XMIN'] = 0
            group.attrs['XMAX'] = data.shape[1] - 1
            group.attrs['YMIN'] = 0
            group.attrs['YMAX'] = data.shape[0] - 1
            group.attrs['subset_x0'] = xsub[0]
            group.attrs['subset_x1'] = xsub[1]
            group.attrs['subset_y0'] = ysub[0]
            group.attrs['subset_y1'] = ysub[1]
            if 'X_FIRST' in h5file[k[0]].attrs.keys():
                group.attrs['X_FIRST'] = float(
                    h5file[k[0]].attrs['X_FIRST']) + xsub[0] * float(
                        h5file[k[0]].attrs['X_STEP'])
                group.attrs['Y_FIRST'] = float(
                    h5file[k[0]].attrs['Y_FIRST']) + ysub[0] * float(
                        h5file[k[0]].attrs['Y_STEP'])
            h5file.close()
            hfout.close()

    elif ext in ['.unw', '.cor', '.hgt']:
        a, p, r = readfile.read_float32(File)

        try:
            Latsub
            Lonsub
            try:
                r['X_FIRST']
                xsub = [0] * 2
                ysub = [0] * 2
                xsub[0] = int(
                    (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP']))
                xsub[1] = int(
                    (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP']))
                ysub[0] = int(
                    (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                ysub[1] = int(
                    (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                print 'Subseting geocoded', ext, ' file with Latitude and Longitude...'
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                sys.exit(1)
        except:
            Geo = 0

        a = a[ysub[0]:ysub[1], xsub[0]:xsub[1]]
        p = p[ysub[0]:ysub[1], xsub[0]:xsub[1]]

        print 'writing >>> ' + outName
        writefile.write_float32(p, outName)

        r['FILE_LENGTH'] = str(p.shape[0])
        r['WIDTH'] = str(p.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        r['subset_x0'] = str(xsub[0])
        r['subset_x1'] = str(xsub[1])
        r['subset_y0'] = str(ysub[0])
        r['subset_y1'] = str(ysub[1])
        try:
            r['Y_FIRST'] = str(
                float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP']))
            r['X_FIRST'] = str(
                float(r['X_FIRST']) + xsub[0] * float(r['X_STEP']))
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == '.dem':
        d, r = readfile.read_dem(File)

        try:
            Latsub
            Lonsub
            #      print Latsub
            try:
                r['X_FIRST']
                xsub = [0] * 2
                ysub = [0] * 2
                xsub[0] = int(
                    (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP']))
                xsub[1] = int(
                    (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP']))
                ysub[0] = int(
                    (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                ysub[1] = int(
                    (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                print 'Subseting', ext, ' file with Latitude and Longitude...'
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                sys.exit(1)
        except:
            Geo = 0

        d = d[ysub[0]:ysub[1], xsub[0]:xsub[1]]

        print 'writing >>> ' + outName
        writefile.write_dem(d, outName)

        r['FILE_LENGTH'] = str(d.shape[0])
        r['WIDTH'] = str(d.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        r['subset_x0'] = str(xsub[0])
        r['subset_x1'] = str(xsub[1])
        r['subset_y0'] = str(ysub[0])
        r['subset_y1'] = str(ysub[1])

        try:
            r['Y_FIRST'] = str(
                float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP']))
            r['X_FIRST'] = str(
                float(r['X_FIRST']) + xsub[0] * float(r['X_STEP']))
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext in ['.jpeg', 'jpg', 'png']:
        import Image
        im = Image.open(File)

        try:
            r = readfile.read_rsc_file(File + '.rsc')
        except:
            sys.exit(1)

        try:
            Latsub
            Lonsub
            try:
                r['X_FIRST']
                xsub = [0] * 2
                ysub = [0] * 2
                xsub[0] = int(
                    (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP']))
                xsub[1] = int(
                    (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP']))
                ysub[0] = int(
                    (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                ysub[1] = int(
                    (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP']))
                print 'Subseting geocoded', ext, ' file with Latitude and Longitude...'
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                sys.exit(1)
        except:
            Geo = 0

        box = (xsub[0], ysub[0], xsub[1], ysub[1])
        output_img = im.crop(box)
        print 'writing >>> ' + outName
        output_img.save(outName)
        #    try:
        #      r=readfile.read_rsc_file(File+'.rsc')
        #    except:
        #      sys.exit(1)

        r['FILE_LENGTH'] = str(ysub[1] - ysub[0])
        r['WIDTH'] = str(xsub[1] - xsub[0])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        r['subset_x0'] = str(xsub[0])
        r['subset_x1'] = str(xsub[1])
        r['subset_y0'] = str(ysub[0])
        r['subset_y1'] = str(ysub[1])
        try:
            r['Y_FIRST'] = str(
                float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP']))
            r['X_FIRST'] = str(
                float(r['X_FIRST']) + xsub[0] * float(r['X_STEP']))
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()
Exemple #6
0
def main(argv):

  try:  
    file=argv[0]
    alks=float(argv[1])
    rlks=float(argv[2])
  except:
    Usage();sys.exit(1)



  ext = os.path.splitext(file)[1]


  outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks'+ext
  if ext == '.int' or ext == '.slc':
    a,p,r = readfile.read_complex64(file)
    plks=multilook(p,alks,rlks)
    alks=multilook(a,alks,rlks)


    r['FILE_LENGTH']=str(dlks.shape[0])
    r['WIDTH']=str(dlks.shape[1])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    try:
       r['Y_STEP']=str(float(r['Y_STEP'])*alks)
       r['X_STEP']=str(float(r['X_STEP'])*rlks)
    except:
       Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()   

  elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
    a,p,r = readfile.read_float32(file)
    plks=multilook(p,alks,rlks)
    alks=multilook(a,alks,rlks)
    
    writefile.write_float32(plks,outName)

    r['FILE_LENGTH']=str(dlks.shape[0])
    r['WIDTH']=str(dlks.shape[1])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    
    try:
       r['Y_STEP']=str(float(r['Y_STEP'])*alks)
       r['X_STEP']=str(float(r['X_STEP'])*rlks)
    except:
       Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()

  elif ext == ('.dem'):
    d,r = readfile.read_dem(file)
    dlks=multilook(d,alks,rlks)

    print 'writing '+outName
    writefile.write_dem(dlks,outName)
    
    r['FILE_LENGTH']=str(dlks.shape[0])
    r['WIDTH']=str(dlks.shape[1])
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)

    try:
      r['Y_STEP']=str(float(r['Y_STEP'])*alks)
      r['X_STEP']=str(float(r['X_STEP'])*rlks)
    except:
      Geo=0

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()

  elif ext in ['.jpeg','jpg','png']:

    import Image
    im = Image.open(file)

    width = im.size[0] / int(rlks)
    height = im.size[1] / int(alks)

    imlks = im.resize((width, height), Image.NEAREST)
    print 'writing ' + outName
    imlks.save(outName)

    try:
      r=readfile.read_rsc_file(file+'.rsc')
    except:
      sys.exit(1)

    r['FILE_LENGTH']=str(height)
    r['WIDTH']=str(width)
    r['XMAX']=str(int(r['WIDTH']) - 1)
    r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    try:
      r['Y_STEP']=str(float(r['Y_STEP'])*alks)
      r['X_STEP']=str(float(r['X_STEP'])*rlks)
    except:
      Geo=0
    
    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()


  elif ext == ('.h5'):

    h5file=h5py.File(file,'r')
   # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
    h5file_lks=h5py.File(outName,'w')
  
    if 'interferograms' in h5file.keys():
      print 'Multilooking the interferograms'
      gg = h5file_lks.create_group('interferograms')
      igramList=h5file['interferograms'].keys()
      for igram in igramList:
        print igram
        unw = h5file['interferograms'][igram].get(igram)
        unwlks=multilook(unw,alks,rlks)
        group = gg.create_group(igram)
        dset = group.create_dataset(igram, data=unwlks, compression='gzip')
        for key, value in h5file['interferograms'][igram].attrs.iteritems():
            group.attrs[key] = value
        group.attrs['WIDTH']=unwlks.shape[1]
        group.attrs['FILE_LENGTH']=unwlks.shape[0]
        try:
           group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP'])
           group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP'])
        except:
           group.attrs['AZIMUTH_PIXEL_SIZE']=alks*float(group.attrs['AZIMUTH_PIXEL_SIZE'])
           group.attrs['RANGE_PIXEL_SIZE']=rlks*float(group.attrs['RANGE_PIXEL_SIZE'])         

      dset1=h5file['mask'].get('mask')
      mask=dset1[0:dset1.shape[0],0:dset1.shape[1]]
      masklks=multilook(mask,alks,rlks)
      group=h5file_lks.create_group('mask')
      dset = group.create_dataset('mask', data=masklks, compression='gzip')

    elif 'timeseries' in h5file.keys():
      print 'Multilooking the time-series'
      group = h5file_lks.create_group('timeseries')
      dateList=h5file['timeseries'].keys()
      for d in dateList:
        print d
        unw = h5file['timeseries'].get(d)
        unwlks=multilook(unw,alks,rlks)
        dset = group.create_dataset(d, data=unwlks, compression='gzip')      

      for key,value in h5file['timeseries'].attrs.iteritems():
        group.attrs[key] = value
      group.attrs['WIDTH']=unwlks.shape[1]
      group.attrs['FILE_LENGTH']=unwlks.shape[0]

      try:
           group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP'])
           group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP'])
      except:
           group.attrs['AZIMUTH_PIXEL_SIZE']=alks*float(group.attrs['AZIMUTH_PIXEL_SIZE'])
           group.attrs['RANGE_PIXEL_SIZE']=rlks*float(group.attrs['RANGE_PIXEL_SIZE'])


      try:
        dset1 = h5file['mask'].get('mask')
        Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
        Masklks=multilook(Mask,alks,rlks)
        group=h5file_lks.create_group('mask')
        dset = group.create_dataset('mask', data=Masklks, compression='gzip')
      except:
        print 'Multilooked file does not include the maske'


    elif 'temporal_coherence' in h5file.keys() or 'velocity' in h5file.keys() or 'mask' in h5file.keys():
      k=h5file.keys()
      print 'multi looking the '+ k[0]
   
      group=h5file_lks.create_group(k[0])    
      dset1 = h5file[k[0]].get(k[0])
      Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
      Masklks=multilook(Mask,alks,rlks)
      dset = group.create_dataset(k[0], data=Masklks, compression='gzip')
      for key , value in h5file[k[0]].attrs.iteritems():
         group.attrs[key]=value

      try:
           group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP'])
           group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP'])
      except:
           group.attrs['AZIMUTH_PIXEL_SIZE']=alks*float(group.attrs['AZIMUTH_PIXEL_SIZE'])
           group.attrs['RANGE_PIXEL_SIZE']=rlks*float(group.attrs['RANGE_PIXEL_SIZE'])
    group.attrs['WIDTH']=Masklks.shape[1]
    group.attrs['FILE_LENGTH']=Masklks.shape[0]
    h5file.close()
    h5file_lks.close()