-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspectral_unmixing_tools_original.py
2471 lines (1963 loc) · 94.6 KB
/
spectral_unmixing_tools_original.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Standard Library Imports
import glob
import json
import os
import random
import subprocess
import time
# Third-Party Imports
import geopandas as gpd
import h5py
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import rasterio
import requests
import ray
from rasterio.features import rasterize
from rasterio.mask import mask
from rasterio.plot import show
from shapely.geometry import box
from sklearn.ensemble import GradientBoostingRegressor
from tqdm import tqdm
# Local Application/Specific Imports
import hytools as ht
def jefe(base_folder, site_code, product_code, year_month, flight_lines):
"""
A control function that orchestrates the processing of spectral data.
It first calls go_forth_and_multiply to generate necessary data and structures,
then processes all subdirectories within the base_folder.
Parameters:
- base_folder (str): The base directory for both operations.
- site_code (str): Site code for go_forth_and_multiply.
- product_code (str): Product code for go_forth_and_multiply.
- year_month (str): Year and month for go_forth_and_multiply.
- flight_lines (list): A list of flight lines for go_forth_and_multiply.
"""
# First, call go_forth_and_multiply with the provided parameters
go_forth_and_multiply(
base_folder=base_folder,
site_code=site_code,
product_code=product_code,
year_month=year_month,
flight_lines=flight_lines
)
polygon_layer = "aop_macrosystems_data_1_7_25.geojson"
process_base_folder(
base_folder=base_folder,
polygon_layer=polygon_layer,
raster_crs_override="EPSG:4326", # Optional CRS override
polygons_crs_override="EPSG:4326", # Optional CRS override
output_masked_suffix="_masked", # Optional suffix for output
plot_output=False, # Disable plotting
dpi=300 # Set plot resolution
)
# Next, process all subdirectories within the base_folder
process_all_subdirectories(base_folder)
# Finally, clean the CSV files by removing rows with any NaN values
#clean_csv_files_in_subfolders(base_folder)
#merge_csvs_by_columns(base_folder)
#validate_output_files(base_folder)
print("Jefe finished. Please check for the _with_mask_and_all_spectra.csv for your hyperspectral data from NEON flight lines extracted to match your provided polygons")
pass
def go_forth_and_multiply(base_folder="output", **kwargs):
#start_time = time.time() # Capture start time
# Create the base folder if it doesn't exist
os.makedirs(base_folder, exist_ok=True)
# Step 1: Download NEON flight lines with kwargs passed to this step
download_neon_flight_lines(**kwargs)
# Step 2: Convert flight lines to ENVI format
flight_lines_to_envi(output_dir = base_folder)
# Step 3: Generate configuration JSON
generate_config_json(base_folder)
# Step 4: Apply topographic and BRDF corrections
apply_topo_and_brdf_corrections(base_folder)
# Step 5: Resample and translate data to other sensor formats
resample_translation_to_other_sensors(base_folder)
#end_time = time.time() # Capture end time
#elapsed_time = end_time - start_time # Calculate elapsed time
#hours, rem = divmod(elapsed_time, 3600)
# minutes, seconds = divmod(rem, 60)
print("Processing complete.")
#print(f"Total time taken: {int(hours)} hours, {int(minutes)} minutes, {int(seconds)} seconds.")
pass
import requests
import subprocess
def download_neon_file(site_code, product_code, year_month, flight_line):
server = 'http://data.neonscience.org/api/v0/'
data_url = f'{server}data/{product_code}/{site_code}/{year_month}'
# Make the API request
response = requests.get(data_url)
if response.status_code == 200:
print(f"Data retrieved successfully for {year_month}!")
data_json = response.json()
# Initialize a flag to check if the file was found
file_found = False
# Iterate through files in the JSON response to find the specific flight line
for file_info in data_json['data']['files']:
file_name = file_info['name']
if flight_line in file_name:
print(f"Downloading {file_name} from {file_info['url']}")
# Use subprocess.run to handle output
try:
result = subprocess.run(
['wget', '--no-check-certificate', file_info["url"], '-O', file_name],
stdout=subprocess.PIPE, # Capture standard output
stderr=subprocess.PIPE, # Capture standard error
text=True # Decode to text
)
# Check for errors
if result.returncode != 0:
print(f"Error downloading file: {result.stderr}")
else:
print(f"Download completed for {file_name}")
except Exception as e:
print(f"An error occurred: {e}")
file_found = True
break
if not file_found:
print(f"Flight line {flight_line} not found in the data for {year_month}.")
else:
print(f"Failed to retrieve data for {year_month}. Status code: {response.status_code}, Response: {response.text}")
pass
def download_neon_flight_lines(site_code, product_code, year_month, flight_lines):
"""
Downloads NEON flight line files given a site code, product code, year, month, and flight line(s).
Args:
- site_code (str): The site code.
- product_code (str): The product code.
- year_month (str): The year and month of interest in 'YYYY-MM' format.
- flight_lines (str or list): A single flight line identifier or a list of flight line identifiers.
"""
# Check if flight_lines is a single string (flight line), if so, convert it to a list
if isinstance(flight_lines, str):
flight_lines = [flight_lines]
# Iterate through each flight line and download the corresponding file
for flight_line in flight_lines:
print(f"Processing flight line: {flight_line}")
download_neon_file(site_code, product_code, year_month, flight_line)
print("Download completed.\n")
pass
def resample_translation_to_other_sensors(base_folder, conda_env_path='/opt/conda/envs/macrosystems/bin/python'):
# List all subdirectories in the base folder
subdirectories = [os.path.join(base_folder, d) for d in os.listdir(base_folder) if os.path.isdir(os.path.join(base_folder, d))]
print("Starting tranlation to other sensors")
for folder in subdirectories:
print(f"Processing folder: {folder}")
translate_to_other_sensors(folder, conda_env_path)
print("done resampling")
pass
def translate_to_other_sensors(folder_path, conda_env_path='/opt/conda/envs/macrosystems/bin/python'):
# List of sensor types to loop through
sensor_types = [
'Landsat 5 TM',
'Landsat 7 ETM+',
'Landsat 8 OLI',
'Landsat 9 OLI-2',
'MicaSense',
'MicaSense-to-match TM and ETM+',
'MicaSense-to-match OLI and OLI-2'
]
# Find all files ending with '_envi' but not with 'config_envi' or '.json'
pattern = os.path.join(folder_path, '*_envi')
envi_files = [file for file in glob.glob(pattern) if not file.endswith('config_envi') and not file.endswith('.json')]
# Check if we found exactly one file that matches our criteria
if len(envi_files) != 1:
print(f"Error: Expected to find exactly one file with '_envi' but found {len(envi_files)}: {envi_files}")
return
resampling_file_path = envi_files[0] # Use the file found
json_file = os.path.join('Resampling', 'landsat_band_parameters.json')
for sensor_type in sensor_types:
hdr_path = f"{resampling_file_path}.hdr"
output_path = os.path.join(folder_path, f"{os.path.basename(resampling_file_path)}_resample_{sensor_type.replace(' ', '_').replace('+', 'plus')}.hdr")
command = [
conda_env_path, 'Resampling/resampling_demo.py',
'--resampling_file_path', resampling_file_path,
'--json_file', json_file,
'--hdr_path', hdr_path,
'--sensor_type', sensor_type,
'--output_path', output_path
]
# Run the command
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check for errors
if process.returncode != 0:
print(f"Error executing command: {' '.join(command)}")
print(f"Standard Output: {process.stdout}")
print(f"Error Output: {process.stderr}")
else:
print(f"Command executed successfully for sensor type: {sensor_type}")
print(f"Standard Output: {process.stdout}")
pass
def apply_topo_and_brdf_corrections(base_folder_path, conda_env_path='/opt/conda/envs/macrosystems'):
# Construct the full path to the Python executable in the specified Conda environment
python_executable = os.path.join(conda_env_path, "bin", "python")
print("Starting topo and BRDF correction. This takes a long time.")
# Find all subfolders in the base folder
subfolders = [f for f in glob.glob(os.path.join(base_folder_path, '*')) if os.path.isdir(f)]
for folder in subfolders:
folder_name = os.path.basename(os.path.normpath(folder))
json_file_name = f"{folder_name}_config__envi.json"
json_file_path = os.path.join(folder, json_file_name)
print(f"Processing folder: {folder}")
print(f"Looking for JSON file: {json_file_path}")
# Check if the JSON file exists
if os.path.isfile(json_file_path):
# Call the script with the JSON file path
command = f"{python_executable} image_correct.py {json_file_path}"
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if process.returncode != 0:
print(f"Error executing command: {command}")
print(f"Standard Output: {process.stdout}")
print(f"Error Output: {process.stderr}")
else:
print(f"Successfully processed: {json_file_path}")
print(f"Standard Output: {process.stdout}")
else:
print(f"JSON file not found: {json_file_path}")
print("All done!")
def generate_correction_configs_for_directory(directory):
"""
Generates configuration files for TOPO and BRDF corrections for all ancillary files in a given directory.
Args:
- directory (str): The directory containing the main image and its ancillary files.
"""
# Define your configuration settings
bad_bands = [[300, 400], [1337, 1430], [1800, 1960], [2450, 2600]]
file_type = 'envi'
main_image_name = os.path.basename(directory)
main_image_file = os.path.join(directory, main_image_name ) # Assuming the main image file has .h5 extension
# Glob pattern to find ancillary files within the same directory
anc_files_pattern = os.path.join(directory, "*_ancillary*")
anc_files = glob.glob(anc_files_pattern)
anc_files.sort()
aviris_anc_names = ['path_length', 'sensor_az', 'sensor_zn', 'solar_az', 'solar_zn', 'slope', 'aspect', 'phase', 'cosine_i']
suffix_labels = ["envi", "anc"] # Define suffixes for different types of files
# Loop through each ancillary file and create a separate config file
for i, anc_file in enumerate(anc_files):
if i == 0:
suffix_label = f"_envi"
elif i == 1:
suffix_label = f"_anc"
else:
suffix_label = f"_{i}" # Fallback for unexpected 'i' values
config_dict = {}
config_dict['bad_bands'] = bad_bands
config_dict['file_type'] = file_type
config_dict["input_files"] = [main_image_file]
config_dict["anc_files"] = {
main_image_file: dict(zip(aviris_anc_names, [[anc_file, a] for a in range(len(aviris_anc_names))]))
}
# Export settings
config_dict['export'] = {}
config_dict['export']['coeffs'] = True
config_dict['export']['image'] = True
config_dict['export']['masks'] = True
config_dict['export']['subset_waves'] = []
config_dict['export']['output_dir'] = os.path.join(directory)
config_dict['export']["suffix"] = suffix_label
# Detailed settings for export options, TOPO and BRDF corrections
# These settings include parameters like the type of correction, calculation and application of masks,
# coefficients to be used, output directory, suffixes for output files, and various specific parameters
# for TOPO and BRDF correction methods.
# Input format: Nested dictionaries with specific keys and values as per the correction algorithm requirements
# Example settings include:
# - 'export': Dictionary of export settings like coefficients, image, masks, output directory, etc.
# - 'topo': Dictionary of topographic correction settings including types, masks, coefficients, etc.
# - 'brdf': Dictionary of BRDF correction settings including solar zenith type, geometric model, volume model, etc.
# Additional settings can be added as needed for specific correction algorithms and export requirements.
# TOPO Correction options
config_dict["corrections"] = ['topo','brdf']
config_dict["topo"] = {}
config_dict["topo"]['type'] = 'scs+c'
config_dict["topo"]['calc_mask'] = [["ndi", {'band_1': 850,'band_2': 660,
'min': 0.1,'max': 1.0}],
['ancillary',{'name':'slope',
'min': np.radians(5),'max':'+inf' }],
['ancillary',{'name':'cosine_i',
'min': 0.12,'max':'+inf' }],
['cloud',{'method':'zhai_2018',
'cloud':True,'shadow':True,
'T1': 0.01,'t2': 1/10,'t3': 1/4,
't4': 1/2,'T7': 9,'T8': 9}]]
config_dict["topo"]['apply_mask'] = [["ndi", {'band_1': 850,'band_2': 660,
'min': 0.1,'max': 1.0}],
['ancillary',{'name':'slope',
'min': np.radians(5),'max':'+inf' }],
['ancillary',{'name':'cosine_i',
'min': 0.12,'max':'+inf' }]]
config_dict["topo"]['c_fit_type'] = 'nnls'
# config_dict["topo"]['type'] = 'precomputed'
# config_dict["brdf"]['coeff_files'] = {}
# BRDF Correction options
config_dict["brdf"] = {}
config_dict["brdf"]['solar_zn_type'] ='scene'
config_dict["brdf"]['type'] = 'flex'
config_dict["brdf"]['grouped'] = True
config_dict["brdf"]['sample_perc'] = 0.1
config_dict["brdf"]['geometric'] = 'li_dense_r'
config_dict["brdf"]['volume'] = 'ross_thick'
config_dict["brdf"]["b/r"] = 10 #these may need updating. These constants pulled from literature.
config_dict["brdf"]["h/b"] = 2 # These may need updating. These contanstants pulled from literature.
config_dict["brdf"]['interp_kind'] = 'linear'
config_dict["brdf"]['calc_mask'] = [["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}]]
config_dict["brdf"]['apply_mask'] = [["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}]]
config_dict["brdf"]['diagnostic_plots'] = True
config_dict["brdf"]['diagnostic_waves'] = [448, 849, 1660, 2201]
# ## Flex dynamic NDVI params
config_dict["brdf"]['bin_type'] = 'dynamic'
config_dict["brdf"]['num_bins'] = 25
config_dict["brdf"]['ndvi_bin_min'] = 0.05
config_dict["brdf"]['ndvi_bin_max'] = 1.0
config_dict["brdf"]['ndvi_perc_min'] = 10
config_dict["brdf"]['ndvi_perc_max'] = 95
# Define the number of CPUs to be used (considering the number of image-ancillary pairs)
config_dict['num_cpus'] = 8
# Output path for configuration file
# Assuming you want to include the suffix in the filename:
suffix = config_dict['export']["suffix"]
output_dir = config_dict['export']['output_dir']
config_file_name = f"{suffix}.json"
config_file_path = os.path.join(output_dir, config_file_name)
config_dict["resample"] = False
config_dict["resampler"] = {}
config_dict["resampler"]['type'] = 'cubic'
config_dict["resampler"]['out_waves'] = []
config_dict["resampler"]['out_fwhm'] = []
# Remove bad bands from output waves
for wavelength in range(450,660,100):
bad=False
for start,end in config_dict['bad_bands']:
bad = ((wavelength >= start) & (wavelength <=end)) or bad
if not bad:
config_dict["resampler"]['out_waves'].append(wavelength)
# Construct the filename for the configuration JSON
config_filename = f"{main_image_name}_config_{suffix}.json"
config_file_path = os.path.join(directory, config_filename)
# Save the configuration to a JSON file
with open(config_file_path, 'w') as outfile:
json.dump(config_dict, outfile, indent=3)
print(f"Configuration saved to {config_file_path}")
pass
def generate_config_json(parent_directory):
"""
Loops through each subdirectory within the given parent directory and generates configuration files for each,
excluding certain unwanted directories like '.ipynb_checkpoints'.
Args:
- parent_directory (str): The parent directory containing multiple subdirectories for which to generate configurations.
"""
# Define a list of directories to exclude
exclude_dirs = ['.ipynb_checkpoints']
# Find all subdirectories within the parent directory, excluding the ones in `exclude_dirs`
subdirectories = [os.path.join(parent_directory, d) for d in os.listdir(parent_directory)
if os.path.isdir(os.path.join(parent_directory, d)) and d not in exclude_dirs]
# Loop through each subdirectory and generate correction configurations
for directory in subdirectories:
print(f"Generating configuration files for directory: {directory}")
generate_correction_configs_for_directory(directory)
print("Configuration files generation completed.\n")
pass
def generate_correction_configs(main_image_file, output_dir):
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
base_name = os.path.basename(main_image_file).split('.')[0]
input_dir = os.path.dirname(main_image_file)
# Define the bad bands, file type, and names for the ancillary datasets
bad_bands = [[300, 400], [1337, 1430], [1800, 1960], [2450, 2600]]
file_type = 'envi'
aviris_anc_names = ['path_length', 'sensor_az', 'sensor_zn', 'solar_az', 'solar_zn', 'phase', 'slope', 'aspect', 'cosine_i']
# Find ancillary files related to the main image file
anc_files_pattern = os.path.join(input_dir, f"{base_name}_ancillary*")
anc_files = glob.glob(anc_files_pattern)
anc_files.sort()
files_to_process = [main_image_file] + anc_files
for file_path in files_to_process:
is_ancillary = '_ancillary' in file_path
config_type = 'ancillary' if is_ancillary else ''
config_filename = f"{base_name}_{config_type}.json"
config_file_path = os.path.join(output_dir, config_filename)
config_dict = {
'bad_bands': bad_bands,
'file_type': file_type,
"input_files": [main_image_file],
"export": {
"coeffs": True,
"image": True,
"masks": True,
"subset_waves": [],
"output_dir": output_dir,
"suffix": f"_corrected_{config_type}"
},
"corrections": ['topo','brdf'],
"topo": {
'type': 'scs+c',
'calc_mask': [
["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}],
['ancillary', {'name': 'slope', 'min': np.radians(5), 'max': '+inf'}],
['ancillary', {'name': 'cosine_i', 'min': 0.12, 'max': '+inf'}],
['cloud', {'method': 'zhai_2018', 'cloud': True, 'shadow': True, 'T1': 0.01, 't2': 1/10, 't3': 1/4, 't4': 1/2, 'T7': 9, 'T8': 9}]
],
'apply_mask': [
["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}],
['ancillary', {'name': 'slope', 'min': np.radians(5), 'max': '+inf'}],
['ancillary', {'name': 'cosine_i', 'min': 0.12, 'max': '+inf'}]
],
'c_fit_type': 'nnls'
},
"brdf": {
'solar_zn_type': 'scene',
'type': 'flex',
'grouped': True,
'sample_perc': 0.1,
'geometric': 'li_dense_r',
'volume': 'ross_thick',
"b/r": 10,
"h/b": 2,
'interp_kind': 'linear',
'calc_mask': [["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}]],
'apply_mask': [["ndi", {'band_1': 850, 'band_2': 660, 'min': 0.1, 'max': 1.0}]],
'diagnostic_plots': True,
'diagnostic_waves': [448, 849, 1660, 2201],
'bin_type': 'dynamic',
'num_bins': 25,
'ndvi_bin_min': 0.05,
'ndvi_bin_max': 1.0,
'ndvi_perc_min': 10,
'ndvi_perc_max': 95
},
'num_cpus': 1,
"resample": False,
"resampler": {
'type': 'cubic',
'out_waves': [],
'out_fwhm': []
}
}
# Adjust the ancillary settings as needed for your process
if is_ancillary:
print("ancillary extras")
# Example: Populate config_dict["anc_files"] as necessary
with open(config_file_path, 'w') as outfile:
json.dump(config_dict, outfile, indent=3)
print(f"Configuration saved to {config_file_path}")
pass
def process_hdf5_with_neon2envi(image_path, site_code):
command = [
"/opt/conda/envs/macrosystems/bin/python", "neon2envi2.py",
"--output_dir", "output/",
"--site_code", site_code,
"-anc",
image_path
]
try:
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as proc:
for line in proc.stdout:
print(line, end='') # Print each line of output in real-time
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
pass
def get_spectral_data_and_wavelengths(filename, row_step, col_step):
"""
Retrieve spectral data and wavelengths from a specified file using HyTools library.
Parameters:
- filename: str, the path to the file to be read.
- row_step: int, the step size to sample rows by.
- col_step: int, the step size to sample columns by.
Returns:
- original: np.ndarray, a 2D array where each row corresponds to the spectral data from one pixel.
- wavelengths: np.ndarray, an array containing the wavelengths corresponding to each spectral band.
"""
# Initialize the HyTools object
envi = ht.HyTools()
# Read the file using the specified format
envi.read_file(filename, 'envi')
colrange = np.arange(0, envi.columns).tolist() # Adjusted to use envi.columns for dynamic range
pixel_lines = np.arange(0,envi.lines).tolist()
#pixel_lines
rowrange = sorted(random.sample(pixel_lines, envi.columns))
# Retrieve the pixels' spectral data
original = envi.get_pixels(rowrange, colrange)
#original = pd.DataFrame(envi.get_pixels(rowrange, colrange))
#original['index'] = np.arange(original.shape[0])
# Also retrieve the wavelengths
wavelengths = envi.wavelengths
return original, wavelengths
pass
def load_spectra(filenames, row_step=6, col_step=1):
results = {}
for filename in filenames:
try:
spectral_data, wavelengths = get_spectral_data_and_wavelengths(filename, row_step, col_step)
results[filename] = {"spectral_data": spectral_data, "wavelengths": wavelengths}
except TypeError:
print(f"Error processing file: {filename}")
return results
pass
def extract_overlapping_layers_to_2d_dataframe(raster_path, gpkg_path):
# Load polygons
polygons = gpd.read_file(gpkg_path)
# Initialize a list to store data
data = []
# Ensure polygons are in the same CRS as the raster
with rasterio.open(raster_path) as src:
raster_crs = src.crs
if polygons.crs != raster_crs:
polygons = polygons.to_crs(raster_crs)
raster_bounds = src.bounds
# Corrected line: Use geom to refer to each geometry in the GeoSeries
polygons['intersects'] = polygons.geometry.apply(lambda geom: geom.intersects(box(*raster_bounds)))
overlapping_polygons = polygons[polygons['intersects']].copy()
# Process each overlapping polygon
for index, polygon in overlapping_polygons.iterrows():
mask_result, _ = mask(src, [polygon.geometry], crop=True, all_touched=True)
row = {'polygon_id': index}
for layer in range(mask_result.shape[0]):
# Compute the mean of the raster values for this layer, excluding nodata values
valid_values = mask_result[layer][mask_result[layer] != src.nodata]
if valid_values.size > 0:
layer_mean = valid_values.mean()
else:
layer_mean = np.nan # Use NaN for areas with only nodata values
row[f'layer_{layer+1}'] = layer_mean
# Append the row to the data list
data.append(row)
# Create DataFrame from accumulated data
results_df = pd.DataFrame(data)
return results_df
pass
def rasterize_polygons_to_match_envi(gpkg_path, existing_raster_path, output_raster_path, attribute=None):
# Load polygons
polygons = gpd.read_file(gpkg_path)
# Read existing raster metadata
with rasterio.open(existing_raster_path) as existing_raster:
existing_meta = existing_raster.meta
existing_crs = existing_raster.crs
# Plot the existing raster
fig, axs = plt.subplots(1, 3, figsize=(21, 40))
with rasterio.open(existing_raster_path) as existing_raster:
show(existing_raster, ax=axs[0], title="Existing Raster")
# Reproject polygons if necessary and plot them
if polygons.crs != existing_crs:
polygons = polygons.to_crs(existing_crs)
polygons.plot(ax=axs[1], color='red', edgecolor='black')
axs[1].set_title("Polygons Layer")
# Rasterize polygons
rasterized_polygons = rasterize(
shapes=((geom, value) for geom, value in zip(polygons.geometry, polygons[attribute] if attribute and attribute in polygons.columns else polygons.index)),
out_shape=(existing_meta['height'], existing_meta['width']),
fill=0,
transform=existing_meta['transform'],
all_touched=True,
dtype=existing_meta['dtype']
)
# Save the rasterized polygons to a new ENVI file
with rasterio.open(output_raster_path, 'w', **existing_meta) as out_raster:
out_raster.write(rasterized_polygons, 1)
# Plot the new rasterized layer
with rasterio.open(output_raster_path) as new_raster:
show(new_raster, ax=axs[2], title="Rasterized Polygons Layer")
plt.tight_layout()
plt.show()
print(f"Rasterization complete. Output saved to {output_raster_path}")
pass
def prepare_spectral_data(spectral_data, wavelengths):
# Transpose and melt the spectral data to long format
long_df = pd.melt(pd.DataFrame(spectral_data).transpose(), var_name="band", value_name="reflectance")
# Create a DataFrame for wavelengths and assign a 'band' column based on index
waves = pd.DataFrame(wavelengths, columns=["wavelength_nm"])
waves['band'] = range(len(waves))
# Merge the spectral data with wavelengths using the 'band' column
merged_data = pd.merge(long_df, waves, on='band')
# Convert 'wavelength_nm' to numeric, if necessary
merged_data["wavelength_nm"] = pd.to_numeric(merged_data["wavelength_nm"])
return merged_data
pass
def reshape_spectra(results, index):
keys = list(results.keys())
if index < 0 or index >= len(keys):
print("Index out of range")
return None
first_key = keys[index]
spectral_data = results[first_key]['spectral_data']
wavelengths = results[first_key]['wavelengths']
if index < 4:
df_spectral_data = pd.DataFrame(spectral_data, columns=wavelengths.astype(str))
long_df = pd.melt(df_spectral_data, var_name="wavelength_nm", value_name="reflectance")
waves = pd.DataFrame(wavelengths, columns=["wavelength_nm"])
waves['band'] = range(len(waves))
long_df["wavelength_nm"] = pd.to_numeric(long_df["wavelength_nm"])
merged_data = pd.merge(long_df, waves, on='wavelength_nm')
# Add a label column to the merged data
first_key = keys[index].replace("export/resample_", "").replace(".img", "") # Modified here to remove "export/"
merged_data['sensor'] = first_key
merged_data = merged_data.reindex(columns=['sensor', 'band', 'wavelength_nm', 'reflectance','pixel'])
length = len(merged_data)
sequence = np.arange(0, 1071) # Creates an array [1, 2, ..., 999]
repeated_sequence = np.resize(sequence, length) # Resize the sequence to match the DataFrame's length
merged_data['pixel'] = repeated_sequence # Add the column to your DataFrame
merged_data['sensor_band'] = merged_data['sensor'].astype(str) + '_' + merged_data['band'].astype(str)
return merged_data
else:
merged_data = prepare_spectral_data(spectral_data, wavelengths)
# Add a label column to the merged data
first_key = keys[index].replace("export/ENVI__corrected_0", "hyperspectral_corrected")
first_key = first_key.replace("output/ENVI", "hyperspectral_original")
merged_data['sensor'] = first_key
merged_data = merged_data.reindex(columns=['sensor', 'band', 'wavelength_nm', 'reflectance','pixel'])
length = len(merged_data)
sequence = np.arange(0, 1071) # Creates an array [1, 2, ..., 999]
repeated_sequence = np.resize(sequence, length) # Resize the sequence to match the DataFrame's length
merged_data['pixel'] = repeated_sequence # Add the column to your DataFrame
merged_data['sensor_band'] = merged_data['sensor'].astype(str) + '_' + merged_data['band'].astype(str)
return merged_data
pass
def concatenate_sensors(reshape_spectra_function, spectra, sensors_range):
all_spectra = []
for sensor in sensors_range: # Typically range(6) for sensors 0 to 5
reshaped_spectra = reshape_spectra_function(spectra, sensor)
all_spectra.append(reshaped_spectra)
# Concatenate all reshaped spectra DataFrames into one, preserving columns
concatenated_spectra = pd.concat(all_spectra, ignore_index=True)
return concatenated_spectra
pass
def plot_spectral_data(df, highlight_pixel):
df = df[df['wavelength_nm'] > 0] # Exclude negative wavelength_nm values
df['reflectance'] = df['reflectance'].replace(-9999, np.nan)
unique_indices = df['pixel'].unique()
for idx in unique_indices:
subset = df[df['pixel'] == idx]
plt.plot(subset['wavelength_nm'], subset['reflectance'], alpha=0.05, color="blue")
# Highlight a specific pixel
highlighted_subset = df[df['pixel'] == highlight_pixel]
# Inside the plot_spectral_data function, after highlighting the specific pixel
if (highlighted_subset['reflectance'] == -9999).all() or highlighted_subset['reflectance'].isna().all():
print(f"Warning: Pixel {highlight_pixel} data is entirely -9999 or NaN.")
plt.plot(highlighted_subset['wavelength_nm'], highlighted_subset['reflectance'], color='red', linewidth=10, label=f'Pixel {highlight_pixel}')
plt.xlabel('Wavelength (nm)')
plt.ylabel('Reflectance')
plt.legend()
plt.tight_layout()
plt.ylim(0, 10000)
plt.show()
pass
def plot_each_sensor_with_highlight(concatenated_sensors, highlight_pixel, save_path=None):
sensors = concatenated_sensors['sensor'].unique()
fig, axs = plt.subplots(len(sensors), 1, figsize=(10, 5 * len(sensors)))
for i, sensor in enumerate(sensors):
df = concatenated_sensors[concatenated_sensors['sensor'] == sensor]
pixels = df['pixel'].unique()
for pixel in pixels:
subset = df[df['pixel'] == pixel]
axs[i].plot(subset['wavelength_nm'], subset['reflectance'], alpha=0.05, color="blue")
highlighted_subset = df[df['pixel'] == highlight_pixel]
if not highlighted_subset.empty and not highlighted_subset['reflectance'].isna().all():
axs[i].plot(highlighted_subset['wavelength_nm'], highlighted_subset['reflectance'], color='red', linewidth=2, label=f'Pixel {highlight_pixel}')
axs[i].set_title(sensor)
axs[i].set_xlabel('Wavelength (nm)')
axs[i].set_ylabel('Reflectance')
axs[i].set_ylim(0, 10000)
axs[i].set_xlim(350, 2550)
axs[i].legend()
plt.tight_layout()
if save_path:
plt.savefig(save_path, format=save_path.split('.')[-1])
plt.show()
pass
def plot_with_highlighted_sensors(concatenated_sensors, highlight_pixels, save_path=None):
plt.figure(figsize=(10, 5))
# Ensure highlight_pixels is a list for iteration
if not isinstance(highlight_pixels, list):
highlight_pixels = [highlight_pixels]
# Apply initial data cleaning
concatenated_sensors = concatenated_sensors[concatenated_sensors['wavelength_nm'] > 0]
concatenated_sensors['reflectance'] = concatenated_sensors['reflectance'].replace(-9999, np.nan)
# Plotting hyperspectral corrected data in blue
hyperspectral_corrected = concatenated_sensors[concatenated_sensors['sensor'] == 'hyperspectral_corrected']
for pixel in hyperspectral_corrected['pixel'].unique():
subset = hyperspectral_corrected[hyperspectral_corrected['pixel'] == pixel]
plt.plot(subset['wavelength_nm'], subset['reflectance'], alpha=0.05, color="blue")
# Overlaying highlighted lines from each sensor in red
for sensor in concatenated_sensors['sensor'].unique():
if sensor != 'hyperspectral_corrected': # Exclude hyperspectral corrected data from red lines
for highlight_pixel in highlight_pixels:
highlighted_subset = concatenated_sensors[(concatenated_sensors['pixel'] == highlight_pixel) & (concatenated_sensors['sensor'] == sensor)]
if not highlighted_subset.empty:
plt.plot(highlighted_subset['wavelength_nm'], highlighted_subset['reflectance'], color='red', linewidth=2, label=sensor)
plt.xlabel('Wavelength (nm)')
plt.ylabel('Reflectance')
plt.ylim(0, None) # Adjusted to auto-scale based on data
plt.xlim(350, 2550)
plt.legend()
plt.tight_layout()
if save_path:
plt.savefig(save_path, format=save_path.split('.')[-1])
plt.show()
pass
def fit_models_with_different_alpha(data, n_levels=100):
data['reflectance'] = data['reflectance'].replace(np.nan, 0)
X = data[['wavelength_nm']]
y = data['reflectance']
# Store models and predictions
models = []
alphas = np.linspace(0.01, 0.99, n_levels)
for alpha in alphas:
model = GradientBoostingRegressor(n_estimators=500, max_depth=15, learning_rate=0.09,
subsample=0.1, loss='quantile', alpha=alpha)
model.fit(X, y)
models.append(model)
# You can also store predictions if needed
data[f'{alpha:.2f}'] = model.predict(X)
return models, data
pass
def plot_with_highlighted_sensors(concatenated_sensors, highlight_pixels, save_path=None):
plt.figure(figsize=(10, 5))
# Ensure highlight_pixels is a list for iteration
if not isinstance(highlight_pixels, list):
highlight_pixels = [highlight_pixels]
# Apply initial data cleaning
concatenated_sensors = concatenated_sensors[concatenated_sensors['wavelength_nm'] > 0]
#concatenated_sensors['boosted_predictions_01'] = concatenated_sensors['boosted_predictions_01'].replace(-9999, np.nan)
concatenated_sensors['reflectance'] = concatenated_sensors['reflectance'].replace(-9999, np.nan)
# Plotting hyperspectral corrected data in blue
hyperspectral_corrected = concatenated_sensors[concatenated_sensors['sensor'] == 'hyperspectral_corrected']
for pixel in hyperspectral_corrected['pixel'].unique():
subset = hyperspectral_corrected[hyperspectral_corrected['pixel'] == pixel]
plt.plot(subset['wavelength_nm'], subset['reflectance'], alpha=0.05, color="blue")
# Overlaying highlighted lines from each sensor in red
for sensor in concatenated_sensors['sensor'].unique():
if sensor != 'hyperspectral_corrected': # Exclude hyperspectral corrected data from red lines
for highlight_pixel in highlight_pixels:
highlighted_subset = concatenated_sensors[(concatenated_sensors['pixel'] == highlight_pixel) & (concatenated_sensors['sensor'] == sensor)]
if not highlighted_subset.empty:
plt.plot(highlighted_subset['wavelength_nm'], highlighted_subset['0.50'], linewidth=10, label=sensor)
plt.xlabel('Wavelength (nm)')
plt.ylabel('Reflectance')
plt.ylim(0, 10000) # Adjusted to auto-scale based on data
plt.xlim(350, 2550)
plt.legend()
plt.tight_layout()
if save_path:
plt.savefig(save_path, format=save_path.split('.')[-1])
plt.show()
pass
def boosted_quantile_plot(data, num_lines=10, title='Hyperspectral Corrected Predictions by Alpha', save_path=None):
plt.figure(figsize=(10, 6))
total_alphas = 100
step = total_alphas // num_lines
# Plotting hyperspectral corrected data in blue
hyperspectral_corrected = concatenated_sensors[concatenated_sensors['sensor'] == 'hyperspectral_corrected']
for pixel in hyperspectral_corrected['pixel'].unique():
subset = hyperspectral_corrected[hyperspectral_corrected['pixel'] == pixel]
plt.plot(subset['wavelength_nm'], subset['reflectance'], alpha=0.04, color="blue", linewidth=0.3)
alpha_values = np.linspace(0.01, 0.99, total_alphas)[::step]
for alpha_value in alpha_values:
alpha_col = f'{alpha_value:.2f}'
adjusted_alpha = 1 - alpha_value if alpha_value > 0.5 else alpha_value
plt.plot(data['wavelength_nm'], data[alpha_col], label=f'Probability {alpha_col}', alpha=adjusted_alpha ,color="red", linewidth=adjusted_alpha*6)