-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathokokyst_tools.py
More file actions
146 lines (116 loc) · 3.9 KB
/
okokyst_tools.py
File metadata and controls
146 lines (116 loc) · 3.9 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import datetime
import numpy as np
import gsw
# Local files
from station.station_class import Station
import ctdConfig as CTDConfig
import okokyst_metadata
__author__ = 'Trond Kristiansen'
__email__ = 'trond.kristiansen@niva.no'
__created__ = datetime.datetime(2017, 2, 24)
__modified__ = datetime.datetime(2019, 1, 8)
__version__ = "1.0"
__status__ = "Development"
def createNewFile(filename, station, CTDConfig):
infile = open(filename, 'r')
lines = infile.readlines()
l = lines[0].split(";")
options = None
if l[2] == "Cond." and len(l)==13:
CTDConfig.conductivity_to_salinity = True
CTDConfig.conductivityMissing = False
elif l[2] == "Sal." and 'Cond.' not in l:
CTDConfig.conductivity_to_salinity=False
CTDConfig.conductivityMissing = True
elif l[2] == "Sal." and 'Cond.' in l:
CTDConfig.conductivity_to_salinity=False
CTDConfig.conductivityMissing = False
if ('S. vel.' in l and 'Cond.' in l):
CTDConfig.conductivityMissing = False
CTDConfig.conductivity_to_salinity = False
l[-4] = l[-3]
l[-3] = l[-2]
l[-2] = l[-1]
options = ['S. vel.']
if l[8] == "Press":
CTDConfig.calculate_depth_from_pressure=True
okokyst_metadata.addStationMeadata(station, CTDConfig, options)
# Create a new file by adding a header to the existing CTD file.
newfilename = filename[0:-4] + "_edited.txt"
if os.path.exists(newfilename):
os.remove(newfilename)
if CTDConfig.debug:
print("=> Creating new output file: %s" % newfilename)
outfile = open(newfilename, 'a')
outfile.writelines(station.header + station.mainHeader)
counter = 0
for line in lines[1:]:
l = line.split(";")
# Calculate salinity from conductivty and replace with conductivity in data array
if CTDConfig.conductivity_to_salinity and counter > 0:
C = float(l[2])
T = float(l[3])
P = float(l[8])
salinity = gsw.SP_from_C(C, T, P)
l[2] = salinity
# If only pressure is given, convert to depth in meters
if CTDConfig.calculate_depth_from_pressure:
P = float(l[8])
l[8]=pressure_to_depth(P,station.latitude)
line = ';'.join(map(str, l))
if counter > 0:
outfile.writelines(line)
counter += 1
outfile.close()
infile.close()
return newfilename
def pressure_to_depth(P, lat):
"""Compute depth from pressure and latitude
Usage: depth(P, lat)
Input:
P = Pressure, [dbar]
lat = Latitude [deg]
Output:
Depth [m]
Algorithm: UNESCO 1983
"""
a1 = 9.72659
a2 = -2.2512e-5
a3 = 2.279e-10
a4 = -1.82e-15
b = 1.092e-6
g0 = 9.780318
g1 = 5.2788e-3
g2 = 2.36e-5
rad = np.pi / 180.
X = np.sin(lat*rad)
X = X*X
grav = g0 * (1.0 + (g1 + g2*X)*X) + b*P
nom = (a1 + (a2 + (a3 + a4*P)*P)*P)*P
return nom / grav
# After identifying correct folder, find the txt file
def locateFile(basepath, subStation):
filename = "%s/%s_edited.txt" % (basepath, subStation)
####print (filename)
if os.path.isfile(filename):
return filename
else:
# In cases like for 2020 where the edited files are not named equal to earlier
# years but simply had the _:edited removed. These files come from Lizas script.
filename = "%s/%s.txt" % (basepath, subStation)
if os.path.isfile(filename):
return filename
def locateDir(folder):
if os.path.isdir(folder):
return True
def findMaximumWindow(cast, tempName):
maxDepth = np.max(cast[tempName].index.values)
window = 1
if maxDepth > 400:
window = 21
if 400 > maxDepth > 100:
window = 11
if 50 > maxDepth > 0:
window = 5
return 1 #window #window