Skip to content

Commit a1e1b42

Browse files
author
Nicholas Holschuh
committedJun 21, 2023
Added compare_list
1 parent 7b10ea1 commit a1e1b42

13 files changed

+301
-0
lines changed
 

‎COPdem_filelist.mat

3.17 MB
Binary file not shown.

‎add_datavariable_xr.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import xarray as xr
2+
import numpy as np
3+
4+
def add_datavariable_xr(xarray_dataset,new_datavariable,varname,coordinate_names=['y','x']):
5+
"""
6+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
7+
%
8+
% This function uses xarray to construct a netcdf
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
% The inputs are:
12+
%
13+
%
14+
%%%%%%%%%%%%%%%
15+
% The outputs are:
16+
%
17+
%
18+
%
19+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20+
"""
21+
22+
coordinate_names = tuple(coordinate_names)
23+
xarray_dataset = xarray_dataset.assign({varname:(coordinate_names,new_datavariable)})
24+
25+
return xarray_dataset

‎box_from_corners.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
3+
4+
def box_from_corners(xs,ys):
5+
"""
6+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
7+
%
8+
% This function prints out the minimum and maximum values of an array
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
% The inputs are:
12+
%
13+
% input_array -- array of data to analyze
14+
%
15+
%%%%%%%%%%%%%%%
16+
% The outputs are:
17+
%
18+
% output -- the min and max in a 1x2 array
19+
%
20+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21+
"""
22+
x_array = np.array([xs[0],xs[1],xs[1],xs[0],xs[0]])
23+
y_array = np.array([ys[1],ys[1],ys[0],ys[0],ys[1]])
24+
25+
box = np.stack([x_array,y_array]).T
26+
27+
return box
28+

‎cmap_data.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import matplotlib
2+
3+
4+
def cmap_data(colormapname):
5+
"""
6+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
7+
%
8+
% This function gets the colormap object for value extraction
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
% The inputs are:
12+
%
13+
% colormapname -- the string for the colormap of choice
14+
%
15+
%%%%%%%%%%%%%%%
16+
% The outputs are:
17+
%
18+
% cmap -- The colormap object, which takes values from 0-1 to generate a color.
19+
%
20+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21+
"""
22+
cmap = matplotlib.cm.get_cmap(colormapname)
23+
24+
return cmap
25+

‎compare_list.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
3+
def compare_list(list1,list2):
4+
"""
5+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
6+
%
7+
% This function tests the equality of every item in a list
8+
%
9+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10+
% The inputs are:
11+
%
12+
% list1 -- first list to compare
13+
% list2 -- second list to compare
14+
%
15+
%%%%%%%%%%%%%%%
16+
% The outputs are:
17+
%
18+
% true/false
19+
%
20+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21+
"""
22+
result1 = all(map(lambda x, y: np.all(x == y), list1,list2))
23+
result2 = all(map(lambda x, y: np.all(np.array(x).shape == np.array(y).shape), list1,list2))
24+
25+
comb_result = np.all([result1,result2])
26+
27+
return comb_result,result1,result2
28+

‎crossovers.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
3+
4+
def crossovers(line1,line2):
5+
6+
import numpy.core.umath_tests as ut
7+
8+
x_down = line1[:,0]
9+
y_down = line1[:,1]
10+
x_up = line2[:,0]
11+
y_up = line2[:,1]
12+
13+
p = np.column_stack((x_down, y_down))
14+
q = np.column_stack((x_up, y_up))
15+
16+
(p0, p1, q0, q1) = p[:-1], p[1:], q[:-1], q[1:]
17+
rhs = q0 - p0[:, np.newaxis, :]
18+
19+
mat = np.empty((len(p0), len(q0), 2, 2))
20+
mat[..., 0] = (p1 - p0)[:, np.newaxis]
21+
mat[..., 1] = q0 - q1
22+
mat_inv = -mat.copy()
23+
mat_inv[..., 0, 0] = mat[..., 1, 1]
24+
mat_inv[..., 1, 1] = mat[..., 0, 0]
25+
26+
det = mat[..., 0, 0] * mat[..., 1, 1] - mat[..., 0, 1] * mat[..., 1, 0]
27+
mat_inv /= det[..., np.newaxis, np.newaxis]
28+
29+
30+
params = ut.matrix_multiply(mat_inv, rhs[..., np.newaxis])
31+
intersection = np.all((params > 0.0001) & (params < 0.9999), axis=(-1, -2))
32+
p0_s = params[intersection, 0, :] * mat[intersection, :, 0]
33+
34+
xover_point = p0_s + p0[np.where(intersection)[0]]
35+
36+
return {'intersection_ind':[np.where(intersection)[0],np.where(intersection)[1]], 'intersection_points':xover_point}

‎dependencies.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
astropy
2+
hdf5storage
3+
ezdxf
4+
mat73
5+
imageio
6+
ffmpeg-python

‎find_COPdem_fn.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import sys
3+
import os
4+
import inspect
5+
sys.path.append('/mnt/data01/Code/')
6+
7+
8+
def find_COPdem_fn(lat,lon):
9+
"""
10+
This is a function that outputs the filename associated wtih the desired latitude & longitude
11+
"""
12+
13+
from NDH_Tools import find_nearest_xy
14+
from NDH_Tools import loadmat
15+
16+
curpath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
17+
mf_name = curpath+'/COPdem_filelist.mat'
18+
19+
file_info = loadmat(mf_name)
20+
target = np.array([[float(lat)], [float(lon)]])
21+
result = find_nearest_xy(file_info['latlon'],target)
22+
23+
if result['distance'] > 1:
24+
print('This DEM likely doesn\'t contain your point of interest.')
25+
print('The DEM center is %0.2f degrees away from the target point.' % result['distance'])
26+
27+
return file_info['fn'][result['index'][0]]
28+

‎index_list.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
def index_list(input_list,index_array):
4+
"""
5+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
6+
%
7+
% This function allows you to index a list by integer array input
8+
%
9+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10+
% The inputs are:
11+
%
12+
% input_list -- the list you want to sort
13+
% index_array -- array of vlues
14+
%
15+
%%%%%%%%%%%%%%%
16+
% The outputs are:
17+
%
18+
% output_list -- the sorted_list you want to sort
19+
%
20+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21+
"""
22+
output_list = [input_list[i] for i in index_array]
23+
24+
return output_list
25+

‎make_nc.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import xarray as xr
2+
import numpy as np
3+
4+
def make_nc(input_x,input_y,input_vars,input_varnames,filename='temp.nc',description='No description provided',writefile0_or_dataset1_or_both2 = 1):
5+
"""
6+
% (C) Nick Holschuh - Amherst College -- 2022 (Nick.Holschuh@gmail.com)
7+
%
8+
% This function uses xarray to construct a netcdf
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
% The inputs are:
12+
%
13+
% input_x,
14+
% input_y,
15+
% input_vars,
16+
% input_varnames,
17+
% filename='temp.nc',
18+
% description='No description provided',
19+
% writefile0_or_dataset1 = 1
20+
%
21+
%%%%%%%%%%%%%%%
22+
% The outputs are:
23+
%
24+
% output -- the min and max in a 1x2 array
25+
%
26+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27+
"""
28+
data_dict = {}
29+
if input_vars.ndim == 2:
30+
data_dict[input_varnames[0]] = (['y','x'],input_vars)
31+
else:
32+
for ind1,i in enumerate(input_varnames):
33+
data_dict[i] = (['y','x'],input_vars[:,:,ind1])
34+
35+
coord_dict = {'x':(['x'],input_x),'y':(['y'],input_y)}
36+
37+
ds = xr.Dataset(
38+
data_vars=data_dict,
39+
coords=coord_dict,
40+
attrs=dict(description=description),
41+
)
42+
43+
if writefile0_or_dataset1_or_both2 == 0:
44+
ds.to_netcdf(path=filename)
45+
elif writefile0_or_dataset1_or_both2 == 2:
46+
ds.to_netcdf(path=filename)
47+
return ds
48+
else:
49+
return ds

‎remove_arrow.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def remove_arrow(input_axisObject,num=1):
2+
"""
3+
% (C) Nick Holschuh - Amherst College - 2022 (Nick.Holschuh@gmail.com)
4+
% This function removes contours (but I don't remember why I needed this)
5+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6+
% The inputs are as follows:
7+
%
8+
% input_axisObject - axis object to remove to remove the most recent line or lines
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
"""
12+
13+
for i in range(num):
14+
try:
15+
input_axisObject.patches.pop(-1)
16+
except:
17+
print('Couldn''t remove image')

‎remove_image.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def remove_image(input_axisObject,num=1):
2+
"""
3+
% (C) Nick Holschuh - Amherst College - 2022 (Nick.Holschuh@gmail.com)
4+
% This function removes contours (but I don't remember why I needed this)
5+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6+
% The inputs are as follows:
7+
%
8+
% input_axisObject - axis object to remove to remove the most recent line or lines
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
"""
12+
13+
for i in range(num):
14+
try:
15+
input_axisObject.images[-1].remove()
16+
except:
17+
print('Couldn''t remove image')

‎remove_line.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def remove_line(input_axisObject,num=1):
2+
"""
3+
% (C) Nick Holschuh - Amherst College - 2022 (Nick.Holschuh@gmail.com)
4+
% This function removes contours (but I don't remember why I needed this)
5+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6+
% The inputs are as follows:
7+
%
8+
% input_axisObject - axis object to remove to remove the most recent line or lines
9+
%
10+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11+
"""
12+
13+
for i in range(num):
14+
try:
15+
input_axisObject.lines.pop(-1)
16+
except:
17+
print('Couldn''t remove image')

0 commit comments

Comments
 (0)
Please sign in to comment.