-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_prog.py
More file actions
165 lines (130 loc) · 5.32 KB
/
create_prog.py
File metadata and controls
165 lines (130 loc) · 5.32 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
from astropy.io import fits
from urllib.request import urlopen
from dep_obtain import get_obtain_data
from common import *
def create_prog(instrObj):
'''
Creates a temporary staging data file "createprog.txt' listing all
program information for each file. This file is input to getProgInfo.py.
The following data are written out one value per line per fits file:
file
utdate
utc
outdir
observer
frameno
imagetyp
progid
progpi
proginst
progtitl
oa
<repeats here>
@type instrObj: instrument
@param instr: The instrument object
'''
#short vars
instr = instrObj.instr
utDate = instrObj.utDate
stageDir = instrObj.dirs['stage']
log = instrObj.log
#info
if log: log.info('create_prog: Getting FITS file information')
# Get OA from dep_obtain file
obFile = stageDir + '/dep_obtain' + instr + '.txt'
obData = get_obtain_data(obFile)
oa = ''
if len(obData) >= 1: oa = obData[0]['OA']
# Get all files
fileList = []
locateFile = stageDir + '/dep_locate' + instr + '.txt'
with open(locateFile, 'r') as loc:
for item in loc:
fileList.append(item.strip())
# loop through files and add data to createprog.txt
outfile = stageDir + '/createprog.txt'
with open(outfile, 'w') as ofile:
for filename in fileList:
#skip blank lines
if filename.strip() == '': continue
#skip OSIRIS files that end in 'x'
if instr == 'OSIRIS':
if filename[-1] == 'x':
log.info(filename + ': file ends with x')
continue
#load fits into instrObj
#todo: Move all keyword fixes as standard steps done upfront?
instrObj.set_fits_file(filename)
# Temp fix for bad file times (NIRSPEC legacy)
instrObj.fix_datetime(filename)
#get image type
instrObj.set_koaimtyp()
imagetyp = instrObj.get_keyword('KOAIMTYP')
#get date-obs
instrObj.set_dateObs()
dateObs = instrObj.get_keyword('DATE-OBS', False)
#get utc
instrObj.set_utc()
utc = instrObj.get_keyword('UTC', False)
#get observer
observer = instrObj.get_keyword('OBSERVER')
if observer == None: observer = 'None'
observer = observer.strip()
#get fileno
fileno = instrObj.get_fileno()
#get outdir
outdir = instrObj.get_outdir()
#lop off everything before /sdata
# fileparts = filename.split('/sdata')
# if len(fileparts) > 1: newFile = '/sdata' + fileparts[-1]
# else : newFile = filename
#TODO: NOTE: removing this string split since is causing problems with new code and I don't think it is necessary
newFile = filename
# Get the semester
instrObj.set_semester()
sem = instrObj.get_keyword('SEMESTER')
sem = sem.strip()
#write out vars to file, one line each var
newFile = newFile.replace('//','/')
ofile.write(newFile+'\n')
ofile.write(dateObs+'\n')
ofile.write(utc+'\n')
ofile.write(outdir+'\n')
ofile.write(observer+'\n')
ofile.write(str(fileno)+'\n')
ofile.write(imagetyp+'\n')
#if PROGNAME exists (either assigned from command line or in PROGNAME), use that to populate the PROG* values
#NOTE: PROGNAME can be in format with or without semester
if instrObj.config['MISC']['ASSIGN_PROGNAME']:
progname = get_progid_assign(instrObj.config['MISC']['ASSIGN_PROGNAME'], utc)
if log: log.info(f"Force assigning {os.path.basename(newFile)} to PROGID '{progname}'")
else:
progname = instrObj.get_keyword('PROGNAME')
if progname != None: progname = progname.replace('ToO_', '')
#valid progname?
isProgValid = is_progid_valid(progname)
if progname and not isProgValid:
if log: log.warn('create_prog: Invalid PROGNAME: ' + str(progname))
#try to assign PROG* keywords from progname
progid = 'PROGID'
progpi = 'PROGPI'
proginst = 'PROGINST'
progtitl = 'PROGTITL'
if isProgValid:
progname = progname.strip().upper()
if progname == 'ENG':
progid = 'ENG'
else:
if '_' in progname: sem, progname = progname.split('_')
semid = sem + '_' + progname
progid = progname
progpi = get_prog_pi (semid, 'PROGPI' , log)
proginst = get_prog_inst (semid, 'PROGINST', log)
progtitl = get_prog_title(semid, 'PROGTITL', log)
ofile.write(progid + '\n')
ofile.write(progpi + '\n')
ofile.write(proginst + '\n')
ofile.write(progtitl + '\n')
#write OA last
ofile.write(oa + '\n')
if log: log.info('create_prog: finished, {} created'.format(outfile))