-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstrument.py
More file actions
1229 lines (952 loc) · 41.6 KB
/
instrument.py
File metadata and controls
1229 lines (952 loc) · 41.6 KB
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
"""
The parent class for all the instruments to streamline big picture things
Contains basic keyword values common across all the instruments
Children will contain the instrument specific values
12/14/2017 M. Brown - Created initial file
"""
#import datetime as dt
import os
from common import *
from astropy.io import fits
from datetime import timedelta, datetime as dt
from envlog import *
import shutil
import create_log as cl
from verification import *
import urllib.request
import json
import numpy as np
import re
from dep_obtain import get_obtain_data
import math
import db_conn
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from PIL import Image
from astropy.visualization import ZScaleInterval, AsinhStretch
from astropy.visualization.mpl_normalize import ImageNormalize
class Instrument:
def __init__(self, instr, utDate, config, log=None):
"""
Base Instrument class to hold all the values common between
instruments. Contains default values where possible
but null values should be replaced in the init of the
subclasses.
@param instr: instrument name
@type instr: string
@param utDate: UT date of observation
@type utDate: string (YYYY-MM-DD)
@param config: config object containing configration variables
@type config: config object
@type log: Logger Object
@param log: The log handler for the script. Writes to the logfile
"""
#class inputs
self.instr = instr
self.utDate = utDate
self.config = config
self.log = log
# Keyword values to be used with a FITS file during runtime
# NOTE: array may be used to denote an ordered list of possible keywords to look for.
# NOTE: these may be overwritten by instr_*.py
self.keywordMap = {}
self.keywordMap['INSTRUME'] = 'INSTRUME'
self.keywordMap['UTC'] = 'UTC'
self.keywordMap['DATE-OBS'] = 'DATE-OBS'
self.keywordMap['SEMESTER'] = 'SEMESTER'
self.keywordMap['OFNAME'] = 'OUTFILE'
self.keywordMap['FRAMENO'] = 'FRAMENO'
self.keywordMap['OUTDIR'] = 'OUTDIR'
self.keywordMap['FTYPE'] = 'INSTR' # For instruments with two file types
# Other values that can be overwritten in instr-*.py
self.endHour = '20:00:00' # 24 hour period start/end time (UT)
# Values to be populated by subclass
self.prefix = ''
self.rawfile = ''
self.koaid = ''
self.sdataList = []
self.extraMeta = {}
self.keywordSkips = []
#init fits specific vars
self.fitsHdu = None
self.fitsHeader = None
self.fitsFilepath = None
#other helpful vars
self.rootDir = self.config[self.instr]['ROOTDIR']
if self.rootDir.endswith('/'): self.rootDir = self.rootDir[:-1]
self.utDateDir = self.utDate.replace('/', '-').replace('-', '')
# Verify input parameters
verify_instrument(self.instr.upper())
verify_date(self.utDate)
assert os.path.isdir(self.rootDir), 'rootDir does not exist'
#create db conn obj
self.db = db_conn.db_conn('config.live.ini', configKey='DATABASE', persist=True)
def __del__(self):
"""
Close the database connection, if it is open
"""
if self.db:
self.db.close()
#abstract methods that must be implemented by inheriting classes
def get_dir_list(self) : raise NotImplementedError("Abstract method not implemented!")
def get_prefix(self) : raise NotImplementedError("Abstract method not implemented!")
def set_koaimtyp(self) : raise NotImplementedError("Abstract method not implemented!")
def dep_init(self, fullRun=True):
'''
Perform specific initialization tasks for DEP processing.
'''
#TODO: exit if existence of output/stage dirs? Maybe put override in config?
#store config
self.telUrl = self.config['API']['TELAPI']
self.metadataTablesDir = self.config['MISC']['METADATA_TABLES_DIR']
#check and create dirs
self.init_dirs(fullRun)
#create log if it does not exist
if not self.log:
self.log = cl.create_log(self.rootDir, self.instr, self.utDate, True)
self.log.info('instrument.py: log created')
#create README (output dir with everything before /koadata##/... stripped off)
readmeFile = self.dirs['output'] + '/README';
with open(readmeFile, 'w') as f:
path = self.dirs['output']
# match = re.search( r'.*(/.*/.*/\d\d\d\d\d\d\d\d)$', path, re.M)
# if match: path = match.groups(0)[0]
f.write(path + '\n')
def init_dirs(self, fullRun=True):
# get the various root dirs
self.dirs = get_root_dirs(self.rootDir, self.instr, self.utDate)
# Create the output directories, if they don't already exist.
# Unless this is a full pyDEP run, in which case we exit with warning
for key, dir in self.dirs.items():
if os.path.isdir(dir):
if fullRun and key != 'process':
raise Exception('instrument.py: Full pyDEP run, but staging and/or output directories already exist')
else:
try:
os.makedirs(dir)
except:
raise Exception('instrument.py: could not create directory: {}'.format(dir))
# Additions for NIRSPEC
# TODO: move this to instr_nirspec.py?
if self.instr == 'NIRSPEC':
for dir in ['scam', 'spec']:
newdir = self.dirs['lev0'] + '/' + dir
if not os.path.isdir(newdir):
os.mkdir(newdir)
def set_fits_file(self, filename):
'''
Sets the current FITS file we are working on. Clears out temp fits variables.
'''
#todo: should we have option to just read the header for performance if that is all that is needed?
try:
self.fitsHdu = fits.open(filename, ignore_missing_end=True)
self.fitsHeader = self.fitsHdu[0].header
# print('hdu0',type(self.fitsHeader
# print('hdu1',type(self.fitsHdu[1]))
#self.fitsHeader = fits.getheader(filename)
self.fitsFilepath = filename
except:
self.log.warning('set_fits_file: Could not read FITS file "' + filename + '"!')
return False
self.koaid = '';
self.rawfile = ''
self.prefix = ''
self.extraMeta = {}
return True
def get_keyword(self, keyword, useMap=True, default=None, ext=None):
'''
Gets keyword value from the FITS header as defined in keywordMap class variable.
NOTE: FITS file must be loaded first with self.set_fits_file
@param keyword: keyword value or ordered list of keyword values to search for
@type instr: string or list
'''
# check for loaded fitsHeader
if ext is None:
if not self.fitsHeader:
raise Exception('get_keyword: ERROR: no FITS header loaded')
return default
else:
if not self.fitsHdu[ext].header:
raise Exception('get_keyword: ERROR: no FITS header loaded')
return default
#use keyword mapping?
if useMap:
#if keyword is mapped, then use mapped value(s)
if isinstance(keyword, str) and keyword in self.keywordMap:
keyword = self.keywordMap[keyword]
#We allow an array of mapped keys, so if keyword is still a string then put in array
mappedKeys = keyword
if isinstance(mappedKeys, str):
mappedKeys = [mappedKeys]
#loop
if ext is None:
fitshead = self.fitsHeader
else:
fitshead = self.fitsHdu[ext].header
for mappedKey in mappedKeys:
try:
val = fitshead.get(mappedKey)
if self._chk_valid(val):
return val
except fits.verify.VerifyError:
continue
return default
@staticmethod
def _chk_valid(val):
if val == 0:
return True
if (not val or isinstance(val, fits.Undefined) or
str(val).strip().lower() in ('nan', '-nan')):
return False
return True
def set_keyword(self, keyword, value, comment='', useMap=False, ext=None):
'''
Sets keyword value in FITS header.
NOTE: Mapped values are only used if "useMap" is set to True, otherwise keyword name is as provided.
NOTE: FITS file must be loaded first with self.set_fits_file
'''
# check for loaded fitsHeader
if not self.fitsHeader:
raise Exception('get_keyword: ERROR: no FITS header loaded')
return None
#use keyword mapping?
if useMap:
#NOTE: We allow an array of mapped keys, so if keyword is array, then use first value
if keyword in self.keywordMap:
keyword = self.keywordMap[keyword]
#NOTE: If keyword is mapped to an array of key values, the first value will be used.
if isinstance(keyword, list):
keyword = keyword[0]
#handle infinite and NaN values
if value == math.inf or str(value).lower() in ('nan', '-nan'):
self.log.error(f'set_keyword: ERROR: keyword {keyword} value '
f'is {value}. Setting to null.')
return None
#ok now we can update
if ext == None:
self.fitsHeader.update({keyword : (value, comment)})
else:
(self.fitsHdu[ext].header).update({keyword : (value, comment)})
def is_fits_valid(self):
'''
Basic checks that the fits file is valid.
'''
#check no data
if len(self.fitsHdu) == 0:
self.log.error('is_fits_valid: No HDUs.')
return False
#any corrupted HDUs?
for hdu in self.fitsHdu:
hdu_type = str(type(hdu))
if 'CorruptedHDU' in hdu_type:
self.log.error('is_fits_valid: Corrupted HDU found.')
return False
return True
def set_koaid(self):
'''
Create and add KOAID to header if it does not already exist
'''
# self.log.info('set_koaid: setting KOAID keyword value')
#skip if it exists
if self.get_keyword('KOAID', False) != None: return True
#make it
koaid, result = self.make_koaid()
if not result:
self.log.warning('set_koaid: Could not create KOAID. UDF!')
return False
#save it
self.set_keyword('KOAID', koaid, 'KOA: Data file name')
return True
def make_koaid(self):
"""
Function to create the KOAID for the current loaded FITS file
Returns the koaid and TRUE if the KOAID is successfully created
"""
#TODO: see old/common.koaid() and make sure all logic is moved here or to instr_*.py
# Get the prefix for the correct instrument and configuration
self.prefix = self.get_prefix()
if self.prefix == '':
return '', False
# Extract the UTC time and date observed from the header
utc = self.get_keyword('UTC', False)
if utc == None: return '', False
dateobs = self.get_keyword('DATE-OBS')
if dateobs == None: return '', False
# Create a timedate object using the string from the header
try:
utc = dt.strptime(utc, '%H:%M:%S.%f')
except:
return '', False
# Extract the hour, minute, and seconds from the UTC time
hour = utc.hour
minute = utc.minute
second = utc.second
# Calculate the total number of seconds since Midnight
totalSeconds = str((hour * 3600) + (minute * 60) + second)
# Remove any date separators from the date
dateobs = dateobs.replace('-','')
dateobs = dateobs.replace('/','')
# Create the KOAID from the parts
koaid = self.prefix + '.' + dateobs + '.' + totalSeconds.zfill(5) + '.fits'
return koaid, True
def get_instr(self):
"""
Method to extract the name of the instrument from the INSTRUME keyword value
"""
# Extract the Instrume value from the header as lowercase
instr = self.get_keyword('INSTRUME')
if (instr == None) : return ''
instr = instr.lower()
# Split the value up into an array
instr = instr.split(' ')
# The instrument name should always be the first value
instr = instr[0].replace(':','')
return instr
def get_raw_fname(self):
"""
Determines the original filename
"""
#todo: is this function needed?
# Get the root name of the file
outfile = self.get_keyword('OFNAME')
if outfile == None: return '', False
# Get the frame number of the file
frameno = self.get_keyword('FRAMENO')
if frameno == None: return '', False
# Determine the necessary padding required
zero = ''
if float(frameno) < 10: zero = '000'
elif 10 <= float(frameno) < 100: zero = '00'
elif 100 <= float(frameno)< 1000: zero = '0'
# Construct the original filename
filename = outfile.strip() + zero + str(frameno).strip() + '.fits'
return filename, True
def set_instr(self):
'''
Check that value(s) in header indicates this is valid instrument and fixes if needed.
(ported to python from check_instr.pro)
'''
#todo: go over idl file again and pull out logic for other instruments
#self.log.info('set_instr: verifying this is a ' + self.instr + ' FITS file')
ok = False
#direct match (or starts with match)?
instrume = self.get_keyword('INSTRUME')
if instrume and instrume.startswith(self.instr):
if instrume != self.instr:
self.set_keyword('INSTRUME', self.instr, 'KOA: Instrument')
ok = True
#mira not ok
outdir = self.get_keyword('OUTDIR')
if (outdir and '/mira' in outdir) : ok = False
#No DCS keywords, check others
if (not ok):
filname = self.get_keyword('FILNAME')
if (filname and self.instr in filname): ok = True
outdir = self.get_keyword('OUTDIR')
if (outdir and self.instr in outdir.upper()): ok = True
if (outdir and 'sdata100' in outdir and outdir.endswith('fcs')): ok = True
currinst = self.get_keyword('CURRINST')
if (currinst and self.instr == currinst): ok = True
#if fixed, then update 'INSTRUME' in header
if ok:
self.set_keyword('INSTRUME', self.instr, 'KOA: Fixing INSTRUME keyword')
self.log.info('set_instr: fixing INSTRUME value')
#log err
if (not ok):
self.log.warning('set_instr: cannot determine if file is from ' + self.instr + '. UDF!')
return ok
def set_dateObs(self):
'''
Checks to see if we have a DATE-OBS keyword, and if it needs to be fixed or created.
'''
#try to get from header (unmapped or mapped)
dateObs = self.get_keyword('DATE-OBS', False)
if dateObs == None: dateObs = self.get_keyword('DATE-OBS')
#validate
valid = False
if dateObs:
dateObs = str(dateObs) #NOTE: sometimes we can get a number
dateObs = dateObs.strip()
valid = re.search('^\d\d\d\d[-]\d\d[-]\d\d', dateObs)
#fix slashes?
if not valid and '/' in dateObs:
orig = dateObs
day, month, year = dateObs.split('/')
if int(year)<50: year = '20' + year
else: year = '19' + year
dateObs = year + '-' + month + '-' + day
self.set_keyword('DATE-OBS', dateObs, 'KOA: Value corrected (' + orig + ')')
self.log.warning('set_dateObs: fixed DATE-OBS format (orig: ' + orig + ')')
valid = True
#if we couldn't match valid pattern, then build from file last mod time
#note: converting to universal time (+10 hours)
if not valid:
filename = self.fitsFilepath
lastMod = os.stat(filename).st_mtime
dateObs = dt.fromtimestamp(lastMod) + timedelta(hours=10)
dateObs = dateObs.strftime('%Y-%m-%d')
self.set_keyword('DATE-OBS', dateObs, 'KOA: Observing date')
self.log.warning('set_dateObs: set DATE-OBS value from FITS file time')
# If good match, just take first 10 chars (some dates have 'T' format and extra time)
if len(dateObs) > 10:
orig = dateObs
dateObs = dateObs[0:10]
self.set_keyword('DATE-OBS', dateObs, 'KOA: Value corrected (' + orig + ')')
self.log.warning('set_dateObs: fixed DATE-OBS format (orig: ' + orig + ')')
return True
def set_utc(self):
'''
Checks to see if we have a UTC time keyword, and if it needs to be fixed or created.
'''
#try to get from header unmapped and mark if update needed
update = False
utc = self.get_keyword('UTC', False)
if utc == None: update = True
#try to get from header mapped
if utc == None:
utc = self.get_keyword('UTC')
#validate
valid = False
if utc:
utc = str(utc) #NOTE: sometimes we can get a number
utc = utc.strip()
valid = re.search('^\d\d:\d\d:\d\d.\d\d', utc)
#if we couldn't match valid pattern, then build from file last mod time
#note: converting to universal time (+10 hours)
if not valid:
filename = self.fitsFilepath
lastMod = os.stat(filename).st_mtime
utc = dt.fromtimestamp(lastMod) + timedelta(hours=10)
utc = utc.strftime('%H:%M:%S.00')
update = True
self.log.warning(f'set_utc: set UTC value from FITS file time ({utc})')
#update/add if need be
if update:
self.set_keyword('UTC', utc, 'KOA: UTC keyword corrected')
return True
def set_ut(self):
#skip if it exists
if self.get_keyword('UT', False) != None: return True
#get utc from header
utc = self.get_keyword('UTC', False)
if utc == None:
self.log.warning('set_ut: Could not get UTC value. UDF!')
return False
#copy to UT
self.set_keyword('UT', utc, 'KOA: Observing time')
return True
def get_outdir(self):
'''
Returns outdir if keyword exists, else derive from filename
'''
#return by keyword index if it exists
outdir = self.get_keyword('OUTDIR')
if (outdir != None) : return outdir
#Returns the OUTDIR associated with the filename, else returns None.
#OUTDIR = [/s]/sdata####/account/YYYYmmmDD
#todo: should we look for '/s/' and subtract one from index?
#NOTE: for reprocessing old data that doesn't have OUTDIR keyword, this matches
#on /stage/ or /storageserver/ instead of /s/, which still gets the job done. not ideal.
try:
filename = self.fitsFilepath
start = filename.find('/s')
end = filename.rfind('/')
return filename[start:end]
except:
#todo: really return "None"?
return "None"
def get_fileno(self):
#todo: do we need this function instead of using keyword mapping? see subclass set_frameno
keys = self.fitsHeader
fileno = keys.get('FILENUM')
if (fileno == None): fileno = keys.get('FILENUM2')
if (fileno == None): fileno = keys.get('FRAMENO')
if (fileno == None): fileno = keys.get('IMGNUM')
if (fileno == None): fileno = keys.get('FRAMENUM')
return fileno
def set_prog_info(self, progData):
# self.log.info('set_prog_info: setting program information keywords')
#note: progData is also stored in newproginfo.txt output from getProgInfo.py
#find matching filename in array
dataKey = None
data = None
for key, progFile in enumerate(progData):
filepath = progFile['file']
if filepath in self.fitsFilepath:
dataKey = key
data = progFile
break
if data == None:
self.log.error('set_prog_info: Could not get program info. UDF!')
return False
#create keywords, deal with blank/undefined vals
assert 'progid' in data and data['progid'], 'PROGID not found.'
assert 'progpi' in data and data['progpi'], 'PROGPI not found.'
assert 'proginst' in data and data['proginst'], 'PROGINST not found.'
assert 'progtitl' in data and data['progtitl'], 'PROGTITL not found.'
if data['progid'] == 'PROGID' : data['progid'] = 'NONE'
if data['progpi'] == 'PROGPI' : data['progpi'] = 'NONE'
if data['proginst'] == 'PROGINST': data['proginst'] = 'NONE'
if data['progtitl'] in ('PROGTITL', 'NONE'): data['progtitl'] = ''
self.set_keyword('PROGID' , data['progid'] , 'KOA: Program ID')
self.set_keyword('PROGPI' , data['progpi'] , 'KOA: Program principal investigator')
self.set_keyword('PROGINST', data['proginst'], 'KOA: Program institution')
#extra warning for log
if data['progid'] == 'NONE':
time = self.get_keyword('DATE-OBS') + ' ' + self.get_keyword('UTC')
self.log.info(f"set_prog_info: PROGID is NONE for {os.path.basename(self.fitsFilepath)} (@{time})")
#enocde unicode chars in progtitl
title = data['progtitl']
title = title.encode('ascii', errors='xmlcharrefreplace').decode('utf8')
#divide PROGTITL into length 50 (+20 for comments) chunks PROGTL1/2/3
progtl1 = title[0:50]
progtl2 = title[50:100]
progtl3 = title[100:150]
self.set_keyword('PROGTL1', progtl1, 'Program title 1')
self.set_keyword('PROGTL2', progtl2, 'Program title 2')
self.set_keyword('PROGTL3', progtl3, 'Program title 3')
#NOTE: PROGTITL goes in metadata but not in header so we store in temp dict for later
self.extraMeta['PROGTITL'] = title
return True
def set_semester(self):
"""
Determines the Keck observing semester from the DATE-OBS keyword in header
and updates the SEMESTER keyword in header.
semester('2017-08-01') --> 2017A
semester('2017-08-02') --> 2017B
A = Feb. 2 to Aug. 1 (UT)
B = Aug. 2 to Feb. 1 (UT)
"""
#special override via command line option
assign_progname = self.config.get('MISC', {}).get('ASSIGN_PROGNAME')
if assign_progname:
utc = self.get_keyword('UTC', False)
progname = get_progid_assign(assign_progname, utc)
if '_' in progname and is_progid_valid(progname):
semester, progid = progname.split('_')
self.set_keyword('SEMESTER', semester, 'Calculated SEMESTER from PROGNAME')
self.log.info(f"set_semester: Set SEMESTER to '{semester}' from ASSIGN_PROGNAME '{progname}'")
return True
#special override assign using PROGNAME
progname = self.get_keyword('PROGNAME', default='')
if '_' in progname and is_progid_valid(progname):
semester, progid = progname.split('_')
self.set_keyword('SEMESTER', semester, 'Calculated SEMESTER from PROGNAME')
self.log.info(f"set_semester: Set SEMESTER to '{semester}' from PROGNAME '{progname}'")
return True
#normal assign using DATE-OBS and UTC
else:
dateObs = self.get_keyword('DATE-OBS', False)
utc = self.get_keyword('UTC', False)
if not dateObs or not utc:
self.log.error('set_semester: Could not parse DATE-OBS and UTC')
return False
#Slightly unintuitive, but get utc datetime obj and subtract 10 hours to convert to HST
#and another 10 for 10 am cutoff as considered next days observing.
d = dt.strptime(dateObs+' '+utc, "%Y-%m-%d %H:%M:%S.%f")
d = d - timedelta(hours=20)
#define cutoffs and see where it lands
#NOTE: d.year is wrong when date is Jan 1 and < 20:00:00,
#but it doesn't matter since we assume 'B' which is correct for Jan1
semA = dt.strptime(f'{d.year}-02-01 00:00:00.00', "%Y-%m-%d %H:%M:%S.%f")
semB = dt.strptime(f'{d.year}-08-01 00:00:00.00', "%Y-%m-%d %H:%M:%S.%f")
sem = 'B'
if d >= semA and d < semB: sem = 'A'
#adjust year if january
year = d.year
if d.month == 1: year -= 1
semester = f'{year}{sem}'
self.set_keyword('SEMESTER', semester, 'Calculated SEMESTER from DATE-OBS')
return True
def set_propint(self, progData):
'''
Set proprietary period length.
NOTE: This must come after set_semester() is called
'''
# self.log.info('set_propint: determining PROPINT value')
#create semid
semid = self.get_semid()
assert (semid != None), 'set_propint: Could not create SEMID.'
# Default to 18 for ENG data (***verify with SAs***)
progid = self.fitsHeader.get('PROGID').upper()
if progid == 'ENG':
propint = 18
else:
#create url and get data
#todo: test this
query = f'select propmin from koa_ppp where semid="{semid}" and utdate="{self.utDate}"'
data = self.db.query('koa', query, getOne=True)
if not data:
self.log.info('set_propint: PROPINT not found for ' + semid + ' and ' + self.utDate + ', defaulting to 18 months')
propint = 18
else:
propint = int(data['propmin'])
#NOTE: PROPINT goes in metadata but not in header so we store in temp dict for later
self.extraMeta['PROPINT'] = propint
return True
def set_datlevel(self, datlevel):
'''
Adds "DATLEVEL" keyword to header
'''
self.set_keyword('DATLEVEL' , datlevel, 'KOA: Data reduction level')
return True
def set_dqa_date(self):
"""
Adds date timestamp for when the DQA module was run
"""
dqa_date = dt.strftime(dt.now(), '%Y-%m-%dT%H:%M:%S')
self.set_keyword('DQA_DATE', dqa_date, 'KOA: Data quality assess time')
return True
def set_dqa_vers(self):
'''
Adds DQA version keyword to header
'''
version = self.config['INFO']['DEP_VERSION']
self.set_keyword('DQA_VERS', version, 'KOA: Data quality assess code version')
return True
def set_image_stats_keywords(self):
'''
Adds mean, median, std keywords to header
'''
# self.log.info('set_image_stats_keywords: setting image statistics keyword values')
image = self.fitsHdu[0].data
imageStd = float("%0.2f" % np.std(image))
imageMean = float("%0.2f" % np.mean(image))
imageMedian = float("%0.2f" % np.median(image))
self.set_keyword('IMAGEMN' , imageMean, 'KOA: Image data mean')
self.set_keyword('IMAGESD' , imageStd, 'KOA: Image data standard deviation')
self.set_keyword('IMAGEMD' , imageMedian, 'KOA: Image data median')
return True
def set_npixsat(self, satVal=None, ext=0):
'''
Determines number of saturated pixels and adds NPIXSAT to header
'''
# self.log.info('set_npixsat: setting pixel saturation keyword value')
if satVal == None:
satVal = self.get_keyword('SATURATE')
if satVal == None:
self.log.warning("set_npixsat: Could not find SATURATE keyword")
else:
image = self.fitsHdu[ext].data
pixSat = image[np.where(image >= satVal)]
nPixSat = len(image[np.where(image >= satVal)])
self.set_keyword('NPIXSAT', nPixSat, 'KOA: Number of saturated pixels',ext=ext)
return True
def set_oa(self):
'''
Adds observing assistant name to header
'''
# Get OA from dep_obtain file
obFile = self.dirs['stage'] + '/dep_obtain' + self.instr + '.txt'
obData = get_obtain_data(obFile)
oa = None
if len(obData) >= 1: oa = obData[0]['OA']
if oa == None:
self.log.warning("set_oa: Could not find OA data")
else:
self.set_keyword('OA', oa, 'KOA: Observing Assistant name')
return True
def set_ofName(self):
"""
Adds OFNAME keyword to header
"""
# self.log.info('set_ofName: setting OFNAME keyword value')
#get value
ofName = self.get_keyword('OFNAME')
if (ofName == None):
self.log.error('set_ofName: cannot find value for OFNAME')
return False
#add *.fits to output if it does not exist (to fix old files)
if (ofName.endswith('.fits') == False) : ofName += '.fits'
#update
self.set_keyword('OFNAME', ofName, 'KOA: Original file name')
return True
def set_weather_keywords(self):
'''
Adds all weather related keywords to header.
NOTE: DEP should not exit if weather files are not found
'''
# self.log.info('set_weather_keywords: setting weather keyword values')
#get input vars
dateobs = self.get_keyword('DATE-OBS')
utc = self.get_keyword('UTC', False)
telnr = self.get_telnr()
#get data but continue even if there were errors for certain keywords
data, errors, warns = envlog(telnr, dateobs, utc)
if type(data) is not dict:
self.log.error(f"Could not get weather data for {dateobs} {utc}")
return True
if len(errors) > 0:
self.log.error(f"EPICS archiver error for {dateobs} {utc}: {str(errors)}")
if len(warns) > 0:
self.log.info(f"EPICS archiver warn {dateobs} {utc}: {str(warns)}")
#set keywords
self.set_keyword('WXDOMHUM' , data['wx_domhum'], 'KOA: Weather dome humidity')
self.set_keyword('WXDOMTMP' , data['wx_domtmp'], 'KOA: Weather dome temperature')
self.set_keyword('WXDWPT' , data['wx_dewpoint'], 'KOA: Weather dewpoint')
self.set_keyword('WXOUTHUM' , data['wx_outhum'], 'KOA: Weather outside humidity')
self.set_keyword('WXOUTTMP' , data['wx_outtmp'], 'KOA: Weather outside temperature')
self.set_keyword('WXPRESS' , data['wx_pressure'], 'KOA: Weather pressure')
self.set_keyword('WXWNDIR' , data['wx_winddir'], 'KOA: Weather wind direction')
self.set_keyword('WXWNDSP' , data['wx_windspeed'], 'KOA: Weather wind speed')
self.set_keyword('WXTIME' , data['wx_time'], 'KOA: Weather measurement time')
self.set_keyword('GUIDFWHM' , data['guidfwhm'], 'KOA: Guide star FWHM value')
self.set_keyword('GUIDTIME' , data['fwhm_time'], 'KOA: Guide star FWHM measure time')
return True
def get_telnr(self):
'''
Gets telescope number for instrument via API
#todo: store this and skip api call if exists? Would need to clear var on fits reload.
#todo: Replace API call with hard-coded?
'''
url = self.telUrl + 'cmd=getTelnr&instr=' + self.instr.upper()
data = get_api_data(url, getOne=True)
telNr = int(data['TelNr'])
assert telNr in [1, 2], 'telNr "' + telNr + '"" not allowed'
return telNr
def write_lev0_fits_file(self):
#make sure we have a koaid
koaid = self.get_keyword('KOAID')
if (not koaid):
self.log.error('write_lev0_fits_file: Could not find KOAID for output filename.')
return False
#build outfile path
outfile = self.dirs['lev0']
if (koaid.startswith('NC')): outfile += '/scam'
elif (koaid.startswith('NS')): outfile += '/spec'
outfile += '/' + koaid
# already exists?
if os.path.isfile(outfile):
self.log.error('write_lev0_fits_file: file already exists. Duplicate KOAID?')
return False
#write out new fits file with altered header
try:
self.fitsHdu.writeto(outfile)
self.log.info('write_lev0_fits_file: output file is ' + outfile)
except:
try:
self.fitsHdu.writeto(outfile, output_verify='ignore')
self.log.info('write_lev0_fits_file: Forced to write FITS using output_verify="ignore". May want to inspect:' + outfile)
except Exception as e:
self.log.error('write_lev0_fits_file: Could not write out lev0 FITS file to ' + outfile)
self.log.info(str(e))
#If it someone still wrote something out, remove it
if os.path.isfile(outfile):
os.remove(outfile)
return False
self.set_filesize(outfile)
return True
def make_jpg(self):
'''
Make the jpg(s) for current fits file
'''
# Find fits file in lev0 dir to convert based on koaid
koaid = self.fitsHeader.get('KOAID')
fits_filepath = ''
for root, dirs, files in os.walk(self.dirs['lev0']):
if koaid in files:
fits_filepath = f'{root}/{koaid}'
if not fits_filepath:
self.log.error(f'make_jpg: Could not find KOAID: {koaid}')
return False
outdir = os.path.dirname(fits_filepath)
#call instrument specific create_jpg function
try:
self.log.info(f'make_jpg: Creating jpg from: {fits_filepath}')
self.create_jpg_from_fits(fits_filepath, outdir)
except Exception as e:
self.log.error(f'make_jpg: Could not create JPG from: {fits_filepath}')
self.log.error(e)
return False
return True
def create_jpg_from_fits(self, fits_filepath, outdir):
'''
Basic convert fits primary data to jpg. Instrument subclasses can override this function.
'''
#get image data
hdu = fits.open(fits_filepath, ignore_missing_end=True)
data = hdu[0].data