aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/dtoi/selftest.x
blob: 3a7a4a4aeb6081bd4b6decdd3be5b8b78cd84402 (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
include	<gio.h>
include	<gset.h>
include	<mach.h>

define	A0	(-1.74743)
define	A1	(0.73)
define	A2	(-0.24)
define	A3	(0.035)
define	MAXDEN	6.0

# T_SELFTEST -- a test procedure for the DTOI package.  Two intensity vectors
# are calculated in different ways and compared.  A plot of the residuals is
# shown.  A plot showing the extent of truncation errors is also drawn.  Two
# standard ranges of data values are available: 12 bit, representing PDS
# format data and 15 bit, representing the FITS data format available on the
# PDS.  Any other choice results in a small test, ranging from 1 - 144.

procedure t_selftest

bool	verbose
char	device[SZ_FNAME]
pointer	sp, intk, intc, raw, den, gp
int	min_raw, max_raw, nvalues, i, nbits
real	scale, factor, ceiling

bool	clgetb()
pointer	gopen()
int	clgeti()
real	clgetr()

begin
	call smark (sp)

	nbits = clgeti ("nbits")

	switch (nbits) {
	case 12:
	    min_raw = 1
	    max_raw = 3072
	    scale   = 0.00151
	case 15:
	    min_raw = 1
	    max_raw = 24576
	    scale   = 4.65 / 24575. 
	case 0:
	    call eprintf ("Using test data range from 1 - 144\n")
	    min_raw = 1
	    max_raw = 144
	    scale   = 0.0325
	default:
	    call eprintf ("Unknown case: nbits = '%d', Please supply values:\n")
		call pargi (nbits)
	    min_raw = 1
	    max_raw = clgeti ("max_raw")
	    # max density = 6.0.  Density = raw value * scale.
	    call clputr ("scale.p_maximum", real (MAXDEN / max_raw))
	    call clputr ("scale.p_default", real (4.65 / (max_raw - 1)))
	    scale = clgetr ("scale")
	}

	call clgstr ("device", device, SZ_FNAME)
	verbose = clgetb ("verbose")
	ceiling = clgetr ("ceiling")

	gp = gopen (device, NEW_FILE, STDGRAPH)

	nvalues = max_raw - min_raw + 1
	call salloc (intk, nvalues, TY_REAL)
	call salloc (intc, nvalues, TY_REAL)
	call salloc (den,  nvalues, TY_REAL)
	call salloc (raw,  nvalues, TY_REAL)

	do i = 1, nvalues
	    Memr[raw+i-1] = min_raw + i - 1

	call amulkr (Memr[raw], scale, Memr[den], nvalues)

	call hd_known (min_raw, max_raw, scale, Memr[intk], nvalues)
	call hd_calc  (min_raw, max_raw, scale, Memr[intc], nvalues)
	 
	if (verbose) {
	    factor = ceiling / Memr[intc+nvalues-1]
	    call printf ("# %20tRaw Value %40tDensity %60tIntensity\n\n")
	    do i = 1, nvalues {
	        call printf ("%20t%d %40t%g %60t%g\n")
		    call pargi (i)
		    call pargr (Memr[den+i-1])
		    call pargr (Memr[intc+i-1] * factor)
	    }
	}

	call hd_plotit (gp, Memr[den], Memr[intk], Memr[intc], nvalues)

	call hd_trunc (gp, Memr[den], nvalues, ceiling, Memr[intc])

	call gclose (gp)
	call sfree (sp)
end


# HD_KNOWN -- Calculate vector of known intensity values.

procedure hd_known (min_raw, max_raw, scale, intk, nvalues)

int	min_raw			# Minimum raw data value
int	max_raw			# Maximum raw data value
real	scale			# Density = raw_value * scale
real	intk[nvalues]		# Known intensities - filled on return
int	nvalues			# Number of intensity values requested

int	i
real	density, logo
real	exp

begin
	do i = min_raw, max_raw {
	    density = max (EPSILONR, i * scale)
	    logo = log10 ((10. ** density) - 1.0)
	    exp  = A0 + A1 * logo + A2 * logo ** 2 + A3 * logo ** 3
	    intk[i] = 10 ** (exp)
	}
end


# HD_CALC -- Calcuate vector of intensity values as in HDTOI.

procedure hd_calc (min, max, scale, intc, nvalues)

int	min			# Minimum raw data value
int	max			# Maximum raw data value
real	scale			# Density = raw_value * scale
real	intc[nvalues]		# Calculated intensity values - filled on return
int	nvalues			# Number of intensity values requested

real	cfit[9]
pointer	sp, lut

begin
	call smark (sp)
	call salloc (lut, nvalues, TY_REAL)

	cfit[1] = 5.0
	cfit[2] = 4.0
	cfit[3] = -10.0
	cfit[4] = MAXDEN
	cfit[5] = 1.
	cfit[6] = A0
	cfit[7] = A1
	cfit[8] = A2
	cfit[9] = A3
	call st_wlut (Memr[lut], min, max, scale, cfit)
	call st_transform (min, max, Memr[lut], nvalues, intc)

	call sfree (sp)
end


# HD_TRUNC -- Investigate truncation errors for real versus int output image.

procedure hd_trunc (gp, density, nvalues, ceiling, intc)

pointer	gp			# Pointer to graphics stream
real	density[nvalues]	# Density array
int	nvalues			# Number of density, intensity values
real	ceiling			# Max intensity to output
real	intc[nvalues]		# Calculated intensity values

pointer	sp, yint, yreal
int	npvals
real	factor

begin
	call smark (sp)

	# Only the lowest 5% of the data values are plotted
	npvals = nvalues * 0.05

	call salloc (yint, npvals, TY_INT)
	call salloc (yreal, npvals, TY_REAL)

	# Scale intensity vector to ceiling 
	factor = ceiling / intc[nvalues]

	call amulkr (intc, factor, intc, npvals)
	call achtri (intc, Memi[yint], npvals)
	call achtir (Memi[yint], Memr[yreal], npvals)

	call gascale (gp, density,  npvals, 1)
	call gascale (gp, Memr[yreal], npvals, 2)
	call gsview  (gp, 0.2, 0.9, 0.1, 0.4)
	call gseti (gp, G_ROUND, YES)
	call glabax  (gp, 
	    "Expand to see Truncation Errors\n (real=SOLID, integer=DASHED)", 
	    "Density (Lowest 5% only)", "Intensity")

	call gseti  (gp, G_PLTYPE, GL_SOLID)
	call gpline (gp, density, intc, npvals)

	call gseti  (gp, G_PLTYPE, GL_DASHED)
	call gpline (gp, density, Memr[yreal], npvals)

	call sfree (sp)
end


# HD_PLOTIT -- Plot residuals of calculated versus known itensity.

procedure hd_plotit (gp, density, intk, intc, nvalues)

pointer	gp			# Pointer to graphics stream
real	density[nvalues]	# Density array
real	intk[nvalues]		# Array of known intensities
real	intc[nvalues]		# Array of calculated intensities
int	nvalues			# Number of density, intensity values

pointer	sp, resid

begin
	call smark (sp)
	call salloc (resid, nvalues, TY_REAL)

	call asubr (intk, intc, Memr[resid], nvalues)

	call gascale (gp, density, nvalues, 1)
	call gascale (gp, Memr[resid], nvalues, 2)
	call gsview  (gp, 0.2, 0.9, 0.6, 0.9)
	call gseti   (gp, G_ROUND, YES)

	call glabax  (gp, "Residual Intensity\n (Known - Calculated)", 
	    "Density", "")
	call gpline  (gp, density, Memr[resid], nvalues)

	call sfree (sp)
end


# ST_WLUT -- Generate look up table, using technique of HDTOI.

procedure st_wlut (lut, min, max, scale, cfit)

real	lut[ARB]	# Look up table of intensities
int	min		# Minimum raw data value
int	max		# Maximum raw data value
real	scale		# Density = raw_value * scale
real	cfit[ARB]	# Coefficient array for restoring curfit

pointer	cv, sp, den, indv, kv
int	nvalues, i
extern	hd_powerr()

begin
	call smark (sp)
	nvalues = max - min + 1
	call salloc (den, nvalues, TY_REAL)
	call salloc (indv, nvalues, TY_REAL)
	call salloc (kv, nvalues, TY_REAL)
	do i = 1, nvalues
	    Memr[kv+i-1] = real (i)

	call amulkr (Memr[kv], scale, Memr[den], nvalues)

	call cvrestore (cv, cfit)
	call cvuserfnc (cv, hd_powerr)

	call hd_aptrans (Memr[den], Memr[indv], nvalues, "logo")
	call cvvector (cv, Memr[indv], lut, nvalues)
	do i = 1, nvalues
	    lut[i] = 10.0 ** lut[i]

	call cvfree (cv)
	call sfree (sp)
end


# ST_TRANSFORM -- Apply transformation from look up table to input vector.

procedure st_transform (min, max, lut, nvalues, intc)

int	min		# Minimum raw data value
int	max		# Maximum raw data value
real	lut[ARB]	# Array of intensity values
int	nvalues		# Number of density, intensity values
real	intc[ARB]	# Calculated intensities - returned

int	i

begin
	do i = 1, nvalues
	    intc[i] = lut[i]
end