-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_experiment.py
executable file
·401 lines (354 loc) · 16.9 KB
/
add_experiment.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/opt/websites/database/calendar/venv/bin/python
###########################################################
#
# Title: add_experiment.py
#
# Description: This script adds an experiment from the
# AMISR_PROCESSED NAS into the SRI calendar
# database. It requires a Madrigal.ini file
# to do this.
#
# Author: M. Nicolls 2007
#
# Modified: 7 Oct 2019 - Ashton Reimer
# Moved scripts from io to io2, upgraded to
# python2.7
#
# Modified: 5 Oct 2022 - Pablo Reyes
# Change classification from:
# Alternating Code -> E-region bottom F-region
# Long Pulse -> F-region
# adds the integration time to the left column
# avoids duplications of files. After all the uncorrected Ne
# is included in the regular file
# - avoids duplication of images, e.g. Ne and Nenotr
# - Sorts the lists in order to have the links organized
# - Fixed Vvels different integration times
# Modified: 6 Oct 2022 - Pablo Reyes
# - Adding a prefix to the figure name to allow
# multiple integration times to coexist.
# Modified: 24 Oct 2022 - Pablo Reyes
# - Fixed a bug in line ~ 244 image_label
# This bug was creating problems in adding processed data files
# Modified: 06 Dec 2022 - Pablo Reyes
# - code put in place to avoidthe copy of the same file twice one
# for NeNoTr and the other for fitted data, since they are the
# same file
# Modified: 06 Mar 2023 - Pablo Reyes
# - Eliminating description "bottom..." it was too verbose.
# Modified: 06 Apr 2023 - Pablo Reyes
# - adding space in the fig names after the h5 file prefix.
# Modified: 16 Apr 2023 - Pablo Reyes
# - fixing a bug in uploading vvels figures
###########################################################
import sys
import os.path
import tables
import scipy
import scipy.io
import glob
import shutil
import copy
import datetime
import ConfigParser
import shutil
import re
import logging
LOG_DIR = '/opt/websites/database/calendar/amisrdb/ExperimentDetails/logs'
LOG_FILENAME = 'GenExpLists.log'
#############################
copyON = 1
linkMad = 1
Mad2Path = '%(MADRIGAL_HTTP_PATH)s'
MadPath = '%(MADRIGAL3_HTTP_PATH)s'
def usage():
print "usage: ", sys.argv[0]
print "\t EXPERIMENT DIRECTORIES"
print "\t EXPERIMENT DIRECTORIES mad2"
print "\t optional mad2 : for posting old madrigal2 link"
sys.exit(2)
def get_intg_time(x):
mpos = x.find("minute")
spos = x.find("second")
if mpos>=0:
subx = x[(mpos-7):mpos]
secs = 60*int(subx[subx.find(" "):])
elif spos>=0:
subx = x[(spos-7):spos]
secs = int(subx[subx.find(" "):])
else:
secs = 1e99
return secs
if __name__ == '__main__':
FILE_OUT = 'Data.ini'
SEC_TEMPLATE = {'Path':'','Images':{'Count':0,'Hash':[]}, 'Links':{'Count':0}}
INSTRUMENTS = {
'61': {
'OUTPUT_PATH': '/opt/websites/database/calendar/amisrdb/ExperimentDetails/PFISR/Experiments',
'INST_MNEUMONIC': 'pfa'
},
'91': {
'OUTPUT_PATH': '/opt/websites/database/calendar/amisrdb/ExperimentDetails/RISR-N/Experiments',
'INST_MNEUMONIC': 'ran'
},
}
list_of_datafiles = []
avoid_overwrite = True
avoid_overwrite = False
try:
dirs2do = sorted(glob.glob(sys.argv[1]))
except:
usage()
try:
onlymad2 = sys.argv[2] == "mad2"
except:
onlymad2 = False
for direc in dirs2do:
if not os.path.isdir(direc) or os.path.islink(direc):
continue
print 'Doing %s' % direc
try:
MAD_FILE = os.path.join(direc,'Madrigal.ini')
MAD_PATH = os.path.dirname(MAD_FILE)
MAD_EXP = os.path.basename(MAD_PATH)
# read config file
config = ConfigParser.ConfigParser()
config.optionxform = str
config.read(MAD_FILE)
except:
raise IOError, 'Error reading Madrigal ini file'
# get instrument
try:
inst=config.get('Experiment','instrument')
expName = config.get('DEFAULT','ExperimentName')
OUTPUT_PATH = os.path.join(INSTRUMENTS[inst]['OUTPUT_PATH'],expName)
except:
raise IOError, 'Could not find instrument'
#
if MAD_EXP != expName:
config.set('DEFAULT','ExperimentName',MAD_EXP)
DSTR_OUT={}
if copyON:
DSTR_OUT['Data Files']=copy.deepcopy(SEC_TEMPLATE)
DSTR_OUT['Data Files']['Path']= 'DataFiles'
fdir = os.path.join(OUTPUT_PATH,DSTR_OUT['Data Files']['Path'])
if not os.path.exists(fdir):
try:
os.mkdir(fdir)
except:
raise IOError, 'Unable to make dir %s' % fdir
DSTR_OUT['Additional Plots']=copy.deepcopy(SEC_TEMPLATE)
DSTR_OUT['Additional Plots']['Path']='AdditPlots'
additdir = os.path.join(OUTPUT_PATH,DSTR_OUT['Additional Plots']['Path'])
if not os.path.exists(additdir):
try:
os.mkdir(additdir)
except:
raise IOError, 'Unable to make dir %s' % additdir
uploaded_files = []
uploaded_figs = []
for ifile in range(1,100000):
tsec = 'File%s' % ifile
if not config.has_section(tsec):
break;
try:
fname = config.get(tsec,'hdf5Filename')
type = config.get(tsec,'type')
ckindat = config.get(tsec,'ckindat')
kindat = config.get(tsec,'kindat')
status = config.get(tsec,'status')
category = int(config.get(tsec,'category'))
history = config.get(tsec,'history')
fileOutVerTag = config.get(tsec,'fileOutVerTag')
except:
raise IOError, 'Could not read sec %s' % tsec
print(ckindat)
tname=ckindat
classification_style = "Ionospheric_Regions" # "Pulse_Type"
if classification_style == "Ionospheric_Regions":
if tname.find('Barker')>=0 or tname.find("D-region")>=0:
#classname = "D-region bottom E-region (Barker/mc)"
classname = "D-region (Barker/mc)"
elif tname.find("Alternating Code")>=0 or \
tname.find("E-region")>=0:
#classname = 'E-region bottom F-region (Alternating Codes)'
classname = 'E-region (Alternating Codes)'
elif tname.find('Long Pulse')>=0 or \
(tname.find("F-region")>=0 and
tname.find("E-region")<0):
classname = 'F-region (Long Pulse)'
elif classification_style == "Pulse_Type":
if tname.startswith('Alternating Code'):
classname = 'Alternating Code'
elif tname.startswith('Long Pulse'):
classname = 'Long Pulse'
if tname.find("Velocity")>=0 or tname.find("Vector")>=0 \
or tname.find("Vel.") >=0 :
classname = "Resolved Velocity"
tname = classname
if category != 1:
tname += ",ver:" + fileOutVerTag
if DSTR_OUT.has_key(tname):
tpath = DSTR_OUT[tname]['Path']
else:
DSTR_OUT[tname]=copy.deepcopy(SEC_TEMPLATE)
tpath = 'Path%s' % str(len(DSTR_OUT.keys())) + "_" + fileOutVerTag
DSTR_OUT[tname]['Path'] = tpath
# make dir
tdir = os.path.join(OUTPUT_PATH,tpath)
if not os.path.exists(tdir):
try:
os.mkdir(tdir)
except:
raise IOError, 'Unable to make dir %s' % tdir
# data file
if copyON:
try:
print("Copy fname:",fname)
target = os.path.join(fdir,os.path.basename(fname))
print("Copy to :", target)
if avoid_overwrite and os.path.exists(target):
print(target,"already exists. skipping")
else:
if target not in list_of_datafiles:
# This avoids to copy the same file twice for NeNoTr and for fitted data
# since both are the same file
list_of_datafiles.append(target)
shutil.copyfile(fname, target)
else:
print(target,"was already copied")
except Exception, e:
raise Exception,'Exception: Unable to copy file %s\n%s' % (fname,str(e))
if copyON:
#tit = ckindat + ' - hdf'
tit = classname
if category != 1:
tit += ', status:' + status
if len(history)>0:
tit += ', history:' + history
tit += ', ver:' + fileOutVerTag + ' - hdf'
if not DSTR_OUT['Data Files']['Images'].has_key(tit):
DSTR_OUT['Data Files']['Images']['Count']+=1
DSTR_OUT['Data Files']['Images'][tit] = {'Count':0,
'imgCount':DSTR_OUT['Data Files']['Images']['Count']}
DSTR_OUT['Data Files']['Images']['Hash'].append(tit)
file2upload = os.path.basename(fname)
if file2upload not in uploaded_files:
uploaded_files.append(file2upload)
DSTR_OUT['Data Files']['Images'][tit]['Count']+=1
image_label = 'image%s%s' % (
# Fixed on Oct 24, 2022 by Pablo Reyes
#str(DSTR_OUT['Data Files']['Images']['Count']),
str(DSTR_OUT['Data Files']['Images'][tit]['imgCount']),
chr(DSTR_OUT['Data Files']['Images'][tit]['Count']-1+ord('a')))
DSTR_OUT['Data Files']['Images'][tit][image_label] = file2upload
# images
for iimg in range(1,100000):
timg = 'image%s' % iimg
timgtit = 'imageTitle%s' % iimg
if not config.has_option(tsec,timg):
break;
img = config.get(tsec,timg)
tit = config.get(tsec,timgtit)
#if tit.__contains__('Electron Density'):
# tit = 'Electron Density'
if tit.__contains__('Geometry Plot'):
try:
# Nov 15, 2022. P. Reyes
# Adding a prefix to allow different geo plots like risrn 20200229.001 GPSN.v01.85.db92.risrn
fig_prefix = tit[:tit.find(' ')]
fig2upload = fig_prefix + " " + os.path.basename(img)
print("Copy img:",img)
#print("Copy to :",os.path.join(additdir,os.path.basename(img)))
print("Copy to :",os.path.join(additdir,fig2upload))
shutil.copyfile(img,os.path.join(additdir,fig2upload))
except:
raise IOError, 'Unable to copy file %s' % img
tname_geo='Additional Plots' # new thumbnail
if not DSTR_OUT[tname_geo]['Images'].has_key(tit):
DSTR_OUT[tname_geo]['Images']['Count']+=1
DSTR_OUT[tname_geo]['Images'][tit] = {'Count':0,'imgCount':DSTR_OUT[tname_geo]['Images']['Count']}
DSTR_OUT[tname_geo]['Images']['Hash'].append(tit)
DSTR_OUT[tname_geo]['Images'][tit]['Count']+=1
DSTR_OUT[tname_geo]['Images'][tit]['image%s%s' % (
str(DSTR_OUT[tname_geo]['Images']['Count']),
chr(DSTR_OUT[tname_geo]['Images'][tit]['Count']-1+ord('a')))] = fig2upload
continue
if type == 'velocity':
try:
p1 = re.compile('\d+sec')
sintgsec = p1.findall(tit)[0]
tit = tit[tit.find(sintgsec):]
#tit = sintgsec + ' Magnitude and Direction'
except Exception as e:
print('Problem Encounterd: Skipping.\n%s' % str(e))
continue
if os.path.basename(img).find(sintgsec) < 0:
# intgseconds doesn't match the image file
continue
try:
if type != 'velocity':
fig_prefix = tit[:tit.find(' ')]
fig2upload = fig_prefix + " " + os.path.basename(img)
else:
fig2upload = os.path.basename(img)
img_destiny = os.path.join(tdir, fig2upload)
#print('tit:',tit,"copy",img)
shutil.copyfile(img,img_destiny)
except:
raise IOError, 'Unable to copy file %s' % img
if not DSTR_OUT[tname]['Images'].has_key(tit):
DSTR_OUT[tname]['Images']['Count']+=1
DSTR_OUT[tname]['Images'][tit] = {'Count':0,'imgCount':DSTR_OUT[tname]['Images']['Count']}
DSTR_OUT[tname]['Images']['Hash'].append(tit)
unique_name = tname+'|fig2upload:'+fig2upload
if unique_name not in uploaded_figs:
uploaded_figs.append(unique_name)
print(unique_name)
DSTR_OUT[tname]['Images'][tit]['Count']+=1
DSTR_OUT[tname]['Images'][tit]['image%s%s' % (
str(DSTR_OUT[tname]['Images']['Count']),
chr(DSTR_OUT[tname]['Images'][tit]['Count']-1+ord('a')))] = fig2upload
with open(os.path.join(OUTPUT_PATH,FILE_OUT),'w') as fid:
#for key in sorted(DSTR_OUT.keys(), key=lambda x:(x[0:2],get_intg_time(x))):
for key in sorted(DSTR_OUT.keys(), key=lambda x:(x[0],len(x))):
fid.write('[%s]\n\n' % key)
fid.write('Path: %s\n\n' % DSTR_OUT[key]['Path'])
for skey in sorted(DSTR_OUT[key]['Images']['Hash']):
fid.write('imageTitle%d: %s\n' % (DSTR_OUT[key]['Images'][skey]['imgCount'],skey))
for i in range(DSTR_OUT[key]['Images'][skey]['Count']):
try:
bp = 'image%d%s' % (DSTR_OUT[key]['Images'][skey]['imgCount'],chr(i+ord('a')))
towrite = '%s: %s\n' % (bp, DSTR_OUT[key]['Images'][skey][bp])
fid.write(towrite)
except:
print '???'
print 'skey', skey
print 'bp',bp
print 'to write', towrite
fid.write('\n')
if linkMad:
expId = MAD_EXP
year = int(expId[0:4])
month = int(expId[4:6])
day = int(expId[6:8])
num = int(expId[10:12])
#extChar =chr(num+ord('a')-1)
if num <= 26:
extChar = chr(96 + num)
else:
extChar = chr(96 + (num-1)//26) + chr(97 + (num-1)%26)
thisDate = datetime.datetime(year, month, day)
if onlymad2:
madLink='%sexp=%s/%s/%s%s&displayLevel=0' % (Mad2Path,thisDate.strftime('%Y').lower(),INSTRUMENTS[inst]['INST_MNEUMONIC'],thisDate.strftime('%d%b%y').lower(), extChar)
else:
madLink='%sexperiment_list=/%s/%s/%s%s' % (MadPath,thisDate.strftime('%Y').lower(),INSTRUMENTS[inst]['INST_MNEUMONIC'],thisDate.strftime('%d%b%y').lower(), extChar)
# Madrigal2:
# http://isr.sri.com/madrigal/cgi-bin/madExperiment.cgi?exp=2023/pfa/01mar23b&displayLevel=0
# In Madrigal3:
# https://data.amisr.com/madrigal/showExperiment?experiment_list=/2023/pfa/01mar23b
fid.write('[Links]\n\n')
fid.write('Path: %s\n\n' % '')
fid.write('imageTitle1: %s\n' % ('Access Data from Madrigal'))
bp = 'image1a'
fid.write('%s: %s\n\n' % (bp, madLink))