-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_weekly_report.py
executable file
·1238 lines (1039 loc) · 35.9 KB
/
create_weekly_report.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env /proj/sot/ska/bin/python
#############################################################################################################
# #
# create_weekly_report.py: create weekly report #
# #
# author: t. isobe (tisobe@cfa.harvard.edu) #
# #
# Last Update: Sep 16, 2016 #
# #
#############################################################################################################
import sys
import os
import string
import re
import copy
import math
import Cookie
import unittest
import time
import random
#
#--- from ska
#
from Ska.Shell import getenv, bash
ascdsenv = getenv('source /home/ascds/.ascrc -r release; source /home/mta/bin/reset_param ', shell='tcsh')
ascdsenv['IDL_PATH'] = '+/usr/local/rsi/user_contrib/astron_Oct09/pro:+/home/mta/IDL:/home/nadams/pros:+/data/swolk/idl_libs:/home/mta/IDL/tara:widget_tools:utilities:event_browser'
ascdsenv2 = getenv('source /proj/sot/ska/bin/ska_envs.csh', shell='tcsh')
ascdsenv2['IDL_PATH'] = '+/usr/local/rsi/user_contrib/astron_Oct09/pro:+/home/mta/IDL:/home/nadams/pros:+/data/swolk/idl_libs:/home/mta/IDL/tara:widget_tools:utilities:event_browser'
#
#--- reading directory list
#
path = '/data/mta/Script/Python_script2.7/dir_list_py'
f = open(path, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
for ent in data:
atemp = re.split(':', ent)
var = atemp[1].strip()
line = atemp[0].strip()
exec "%s = %s" %(var, line)
#
#--- append path to a private folders
#
#sys.path.append(base_dir)
sys.path.append(mta_dir)
import mta_common_functions as mcf
import convertTimeFormat as tcnv
#
#--- temp writing file name
#
rtail = int(10000 * random.random()) #---- put a romdom # tail so that it won't mix up with other scripts space
zspace = '/tmp/zspace' + str(rtail)
#
#--- set directory paths
#
d_dir = '/data/mta4/www/DAILY/mta_deriv/'
wdir = '/data/mta/Script/Weekly/'
tdir = wdir + 'Templates/'
odir = '/data/mta4/www/REPORTS/'
#
#--- admin email address
#
admin = 'tisobe@cfa.harvard.edu'
#------------------------------------------------------------------------------------------
#-- create_weekly_report: main script to create the weekly report for the week ---
#------------------------------------------------------------------------------------------
def create_weekly_report(date, year, debug = 0):
"""
main script to set up the weekly report template for the week
input: date --- date in the format of mmdd (e.g. 0910)
year --- year in the format of yyyy (e.g. 2015)
debug --- if it is other than 0, print out some output
output: a direcotry containing templete (e.g. Sep10)
"""
#
#--- if the test is requested, create Test directory
#
if debug != 0:
os.system('mkdir /data/mta/Script/Weekly/Test/')
odir = '/data/mta/Script/Weekly/Test/'
else:
odir = '/data/mta4/www/REPORTS/'
oned = 86400
syear = str(year) #--- 4 digit year
yrd2 = year[2] + year[3] #--- 2 digit year
year = int(float(year)) #--- integer year
date = str(date)
smon = date[0] + date[1] #--- two digit month
mon = int(float(smon)) #--- integer month
lmon = tcnv.changeMonthFormat(mon) #--- month in letter (e.g.Mar)
sday = date[2] + date[3] #--- two digit mday
day = int(float(sday)) #--- integer mday
stop = tcnv.convertDateToCTime(year, mon, day, 0, 0, 0)
day_n = stop - 7 * oned
# tout = tcnv.axTimeMTA(day_n)
# ttemp = re.split(':', tout)
# iru_start = str(ttemp[0]) + '_' + str(ttemp[1])
#day01 = stop - 6 * oned
#day0 = stop - 7 * oned
day01 = stop - 5 * oned
day0 = stop - 6 * oned
lday0 = stime_to_ddate(day0)
sday0 = sdate_to_ldate(lday0)
start = day0
lday1 = stime_to_ddate(day01)
tout = tcnv.axTimeMTA(day0)
ttemp = re.split(':', tout)
iru_start = str(ttemp[0]) + '_' + str(ttemp[1])
#
#--- year of the beginning of the period; could be different from that of the end
#
byear = ttemp[0]
lday0 = stime_to_ddate(day0)
day1 = stop - 5 * oned
lday1 = stime_to_ddate(day1)
day2 = stop - 4 * oned
lday2 = stime_to_ddate(day2)
sday2 = sdate_to_ldate(lday2)
day3 = stop - 3 * oned
lday3 = stime_to_ddate(day3)
day4 = stop - 2 * oned
lday4 = stime_to_ddate(day4)
sday4 = sdate_to_ldate(lday4)
day5 = stop - 1 * oned
lday5 = stime_to_ddate(day5)
day6 = stop
lday6 = stime_to_ddate(day6)
sday6 = sdate_to_ldate(lday6)
#tout = tcnv.axTimeMTA(day5)
tout = tcnv.axTimeMTA(day6)
ttemp = re.split(':', tout)
iru_stop = '_' + str(ttemp[1])
day7 = stop + 1 * oned
lday7 = stime_to_ddate(day7)
#
#---- setting file name
#
atemp = re.split('\/', lday6)
file_date = atemp[0] + atemp[1]
file_date2 = atemp[0] + '/' + atemp[1]
file_name = file_date + '.html'
#
#--- title
#
titledate = lday0 + ' - ' + lday6
ldate = sdate_to_ldate(lday6)
ldate_sp = sdate_to_ldate_with_space(lday6)
#
#--- focal temp file name
#
fptemp = file_date + '_fptemp.gif'
fpext_range = str(start)+' '+ str(stop)
fpstart = str(start)
fplsub = '"'+ sday0 + '", "' + sday2 + '", "' + sday4 + '", "' + sday6 + '"'
fpdsub = str(day0) + ', ' + str(day2) + ', ' + str(day4) + ', ' + str(day6)
#
#--- IRU span
#
irudate = iru_start + iru_stop
#
#--- telemetry idl command
#
telmstart = stime_to_ddate2(start)
telmstop = stime_to_ddate2(stop)
telem_command = 'weekly_telem,' + telmstart + ',' + telmstop
#
#--- telemetry header line
#
daylist = '|' + lday0 +'|' + lday1 + '|' + lday2 +'|' + lday3 +'|' + lday4 +'|' + lday5 +'|' + lday6
#
#--- find trending dates/title
#
tfile = tdir + 'trending_order'
f = open(tfile, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
data.reverse()
chk = 0
for ent in data:
if ent == '\s+' or ent == '':
continue
atemp = re.split(' : ', ent)
adate = atemp[0].strip()
aname = atemp[1].strip()
if chk == 0:
if adate == file_date2:
title = aname
chk = 1
else:
if aname == title:
last_trend_date = adate
break
#
#--- index.html input
#
s1 = sday0[0:3] + ' ' + sday0[3:5]
s2 = sday6[0:3] + ' ' + sday6[3:5]
index = '<td> <a href="./' + str(year) + '/' + file_date + '.html">' + s1 + ' - ' + s2 + '</a>'
#
#--- debugging output
#
if debug != 0:
print "file_name; " + file_name
print "title date: " + titledate
print "ldate: " + ldate
print "fptemp: " + fptemp
print "fpext_range: " + fpext_range
print "fpstart: " + fpstart
print " fplsub: " + fplsub
print " fpdsub: " + fpdsub
print "irudate: " + irudate
print "telmstart: " + telmstart
print "telmstop: " + telmstop
print "telmcommand: " + telem_command
print "daylist: " + daylist
print "title: " + title
print "last_trend_date: " + last_trend_date
print "index: " + index
#
#--- create a work directory
#
cmd = 'mkdir ' + wdir + ldate
os.system(cmd)
outdir = wdir + ldate + '/'
cmd = 'cp ' + tdir + 'get_ftemp_data.perl' + ' ' + outdir
os.system(cmd)
cmd = 'cp ' + tdir + 'subst3.perl' + ' ' + outdir
os.system(cmd)
cmd = 'cp ' + tdir + 'test' + ' ' + outdir
os.system(cmd)
cmd = 'mkdir ' + outdir + '/param'
os.system(cmd)
#
#--- create instruction page
#
tfile = tdir + 'instruction'
f = open(tfile, 'r')
input = f.read()
f.close()
input = input.replace('#LDATE_S#', ldate_sp)
input = input.replace('#LDATE#', ldate)
input = input.replace('#DDATE#', file_date)
input = input.replace('#YEAR#', syear)
input = input.replace('#DAYLIST#', daylist)
input = input.replace('#TELM_CMD#',telem_command)
input = input.replace('#INDEX#', index)
ofile = outdir + ldate.lower()
fo = open(ofile, 'w')
fo.write(input)
fo.close()
#
#--- focal temp related files
#
tfile = tdir + 'get_ftemp_wrap_script'
f = open(tfile, 'r')
input = f.read()
f.close()
input = input.replace('#MANDD#', ldate)
ofile = outdir + 'get_ftemp_wrap_script'
fo = open(ofile, 'w')
fo.write(input)
fo.close()
cmd = 'chmod 755 ' + outdir + 'get_ftemp_wrap_script'
os.system(cmd)
tfile = tdir + 'get_ftemp_main_script'
f = open(tfile, 'r')
input = f.read()
f.close()
#input = input.replace('#START#', str(start))
#input = input.replace('#STOP#', str(stop))
input = input.replace('#START#', str(start + 86400))
input = input.replace('#STOP#', str(stop + 86400))
ofile = outdir + 'get_ftemp_main_script'
fo = open(ofile, 'w')
fo.write(input)
fo.close()
cmd = 'chmod 755 ' + outdir + 'get_ftemp_main_script'
os.system(cmd)
tfile = tdir + 'plot_erad_time.pro'
f = open(tfile, 'r')
input = f.read()
f.close()
input = input.replace('#START#', str(start))
input = input.replace('#SDATELIST#', fpdsub)
input = input.replace('#LDATELIST#', fplsub)
ofile = outdir + 'plot_erad_time.pro'
fo = open(ofile, 'w')
fo.write(input)
fo.close()
cmd = 'cp -f ' + outdir + 'plot_erad_time.pro ./Focal/'
os.system(cmd)
#
#--- this_week file
#
tfile = tdir + 'this_week'
f = open(tfile, 'r')
input = f.read()
f.close()
input = input.replace('#DDATE#', file_date)
input = input.replace('#IRUSPAN1#', irudate)
input = input.replace('#IRUSPAN2#', irudate)
input = input.replace('#TITLE#', title)
input = input.replace('#TITLEDATE#',titledate)
atemp = last_trend_date
atemp = atemp.replace('/', '')
input = input.replace('#PREVREPORT#', atemp)
atemp = re.split('/', last_trend_date)
pmon = int(float(atemp[0]))
lmon = tcnv.changeMonthFormat(pmon)
line = lmon + ' ' + atemp[1]
#
#--- the previous report could be from the last year
#
ryear = syear
if mon < pmon:
ryear = year -1
ryear = str(ryear)
input = input.replace('#RYEAR#', ryear)
input = input.replace('#PREVDATE#', line)
atitle = str(title)
atitle = atitle.replace(' ', '_')
#file = tdir + 'Headers/' + atitle
#fs = open(file, 'r')
#trend = fs.read()
#fs.close()
#input = input.replace("#TREND#", trend)
[temp1, temp2, temp3, temp4] = read_cti_values()
input = input.replace('#ATEMP#', temp1)
input = input.replace('#ATEMP2#', temp2)
input = input.replace('#DTEMP#', temp3)
input = input.replace('#DTEMP2#', temp4)
[val, step] = read_sim()
input = input.replace('#WMOVE#', val)
input = input.replace('#WSTEP#', step)
#
#--- make photon and bad pixel output
#
run_bad_pix_and_photon(outdir)
#
#--- run to get focal temp fits files
#
tstop = stop + 86400
[fcnt, fdata] = run_focal_temp_data(outdir, start, stop, fptemp)
[fcnt, fdata] = run_focal_temp_data_new()
input = input.replace('#TEMPPEAK#', str(fcnt))
input = input.replace('#TEMPLIST#', fdata)
#
#--- bad pixel
#
file = outdir + 'bad_pix_list'
fs = open(file, 'r')
bdata = fs.read()
fs.close()
input = input.replace('BAD_PIXEL_TABLE', bdata)
#
#--- photon
#
file = outdir + 'photons'
fs = open(file, 'r')
pdata = fs.read()
fs.close()
input = input.replace('PHOTON_TABLE', pdata)
#
#--- telem data
#
update_weekly_telem(year, byear, mon)
tdata = run_telem_data(telem_command, daylist, outdir)
input = input.replace('TELEM_TABLE', tdata)
#
#--- trend data
#
trend = set_trend_data_input(str(date))
input = input.replace('#TREND#', trend)
#
#--- write out the weekly report
#
ofile = outdir + file_name
fo = open(ofile, 'w')
fo.write(input)
fo.close()
#
#--- clean up
#
cmd = 'rm ./out ./out2'
os.system(cmd)
#
#--- move files
#
move_files(date, year, outdir, file_name, fptemp, odir)
#
#--- send out email to admin; notify the job complete
#
send_email_to_admin(date, year)
#----------------------------------------------------------------------------------
#-- stime_to_ddate: change data in second from 1998.1.1 to mm/dd/yy format --
#----------------------------------------------------------------------------------
def stime_to_ddate(stime):
"""
change data in second from 1998.1.1 to mm/dd/yy format
input: stime --- time in seconds from 1998.1.1
output: dtime --- date in the form of mm/dd/yy (e.g. 08/19/15)
"""
tlist = tcnv.axTimeMTA(stime)
atemp = re.split(':', tlist)
year = int(float(atemp[0]))
ydate = int(float(atemp[1]))
[mon, date] = tcnv.changeYdateToMonDate(year, ydate)
lyear = str(atemp[0])
syr = lyear[2] + lyear[3]
smon = str(mon)
if mon < 10:
smon = '0' + smon
sday = str(date)
if date < 10:
sday = '0' + sday
dtime = smon + '/' + sday + '/' + syr
return dtime
#----------------------------------------------------------------------------------
#-- stime_to_ddate2: change data in second from 1998.1.1 to yyyymmdd format --
#----------------------------------------------------------------------------------
def stime_to_ddate2(stime):
"""
change data in second from 1998.1.1 to yyyymmdd format
input: stime --- time in seconds from 1998.1.1
output: dtime --- date in the form of yyyymmdd (e.g. 20150819)
"""
tlist = tcnv.axTimeMTA(stime)
atemp = re.split(':', tlist)
year = int(float(atemp[0]))
lyear = str(atemp[0])
ydate = int(float(atemp[1]))
[mon, date] = tcnv.changeYdateToMonDate(year, ydate)
smon = str(mon)
if mon < 10:
smon = '0' + smon
sday = str(date)
if date < 10:
sday = '0' + sday
dtime = lyear + smon + sday
return dtime
#----------------------------------------------------------------------------------
#-- sdate_to_ldate: change date in second from 1998.1.1 to MMMdd --
#----------------------------------------------------------------------------------
def sdate_to_ldate(sdate):
"""
change date in second from 1998.1.1 to MMMdd
input: stime --- time in seconds from 1998.1.1
output: ldate --- date in form of MMMdd (e.g. Aug19)
"""
atemp = re.split('\/', sdate)
mon = int(float(atemp[0]))
lmon = tcnv.changeMonthFormat(mon)
ldate = lmon + atemp[1]
return ldate
#----------------------------------------------------------------------------------
#-- sdate_to_ldate_with_space: change date in second from 1998.1.1 to MMM dd --
#----------------------------------------------------------------------------------
def sdate_to_ldate_with_space(sdate):
"""
change date in second from 1998.1.1 to MMM dd
input: stime --- time in seconds from 1998.1.1
output: ldate --- date in form of MMM dd (e.g. Aug 19)
"""
atemp = re.split('\/', sdate)
mon = int(float(atemp[0]))
lmon = tcnv.changeMonthFormat(mon)
ldate = lmon + ' ' + atemp[1]
return ldate
#----------------------------------------------------------------------------------
#-- read_cti_values: read cti values from the fitting result files --
#----------------------------------------------------------------------------------
def read_cti_values():
"""
read cti values from the fitting result file
input: none but read from /data/mta_www/mta_cti/Plot_adjust/fitting_result etc
output: ftemp1 --- Adjucted cti in CTI/year
ftemp2 --- Adjucted cti in CTI/day
ftemp3 --- Detrended cti in CTI/year
ftemp4 --- Detrended cti in CTI/day
"""
file = '/data/mta_www/mta_cti/Plot_adjust/fitting_result'
[ftemp1, ftemp2] = read_cti(file)
file = '/data/mta_www/mta_cti/Det_Plot_adjust/fitting_result'
[ftemp3, ftemp4] = read_cti(file)
return [ftemp1, ftemp2, ftemp3, ftemp4]
#----------------------------------------------------------------------------------
#-- read_cti: find a cti value from the file --
#----------------------------------------------------------------------------------
def read_cti(file):
"""
find a cti value from the file
input: file ---- the file name
output: ftemp1 ---- cti in CTI/year
ftemp2 ---- cti in CTI/day
"""
f = open(file, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
chk = 0
for ent in data:
if chk == 0:
mc = re.search('mn K alpha', ent)
if mc is not None:
chk = 1
continue
else:
mc = re.search('ACIS-I Average:', ent)
if mc is not None:
atemp = re.split('\s+', ent)
val = float(atemp[2])
yval = val / 365.0
ftemp1 = '%2.3e' % val
ftemp2 = '%2.3e' % yval
break
return [ftemp1, ftemp2]
#----------------------------------------------------------------------------------
#-- read_sim: read sim movement values from weekly averaged page --
#----------------------------------------------------------------------------------
def read_sim():
"""
read sim movement values from weekly averaged page
input: none, but read from /data/mta_www/mta_sim/wksum.html
output: val --- weekly average time/step
step --- counts of TSC moves
"""
f = open('/data/mta_www/mta_sim/wksum.html', 'r')
data = [line.strip() for line in f.readlines()]
f.close()
data.reverse()
i = 0
for line in data:
if line == '':
continue
if i == 4:
atemp = re.split('<td>', line)
btemp = re.split('</td>', atemp[1])
val = btemp[0].strip()
val = float(val)
val = "%1.5f" % (val)
val = str(val)
elif i == 13:
atemp = re.split('<td>', line)
btemp = re.split('</td>', atemp[1])
step = btemp[0].strip()
break
i += 1
return [val, step]
#----------------------------------------------------------------------------------
#-- run_bad_pix_and_photon: run bad pixel table script and photon table scrit --
#----------------------------------------------------------------------------------
def run_bad_pix_and_photon(outdir):
"""
run bad pixel table script and photon table scrit
input: outdir --- output directory name
output: files in outdir: bad_pix_list, photons
"""
cmd1 = "/usr/bin/env PERL5LIB="
cmd2 = 'perl /data/mta4/MTA/bin/weekly_obs2html.pl 8 photons'
cmd = cmd1 + cmd2
#
#--- run the phonton script
#
bash(cmd, env=ascdsenv)
mcf.rm_file(zspace)
cmd2 = 'perl ' + tdir + 'read_bad_pix_new.perl'
cmd = cmd1 + cmd2
#
#--- run the bad pixel script
#
bash(cmd, env=ascdsenv)
mcf.rm_file(zspace)
cmd = 'mv photons bad_pix_list ' + outdir
os.system(cmd)
#----------------------------------------------------------------------------------
#-- run_focal_temp_data_new: run focal temp script and create a plot, read a table --
#----------------------------------------------------------------------------------
def run_focal_temp_data_new():
"""
read output of find_focal_temp_peaks.py and get focal temp information
input: none, but read '/data/mta/Script/Weekly/Focal/focal_temp_list'
output: fcnt --- number of peaks observed
fdata --- table input
"""
f = open('/data/mta/Script/Weekly/Focal/focal_temp_list', 'r')
data = [line.strip() for line in f.readlines()]
f.close()
fcnt = len(data)
fdata = ''
for ent in data:
fdata = fdata + ent + '\n'
return [fcnt, fdata]
#----------------------------------------------------------------------------------
#-- run_focal_temp_data: run focal temp script and create a plot, read a table --
#----------------------------------------------------------------------------------
def run_focal_temp_data(outdir, start, stop, fptemp):
"""
run focal temp script and create a plot, read a table
input: outdir --- output direcotry name
start --- start time in seconds from 1998.1.1
stop --- stop time in seconds from 1998.1.1
fptemp --- plot name
output: fcnt --- number of peaks observed
fdata --- table input
"""
cmd = 'cp -f Templates/test .'
os.system(cmd)
cmd1 = '/usr/bin/env PERL5LIB="" '
#cmd2 = ' source /home/mta/bin/reset_param ; '
cmd2 = ' /usr/local/bin/perl ' + outdir + 'get_ftemp_data.perl ' + str(start) + ' ' + str(stop)
cmd = cmd1 + cmd2
#
#--- run the focal temp script to extract data
#
bash(cmd, env=ascdsenv)
cmd = 'cp -f *fits ./Focal/.'
os.system(cmd)
cmd = 'rm ./test'
os.system(cmd)
cmd = 'mv -f *fits ./Focal'
os.system(cmd)
cmd1 = "/usr/bin/env PERL5LIB="
cmd2 = ' idl ./Focal/run_temp > out'
cmd = cmd1 + cmd2
#
#--- run the focal temp script to create a plot
#
bash(cmd, env=ascdsenv2)
cmd = 'mv -f ./Focal/*fits ' + outdir
os.system(cmd)
cmd = 'mv ./Focal/*.gif ' + outdir + fptemp
os.system(cmd)
#
#--- read focal temp data
#
[fcnt, fdata] = read_focal_temp_output()
cmd = 'rm ./out'
###os.system(cmd)
return [fcnt, fdata]
#----------------------------------------------------------------------------------
#-- read_focal_temp_output: read the focal temperature output and adjust it for better look
#----------------------------------------------------------------------------------
def read_focal_temp_output():
"""
read the focal temperature output and adjust it for better look
input: none, but read from forcal temp script output
output: fcnt --- number of peaks
out --- adjucted table
"""
f = open('./out', 'r')
data = [line.strip() for line in f.readlines()]
f.close()
rows = []
chk = 0
for ent in data:
if chk == 0:
mc = re.search('ALT', ent)
if mc is not None:
chk = 1
continue
else:
mc1 = re.search('<tr', ent)
mc2 = re.search('</tr', ent)
if mc1 is not None:
save = ent
elif mc2 is not None:
save = save + ent
rows.append(save)
out = ''
for ent in rows:
atemp = re.split('<td>', ent)
line = '<tr align=center>'
for i in range(1, 4):
btemp = re.split('</td>', atemp[i])
val = float(btemp[0])
val = "%3.2f" % (val)
val = str(val)
line = line + '<td>' + val + '</td>'
out = out + line + '<td align=left> </td></tr>\n'
fcnt = len(rows)
return [fcnt, out]
#----------------------------------------------------------------------------------
#-- update_weekly_telem: adjusting weekly_telem.pro --
#----------------------------------------------------------------------------------
def update_weekly_telem(year, byear, mon):
"""
adjusting weekly_telem.pro
--- line 55 is the check of monthly boundary. it needs to be updated.
input: year --- year in 4 digit
bear --- year of beginning date
mon --- month in
output: <wdir>/Telem/weekly_telem.pro updated
"""
#
#--- convert year, byear, and mon to integer
#
year = int(float(year))
byear = int(float(byear))
mon = int(float(mon))
#
#--- if the year changes from the beginning of the period to the end of the period
#--- use the year of the beginning of the period and change the monthe Dec (12)
#
if byear < year:
year = byear
mon = 12
else:
mon -= 1 #--- a quick fix for the month change mon was "this month"
#--- and the transition occurs from the last month to this month
mon_plus = (0, 32, 29, 32, 31, 32, 31, 32, 32, 31, 32, 31, 32)
nyear = year
nmon = mon + 1
if nmon > 12:
nyear += 1
nmon = 1
lday = mon_plus[mon]
#
#--- if it is a leap year, the end of Feb is 29
#
if mon == 2:
if tcnv.isLeapYear(year):
lday += 1
smon = str(mon)
if mon < 10:
smon = '0' + smon
snmon = str(nmon)
if nmon < 10:
snmon = '0' + snmon
this = str(year) + smon + str(lday)
that = str(nyear) + snmon + '01'
file = tdir + 'weekly_telem_template'
f = open(file, 'r')
data = f.read()
f.close()
data = data.replace('#THIS#', this)
data = data.replace('#THAT#', that)
out = wdir + 'Telem/weekly_telem.pro'
fo = open(out, 'w')
fo.write(data)
fo.close()
cmd = 'chmod 755 ' + out
os.system(cmd)
#----------------------------------------------------------------------------------
#-- run_telem_data: run telemetry data idl script and clean up the result --
#----------------------------------------------------------------------------------
def run_telem_data(telem_command, daylist, outdir):
"""
run telemetry data idl script and clean up the result
input: telem_command --- idl command to extract data (e.g. weekly_telem,20150904,20150910)
daylist --- date header for telem.txt
outdir --- output directory name
output: tdata --- a table data created by the process
"""
fo = open('./run_telem', 'w')
fo.write("cd,'./Telem/'\n")
fo.write(telem_command)
fo.write('\n')
#fo.write("cd,'../'\n")
fo.write('exit\n')
fo.close()
fo = open('./Telem/header', 'w')
fo.write(daylist)
fo.write('\n')
fo.close()
cmd1 = "/usr/bin/env PERL5LIB="
cmd2 = ' idl ./run_telem > out2'
cmd = cmd1 + cmd2
bash(cmd, env=ascdsenv2)
##cmd = 'idl ./run_telem > out2'
##os.system(cmd)
##cmd = 'rm ./run_telem'
##os.system(cmd)
cmd = 'cat ./Telem/header ./Telem/telem.txt > temp'
os.system(cmd)
cmd = 'mv temp ./Telem/telem.txt'
os.system(cmd)
cmd = 'cd ./Telem; perl ./telem.pl ./telem.txt ./telem.html > zzz'
os.system(cmd)
cmd = 'cd ./Telem; perl ./zx_find_error2.perl zzz > zx_error_list'
os.system(cmd)
cmd = 'cd ./Telem; perl ./zx_remove.perl > ./clean_list'
os.system(cmd)
cmd = 'cd ./Telem; perl ./reduce_entry.perl'
os.system(cmd)
cmd = 'cd ./Telem; perl ./telem.pl ./reduced_clean_list telem.html'
os.system(cmd)
os.system(cmd)
f = open('./Telem/telem.html', 'r')
tdata = f.read()
f.close()
cmd = 'mv ./Telem/telem.html ' + outdir
os.system(cmd)
cmd = ' cd ./Telem; rm -rf zzz zx_error_list ./clean_list ./reduced_clean_list ./header ./telem.txt ./run_telem'
os.system(cmd)
return tdata
#---------------------------------------------------------------------------------
#-- set_trend_data_input: create trend data table input --
#---------------------------------------------------------------------------------
def set_trend_data_input(tdate):
"""
create trend data table input
input: tdate --- date of the report
output: out --- string of the html table of the trend
"""
#
#--- find which data to read from the report date
#
title = find_input_title(tdate)
#
#--- read msid lists
#
title = title.replace(' ', '_')
ltitle = title.lower()
file = tdir + 'Headers/Dsave/' + str(ltitle)
f= open(file, 'r')
data = [line.strip() for line in f.readlines()]
f.close()
#
#--- read header file
#
file = tdir + '/Headers/' + str(title)
f= open(file, 'r')
hdata = [line.strip() for line in f.readlines()]
f.close()
#
#--- create a dictionary which contains table name (e.g.acistemp.html) as key
#--- and head lines as data
#
chk = 0
hdict = {}
for ent in hdata:
if chk == 0:
atemp = re.split('.html', ent)
fname = atemp[0]
line = ''
chk = 1
else:
if ent == "<-->":