aboutsummaryrefslogtreecommitdiff
path: root/noao/onedspec/getcalib.x
blob: 6d7d77c4338ee683e35ad9de8ed78969299ff6f2 (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
include <ctype.h>
include	<error.h>
include	<mach.h>

define	STD_TYPES	"|star|blackbody|"
define	UNKNOWN		0	# Unknown calibration file type
define	STAR		1	# Standard star calibration file
define	BLACKBODY	2	# Blackbody calibration file

define	NALLOC	128	# Allocation block size

# GETCALIB -- Get flux data.
# This is either for a blackbody of specified magnitude and type or
# a specified standard star with calibration data in a database directory.

procedure getcalib (waves, dwaves, mags, nwaves)

pointer	waves			#O Pointer to calibration wavelengths
pointer	dwaves			#O Pointer to calibration bandpasses
pointer	mags			#O Pointer to calibration magnitudes
int	nwaves			#O Number of calibration points

real	weff, wave, mag, dwave, wave1, wave2
int	i, j, fd, nalloc
pointer	sp, dir, star, name, file, type, units, band, str
pointer	un, unang

bool	streq()
int	open(), fscan(), nscan(), getline(), strdic()
pointer	un_open()
errchk	getbbcal, open, un_open, un_ctranr
define	getstd_	10

begin
	call smark (sp)
	call salloc (dir, SZ_FNAME, TY_CHAR)
	call salloc (star, SZ_FNAME, TY_CHAR)
	call salloc (name, SZ_LINE, TY_CHAR)
	call salloc (file, SZ_LINE, TY_CHAR)
	call salloc (type, SZ_LINE, TY_CHAR)
	call salloc (units, SZ_LINE, TY_CHAR)
	call salloc (band, SZ_LINE, TY_CHAR)
	call salloc (str, SZ_LINE, TY_CHAR)
	Memc[str] = EOS

	# Convert the star name to a file name and open the file.
	# If an error occurs print a list of files.

getstd_	call clgstr ("caldir", Memc[dir], SZ_FNAME)
	call clgstr ("star_name", Memc[star], SZ_FNAME)

	call strcpy (Memc[star], Memc[name], SZ_LINE)
	call strlwr (Memc[name])
	j = name
	for (i=name; Memc[i]!=EOS; i=i+1) {
	    if (IS_WHITE(Memc[i]) || Memc[i]=='+' || Memc[i]=='-')
		next
	    Memc[j] = Memc[i]
	    j = j + 1
	}
	Memc[j] = EOS

	# Check if this is an alternate name.
	call sprintf (Memc[file], SZ_LINE, "%snames.men")
	    call pargstr (Memc[dir])
	ifnoerr (fd = open (Memc[file], READ_ONLY, TEXT_FILE)) {
	    while (fscan (fd) != EOF) {
		call gargwrd (Memc[file], SZ_LINE)
		if (streq (Memc[file], Memc[name])) {
		    call gargwrd (Memc[name], SZ_LINE)
		    break
		}
	    }
	}

	call sprintf (Memc[file], SZ_LINE, "%s%s.dat")
	    call pargstr (Memc[dir])
	    call pargstr (Memc[name])

	iferr (fd = open (Memc[file], READ_ONLY, TEXT_FILE)) {
	    if (streq (Memc[file], Memc[str]))
		call erract (EA_ERROR)
	    call strcpy (Memc[file], Memc[str], SZ_LINE)
	    call sprintf (Memc[file], SZ_LINE, "%sstandards.men")
		call pargstr (Memc[dir])
	    fd = open (Memc[file], READ_ONLY, TEXT_FILE)
	    while (getline (fd, Memc[file]) != EOF)
		call putline (STDERR, Memc[file]) 
	    call close (fd)
	    Memc[star] = EOS
	    goto getstd_
	}

	# Read the calibration data.
	type = STAR
	call strcpy ("angstroms", Memc[units], SZ_LINE)
	Memc[band] = EOS
	weff = INDEFR

	nalloc = 0
	nwaves = 0
	while (fscan (fd) != EOF) {

	    # Check for comments and parameters.
	    call gargwrd (Memc[str], SZ_LINE)
	    if (nscan() != 1)
		next
	    if (Memc[str] == '#') {
		call gargwrd (Memc[str], SZ_LINE)
		call strlwr (Memc[str])
		if (streq (Memc[str], "type")) {
		    call gargwrd (Memc[str], SZ_LINE)
		    type = strdic (Memc[str], Memc[str], SZ_LINE, STD_TYPES)
		} else if (streq (Memc[str], "units"))
		    call gargwrd (Memc[units], SZ_LINE)
		else if (streq (Memc[str], "band"))
		    call gargwrd (Memc[band], SZ_LINE)
		else if (streq (Memc[str], "weff"))
		    call gargr (weff)
		next
	    }
	    call reset_scan ()

	    # Read data.
	    call gargr (wave)
	    call gargr (mag)
	    call gargr (dwave)
	    if (nscan() != 3)
		next

	    if (nalloc == 0) {
		nalloc = nalloc + NALLOC
		call malloc (waves, nalloc, TY_REAL)
		call malloc (mags, nalloc, TY_REAL)
		call malloc (dwaves, nalloc, TY_REAL)
	    } else if (nwaves == nalloc) {
		nalloc = nalloc + NALLOC
		call realloc (waves, nalloc, TY_REAL)
		call realloc (mags, nalloc, TY_REAL)
		call realloc (dwaves, nalloc, TY_REAL)
	    }

	    Memr[waves+nwaves] = wave
	    Memr[mags+nwaves] = mag
	    Memr[dwaves+nwaves] = dwave
	    nwaves = nwaves + 1
	}
	call close (fd)

	if (nwaves == 0)
	    call error (1, "No calibration data found")

	call realloc (waves, nwaves, TY_REAL)
	call realloc (mags, nwaves, TY_REAL)
	call realloc (dwaves, nwaves, TY_REAL)

	# This routine returns wavelengths in Angstroms.
	un = un_open (Memc[units])
	unang = un_open ("Angstroms")
	call un_ctranr (un, unang, weff, weff, 1)
	do i = 1, nwaves {
	    wave = Memr[waves+i-1]
	    dwave = Memr[dwaves+i-1]
	    wave1 = wave - dwave / 2
	    wave2 = wave + dwave / 2
	    call un_ctranr (un, unang, wave1, wave1, 1)
	    call un_ctranr (un, unang, wave2, wave2, 1)
	    wave = (wave1 + wave2) / 2.
	    dwave = abs (wave1 - wave2)
	    Memr[waves+i-1] = wave
	    Memr[dwaves+i-1] = dwave
	}
	call un_close (un)
	call un_close (unang)

	switch (type) {
	case UNKNOWN:
	    call freecalib (waves, dwaves, mags)
	    call error (1, "Unknown calibration type")
	case BLACKBODY:
	    call getbbcal (Memr[waves], Memr[mags], nwaves, Memc[band],
		weff, Memc[dir])
	}

	call sfree (sp)
end


# GETBBCAL -- Get blackbody calibration data.

procedure getbbcal (waves, mags, nwaves, band, weff, caldir)

real	waves[nwaves]		#I Calibration wavelengths
real	mags[nwaves]		#I Calibration magnitudes
int	nwaves			#I Number of calibration points
char	band[ARB]		#I Bandpass of data
real	weff			#I Effective wavelength
char	caldir[ARB]		#I Calibration directory

int	i, j, col1, col2, fd
real	mag, m1, m2, dm, teff, t, dt
pointer	sp, bands, magband, sptype, default, fname, str

bool	streq(), strne()
int	clgwrd(), nowhite(), ctor(), strdic(), strncmp()
int	open(), fscan(), nscan()
real	clgetr()
errchk	open

begin
	if (band [1] == EOS || IS_INDEFR(weff))
	    call error (1,
	    "Blackbody calibration file has no band or effective wavelength")

	call smark (sp)
	call salloc (bands, SZ_LINE, TY_CHAR)
	call salloc (magband, SZ_LINE, TY_CHAR)
	call salloc (sptype, SZ_LINE, TY_CHAR)
	call salloc (default, SZ_LINE, TY_CHAR)
	call salloc (fname, SZ_FNAME, TY_CHAR)
	call salloc (str, SZ_LINE, TY_CHAR)

	# Create list of acceptable magnitudes.
	call sprintf (Memc[bands], SZ_LINE, "|")
	call sprintf (Memc[fname], SZ_FNAME, "%sparams.dat")
	    call pargstr (caldir)
	ifnoerr (fd = open (Memc[fname], READ_ONLY, TEXT_FILE)) {
	    while (fscan (fd) != EOF) {
		call gargwrd (Memc[str], SZ_LINE)
		if (Memc[str] != '#')
		    next
		call gargwrd (Memc[str], SZ_LINE)
		if (strne (Memc[str], "Type"))
		    next

		call gargwrd (Memc[str], SZ_LINE)
		j = nscan()
		repeat {
		    i = j
		    call gargwrd (Memc[str], SZ_LINE)
		    j = nscan()
		    if (i == j)
			break
		    call strcat (Memc[str], Memc[bands], SZ_LINE)
		    call strcat ("|", Memc[bands], SZ_LINE)
		}
		break
	    }
	    call close (fd)
	}
	col1 = strdic (band, Memc[str], SZ_LINE, Memc[bands]) + 2
	if (col1 == 2) {
	    call strcat (band, Memc[bands], SZ_LINE)
	    call strcat ("|", Memc[bands], SZ_LINE)
	}
	col1 = strdic (band, Memc[str], SZ_LINE, Memc[bands]) + 2
	call clpstr ("magband.p_min", Memc[bands])

	# Get blackbody parameters.
	mag = clgetr ("mag")
	col2 = clgwrd ("magband", Memc[magband], SZ_LINE, Memc[bands]) + 2
	call clgstr ("teff", Memc[sptype], SZ_LINE)
	i = nowhite (Memc[sptype], Memc[sptype], SZ_LINE)

	# Convert spectral type to effective temperature.
	i = 1
	if (ctor (Memc[sptype], i, teff) == 0) {
	    teff = INDEFR
	    call sprintf (Memc[fname], SZ_FNAME, "%sparams.dat")
		call pargstr (caldir)
	    fd = open (Memc[fname], READ_ONLY, TEXT_FILE)
	    while (fscan (fd) != EOF) {
		call gargwrd (Memc[str], SZ_FNAME)
		if (strncmp (Memc[str], Memc[sptype], 2) != 0)
		    next
		call gargr (t)
		if (nscan() < 2)
		    next

		call strcpy (Memc[str], Memc[default], SZ_LINE)
		teff = t

		if (streq (Memc[default], Memc[sptype]))
		    break
	    }
	    call close (fd)

	    if (IS_INDEFR(teff))
		call error (1, "Failed to determine effective temperature")
	    if (strne (Memc[default], Memc[sptype])) {
		call eprintf ("WARNING: Effective temperature for %s not found")
		    call pargstr (Memc[sptype])
		call eprintf (" - using %s\n")
		    call pargstr (Memc[default])
		call strcpy (Memc[default], Memc[sptype], SZ_LINE)
	    }
	} else
	    Memc[sptype] = EOS

	# Transform the input magnitude from the input passband to the
	# data passband if necessary.
	if (strne (Memc[magband], band)) {
	    
	    # Get spectral type if necessary.
	    if (Memc[sptype] == EOS) {
		dt = MAX_REAL
		call sprintf (Memc[fname], SZ_FNAME, "%sparams.dat")
		    call pargstr (caldir)
		fd = open (Memc[fname], READ_ONLY, TEXT_FILE)
		while (fscan (fd) != EOF) {
		    call gargwrd (Memc[str], SZ_FNAME)
		    if (Memc[str+2] != 'V')
			next
		    call gargr (t)
		    if (nscan() < 2)
			next
		    if (abs (t - teff) < dt) {
			dt = abs (t - teff)
			call strcpy (Memc[str], Memc[sptype], SZ_LINE)
		    }
		}
		call close (fd)

		if (Memc[sptype] == EOS)
		    call error (1, "Failed to determine spectral type")
		call eprintf ("WARNING: Assuming spectral type of %s\n")
		    call pargstr (Memc[sptype])
	    }

	    # Get magnitude correction.
	    dm = INDEFR
	    call sprintf (Memc[fname], SZ_FNAME, "%sparams.dat")
		call pargstr (caldir)
	    fd = open (Memc[fname], READ_ONLY, TEXT_FILE)
	    while (fscan (fd) != EOF) {
		call gargwrd (Memc[str], SZ_LINE)
		if (strncmp (Memc[str], Memc[sptype], 2) != 0)
		    next

		call gargr (t)

		m1 = INDEFR
		m2 = INDEFR
		do i = 1, max (col1, col2) {
		    call gargr (t)
		    if (i == col1)
			m1 = t
		    if (i == col2)
			m2 = t
		}

		if (!IS_INDEFR(m1) && !IS_INDEFR(m2)) {
		    call strcpy (Memc[str], Memc[default], SZ_LINE)
		    dm = m1 - m2
		    if (streq (Memc[default], Memc[sptype]))
			break
		}
	    }
	    call close (fd)

	    if (IS_INDEFR(dm)) {
		call sprintf (Memc[str], SZ_LINE,
		"No information in %s to convert %s mag to %s mag for %s star")
		    call pargstr (Memc[fname])
		    call pargstr (Memc[magband])
		    call pargstr (band)
		    call pargstr (Memc[sptype])
		call error (1, Memc[str])
	    }
	    if (strne (Memc[default], Memc[sptype])) {
		call eprintf (
		"WARNING: Converting %s mag to %s mag using spectral type %s\n")
		    call pargstr (Memc[magband])
		    call pargstr (band)
		    call pargstr (Memc[default])
	    }

	    mag = mag + dm

	    call eprintf ("Blackbody: %s = %.2f, %s = %.2f, Teff = %d\n")
		call pargstr (Memc[magband])
		call pargr (mag - dm)
		call pargstr (band)
		call pargr (mag)
		call pargr (teff)

	} else {
	    call eprintf ("Blackbody: %s = %.2f, Teff = %d\n")
		call pargstr (band)
		call pargr (mag)
		call pargr (teff)
	}

	# Convert the calibration magnitudes to the specified magnitude and
	# apply the blackbody function.
	m1 = -2.5 * log10 (weff**3 * (exp(1.4387E8/(weff*teff)) - 1))
	do i = 1, nwaves
	    mags[i] = mags[i] + mag + m1 +
		2.5 * log10 (waves[i]**3 * (exp(1.4387E8/(waves[i]*teff)) - 1))

	call sfree (sp)
end


# FREECALIB -- Free calibration data arrays.

procedure freecalib (waves, dwaves, mags)

pointer	waves, dwaves, mags

begin
	call mfree (waves, TY_REAL)
	call mfree (dwaves, TY_REAL)
	call mfree (mags, TY_REAL)
end