aboutsummaryrefslogtreecommitdiff
path: root/noao/astcat/src/agetcat/t_ahedit.x
blob: c75ce3a76e64a9ea39473d5f34a6a330924329a7 (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
include "../../lib/astrom.h"
include <pkg/cq.h>

procedure t_ahedit()

pointer	sp, images, str1, str2
pointer	at, cq, res, im
int	j, imlist, catno, wcstype
bool	hupdate, wcsedit, hdredit, update, verbose
bool	clgetb()
pointer	cq_map(), cq_fimquery(), immap()
int	imtopen(), imtlen(), cq_setcat(), imtrgetim(), imaccess(), strdic()
errchk	immap(), imaccess()

begin
	# Allocate some working memory.
	call smark (sp)
	call salloc (images, SZ_FNAME, TY_CHAR)
	call salloc (str1, SZ_FNAME, TY_CHAR)
	call salloc (str2, SZ_FNAME, TY_CHAR)

	# Get the iportant query parameters.
	call clgstr ("images", Memc[images], SZ_FNAME)

	# Get the editing parameters.
	hupdate = clgetb ("hupdate")
	wcsedit = clgetb ("wcsedit")
	call clgstr ("wcs", Memc[str1], SZ_FNAME)
	wcstype = strdic (Memc[str1], Memc[str1], SZ_FNAME, CQ_WTYPESTR)
	if (wcstype <= 0)
	    wcstype = CQ_WNONE
	hdredit = clgetb ("hdredit")
	update = clgetb ("update")
	if (hupdate)
	    verbose = clgetb ("verbose")
	else
	    verbose = true

	# Open the image list.
	imlist = imtopen (Memc[images])
	if (imtlen (imlist) <= 0) {
	    if (verbose)
		call printf ("The input image list is empty\n")
	    call imtclose (imlist)
	    call sfree (sp)
	    return
	}

	# Initalize the data structures
	call at_ahinit (at)

	# Allocate the astrometry structure and read in the algorithm
	# parameters.
	call at_hapars (at)

	# Print the default wcs parameters.
	#call at_wcshow (at)
	# Print the default image data parameters.
	#call at_imshow (at)

	# Set the i/o parameters.
	call clgstr ("imsurveys", Memc[str1], SZ_FNAME)
	call clgstr ("imdb", Memc[str2], SZ_FNAME)
	call at_sets (at, IMAGES, Memc[images])
	call at_sets (at, SURVEYS, Memc[str1])
	call at_sets (at, IMDB, Memc[str2])

	# Print the i/o parameters.
	#call at_ioshow (at)

	# Open the catalog database.
	cq = cq_map (Memc[str2], READ_ONLY)
	if (cq == NULL) {
	    if (verbose) {
	        call printf ("\nCannot opening surveys database %s\n")
		    call pargstr (Memc[str2])
	    }
	    call at_ahfree (at)
	    call imtclose (imlist)
	    call sfree (sp)
	    return
	} else if (verbose) {
	    call printf ("\nOpening surveys database %s\n")
		call pargstr (Memc[str2])
	    call flush (STDOUT)
	}

	# Get the catalog name and save it.
	catno = cq_setcat (cq, Memc[str1])
	if (Memc[str1] == EOS) {
	    catno = ERR
	} else if (catno == 0) {
	    if (verbose) {
	        call printf ("Cannot locate survey %s\n")
	            call pargstr (Memc[str1])
	    }
	} else {
	    if (verbose) {
	        call printf ("Selecting survey %s\n")
		    call pargstr (Memc[str1])
	    }
	    call at_sets (at, CATNAME, Memc[str1])
	}

	# Loop over the field centers.
	do j = 1, imtlen (imlist) {

	    # Get the output image name.
	    if (imtrgetim (imlist, j, Memc[str1], SZ_FNAME) == EOF)
	        break
	    call at_sets (at, IMNAME, Memc[str1])

	    # Query the image survey to get the header info even though the
	    # image already exists.
	    if (verbose) {
		call printf ("Getting image %s ...\n")
		    call pargstr (Memc[str1])
		    call flush (STDOUT)
	    }
	    if (catno <= 0)
		res = NULL
	    else
		res = cq_fimquery (cq, Memc[str1])

	    # Open the output file.
	    iferr {
		if (imaccess (Memc[str1], READ_WRITE) == YES) {
		    im = immap (Memc[str1], READ_WRITE, 0)
		    if (wcsedit) {
		        if (res != NULL)
		            call at_wedit (im, res, NULL, wcstype, hupdate,
			        verbose) 
		        else
		            call at_wedit (im, NULL, at, wcstype, hupdate,
			        verbose) 
		    }
		    if (hdredit) {
		        if (res != NULL)
		            call at_hedit (im, res, NULL, hupdate, verbose)
		        else
		            call at_hedit (im, NULL, at, hupdate, verbose)
		    }
	            call imunmap (im)
		} else
		    im = NULL
	    } then {
	        if (verbose) {
		    call printf ("    Warning %s is not a valid image name\n")
			call pargstr (Memc[str1])
		}
		im = NULL
	    }

	    # Close the query structure.
	    if (res != NULL)
		call cq_imclose (res)

	}

	# Close the catalog database.
	call cq_unmap (cq)

	# Update the algorithm parameters.
	if (update)
	    call at_hppars (at)

	# Close the image lists.
	call imtclose (imlist)

	# Free the astrometry structure.
	call at_ahfree (at)

	# Free the working memory.
	call sfree (sp)
end