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
|
include "../../lib/astrom.h"
define SZ_HDRTEXT 5 * SZ_LINE
# T_AFILTCAT -- Filter existing astrometry catalogs.
procedure t_afiltcat()
pointer sp, input, output, catdb, catname, infname, outfname, tmpfname, hdrtext
pointer at, cq, res
int icatlist, ocatlist, catno, infd, outfd, nlines
bool standard, filter, update, verbose
pointer cq_map(), cq_fquery()
int fntopnb(), fntlenb(), fntgfnb(), cq_setcat(), open(), at_gcathdr()
int at_pcathdr()
bool streq(), clgetb()
errchk open()
begin
# Allocate some working space.
call smark (sp)
call salloc (input, SZ_FNAME, TY_CHAR)
call salloc (output, SZ_FNAME, TY_CHAR)
call salloc (catdb, SZ_FNAME, TY_CHAR)
call salloc (catname, SZ_FNAME, TY_CHAR)
call salloc (infname, SZ_FNAME, TY_CHAR)
call salloc (outfname, SZ_FNAME, TY_CHAR)
call salloc (tmpfname, SZ_FNAME, TY_CHAR)
call salloc (hdrtext, SZ_HDRTEXT, TY_CHAR)
# Get the important query parameters.
call clgstr ("input", Memc[input], SZ_FNAME)
call clgstr ("output", Memc[output], SZ_FNAME)
call clgstr ("catdb", Memc[catdb], SZ_FNAME)
call clgstr ("catalogs", Memc[catname], SZ_FNAME)
standard = clgetb ("standard")
filter = clgetb ("filter")
update = clgetb ("update")
verbose = clgetb ("verbose")
# Open the input catalog list.
icatlist = fntopnb (Memc[input], NO)
ocatlist = fntopnb (Memc[output], NO)
# Check that the input and output catalogs are the same size.
if (fntlenb (icatlist) != fntlenb (ocatlist)) {
if (verbose) {
call printf (
"Input and output file lists lengths are different\n")
call flush (STDOUT)
}
call fntclsb (icatlist)
call fntclsb (ocatlist)
call sfree (sp)
return
}
# Map the database.
cq = cq_map (Memc[catdb], READ_ONLY)
if (cq == NULL) {
if (verbose) {
call printf ("Cannot open catalog configuration file %s\n")
call pargstr (Memc[catdb])
call flush (STDOUT)
}
call fntclsb (icatlist)
call fntclsb (ocatlist)
call sfree (sp)
return
} else {
if (verbose) {
call printf ("\nOpening catalog configuration file %s ...\n")
call pargstr (Memc[catdb])
call flush (STDOUT)
}
}
# Locate the dummy record, usually called "stext".
catno = cq_setcat (cq, Memc[catname])
if (catno <= 0) {
if (verbose) {
call printf ("Cannot locate dummy catalog %s\n")
call pargstr (Memc[catname])
call flush (STDOUT)
}
call cq_unmap (cq)
call fntclsb (icatlist)
call fntclsb (ocatlist)
call sfree (sp)
return
} else {
if (verbose) {
call printf ("Selecting dummy catalog %s\n")
call pargstr (Memc[catname])
call flush (STDOUT)
}
}
# Initilize the astrometry data structure.
call at_afinit (at)
# Initialize the algorithm parameters.
call at_fapars (at)
# Store the input and output templates.
call at_sets (at, CATALOGS, Memc[catname])
call at_sets (at, INPUT, Memc[input])
call at_sets (at, OUTPUT, Memc[output])
call at_sets (at, CATDB, Memc[catdb])
call at_sets (at, CATNAME, Memc[catname])
# Loop over the input and output files.
while (fntgfnb (icatlist, Memc[infname], SZ_FNAME) != EOF &&
fntgfnb (ocatlist, Memc[outfname], SZ_FNAME) != EOF) {
# Store the input and output catalog names.
call at_sets (at, INFNAME, Memc[infname])
call at_sets (at, OUTFNAME, Memc[outfname])
# Create a temporary name and open the output file.
if (streq (Memc[infname], Memc[outfname]))
call mktemp ("tmp", Memc[tmpfname], SZ_FNAME)
else
call strcpy (Memc[outfname], Memc[tmpfname], SZ_FNAME)
iferr {
outfd = open (Memc[tmpfname], NEW_FILE, TEXT_FILE)
} then {
if (verbose) {
call printf (" Cannot open output file %s\n")
call pargstr (Memc[outfname])
call flush (STDOUT)
next
}
}
# Read the input catalog header.
infd = open (Memc[infname], READ_ONLY, TEXT_FILE)
nlines = at_gcathdr (infd, Memc[hdrtext], SZ_HDRTEXT)
call close (infd)
if (nlines <= 0)
nlines = at_pcathdr ("acatpars", Memc[hdrtext], SZ_HDRTEXT)
# Read in the catalog and make it look like the results
# of a query.
if (nlines > 0) {
res = cq_fquery (cq, Memc[infname], Memc[hdrtext])
if (res != NULL) {
if (filter) {
if (verbose) {
call printf (
" Filtering catalog %s to catalog %s\n")
call pargstr (Memc[infname])
call pargstr (Memc[outfname])
}
call at_wfilrecs (outfd, at, res, standard)
} else {
if (verbose) {
call printf (
" Copying catalog %s to catalog %s\n")
call pargstr (Memc[infname])
call pargstr (Memc[outfname])
}
call at_wnofilrecs (outfd, res, standard)
}
} else {
if (verbose) {
call printf (" Cannot read catalog %s\n")
call pargstr (Memc[infname])
call flush (STDOUT)
}
}
} else {
if (verbose) {
call printf (" Cannot decode catalog %s\n")
call pargstr (Memc[infname])
call flush (STDOUT)
}
res = NULL
}
# Close the results structure.
if (res != NULL)
call cq_rclose (res)
# Close the output file.
call close (outfd)
# Replace the existing file with the temporary one.
if (streq (Memc[infname], Memc[outfname])) {
call delete (Memc[infname])
call rename (Memc[tmpfname], Memc[infname])
}
}
# Free the database.
call cq_unmap (cq)
# Update the algorithm parameters.
if (update)
call at_fppars (at)
# Free the astrometry data structure.
call at_affree (at)
# Free the input catalog list.
call fntclsb (icatlist)
call fntclsb (ocatlist)
call sfree (sp)
end
|