aboutsummaryrefslogtreecommitdiff
path: root/noao/obsutil/src/ccdtime/t_ccdtime.x
blob: 0c0e8822d14845b5655f7289772c37575de5e3ad (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
define	NF		25		# Maximum number of filters
define	SZ_FILTER	7		# Maximum length of filter name
define	TOL		0.0001		# Convergence tolerance


# T_CCDTIME -- Compute the time, magnitude, and signal-to-noise for CCD
# exposures for a given telescope, detector, and filters.  The telescope
# detector, and filter data come from a text database.  The computed
# quantities fix two parameters and compute the third.

procedure t_ccdtime()

pointer	database		# Telescope, detector, filter database
pointer	telescope		# Telescope
pointer	detector		# Detector
pointer	fltr[5]			# Filters
real	time			# Target time (sec)
real	mag			# Target magnitude
real	snr			# Target SNR
int	sum			# CCD summing
real	seeing			# Seeing (arcsec)
real	phase			# Moon phase (0-28)
real	airmass			# Airmass

int	i, j, k, nf, n
real	a, b, c
real	aper, scale, trans, nread, dark, pixsize, lum, p
real	star[NF], sky[NF], t, m, s, nstar, nsky, ndark, noise, npix
real	dqe, ext, starmag, counts, sky0, sky1, sky2
pointer	sp, tdb, ddb, filter[NF], fdb[NF]

int	clgeti()
real	clgetr()
double	dbgetd()
pointer	dbopen()
errchk	dbopen

define	done_	10

begin
	call smark (sp)
	call salloc (database, SZ_FNAME, TY_CHAR)
	call salloc (telescope, SZ_FNAME, TY_CHAR)
	call salloc (detector, SZ_FNAME, TY_CHAR)
	do i = 1, 5
	    call salloc (fltr[i], SZ_LINE, TY_CHAR)
	do i = 1, NF
	    call salloc (filter[i], SZ_FILTER, TY_CHAR)

	# Get task parameters.
	call clgstr ("database", Memc[database], SZ_FNAME)
	call clgstr ("telescope", Memc[telescope], SZ_FNAME)
	call clgstr ("detector", Memc[detector], SZ_FNAME)
	call clgstr ("f1", Memc[fltr[1]], SZ_LINE)
	call clgstr ("f2", Memc[fltr[2]], SZ_LINE)
	call clgstr ("f3", Memc[fltr[3]], SZ_LINE)
	call clgstr ("f4", Memc[fltr[4]], SZ_LINE)
	call clgstr ("f5", Memc[fltr[5]], SZ_LINE)

	time = clgetr ("time")
	mag = clgetr ("magnitude")
	snr = clgetr ("snr")
	sum = clgeti ("sum")
	seeing = clgetr ("seeing")
	phase = clgetr ("phase")
	airmass = clgetr ("airmass")

	# The input filter strings may be lists of filters and here
	# we expand them into all filters.

	nf = 1; k = 0
	do i = 1, 5 {
	    for (j = fltr[i]; Memc[j] != EOS; j = j + 1) {
	        Memc[filter[nf]+k] = Memc[j]
	        if (Memc[j] == ',') {
		    Memc[filter[nf]+k] = EOS
		    if (k > 0) {
			nf = nf + 1; k = 0
		    }
		} else
		    k = k + 1
	    }
	    Memc[filter[nf]+k] = EOS
	    if (k > 0) {
		nf = nf + 1; k = 0
	    }
	}
	nf = nf - 1

	i = 0
	if (IS_INDEFR(time))
	    i = i + 1
	 else if (time <= 0.)
	    call error (1, "Requested time must be greater than zero")
	 else if (time > 100000.)
	    call error (1, "Requested time must be less than 100,000")

	if (IS_INDEFR(mag))
	    i = i + 1
	else if (mag > 40.)
	    call error (1, "Requested magnitude must be less than 40")
	else if (mag < -40.)
	    call error (1, "Requested magnitude must be greater than -40")

	if (IS_INDEFR(snr))
	    i = i + 1
	 else if (snr <= 0.)
	    call error (1, "Requested SNR must be greater than zero")
	 else if (snr > 100000.)
	    call error (1, "Requested SNR must be less than 100,000")

	if (i > 1) {
	    call sfree (sp)
	    call error (1,
		"At least two of time, magnitude, and snr must be specified")
	}

	if (phase < 14)
	    p = phase
	else
	    p = 28 - phase

	# Open database entries.
	# If '?' this will print list and then the task will exit.
	# If an error occurs in the telescope or detector abort.
	# If an error occurs in the filter ignore the filter.

	tdb = dbopen ("", Memc[database], "telescope", Memc[telescope])
	ddb = dbopen ("", Memc[database], "detector", Memc[detector])

	n = 0
	do i = 1, nf {
	    iferr (fdb[n+1]=dbopen("",Memc[database],"filter",Memc[filter[i]]))
		next
	    if (fdb[n+1] == NULL)
		goto done_
	    n = n + 1
	    filter[n] = filter[i]
	}
	if (tdb == NULL || ddb == NULL || n == 0)
	    goto done_

	# Get star and sky rates for telescope/detector/filter combination.
	# Convert to a standard rate at 20th magnitude at the given airmass.

	do i = 1, n {
	    # Get telescope parameters.
	    aper = dbgetd (tdb, "aperture", Memc[filter[i]], Memc[detector])
	    scale = dbgetd (tdb, "scale", Memc[filter[i]], Memc[detector])
	    trans = dbgetd (tdb, "transmission", Memc[filter[i]],
		Memc[detector])

	    # Get detector parameters.
	    dqe = dbgetd (ddb, Memc[filter[i]], Memc[filter[i]],
		Memc[telescope])
	    nread = dbgetd (ddb, "rdnoise", Memc[filter[i]], Memc[telescope])
	    dark = dbgetd (ddb, "dark", Memc[filter[i]], Memc[telescope])
	    pixsize = dbgetd (ddb,"pixsize",Memc[filter[i]],Memc[telescope]) *
		(scale / 1000) * sum
	    npix  =  max (9, nint (1.4 * (seeing / pixsize)**2+0.5))

	    # Get filter parameters.
	    iferr (ext = dbgetd (fdb[i], "extinction", Memc[telescope],
		Memc[detector]))
		ext = 1
	    starmag = dbgetd (fdb[i], "mag", Memc[telescope], Memc[detector])
	    counts = dbgetd (fdb[i], "star", Memc[telescope], Memc[detector])
	    sky0 = dbgetd (fdb[i], "sky0", Memc[telescope], Memc[detector])
	    sky1 = dbgetd (fdb[i], "sky1", Memc[telescope], Memc[detector])
	    sky2 = dbgetd (fdb[i], "sky2", Memc[telescope], Memc[detector])

	    lum = 10. ** (0.4 * (starmag - 20.))
	    star[i] = counts * lum * aper ** 2 * trans *
		10 ** (0.4 * (1 - airmass) * ext)
	    star[i] = star[i] * dqe
	    sky[i] = sky0 + sky1 * p + sky2 * p * p
	    sky[i] = star[i] * pixsize ** 2 * 10. ** ((20. - sky[i]) / 2.5)
	}
	if (!IS_INDEFR(mag))
	    lum = 10. ** (0.4 * (20. - mag))

	# Print header and column labels.
	call printf ("Database: %-20s  Telescope: %-10s  Detector: %-10s\n")
	    call pargstr (Memc[database])
	    call pargstr (Memc[telescope])
	    call pargstr (Memc[detector])
	call printf ("  Sum: %-2d  Arcsec/pixel: %-4.2f  Pixels/star: %-4.1f\n")
	    call pargi (sum)
	    call pargr (pixsize)
	    call pargr (npix)
	call printf ("  Seeing: %-.3g  Airmass: %-4.2f  Phase: %-4.1f\n")
	    call pargr (seeing)
	    call pargr (airmass)
	    call pargr (phase)
	call printf ("\n%7s %7s %7s %7s %7s %7s %s\n")
	    call pargstr ("Filter")
	    call pargstr ("Time")
	    call pargstr ("Mag")
	    call pargstr ("SNR")
	    call pargstr ("Star")
	    call pargstr ("Sky/pix")
	    call pargstr ("   Noise contributions")
	call printf ("%47w %7s %7s %7s\n")
	    call pargstr ("Star")
	    call pargstr ("Sky")
	    call pargstr ("CCD")

	# Compute exposure time through each filter.

	if (!IS_INDEFR(mag) && !IS_INDEFR(snr)) {
	    call printf ("\n")
	    do i = 1, n {
		a = ((star[i] * lum) / snr) ** 2
		b = -(star[i] * lum + npix * (sky[i] + dark))
		c = -npix * nread ** 2
		t = (-b + sqrt (b**2 - 4 * a * c)) / (2 * a)

		nstar = star[i] * lum * t
		nsky  = sky[i] * t
		ndark = dark * t
		noise = sqrt(nstar + npix * (nsky + ndark + nread**2))
		s = nstar / noise
		m = mag

		call printf (
		    "%7s %7.2f %7.1f %7.1f %7.1f %7.1f %7.2f %7.2f %7.2f\n")
		    call pargstr (Memc[filter[i]])
		    call pargr (t)
		    call pargr (m)
		    call pargr (s)
		    call pargr (nstar)
		    call pargr (nsky)
		    call pargr (sqrt (nstar))
		    call pargr (sqrt (npix * nsky))
		    call pargr (sqrt (npix * (ndark + nread**2)))
	    }
	}

	# Compute magnitude through each filter.
	# Use resubstitution to iterate for SNR.

	if (!IS_INDEFR(time) && !IS_INDEFR(snr)) {
	    call printf ("\n")
	    do i = 1, n {
		m = 20
		s = 0
		do j = 1, 100 {
		    t = time
		    nstar = star[i] * 10**(0.4*(20.0-m)) * t
		    nsky  = sky[i] * t
		    ndark = dark * t
		    noise = sqrt(nstar + npix * (nsky + ndark + nread**2))
		    m = 20 - 2.5 * log10 (snr * noise / (t * star[i]))
		    s = nstar / noise
		    if (abs(1-s/snr) <= TOL)
			break
		}

		call printf (
		    "%7s %7.2f %7.1f %7.1f %7.1f %7.1f %7.2f %7.2f %7.2f\n")
		    call pargstr (Memc[filter[i]])
		    call pargr (t)
		    call pargr (m)
		    call pargr (s)
		    call pargr (nstar)
		    call pargr (nsky)
		    call pargr (sqrt (nstar))
		    call pargr (sqrt (npix * nsky))
		    call pargr (sqrt (npix * (ndark + nread**2)))
	    }
	}

	# Compute SNR through each filter.

	if (!IS_INDEFR(time) && !IS_INDEFR(mag)) {
	    call printf ("\n")
	    do i = 1, n {
		t = time
		m = mag
		nstar = star[i] * lum * t
		nsky  = sky[i] * t
		ndark = dark * t
		noise = sqrt(nstar + npix * (nsky + ndark + nread**2))
		s = nstar / noise

		call printf (
		    "%7s %7.2f %7.1f %7.1f %7.1f %7.1f %7.2f %7.2f %7.2f\n")
		    call pargstr (Memc[filter[i]])
		    call pargr (t)
		    call pargr (m)
		    call pargr (s)
		    call pargr (nstar)
		    call pargr (nsky)
		    call pargr (sqrt (nstar))
		    call pargr (sqrt (npix * nsky))
		    call pargr (sqrt (npix * (ndark + nread**2)))
	    }
	}
	call printf ("\n")

done_
	call dbclose (tdb)
	call dbclose (ddb)
	do i = 1, n
	    call dbclose (fdb[i])
	call sfree (sp)
end