aboutsummaryrefslogtreecommitdiff
path: root/noao/obsutil/src/sptime/stdisperser.x
blob: 090cf0103bfc18d7a5a0ef4dcc243332b7b68278 (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
include	"sptime.h"

# These routines interface between any type of disperser and the
# disperser type specific routines (such as those for gratings).


# ST_DISPERSER -- Initialize disperser data.

procedure st_disperser (st, name, index)

pointer st		#I SPECTIME pointer
char	name[ARB]	#I Table name
int	index		#I Grating index

int	order, oref, stgeti(), tabgeti(), strdic()
real	f, phi, g, blaze, wb, db, ref, stgetr(), tabgetr(), gr_getr()
pointer sp, str, fname, gr, gr_open()
bool	streq()
errchk	gr_open, tab_getr, gr_getr, st_gtable1

begin
	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (fname, SZ_LINE, TY_CHAR)

	ST_GR(st,index) = NULL
	ST_DISPTYPE(st,index) = 0

	# Get disperser type.
	if (streq (name, "disperser")) {
	    call stgstr (st, "disptype", "disperser", "", Memc[str], SZ_LINE)
	    if (Memc[str] == EOS) {
		iferr (call tabgstr (ST_TAB(st), "disperser", "spectrograph",
		    "type", Memc[str], SZ_LINE))
		    call strcpy ("generic", Memc[str], SZ_LINE)
	    }
	} else if (streq (name, "xdisperser")) {
	    call stgstr (st, "xdisptype", "xdisperser", "", Memc[str], SZ_LINE)
	    if (Memc[str] == EOS) {
		iferr (call tabgstr (ST_TAB(st), "xdisperser", "spectrograph",
		    "type", Memc[str], SZ_LINE))
		    Memc[str] = EOS
	    }
	} else
	    Memc[str] = EOS
	ST_DISPTYPE(st,index) = strdic (Memc[str],Memc[str],SZ_LINE,DISPTYPES)

	switch (ST_DISPTYPE(st,index)) {
	case GRATING:
	    f = ST_CAMFL(st) * 1000
	    switch (index) {
	    case 1:
		g = stgetr (st, "gmm", name, INDEFR)
		blaze = stgetr (st, "blaze", name, INDEFR)
		oref = stgeti (st, "oref", name, 1)
		wb = stgetr (st, "wavelength", name, INDEFR)
		db = stgetr (st, "dispersion", name, INDEFR)
		ref = stgetr (st, "eff", name, INDEFR)
	    case 2:
		g = stgetr (st, "xgmm", name, INDEFR)
		blaze = stgetr (st, "xblaze", name, INDEFR)
		oref = stgeti (st, "xoref", name, 1)
		wb = stgetr (st, "xwavelength", name, INDEFR)
		db = stgetr (st, "xdispersion", name, INDEFR)
		ref = stgetr (st, "xeff", name, INDEFR)

		# Check old names.
		if (IS_INDEFR(g)) {
		    iferr (g = tabgetr (ST_TAB(st), name, "spectrograph",
			"gmm"))
		    g = INDEFR
		}
	    }

	    # Old names.
	    if (IS_INDEFR(g)) {
		iferr (g = tabgetr (ST_TAB(st), name, "spectrograph",
		    "gmm"))
		    g = INDEFR
	    }
	    if (IS_INDEFR(blaze)) {
		iferr (blaze = tabgetr (ST_TAB(st), name, "spectrograph",
		    "blaze"))
		    blaze = INDEFR
	    }
	    if (IS_INDEFI(oref)) {
		iferr (oref = tabgeti (ST_TAB(st), name, "spectrograph",
		    "oref"))
		    oref = 1
	    }
	    if (IS_INDEFR(wb)) {
		iferr (wb = tabgetr (ST_TAB(st), name, "spectrograph",
		    "wavelength"))
		    wb = INDEFR
	    }
	    if (IS_INDEFR(db)) {
		iferr (db = tabgetr (ST_TAB(st), name, "spectrograph",
		    "dispersion"))
		    db = INDEFR
	    }
	    if (IS_INDEFR(ref)) {
		iferr (ref = tabgetr (ST_TAB(st), name, "spectrograph",
		    "reflectance"))
		    ref = 1.
	    }

	    phi = ST_INOUTA(st,index)
#	    if (!IS_INDEFR(db))
#		db = db / f

	    iferr (gr = gr_open (ST_CW(st), ST_ORDER(st,index), ref, wb, db,
		oref, f, g, blaze, 1., phi, INDEF, INDEF, 1, YES)) {
		g = 300.
		blaze = 6
	        gr = gr_open (ST_CW(st), ST_ORDER(st,index), ref, wb, db,
		    oref, f, g, blaze, 1., phi, INDEF, INDEF, 1, YES)
	    }
	    ST_GR(st,index) = gr

	    if (IS_INDEF(ST_CW(st)))
		ST_CW(st,index) = gr_getr (gr, "wavelength")
	    if (IS_INDEFI(ST_ORDER(st,index)))
		ST_ORDER(st,index) = nint (gr_getr (gr, "order"))
	    ST_DISP(st,index) = gr_getr (gr, "dblaze") * oref * f

	    # Look for explicit blaze functions.
	    do order = ST_ORDER(st,index)-1, ST_ORDER(st,index)+1 {
		call sprintf (Memc[str], SZ_LINE, "eff%d")
		    call pargi (order)
		ifnoerr (call tabgstr (ST_TAB(st), name, "spectrograph",
		    Memc[str], Memc[fname], SZ_LINE)) {
		    if (streq (name, "disperser"))
			call st_gtable1 (st, Memc[str], Memc[fname])
		    else if (streq (name, "xdisperser")) {
			call sprintf (Memc[str], SZ_LINE, "xeff%d")
			    call pargi (order)
			call st_gtable1 (st, Memc[str], Memc[fname])
		    }
		}
	    }
	case GRISM:
	    f = ST_CAMFL(st) * 1000
	    switch (index) {
	    case 1:
		g = stgetr (st, "gmm", name, INDEFR)
		blaze = stgetr (st, "blaze", name, INDEFR)
		ref = stgetr (st, "eff", name, 1.)
		db = stgetr (st, "indexref", name, INDEFR)
		if (!IS_INDEFI(ST_ORDER(st,index)) && ST_ORDER(st,index)!=1) {
		    call sprintf (Memc[str], SZ_LINE, "index%d")
			call pargi (ST_ORDER(st,index))
		    iferr (wb = tabgetr (ST_TAB(st), name, "spectrograph",
			Memc[str]))
			wb = db
		    db = wb
		}
	    case 2:
		g = stgetr (st, "xgmm", name, INDEFR)
		blaze = stgetr (st, "xblaze", name, INDEFR)
		ref = stgetr (st, "xeff", name, 1.)
		db = stgetr (st, "xindexref", name, INDEFR)
		if (!IS_INDEFI(ST_ORDER(st,index)) && ST_ORDER(st,index)!=1) {
		    call sprintf (Memc[str], SZ_LINE, "index%d")
			call pargi (ST_ORDER(st,index))
		    iferr (wb = tabgetr (ST_TAB(st), name, "spectrograph",
			Memc[str]))
			wb = db
		    db = wb
		}
	    }

	    # Old names.
	    if (IS_INDEFR(g)) {
		iferr (g = tabgetr (ST_TAB(st), name, "spectrograph",
		    "gmm"))
		    g = INDEFR
	    }
	    if (IS_INDEFR(blaze)) {
		iferr (blaze = tabgetr (ST_TAB(st), name, "spectrograph",
		    "prism"))
		    blaze = INDEFR
	    }
	    if (IS_INDEFR(ref)) {
		iferr (ref = tabgetr (ST_TAB(st), name, "spectrograph",
		    "transmission"))
		    ref = 1
	    }
	    if (IS_INDEFR(db)) {
		if (!IS_INDEFI(ST_ORDER(st,index))) {
		    call sprintf (Memc[str], SZ_LINE, "index%d")
			call pargi (ST_ORDER(st,index))
		    iferr (db = tabgetr (ST_TAB(st), name, "spectrograph",
			Memc[str]))
			db = tabgetr (ST_TAB(st), name, "spectrograph",
			    "index1")
		} else
		    db = tabgetr (ST_TAB(st), name, "spectrograph", "index1")
	    }
	    oref = 1

	    iferr (gr = gr_open (ST_CW(st), ST_ORDER(st,index), ref, INDEF,
		INDEF, oref, f, g, blaze, db, 0., blaze, blaze, 1, YES)) {
		g = 300.
		blaze = 6.
		gr = gr_open (ST_CW(st), ST_ORDER(st,index), ref, INDEF,
		    INDEF, oref, f, g, blaze, db, 0., blaze, blaze, 1, YES)
	    }

	    ST_GR(st,index) = gr

	    if (IS_INDEF(ST_CW(st)))
		ST_CW(st,index) = gr_getr (gr, "wavelength")
	    if (IS_INDEFI(ST_ORDER(st,index)))
		ST_ORDER(st,index) = nint (gr_getr (gr, "order"))
	    ST_DISP(st,index) = gr_getr (gr, "dblaze") * oref * f

	    # Look for explicit blaze functions.
	    do order = ST_ORDER(st,index)-1, ST_ORDER(st,index)+1 {
		call sprintf (Memc[str], SZ_LINE, "eff%d")
		    call pargi (order)
		ifnoerr (call tabgstr (ST_TAB(st), name, "spectrograph",
		    Memc[str], Memc[fname], SZ_LINE)) {
		    if (streq (name, "disperser"))
			call st_gtable1 (st, Memc[str], Memc[fname])
		    else if (streq (name, "xdisperser")) {
			call sprintf (Memc[str], SZ_LINE, "xeff%d")
			    call pargi (order)
			call st_gtable1 (st, Memc[str], Memc[fname])
		    }
		}
	    }
	case GENERIC:
	    f = ST_CAMFL(st) * 1000
	    g = INDEFR
	    blaze = INDEFR
	    oref = 1

	    switch (index) {
	    case 1:
		g = INDEFR
		blaze = INDEFR
		oref = 1
		wb = stgetr (st, "wavelength", name, INDEFR)
		db = stgetr (st, "dispersion", name, INDEFR)
		ref = stgetr (st, "eff", name, INDEFR)
	    case 2:
		g = INDEFR
		blaze = INDEFR
		oref = 1
		wb = stgetr (st, "xwavelength", name, INDEFR)
		db = stgetr (st, "xdispersion", name, INDEFR)
		ref = stgetr (st, "xeff", name, INDEFR)
	    }

	    if (IS_INDEFR(wb)) {
		iferr (wb = tabgetr (ST_TAB(st), name, "spectrograph",
		    "wavelength"))
		    wb = INDEFR
	    }
	    if (IS_INDEFR(db)) {
		iferr (db = tabgetr (ST_TAB(st), name, "spectrograph",
		    "dispersion"))
		    db = INDEFR
	    }
	    if (IS_INDEFR(ref)) {
		iferr (ref = tabgetr (ST_TAB(st), name, "spectrograph",
		    "reflectance"))
		    ref = 1.
	    }

	    phi = ST_INOUTA(st,index)

	    gr = gr_open (ST_CW(st), ST_ORDER(st,index), ref, wb, db,
		oref, f, g, blaze, 1., phi, INDEF, INDEF, 1, NO)
	    ST_GR(st,index) = gr

	    if (IS_INDEF(ST_CW(st)))
		ST_CW(st,index) = gr_getr (gr, "wavelength")
	    if (IS_INDEFI(ST_ORDER(st,index)))
		ST_ORDER(st,index) = nint (gr_getr (gr, "order"))
	    ST_DISP(st,index) = gr_getr (gr, "dblaze") * oref * f

	    # Look for explicit blaze functions.
	    do order = ST_ORDER(st,index)-1, ST_ORDER(st,index)+1 {
		call sprintf (Memc[str], SZ_LINE, "eff%d")
		    call pargi (order)
		ifnoerr (call tabgstr (ST_TAB(st), name, "spectrograph",
		    Memc[str], Memc[fname], SZ_LINE)) {
		    if (streq (name, "disperser"))
			call st_gtable1 (st, Memc[str], Memc[fname])
		    else if (streq (name, "xdisperser")) {
			call sprintf (Memc[str], SZ_LINE, "xeff%d")
			    call pargi (order)
			call st_gtable1 (st, Memc[str], Memc[fname])
		    }
		}
	    }
	}

	call sfree (sp)
end


# ST_DISPEFF -- Return disperser efficiency.

real procedure st_dispeff (st, name, wave, order)

pointer st		#I SPECTIME pointer
char	name[ARB]	#I Table name
real	wave		#I Wavelength
int	order		#I Order
real	eff		#O Efficiency

real	w
pointer	sp, tab, str

int	tabgeti()
real	tabinterp1(), tabgetr(), gr_eff()
bool	tabexists(), streq()
errchk	tabinterp1, gr_eff

begin
	tab = ST_TAB(st)
	eff = INDEF

	if (streq (name, "disperser") && ST_DISPTYPE(st,1) == 0)
	    return (eff)
	if (streq (name, "xdisperser") && ST_DISPTYPE(st,2) == 0)
	    return (eff)

	call smark (sp)
	call salloc (str, SZ_FNAME, TY_CHAR)
	if (streq (name, "disperser")) {
	    call sprintf (Memc[str], SZ_FNAME, "eff%d")
		call pargi (order)
	    if (!tabexists (tab, Memc[str])) {
		if (order == 1 && tabexists (tab, name)) {
		    if (tabgeti (tab, name, "", "table.ndim") != 0)
			call strcpy (name, Memc[str], SZ_FNAME)
		}
	    }
	    if (tabexists (tab, Memc[str])) {
		eff = tabinterp1 (tab, Memc[str], wave)
		w = max (tabgetr (tab, Memc[str], "", "table.xmin"), wave)
		w = min (tabgetr (tab, Memc[str], "", "table.xmax"), w)
		if (w != wave) {
		    eff = tabinterp1 (tab, Memc[str], w) /
		        gr_eff (ST_GR(st,1), w, order)
		    eff = eff * gr_eff (ST_GR(st,1), wave, order)
		}
	    } else
		eff = gr_eff (ST_GR(st,1), wave, order)
	} else if (streq (name, "xdisperser")) {
	    call sprintf (Memc[str], SZ_FNAME, "xeff%d")
		call pargi (order)
	    if (!tabexists (tab, Memc[str])) {
		if (order == 1 && tabexists (tab, name)) {
		    if (tabgeti (tab, name, "", "table.ndim") != 0)
			call strcpy (name, Memc[str], SZ_FNAME)
		}
	    }
	    if (tabexists (tab, Memc[str])) {
		eff = tabinterp1 (tab, Memc[str], wave)
		w = max (tabgetr (tab, Memc[str], "", "table.xmin"), wave)
		w = min (tabgetr (tab, Memc[str], "", "table.xmax"), w)
		if (w != wave) {
		    eff = tabinterp1 (tab, Memc[str], w) /
		        gr_eff (ST_GR(st,2), w, order)
		    eff = eff * gr_eff (ST_GR(st,2), wave, order)
		}
	    } else
		eff = gr_eff (ST_GR(st,2), wave, order)
	}

	if (IS_INDEF(eff))
	    eff = 0.

	call sfree (sp)
	return (eff)
end


# ST_X2W -- Return wavelength at given position on detector.

real procedure st_x2w (st, index, x)

pointer st		#I SPECTIME pointer
int	index		#I Grating index
real	x		#I Detector position (mm from center)
real	w		#O Wavelength (Angstroms)

real	gr_x2w()

begin
	switch (ST_DISPTYPE(st,index)) {
	case GRATING:
	    w = gr_x2w (ST_GR(st,index), x, ST_ORDER(st,index)) 
	case GRISM:
	    w = gr_x2w (ST_GR(st,index), x, ST_ORDER(st,index)) 
	case GENERIC:
	    w = gr_x2w (ST_GR(st,index), x, ST_ORDER(st,index)) 
	}

	return (w)
end


# ST_W2X -- Return wavelength at given position on detector.

real procedure st_w2x (st, index, w)

pointer st		#I SPECTIME pointer
int	index		#I Grating index
real	w		#I Wavelength (Angstroms)
real	x		#O Detector position (mm from center)

real	gr_w2x()

begin
	switch (ST_DISPTYPE(st,index)) {
	case GRATING:
	    x = gr_w2x (ST_GR(st,index), w, ST_ORDER(st,index)) 
	case GRISM:
	    x = gr_w2x (ST_GR(st,index), w, ST_ORDER(st,index)) 
	case GENERIC:
	    x = gr_w2x (ST_GR(st,index), w, ST_ORDER(st,index)) 
	}

	return (x)
end


# ST_W2DW -- Return dispersion on detector at given wavelength.

real procedure st_w2dw (st, index, w)

pointer st		#I SPECTIME pointer
int	index		#I Grating index
real	w		#I Wavelength (Angstroms)
real	d		#I Dispersion (Angstroms/mm)

real	gr_w2dw()

begin
	switch (ST_DISPTYPE(st,index)) {
	case GRATING:
	    d = gr_w2dw (ST_GR(st,index), w, ST_ORDER(st,index)) 
	case GRISM:
	    d = gr_w2dw (ST_GR(st,index), w, ST_ORDER(st,index)) 
	case GENERIC:
	    d = gr_w2dw (ST_GR(st,index), w, ST_ORDER(st,index)) 
	}

	return (d)
end