16
16
import unittest
17
17
18
18
class SpectraFile (object ):
19
- SPECTRAFILE = "None"
20
- SPECTRA_PATH = "None"
21
-
22
- def set_spectra_file (self , spectra_filename , spectra_filepath ):
23
- self .SPECTRA_PATH = spectra_filepath
24
- self .SPECTRAFILE = spectra_filename
19
+ def __init__ (self , spectrafile , spectrapath ):
20
+ self .sfile = spectrafile
21
+ self .spath = spectrapath
25
22
26
- def read_json (self , path , spectra_file ):
23
+ def read_json (self ):
27
24
"""Simple JSON reader"""
28
- with open (path + spectra_file , "r" ) as f :
25
+ with open (self . spath + self . sfile , "r" ) as f :
29
26
data = json .load (f )
30
27
return data
31
28
32
- def pandas_read_json (self , path , spectra_file ):
29
+ def pandas_read_json (self ):
33
30
"""pandas read json to dataframe"""
34
- with open (path + spectra_file , "r" ) as f :
31
+ with open (self . spath + self . sfile , "r" ) as f :
35
32
data = json .load (f )
36
33
dataframe = pd .DataFrame (data )
37
34
return dataframe
38
35
39
- def pandas_read_json_spectra (self , path , spectra_file ):
36
+ def pandas_read_json_spectra (self ):
40
37
"""Normalise the results to get the spectra"""
41
- with open (path + spectra_file , "r" ) as f :
38
+ with open (self . spath + self . sfile , "r" ) as f :
42
39
data = json .load (f )
43
40
result = json_normalize (data ["Spectra" ])
44
41
return result
45
42
46
- def pandas_read_json_str (self , path , spectra_file ):
43
+ def pandas_read_json_str (self ):
47
44
"""Normalise the results to get the spectra"""
48
- with open (path + spectra_file , "r" ) as f :
45
+ with open (self . spath + self . sfile , "r" ) as f :
49
46
data = json .load (f )
50
47
return data
51
48
52
- def get_only_spectra_pixels (self , path , spectra_file ):
49
+ def get_only_spectra_pixels (self ):
53
50
"""Get just the spectra pixels"""
54
- with open (path + spectra_file , "r" ) as f :
51
+ with open (self . spath + self . sfile , "r" ) as f :
55
52
data = json .load (f )
56
53
result = json_normalize (data ["Spectra" ])
57
54
return result ["Pixels" ]
@@ -80,7 +77,7 @@ def get_spectra_metadata(self, spectra_number):
80
77
# Metadata [0] and Pixels [1]
81
78
return whole_file ['Spectra' ][spectra_number ]['Metadata' ]
82
79
83
- def get_spectra_pixels (self , spectra_number , path , filename ):
80
+ def get_spectra_pixels (self , spectra_number ):
84
81
"""Returns the dict represtning the metadata from the spectra file.
85
82
for ONE of the up/down pairs.
86
83
@@ -94,7 +91,7 @@ def get_spectra_pixels(self, spectra_number, path, filename):
94
91
So to get the first upwelling spetra, 0 is used."""
95
92
if not self .valid_spectra (spectra_number ):
96
93
raise IndexError ("Not a valid spectra number for this spectrometer: [0-3]" )
97
- whole_file = self .read_json (path , filename )
94
+ whole_file = self .read_json ()
98
95
# File format has spectra which contains a list of dicts:
99
96
# Metadata [0] and Pixels [1]
100
97
return whole_file ['Spectra' ][spectra_number ]['Pixels' ]
@@ -119,17 +116,19 @@ def test_invalid_spectra(self):
119
116
120
117
if __name__ == "__main__" :
121
118
unittest .main ()
122
-
123
- sf = SpectraFile ()
124
-
119
+
125
120
path = '/home/centos/Downloads/'
126
121
filename = 'spectra.csv'
127
122
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 )
123
+ # Create a spectra file object
124
+ sf = SpectraFile (filename , path )
125
+
126
+ # Test the parse methods
127
+ data1 = sf .read_json ()
128
+ df = sf .pandas_read_json ()
129
+ df1 = sf .pandas_read_json_str ()
130
+ df2 = sf .pandas_read_json_spectra () # This is getting all four spectra pairs (UP/DOWN)
131
+ pixels = sf .get_only_spectra_pixels ()
133
132
metadata_upwell_zero = sf .get_spectra_metadata (0 )
134
133
135
134
0 commit comments