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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
include <fset.h>
include <imhdr.h>
include <imset.h>
include <error.h>
include "linmatch.h"
# T_LINMATCH -- Compute the parameters required to match the intensity scale
# of an image to that of a reference image using an expression of the form
# I(ref) = a + b * I(image)
procedure t_linmatch()
pointer freglist #I pointer to reference regions list
pointer database #I pointer to database file
int dformat #I write the output file in database format
int interactive #I interactive mode ?
int verbose #I verbose mode
int list1, listr, list2, reglist, reclist, stat, nregions, shiftslist
int rpfd, ipfd, sfd
pointer sp, reference, imager, image1, imtemp, image2, str, str1, shifts
pointer ls, db, gd, id, imr, im1, im2
bool clgetb()
int imtopen(), fntopnb(), imtlen(), fntlenb(), access(), btoi(), open()
int rg_lstati(), imtgetim(), fntgfnb(), rg_lregions(), rg_lscale()
int rg_lrphot(), rg_liscale()
pointer dtmap(), gopen(), immap()
real rg_lstatr()
errchk gopen()
begin
call fseti (STDOUT, F_FLUSHNL, YES)
# Allocate temporary space.
call smark (sp)
call salloc (reference, SZ_FNAME, TY_CHAR)
call salloc (freglist, SZ_LINE, TY_CHAR)
call salloc (image1, SZ_FNAME, TY_CHAR)
call salloc (imager, SZ_FNAME, TY_CHAR)
call salloc (image2, SZ_FNAME, TY_CHAR)
call salloc (imtemp, SZ_FNAME, TY_CHAR)
call salloc (database, SZ_FNAME, TY_CHAR)
call salloc (shifts, SZ_FNAME, TY_CHAR)
call salloc (str, SZ_LINE, TY_CHAR)
call salloc (str1, SZ_LINE, TY_CHAR)
# Open the input and output image lists.
call clgstr ("input", Memc[str], SZ_LINE)
list1 = imtopen (Memc[str])
call clgstr ("reference", Memc[reference], SZ_LINE)
call clgstr ("regions", Memc[freglist], SZ_LINE)
call clgstr ("lintransform", Memc[database], SZ_LINE)
call clgstr ("output", Memc[str], SZ_LINE)
list2 = imtopen (Memc[str])
call clgstr ("records", Memc[str], SZ_LINE)
if (Memc[str] == EOS)
reclist = NULL
else
reclist = fntopnb (Memc[str], NO)
call clgstr ("shifts", Memc[shifts], SZ_LINE)
# Open the cross correlation fitting structure.
call rg_glpars (ls)
# Test the reference image list length
if ((rg_lstati (ls, BZALGORITHM) == LS_FILE || rg_lstati(ls,
BSALGORITHM) == LS_FILE) || (rg_lstati(ls, BZALGORITHM) ==
LS_NUMBER && rg_lstati(ls, BSALGORITHM) == LS_NUMBER)) {
listr = NULL
reglist = NULL
shiftslist = NULL
call rg_lsets (ls, REGIONS, "")
} else if (rg_lstati(ls, BZALGORITHM) == LS_PHOTOMETRY || rg_lstati (ls,
BSALGORITHM) == LS_PHOTOMETRY) {
listr = fntopnb (Memc[reference], NO)
if (fntlenb (listr) <= 0)
call error (0, "The reference photometry list is empty.")
reglist = fntopnb (Memc[freglist], NO)
if (fntlenb (listr) > 1 && fntlenb (listr) != imtlen (list1)) {
call eprintf ("The number of reference photometry files")
call eprintf (" and input images is not the same.\n")
call erract (EA_FATAL)
}
if (fntlenb(reglist) != imtlen(list1)) {
call eprintf ("The number of input photometry files and")
call eprintf ("images are not the same.\n")
call erract (EA_FATAL)
}
shiftslist = NULL
call rg_lsets (ls, REGIONS, Memc[freglist])
} else {
listr = imtopen (Memc[reference])
if (imtlen (listr) <= 0)
call error (0, "The reference image list is empty.")
if (imtlen (listr) > 1 && imtlen (listr) != imtlen (list1))
call error (0,
"The number of reference and input images is not the same.")
iferr {
reglist = fntopnb (Memc[freglist], NO)
} then
reglist = NULL
if (Memc[shifts] == EOS)
shiftslist = NULL
else {
shiftslist = fntopnb (Memc[shifts], NO)
if (imtlen(listr) != fntlenb (shiftslist))
call error (0,
"The number of shifts files and images is not the same.")
}
call rg_lsets (ls, REGIONS, Memc[freglist])
}
# Close the output image list if it is empty.
if (imtlen (list2) <= 0) {
call imtclose (list2)
list2 = NULL
}
# Check that the output image list is the same as the input image
# list.
if (list2 != NULL) {
if (imtlen (list1) != imtlen (list2))
call error (0,
"The number of input and output images are not the same.")
}
# Check that the record list is the same length as the input image
# list length.
if (reclist != NULL) {
if (fntlenb (reclist) != imtlen (list1))
call error (0,
"Input image and record lists are not the same length")
}
# Open the database file.
dformat = btoi (clgetb ("databasefmt"))
if (rg_lstati(ls, BZALGORITHM) == LS_FILE && rg_lstati(ls,
BSALGORITHM) == LS_FILE) {
if (dformat == YES)
db = dtmap (Memc[database], READ_ONLY)
else
db = open (Memc[database], READ_ONLY, TEXT_FILE)
} else if (clgetb ("append")) {
if (dformat == YES)
db = dtmap (Memc[database], APPEND)
else
db = open (Memc[database], NEW_FILE, TEXT_FILE)
} else if (access(Memc[database], 0, 0) == YES) {
call error (0, "The shifts database file already exists")
} else {
if (dformat == YES)
db = dtmap (Memc[database], NEW_FILE)
else
db = open (Memc[database], NEW_FILE, TEXT_FILE)
}
call rg_lsets (ls, DATABASE, Memc[database])
if ((rg_lstati(ls, BZALGORITHM) == LS_FILE || rg_lstati(ls,
BSALGORITHM) == LS_FILE) || (rg_lstati(ls, BZALGORITHM) ==
LS_NUMBER && rg_lstati(ls, BSALGORITHM) == LS_NUMBER))
interactive = NO
else
interactive = btoi (clgetb ("interactive"))
if (interactive == YES) {
call clgstr ("graphics", Memc[str], SZ_FNAME)
iferr (gd = gopen (Memc[str], NEW_FILE, STDGRAPH))
gd = NULL
call clgstr ("display", Memc[str], SZ_FNAME)
iferr (id = gopen (Memc[str], APPEND, STDIMAGE))
id = NULL
verbose = YES
} else {
gd = NULL
id = NULL
verbose = btoi (clgetb ("verbose"))
}
# Initialize the reference image pointer.
imr = NULL
sfd = NULL
rpfd = NULL
ipfd = NULL
# Do each set of input and output images.
while ((imtgetim (list1, Memc[image1], SZ_FNAME) != EOF)) {
# Open the reference image and associated regions files
# if the correlation function is not file.
if (rg_lstati(ls, BZALGORITHM) == LS_PHOTOMETRY || rg_lstati(ls,
BSALGORITHM) == LS_PHOTOMETRY) {
if (fntgfnb(listr, Memc[str], SZ_FNAME) != EOF) {
if (rpfd != NULL)
call close (rpfd)
rpfd = open (Memc[str], READ_ONLY, TEXT_FILE)
call rg_lsets (ls, REFIMAGE, Memc[str])
call rg_lsetr (ls, RGAIN, rg_lstatr (ls,GAIN))
call rg_lsetr (ls, RREADNOISE, rg_lstatr (ls,READNOISE))
nregions = rg_lrphot (rpfd, ls, 1, rg_lstati(ls,
MAXNREGIONS), YES)
if (nregions <= 0 && interactive == NO)
call error (0,
"The reference photometry file is empty.")
}
} else if ((rg_lstati(ls, BZALGORITHM) == LS_FILE || rg_lstati(ls,
BSALGORITHM) == LS_FILE) || (rg_lstati(ls,BZALGORITHM) ==
LS_NUMBER && rg_lstati(ls,BSALGORITHM) == LS_NUMBER)) {
call rg_lsets (ls, REFIMAGE, "reference")
} else {
if (imtgetim(listr, Memc[str], SZ_FNAME) != EOF) {
if (imr != NULL)
call imunmap (imr)
imr = immap (Memc[str], READ_ONLY, 0)
if (IM_NDIM(imr) > 2)
call error (0, "Referenc image must be 1D or 2D")
call rg_lgain (imr, ls)
if (!IS_INDEFR(rg_lstatr(ls,GAIN)))
call rg_lsetr (ls, RGAIN, rg_lstatr (ls,GAIN))
call rg_lrdnoise (imr, ls)
if (!IS_INDEFR(rg_lstatr(ls,READNOISE)))
call rg_lsetr (ls, RREADNOISE, rg_lstatr (ls,READNOISE))
call rg_lsets (ls, REFIMAGE, Memc[str])
nregions = rg_lregions (reglist, imr, ls, 1, NO)
if (nregions <= 0 && interactive == NO)
call error (0, "The regions list is empty.")
if (shiftslist != NULL) {
if (sfd != NULL)
call close (sfd)
if (fntgfnb (shiftslist, Memc[str], SZ_FNAME) == EOF) {
call rg_lsets (ls, SHIFTSFILE, "")
sfd = NULL
} else {
call rg_lsets (ls, SHIFTSFILE, Memc[str])
sfd = open (Memc[str], READ_ONLY, TEXT_FILE)
}
}
}
}
# Open the input image.
if (list2 == NULL && imr == NULL)
im1 = NULL
else {
im1 = immap (Memc[image1], READ_ONLY, 0)
if (IM_NDIM(im1) > 2) {
call error (0, "Input images must be 1D or 2D")
} else if (imr != NULL) {
if (IM_NDIM(im1) != IM_NDIM(imr)) {
call eprintf ("Input images must have same")
call eprintf (" dimensionality as reference images.\n")
call erract (EA_FATAL)
}
}
call rg_lgain (im1, ls)
if (!IS_INDEFR(rg_lstatr(ls,GAIN)))
call rg_lsetr (ls, IGAIN, rg_lstatr (ls, GAIN))
call rg_lrdnoise (im1, ls)
if (!IS_INDEFR(rg_lstatr(ls,READNOISE)))
call rg_lsetr (ls, IREADNOISE, rg_lstatr (ls, READNOISE))
}
call rg_lsets (ls, IMAGE, Memc[image1])
# Open the input photometry file.
if (rpfd != NULL) {
if (fntgfnb (reglist, Memc[str], SZ_FNAME) != EOF) {
ipfd = open (Memc[str], READ_ONLY, TEXT_FILE)
call rg_lsets (ls, PHOTFILE, Memc[str])
}
nregions = rg_lrphot (ipfd, ls, 1, rg_lstati (ls,
NREGIONS), NO)
if (nregions <= 0 && interactive == NO)
call error (0,
"The input photometry file is empty.")
if (nregions < rg_lstati (ls, NREGIONS) && interactive == NO) {
call eprintf ("The input photometry file has fewer")
call eprintf (" objects than the reference photoemtry")
call eprintf (" file.\n")
call erract (EA_FATAL)
}
}
# Open the output image if any.
if (list2 == NULL) {
im2 = NULL
Memc[image2] = EOS
} else if (imtgetim (list2, Memc[image2], SZ_FNAME) != EOF) {
call xt_mkimtemp (Memc[image1], Memc[image2], Memc[imtemp],
SZ_FNAME)
im2 = immap (Memc[image2], NEW_COPY, im1)
} else {
im2 = NULL
Memc[image2] = EOS
}
call rg_lsets (ls, OUTIMAGE, Memc[image2])
# Get the record names.
if (reclist == NULL)
call strcpy (Memc[image1], Memc[str], SZ_FNAME)
else if (fntgfnb (reclist, Memc[str], SZ_FNAME) == EOF)
call strcpy (Memc[image1], Memc[str], SZ_FNAME)
call rg_lsets (ls, RECORD, Memc[str])
# Compute the initial shift.
if (sfd != NULL) {
call rg_lgshift (sfd, ls)
} else {
call rg_lsetr (ls, SXSHIFT, rg_lstatr (ls, XSHIFT))
call rg_lsetr (ls, SYSHIFT, rg_lstatr (ls, YSHIFT))
}
# Compute the scaling factors.
if (interactive == YES) {
stat = rg_liscale (imr, im1, im2, db, dformat, reglist,
rpfd, ipfd, sfd, ls, gd, id)
} else {
stat = rg_lscale (imr, im1, db, dformat, ls)
if (verbose == YES) {
if (rg_lstati(ls,BSALGORITHM) == LS_PHOTOMETRY ||
rg_lstati(ls,BZALGORITHM) == LS_PHOTOMETRY)
call rg_lstats (ls, PHOTFILE, Memc[str1], SZ_FNAME)
else
call strcpy (Memc[image1], Memc[str1], SZ_FNAME)
call rg_lstats (ls, REFIMAGE, Memc[str], SZ_LINE)
call printf (
"Average scale factors from %s to %s are %g %g\n")
call pargstr (Memc[str1])
call pargstr (Memc[str])
call pargr (rg_lstatr (ls, TBSCALE))
call pargr (rg_lstatr (ls, TBZERO))
}
}
# Scale the image.
if (im2 != NULL && stat == NO) {
if (verbose == YES) {
call printf (
"\tScaling image %s to image %s ...\n")
call pargstr (Memc[image1])
call pargstr (Memc[imtemp])
}
call imseti (im1, IM_CANCEL, YES)
call rg_limscale (im1, im2, rg_lstatr (ls, TBSCALE),
rg_lstatr (ls, TBZERO))
}
# Close up the input and output images.
if (im1 != NULL)
call imunmap (im1)
if (im2 != NULL) {
call imunmap (im2)
if (stat == YES)
call imdelete (Memc[image2])
else
call xt_delimtemp (Memc[image2], Memc[imtemp])
}
if (stat == YES)
break
}
# Close up the files and images.
if (imr != NULL)
call imunmap (imr)
# Close up the lists.
if (list1 != NULL)
call imtclose (list1)
if (listr != NULL) {
if (rg_lstati (ls, BZALGORITHM) == LS_PHOTOMETRY || rg_lstati(ls,
BSALGORITHM) == LS_PHOTOMETRY)
call fntclsb (listr)
else
call imtclose (listr)
}
if (list2 != NULL)
call imtclose (list2)
if (sfd != NULL)
call close (sfd)
if (rpfd != NULL)
call close (rpfd)
if (ipfd != NULL)
call close (ipfd)
if (shiftslist != NULL)
call fntclsb (shiftslist)
if (reglist != NULL)
call fntclsb (reglist)
if (reclist != NULL)
call fntclsb (reclist)
if (dformat == YES)
call dtunmap (db)
else
call close (db)
# Close up the graphics and image display devices.
if (gd != NULL)
call gclose (gd)
if (id != NULL)
call gclose (id)
# Free the matching structure.
call rg_lfree (ls)
call sfree (sp)
end
# RG_LGAIN -- Fetch the gain parameter from the image header.
procedure rg_lgain (im, ls)
pointer im #I pointer to the input image
pointer ls #I pointer to the intensity matching structure
int ip
pointer sp, key
real epadu
int ctor()
real imgetr()
errchk imgetr()
begin
call smark (sp)
call salloc (key, SZ_FNAME, TY_CHAR)
call rg_lstats (ls, CCDGAIN, Memc[key], SZ_FNAME)
ip = 1
if (ctor (Memc[key], ip, epadu) <= 0) {
iferr {
epadu = imgetr (im, Memc[key])
} then {
epadu = INDEFR
call eprintf ("Warning: Image %s Keyword %s not found.\n")
call pargstr (IM_HDRFILE(im))
call pargstr (Memc[key])
}
} else
epadu = INDEFR
if (IS_INDEFR(epadu) || epadu <= 0.0)
call rg_lsetr (ls, GAIN, INDEFR)
Else
call rg_lsetr (ls, GAIN, epadu)
call sfree (sp)
end
# LG_LRDNOISE -- Fetch the readout noise from the image header.
procedure rg_lrdnoise (im, ls)
pointer im #I pointer to the input image
pointer ls #I pointer to the intensity matching structure
int ip
pointer sp, key
real rdnoise
int ctor()
real imgetr()
errchk imgetr()
begin
call smark (sp)
call salloc (key, SZ_FNAME, TY_CHAR)
call rg_lstats (ls, CCDREAD, Memc[key], SZ_FNAME)
ip = 1
if (ctor (Memc[key], ip, rdnoise) <= 0) {
iferr {
rdnoise = imgetr (im, Memc[key])
} then {
rdnoise = INDEFR
call eprintf ("Warning: Image %s Keyword %s not found.\n")
call pargstr (IM_HDRFILE(im))
call pargstr (Memc[key])
}
} else
rdnoise = INDEFR
if (IS_INDEFR(rdnoise) || rdnoise <= 0.0)
call rg_lsetr (ls, READNOISE, INDEFR)
else
call rg_lsetr (ls, READNOISE, rdnoise)
call sfree (sp)
end
# RG_LGSHIFT -- Read the x and y shifts from a file
procedure rg_lgshift (fd, ls)
int fd #I input shifts file descriptor
pointer ls #I pointer to the intensity matching structure
real xshift, yshift
int fscan(), nscan()
begin
xshift = 0.0
yshift = 0.0
while (fscan(fd) != EOF) {
call gargr (xshift)
call gargr (yshift)
if (nscan() >= 2)
break
xshift = 0.0
yshift = 0.0
}
call rg_lsetr (ls, SXSHIFT, xshift)
call rg_lsetr (ls, SYSHIFT, yshift)
end
# RG_LIMSCALE -- Linearly scale the input image.
procedure rg_limscale (im1, im2, bscale, bzero)
pointer im1 #I pointer to the input image
pointer im2 #I pointer to the output image
real bscale #I the bscale value
real bzero #I the bzero value
int ncols
pointer sp, v1, v2, buf1, buf2
int imgnlr(), impnlr()
begin
call smark (sp)
call salloc (v1, IM_MAXDIM, TY_LONG)
call salloc (v2, IM_MAXDIM, TY_LONG)
ncols = IM_LEN(im1,1)
call amovkl (long(1), Meml[v1], IM_MAXDIM)
call amovkl (long(1), Meml[v2], IM_MAXDIM)
while (imgnlr (im1, buf1, Meml[v1]) != EOF) {
if (impnlr (im2, buf2, Meml[v2]) != EOF)
call altmr (Memr[buf1], Memr[buf2], ncols, bscale, bzero)
}
call sfree (sp)
end
|