-
Notifications
You must be signed in to change notification settings - Fork 2
/
MPL_daily_update.py
182 lines (136 loc) · 5.89 KB
/
MPL_daily_update.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 14 13:04:21 2014
Updated on May 7,2014
@author: User
"""
import os, sys
import pandas as pan
import datetime,time
import numpy as np
import glob
#Step 1: Set folders for locations of unsorted raw data files, python code, webdata folder
if sys.platform == 'win32':
import matplotlib
from matplotlib import pyplot as plt
datalib = 'C:\Users\dashamstyr\Dropbox\MPL website\pcottle\MPLData\Raw_Files'
sitelib = 'C:\Users\dashamstyr\Dropbox\MPL website\WebData'
codelib = 'C:\Users\dashamstyr\Dropbox\Python_Scripts\GIT_Repos\MPLcode'
historyfile = 'C:\Users\dashamstyr\Dropbox\MPL website\pcottle\MPLData\MPL_location_history.txt'
else:
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
datalib = '/data/lv1/pcottle/MPLData/Raw_Files'
sitelib = '/data/lv1/WebData'
codelib = '/data/lv1/pcottle/MPLCode'
historyfile = '/data/lv1/pcottle/MPLData/MPL_location_history.txt'
homepage = os.path.join(sitelib,'index.html')
try:
sys.path.append(codelib)
from MPLcode import MPLtools as mtools
from MPLcode import HTMLtools as htools
from MPLcode import MPLplot as mplot
from MPLcode import MPLprocesstools as mproc
except ImportError:
try:
import MPLtools as mtools
import HTMLtools as htools
import MPLplot as mplot
import MPLprocesstools as mproc
except ImportError:
raise Exception('You havent specified where your modules are located!')
olddir=os.getcwd()
#Set global variables for plots and data files
#--------------------------------------------------------------------
#set altitude range and time step sizes for processing and plots
altrange = np.arange(150,15060,60)#meters
timestep = '120S' #seconds
#----------------------------------------------------------------------
location_dict = htools.locations(historyfile)
for loc,dates in location_dict.iteritems():
templib = os.path.join(sitelib,loc)
tempdatlib = os.path.join(templib,'Data_Files')
tempimglib = os.path.join(templib,'Image_Files')
for lib in [templib,tempdatlib,tempimglib]:
if not os.path.isdir(lib):
os.mkdir(lib)
if 'na' in dates[1]:
current_loc = loc
#Step 2: Open raw data folder and make a list of all .mpl files
#os.chdir(datalib)
allfiles = os.listdir(datalib)
procfiles=[]
for root,dirs,files in os.walk(sitelib):
for f in files:
if f.endswith('.h5'):
procfiles.append(f.split('.')[0])
#Step 3: Make dict of all year-month-day combos in .mpl files
rawfiles=[]
for f in allfiles:
if f.endswith('.mpl') and f[:8] not in procfiles:
rawfiles.append(f)
if rawfiles:
np.sort(rawfiles)
filenames,filedates = htools.listdates(rawfiles)
#Step 4: Loop through .mpl files and ...
newday = filedates[0]
dailympl =[]
MPLdat_event = []
for f,date in zip(filenames,filedates):
#Step 4a: Bundle files by day and generate new processed filename YYYYMMDD.h5 (&.png)
if date != newday or f==filenames[-1]:
tempyear = newday[0]
tempmonth = newday[1]
tempday = newday[2]
savefilename = '{0}{1}{2}'.format(tempyear,tempmonth,tempday)
saveloc = htools.findloc(f,location_dict)
savefoldername = '{0}-{1}'.format(tempyear,tempmonth)
loclib = os.path.join(sitelib,saveloc)
procdatlib = os.path.join(loclib,'Data_Files',savefoldername)
procimglib = os.path.join(loclib,'Image_Files',savefoldername)
if not os.path.isdir(procdatlib):
os.mkdir(procdatlib)
if not os.path.isdir(procimglib):
os.mkdir(procimglib)
# MPLdat_event = mtools.MPL()
for mpl in dailympl:
MPLdat_temp = mtools.MPL()
MPLdat_temp.fromMPL(mpl)
MPLdat_temp.alt_resample(altrange, verbose=False)
if MPLdat_event:
MPLdat_event.append(MPLdat_temp)
else:
MPLdat_event = MPLdat_temp
#sort by index to make certain data is in order then set date ranges to match
MPLdat_event.header.sort_index()
for n in range(MPLdat_event.header['numchans'][0]):
data = MPLdat_event.data[n]
data = data.sort_index()
MPLdat_event.time_resample(timestep,verbose=False)
MPLdat_event.calc_all()
MPLdat_event=mproc.NRB_mask_all(MPLdat_event)
#Now generate a new figure.
kwargs = {'saveplot':True,'showplot':False,'verbose':False,
'savefilepath':procimglib,'savefilename':'{0}.png'.format(savefilename),
'SNRmask':True}
mplot.doubleplot(MPLdat_event,**kwargs)
plt.close('all')
#Step 4c: Switch to YYYY-MM folder (create if not there already) and save .h5, then repeat for .png
os.chdir(procdatlib)
MPLdat_event.save_to_HDF('{0}.h5'.format(savefilename))
newday = date
dailympl = []
MPLdat_event = []
dailympl.append(os.path.join(datalib,f))
os.chdir(olddir)
else:
dailympl.append(os.path.join(datalib,f))
newday = date
with open(homepage,'r') as htmltemp:
data = htmltemp.readlines()
for n in range(len(data)):
if "<h1>Current Location:" in data[n]:
data[n] = '<h1>Current Location: {0}</h1>\r\n'.format(current_loc)
with open(homepage,'w') as htmlhome:
htmlhome.writelines(data)