diff --git a/neonwranglerpy/lib/clip_plot.py b/neonwranglerpy/lib/clip_plot.py index e43ff26..e1a25ee 100644 --- a/neonwranglerpy/lib/clip_plot.py +++ b/neonwranglerpy/lib/clip_plot.py @@ -10,7 +10,7 @@ def clip_plot(plt, list_data, bff=12, savepath=""): # plt['easting'] = plt['easting'].astype(int) # plt['northing'] = plt['northing'].astype(int) # tile = plt["plt_e"].values.astype(str) + "_" + plt["plt_n"].values.astype(str) - tiles = str(plt.plt_e[0]) + "_" + str(plt.plt_n[0]) + tiles = str(plt["plt_e"]) + "_" + str(plt["plt_n"]) tiles = [f for f in list_data if tiles in f] missed_plots = [] diff --git a/neonwranglerpy/lib/extract_hsi_to_tif.py b/neonwranglerpy/lib/extract_hsi_to_tif.py index 7432721..e7b77f8 100644 --- a/neonwranglerpy/lib/extract_hsi_to_tif.py +++ b/neonwranglerpy/lib/extract_hsi_to_tif.py @@ -192,6 +192,9 @@ def array2raster(newRaster, originY = extent['yMax'] res = reflArray_metadata['res']['pixelWidth'] transform = Affine.translation(originX, originY) * Affine.scale(res, -res) + + if not os.path.exists(ras_dir): + os.makedirs(ras_dir) with rasterio.open( "{}/{}".format(ras_dir, newRaster), 'w', @@ -306,6 +309,7 @@ def generate_raster(h5_path, solar_tilename = os.path.splitext( os.path.basename(rgb_filename))[0] + "_solar_sensor_angle{}.tif".format(suffix) # Save georeference crop to file + save_dir = os.path.abspath(save_dir) array2raster(solar_tilename, sol_sens_angle, metadata, diff --git a/tests/raw_data/h5_data/DP3.30006.001/2017/FullSite/D16/2017_ABBY_1/L3/Spectrometer/Reflectance/NEON_D16_ABBY_DP3_559000_5070000_reflectance.h5 b/tests/raw_data/h5_data/DP3.30006.001/2017/FullSite/D16/2017_ABBY_1/L3/Spectrometer/Reflectance/NEON_D16_ABBY_DP3_559000_5070000_reflectance.h5 new file mode 100644 index 0000000..aeae551 Binary files /dev/null and b/tests/raw_data/h5_data/DP3.30006.001/2017/FullSite/D16/2017_ABBY_1/L3/Spectrometer/Reflectance/NEON_D16_ABBY_DP3_559000_5070000_reflectance.h5 differ diff --git a/tests/test_h5refl2array.py b/tests/test_h5refl2array.py index d7db7aa..1354319 100644 --- a/tests/test_h5refl2array.py +++ b/tests/test_h5refl2array.py @@ -1,35 +1,52 @@ import unittest import numpy as np -import h5py +import subprocess import os +import pytest -class TestFunc(unittest.TestCase): +from neonwranglerpy.lib.extract_hsi_to_tif import h5refl2array - def setUp(self): - # Prepare a dummy hdf5 file for testing - self.filename = "testfile.h5" - with h5py.File(self.filename, 'w') as f: - # create datasets in the hdf5 file similar to the actual data the function expects - ## Omitted here for brevity +# Main Paths +file_location = os.path.dirname(os.path.realpath(__file__)) +neonwranglerpy_root_dir = os.path.abspath(os.path.join(file_location, os.pardir)) - def test_reflection2array(self): - reflArray, metadata, sol_az, sol_zn, sns_az, sns_zn = h5refl2array(self.filename) +# Paths of the raw data files used +raw_dir_files = os.path.normpath(os.path.join(neonwranglerpy_root_dir, 'raw_data')) - # Test the type of the returned objects - self.assertTrue(isinstance(reflArray, np.ndarray)) - self.assertTrue(isinstance(metadata, dict)) - self.assertTrue(isinstance(sol_az, list)) - self.assertTrue(isinstance(sol_zn, list)) - self.assertTrue(isinstance(sns_az, np.ndarray)) - self.assertTrue(isinstance(sns_zn, np.ndarray)) +test_reflection2array_data = [ + ('test_reflection2array', "h5_data/DP3.30006.001/2017/FullSite/D16/2017_ABBY_1/L3/Spectrometer/Reflectance" + "/NEON_D16_ABBY_DP3_559000_5070000_reflectance.h5") +] - # Test the shape or any other property of the returned objects - # This will really depend on what you're expecting - ## Omitted here for brevity - def tearDown(self): - # Removes the test file - os.remove(self.filename) +def setup_module(): + """Automatically sets up the environment before the module runs.""" + os.chdir(neonwranglerpy_root_dir) + subprocess.call(['cp', '-r', 'tests/raw_data', neonwranglerpy_root_dir]) -if __name__ == '__main__': - unittest.main() + +def teardown_module(): + """Automatically clean up after the module.""" + os.chdir(neonwranglerpy_root_dir) + subprocess.call(['rm', '-r', 'raw_data']) + + +def setup_functions(): + """Set up functions.""" + teardown_module() + setup_module() + + +@pytest.mark.parametrize("test_name, path", test_reflection2array_data) +def test_reflection2array(test_name, path): + setup_functions() + path = os.path.join(raw_dir_files, path) + reflArray, metadata, sol_az, sol_zn, sns_az, sns_zn = h5refl2array(path) + + # Test the type of the returned objects + assert (isinstance(reflArray, np.ndarray)) + assert (isinstance(metadata, dict)) + assert (isinstance(sol_az, np.ndarray)) + assert (isinstance(sol_zn, np.ndarray)) + assert (isinstance(sns_az, np.ndarray)) + assert (isinstance(sns_zn, np.ndarray)) diff --git a/tests/test_lib.py b/tests/test_lib.py index f48afc0..5306cc2 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -49,7 +49,15 @@ 'utmZone': ['6N', '6N'], 'easting': [559120, 559120], 'northing': [5070120, 5070120] - }, "DP3.30015.001", "raster_data", "2018", ["NEON_D16_ABBY_DP3_559000_5070000_CHM.tif", True]) + }, "DP3.30015.001", "raster_data", "2018", ["NEON_D16_ABBY_DP3_559000_5070000_CHM.tif", True]), + ('test_extract_h5', { + 'plotID': ['TEST_0000', 'TEST_0000'], + 'subplotID': ['A', "A"], + 'siteID': ['ABBY', 'ABBY'], + 'utmZone': ['6N', '6N'], + 'easting': [559120, 559120], + 'northing': [5070120, 5070120] + }, "DP3.30006.001", "h5_data", "2017", ["NEON_D16_ABBY_DP3_559000_5070000_reflectance_hyperspectral.tif", True]) ]