|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +''' |
| 4 | +This script has been copied from LiCSBAS package and modified for PyRate package. |
| 5 | +See: https://github.com/yumorishita/LiCSBAS/blob/master/bin/LiCSBAS_plot_ts.py |
| 6 | +''' |
| 7 | + |
| 8 | +# plotting PyRate velocity and ts files |
| 9 | +import rasterio |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import matplotlib.backend_bases |
| 12 | +import numpy as np |
| 13 | +import os, sys, re |
| 14 | +import xarray as xr |
| 15 | +from datetime import datetime as dt |
| 16 | +from pylab import plot, ginput, show, axis # for velocity profile |
| 17 | + |
| 18 | +if len(sys.argv) != 2: |
| 19 | + print('Exiting: Provide abs path to <outdir> as command line argument') |
| 20 | + exit() |
| 21 | +else: |
| 22 | + path = sys.argv[1] |
| 23 | + print(f"Looking for PyRate products in: {path}") |
| 24 | + |
| 25 | +#path = "/home/547/co9432/EROMANGA_output/result_dflt_refpix_cc07_cc_filt_slp1km_indpt_orbit_quadfit/" |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +# ----- Reading linear velocity file -------------------------------- |
| 30 | +with rasterio.open(os.path.join(path, 'linear_rate.tif')) as src2: |
| 31 | + vel = src2.read() |
| 32 | + bounds2 = src2.bounds |
| 33 | + x_coord2 = np.linspace(bounds2[0], bounds2[2], src2.width) |
| 34 | + y_coord2 = np.linspace(bounds2[1], bounds2[3], src2.height) |
| 35 | + # grab list of time series dates from metadata |
| 36 | + ed = src2.tags()['EPOCH_DATE'] |
| 37 | + |
| 38 | +# convert metadata string to list of strings |
| 39 | +date_str = re.findall(r'\'(.+?)\'', ed) |
| 40 | +imdates_dt = [dt.strptime(x, '%Y-%m-%d') for x in date_str] |
| 41 | + |
| 42 | +# make velocity xarray |
| 43 | +dac2 = xr.DataArray(vel[0,:,:], coords={'lon': x_coord2, 'lat': y_coord2}, dims=['lat', 'lon']) |
| 44 | +vs = xr.Dataset() |
| 45 | +vs['vel'] = dac2 |
| 46 | +longitude = vs.coords['lon'] |
| 47 | +latitude = vs.coords['lat'] |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +# ------ Masking the velocity file using linear sample -------------- |
| 52 | +# # linear resample *tif file and mask out velocity |
| 53 | +src3 = rasterio.open(os.path.join(path, 'linear_samples.tif')) |
| 54 | +lsample = src3.read() |
| 55 | +lsamp_val = lsample[0,:,:] |
| 56 | +[mask_x,mask_y] = np.where((lsamp_val >= (len(imdates_dt)-2))) |
| 57 | +vel_test = np.empty((vel.shape[1],vel.shape[2],)) * np.nan |
| 58 | +vel_test[mask_x,mask_y] = vs.vel.data[mask_x,mask_y] |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +# -------- Plot velocity profile across two points ------------------- |
| 63 | +def vel_profile(line): |
| 64 | + num = 500 |
| 65 | + x, y = np.linspace(line[0][0], line[1][0], num), np.linspace(line[0][1], line[1][1], num) |
| 66 | + ## Extract the values along the line, using nearest-neighbor interpolation |
| 67 | + zi = vel_test[y.astype(int), x.astype(int)] |
| 68 | + return x, y, zi |
| 69 | + |
| 70 | + |
| 71 | +vmin = -50; vmax = 50 |
| 72 | +cmap = matplotlib.cm.bwr_r |
| 73 | +cmap.set_bad('grey',1.) |
| 74 | +fig, axes = plt.subplots(nrows=2) |
| 75 | +cax = axes[0].imshow(vel_test, clim=[vmin, vmax], cmap = cmap) |
| 76 | +cbr = fig.colorbar(cax,ax=axes[0], orientation='vertical') |
| 77 | +cbr.set_label('mm/yr') |
| 78 | + |
| 79 | + |
| 80 | +ig = 1 |
| 81 | +while ig!=2: |
| 82 | + print("Please click two points") |
| 83 | + pts = ginput(2) # it will wait for two clicks |
| 84 | + [x,y,zi] = vel_profile(pts) |
| 85 | + if axes[0].lines: |
| 86 | + del axes[0].lines[0] |
| 87 | + axes[1].cla() |
| 88 | + axes[0].plot([pts[0][0], pts[1][0]], [pts[0][1], pts[1][1]], 'ro-') # axes[0].plot([x0, x1], [y0, y1], 'ro-') |
| 89 | + axes[1].plot(x, zi, 'gray') |
| 90 | + axes[1].plot(x, zi, 'r.') |
| 91 | + # axes[1].set_ylim(-5,5) |
| 92 | + axes[1].set_ylabel('LOS velocity [mm/yr]') |
| 93 | + axes[1].set_xlabel('x-axis') |
| 94 | + axes[1].grid(zorder=0) |
| 95 | + plt.pause(0.1) |
| 96 | + |
| 97 | + ct = input('would you like to continue? (y/n) ') |
| 98 | + if ct == 'n': |
| 99 | + ig = 2 |
| 100 | + print('Plotting done!') |
| 101 | + break |
| 102 | +plt.show() |
0 commit comments