aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/src/doecslit/sproc.cl
blob: 8caeadd946b3ed00714225c1c5028ea424018725 (plain) (blame)
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
# SPROC -- Process echelle slit spectra
# This program combines all the operations of scattered light
# subtraction, extraction, dispersion correction, extinction correction,
# and flux calibration in as simple and noninteractive manner as
# possible.  The data must all share the same position on the 2D image
# and the same dispersion solution apart from small instrumental changes
# which can be followed automatically.

procedure sproc (objects, apref, arcs, arctable, standards, recenter,
	resize, quicklook, trace, scattered, arcap, dispcor, extcor,
	fluxcal, splot, redo, update, batch, listonly)

file	objects			{prompt="List of object spectra"}

file	apref			{prompt="Aperture reference spectrum"}
file	arcs			{prompt="List of arc spectra"}
file	arctable		{prompt="Arc assignment table (optional)"}
file	standards		{prompt="List of standard star spectra\n"}

bool	recenter		{prompt="Recenter object apertures?"}
bool	resize			{prompt="Resize object apertures?"}
bool	quicklook		{prompt="Edit/review object apertures?"}
bool	trace			{prompt="Trace object spectra?"}
bool	scattered		{prompt="Subtract scattered light?"}
bool	arcap			{prompt="Use object apertures for arcs?"}
bool	dispcor			{prompt="Dispersion correct spectra?"}
bool	extcor			{prompt="Extinction correct spectra?"}
bool	fluxcal			{prompt="Flux calibrate spectra?"}
bool	splot			{prompt="Plot the final spectrum?"}
bool	redo			{prompt="Redo operations if previously done?"}
bool	update			{prompt="Update spectra if cal data changes?"}
bool	batch			{prompt="Extract objects in batch?"}
bool	listonly		{prompt="List steps but don't process?\n"}

real	datamax = INDEF		{prompt="Max data value / cosmic ray threshold"}

string	anssplot = "yes"	{prompt="Splot spectrum?", mode="q",
				 enum="no|yes|NO|YES"}
bool	newaps, newdisp, newsens, newarcs
bool	fluxcal1, splot1, splot2
bool	dobatch

struct	*fd1, *fd2, *fd3

begin
	string	imtype, ectype
	string	arcref, spec, arc
	string	arcrefec, specec, arcec
	string	temp, done
	string	str1, str2, str3, str4, arcrefs, log1, log2
	bool	reextract, extract, scat, disp, ext, flux, log, disperr
	int	i, j, n
	struct	err
	str1 = ""

	# Call a separate task to do the listing to minimize the size of
	# this script and improve it's readability.

	dobatch = no
	if (listonly) {
	    slistonly (objects, apref, arcs, standards, scattered,
		dispcor, extcor, fluxcal, redo, update)
	    bye
	}

	imtype = "." // envget ("imtype")
	i = stridx (",", imtype)
	if (i > 0)
	    imtype = substr (imtype, 1, i-1)
	ectype = ".ec" // imtype
	n = strlen (imtype)

	# Temporary files used repeatedly in this script.  Under some
	# abort circumstances these files may be left behind.

	temp = mktemp ("tmp$iraf")
	done = mktemp ("tmp$iraf")

	# Rather than always have switches on the logfile and verbose flags
	# we use TEE and set a file to "dev$null" if output is not desired.
	# We must check for the null string to signify no logfile.

	tee.append = yes
	if (logfile == "")
	    log1 = "dev$null"
	else
	    log1 = logfile
	if (verbose)
	    log2 = "STDOUT"
	else
	    log2 = "dev$null"

	# If the update switch is used changes in the calibration data
	# can cause images to be reprocessed (if they are in the object
	# list).  Possible changes are in the aperture definitions,
	# dispersion solution, and sensitivity function.  The newarcs
	# flag is used to only go through the arc image headers once
	# setting the reference spectrum, airmass, and UT.

	newaps = no
	newdisp = no
	newsens = no
	newarcs = yes
	fluxcal1 = fluxcal

	# Check if there are aperture definitions in the database and
	# define them if needed.  This is usually somewhat interactive.
	# Set the newaps flag in case an update is desired.

	# Initialize APSCRIPT for aperture reference.
	apslitproc.saturation = INDEF
	apslitproc.references = ""
	apslitproc.ansfind = "YES"
	if (recenter)
	    apslitproc.ansrecenter = "YES"
	else
	    apslitproc.ansrecenter = "NO"
	if (resize)
	    apslitproc.ansresize = "YES"
	else
	    apslitproc.ansresize = "NO"
	apslitproc.ansedit = "yes"
	apslitproc.anstrace = "YES"
	apslitproc.ansfittrace = "yes"
	apslitproc.ansextract = "NO"

	i = strlen (apref)
	if (i > n && substr (apref, i-n+1, i) == imtype)
	    apref = substr (apref, 1, i-n)

	reextract = redo
	if (reextract || !access (database // "/ap" // apref)) {
	    if (!access (apref // imtype)) {
		printf ("Aperture reference spectrum not found - %s%s\n",
		    apref, imtype) | scan (err)
		error (1, err // "\nCheck setting of imtype")
	    }
	    scat = no
	    if (scattered) {
		hselect (apref, "apscatte", yes, > temp)
		fd1 = temp
		if (fscan (fd1, str1) < 1)
		    scat = yes
		fd1 = ""; delete (temp, verify=no)
	    }

	    print ("Set reference aperture for ", apref) | tee (log1)
	    delete (database//"/ap"//apref, verify=no, >& "dev$null")
	    apslitproc (apref)
	    newaps = yes
	}

	# Initialize APSCRIPT for aperture definitions.
	if (quicklook) {
	    apslitproc.ansedit = "NO"
	    apslitproc.ansfittrace = "NO"
	}
	if (trace) {
	    apslitproc.anstrace = "yes"
	} else {
	    apslitproc.anstrace = "NO"
	}
	apslitproc.ansextract = "NO"
	apslitproc.ansscat = "NO"

	print ("Define object apertures", >> log1)
	if (redo)
	    apslitproc ("@"//objects, references=apref)
	else
	    apslitproc ("@"//objects, references="NEW"//apref)
	if (dispcor && fluxcal1) {
	    if (redo)
		apslitproc ("@"//standards, references=apref)
	    else
		apslitproc ("@"//standards, references="NEW"//apref)
	}

	# Initialize APSCRIPT for extraction and SPLOT.
	apslitproc.ansrecenter = "NO"
	apslitproc.ansresize = "NO"
	apslitproc.ansedit = "NO"
	apslitproc.anstrace = "NO"
	apslitproc.ansextract = "YES"
	apslitproc.ansreview = "NO"
	apslitproc.ansscat = "NO"
	apslitproc.anssmooth = "YES"

	if (splot && !quicklook) {
	    splot1 = yes
	    splot2 = yes
	} else {
	    splot1 = no
	    splot2 = no
	}

	# The next step is to setup the scattered light correction if needed.
	# We use the aperture reference image for the interactive setting.
	# If this image has been  scattered light corrected we assume the
	# scattered light functions parameters are correctly set.

	scat = no
	if (scattered) {
	    hselect (apref, "apscatte", yes, > temp)
	    fd1 = temp
	    if (fscan (fd1, str1) < 1)
		scat = yes
	    fd1 = ""; delete (temp, verify=no)
	}
	if (scat) {
	    print ("Setup and do scattered light subtraction in ", apref) |
		tee (log1)
	    apslitproc.ansfitscatter = "yes"
	    apslitproc.ansfitsmooth = "yes"
	    apslitproc (apref, ansextract="NO", ansscat="YES")
	    apslitproc.ansfitscatter = "NO"
	    apslitproc.ansfitsmooth = "NO"
	}

	# If not dispersion correcting we can go directly to extracting
	# the object spectra.  The reference arcs are the first on
	# the arc lists.  The processing of the reference arcs is done
	# by the task ARCREFS.

	arcref = ""
	arcrefs = ""
	if (dispcor) {
	    if (arctable == "")
		arcrefs = "@"//arcs
	    else
		arcrefs = arctable

	    fd1 = arcs
	    if (fscan (fd1, arcref) == EOF)
		error (1, "No reference arcs")
	    fd1 = ""
	    if (!access (arcref // imtype)) {
		printf ("Arc reference spectrum not found - %s%s\n",
		    arcref, imtype) | scan (err)
		error (1, err // "\nCheck setting of imtype")
	    }
	    arcrefec = arcref // ectype
	    reextract = redo || (update && newaps)
	    if (reextract && access (arcrefec))
	        imdelete (arcrefec, verify=no)

	    apslitproc.references = apref
	    sarcrefs (arcref, done, log1, log2)
	    apslitproc.references = ""

	    if (fluxcal1)
		sfluxcal (standards, arcs, arcref, arcrefs, redo, update,
		    scattered, arcap, extcor, done, log1, log2)
	}

	# Now we are ready to process the object spectra.

	reextract = redo || (update && (newaps || newdisp))
	fd1 = objects
	while (fscan (fd1, spec) != EOF) {
	    # Check if previously done; i.e. arc.
	    if (access (done)) {
	        fd2 = done
	        while (fscan (fd2, specec) != EOF)
		    if (spec == specec)
		        break
	        if (spec == specec)
		    next
	        fd2 = ""
	    }
	    if (!access (spec // imtype)) {
		printf ("Object spectrum not found - %s%s\n",
		    spec, imtype) | scan (err)
		print (err) | tee (log1)
		print ("Check setting of imtype")
		next
	    }
	    specec = spec // ectype

	    # Determine required operations from the flags and image header.
	    scat = no
	    extract = no
	    disp = no
	    ext = no
	    flux = no
	    if (scattered) {
		hselect (spec, "apscatte", yes, > temp)
		fd2 = temp
		if (fscan (fd2, str1) < 1)
		    scat = yes
		fd2 = ""; delete (temp, verify=no)
	    }
	    if (reextract || !access (specec) || (update && scat))
		extract = yes
	    else {
		hselect (specec, "dc-flag", yes, > temp)
		hselect (specec, "ex-flag", yes, >> temp)
		hselect (specec, "ca-flag", yes, >> temp)
		fd2 = temp
		if (fscan (fd2, str1) == 1) {
		    extract = update && newdisp
		    if (update && !newdisp)
		        # We really should check if REFSPEC will assign
			# different reference spectra.
			;
		} else
		    disp = dispcor
		if (fscan (fd2, str1) == 1)
		    extract = update && !extcor
		else
		    ext = extcor
		if (fscan (fd2, str1) == 1)
		    extract = update && (!fluxcal1 || newsens)
		else
		    flux = fluxcal1
		fd2 = ""; delete (temp, verify=no)
	    }

	    if (extract) {
		disp = dispcor
		ext = extcor
		flux = fluxcal1
	    }

	    # If fully processed go to the next object.
	    if (!extract && !disp && !extcor && !flux)
		next

	    # If not interactive and the batch flag is set submit rest to batch.
	    if (batch && !splot1 && !splot2) {
		fd1 = ""
		flprcache
		sbatch.objects = objects
		sbatch.datamax = datamax
		sbatch.arcs = arcs
		sbatch.arcref = arcref
		sbatch.arcrefs = arcrefs
		sbatch.done = done
		sbatch.logfile = log1
		sbatch.redo = reextract
		sbatch.update = update
		sbatch.scattered = scattered
		sbatch.arcap = arcap
		sbatch.dispcor = dispcor
		sbatch.fluxcal1 = fluxcal1
		sbatch.extcor = extcor
		sbatch.newaps = newaps
		sbatch.newdisp = newdisp
		sbatch.newsens = newsens
		sbatch.newarcs = newarcs
		dobatch = yes
		return
	    }

	    # Process the spectrum in foreground.
	    if (extract) {
		if (access (specec))
		    imdelete (specec, verify=no)

		if (scat) {
		    print ("Subtract scattered light in ", spec) | tee (log1)
		    apslitproc (spec, ansextract="NO", ansscat="YES")
		}

		print ("Extract object spectrum ", spec) | tee (log1)
		hselect (spec, "date-obs,ut,exptime", yes, > temp)
		hselect (spec, "ra,dec,epoch,st", yes, >> temp)
		fd2 = temp
		if (fscan (fd2, str1, str2, str3) == 3) {
		    setjd (spec, observatory=observatory, date="date-obs",
			time="ut", exposure="exptime", jd="jd", hjd="",
			ljd="ljd", utdate=yes, uttime=yes, listonly=no,
			>> log1)
		    if (fscan (fd2, str1, str2, str3, str4) == 4)
			setairmass (spec, intype="beginning",
			    outtype="effective", exposure="exptime",
			    observatory=observatory, show=no, update=yes,
			    override=yes, >> log1)
		}
		fd2 = ""; delete (temp, verify=no)
		apslitproc (spec, saturation=datamax)
	    }

	    disperr = no
	    if (disp) {
		# Fix arc headers if necessary.
		if (newarcs) {
	    	    fd2 = arcs
	    	    while (fscan (fd2, arc) != EOF) {
			hselect (arc, "date-obs,ut,exptime", yes, > temp)
			hselect (arc, "ra,dec,epoch,st", yes, >> temp)
			fd3 = temp
			if (fscan (fd3, str1, str2, str3) == 3) {
			    setjd (arc, observatory=observatory,
				date="date-obs", time="ut", exposure="exptime",
				jd="jd", hjd="", ljd="ljd", utdate=yes,
				uttime=yes, listonly=no, >> log1)
			    if (fscan (fd3, str1, str2, str3, str4) == 4)
				setairmass (arc, intype="beginning",
				    outtype="effective", exposure="exptime",
				    observatory=observatory, show=no,
				    update=yes, override=yes, >> log1)
			}
			fd3 = ""; delete (temp, verify=no)
	        	hedit (arc, "refspec1", arc, add=yes, verify=no,
		    	    show=no, update=yes)
		    }
	    	    fd2 = ""
		    newarcs = no
		}

		print ("Assign arc spectra for ", spec) | tee (log1)
		refspectra (spec, references=arcrefs,
		    apertures="", refaps="", ignoreaps=no,
		    select=sparams.select, sort=sparams.sort,
		    group=sparams.group, time=sparams.time,
		    timewrap=sparams.timewrap, override=yes, confirm=no,
		    assign=yes, logfiles="STDOUT", verbose=no) |
		    tee (log1, > log2)

		sdoarcs (spec, arcref, reextract, arcap, log1, no)

	        hselect (specec, "refspec1", yes, > temp)
	        fd2 = temp
	        i = fscan (fd2, arc)
	        fd2 = ""; delete (temp, verify=no)
		if (i < 1) {
		    print ("No arc reference assigned for ", spec) | tee (log1)
		    disperr = yes
		} else {
	            print ("Dispersion correct ", spec) | tee (log1)
		    dispcor (specec, "", linearize=sparams.linearize,
			database=database, table=arcref//ectype,
			w1=INDEF, w2=INDEF, dw=INDEF, nw=INDEF,
			log=sparams.log, flux=sparams.flux, samedisp=no,
			global=no, confirm=no, ignoreaps=no, listonly=no,
			logfile=logfile)
		    hedit (specec, "dc-flag", 0, add=yes, show=no,
			verify=no, update=yes)
		}
	    }

	    if (!disperr && (extract || disp)) {
	        if (ext)
		    print ("Extinction correct ", spec) | tee (log1)
	        if (flux)
		    print ("Flux calibrate ", spec) | tee (log1)
	        if (flux || ext)
		    calibrate (specec, "", extinct=extcor, flux=fluxcal1,
			extinction=extinction, observatory=observatory,
			ignoreaps=no, sensitivity="sens", fnu=sparams.fnu) |
			tee (log1, > log2)
	    }
	    if (extract || disp || ext || flux) {
		if (splot1) {
		    print (specec, ":")
		    str1 = anssplot
		    if (str1 == "NO" || str1 == "YES")
			splot1 = no
		    if (str1 == "no" || str1 == "NO")
			splot2 = no
		    else
			splot2 = yes
		}
		if (splot2)
		    splot (specec)
		else if (splot && quicklook) {
		    if (disp) {
			print ("q") |
			specplot (specec, apertures="", autolayout=no,
			    scale=1., offset=0., step=0., sysid=yes,
			    yscale=yes, xmin=INDEF, xmax=INDEF, ymin=INDEF,
			    ymax=INDEF, logfile="", graphics="stdgraph",
			    cursor="STDIN")
		    } else {
			print ("q") |
			specplot (specec, apertures="", autolayout=yes,
			    autoscale=no, scale=1., offset=0., step=0.,
			    sysid=yes, yscale=no, xmin=INDEF, xmax=INDEF,
			    ymin=INDEF, ymax=INDEF, logfile="",
			    graphics="stdgraph", cursor="STDIN")
		    }
		}
	    }
	    print (spec, >> done)
	}
	fd1 = ""

	if (access (done))
	    delete (done, verify=no)
end