Skip to content

Commit

Permalink
Made for monitoring mode:
Browse files Browse the repository at this point in the history
-Update so that LiCSBAS time series are referenced correctly.
-Some small changes to outputs to terminal.
  • Loading branch information
matthew-gaddes committed Nov 15, 2021
1 parent 172c768 commit 0dfb5e4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Binary file modified licsalert/__pycache__/licsalert.cpython-37.pyc
Binary file not shown.
Binary file modified licsalert/__pycache__/monitoring_functions.cpython-37.pyc
Binary file not shown.
9 changes: 7 additions & 2 deletions licsalert/licsalert.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ def LiCSBAS_for_LiCSAlert(LiCSAR_frame, LiCSAR_frames_dir, LiCSBAS_out_dir, logf

#%%

def LiCSAlert_preprocessing(displacement_r2, downsample_run=1.0, downsample_plot=0.5, verbose=True):
def LiCSAlert_preprocessing(displacement_r2, downsample_run=1.0, downsample_plot=0.5, verbose=True, mean_centre = True):
"""A function to downsample the data at two scales (one for general working [ie to speed things up], and one
for faster plotting. ) Also, data are mean centered, which is required for ICASAR and LiCSAlert.
Note that the downsamples are applied consecutively, so are compound (e.g. if both are 0.5,
Expand All @@ -944,6 +944,7 @@ def LiCSAlert_preprocessing(displacement_r2, downsample_run=1.0, downsample_plot
downsample_run | float | in range [0 1], and used to downsample the "incremental" data
downsample_plot | float | in range [0 1] and used to downsample the data again for the "incremental_downsample" data
verbose | boolean | if True, informatin returned to terminal
mean_centre | boolean | add option to control if mean centered.
Outputs:
displacement_r2 | dict | input data stored in a dict as row vectors with a mask
Expand All @@ -956,6 +957,7 @@ def LiCSAlert_preprocessing(displacement_r2, downsample_run=1.0, downsample_plot
2021_04_14 | MEG | Update so handle rank2 arrays of lons and lats properly.
2021_05_05 | MEG | Add check that lats are always the right way up, and fix bug in lons.
2021_10_13 | MEG | Add function to also downsample ENU grids.
2021_11_15 | MEG | Add option to control mean centering, and warning that it is happening.
"""
import numpy as np
from licsalert.downsample_ifgs import downsample_ifgs
Expand All @@ -967,7 +969,10 @@ def LiCSAlert_preprocessing(displacement_r2, downsample_run=1.0, downsample_plot
shape_start = displacement_r2["mask"].shape # and the shape of the ifgs (ny, nx)

# 0: Mean centre the interferograms (ie. break any connection to a reference pixel/region that the interferogram was set to)
displacement_r2["incremental"] = displacement_r2["incremental"] - np.mean(displacement_r2["incremental"], axis = 1)[:,np.newaxis] # mean centre the data (along rows)
if mean_centre:
if verbose:
print(f"LiCSAlert_preprocessing: mean centering the interferograms before downsampling")
displacement_r2["incremental"] = displacement_r2["incremental"] - np.mean(displacement_r2["incremental"], axis = 1)[:,np.newaxis] # mean centre the data (along rows)

# 1: Downsample the ifgs for use in all following functions.
if downsample_run != 1.0: # if we're not actually downsampling, skip for speed
Expand Down
24 changes: 15 additions & 9 deletions licsalert/monitoring_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ def initiate_licsalert_volcano(licsalert_dir, region, volc, template_file_dir):
with open(licsalert_dir / "all_volcs_products" / "licsalert_all_volcs_log.txt", "a") as all_volcs_log:
message = f"A licsbas .json was found for {volc} but no licsalert directory. The directory has now been created, and the template files copied in. "
print(message)
all_volcs_log.write(message)
all_volcs_log.write(message + '\n')



# 1: get all the licsbas volcanoes, and loop through seeing how they compare to the files used by licsalert for that volcano
Expand Down Expand Up @@ -422,6 +423,13 @@ def baseline_from_names(names_list):
licsbas_json_creation_time = licsbas_json_creation_time.replace(microsecond = 0)

print(f"Opening the LiCSBAS .json file with timestamp {licsbas_json_timestamp} and system creation time {licsbas_json_creation_time}. ")

# 0: Get the reference area.
ref_list = licsbas_data['refarea']
ref_xy = {'x_start' : int(ref_list[0]), # convert the correct part of the string to an integer
'x_stop' : int(ref_list[1]),
'y_start' : int(ref_list[2]),
'y_stop' : int(ref_list[3])}

# 1: get the lons and lats of each pixel in the image
lons_mg, lats_mg = np.meshgrid(licsbas_data['x'], licsbas_data['y'])
Expand All @@ -446,6 +454,11 @@ def baseline_from_names(names_list):
n_im, length, width = cumulative_r3.shape # time series size, n_im = n_acquisisions
mask_coh_water_r3 = np.repeat(mask_coh_water[np.newaxis,], n_im, axis = 0) # new version that has expanded to be the same size as the cumulative ifgs

# 3a: Reference the time series
ifg_offsets = np.nanmean(cumulative_r3[:, ref_xy['y_start']: ref_xy['y_stop'], ref_xy['x_start']: ref_xy['x_stop']], axis = (1,2)) # get the offset between the reference pixel/area and 0 for each time
cumulative_r3 = cumulative_r3 - np.repeat(np.repeat(ifg_offsets[:,np.newaxis, np.newaxis], cumulative_r3.shape[1], axis = 1), cumulative_r3.shape[2], axis = 2) # do the correction (first make ifg_offsets teh same size as cumulative).

# 3b: Deal with masking etc.
cumulative_r3_ma = ma.array(cumulative_r3, mask=mask_coh_water_r3) # mask the cumulative ifgs (first one should be all 0s), note that this could still have nans in it
for nan_pixel in np.argwhere(np.isnan(cumulative_r3_ma)): # find any pixels that are nan in this, and iterate over
mask_coh_water_r3[:, nan_pixel[1], nan_pixel[2]] = 1 # and modify the mask so that they are masked for all times
Expand All @@ -464,14 +477,7 @@ def baseline_from_names(names_list):
tbaseline_info["baselines"] = baseline_from_names(tbaseline_info["ifg_dates"]) # and their temporal baselines
tbaseline_info["baselines_cumulative"] = np.cumsum(tbaseline_info["baselines"]) # cumulative baslines, e.g. 12 24 36 48 etc

# 5: Get the reference area.
ref_list = licsbas_data['refarea']
ref_xy = {'x_start' : int(ref_list[0]), # convert the correct part of the string to an integer
'x_stop' : int(ref_list[1]),
'y_start' : int(ref_list[2]),
'y_stop' : int(ref_list[3])}

# 6: Try to get the DEM
# 5: Try to get the DEM
try:
dem = nested_lists_to_numpy(licsbas_data['elev']) #
displacement_r2['dem'] = dem # and added to the displacement dict in the same was as the lons and lats
Expand Down

0 comments on commit 0dfb5e4

Please sign in to comment.