def read_HDFEOS(inps): """read displacement from HDFEOS""" print('read displacement, incidence and azimuth information') # read metadata HDFEOS_file = inps.input_HDFEOS[0] atr = readfile.read_attribute(HDFEOS_file) if inps.date == None: date1 = atr['START_DATE'] date2 = atr['END_DATE'] else: # date1 and date2 if '_' in "".join(inps.date): date1, date2 = ptime.yyyymmdd("".join(inps.date).split('_')) else: date1 = atr['START_DATE'] date2 = ptime.yyyymmdd("".join(inps.date)) # read angle infomation azimuth = readfile.read(HDFEOS_file, datasetName='/HDFEOS/GRIDS/timeseries/geometry/azimuthAngle')[0] incidence = readfile.read(HDFEOS_file, datasetName='/HDFEOS/GRIDS/timeseries/geometry/incidenceAngle')[0] if inps.velocity: vel_file = 'velocity.h5' iargs = [HDFEOS_file, '--start-date', date1, '--end-date', date2, '-o', vel_file, '--update'] print('\ntimeseries2velocity.py', ' '.join(iargs)) mintpy.timeseries2velocity.main(iargs) data = readfile.read(vel_file, datasetName='velocity')[0] os.remove(vel_file) else: # read / prepare data slice_list = readfile.get_slice_list(HDFEOS_file) # read/prepare data dname = 'displacement' slice_name1 = view.search_dataset_input(slice_list, '{}-{}'.format(dname, date1))[0][0] slice_name2 = view.search_dataset_input(slice_list, '{}-{}'.format(dname, date2))[0][1] data = readfile.read("".join(inps.input_HDFEOS), datasetName=slice_name2)[0] data -= readfile.read("".join(inps.input_HDFEOS), datasetName=slice_name1)[0] print("mask file") maskfile = readfile.read(HDFEOS_file, datasetName='/HDFEOS/GRIDS/timeseries/quality/mask')[0] data[maskfile == 0] = np.nan azimuth[maskfile == 0] = np.nan incidence[maskfile == 0] = np.nan return date1, date2, data, atr, incidence, azimuth
def extract_data(inps, dataset, outdir): """extract data from HDFEOS file based on the given attribute""" # read HDFEOS file # metadata atr = readfile.read_attribute("".join(inps.input_HDFEOS)) attr = "".join(inps.attribute) # read 2d data if attr == 'displacement' or attr == 'velocity': if inps.date == None: date1 = atr['START_DATE'] date2 = atr['END_DATE'] #raise Exception("ERROR! Date for displacement must be given!") else: # date1 and date2 if '_' in "".join(inps.date): date1, date2 = ptime.yyyymmdd("".join(inps.date).split('_')) else: date1 = atr['START_DATE'] date2 = ptime.yyyymmdd("".join(inps.date)) #date12 = '{}_{}'.format(date1, date2) if attr == 'displacement': # read / prepare data slice_list = readfile.get_slice_list("".join(inps.input_HDFEOS)) # read/prepare data dname = 'displacement' slice_name1 = view.search_dataset_input( slice_list, '{}-{}'.format(dname, date1))[0][0] slice_name2 = view.search_dataset_input( slice_list, '{}-{}'.format(dname, date2))[0][1] data = readfile.read("".join(inps.input_HDFEOS), datasetName=slice_name2)[0] data -= readfile.read("".join(inps.input_HDFEOS), datasetName=slice_name1)[0] data_name = '{}_{}_{}'.format(attr, date1, date2) if inps.mask: print("mask file") maskfile = readfile.read( "".join(inps.input_HDFEOS), datasetName='/HDFEOS/GRIDS/timeseries/quality/mask')[0] data[maskfile == 0] = np.nan outfile = outdir + '/' + data_name + '.h5' atr['FILE_TYPE'] = '.unw' writefile.write(data, out_file=outfile, metadata=atr) #print('converting range to phase') #data *= range2phase #if inps.ref_yx: # data -= data[inps.ref_yx[0], inps.ref_yx[1]] elif attr == 'velocity': maskfile = readfile.read( "".join(inps.input_HDFEOS), datasetName='/HDFEOS/GRIDS/timeseries/quality/mask')[0] dname = 'displacement' data_timeseries = readfile.read("".join(inps.input_HDFEOS), datasetName=dataset + dname)[0] if inps.mask: print("mask file") data_timeseries[:, maskfile == 0] = np.nan bperp_date = h5py.File("".join(inps.input_HDFEOS), 'r') data_bperp = bperp_date[(dataset + 'bperp')] data_date = bperp_date[(dataset + 'date')] ts_data_name = 'timeseries_ERA_demErr.h5' outfile = outdir + '/' + ts_data_name # write to HDF5 file dsDict = dict() dsDict['bperp'] = data_bperp dsDict['date'] = data_date dsDict['timeseries'] = data_timeseries atr['FILE_TYPE'] = attr writefile.write(dsDict, out_file=outfile, metadata=atr) # run timeseries2velocity.py output_vel = 'velocity_' + date1 + '_' + date2 + '.h5' data_name = output_vel.split('.')[0] os.chdir(outdir) completion_status = os.system( multitrack_utilities.seperate_str_byspace([ 'timeseries2velocity.py', outfile, '--start-date', date1, '--end-date', date2, '--exclude', '../exclude_date.txt', '--output', output_vel ])) if completion_status == 1: raise Exception('error when generate velocity!') data_vel, vel_atr = readfile.read(output_vel, datasetName='velocity') data = data_vel if inps.mask: print("mask file") data[~maskfile] = np.nan writefile.write(data, out_file=output_vel, metadata=vel_atr) os.chdir('../') else: data = readfile.read("".join(inps.input_HDFEOS), datasetName=dataset + attr)[0] outfile = outdir + '/' + attr + '.h5' data_name = attr atr['FILE_TYPE'] = attr if inps.mask: print("mask file") maskfile = readfile.read( "".join(inps.input_HDFEOS), datasetName='/HDFEOS/GRIDS/timeseries/quality/mask')[0] data[maskfile == 0] = np.nan writefile.write(data, out_file=outfile, metadata=atr) # mask data #if inps.mask: # print("mask file") # maskfile = readfile.read("".join(inps.input_HDFEOS),datasetName='/HDFEOS/GRIDS/timeseries/quality/mask')[0] # data[maskfile == 0] = np.nan return data, data_name, atr
def read_data(inps): # metadata atr = readfile.read_attribute(inps.file) if 'WAVELENGTH' in atr.keys(): range2phase = -4 * np.pi / float(atr['WAVELENGTH']) # change reference pixel if inps.ref_lalo: if 'Y_FIRST' in atr.keys(): coord = ut.coordinate(atr) ref_y, ref_x = coord.geo2radar(inps.ref_lalo[0], inps.ref_lalo[1])[0:2] inps.ref_yx = [ref_y, ref_x] else: raise ValueError("input file is not geocoded --> reference point in lat/lon is NOT support") if inps.ref_yx: atr['REF_Y'] = inps.ref_yx[0] atr['REF_X'] = inps.ref_yx[1] if 'Y_FIRST' in atr.keys(): coord = ut.coordinate(atr) ref_lat, ref_lon = coord.radar2geo(inps.ref_yx[0], inps.ref_yx[1])[0:2] atr['REF_LAT'] = ref_lat atr['REF_LON'] = ref_lon print('change reference point to y/x: {}'.format(inps.ref_yx)) # various file types print('read {} from file {}'.format(inps.dset, inps.file)) k = atr['FILE_TYPE'] if k == 'velocity': # read/prepare data data = readfile.read(inps.file)[0] # velocity to displacement print('convert velocity to displacement for {}'.format(atr['DATE12'])) date1, date2 = atr['DATE12'].split('_') dt1, dt2 = ptime.date_list2vector([date1, date2])[0] data *= (dt2 - dt1).days / 365.25 # displacement to phase print('convert displacement to phase in radian') data *= range2phase if inps.ref_yx: data -= data[inps.ref_yx[0], inps.ref_yx[1]] # metadata atr['FILE_TYPE'] = '.unw' atr['UNIT'] = 'radian' # output filename if not inps.outfile: inps.outfile = os.path.join(os.path.dirname(inps.file), '{}.unw'.format(atr['DATE12'])) elif k == 'timeseries': # date1 and date2 if '_' in inps.dset: date1, date2 = ptime.yyyymmdd(inps.dset.split('_')) else: date1 = atr['REF_DATE'] date2 = ptime.yyyymmdd(inps.dset) # read/prepare data data = readfile.read(inps.file, datasetName=date2)[0] data -= readfile.read(inps.file, datasetName=date1)[0] print('converting range to phase') data *= range2phase if inps.ref_yx: data -= data[inps.ref_yx[0], inps.ref_yx[1]] # metadata atr['DATE'] = date1[2:8] atr['DATE12'] = '{}-{}'.format(date1[2:8], date2[2:8]) atr['FILE_TYPE'] = '.unw' atr['UNIT'] = 'radian' # output filename if not inps.outfile: inps.outfile = '{}_{}.unw'.format(date1, date2) if inps.file.startswith('geo_'): inps.outfile = 'geo_'+inps.outfile elif k == 'HDFEOS': dname = inps.dset.split('-')[0] # date1 and date2 if dname == 'displacement': if '-' in inps.dset: suffix = inps.dset.split('-')[1] if '_' in suffix: date1, date2 = ptime.yyyymmdd(suffix.split('_')) else: date1 = atr['REF_DATE'] date2 = ptime.yyyymmdd(suffix) else: raise ValueError("No '-' in input dataset! It is required for {}".format(dname)) else: date_list = HDFEOS(inps.file).get_date_list() date1 = date_list[0] date2 = date_list[-1] date12 = '{}_{}'.format(date1, date2) # read / prepare data slice_list = readfile.get_slice_list(inps.file) if 'displacement' in inps.dset: # read/prepare data slice_name1 = view.search_dataset_input(slice_list, '{}-{}'.format(dname, date1))[0][0] slice_name2 = view.search_dataset_input(slice_list, '{}-{}'.format(dname, date2))[0][0] data = readfile.read(inps.file, datasetName=slice_name1)[0] data -= readfile.read(inps.file, datasetName=slice_name2)[0] print('converting range to phase') data *= range2phase if inps.ref_yx: data -= data[inps.ref_yx[0], inps.ref_yx[1]] else: slice_name = view.search_dataset_input(slice_list, inps.dset)[0][0] data = readfile.read(inps.file, datasetName=slice_name)[0] # metadata atr['DATE'] = date1[2:8] atr['DATE12'] = '{}-{}'.format(date1[2:8], date2[2:8]) if dname == 'displacement': atr['FILE_TYPE'] = '.unw' atr['UNIT'] = 'radian' elif 'coherence' in dname.lower(): atr['FILE_TYPE'] = '.cor' atr['UNIT'] = '1' elif dname == 'height': atr['FILE_TYPE'] = '.dem' atr['DATA_TYPE'] = 'int16' else: raise ValueError('unrecognized input dataset type: {}'.format(inps.dset)) # output filename if not inps.outfile: inps.outfile = '{}{}'.format(date12, atr['FILE_TYPE']) elif k == 'ifgramStack': dname, date12 = inps.dset.split('-') date1, date2 = date12.split('_') # read / prepare data data = readfile.read(inps.file, datasetName=inps.dset)[0] if dname.startswith('unwrapPhase'): if 'REF_X' in atr.keys(): data -= data[int(atr['REF_Y']), int(atr['REF_X'])] print('consider reference pixel in y/x: ({}, {})'.format(atr['REF_Y'], atr['REF_X'])) else: print('No REF_Y/X found.') # metadata atr['DATE'] = date1[2:8] atr['DATE12'] = '{}-{}'.format(date1[2:8], date2[2:8]) if dname.startswith('unwrapPhase'): atr['FILE_TYPE'] = '.unw' atr['UNIT'] = 'radian' elif dname == 'coherence': atr['FILE_TYPE'] = '.cor' atr['UNIT'] = '1' elif dname == 'wrapPhase': atr['FILE_TYPE'] = '.int' atr['UNIT'] = 'radian' elif dname == 'connectComponent': atr['FILE_TYPE'] = '.conncomp' atr['UNIT'] = '1' atr['DATA_TYPE'] = 'byte' else: raise ValueError('unrecognized dataset type: {}'.format(inps.dset)) # output filename if not inps.outfile: inps.outfile = '{}{}'.format(date12, atr['FILE_TYPE']) if inps.file.startswith('geo_'): inps.outfile = 'geo_'+inps.outfile else: # read data data = readfile.read(inps.file, datasetName=inps.dset)[0] if inps.outfile: fext = os.path.splitext(inps.outfile)[1] atr['FILE_TYPE'] = fext else: # metadata if 'coherence' in k.lower(): atr['FILE_TYPE'] = '.cor' elif k in ['mask']: atr['FILE_TYPE'] = '.msk' elif k in ['geometry'] and inps.dset == 'height': if 'Y_FIRST' in atr.keys(): atr['FILE_TYPE'] = '.dem' else: atr['FILE_TYPE'] = '.hgt' atr['UNIT'] = 'm' else: atr['FILE_TYPE'] = '.unw' inps.outfile = '{}{}'.format(os.path.splitext(inps.file)[0], atr['FILE_TYPE']) # mask if inps.mask_file: for m_file in inps.mask_file: print('mask data based on input file: {}'.format(m_file)) mask = readfile.read(m_file)[0] mask *= ~np.isnan(data) data[mask==0] = np.nan # get rid of starting . if output as hdf5 file if inps.outfile.endswith('.h5'): if atr['FILE_TYPE'].startswith('.'): atr['FILE_TYPE'] = atr['FILE_TYPE'][1:] atr['PROCESSOR'] = 'roipac' return data, atr, inps.outfile