-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepFiles.py
144 lines (129 loc) · 4.37 KB
/
prepFiles.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
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
#!/usr/bin/env python3
"""This script prepares all the RINEX files and antenna information files for
automatic processing
"""
import re
import os
import datetime
import sys
# Compile the regular expressions
p1 = re.compile('^\w{8}\.\d{2}o$', re.I)
# Create the directory for the individual RINEX antenna information files
os.mkdir('rinexantls')
# Rename files that don't have a '0' before the extension. If renaming will
# cause a name conflict then rename totally
filenum = 0
newFile = {}
fout = open('nameChanges.dat', 'w')
for f in os.listdir('.'):
if p1.match(f):
if f[7:8] != '0':
new_f = f[:7] + '0' + f[8:]
if os.path.isfile(new_f):
conflict = True
while conflict:
new_f = '{:04d}{}0.{}'.format(filenum, f[4:7], f[9:])
filenum += 1
if not os.path.isfile(new_f):
conflict = False
newFile[f] = new_f
fout.write(f"{new_f} {f}\n")
os.rename(f, new_f)
fout.close()
# Read in the start time, end time, and duration for each RINEX file
unsorted_rnx = []
unsorted_start = []
start = {}
end = {}
duration = {}
for line in open('durations.dat'):
cols = line.split()
try:
f = newFile[cols[0]]
except KeyError:
f = cols[0]
unsorted_rnx.append(f)
try:
start[f] = datetime.datetime.strptime(cols[1] + ' ' + cols[2],
'%Y-%m-%d %H:%M:%S')
except ValueError:
h, m, s = cols[2].split(':')
if (abs(float(s) - 0) <= abs(float(s) - 30)):
s = '00'
else:
s = '30'
cols[2] = h + ':' + m + ':' + s
start[f] = datetime.datetime.strptime(cols[1] + ' ' + cols[2],
'%Y-%m-%d %H:%M:%S')
unsorted_start.append(start[f])
try:
end[f] = datetime.datetime.strptime(cols[3] + ' ' + cols[4],
'%Y-%m-%d %H:%M:%S')
except ValueError:
h, m, s = cols[4].split(':')
if (abs(float(s) - 0) <= abs(float(s) - 30)):
s = '00'
else:
s = '30'
cols[4] = h + ':' + m + ':' + s
end[f] = datetime.datetime.strptime(cols[3] + ' ' + cols[4],
'%Y-%m-%d %H:%M:%S')
duration[f] = int(float(cols[5]))
sorted_start, sorted_rnx = \
(list(t) for t in zip(*sorted(zip(unsorted_start, unsorted_rnx))))
# Read in the station, antenna, and height for each RINEX file
station = {}
antenna = {}
height = {}
for line in open('RinexAntLs.txt'):
cols = line.split()
try:
f = newFile[cols[0]]
except KeyError:
f = cols[0]
station[f] = f[:4]
antenna[f] = cols[1]
height[f] = cols[2]
# Loop until all RINEX files are in a cluster
while sorted_rnx:
#print(sorted_rnx[0], sorted_rnx[1], sorted_rnx[2])
rnx0 = sorted_rnx[0]
cutoff = end[rnx0]
data = []
data.append([rnx0, antenna[rnx0], height[rnx0]])
sorted_rnx.remove(rnx0)
#print('rem 1', sorted_rnx[0], sorted_rnx[1], sorted_rnx[2])
# Open the cluster RINEX antemnna information file
rnxantls = rnx0[9:11] + rnx0[4:7] + '_ls'
if os.path.isfile(rnxantls):
conflict = True
filenum = 0
while conflict:
filenum += 1
rnxantls = rnxantls[:5] + str(filenum) + '_ls'
if not os.path.isfile(rnxantls):
conflict = False
fout = open(rnxantls, 'w')
# Loop over the RINEX files left
rnx_remove = []
for rnx in sorted_rnx:
#print(sorted_rnx[0], sorted_rnx[1], sorted_rnx[2])
delta = cutoff - start[rnx]
#print(rnx, delta.total_seconds())
if delta.total_seconds() >= 7200:
if end[rnx] < cutoff:
cutoff = end[rnx]
data.append([rnx, antenna[rnx], height[rnx]])
rnx_remove.append(rnx)
#print('rem 2', sorted_rnx[0], sorted_rnx[1], sorted_rnx[2])
for rnx in rnx_remove:
sorted_rnx.remove(rnx)
# Write out the cluster data
for line in data:
fout.write(line[0] + ' ' + line[1] + ' ' + line[2] + '\n')
fout.close()
# Move antenna information files to rinexantls/
os.system('mv *_ls rinexantls')
# Delete nameChanges.dat if empty
if os.stat('nameChanges.dat').st_size == 0:
os.remove('nameChanges.dat')