-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
72 lines (58 loc) · 2.41 KB
/
data.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
import pandas as pd
import gsw
DATES_COLUMNS = ['Date', 'Time']
class Data:
def __init__(self, path):
self._read(path)
self._clean()
def _read(self, path):
self.data = pd.read_csv(path, sep='\s+', parse_dates=DATES_COLUMNS)
for column in self.data.columns:
if column not in DATES_COLUMNS:
self.data[column].astype(float)
def _clean(self):
for_remove = []
cur = 0
for i in range(len(self.data)):
if self.data.iloc[cur]['Pres'] > self.data.iloc[i]['Pres']:
for_remove.append(self.data.index[i])
continue
else:
removed = False
for column in self.data.columns:
if column not in DATES_COLUMNS:
if self.data.iloc[i][column] < 1e-3:
for_remove.append(self.data.index[i])
removed = True
break
if not removed:
cur = i
self.data = self.data.drop(for_remove)
def add_columns(self, columns):
for column_name in columns:
self.data[column_name] = columns[column_name]
self.data[column_name].astype(float)
def calc_teos10_columns(self, lat, lng):
# Practical Salinity
SP = gsw.SP_from_C(self.data['Cond'], self.data['Temp'], self.data['Pres'])
# Absolute Salinity
SA = gsw.SA_from_SP_Baltic(SP, lng, lat)
# Conservative Temperature
CT = gsw.CT_from_t(SA, self.data['Temp'], self.data['Pres'])
# Sigma(density) with reference pressure of 0 dbar
sigma = gsw.sigma0(SA, CT)
# Depth
depth = list(map(abs, gsw.z_from_p(self.data['Pres'], lat)))
return {'PracticalSalinity' : SP,
'AbsoluteSalinity' : SA,
'ConservativeTemperature' : CT,
'Sigma(density)' : sigma,
'Depth' : depth}
def save_to_file(self, path):
with open(path, 'w') as f:
f.write(self._get_csv_string())
def _get_csv_string(self):
new_data = self.data.copy()
new_data['Date'] = new_data['Date'].apply(lambda x: x.strftime('%d-%m-%Y'))
new_data['Time'] = new_data['Time'].apply(lambda x: x.strftime('%H:%M:%S.%f')[:11])
return new_data.to_csv(sep=',', encoding='utf-8', index=False, float_format='%.3f')