Skip to content

Commit 7b10ea1

Browse files
author
Nicholas Holschuh
committed
Adding changes from HPC HolschuhLab
1 parent 71aba70 commit 7b10ea1

8 files changed

+130
-31
lines changed

find_nearest_xy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ def find_nearest_xy(vector_2_search,value,how_many=1):
3434

3535
comp_vec = vector_2_search[:,0] + vector_2_search[:,1]*np.sqrt(np.array(-1,dtype=complex));
3636
ss = value.shape;
37-
if np.max(ss) > 1 & ss[0] > ss[1]:
37+
if np.all([np.min(ss) > 1,ss[0] >= ss[1]]):
3838
value2 = value.transpose();
3939
elif ss[0] == 1:
4040
value2 = value.transpose();
4141
else:
4242
value2 = value;
4343

4444

45-
value_search = value2[:,0] + value2[:,1]*np.sqrt(np.array(-1,dtype=complex));
45+
value_search = value2[0,:] + value2[1,:]*np.sqrt(np.array(-1,dtype=complex));
4646

4747
complex_dists = np.tile(comp_vec,(len(value_search),1)).transpose()-np.tile(value_search,(len(comp_vec),1))
4848
dists = np.abs(complex_dists);

flatten_list.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def flatten_list(list_of_lists):
1+
import sys
2+
sys.path.append('/mnt/data01/Code/')
3+
4+
def flatten_list(list_of_lists,recursive=1):
25
"""
36
% (C) Nick Holschuh - Amherst College - 2022 ([email protected])
47
% This will take a list of lists (or an array of arrays) and flattens
@@ -7,6 +10,7 @@ def flatten_list(list_of_lists):
710
% The inputs are as follows:
811
%
912
% flatten_list -- The list of lists to be flattened
13+
% recursive -- This will keep flattening until the list stops changing
1014
%
1115
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216
% The outputs are as follows:
@@ -18,19 +22,39 @@ def flatten_list(list_of_lists):
1822
"""
1923

2024
import numpy as np
25+
from NDH_Tools import compare_list
2126

2227
flat_list = []
2328
array_flag = 0
24-
for sublist in list_of_lists:
25-
if isinstance(sublist,type([])):
26-
for item in sublist:
27-
flat_list.append(item)
28-
elif isinstance(sublist,type(np.array([]))):
29-
array_flag = 1
30-
for item in sublist:
31-
flat_list.append(item)
32-
else:
33-
flat_list.append(sublist)
29+
recursive_flag = 1
30+
flat_list_check = list_of_lists.copy()
31+
32+
while recursive_flag == 1:
33+
for sublist in list_of_lists:
34+
if isinstance(sublist,type([])):
35+
for item in sublist:
36+
flat_list.append(item)
37+
elif isinstance(sublist,type(np.array([]))):
38+
array_flag = 1
39+
for item in sublist:
40+
flat_list.append(item)
41+
else:
42+
flat_list.append(sublist)
3443

35-
flat_list = np.array(flat_list)
36-
return flat_list
44+
flat_list = list(np.array(flat_list))
45+
46+
######### Here we test if the lists are truly identical
47+
comb_result,r1,r2 = compare_list(flat_list,flat_list_check)
48+
49+
if comb_result:
50+
recursive_flag = 0
51+
if recursive == 0:
52+
recursive_flag = 0
53+
54+
if recursive_flag == 1:
55+
flat_list_check = flat_list.copy()
56+
list_of_lists = flat_list.copy()
57+
flat_list = []
58+
59+
60+
return flat_list

generate_animation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def generate_animation(fps,title='Matplotlib Animation',comment='Matplotlib Anim
99
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010
% The inputs are as follows:
1111
%
12-
% spacing -- the gap between sequential values
13-
% fps -- the offset from 0 to round to
14-
%
12+
% fps -- The framerate (in frames per second)
13+
% title -- This is the title of the video as stored in the file
14+
% comment -- This stores some meta-text about your videoo
1515
%
1616
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1717
% The outputs are as follows:

is2_fileselect.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from scipy.io import loadmat
77
import inspect
88

9-
def is2_fileselect(xbounds, ybounds, fdir='/data/rd08/projects/IS2/ATL06', ant_or_green=0, filetype='ATL06', cycle=6, print_flag=0):
9+
def is2_fileselect(xbounds, ybounds, fdir='/mnt/data01/Data/IS2/ATL11/', ant_or_green=0, filetype='ATL11', cycle=6, print_flag=0):
1010
"""
1111
% (C) Nick Holschuh - Amherst College -- 2022 ([email protected])
1212
% This function identifies the files and the segids for ICESat-2 data that falls within
@@ -17,9 +17,9 @@ def is2_fileselect(xbounds, ybounds, fdir='/data/rd08/projects/IS2/ATL06', ant_o
1717
% xbounds -- two values that define the minimum x and maximum x coord (polar stereographic)
1818
% ybounds -- two values that define the minumum y and maximum y coord (polar stereographic)
1919
% fdir -- the absolute path to the directory where the ATL06/11 files are stored
20-
% ant_or_green -- Specifices which region range to select from
21-
% filetype -- string, currently only 'ATL06' is implemented
22-
% cycle -- the cycle number for plotting, from 1-6 [default 6]
20+
% ant_or_green -- Specifices which region range to select from [0: Ant, 1:Gre]
21+
% filetype -- string, currently only 'ATL06' and 'ATL11' are implemented
22+
% cycle -- the cycle number for plotting, from 1-6 [default 6], only required for ATL06
2323
%
2424
%%%%%%%%%%%%%%%
2525
% The outputs are:

loadmat.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
11
import hdf5storage
2-
from scipy.io import loadmat
2+
import mat73
3+
import scipy
34

4-
def loadmat(fn):
5+
import sys
6+
sys.path.append('/mnt/data01/Code/')
7+
8+
def loadmat(fn,varnames=None):
59
"""
610
711
812
"""
13+
14+
from NDH_Tools import read_h5
915

1016
try:
11-
data = hdf5storage.loadmat(fn)
17+
if varnames == None:
18+
data = mat73.loadmat(fn)
19+
#print('Used the Mat73 Method')
20+
else:
21+
data = read_h5(fn,varnames)
22+
1223
except:
13-
data = loadmat(fn)
14-
24+
try:
25+
26+
data = scipy.io.loadmat(fn,varnames,squeeze_me=True)
27+
28+
# ############ This loop collapses unnecessary dimensions
29+
# for i in data.keys():
30+
# if type(data[i]) == type({}):
31+
# for j in data[i].keys():
32+
# if type(data[i][j]) == type({}):
33+
# pass
34+
# else:
35+
# try:
36+
# data[i][j] = np.squeeze(data[i][j])
37+
# except:
38+
# pass
39+
# else:
40+
# try:
41+
# data[i] = np.squeeze(data[i])
42+
# except:
43+
# pass
44+
45+
46+
#print('Used the SciPy Method')
47+
except:
48+
print('Something is wrong with this .mat file')
49+
1550
return data
1651

52+

regrid.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import numpy as np
22
from scipy.interpolate import RegularGridInterpolator
3+
import matplotlib.pyplot as plt
4+
5+
class CustomError(Exception):
6+
pass
37

48
def regrid(inx,iny,indata,newx,newy):
59
"""
@@ -22,6 +26,8 @@ def regrid(inx,iny,indata,newx,newy):
2226
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2327
"""
2428

29+
############### Here we decide if we need to flip the input data to make axes increasing --
30+
############### (This is important so that we know to re-flip the output before returning)
2531
dx = inx[1]-inx[0]
2632
dy = iny[1]-iny[0]
2733

@@ -39,12 +45,33 @@ def regrid(inx,iny,indata,newx,newy):
3945
else:
4046
flipy_flag = 0
4147

48+
############### But we also need to know if we need to flip the comparison grid --
49+
############### (This is only important for the regridding step)
50+
dx2 = newx[1]-newx[0]
51+
dy2 = newy[1]-newy[0]
52+
if dx2 < 0:
53+
newx = newx[::-1]
54+
if dy2 < 0:
55+
newy = newy[::-1]
56+
57+
4258
############## Here is the actual function
43-
my_interpolater = RegularGridInterpolator((iny,inx),indata)
59+
my_interpolater = RegularGridInterpolator((iny,inx),indata,fill_value=np.NaN,bounds_error=False)
4460

4561
finalx,finaly = np.meshgrid(newx,newy)
4662

47-
outdata = my_interpolater((finaly,finalx))
63+
try:
64+
outdata = my_interpolater((finaly,finalx))
65+
except:
66+
print('Input Grid (must have values for all output grid: [',np.min(inx),np.max(inx),'],[',np.min(iny),np.max(iny),']')
67+
print('Output Grid: [',np.min(newx),np.max(newx),'],[',np.min(newy),np.max(newy),']')
68+
box1x = [np.min(inx),np.max(inx),np.max(inx),np.min(inx),np.min(inx)]
69+
box1y = [np.max(iny),np.max(iny),np.min(iny),np.min(iny),np.max(iny)]
70+
box2x = [np.min(newx),np.max(newx),np.max(newx),np.min(newx),np.min(newx)]
71+
box2y = [np.max(newy),np.max(newy),np.min(newy),np.min(newy),np.max(newy)]
72+
plt.plot(box1x,box1y,'--',c='black',label='Input Grid')
73+
plt.plot(box2x,box2y,'-',c='red',label='Output Grid')
74+
raise CustomError("You have a bounds error for your grids")
4875

4976
if flipy_flag == 1:
5077
outdata = np.flipud(outdata)

smooth_ndh.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from scipy import signal
22
import numpy as np
33

4-
def smooth_ndh(input_array,smooth_window):
4+
def smooth_ndh(input_array,smooth_window,window_type='Boxcar'):
55
"""
66
% (C) Nick Holschuh - Amherst College - 2022 ([email protected])
77
% Convolution based smoothing function -- both 1d and 2d
@@ -28,6 +28,17 @@ def smooth_ndh(input_array,smooth_window):
2828
smoothing_kernel = smoothing_kernel/np.prod(smoothing_kernel.shape)
2929
else:
3030
smoothing_kernel = smooth_window
31+
32+
if window_type == 'Gaussian':
33+
if len(input_array.shape) == 2:
34+
x = np.linspace(-4,4,smooth_window)
35+
y = np.linspace(-4,4,smooth_window)
36+
xmesh, ymesh = np.meshgrid(x,y);
37+
smoothing_kernel = np.exp(-(np.power(xmesh, 2.) / (2 * np.power(1, 2.)) + np.power(ymesh, 2.) / (2 * np.power(1, 2.))))
38+
elif len(input_array.shape) == 1:
39+
x = np.linspace(-4,4,smooth_window)
40+
smoothing_kernel = np.exp(-np.power(x, 2.) / (2 * np.power(1, 2.)))
41+
3142

3243
first_out = signal.fftconvolve(input_array, smoothing_kernel, mode = 'same')
3344
scalar = signal.fftconvolve(np.ones(input_array.shape), smoothing_kernel, mode = 'same')

yearfrac.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ def yearfrac(date):
5555
years = i.astype('datetime64[Y]').astype(int) + 1970
5656
months = i.astype('datetime64[M]').astype(int) % 12 + 1
5757
days = i - i.astype('datetime64[M]') + 1
58-
days = (days*1.15741e-14).astype('int')
58+
days = (days).astype('int')
5959

60+
#print(years,months,days)
6061
date_output.append(float(years)+float(months)/12+float(days)/365.25)
6162

6263
return date_output

0 commit comments

Comments
 (0)