Skip to content

Commit 400b2a2

Browse files
committed
Added the function to calculate spherical spreading corrections for radar images
1 parent 0852e02 commit 400b2a2

9 files changed

+191
-158
lines changed

Diff for: SpreadingCorrection.nc

9.24 MB
Binary file not shown.

Diff for: Untitled.ipynb

-138
This file was deleted.

Diff for: find_pixelcoords.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
if os.path.isfile(i): sys.path.append(i)
99
################################################################################################
1010

11-
def find_pixelcoords(im_filename,original_width,original_height,im_pick_params=0):
11+
def find_pixelcoords(im_filename,original_width,original_height,im_pick_params=0,predefined_row_inds=[]):
1212
"""
1313
% (C) Nick Holschuh - Amherst College -- 2022 ([email protected])
1414
%
@@ -20,14 +20,16 @@ def find_pixelcoords(im_filename,original_width,original_height,im_pick_params=0
2020
%
2121
% im_filename - The filename of the image that you want to extract info from
2222
% originial_width - The original matrix width (used to generate the image)
23-
% original__height - The original matrix height (used to generate the image
23+
% original__height - The original matrix height (used to generate the image)
2424
% im_pick_params - This defines how the contours are treated. There should be four parameters per image:
2525
% ### ---- which bands to look for minima in (color of lines)
2626
% ### ---- minimum contour size (number of points)
2727
% ### ---- the aggregation type (0: average of marker, 1: horizontal bar)
2828
% ### ---- distance threshold for pixel combination or separation
2929
% ### ---- the distance calculation method (0:true or 1:vertical or 2:overweight horizontal)
3030
% 0 defaults to [[0,5,0,5],[2,25,1,10]] which looks for blue points and red lines
31+
% predefined_row_inds - Some images have white pixels in the plot. This allows you to predefine
32+
% which rows should be treated as "within the image"
3133
%
3234
%%%%%%%%%%%%%%%
3335
% The outputs are:
@@ -66,7 +68,11 @@ def find_pixelcoords(im_filename,original_width,original_height,im_pick_params=0
6668

6769
rinds = ndh.minmax(np.where(row_sum <= np.percentile(row_sum,80)-1))
6870
cinds = ndh.minmax(np.where(col_sum <= np.percentile(col_sum,80)-1))
69-
71+
72+
########################## If we want to figure out the number of rows from the full dataset
73+
if len(predefined_row_inds) > 0:
74+
rinds = predefined_row_inds
75+
7076
xrange = np.linspace(0,original_width,cinds[1]-cinds[0])
7177
yrange = np.linspace(0,original_height,rinds[1]-rinds[0])
7278

Diff for: make_nc.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import xarray as xr
22
import numpy as np
33

4-
def make_nc(input_x,input_y,input_vars,input_varnames,num_dims=0,filename='temp.nc',description='No description provided',writefile0_or_dataset1_or_both2 = 1):
4+
def make_nc(input_x,input_y,input_vars,input_varnames,num_dims=0,filename='temp.nc',description='No description provided',coord_vars=['x','y'],writefile0_or_dataset1_or_both2 = 1):
55
"""
66
% (C) Nick Holschuh - Amherst College -- 2022 ([email protected])
77
%
@@ -16,6 +16,7 @@ def make_nc(input_x,input_y,input_vars,input_varnames,num_dims=0,filename='temp.
1616
% input_varnames,
1717
% filename='temp.nc',
1818
% description='No description provided',
19+
% coord_vars=List of names for x coord and y coord, defaults to ['x','y']
1920
% writefile0_or_dataset1 = 1
2021
%
2122
%%%%%%%%%%%%%%%
@@ -41,35 +42,35 @@ def make_nc(input_x,input_y,input_vars,input_varnames,num_dims=0,filename='temp.
4142
if num_dims == 0:
4243

4344
if input_vars.ndim == 1:
44-
data_dict[input_varnames[0]] = (['x'],input_vars)
45-
coord_dict = {'x':(['x'],input_x)}
45+
data_dict[input_varnames[0]] = ([coord_vars[0]],input_vars)
46+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x)}
4647
elif input_vars.ndim == 2:
47-
data_dict[input_varnames[0]] = (['y','x'],input_vars)
48-
coord_dict = {'x':(['x'],input_x),'y':(['y'],input_y)}
48+
data_dict[input_varnames[0]] = ([coord_vars[1],coord_vars[0]],input_vars)
49+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x),coord_vars[1]:([coord_vars[1]],input_y)}
4950

5051
####### If there is more than one variable provided, then it assumes that
5152
####### the variable data has been provided as a 3D array, with the
5253
####### third dimension defining each variable
5354
else:
5455
for ind1,i in enumerate(input_varnames):
55-
data_dict[i] = (['y','x'],input_vars[:,:,ind1])
56-
coord_dict = {'x':(['x'],input_x),'y':(['y'],input_y)}
56+
data_dict[i] = ([coord_vars[1],coord_vars[0]],input_vars[:,:,ind1])
57+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x),coord_vars[1]:([coord_vars[1]],input_y)}
5758

5859
######## Here we handle the case where you specify 1D data but provide
5960
######## multiple data variables
6061
if num_dims == 1:
6162
for ind1,i in enumerate(input_varnames):
62-
data_dict[i] = (['x'],input_vars[ind1])
63-
coord_dict = {'x':(['x'],input_x)}
63+
data_dict[i] = ([coord_vars[0]],input_vars[ind1])
64+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x)}
6465

6566
if num_dims == 2:
6667
if input_vars.ndim == 2:
67-
data_dict[input_varnames[0]] = (['y','x'],input_vars)
68-
coord_dict = {'x':(['x'],input_x),'y':(['y'],input_y)}
68+
data_dict[input_varnames[0]] = ([coord_vars[1],coord_vars[0]],input_vars)
69+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x),coord_vars[1]:([coord_vars[1]],input_y)}
6970
else:
7071
for ind1,i in enumerate(input_varnames):
71-
data_dict[i] = (['y','x'],input_vars[:,:,ind1])
72-
coord_dict = {'x':(['x'],input_x),'y':(['y'],input_y)}
72+
data_dict[i] = ([coord_vars[1],coord_vars[0]],input_vars[:,:,ind1])
73+
coord_dict = {coord_vars[0]:([coord_vars[0]],input_x),coord_vars[1]:([coord_vars[1]],input_y)}
7374

7475
######## Here is where the netcdf actually gets written
7576
ds = xr.Dataset(

Diff for: process_Standard_pickedpdf.py

+48-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212

13-
def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_flag=1, layer_save_type=1, layer_load=''):
13+
def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_flag=1, layer_save_type=1, layer_load='', find_rows_from_fullimageset = 0):
1414
"""
1515
% (C) Nick Holschuh - Amherst College -- 2022 ([email protected])
1616
%
@@ -26,6 +26,9 @@ def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_fl
2626
% layer_save_type=1 - For most applications, this should be set to 1, which is, save files in your current dir.
2727
% 0 - This allows you to save layer files within the cresis file_tree
2828
% layer_load='' - This is not fully implemented, but it would allow you to populate existing layer files
29+
% find_rows_from_fullimageset - Setting this to 1 will search all images to figure out which rows
30+
% of pixels are within the plot. For use when some of the bottom of radargrams
31+
% is all white.
2932
%
3033
%%%%%%%%%%%%%%%
3134
% The outputs are:
@@ -37,6 +40,7 @@ def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_fl
3740
import glob
3841
import os
3942
import numpy as np
43+
import matplotlib.pyplot as plt
4044

4145
from PIL import Image
4246
import cv2
@@ -100,6 +104,8 @@ def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_fl
100104
os.makedirs(comb_deconstruct_dir)
101105
if not os.path.isdir(save_dir):
102106
os.makedirs(save_dir)
107+
108+
103109
##########################################################################################################
104110
# Part 3 #################################################################################################
105111
######## Here we define the objects that need to be populated with picks
@@ -116,6 +122,42 @@ def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_fl
116122
##########################################################################################################
117123
# Part 5 ##################################################################################################
118124
print('Starting the information extraction.')
125+
126+
127+
128+
########## For images that have white at the bottom, we first look through all images to figure out the appropriate
129+
########## row indecies to use.
130+
131+
if find_rows_from_fullimageset == 1:
132+
for ind1,frame_fn in enumerate(frame_list):
133+
im_handle = Image.open(frame_fn)
134+
np_frame = np.array(im_handle)
135+
np_frame_dims = np_frame.shape
136+
137+
#print('Frame number %0.2d' % ind1)
138+
#print('np_frame shape: ',np_frame_dims)
139+
140+
########## For PNGs with RGB+components, this works best
141+
if np_frame_dims[2] == 4:
142+
if ind1 == 0:
143+
im_frame = np.array(np_frame[:,:,3] != 0).astype(float)
144+
else:
145+
im_frame = im_frame + np.array(np_frame[:,:,3] != 0).astype(float)
146+
########## For greyscale+transparent, this is what you need
147+
elif np_frame_dims[2] == 2:
148+
if ind1 == 0:
149+
im_frame = np.array(np_frame[:,:,1] != 0).astype(float)
150+
else:
151+
im_frame = im_frame + np.array(np_frame[:,:,1] != 0).astype(float)
152+
153+
if 'im_frame' in locals():
154+
#plt.imshow(im_frame)
155+
selected_rows = ndh.minmax(np.where(im_frame > 0)[0])
156+
#print(selected_rows)
157+
else:
158+
find_rows_from_fullimageset = 0
159+
print('Something went wrong with the full image-set. Defaulting to local row search.')
160+
119161
########## Here we actually load the images and extract pixel coordinate information
120162
error_frames = []
121163
good_frames = []
@@ -137,8 +179,11 @@ def process_Standard_pickedpdf(picked_files,orig_radar_dir,layer_save, cresis_fl
137179
original_height = height_index['index'][0]+100
138180
elif crop == 'nocrop':
139181
original_height = len(frame_data['Time'])
140-
141-
picks = ndh.find_pixelcoords(frame_fn,original_width,original_height,im_pick_params=[[2,25,1,10,1]])
182+
183+
if find_rows_from_fullimageset == 0:
184+
picks = ndh.find_pixelcoords(frame_fn,original_width,original_height,im_pick_params=[[2,25,1,10,1]])
185+
else:
186+
picks = ndh.find_pixelcoords(frame_fn,original_width,original_height,im_pick_params=[[2,25,1,10,1]], predefined_row_inds=selected_rows)
142187

143188

144189

Diff for: rotation.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
def rotation(input_vec,rotation_angle,dims=2,axis=0):
3+
4+
import numpy as np
5+
6+
if dims == 2:
7+
if axis == 0:
8+
########## A rotation around x
9+
rotation = np.array([[np.cos(rotation_angle),-np.sin(rotation_angle)],[np.sin(rotation_angle),np.cos(rotation_angle)]])
10+
11+
if dims == 3:
12+
if axis == 0:
13+
########## A rotation around x
14+
rotation = np.array([[1,0,0],[0,np.cos(ref_axial_tilt),-np.sin(ref_axial_tilt)],[0,np.sin(ref_axial_tilt),np.cos(ref_axial_tilt)]])
15+
16+
if axis == 1:
17+
########## A rotation around y
18+
rotation = np.array([[np.cos(ref_axial_tilt),0,np.sin(ref_axial_tilt)],[0,1,0],[-np.sin(ref_axial_tilt),0,np.cos(ref_axial_tilt)]])
19+
20+
if axis == 2:
21+
########## A rotation around z
22+
rotation = np.array([[np.cos(ref_axial_tilt),-np.sin(ref_axial_tilt),0],[np.sin(ref_axial_tilt),np.cos(ref_axial_tilt),0],[0,0,1]])
23+
24+
input_vec_rotated = np.matmul(input_vec,rotation)
25+
return input_vec_rotated

Diff for: smooth_ndh.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def smooth_ndh(input_array,smooth_window,window_type='Boxcar'):
3939
x = np.linspace(-4,4,smooth_window)
4040
smoothing_kernel = np.exp(-np.power(x, 2.) / (2 * np.power(1, 2.)))
4141

42-
42+
43+
input_array[input_array == np.NaN] = 0
4344
first_out = signal.fftconvolve(input_array, smoothing_kernel, mode = 'same')
4445
scalar = signal.fftconvolve(np.ones(input_array.shape), smoothing_kernel, mode = 'same')
4546
out_data = first_out / scalar;

0 commit comments

Comments
 (0)