8 | | python track.py glider_name start_date end_date |
---|
9 | | |
---|
10 | | where start_date and end_date are of the form: YYYYMMDD. |
---|
11 | | |
---|
12 | | start_date is inclusive. stop_date is exclusive. |
---|
13 | | An exception is raised if start_date < stop_date. |
---|
| 8 | python track.py [positions [start_date [end_date]]] |
---|
| 9 | |
---|
| 10 | where (optional) positions are the number of positions to report, and |
---|
| 11 | |
---|
| 12 | where (optional) start_date and end_date are of the form: YYYYMMDD. |
---|
| 13 | |
---|
| 14 | start_date is inclusive; stop_date is exclusive. |
---|
| 15 | |
---|
| 16 | An exception is raised if start_date > stop_date. |
---|
22 | | glider_name = 'pelagia' |
---|
23 | | comm_names = ['freewave','network',] |
---|
24 | | start_date = '20120125' # inclusive |
---|
25 | | stop_date = '20120127' # exclusive |
---|
| 25 | glider_dir = '/var/opt/gmc/gliders' |
---|
| 26 | gliders = ['pelagia', 'ramses'] |
---|
| 27 | comms = ['freewave', 'network', ] |
---|
| 28 | positions = 10 # report most recent number of positions |
---|
| 29 | tomorrow = datetime.now() + timedelta(1) |
---|
| 30 | start_date = '20120125' # inclusive |
---|
| 31 | stop_date = '%04d%02d%02d' % (tomorrow.year, # exclusive |
---|
| 32 | tomorrow.month, |
---|
| 33 | tomorrow.day) |
---|
63 | | file_patterns = [file_pattern_template % (glider_name, |
---|
64 | | comm_name, |
---|
65 | | date.year, |
---|
66 | | date.month, |
---|
67 | | date.day,) |
---|
68 | | for comm_name in comm_names |
---|
69 | | for date in date_range] |
---|
70 | | |
---|
71 | | log_dir = os.path.join('/var/opt/gmc/gliders',glider_name,'logs') |
---|
72 | | file_paths = [] |
---|
73 | | for file_pattern in file_patterns: |
---|
74 | | file_paths.extend(glob(os.path.join(log_dir, file_pattern))) |
---|
| 89 | file_patterns = {} |
---|
| 90 | for glider in gliders: |
---|
| 91 | file_patterns[glider] = [file_pattern_template % (glider, |
---|
| 92 | comm, |
---|
| 93 | date.year, |
---|
| 94 | date.month, |
---|
| 95 | date.day,) |
---|
| 96 | for comm in comms |
---|
| 97 | for date in date_range] |
---|
| 98 | |
---|
| 99 | log_dirs = {} |
---|
| 100 | for glider in gliders: |
---|
| 101 | log_dirs[glider] = os.path.join(glider_dir, glider, 'logs') |
---|
| 102 | |
---|
| 103 | file_paths = {} |
---|
| 104 | for glider in gliders: |
---|
| 105 | file_paths[glider] = [] |
---|
| 106 | for file_pattern in file_patterns[glider]: |
---|
| 107 | file_paths[glider].extend(glob(os.path.join(log_dirs[glider], |
---|
| 108 | file_pattern))) |
---|
80 | | file_paths = find(glider_name,start_date,stop_date) |
---|
81 | | points = [] |
---|
82 | | for file_path in file_paths: |
---|
83 | | handle = open(file_path) |
---|
84 | | contents = handle.readlines() |
---|
85 | | handle.close() |
---|
86 | | protents = contents[4:] |
---|
87 | | xref = zip(protents,contents) |
---|
88 | | for pline,cline in xref: |
---|
89 | | match = p_pattern.search(pline) |
---|
90 | | if match: |
---|
91 | | lat,lon,fresh = match.groups() |
---|
92 | | if lat == bad_coordinate: continue |
---|
93 | | if lon == bad_coordinate: continue |
---|
94 | | if fresh == bad_freshness: continue |
---|
95 | | if float(fresh) >= stale: continue |
---|
96 | | match = c_pattern.search(cline) |
---|
| 115 | file_paths = find(gliders, start_date, stop_date) |
---|
| 116 | points = {} |
---|
| 117 | for glider in gliders: |
---|
| 118 | points[glider] = [] |
---|
| 119 | for file_path in file_paths[glider]: |
---|
| 120 | handle = open(file_path) |
---|
| 121 | contents = handle.readlines() |
---|
| 122 | handle.close() |
---|
| 123 | protents = contents[4:] |
---|
| 124 | xref = zip(protents, contents) |
---|
| 125 | for pline, cline in xref: |
---|
| 126 | match = p_pattern.search(pline) |
---|
98 | | weekday, month, day, time, year = match.groups() |
---|
99 | | points.append({'lat':lat, |
---|
100 | | 'lon':lon, |
---|
101 | | 'fresh':fresh, |
---|
102 | | 'weekday':weekday, |
---|
103 | | 'month':month, |
---|
104 | | 'day':day, |
---|
105 | | 'time':time, |
---|
106 | | 'year':year,}) |
---|
107 | | |
---|
108 | | points.sort(cmp=lambda x,y: cmp(datetime(int(x['year']), |
---|
109 | | months[x['month']], |
---|
110 | | int(x['day']), |
---|
111 | | int(x['time'][0:2]), |
---|
112 | | int(x['time'][3:5]), |
---|
113 | | int(x['time'][6:8])), |
---|
114 | | datetime(int(y['year']), |
---|
115 | | months[y['month']], |
---|
116 | | int(y['day']), |
---|
117 | | int(y['time'][0:2]), |
---|
118 | | int(y['time'][3:5]), |
---|
119 | | int(y['time'][6:8])))) |
---|
| 128 | lat, lon, fresh = match.groups() |
---|
| 129 | if lat == bad_coordinate: |
---|
| 130 | continue |
---|
| 131 | if lon == bad_coordinate: |
---|
| 132 | continue |
---|
| 133 | if fresh == bad_freshness: |
---|
| 134 | continue |
---|
| 135 | # if float(fresh) >= stale: |
---|
| 136 | # continue |
---|
| 137 | match = c_pattern.search(cline) |
---|
| 138 | if match: |
---|
| 139 | weekday, month, day, time, year = match.groups() |
---|
| 140 | year = int(year) |
---|
| 141 | month = months[month] |
---|
| 142 | day = int(day) |
---|
| 143 | hour = int(time[0:2]) |
---|
| 144 | minute = int(time[3:5]) |
---|
| 145 | second = int(time[6:8]) |
---|
| 146 | stamp = datetime(year, month, day, |
---|
| 147 | hour, minute, second) |
---|
| 148 | stamp = stamp - timedelta(seconds=float(fresh)) |
---|
| 149 | points[glider].append({'lat': lat, |
---|
| 150 | 'lon': lon, |
---|
| 151 | 'fresh': fresh, |
---|
| 152 | 'stamp': stamp, }), |
---|
| 153 | break |
---|
| 154 | |
---|
| 155 | points[glider].sort(cmp=lambda x, y: cmp(x['stamp'], y['stamp'])) |
---|
| 156 | # If two adjacent positions differ in time by less than delta, |
---|
| 157 | # then remove the earlier position from the report. |
---|
| 158 | # This effect is cumulative for a string of adjacent positions. |
---|
| 159 | for pointa, pointb in zip(points[glider][:-1], points[glider][1:]): |
---|
| 160 | if pointb['stamp'] - pointa['stamp'] < delta: |
---|
| 161 | points[glider].remove(pointa) |
---|
145 | | out_dir = os.path.join('/home/localuser/deployment/tracks',glider_name) |
---|
146 | | try: |
---|
147 | | os.makedirs(out_dir) |
---|
148 | | except: |
---|
149 | | pass |
---|
150 | | |
---|
151 | | contents = ["%s position report from %s to %s\n" % |
---|
152 | | (glider_name, start_date, stop_date,),] |
---|
153 | | points = parse(glider_name,start_date,stop_date) |
---|
154 | | spec = "Lat: %s, Lon: %s on %s %s %02d, %04d at %s UTC\n" |
---|
155 | | contents.extend([spec % (point['lat'], |
---|
156 | | point['lon'], |
---|
157 | | point['weekday'], |
---|
158 | | point['month'], |
---|
159 | | int(point['day']), |
---|
160 | | int(point['year']), |
---|
161 | | point['time'],) |
---|
162 | | for point in points]) |
---|
163 | | |
---|
164 | | file_name = "%s_track_%s_%s.txt" % (glider_name, |
---|
165 | | start_date, |
---|
166 | | stop_date,) |
---|
167 | | handle = open(os.path.join(out_dir,file_name),"w") |
---|
168 | | handle.writelines(contents) |
---|
169 | | handle.close() |
---|
| 186 | points = parse(gliders, start_date, stop_date) |
---|
| 187 | spec = "Glider: %s, Lat: %s, Lon: %s on %s %s %02d, " \ |
---|
| 188 | "%04d at %02d:%02d:%02d UTC\n" |
---|
| 189 | for glider in gliders: |
---|
| 190 | for point in points[glider][-positions:]: |
---|
| 191 | print spec % (glider, |
---|
| 192 | point['lat'], |
---|
| 193 | point['lon'], |
---|
| 194 | weekdays[point['stamp'].weekday()], |
---|
| 195 | shtnom[point['stamp'].month], |
---|
| 196 | point['stamp'].day, |
---|
| 197 | point['stamp'].year, |
---|
| 198 | point['stamp'].hour, |
---|
| 199 | point['stamp'].minute, |
---|
| 200 | point['stamp'].second) |
---|