-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime-example-1.py
More file actions
190 lines (130 loc) · 5.01 KB
/
datetime-example-1.py
File metadata and controls
190 lines (130 loc) · 5.01 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 02, 2015
Examples of datetime and pytz
usage, conversions
@author:
Jennifer Patterson
CeNCOOS
jpatterson@mbari.org
Example input time
231,3:03:17 PM
yearday, time in hh:mm:ss UTC, 12 hour clock
"""
from pylab import date2num
import dateutil
from pytz import timezone
import pytz
from datetime import datetime, timedelta
import os, errno
def findAge(timeUTC):
# find age from pytz date in utc
timeNowUTC = datetime.utcnow().replace(tzinfo = pytz.utc)
diffnowUTC = timeNowUTC - timeUTC
return(diffnowUTC)
utc = pytz.utc
pacific = timezone('US/Pacific')
timeNowUTC = datetime.utcnow().replace(tzinfo = utc)
thisYear = timeNowUTC.year
ldir ='/datetime-example-1-sensorFile.txt'
print "\nChecking OS Dates on Local Files"
lFiles = []
for filenames in os.walk(ldir):
for fn in filenames[2]: lFiles.append(fn)
sortedFiles = sorted(lFiles)
files2open = []
for f in sortedFiles: # parse file os timestamp and make utc date, not timestamp in file text
MM = f[0:2]
DD = f[3:5]
YYYY = f[6:10]
hh = f[11:13]
mm = f[14:16]
ss = f[17:19]
# look at files now minus 4 days for backfilling in case of collection/server issues
ftime = MM+'/'+DD+'/'+YYYY+' '+hh+':'+mm+':00'
pacific = timezone('US/Pacific')
fdatetime = datetime(int(YYYY), int(MM), int(DD), int(hh), int(mm), int(ss))
print 'fdatetime = ',fdatetime
fpytzUTC = utc.localize(fdatetime)
print 'fpytz =',fpytzUTC
a = findAge(fpytzUTC)
if a.days < 4 and fpytzUTC.year == thisYear:
files2open.append(f)
print 'file age in days = ',a.days
# file will be older than 4 days but for the sake of the example
# lets add it to the list anyway so we can continue
files2open.append(f)
lfn = 'datetime-example-1-lastOutputFile.csv'
# open last csv file
# check for new timestamps
# csv file timestamps are local (pacific)
lf = open(lfn,'r')
lfc = lf.readlines()
for r in lfc:
rc = r.split(',')
lastdate = rc[2]
lf.close()
print '\nReading CSV file Date =',lastdate
ftime = dateutil.parser.parse(lastdate)
print ftime
'''
if this was a utc timestamp, add that info here
lastpytzUTC = utc.localize(ftime)
print lastpytzUTC
'''
# But this reads a file with a local time stamp, so applying pacific time
lastpytzL = pacific.localize(ftime)
print lastpytzL
ffn = ldir +'/' + fn
profilerfile = open(ffn,'r')
s = profilerfile.readlines()
theseTimes = []
count = 0
# --- Get timestamp from inside the file ---
#reading lines in data file
for line in s:
line = line.rstrip('\n')
if count >= 1: # start after the header lines
line = line.split(',')
# parse file yearday
fyd = datetime(thisYear, 1, 1) + timedelta(int(line[0]) - 1)
# parse file time
fts = dateutil.parser.parse(line[1])
# combine into one timestamp
ts = fyd.replace(year=thisYear, hour=fts.hour, minute=fts.minute, second=fts.second)
# attach time zone to the datetime obj
pytzUTC = utc.localize(ts)
# converts to pacific time
convertedtoLocal = pacific.normalize(pytzUTC.astimezone(pacific))
theseTimes.append(convertedtoLocal)
count= count +1
if lastpytzL > min(theseTimes) and lastpytzL < max(theseTimes):
# same time as last profile processed. closing file
profilerfile.close()
elif lastpytzL > min(theseTimes):
# older time than last profile processed. closing file
profilerfile.close()
else:
# new profile data, Continuing processing, checking for surface data
data = []
header = ['Parameter', 'Last value', 'Local Time']
count = 0
times = []
surface_values = []
surface = 0
for line in s:
line = line.rstrip('\n')
linesplit = [x.strip() for x in line.split(',')]
if count == 0:
parameters = linesplit[2:]
if count >= 1: # start after the header lines
if (surface == 0) and float(linesplit[2]) > 1.75: # if surface not found yet look for surface
fyd = datetime(thisYear, 1, 1) + timedelta(int(linesplit[0]) - 1) # parse file yearday
fts = dateutil.parser.parse(linesplit[1]) # parse file time stamp
ts = fyd.replace(year=thisYear, hour=fts.hour, minute=fts.minute) # combine into one timestamp
pytzUTC = utc.localize(ts)
pytzL = pacific.normalize(pytzUTC.astimezone(pacific))
surface_values = (linesplit[2:])
for x in surface_values: times.append(pytzL.strftime("%m/%d/%y %I:%M %p"))
count = count + 1
profilerfile.close()