def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() ver = "1.2.5" date = 20210105 author = "Y. Morishita" print("\n{} ver{} {} {}".format(os.path.basename(argv[0]), ver, date, author), flush=True) print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) ### For parallel processing global ifgdates2, in_dir, out_dir, length, width, x1, x2, y1, y2, cycle, cmap_wrap #%% Set default in_dir = [] out_dir = [] range_str = [] range_geo_str = [] try: n_para = len(os.sched_getaffinity(0)) except: n_para = multi.cpu_count() q = multi.get_context('fork') cmap_wrap = SCM.romaO #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "hi:o:r:g:", ["help", "n_para="]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-i': in_dir = a elif o == '-o': out_dir = a elif o == '-r': range_str = a elif o == '-g': range_geo_str = a elif o == '--n_para': n_para = int(a) if not in_dir: raise Usage('No input directory given, -i is not optional!') if not out_dir: raise Usage('No output directory given, -o is not optional!') if not range_str and not range_geo_str: raise Usage('No clip area given, use either -r or -g!') if range_str and range_geo_str: raise Usage('Both -r and -g given, use either -r or -g not both!') elif not os.path.isdir(in_dir): raise Usage('No {} dir exists!'.format(in_dir)) elif not os.path.exists(os.path.join(in_dir, 'slc.mli.par')): raise Usage('No slc.mli.par file exists in {}!'.format(in_dir)) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Read info and make dir in_dir = os.path.abspath(in_dir) out_dir = os.path.abspath(out_dir) ifgdates = tools_lib.get_ifgdates(in_dir) n_ifg = len(ifgdates) mlipar = os.path.join(in_dir, 'slc.mli.par') width = int(io_lib.get_param_par(mlipar, 'range_samples')) length = int(io_lib.get_param_par(mlipar, 'azimuth_lines')) speed_of_light = 299792458 #m/s radar_frequency = float(io_lib.get_param_par(mlipar, 'radar_frequency')) #Hz wavelength = speed_of_light / radar_frequency #meter if wavelength > 0.2: ## L-band cycle = 1.5 # 2pi/cycle for png else: ## C-band cycle = 3 # 2pi*3/cycle for png dempar = os.path.join(in_dir, 'EQA.dem_par') lat1 = float(io_lib.get_param_par(dempar, 'corner_lat')) # north lon1 = float(io_lib.get_param_par(dempar, 'corner_lon')) # west postlat = float(io_lib.get_param_par(dempar, 'post_lat')) # negative postlon = float(io_lib.get_param_par(dempar, 'post_lon')) # positive lat2 = lat1 + postlat * (length - 1) # south lon2 = lon1 + postlon * (width - 1) # east if not os.path.exists(out_dir): os.mkdir(out_dir) #%% Check and set range to be clipped ### Read -r or -g option if range_str: ## -r if not tools_lib.read_range(range_str, width, length): print('\nERROR in {}\n'.format(range_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range(range_str, width, length) else: ## -g if not tools_lib.read_range_geo(range_geo_str, width, length, lat1, postlat, lon1, postlon): print('\nERROR in {}\n'.format(range_geo_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range_geo(range_geo_str, width, length, lat1, postlat, lon1, postlon) range_str = '{}:{}/{}:{}'.format(x1, x2, y1, y2) ### Calc clipped info width_c = x2 - x1 length_c = y2 - y1 lat1_c = lat1 + postlat * y1 # north lon1_c = lon1 + postlon * x1 # west lat2_c = lat1_c + postlat * (length_c - 1) # south lon2_c = lon1_c + postlon * (width_c - 1) # east print("\nArea to be clipped:", flush=True) print(" 0:{}/0:{} -> {}:{}/{}:{}".format(width, length, x1, x2, y1, y2)) print(" {:.7f}/{:.7f}/{:.7f}/{:.7f} ->".format(lon1, lon2, lat2, lat1)) print(" {:.7f}/{:.7f}/{:.7f}/{:.7f}".format(lon1_c, lon2_c, lat2_c, lat1_c)) print(" Width/Length: {}/{} -> {}/{}".format(width, length, width_c, length_c)) print("", flush=True) clipareafile = os.path.join(out_dir, 'cliparea.txt') with open(clipareafile, 'w') as f: f.write(range_str) #%% Make clipped par files mlipar_c = os.path.join(out_dir, 'slc.mli.par') dempar_c = os.path.join(out_dir, 'EQA.dem_par') ### slc.mli.par with open(mlipar, 'r') as f: file = f.read() file = re.sub(r'range_samples:\s*{}'.format(width), 'range_samples: {}'.format(width_c), file) file = re.sub(r'azimuth_lines:\s*{}'.format(length), 'azimuth_lines: {}'.format(length_c), file) with open(mlipar_c, 'w') as f: f.write(file) ### EQA.dem_par with open(dempar, 'r') as f: file = f.read() file = re.sub(r'width:\s*{}'.format(width), 'width: {}'.format(width_c), file) file = re.sub(r'nlines:\s*{}'.format(length), 'nlines: {}'.format(length_c), file) file = re.sub(r'corner_lat:\s*{}'.format(lat1), 'corner_lat: {}'.format(lat1_c), file) file = re.sub(r'corner_lon:\s*{}'.format(lon1), 'corner_lon: {}'.format(lon1_c), file) with open(dempar_c, 'w') as f: f.write(file) #%% Clip or copy other files than unw and cc files = sorted(glob.glob(os.path.join(in_dir, '*'))) for file in files: if os.path.isdir(file): continue #not copy directory elif file == mlipar or file == dempar: continue #not copy elif os.path.getsize(file) == width * length * 4: ##float file print('Clip {}'.format(os.path.basename(file)), flush=True) data = io_lib.read_img(file, length, width) data = data[y1:y2, x1:x2] filename = os.path.basename(file) outfile = os.path.join(out_dir, filename) data.tofile(outfile) elif file == os.path.join(in_dir, 'slc.mli.png'): print('Recreate slc.mli.png', flush=True) mli = io_lib.read_img(os.path.join(out_dir, 'slc.mli'), length_c, width_c) pngfile = os.path.join(out_dir, 'slc.mli.png') plot_lib.make_im_png(mli, pngfile, 'gray', 'MLI', cbar=False) elif file == os.path.join(in_dir, 'hgt.png'): print('Recreate hgt.png', flush=True) hgt = io_lib.read_img(os.path.join(out_dir, 'hgt'), length_c, width_c) vmax = np.nanpercentile(hgt, 99) vmin = -vmax / 3 ## bnecause 1/4 of terrain is blue pngfile = os.path.join(out_dir, 'hgt.png') plot_lib.make_im_png(hgt, pngfile, 'terrain', 'DEM (m)', vmin, vmax, cbar=True) else: print('Copy {}'.format(os.path.basename(file)), flush=True) shutil.copy(file, out_dir) #%% Clip unw and cc print('\nClip unw and cc', flush=True) ### First, check if already exist ifgdates2 = [] for ifgix, ifgd in enumerate(ifgdates): out_dir1 = os.path.join(out_dir, ifgd) unwfile_c = os.path.join(out_dir1, ifgd + '.unw') ccfile_c = os.path.join(out_dir1, ifgd + '.cc') if not (os.path.exists(unwfile_c) and os.path.exists(ccfile_c)): ifgdates2.append(ifgd) n_ifg2 = len(ifgdates2) if n_ifg - n_ifg2 > 0: print(" {0:3}/{1:3} clipped unw and cc already exist. Skip".format( n_ifg - n_ifg2, n_ifg), flush=True) if n_ifg2 > 0: ### Clip with parallel processing if n_para > n_ifg2: n_para = n_ifg2 print(' {} parallel processing...'.format(n_para), flush=True) p = q.Pool(n_para) p.map(clip_wrapper, range(n_ifg2)) p.close() #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output directory: {}\n'.format(os.path.relpath(out_dir)))
def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() ver = 1.2 date = 20200703 author = "Y. Morishita" print("\n{} ver{} {} {}".format(os.path.basename(argv[0]), ver, date, author), flush=True) print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) #%% Set default xy_str = [] lonlat_str = [] cumfile = 'cum_filt.h5' tsfile = [] refarea = [] refarea_geo = [] maskfile = [] #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "hp:g:i:o:r:", ["help", "ref_geo=", "mask="]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-p': xy_str = a elif o == '-g': lonlat_str = a elif o == '-i': cumfile = a elif o == '-o': tsfile = a elif o == '-r': refarea = a elif o == '--ref_geo': refarea_geo = a elif o == '--mask': maskfile = a if not xy_str and not lonlat_str: raise Usage('No point location given, use either -p or -g!') elif not os.path.exists(cumfile): raise Usage('No {} exists! Use -i option.'.format(cumfile)) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Read info ### Read cumfile cumh5 = h5.File(cumfile, 'r') cum = cumh5['cum'] gap = cumh5['gap'] imdates = cumh5['imdates'][()].astype(str).tolist() n_im, length, width = cum.shape if 'corner_lat' in list(cumh5.keys()): geocod_flag = True lat1 = float(cumh5['corner_lat'][()]) lon1 = float(cumh5['corner_lon'][()]) dlat = float(cumh5['post_lat'][()]) dlon = float(cumh5['post_lon'][()]) else: geocod_flag = False if 'deramp_flag' in list(cumh5.keys()): deramp_flag = cumh5['deramp_flag'][()] else: deramp_flag = None if 'hgt_linear_flag' in list(cumh5.keys()): hgt_linear_flag = cumh5['hgt_linear_flag'][()] else: hgt_linear_flag = None if 'filtwidth_km' in list(cumh5.keys()): filtwidth_km = float(cumh5['filtwidth_km'][()]) filtwidth_yr = float(cumh5['filtwidth_yr'][()]) else: filtwidth_km = filtwidth_yr = None #%% Set info ###Set ref area if refarea: if not tools_lib.read_range(refarea, width, length): print('\nERROR in {}\n'.format(refarea), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range( refarea, width, length) elif refarea_geo and geocod_flag: lat1 = float(cumh5['corner_lat'][()]) lon1 = float(cumh5['corner_lon'][()]) dlat = float(cumh5['post_lat'][()]) dlon = float(cumh5['post_lon'][()]) if not tools_lib.read_range_geo(refarea_geo, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(refarea_geo), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range_geo( refarea_geo, width, length, lat1, dlat, lon1, dlon) else: refarea = cumh5['refarea'][()] refx1, refx2, refy1, refy2 = [ int(s) for s in re.split('[:/]', refarea) ] if geocod_flag: reflat2, reflon1 = tools_lib.xy2bl(refx1, refy1, lat1, dlat, lon1, dlon) reflat1, reflon2 = tools_lib.xy2bl(refx2 - 1, refy2 - 1, lat1, dlat, lon1, dlon) else: reflat1 = reflon1 = reflat2 = reflon2 = None ### Set point if xy_str: ## -p option x, y = [int(s) for s in xy_str.split('/')] if not 1 <= x <= width: print("\nERROR: {} is out of range ({}-{})".format( x, 0, width - 1), file=sys.stderr) return 2 elif not 1 <= y <= length: print("\nERROR: {} is out of range ({}-{})".format( y, 0, length - 1), file=sys.stderr) return 2 if geocod_flag: lat, lon = tools_lib.xy2bl(x, y, lat1, dlat, lon1, dlon) else: lat = lon = None else: ## -g option if not geocod_flag: print('\nERROR: not geocoded, -g option unavailable\n', file=sys.stderr) return 2 lat2 = lat1 + dlat * (length - 1) lon2 = lon1 + dlon * (width - 1) lon, lat = [float(s) for s in lonlat_str.split('/')] if not lon1 <= lon <= lon2: print("\nERROR: {} is out of range ({}-{})".format( lon, lon1, lon2), file=sys.stderr) return 2 elif not lat2 <= lat <= lat1: print("\nERROR: {} is out of range ({}-{})".format( lat, lat2, lat1), file=sys.stderr) return 2 x, y = tools_lib.bl2xy(lon, lat, width, length, lat1, dlat, lon1, dlon) ## update latlon lat, lon = tools_lib.xy2bl(x, y, lat1, dlat, lon1, dlon) if geocod_flag: print('Location: {:.5f}/{:.5f}'.format(lon, lat)) if not tsfile: tsfile = 'ts_{}_{}.txt'.format(x, y) ### Gaps gap1 = gap[:, y, x] ### mask if maskfile: mask = io_lib.read_img(maskfile, length, width) mask[mask == 0] = np.nan else: mask = np.ones((length, width), dtype=np.float32) #%% Read cum data ts = cum[:, y, x] * mask[y, x] if np.all(np.isnan(ts)): print('\nERROR: All cum data are Nan at {}/{}!\n'.format(x, y), file=sys.stderr) return 2 ts_ref = np.nanmean(cum[:, refy1:refy2, refx1:refx2] * mask[refy1:refy2, refx1:refx2], axis=(1, 2)) if np.all(np.isnan(ts_ref)): print('\nERROR: Ref area has only NaN value!\n', file=sys.stderr) return 2 ts_dif = ts - ts_ref ts_dif = ts_dif - ts_dif[0] ## Make first date zero ### Make txt io_lib.make_tstxt(x, y, imdates, ts_dif, tsfile, refx1, refx2, refy1, refy2, gap1, lat=lat, lon=lon, reflat1=reflat1, reflat2=reflat2, reflon1=reflon1, reflon2=reflon2, deramp_flag=deramp_flag, hgt_linear_flag=hgt_linear_flag, filtwidth_km=filtwidth_km, filtwidth_yr=filtwidth_yr) #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output: {}\n'.format(tsfile), flush=True)
dlon = float(cumh5['post_lon'][()]) aspect = np.abs(dlat/dlon/np.cos(np.deg2rad(lat1+dlat*length/2))) except: geocod_flag = False aspect = 1 print('No latlon field found in {}. Skip.'.format(cumfile)) ### Set initial ref area if refarea: if not tools_lib.read_range(refarea, width, length): print('\nERROR in {}\n'.format(refarea), file=sys.stderr) sys.exit(2) else: refx1, refx2, refy1, refy2 = tools_lib.read_range(refarea, width, length) elif refarea_geo and geocod_flag: if not tools_lib.read_range_geo(refarea_geo, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(refarea_geo), file=sys.stderr) sys.exit(2) else: refx1, refx2, refy1, refy2 = tools_lib.read_range_geo(refarea_geo, width, length, lat1, dlat, lon1, dlon) else: refarea = cumh5['refarea'][()] refx1, refx2, refy1, refy2 = [int(s) for s in re.split('[:/]', refarea)] refx1h = refx1-0.5; refx2h = refx2-0.5 ## Shift half for plot refy1h = refy1-0.5; refy2h = refy2-0.5 ### Set initial point if point: if not tools_lib.read_point(point, width, length):
def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) #%% Set default in_dir = [] out_dir = [] range_str = [] range_geo_str = [] #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "hi:o:r:g:", ["help"]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-i': in_dir = a elif o == '-o': out_dir = a elif o == '-r': range_str = a elif o == '-g': range_geo_str = a if not in_dir: raise Usage('No input directory given, -i is not optional!') if not out_dir: raise Usage('No output directory given, -o is not optional!') if not range_str and not range_geo_str: raise Usage('No clip area given, use either -r or -g!') if range_str and range_geo_str: raise Usage('Both -r and -g given, use either -r or -g not both!') elif not os.path.isdir(in_dir): raise Usage('No {} dir exists!'.format(in_dir)) elif not os.path.exists(os.path.join(in_dir, 'slc.mli.par')): raise Usage('No slc.mli.par file exists in {}!'.format(in_dir)) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Read info and make dir in_dir = os.path.abspath(in_dir) out_dir = os.path.abspath(out_dir) ifgdates = tools_lib.get_ifgdates(in_dir) n_ifg = len(ifgdates) mlipar = os.path.join(in_dir, 'slc.mli.par') width = int(io_lib.get_param_par(mlipar, 'range_samples')) length = int(io_lib.get_param_par(mlipar, 'azimuth_lines')) speed_of_light = 299792458 #m/s radar_frequency = float(io_lib.get_param_par(mlipar, 'radar_frequency')) #Hz wavelength = speed_of_light / radar_frequency #meter if wavelength > 0.2: ## L-band cycle = 1.5 # 2pi/cycle for png else: ## C-band cycle = 3 # 2pi*3/cycle for png dempar = os.path.join(in_dir, 'EQA.dem_par') lat1 = float(io_lib.get_param_par(dempar, 'corner_lat')) # north lon1 = float(io_lib.get_param_par(dempar, 'corner_lon')) # west postlat = float(io_lib.get_param_par(dempar, 'post_lat')) # negative postlon = float(io_lib.get_param_par(dempar, 'post_lon')) # positive lat2 = lat1 + postlat * (length - 1) # south lon2 = lon1 + postlon * (width - 1) # east if not os.path.exists(out_dir): os.mkdir(out_dir) #%% Check and set range to be clipped ### Read -r or -g option if range_str: ## -r if not tools_lib.read_range(range_str, width, length): print('\nERROR in {}\n'.format(range_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range(range_str, width, length) else: ## -g if not tools_lib.read_range_geo(range_geo_str, width, length, lat1, postlat, lon1, postlon): print('\nERROR in {}\n'.format(range_geo_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range_geo(range_geo_str, width, length, lat1, postlat, lon1, postlon) range_str = '{}:{}/{}:{}'.format(x1, x2, y1, y2) ### Calc clipped info width_c = x2 - x1 length_c = y2 - y1 lat1_c = lat1 + postlat * y1 # north lon1_c = lon1 + postlon * x1 # west lat2_c = lat1_c + postlat * (length_c - 1) # south lon2_c = lon1_c + postlon * (width_c - 1) # east print("\nArea to be clipped:", flush=True) print(" 0:{}/0:{} -> {}:{}/{}:{}".format(width, length, x1, x2, y1, y2)) print(" {:.7f}/{:.7f}/{:.7f}/{:.7f} ->".format(lon1, lon2, lat2, lat1)) print(" {:.7f}/{:.7f}/{:.7f}/{:.7f}".format(lon1_c, lon2_c, lat2_c, lat1_c)) print(" Width/Length: {}/{} -> {}/{}".format(width, length, width_c, length_c)) print("", flush=True) clipareafile = os.path.join(out_dir, 'cliparea.txt') with open(clipareafile, 'w') as f: f.write(range_str) #%% Make clipped par files mlipar_c = os.path.join(out_dir, 'slc.mli.par') dempar_c = os.path.join(out_dir, 'EQA.dem_par') ### slc.mli.par with open(mlipar, 'r') as f: file = f.read() file = re.sub(r'range_samples:\s*{}'.format(width), 'range_samples: {}'.format(width_c), file) file = re.sub(r'azimuth_lines:\s*{}'.format(length), 'azimuth_lines: {}'.format(length_c), file) with open(mlipar_c, 'w') as f: f.write(file) ### EQA.dem_par with open(dempar, 'r') as f: file = f.read() file = re.sub(r'width:\s*{}'.format(width), 'width: {}'.format(width_c), file) file = re.sub(r'nlines:\s*{}'.format(length), 'nlines: {}'.format(length_c), file) file = re.sub(r'corner_lat:\s*{}'.format(lat1), 'corner_lat: {}'.format(lat1_c), file) file = re.sub(r'corner_lon:\s*{}'.format(lon1), 'corner_lon: {}'.format(lon1_c), file) with open(dempar_c, 'w') as f: f.write(file) #%% Clip or copy other files than unw and cc files = glob.glob(os.path.join(in_dir, '*')) for file in files: if os.path.isdir(file): continue #not copy directory elif file == mlipar or file == dempar: continue #not copy elif os.path.getsize(file) == width * length * 4: ##float file print('Clip {}'.format(os.path.basename(file)), flush=True) data = io_lib.read_img(file, length, width) data = data[y1:y2, x1:x2] filename = os.path.basename(file) outfile = os.path.join(out_dir, filename) data.tofile(outfile) elif file == os.path.join(in_dir, 'slc.mli.png'): print('Recreate slc.mli.png', flush=True) mli = io_lib.read_img(os.path.join(out_dir, 'slc.mli'), length_c, width_c) pngfile = os.path.join(out_dir, 'slc.mli.png') plot_lib.make_im_png(mli, pngfile, 'gray', 'MLI', cbar=False) else: print('Copy {}'.format(os.path.basename(file)), flush=True) shutil.copy(file, out_dir) #%% Clip unw and cc print('\nClip unw and cc', flush=True) ### First, check if already exist ifgdates2 = [] for ifgix, ifgd in enumerate(ifgdates): out_dir1 = os.path.join(out_dir, ifgd) unwfile_c = os.path.join(out_dir1, ifgd + '.unw') ccfile_c = os.path.join(out_dir1, ifgd + '.cc') if not (os.path.exists(unwfile_c) and os.path.exists(ccfile_c)): ifgdates2.append(ifgd) n_ifg2 = len(ifgdates2) if n_ifg - n_ifg2 > 0: print(" {0:3}/{1:3} clipped unw and cc already exist. Skip".format( n_ifg - n_ifg2, n_ifg), flush=True) ### Clip for ifgix, ifgd in enumerate(ifgdates2): if np.mod(ifgix, 100) == 0: print(" {0:3}/{1:3}th unw...".format(ifgix, n_ifg2), flush=True) unwfile = os.path.join(in_dir, ifgd, ifgd + '.unw') ccfile = os.path.join(in_dir, ifgd, ifgd + '.cc') unw = io_lib.read_img(unwfile, length, width) coh = io_lib.read_img(ccfile, length, width) ### Clip unw = unw[y1:y2, x1:x2] coh = coh[y1:y2, x1:x2] ### Output out_dir1 = os.path.join(out_dir, ifgd) if not os.path.exists(out_dir1): os.mkdir(out_dir1) unw.tofile(os.path.join(out_dir1, ifgd + '.unw')) coh.tofile(os.path.join(out_dir1, ifgd + '.cc')) ## Output png for corrected unw pngfile = os.path.join(out_dir1, ifgd + '.unw.png') title = '{} ({}pi/cycle)'.format(ifgd, cycle * 2) plot_lib.make_im_png(np.angle(np.exp(1j * unw / cycle) * cycle), pngfile, 'insar', title, -np.pi, np.pi, cbar=False) #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output directory: {}\n'.format(os.path.relpath(out_dir)))
def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() ver = "1.4.5" date = 20201124 author = "Y. Morishita" print("\n{} ver{} {} {}".format(os.path.basename(argv[0]), ver, date, author), flush=True) print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) ## for parallel processing global cum, mask, deg_ramp, hgt_linearflag, hgt, hgt_min, hgt_max,\ filtcumdir, filtincdir, imdates, cycle, coef_r2m, models, \ filtwidth_yr, filtwidth_km, dt_cum, x_stddev, y_stddev, mask2, cmap_wrap ## global cum_org from hdf5 contaminate in paralell warpper? So pass them by arg. #%% Set default tsadir = [] filtwidth_km = 2 filtwidth_yr = [] deg_ramp = [] hgt_linearflag = False hgt_min = 200 ## meter hgt_max = 10000 ## meter maskflag = True try: n_para = len(os.sched_getaffinity(0)) except: n_para = multi.cpu_count() range_str = [] range_geo_str = [] ex_range_str = [] ex_range_geo_str = [] cumname = 'cum.h5' cmap_vel = SCM.roma.reversed() cmap_noise_r = 'viridis_r' cmap_wrap = SCM.romaO # q = multi.get_context('fork') q = multi.get_context('spawn') compress = 'gzip' #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "ht:s:y:r:", [ "help", "hgt_linear", "hgt_min=", "hgt_max=", "nomask", "n_para=", "range=", "range_geo=", "ex_range=", "ex_range_geo=" ]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-t': tsadir = a elif o == '-s': filtwidth_km = float(a) elif o == '-y': filtwidth_yr = float(a) elif o == '-r': deg_ramp = a elif o == '--hgt_linear': hgt_linearflag = True elif o == '--hgt_min': hgt_min = int(a) elif o == '--hgt_max': hgt_max = int(a) elif o == '--nomask': maskflag = False elif o == '--n_para': n_para = int(a) elif o == '--range': range_str = a elif o == '--range_geo': range_geo_str = a elif o == '--ex_range': ex_range_str = a elif o == '--ex_range_geo': ex_range_geo_str = a if not tsadir: raise Usage('No tsa directory given, -t is not optional!') elif not os.path.isdir(tsadir): raise Usage('No {} dir exists!'.format(tsadir)) elif not os.path.exists(os.path.join(tsadir, cumname)): raise Usage('No {} exists in {}!'.format(cumname, tsadir)) if range_str and range_geo_str: raise Usage( 'Both --range and --range_geo given, use either one not both!') if ex_range_str and ex_range_geo_str: raise Usage( 'Both --ex_range and --ex_range_geo given, use either one not both!' ) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Directory and file setting tsadir = os.path.abspath(tsadir) cumfile = os.path.join(tsadir, cumname) resultsdir = os.path.join(tsadir, 'results') infodir = os.path.join(tsadir, 'info') inparmfile = os.path.join(infodir, '13parameters.txt') if not os.path.exists(inparmfile): ## for old LiCSBAS13 <v1.2 inparmfile = os.path.join(infodir, 'parameters.txt') outparmfile = os.path.join(infodir, '16parameters.txt') pixsp_r = float(io_lib.get_param_par(inparmfile, 'pixel_spacing_r')) pixsp_a = float(io_lib.get_param_par(inparmfile, 'pixel_spacing_a')) x_stddev = filtwidth_km * 1000 / pixsp_r y_stddev = filtwidth_km * 1000 / pixsp_a wavelength = float(io_lib.get_param_par(inparmfile, 'wavelength')) #meter coef_r2m = -wavelength / 4 / np.pi * 1000 #rad -> mm, positive is -LOS if wavelength > 0.2: ## L-band cycle = 1.5 # 2pi/cycle for comparison png elif wavelength <= 0.2: ## C-band cycle = 3 # 3*2pi/cycle for comparison png filtincdir = os.path.join(tsadir, '16filt_increment') if os.path.exists(filtincdir): shutil.rmtree(filtincdir) os.mkdir(filtincdir) filtcumdir = os.path.join(tsadir, '16filt_cum') if os.path.exists(filtcumdir): shutil.rmtree(filtcumdir) os.mkdir(filtcumdir) cumffile = os.path.join(tsadir, 'cum_filt.h5') vconstfile = os.path.join(resultsdir, 'vintercept.filt') velfile = os.path.join(resultsdir, 'vel.filt') cumh5 = h5.File(cumfile, 'r') if os.path.exists(cumffile): os.remove(cumffile) cumfh5 = h5.File(cumffile, 'w') #%% Dates imdates = cumh5['imdates'][()].astype(str).tolist() cum_org = cumh5['cum'] n_im, length, width = cum_org.shape if n_para > n_im: n_para = n_im ### Calc dt in year imdates_dt = ([ dt.datetime.strptime(imd, '%Y%m%d').toordinal() for imd in imdates ]) dt_cum = np.float32((np.array(imdates_dt) - imdates_dt[0]) / 365.25) ### Save dates and other info into cumf cumfh5.create_dataset('imdates', data=cumh5['imdates']) cumfh5.create_dataset('gap', data=cumh5['gap'], compression=compress) if 'bperp' in list(cumh5.keys()): ## if dummy, no bperp field cumfh5.create_dataset('bperp', data=cumh5['bperp']) else: print('No bperp field found in {}. Skip.'.format(cumname)) if 'corner_lat' in list(cumh5.keys()): lat1 = float(cumh5['corner_lat'][()]) lon1 = float(cumh5['corner_lon'][()]) dlat = float(cumh5['post_lat'][()]) dlon = float(cumh5['post_lon'][()]) cumfh5.create_dataset('corner_lat', data=cumh5['corner_lat']) cumfh5.create_dataset('corner_lon', data=cumh5['corner_lon']) cumfh5.create_dataset('post_lat', data=cumh5['post_lat']) cumfh5.create_dataset('post_lon', data=cumh5['post_lon']) else: ## not geocoded print('No latlon field found in {}. Skip.'.format(cumname)) ### temporal filter width if not filtwidth_yr and filtwidth_yr != 0: filtwidth_yr = dt_cum[-1] / (n_im - 1) * 3 ## avg interval*3 ### hgt_linear if hgt_linearflag: hgtfile = os.path.join(resultsdir, 'hgt') if not os.path.exists(hgtfile): print('\nERROR: No hgt file exist in results dir!', file=sys.stderr) print('--hgt_linear option cannot be used.', file=sys.stderr) return 2 hgt = io_lib.read_img(hgtfile, length, width) hgt[np.isnan(hgt)] = 0 else: hgt = [] #%% --range[_geo] and --ex_range[_geo] if range_str: ## --range if not tools_lib.read_range(range_str, width, length): print('\nERROR in {}\n'.format(range_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range(range_str, width, length) range_str = '{}:{}/{}:{}'.format(x1, x2, y1, y2) elif range_geo_str: ## --range_geo if not tools_lib.read_range_geo(range_geo_str, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(range_geo_str), file=sys.stderr) return 1 else: x1, x2, y1, y2 = tools_lib.read_range_geo(range_geo_str, width, length, lat1, dlat, lon1, dlon) range_str = '{}:{}/{}:{}'.format(x1, x2, y1, y2) if ex_range_str: ## --ex_range if not tools_lib.read_range(ex_range_str, width, length): print('\nERROR in {}\n'.format(ex_range_str), file=sys.stderr) return 1 else: ex_x1, ex_x2, ex_y1, ex_y2 = tools_lib.read_range( ex_range_str, width, length) ex_range_str = '{}:{}/{}:{}'.format(ex_x1, ex_x2, ex_y1, ex_y2) elif ex_range_geo_str: ## --ex_range_geo if not tools_lib.read_range_geo(ex_range_geo_str, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(ex_range_geo_str), file=sys.stderr) return 1 else: ex_x1, ex_x2, ex_y1, ex_y2 = tools_lib.read_range_geo( ex_range_geo_str, width, length, lat1, dlat, lon1, dlon) ex_range_str = '{}:{}/{}:{}'.format(ex_x1, ex_x2, ex_y1, ex_y2) ### Make range mask mask2 = np.ones((length, width), dtype=np.float32) if range_str: mask2 = mask2 * np.nan mask2[y1:y2, x1:x2] = 1 if ex_range_str: mask2[ex_y1:ex_y2, ex_x1:ex_x2] = np.nan #%% Display settings print('') print('Size of image (w,l) : {0}, {1}'.format(width, length)) print('Number of images : {}'.format(n_im)) print('Width of filter in space : {} km ({:.1f}x{:.1f} pixel)'.format( filtwidth_km, x_stddev, y_stddev)) print('Width of filter in time : {:.3f} yr ({} days)'.format( filtwidth_yr, int(filtwidth_yr * 365.25))) print('Deramp flag : {}'.format(deg_ramp), flush=True) print('hgt-linear flag : {}'.format(hgt_linearflag), flush=True) if hgt_linearflag: print('Minimum hgt : {} m'.format(hgt_min), flush=True) print('Maximum hgt : {} m'.format(hgt_max), flush=True) if range_str: print('Range : {}'.format(range_str), flush=True) if ex_range_str: print('Excluded range : {}'.format(ex_range_str), flush=True) with open(outparmfile, "w") as f: print('filtwidth_km: {}'.format(filtwidth_km), file=f) print('filtwidth_xpixels: {:.1f}'.format(x_stddev), file=f) print('filtwidth_ypixels: {:.1f}'.format(y_stddev), file=f) print('filtwidth_yr: {:.3f}'.format(filtwidth_yr), file=f) print('filtwidth_day: {}'.format(int(filtwidth_yr * 365.25)), file=f) print('deg_ramp: {}'.format(deg_ramp), file=f) print('hgt_linear: {}'.format(hgt_linearflag * 1), file=f) print('hgt_min: {}'.format(hgt_min), file=f) print('hgt_max: {}'.format(hgt_max), file=f) print('range: {}'.format(range_str), file=f) print('ex_range: {}'.format(ex_range_str), file=f) #%% Load Mask (1: unmask, 0: mask, nan: no cum data) if maskflag: maskfile = os.path.join(resultsdir, 'mask') mask = io_lib.read_img(maskfile, length, width) mask[mask == 0] = np.nan ## 0->nan else: mask = np.ones((length, width), dtype=np.float32) mask[np.isnan(cum_org[0, :, :])] = np.nan #%% First, deramp and hgt-linear if indicated cum = np.zeros((cum_org.shape), dtype=np.float32) * np.nan if not deg_ramp and not hgt_linearflag: cum = cum_org[()] else: if not deg_ramp: print('\nEstimate hgt-linear component,', flush=True) elif not hgt_linearflag: print('\nDeramp ifgs with the degree of {},'.format(deg_ramp), flush=True) else: print('\nDeramp ifgs with the degree of {} and hgt-linear,'.format( deg_ramp), flush=True) print('with {} parallel processing...'.format(n_para), flush=True) args = [(i, cum_org[i, :, :]) for i in range(n_im)] ### Parallel processing p = q.Pool(n_para) _result = np.array(p.map(deramp_wrapper, args), dtype=object) p.close() del args models = _result[:, 1] for i in range(n_im): cum[i, :, :] = _result[i, 0] del _result ### Only for output increment png files print( '\nCreate png for increment with {} parallel processing...'.format( n_para), flush=True) args = [(i, cum_org[i, :, :], cum_org[i - 1, :, :]) for i in range(1, n_im)] p = q.Pool(n_para) p.map(deramp_wrapper2, args) p.close() del args #%% Filter each image cum_filt = cumfh5.require_dataset('cum', (n_im, length, width), dtype=np.float32, compression=compress) print('\nHP filter in time, LP filter in space,', flush=True) print('with {} parallel processing...'.format(n_para), flush=True) ### Parallel processing p = q.Pool(n_para) # cum_filt[:, :, :] = np.array(p.map(filter_wrapper, range(n_im)), dtype=np.float32) cum_filt[:, :, :] = np.array( p.map(filter_wrapper, [(i, cum, filtwidth_yr, dt_cum, x_stddev, y_stddev, mask, coef_r2m, cycle, filtcumdir, imdates, cmap_wrap, filtwidth_km) for i in range(n_im)]), dtype=np.float32) p.close() ### Only for output increment png files print('\nCreate png for increment with {} parallel processing...'.format( n_para), flush=True) args = [(i, cum_filt[i, :, :] - cum_filt[i - 1, :, :]) for i in range(1, n_im)] p = q.Pool(n_para) p.map(filter_wrapper2, [(i, cum, coef_r2m, cycle, mask, filtincdir, imdates, cmap_wrap) for i in args]) p.close() del args #%% Find stable ref point print('\nFind stable reference point...', flush=True) ### Compute RMS of time series with reference to all points sumsq_cum_wrt_med = np.zeros((length, width), dtype=np.float32) for i in range(n_im): sumsq_cum_wrt_med = sumsq_cum_wrt_med + ( cum_filt[i, :, :] - np.nanmedian(cum_filt[i, :, :]))**2 rms_cum_wrt_med = np.sqrt(sumsq_cum_wrt_med / n_im) * mask ### Mask by minimum n_gap n_gap = io_lib.read_img(os.path.join(resultsdir, 'n_gap'), length, width) min_n_gap = np.nanmin(n_gap) mask_n_gap = np.float32(n_gap == min_n_gap) mask_n_gap[mask_n_gap == 0] = np.nan rms_cum_wrt_med = rms_cum_wrt_med * mask_n_gap ### Find stable reference min_rms = np.nanmin(rms_cum_wrt_med) refy1s, refx1s = np.where(rms_cum_wrt_med == min_rms) refy1s, refx1s = refy1s[0], refx1s[0] ## Only first index refy2s, refx2s = refy1s + 1, refx1s + 1 print('Selected ref: {}:{}/{}:{}'.format(refx1s, refx2s, refy1s, refy2s), flush=True) ### Rerferencing cumulative displacement to new stable ref for i in range(n_im): cum_filt[i, :, :] = cum_filt[i, :, :] - cum[i, refy1s, refx1s] ### Save image rms_cum_wrt_med_file = os.path.join(infodir, '16rms_cum_wrt_med') with open(rms_cum_wrt_med_file, 'w') as f: rms_cum_wrt_med.tofile(f) pngfile = os.path.join(infodir, '16rms_cum_wrt_med.png') plot_lib.make_im_png(rms_cum_wrt_med, pngfile, cmap_noise_r, 'RMS of cum wrt median (mm)', np.nanpercentile(rms_cum_wrt_med, 1), np.nanpercentile(rms_cum_wrt_med, 99)) ### Save ref cumfh5.create_dataset('refarea', data='{}:{}/{}:{}'.format(refx1s, refx2s, refy1s, refy2s)) refsfile = os.path.join(infodir, '16ref.txt') with open(refsfile, 'w') as f: print('{}:{}/{}:{}'.format(refx1s, refx2s, refy1s, refy2s), file=f) if 'corner_lat' in list(cumh5.keys()): ## Geocoded ### Make ref_stable.kml reflat = lat1 + dlat * refy1s reflon = lon1 + dlon * refx1s io_lib.make_point_kml(reflat, reflon, os.path.join(infodir, '16ref.kml')) #%% Calc filtered velocity print('\nCalculate velocity of filtered time series...', flush=True) G = np.stack((np.ones_like(dt_cum), dt_cum), axis=1) vconst = np.zeros((length, width), dtype=np.float32) * np.nan vel = np.zeros((length, width), dtype=np.float32) * np.nan bool_unnan = ~np.isnan(cum_filt[0, :, :]).reshape(length, width) ## not all nan cum_pt = cum_filt[()].reshape(n_im, length * width)[:, bool_unnan.ravel()] #n_im x n_pt n_pt_unnan = bool_unnan.sum() vconst_tmp = np.zeros((n_pt_unnan), dtype=np.float32) * np.nan vel_tmp = np.zeros((n_pt_unnan), dtype=np.float32) * np.nan bool_nonan_pt = np.all(~np.isnan(cum_pt), axis=0) ### First, calc vel point without nan print(' First, solving {0:6}/{1:6}th points with full cum...'.format( bool_nonan_pt.sum(), n_pt_unnan), flush=True) vconst_tmp[bool_nonan_pt], vel_tmp[bool_nonan_pt] = np.linalg.lstsq( G, cum_pt[:, bool_nonan_pt], rcond=None)[0] ### Next, calc vel point with nan print(' Next, solving {0:6}/{1:6}th points with nan in cum...'.format( (~bool_nonan_pt).sum(), n_pt_unnan), flush=True) mask_cum = ~np.isnan(cum_pt[:, ~bool_nonan_pt]) vconst_tmp[~bool_nonan_pt], vel_tmp[ ~bool_nonan_pt] = inv_lib.censored_lstsq_slow( G, cum_pt[:, ~bool_nonan_pt], mask_cum) vconst[bool_unnan], vel[bool_unnan] = vconst_tmp, vel_tmp vconst.tofile(vconstfile) vel.tofile(velfile) if maskflag: vel_mskd = vel * mask vconst_mskd = vconst * mask vconst_mskd.tofile(vconstfile + '.mskd') vel_mskd.tofile(velfile + '.mskd') cumfh5.create_dataset('vel', data=vel.reshape(length, width), compression=compress) cumfh5.create_dataset('vintercept', data=vconst.reshape(length, width), compression=compress) #%% Add info and close cumfh5.create_dataset('filtwidth_yr', data=filtwidth_yr) cumfh5.create_dataset('filtwidth_km', data=filtwidth_km) cumfh5.create_dataset('deramp_flag', data=deg_ramp) cumfh5.create_dataset('hgt_linear_flag', data=hgt_linearflag * 1) cumh5.close() cumfh5.close() #%% Output image pngfile = os.path.join(resultsdir, 'vel.filt.png') title = 'Filtered velocity (mm/yr)' vmin = np.nanpercentile(vel, 1) vmax = np.nanpercentile(vel, 99) plot_lib.make_im_png(vel, pngfile, cmap_vel, title, vmin, vmax) ## vintercept pngfile = os.path.join(resultsdir, 'vintercept.filt.png') title = 'Intercept of filtered velocity (mm)' vmin = np.nanpercentile(vconst, 1) vmax = np.nanpercentile(vconst, 99) plot_lib.make_im_png(vconst, pngfile, cmap_vel, title, vmin, vmax) if maskflag: pngfile = os.path.join(resultsdir, 'vel.filt.mskd.png') title = 'Masked filtered velocity (mm/yr)' vmin = np.nanpercentile(vel_mskd, 1) vmax = np.nanpercentile(vel_mskd, 99) plot_lib.make_im_png(vel_mskd, pngfile, cmap_vel, title, vmin, vmax) ## vintercept pngfile = os.path.join(resultsdir, 'vintercept.filt.mskd.png') title = 'Masked intercept of filtered velocity (mm)' vmin = np.nanpercentile(vconst_mskd, 1) vmax = np.nanpercentile(vconst_mskd, 99) plot_lib.make_im_png(vconst_mskd, pngfile, cmap_vel, title, vmin, vmax) #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output: {}\n'.format(os.path.relpath(cumffile)), flush=True) print('To plot the time-series:') print('LiCSBAS_plot_ts.py -i "{}" &\n'.format(os.path.relpath(cumffile)))
def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() ver = "1.3.1" date = 20210107 author = "Y. Morishita" print("\n{} ver{} {} {}".format(os.path.basename(argv[0]), ver, date, author), flush=True) print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) #%% Set default imd_s = [] imd_e = [] cumfile = 'cum_filt.h5' outfile = [] refarea = [] refarea_geo = [] maskfile = [] vstdflag = False sinflag = False pngflag = False cmap = SCM.roma.reversed() #%% Read options try: try: opts, args = getopt.getopt( argv[1:], "hs:e:i:o:r:", ["help", "vstd", "sin", "png", "ref_geo=", "mask="]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-s': imd_s = a elif o == '-e': imd_e = a elif o == '-i': cumfile = a elif o == '-o': outfile = a elif o == '-r': refarea = a elif o == '--ref_geo': refarea_geo = a elif o == '--vstd': vstdflag = True elif o == '--sin': sinflag = True elif o == '--mask': maskfile = a elif o == '--png': pngflag = True if not os.path.exists(cumfile): raise Usage('No {} exists! Use -i option.'.format(cumfile)) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Read info ### Read cumfile cumh5 = h5.File(cumfile, 'r') imdates = cumh5['imdates'][()].astype(str).tolist() cum = cumh5['cum'] n_im_all, length, width = cum.shape if refarea: if not tools_lib.read_range(refarea, width, length): print('\nERROR in {}\n'.format(refarea), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range( refarea, width, length) elif refarea_geo: lat1 = float(cumh5['corner_lat'][()]) lon1 = float(cumh5['corner_lon'][()]) dlat = float(cumh5['post_lat'][()]) dlon = float(cumh5['post_lon'][()]) if not tools_lib.read_range_geo(refarea_geo, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(refarea_geo), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range_geo( refarea_geo, width, length, lat1, dlat, lon1, dlon) else: refarea = cumh5['refarea'][()] refx1, refx2, refy1, refy2 = [ int(s) for s in re.split('[:/]', refarea) ] #%% Setting ### Dates if not imd_s: imd_s = imdates[0] if not imd_e: imd_e = imdates[-1] ### mask if maskfile: mask = io_lib.read_img(maskfile, length, width) mask[mask == 0] = np.nan suffix_mask = '.mskd' else: mask = np.ones((length, width), dtype=np.float32) suffix_mask = '' ### Find date index if not exist in imdates if not imd_s in imdates: for imd in imdates: if int(imd) >= int(imd_s): ## First larger one than imd_s imd_s = imd break if not imd_e in imdates: for imd in imdates[::-1]: if int(imd) <= int(imd_e): ## Last smaller one than imd_e imd_e = imd break ix_s = imdates.index(imd_s) ix_e = imdates.index(imd_e) + 1 #+1 for python custom n_im = ix_e - ix_s ### Calc dt in year imdates_dt = ([ dt.datetime.strptime(imd, '%Y%m%d').toordinal() for imd in imdates[ix_s:ix_e] ]) dt_cum = np.float32((np.array(imdates_dt) - imdates_dt[0]) / 365.25) ### Outfile if not outfile: outfile = '{}_{}.vel{}'.format(imd_s, imd_e, suffix_mask) #%% Display info print('') print('Start date : {}'.format(imdates[ix_s])) print('End date : {}'.format(imdates[ix_e - 1])) print('# of images : {}'.format(n_im)) print('Ref area : {}:{}/{}:{}'.format(refx1, refx2, refy1, refy2)) print('') #%% Calc velocity and vstd vconst = np.zeros((length, width), dtype=np.float32) * np.nan vel = np.zeros((length, width), dtype=np.float32) * np.nan ### Read cum data cum_tmp = cum[ix_s:ix_e, :, :] * mask cum_ref = np.nanmean(cum[ix_s:ix_e, refy1:refy2, refx1:refx2] * mask[refy1:refy2, refx1:refx2], axis=(1, 2)) if np.all(np.isnan(cum_ref)): print('\nERROR: Ref area has only NaN value!\n', file=sys.stderr) return 2 cum_tmp = cum_tmp - cum_ref[:, np.newaxis, np.newaxis] ### Extract not nan points bool_allnan = np.all(np.isnan(cum_tmp), axis=0) cum_tmp = cum_tmp.reshape(n_im, length * width)[:, ~bool_allnan.ravel()].transpose() if not sinflag: ## Linear function print('Calc velocity...') vel[~bool_allnan], vconst[~bool_allnan] = inv_lib.calc_vel( cum_tmp, dt_cum) vel.tofile(outfile) else: ## Linear+sin function print('Calc velocity and annual components...') amp = np.zeros((length, width), dtype=np.float32) * np.nan delta_t = np.zeros((length, width), dtype=np.float32) * np.nan ampfile = outfile.replace('vel', 'amp') dtfile = outfile.replace('vel', 'dt') vel[~bool_allnan], vconst[~bool_allnan], amp[~bool_allnan], delta_t[ ~bool_allnan] = inv_lib.calc_velsin(cum_tmp, dt_cum, imdates[0]) vel.tofile(outfile) amp.tofile(ampfile) delta_t.tofile(dtfile) ### vstd if vstdflag: vstdfile = outfile.replace('vel', 'vstd') vstd = np.zeros((length, width), dtype=np.float32) * np.nan print('Calc vstd...') vstd[~bool_allnan] = inv_lib.calc_velstd_withnan(cum_tmp, dt_cum) vstd.tofile(vstdfile) #%% Make png if specified if pngflag: pngfile = outfile + '.png' title = 'n_im: {}, Ref X/Y {}:{}/{}:{}'.format(n_im, refx1, refx2, refy1, refy2) plot_lib.make_im_png(vel, pngfile, cmap, title) if sinflag: amp_max = np.nanpercentile(amp, 99) plot_lib.make_im_png(amp, ampfile + '.png', 'viridis', title, vmax=amp_max) plot_lib.make_im_png(delta_t, dtfile + '.png', 'hsv', title) if vstdflag: plot_lib.make_im_png(vstd, vstdfile + '.png', cmap, title) #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output: {}'.format(outfile), flush=True) if vstdflag: print(' {}'.format(vstdfile), flush=True) print('')
def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() ver = 1.2 date = 20200703 author = "Y. Morishita" print("\n{} ver{} {} {}".format(os.path.basename(argv[0]), ver, date, author), flush=True) print("{} {}".format(os.path.basename(argv[0]), ' '.join(argv[1:])), flush=True) #%% Set default imd_s = [] cumfile = 'cum_filt.h5' outfile = [] imd_m = [] refarea = [] refarea_geo = [] maskfile = [] pngflag = False #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "hd:i:o:m:r:", ["help", "png", "ref_geo=", "mask="]) except getopt.error as msg: raise Usage(msg) for o, a in opts: if o == '-h' or o == '--help': print(__doc__) return 0 elif o == '-d': imd_s = a elif o == '-i': cumfile = a elif o == '-o': outfile = a elif o == '-m': imd_m = a elif o == '-r': refarea = a elif o == '--ref_geo': refarea_geo = a elif o == '--mask': maskfile = a elif o == '--png': pngflag = True if not imd_s: raise Usage('No date given, -d is not optional!') elif not os.path.exists(cumfile): raise Usage('No {} exists! Use -i option.'.format(cumfile)) except Usage as err: print("\nERROR:", file=sys.stderr, end='') print(" " + str(err.msg), file=sys.stderr) print("\nFor help, use -h or --help.\n", file=sys.stderr) return 2 #%% Read info ### Read cumfile cumh5 = h5.File(cumfile, 'r') imdates = cumh5['imdates'][()].astype(str).tolist() cum = cumh5['cum'] n_im, length, width = cum.shape ### Reference area if refarea: if not tools_lib.read_range(refarea, width, length): print('\nERROR in {}\n'.format(refarea), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range( refarea, width, length) elif refarea_geo: lat1 = float(cumh5['corner_lat'][()]) lon1 = float(cumh5['corner_lon'][()]) dlat = float(cumh5['post_lat'][()]) dlon = float(cumh5['post_lon'][()]) if not tools_lib.read_range_geo(refarea_geo, width, length, lat1, dlat, lon1, dlon): print('\nERROR in {}\n'.format(refarea_geo), file=sys.stderr) return 2 else: refx1, refx2, refy1, refy2 = tools_lib.read_range_geo( refarea_geo, width, length, lat1, dlat, lon1, dlon) else: refarea = cumh5['refarea'][()] refx1, refx2, refy1, refy2 = [ int(s) for s in re.split('[:/]', refarea) ] ### Master (reference) date if not imd_m: imd_m = imdates[0] ### mask if maskfile: mask = io_lib.read_img(maskfile, length, width) mask[mask == 0] = np.nan else: mask = np.ones((length, width), dtype=np.float32) ### Check date if not imd_s in imdates: print('\nERROR: No date of {} exist in {}!'.format(imd_s, cumfile), file=sys.stderr) return 2 if not imd_m in imdates: print('\nERROR: No date of {} exist in {}!'.format(imd_m, cumfile), file=sys.stderr) return 2 ix_s = imdates.index(imd_s) ix_m = imdates.index(imd_m) ### Outfile if not outfile: outfile = '{}_{}.cum'.format(imd_m, imd_s) #%% Make flt cum_s = cum[ix_s, :, :] cum_m = cum[ix_m, :, :] cum_dif = cum_s - cum_m cum_dif = cum_dif - np.nanmean(cum_dif[refy1:refy2, refx1:refx2]) cum_dif = cum_dif * mask cum_dif.tofile(outfile) #%% Make png if specified if pngflag: pngfile = outfile + '.png' title = '{} (Ref X/Y {}:{}/{}:{})'.format(outfile, refx1, refx2, refy1, refy2) plot_lib.make_im_png(cum_dif, pngfile, 'jet', title) #%% Finish elapsed_time = time.time() - start hour = int(elapsed_time / 3600) minite = int(np.mod((elapsed_time / 60), 60)) sec = int(np.mod(elapsed_time, 60)) print("\nElapsed time: {0:02}h {1:02}m {2:02}s".format(hour, minite, sec)) print('\n{} Successfully finished!!\n'.format(os.path.basename(argv[0]))) print('Output: {}\n'.format(outfile), flush=True)