Skip to content

Commit 123f776

Browse files
committed
fixed h5 retriever
1 parent 92d4fe3 commit 123f776

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

neonwranglerpy/lib/clip_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def clip_plot(plt, list_data, bff=12, savepath=""):
1010
# plt['easting'] = plt['easting'].astype(int)
1111
# plt['northing'] = plt['northing'].astype(int)
1212
# tile = plt["plt_e"].values.astype(str) + "_" + plt["plt_n"].values.astype(str)
13-
tiles = str(plt["plt_e"]) + "_" + str(plt["plt_n"])
13+
tiles = str(plt.plt_e[0]) + "_" + str(plt.plt_n[0])
1414
tiles = [f for f in list_data if tiles in f]
1515
missed_plots = []
1616

neonwranglerpy/lib/crop_plot_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def crop_data_to_plot(plt,
1919
dpID='DP3.30006.001',
2020
dataset_path="",
2121
target_year=2018,
22-
bff=520,
22+
bff=20,
2323
tasks=1,
2424
parallelized=False,
2525
savepath=""):

neonwranglerpy/lib/extract_hsi_to_tif.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,25 @@ def h5refl2array(refl_filename, remove_water_bands=True):
4848
sns_zn = hdf5_file[sitename]['Reflectance/Metadata/to-sensor_zenith_angle'][()]
4949
# get solar angles as array to leverage flightpaths mosaic
5050
flightpaths = [
51-
hdf5_file[sitename]['Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index']
52-
[()]
51+
hdf5_file[sitename][
52+
'Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index'][()]
5353
]
5454
sol_zn = [
55-
hdf5_file[sitename]['Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index']
56-
[()]
55+
hdf5_file[sitename][
56+
'Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index'][()]
5757
]
5858
sol_az = [
59-
hdf5_file[sitename]['Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index']
60-
[()]
59+
hdf5_file[sitename][
60+
'Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index'][()]
6161
]
62-
for pt in range(len(solar_angles)):
63-
sol_az[flightpaths == solar_angles[pt][0]] = solar_angles[pt][1]
64-
sol_zn[flightpaths == solar_angles[pt][0]] = solar_angles[pt][2]
62+
# turn sol_az and sol_zn into arrays
63+
sol_zn = np.array(sol_zn)
64+
sol_az = np.array(sol_az)
6565

66+
for pt in range(len(solar_angles)):
67+
good_pixels = flightpaths == solar_angles[pt][0]
68+
sol_az[good_pixels] = solar_angles[pt][1]
69+
sol_zn[good_pixels] = solar_angles[pt][2]
6670

6771
#
6872
mapInfo_string = str(metadata['mapInfo'])
@@ -101,8 +105,9 @@ def tile_solar_angle(full_path):
101105
file_attrs_string = str(list(hdf5_file.items()))
102106
file_attrs_string_split = file_attrs_string.split("'")
103107
sitename = file_attrs_string_split[1]
104-
flight_paths = hdf5_file[sitename]
105-
["Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index"].attrs["Data_Files"]
108+
flight_paths = hdf5_file[
109+
sitename]["Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index"
110+
].attrs["Data_Files"]
106111
flight_paths = str(flight_paths).split(",")
107112
which_paths = np.unique(
108113
hdf5_file[sitename]["Reflectance/Metadata/Ancillary_Imagery/Data_Selection_Index"]
@@ -291,7 +296,12 @@ def generate_raster(h5_path,
291296
os.path.basename(rgb_filename))[0] + "_hyperspectral{}.tif".format(suffix)
292297

293298
# stach solar and sensor data to be used for corrections
294-
sol_sens_angle = np.array([sol_az, sol_zn, sns_az, sns_zn])
299+
sol_az_reshaped = sol_az.squeeze() # Remove the extra dimension
300+
sol_zn_reshaped = sol_zn.squeeze() # Remove the extra dimension
301+
sns_az_reshaped = sns_az # No need to reshape sns_az
302+
sns_zn_reshaped = sns_zn
303+
304+
sol_sens_angle = np.array([sol_az_reshaped, sol_zn_reshaped, sns_az_reshaped, sns_zn_reshaped])
295305
solar_tilename = os.path.splitext(
296306
os.path.basename(rgb_filename))[0] + "_solar_sensor_angle{}.tif".format(suffix)
297307
# Save georeference crop to file

tests/test_h5refl2array.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
import numpy as np
3+
import h5py
4+
import os
5+
6+
class TestFunc(unittest.TestCase):
7+
8+
def setUp(self):
9+
# Prepare a dummy hdf5 file for testing
10+
self.filename = "testfile.h5"
11+
with h5py.File(self.filename, 'w') as f:
12+
# create datasets in the hdf5 file similar to the actual data the function expects
13+
## Omitted here for brevity
14+
15+
def test_reflection2array(self):
16+
reflArray, metadata, sol_az, sol_zn, sns_az, sns_zn = h5refl2array(self.filename)
17+
18+
# Test the type of the returned objects
19+
self.assertTrue(isinstance(reflArray, np.ndarray))
20+
self.assertTrue(isinstance(metadata, dict))
21+
self.assertTrue(isinstance(sol_az, list))
22+
self.assertTrue(isinstance(sol_zn, list))
23+
self.assertTrue(isinstance(sns_az, np.ndarray))
24+
self.assertTrue(isinstance(sns_zn, np.ndarray))
25+
26+
# Test the shape or any other property of the returned objects
27+
# This will really depend on what you're expecting
28+
## Omitted here for brevity
29+
30+
def tearDown(self):
31+
# Removes the test file
32+
os.remove(self.filename)
33+
34+
if __name__ == '__main__':
35+
unittest.main()

0 commit comments

Comments
 (0)