Skip to content

Commit 65cbed2

Browse files
committed
More delicious refactoring
1 parent 5f9d955 commit 65cbed2

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

pyspecchio/spectra_parser.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,39 @@
1616
import unittest
1717

1818
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
2522

26-
def read_json(self, path, spectra_file):
23+
def read_json(self):
2724
"""Simple JSON reader"""
28-
with open(path + spectra_file, "r") as f:
25+
with open(self.spath + self.sfile, "r") as f:
2926
data = json.load(f)
3027
return data
3128

32-
def pandas_read_json(self, path, spectra_file):
29+
def pandas_read_json(self):
3330
"""pandas read json to dataframe"""
34-
with open(path + spectra_file, "r") as f:
31+
with open(self.spath + self.sfile, "r") as f:
3532
data = json.load(f)
3633
dataframe = pd.DataFrame(data)
3734
return dataframe
3835

39-
def pandas_read_json_spectra(self, path, spectra_file):
36+
def pandas_read_json_spectra(self):
4037
"""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:
4239
data = json.load(f)
4340
result = json_normalize(data["Spectra"])
4441
return result
4542

46-
def pandas_read_json_str(self, path, spectra_file):
43+
def pandas_read_json_str(self):
4744
"""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:
4946
data = json.load(f)
5047
return data
5148

52-
def get_only_spectra_pixels(self, path, spectra_file):
49+
def get_only_spectra_pixels(self):
5350
"""Get just the spectra pixels"""
54-
with open(path + spectra_file, "r") as f:
51+
with open(self.spath + self.sfile, "r") as f:
5552
data = json.load(f)
5653
result = json_normalize(data["Spectra"])
5754
return result["Pixels"]
@@ -80,7 +77,7 @@ def get_spectra_metadata(self, spectra_number):
8077
# Metadata [0] and Pixels [1]
8178
return whole_file['Spectra'][spectra_number]['Metadata']
8279

83-
def get_spectra_pixels(self, spectra_number, path, filename):
80+
def get_spectra_pixels(self, spectra_number):
8481
"""Returns the dict represtning the metadata from the spectra file.
8582
for ONE of the up/down pairs.
8683
@@ -94,7 +91,7 @@ def get_spectra_pixels(self, spectra_number, path, filename):
9491
So to get the first upwelling spetra, 0 is used."""
9592
if not self.valid_spectra(spectra_number):
9693
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()
9895
# File format has spectra which contains a list of dicts:
9996
# Metadata [0] and Pixels [1]
10097
return whole_file['Spectra'][spectra_number]['Pixels']
@@ -119,17 +116,19 @@ def test_invalid_spectra(self):
119116

120117
if __name__ == "__main__":
121118
unittest.main()
122-
123-
sf = SpectraFile()
124-
119+
125120
path = '/home/centos/Downloads/'
126121
filename = 'spectra.csv'
127122

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()
133132
metadata_upwell_zero = sf.get_spectra_metadata(0)
134133

135134

0 commit comments

Comments
 (0)