-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021-10-26 read_sed_spectra_file.py
86 lines (74 loc) · 3.44 KB
/
2021-10-26 read_sed_spectra_file.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
# This code is used to deal with the spectral files generated by 'SR-3500 Spectroradiometer/SM-3500 Spectrometer'.
# The spectral file has a suffix of .sed.
#
# To use this code, just give it a root directory, and it will check all the directories contained. If there are .sed
# files in one directory, the code will extract the reflectance data, and generate a .csv file as output in that
# directory.
#
# Because I stored the spectral files of different dates in different folders, I want to deal with them separately in
# each folder. That is why I make a .csv file for each folder.
import os
import numpy as np
import csv
# change the target_directory to the directory that you want
target_directory = r'D:'
# read sed files and put them into a list
def read_sed_spectra_file(filepath):
import numpy as np
with open(filepath) as f:
content_text = f.read()
this_text = putting_a_text_into_a_list(content_text)
# print(this_text)
# now the variable this_text contains all the information splitted.
# this_text[24] is the first line of numeric data, this_text[1047] is the last.
# transfer spectra information to numeric
data_array = np.zeros((1024,4))
for i in range(24, 1048):
for j in range(0,4):
data_array[i-24,j] = float(this_text[i][j])
reflectance = data_array[:,3].transpose().tolist()
return reflectance
# return reflectance(%)
# split the text containing '\t' and '\n'.
# When meet a '\t', set a new element in the list. When meet a '\n', set a new list.
def putting_a_text_into_a_list(text):
start_flag = 0
end_flag = 0
this_text = []
this_line = []
this_word = []
for i in range(0, len(text)):
if text[i] == '\t':
end_flag = i
this_word = text[start_flag: end_flag] # not contain text[end_flag]
this_line.append(this_word)
start_flag = i + 1
if text[i] == '\n':
end_flag = i
this_word = text[start_flag: end_flag] # not contain text[end_flag]
this_line.append(this_word)
this_text.append(this_line)
this_line = []
start_flag = i + 1
end_flag = end_flag + 1
return this_text
for dirpath_dirnames_filenames in os.walk(target_directory):
# go through all the folders
exist_sed_file = False
data_for_this_folder = []
for file_name in dirpath_dirnames_filenames[2]:
# go through all the files and look for .sed files
if os.path.splitext(file_name)[1] == '.sed':
# read_sed_spectra_file() and combine the data
try:
data_for_one_file = read_sed_spectra_file(filepath=os.path.join(dirpath_dirnames_filenames[0],file_name))
except:
pass
else:
exist_sed_file = True
data_for_this_folder.append([file_name] + data_for_one_file)
# generate “Collection_of_sed_spectra_data_in_this_folder.csv” if there is no such file in the directory
if (exist_sed_file == True) and (not "Collection_of_sed_spectra_data_in_this_folder.csv" in dirpath_dirnames_filenames[2]):
csv_filename = os.path.join(dirpath_dirnames_filenames[0], "Collection_of_sed_spectra_data_in_this_folder.csv")
with open(csv_filename, 'w', newline='') as csvfile:
csv.writer(csvfile, delimiter=',').writerows(data_for_this_folder)