Skip to content

Commit

Permalink
Fix bug when pixel spacing is changed from 3 arc second.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-gaddes committed May 25, 2021
1 parent f384c8a commit 627037b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/__pycache/*
__pycache__/*
2 changes: 1 addition & 1 deletion lib/auxiliary_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def griddata_plot(griddata, lons_mg, lats_mg, title, dem_mode = True):
ax.set_xlabel('Longitude (degs)')
lats = lats_mg[:,0] # get a rank 1 of the latitudes of each pixel
ytick_pixel_n = np.linspace(0, griddata.shape[0]-1, 10).astype(int) # only plot every 10th?
plt.yticks(ytick_pixel_n, np.round(lats[ytick_pixel_n],2)[::-1]) # update yticks to latitudes. Not entirely sure why ticks have to be reversed - possibly matplotlib is counting from top left and not bottom left
plt.yticks(ytick_pixel_n, np.round(lats[ytick_pixel_n],2)[::1]) # update yticks to latitudes. have removed - from before 1 to not reverse
ax.set_ylabel('Latitude (degs)')

fig1.tight_layout()
Expand Down
10 changes: 7 additions & 3 deletions lib/random_generation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def create_random_synthetic_ifgs(volcanoes, defo_sources, n_ifgs, n_pix = 224, o
dem_large = volcanoes[volcano_n]['dem'] # open a dem
dem_ll_extent = [(volcanoes[volcano_n]['lons_mg'][0,0], volcanoes[volcano_n]['lats_mg'][0,0] ), # get lon lat of lower left corner
(volcanoes[volcano_n]['lons_mg'][-1,-1], volcanoes[volcano_n]['lats_mg'][-1,-1])] # and upper right corner
#import pdb; pdb.set_trace()
mask_coherence = coherence_mask(volcanoes[volcano_n]['lons_mg'][:n_pix,:n_pix], volcanoes[volcano_n]['lats_mg'][:n_pix,:n_pix]) # generate coherence mask, but at the number of pixels required for the ouput, and not hte size of the large dem
# if threshold is 0, all of the pixels are incoherent , and if 1, none are.

Expand Down Expand Up @@ -416,7 +417,7 @@ def random_crop_of_r2(r2_array, n_pixs=224, extend = False):
centre_y = int(r2_array.shape[0]/2)

x_start = np.random.randint(centre_x - (0.9*n_pixs), centre_x - (0.1*n_pixs)) # x_start always includes hte centre, as it can only go 90% of the scene width below the centre, and at max 10% of the scene width below the cente
y_start = np.random.randint(centre_y - (0.9*n_pixs), centre_y - (0.1*n_pixs))
y_start = np.random.randint(centre_y - (0.9*n_pixs), centre_y - (0.1*n_pixs)) # note that this could be done more efficiently, but it is done this way to ensure that the centre is always in the crop (as this is where a deformation signal will be in SyInteferoPy)

r2_array_subregion = r2_array[y_start:(y_start+n_pixs), x_start:(x_start+n_pixs)] # do the crop
x_pos = np.ceil((r2_array.shape[1]/2) - x_start) # centre of def - offset
Expand Down Expand Up @@ -478,6 +479,7 @@ def create_random_defo_m(dem, lons_mg, lats_mg, deformation_ll, defo_source,
2020/09/24 | MEG | Comment and write docs.
2020/10/09 | MEG | Update bug (deformation_wrapper was returning masked arrays when it should have returned arrays)
2020/10/26 | MEG | Return source_kwargs for potential use as labels when perofming (semi)supervised learning
2021_05_06 | MEG | Add a catch incase defo_source doesn't match dyke/sill/mogi (including wrong case).
"""
import numpy as np
import numpy.ma as ma
Expand Down Expand Up @@ -505,8 +507,10 @@ def create_random_defo_m(dem, lons_mg, lats_mg, deformation_ll, defo_source,
'dip' : np.random.randint(0,5), # in degrees
'opening' : 0.2 + 0.8 * np.random.rand()} # in metres
elif defo_source == 'mogi':
source_kwargs = {'volume_change' : 2e6 + 1e6 * np.random.rand(), # in metres
'depth' : 1000 + 3000 * np.random.rand()} # in metres
source_kwargs = {'volume_change' : 2e6 + 1e6 * np.random.rand(), # in metres
'depth' : 1000 + 3000 * np.random.rand()} # in metres
else:
raise Exception(f"defo_source should be either 'mogi', 'sill', or 'dyke', but is {defo_source}. Exiting.")

# 1: Apply the scaling factor to the source_kwargs # (ie if we're never getting a signal of acceptable size, adjust some kwargs to increase/decrease the magnitude. )
adjustable_source_kwargs = ['opening', 'volume_change']
Expand Down
11 changes: 8 additions & 3 deletions lib/syinterferopy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def deformation_wrapper(lons_mg, lats_mg, deformation_ll, source, dem = None,
History:
2020/08/07 | MEG | Written.
2020/10/09 | MEG | Update so that the DEM is optional.
2021_05_18 | MEG | Fix bug in number of pixels in a degree (hard coded as 1201, now calcualted from lon and lat grids)
"""

import numpy as np
Expand All @@ -75,6 +76,9 @@ def deformation_wrapper(lons_mg, lats_mg, deformation_ll, source, dem = None,


# 1: deal with lons and lats, and get pixels in metres from origin at lower left.
pixs2deg_x = 1 / (lons_mg[0,1] - lons_mg[0,0]) # the number of pixels in 1 deg of longitude
pixs2deg_y = 1 / (lats_mg[0,0] - lats_mg[1,0]) # the number of pixels in 1 deg of latitude
pixs2deg = np.mean([pixs2deg_x, pixs2deg_y])
dem_ll_extent = [(lons_mg[-1,0], lats_mg[-1,-1]), (lons_mg[1,-1], lats_mg[1,0])] # [lon lat tuple of lower left corner, lon lat tuple of upper right corner]
xyz_m, pixel_spacing = lon_lat_to_ijk(lons_mg, lats_mg) # get pixel positions in metres from origin in lower left corner (and also their size in x and y direction)

Expand Down Expand Up @@ -110,9 +114,9 @@ def deformation_wrapper(lons_mg, lats_mg, deformation_ll, source, dem = None,


# 2: calculate deformation location in the new metres from lower left coordinate system.
deformation_xy = ll2xy(np.asarray(dem_ll_extent[0])[np.newaxis,:], 1201,
np.asarray(deformation_ll)[np.newaxis,:]) #1x2 array of lon lat of bottom left corner and of points of interest (the deformation lon lat), and the number of pixels in a degree
# returns the pixel number of the deformation source, from the lower left corner.
deformation_xy = ll2xy(np.asarray(dem_ll_extent[0])[np.newaxis,:], pixs2deg, # lon lat of lower left coerner, number of pixels in 1 degree
np.asarray(deformation_ll)[np.newaxis,:]) # long lat of point of interext (deformation centre)


deformation_m = np.array([[deformation_xy[0,0] * pixel_spacing['x'], deformation_xy[0,1] * pixel_spacing['y']]]) # convert from number of pixels from lower left corner to number of metres from lower left corner, 1x2 array.

Expand Down Expand Up @@ -140,6 +144,7 @@ def deformation_wrapper(lons_mg, lats_mg, deformation_ll, source, dem = None,




#%%


Expand Down

0 comments on commit 627037b

Please sign in to comment.