6
6
This file takes the .pico spectrometer files and parses them to a numpy
7
7
or pandas
8
8
9
- @author: centos
9
+ @author: Declan Valters
10
10
"""
11
11
12
12
import json
15
15
16
16
import unittest
17
17
18
- path = "/home/centos/Downloads/DATA/Spectra_dir/"
19
- spectra_file_test = "QEP1USB1_b000000_s000002_light.pico"
20
-
21
- def read_json ():
22
- """Simple JSON reader"""
23
- with open (path + spectra_file_test , "r" ) as f :
24
- data = json .load (f )
25
- return data
26
-
27
- def pandas_read_json ():
28
- """pandas read json to dataframe"""
29
- with open (path + spectra_file_test , "r" ) as f :
30
- data = json .load (f )
31
- dataframe = pd .DataFrame (data )
32
- return dataframe
33
-
34
- def pandas_read_json_spectra ():
35
- """Normalise the results to get the spectra"""
36
- with open (path + spectra_file_test , "r" ) as f :
37
- data = json .load (f )
38
- result = json_normalize (data ["Spectra" ])
39
- return result
40
-
41
- def pandas_read_json_str ():
42
- """Normalise the results to get the spectra"""
43
- with open (path + spectra_file_test , "r" ) as f :
44
- data = json .load (f )
45
- return data
46
-
47
- def get_only_spectra_pixels ():
48
- """Get just the spectra pixels"""
49
- with open (path + spectra_file_test , "r" ) as f :
50
- data = json .load (f )
51
- result = json_normalize (data ["Spectra" ])
52
- return result ["Pixels" ]
53
-
54
- def valid_spectra (spectra_num ):
55
- """Checks the index provided is a valid range for the spectrometer data
56
- """
57
- return spectra_num in range (0 ,4 )
58
-
59
- def get_spectra_metadata (spectra_number ):
60
- """Returns the dict represtning the metadata from the spectra file.
61
- for ONE of the up/down pairs.
18
+ class SpectraFile (object ):
19
+ SPECTRAFILE = "None"
20
+ SPECTRA_PATH = "None"
62
21
63
- Note the file layout is like this:
64
-
65
- Upwelling Spectra [0]
66
- Upwelling Spectra [1]
67
- Downwelling Spectra [2]
68
- Downwelling Spectra [3]
22
+ def set_spectra_file (self , spectra_filename , spectra_filepath ):
23
+ self .SPECTRA_PATH = spectra_filepath
24
+ self .SPECTRAFILE = spectra_filename
25
+
26
+ def read_json (self , path , spectra_file ):
27
+ """Simple JSON reader"""
28
+ with open (path + spectra_file , "r" ) as f :
29
+ data = json .load (f )
30
+ return data
69
31
70
- So to get the first upwelling spetra, 0 is used."""
71
- if not valid_spectra (spectra_number ):
72
- raise IndexError ("Not a valid spectra number for this spectrometer: [0-3]" )
73
- whole_file = read_json ()
74
- # File format has spectra which contains a list of dicts:
75
- # Metadata [0] and Pixels [1]
76
- return whole_file ['Spectra' ][spectra_number ]['Metadata' ]
77
-
78
- def get_spectra_pixels (spectra_number ):
79
- """Returns the dict represtning the metadata from the spectra file.
80
- for ONE of the up/down pairs.
32
+ def pandas_read_json (self , path , spectra_file ):
33
+ """pandas read json to dataframe"""
34
+ with open (path + spectra_file , "r" ) as f :
35
+ data = json .load (f )
36
+ dataframe = pd .DataFrame (data )
37
+ return dataframe
81
38
82
- Note the file layout is like this:
39
+ def pandas_read_json_spectra (self , path , spectra_file ):
40
+ """Normalise the results to get the spectra"""
41
+ with open (path + spectra_file , "r" ) as f :
42
+ data = json .load (f )
43
+ result = json_normalize (data ["Spectra" ])
44
+ return result
45
+
46
+ def pandas_read_json_str (self , path , spectra_file ):
47
+ """Normalise the results to get the spectra"""
48
+ with open (path + spectra_file , "r" ) as f :
49
+ data = json .load (f )
50
+ return data
51
+
52
+ def get_only_spectra_pixels (self , path , spectra_file ):
53
+ """Get just the spectra pixels"""
54
+ with open (path + spectra_file , "r" ) as f :
55
+ data = json .load (f )
56
+ result = json_normalize (data ["Spectra" ])
57
+ return result ["Pixels" ]
58
+
59
+ def valid_spectra (self , spectra_num ):
60
+ """Checks the index provided is a valid range for the spectrometer data
61
+ """
62
+ return spectra_num in range (0 ,4 )
63
+
64
+ def get_spectra_metadata (self , spectra_number ):
65
+ """Returns the dict represtning the metadata from the spectra file.
66
+ for ONE of the up/down pairs.
83
67
84
- Upwelling Spectra [0] len = 1044
85
- Upwelling Spectra [1] len = 2048
86
- Downwelling Spectra [2] len = 1044
87
- Downwelling Spectra [3] len = 2048
68
+ Note the file layout is like this:
69
+
70
+ Upwelling Spectra [0]
71
+ Upwelling Spectra [1]
72
+ Downwelling Spectra [2]
73
+ Downwelling Spectra [3]
74
+
75
+ So to get the first upwelling spetra, 0 is used."""
76
+ if not self .valid_spectra (spectra_number ):
77
+ raise IndexError ("Not a valid spectra number for this spectrometer: [0-3]" )
78
+ whole_file = self .read_json ()
79
+ # File format has spectra which contains a list of dicts:
80
+ # Metadata [0] and Pixels [1]
81
+ return whole_file ['Spectra' ][spectra_number ]['Metadata' ]
88
82
89
- So to get the first upwelling spetra, 0 is used."""
90
- if not valid_spectra (spectra_number ):
91
- raise IndexError ("Not a valid spectra number for this spectrometer: [0-3]" )
92
- whole_file = read_json ()
93
- # File format has spectra which contains a list of dicts:
94
- # Metadata [0] and Pixels [1]
95
- return whole_file ['Spectra' ][spectra_number ]['Pixels' ]
83
+ def get_spectra_pixels (self , spectra_number , path , filename ):
84
+ """Returns the dict represtning the metadata from the spectra file.
85
+ for ONE of the up/down pairs.
86
+
87
+ Note the file layout is like this:
88
+
89
+ Upwelling Spectra [0] len = 1044
90
+ Upwelling Spectra [1] len = 2048
91
+ Downwelling Spectra [2] len = 1044
92
+ Downwelling Spectra [3] len = 2048
93
+
94
+ So to get the first upwelling spetra, 0 is used."""
95
+ if not self .valid_spectra (spectra_number ):
96
+ raise IndexError ("Not a valid spectra number for this spectrometer: [0-3]" )
97
+ whole_file = self .read_json (path , filename )
98
+ # File format has spectra which contains a list of dicts:
99
+ # Metadata [0] and Pixels [1]
100
+ return whole_file ['Spectra' ][spectra_number ]['Pixels' ]
96
101
97
102
98
103
class spectra_metadata ():
@@ -103,21 +108,28 @@ def __init__(self):
103
108
class TestSpectraParser (unittest .TestCase ):
104
109
105
110
def test_valid_sepctra (self ):
111
+ sf = SpectraFile ()
106
112
for x in range (0 ,4 ):
107
- self .assertTrue (valid_spectra (x ))
113
+ self .assertTrue (sf . valid_spectra (x ))
108
114
109
115
def test_invalid_spectra (self ):
116
+ sf = SpectraFile ()
110
117
for item in [1.5 , "foobar" , 7 , - 2 ]:
111
- self .assertFalse (valid_spectra (item ))
118
+ self .assertFalse (sf . valid_spectra (item ))
112
119
113
120
if __name__ == "__main__" :
114
121
unittest .main ()
115
122
116
- data1 = read_json ()
117
- df = pandas_read_json ()
118
- df1 = pandas_read_json_str ()
119
- df2 = pandas_read_json_spectra () # This is getting all four spectra pairs (UP/DOWN)
120
- pixels = get_only_spectra_pixels ()
121
- metadata_upwell_zero = get_spectra_metadata (0 )
123
+ sf = SpectraFile ()
124
+
125
+ path = '/home/centos/Downloads/'
126
+ filename = 'spectra.csv'
127
+
128
+ data1 = sf .read_json (path , filename )
129
+ df = sf .pandas_read_json (path , filename )
130
+ df1 = sf .pandas_read_json_str (path , filename )
131
+ df2 = sf .pandas_read_json_spectra (path , filename ) # This is getting all four spectra pairs (UP/DOWN)
132
+ pixels = sf .get_only_spectra_pixels (path , filename )
133
+ metadata_upwell_zero = sf .get_spectra_metadata (0 )
122
134
123
135
0 commit comments