def main(argv=None): #%% Check argv if argv == None: argv = sys.argv start = time.time() #%% Set default imd_s = [] imd_e = [] cumfile = 'cum_filt.h5' outfile = [] refarea = [] maskfile = [] vstdflag = False sinflag = False pngflag = False #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "hs:e:i:o:r:", ["help", "vstd", "sin", "png", "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 == '--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 not refarea: refarea = cumh5['refarea'][()] refx1, refx2, refy1, refy2 = [ int(s) for s in re.split('[:/]', refarea) ] else: 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) #%% 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, 'jet', 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', '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: {}'.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 = 20210309 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 tsadir = [] memory_size = 4000 gpu = False cmap_noise_r = 'viridis_r' #%% Read options try: try: opts, args = getopt.getopt(argv[1:], "ht:", ["help", "mem_size=", "gpu"]) 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 == '--mem_size': memory_size = float(a) elif o == '--gpu': gpu = True if not tsadir: raise Usage('No tsa directory given, -d is not optional!') elif not os.path.isdir(tsadir): raise Usage('No {} dir exists!'.format(tsadir)) if gpu: print("\nGPU option is activated. Need cupy module.\n") import cupy as cp 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 settings tsadir = os.path.abspath(tsadir) resultsdir = os.path.join(tsadir, 'results') #%% Read data information cumh5 = h5.File(os.path.join(tsadir, 'cum.h5'), 'r') imdates = cumh5['imdates'][()].astype(str).tolist() cum = cumh5['cum'] n_im, length, width = cum.shape 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) #%% Get patch row number n_store_data = n_im * 2.25 + 100 #3:cum,data,M(bool); 100:bootnum n_patch, patchrow = tools_lib.get_patchrow(width, length, n_store_data, memory_size) #%% For each patch for i, rows in enumerate(patchrow): print('\nProcess {0}/{1}th line ({2}/{3}th patch)...'.format( rows[1], patchrow[-1][-1], i + 1, n_patch), flush=True) start2 = time.time() lengththis = rows[1] - rows[0] #%% Calc STC print(' Calculating STC...', flush=True) ### Read data with extra 1 line for overlapping row_ex1 = 0 if i == 0 else 1 ## first patch row_ex2 = 0 if i == len(patchrow) - 1 else 1 ## last patch _cum = cum[:, rows[0] - row_ex1:rows[1] + row_ex2, :].reshape( n_im, lengththis + row_ex1 + row_ex2, width) ### Calc STC stc = inv_lib.calc_stc(_cum, gpu=gpu)[row_ex1:lengththis + row_ex1, :] ## original length del _cum ### Output data and image stcfile = os.path.join(resultsdir, 'stc') openmode = 'w' if i == 0 else 'a' #w only 1st patch with open(stcfile, openmode) as f: stc.tofile(f) #%% Calc vstd ### Read data for vstd n_pt_all = lengththis * width cum_patch = cum[:, rows[0]:rows[1], :].reshape( (n_im, n_pt_all)).transpose() #(n_pt_all, n_im) ### Remove invalid points bool_unnan_pt = ~np.isnan(cum_patch[:, 0]) cum_patch = cum_patch[bool_unnan_pt, :] ## remain only unnan data n_pt_unnan = bool_unnan_pt.sum() print(' {}/{} points removed due to no data...'.format( n_pt_all - n_pt_unnan, n_pt_all), flush=True) ### Calc vstd by bootstrap vstd = np.zeros((n_pt_all), dtype=np.float32) * np.nan print(' Calculating std of velocity by bootstrap...', flush=True) vstd[bool_unnan_pt] = inv_lib.calc_velstd_withnan(cum_patch, dt_cum, gpu=gpu) ### Output data and image vstdfile = os.path.join(resultsdir, 'vstd') openmode = 'w' if i == 0 else 'a' #w only 1st patch with open(vstdfile, openmode) as f: vstd.tofile(f) #%% Finish patch elapsed_time2 = int(time.time() - start2) print(' Elapsed time for {0}th patch: {1} sec'.format( i + 1, elapsed_time2)) #%% Close h5 file cumh5.close() #%% Output png print('\nOutput png images...') stc = io_lib.read_img(stcfile, length, width) pngfile = stcfile + '.png' title = 'Spatio-temporal consistency (mm)' cmin = np.nanpercentile(stc, 1) cmax = np.nanpercentile(stc, 99) plot_lib.make_im_png(stc, pngfile, cmap_noise_r, title, cmin, cmax) vstd = io_lib.read_img(vstdfile, length, width) pngfile = vstdfile + '.png' title = 'STD of velocity (mm/yr)' cmin = np.nanpercentile(vstd, 1) cmax = np.nanpercentile(vstd, 99) plot_lib.make_im_png(vstd, pngfile, cmap_noise_r, title, cmin, cmax) #%% 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(tsadir)))