aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imcoords/src/t_skyctran.x
blob: 05a7e82497eaa8d8576a5e03dd0fa5879facb4f0 (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
include <fset.h>
include <pkg/skywcs.h>

procedure t_skyctran()

bool	verbose, transform, first_file
int	inlist, outlist, linlist, loutlist, lngcolumn, latcolumn, infd, outfd
int	ilngunits, ilatunits, olngunits, olatunits, min_sigdigits, optype
int	instat, outstat, nilng, nilat, plngcolumn, platcolumn, pxcolumn
int	rvcolumn
double	ilngmin, ilngmax, ilatmin, ilatmax
int	fstati()
pointer	sp, inname, outname, insystem, outsystem, olngformat, olatformat
pointer	ilngformat, ilatformat, str, mwin, mwout, cooin, cooout

bool	clgetb(), streq()
double	clgetd()
int	clpopnu(), clplen(), clgfil(), open(), sk_decwcs()
int	clgeti(), clgwrd(), sk_stati()
errchk	clgwrd()

begin
	call smark (sp)
	call salloc (inname, SZ_FNAME, TY_CHAR)
	call salloc (outname, SZ_FNAME, TY_CHAR)
	call salloc (insystem, SZ_FNAME, TY_CHAR)
	call salloc (outsystem, SZ_FNAME, TY_CHAR)
	call salloc (ilngformat, SZ_FNAME, TY_CHAR)
	call salloc (ilatformat, SZ_FNAME, TY_CHAR)
	call salloc (olngformat, SZ_FNAME, TY_CHAR)
	call salloc (olatformat, SZ_FNAME, TY_CHAR)
	call salloc (str, SZ_FNAME, TY_CHAR)

	# Open the input and output file lists.
	inlist = clpopnu ("input")
	linlist = clplen (inlist)
	outlist = clpopnu ("output")
	loutlist = clplen (outlist)
	call clgstr ("insystem", Memc[insystem], SZ_FNAME)
	call clgstr ("outsystem", Memc[outsystem], SZ_FNAME)
	transform = clgetb ("transform")

	# Fetch the file formatting parameters.
	lngcolumn = clgeti ("lngcolumn")
	latcolumn = clgeti ("latcolumn")
	plngcolumn = clgeti ("plngcolumn")
	platcolumn = clgeti ("platcolumn")
	pxcolumn = clgeti ("pxcolumn")
	rvcolumn = clgeti ("rvcolumn")
	ilngmin = clgetd ("ilngmin")
	ilngmax = clgetd ("ilngmax")
	ilatmin = clgetd ("ilatmin")
	ilatmax = clgetd ("ilatmax")
	nilng = clgeti ("nilng")
	nilat = clgeti ("nilat")
	iferr (ilngunits = clgwrd ("ilngunits", Memc[str], SZ_FNAME,
	    SKY_LNG_UNITLIST))
	    ilngunits = 0
	iferr (ilatunits = clgwrd ("ilatunits", Memc[str], SZ_FNAME,
	    SKY_LAT_UNITLIST))
	    ilatunits = 0
	call clgstr ("ilngformat", Memc[ilngformat], SZ_FNAME)
	call clgstr ("ilatformat", Memc[ilatformat], SZ_FNAME)

	iferr (olngunits = clgwrd ("olngunits", Memc[str], SZ_FNAME,
	    SKY_LNG_UNITLIST))
	    olngunits = 0
	iferr (olatunits = clgwrd ("olatunits", Memc[str], SZ_FNAME,
	    SKY_LAT_UNITLIST))
	    olatunits = 0
	call clgstr ("olngformat", Memc[olngformat], SZ_FNAME)
	call clgstr ("olatformat", Memc[olatformat], SZ_FNAME)
	#min_sigdigits = clgeti ("min_sigdigits")
	min_sigdigits = 7
	verbose = clgetb ("verbose")

	# Test the length of the input coordinate list.
	if (linlist < 1)
	    call error (0, "The input coordinate file list is empty")
	if (loutlist < 1)
	    call error (0, "The output coordinate file list is empty")
	if (loutlist > 1 && loutlist != linlist)
	    call error (0,
	        "The number of input and output files are not the same")

	# Determine the input coordinate system.
	instat = sk_decwcs (Memc[insystem], mwin, cooin, NULL)

	# Determine the output coordinate system.
	outstat = sk_decwcs (Memc[outsystem], mwout, cooout, NULL)

	# Loop over the input files.
	first_file = true
	while (clgfil (inlist, Memc[inname], SZ_FNAME) != EOF) {

	    # Open the input coordinate file. The string "imcursor" is
	    # reserved for the image display cursor.
	    if (streq (Memc[inname], "imcursor") && mwin != NULL) {
		infd = NULL
		optype = sk_stati (cooin, S_PIXTYPE)
		call sk_seti (cooin, S_PIXTYPE, PIXTYPE_TV)
	    } else if (streq (Memc[inname], "grid")) {
		optype = sk_stati (cooin, S_PIXTYPE)
		infd = NULL
	    } else
		infd = open (Memc[inname], READ_ONLY, TEXT_FILE)

	    # Open the output coordinate file.
	    if (clgfil (outlist, Memc[outname], SZ_FNAME) != EOF) {
		outfd = open (Memc[outname], NEW_FILE, TEXT_FILE)
		if (streq (Memc[outname], "STDOUT") || outfd == STDOUT)
		    call fseti (outfd, F_FLUSHNL, YES)
		call fprintf (outfd, "\n")
		if (instat == ERR)
		    call fprintf (outfd,
			"# Error decoding the input coordinate system\n")
		call sk_iiwrite (outfd, "Insystem", Memc[insystem], mwin,
	            cooin)
		if (outstat == ERR)
		    call fprintf (outfd,
		        "# Error decoding the output coordinate system\n")
	        call sk_iiwrite (outfd, "Outsystem", Memc[outsystem], mwout,
	            cooout)
	    } 

	    # Print information about the input and output coordinate system
	    # and the input and output files to the standard output.
	    if (verbose && outfd != STDOUT) {
		if (first_file) {
		    call printf ("\n")
	            if (instat == ERR)
		        call printf (
		            "Error decoding the input coordinate system\n")
	            call sk_iiprint ("Insystem", Memc[insystem], mwin, cooin)
	            if (outstat == ERR)
		        call printf (
		            "Error decoding the output coordinate system\n")
	            call sk_iiprint ("Outsystem", Memc[outsystem], mwout,
		        cooout)
		    call printf ("\n")
		}
		call printf ("Input file: %s  Output file: %s\n")
		    call pargstr (Memc[inname])
		    call pargstr (Memc[outname])
		call flush (STDOUT)
	    }


	    # Print the input and output file name banner.
	    call fprintf (outfd, "\n# Input file: %s  Output file: %s\n")
		call pargstr (Memc[inname])
		call pargstr (Memc[outname])
	    call fprintf (outfd, "\n")

	    # Transform the coordinate list.
	    if (infd == NULL) {
		if (streq ("imcursor", Memc[inname]))
		    call sk_curtran (outfd, mwin, mwout, cooin, cooout,
		        olngunits, olatunits, Memc[olngformat],
			Memc[olatformat], transform)
		else if (instat == ERR || outstat == ERR)
		    call sk_grcopy (outfd, cooin, cooout, ilngmin, ilngmax,
		        nilng, ilatmin, ilatmax, nilat, ilngunits,
		        ilatunits, olngunits, olatunits, Memc[ilngformat],
		        Memc[ilatformat], Memc[olngformat],
		        Memc[olatformat], transform) 
		else
		    call sk_grtran (outfd, mwin, mwout, cooin, cooout,
		        ilngmin, ilngmax, nilng, ilatmin, ilatmax, nilat,
		        ilngunits, ilatunits, olngunits, olatunits,
			Memc[ilngformat], Memc[ilatformat], Memc[olngformat],
			Memc[olatformat], transform)
	    } else {
		if (infd == STDIN && fstati(STDIN, F_REDIR) == NO)
	            call sk_ttytran (infd, outfd, mwin, mwout, cooin, cooout,
	                ilngunits, ilatunits, olngunits, olatunits,
			Memc[olngformat], Memc[olatformat])
		else if (instat == ERR || outstat == ERR)
		    call sk_copytran (infd, outfd, lngcolumn, latcolumn,
		        transform)
		else
	            call sk_listran (infd, outfd, mwin, mwout, cooin, cooout,
	                lngcolumn, latcolumn, plngcolumn, platcolumn,
			pxcolumn, rvcolumn, ilngunits, ilatunits, olngunits,
		        olatunits, Memc[olngformat], Memc[olatformat],
		        min_sigdigits, transform)
	    }

	    # Close the output coordinate file.
	    if (linlist == loutlist)
	        call close (outfd)

	    # Close the input coordinate file.
	    if (infd != NULL)
	        call close (infd)
	    else
		call sk_seti (cooin, S_PIXTYPE, optype)

	    first_file = false
	}

	# Close the image wcs if one was opened.
	if (loutlist < linlist)
	    call close (outfd)
	if (mwin != NULL)
	    call mw_close (mwin)
	if (mwout != NULL)
	    call mw_close (mwout)
	#call mfree (cooin, TY_STRUCT)
	call sk_close (cooin)
	#call mfree (cooout, TY_STRUCT)
	call sk_close (cooout)

	# Close up the lists.
	call clpcls (inlist)
	call clpcls (outlist)

	call sfree (sp)
end