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
|
/* Name:
stcschan-demo1.c
Purpose:
A demonstration of the facilities provided by the AST library
for reading STC metadata encoded using the STC-S linear string
format.
Description:
This program reads an STC-S description from a disk file, and
tests a given position to see if it is inside or outside the
AstroCoordsArea specified by the STC-S description.
Usage:
% stcschan-demo1 <stcs-file> <axis1> <axis2> ...
<stcs-file>: The path to the disk file containing the STC-S
description.
<axis1> <axis2> ...: The axis values at the position to be tested.
If insufficient values are supplied, a message describing the
required values is displayed (label, units, etc).
Example:
% stcschan-demo1 stcs-ex1.txt 1996-01-01T00:00:15 11:56:00 -11:30:00 \
1420.4 1000
To compile and link:
Assuming your starlink distribution is in "/star":
% gcc -o stcschan-demo1 stcschan-demo1.c -L/star/lib \
-I/star/include `ast_link`
*/
/* Include system headers. */
#include <stdio.h>
#include <string.h>
/* Include the AST library header. */
#include "ast.h"
/* Maximum number of axes in an STC-S AstroCoordSystem. */
#define MAX_AXES 5
/* Maximum allowed length for a single line of text form the disk file. */
#define MAX_LINE_LEN 500
/* Prototype for the function that reads text from the disk file. */
const char *source( void );
int main( int argc, char **argv ){
/* Local variables: */
AstKeyMap *warnings;
AstObject *object;
AstRegion *region;
AstStcsChan *channel;
FILE *fd;
char attrib[ 9 ];
char key[ 15 ];
const char *message;
double inpos[ MAX_AXES ];
double outpos[ MAX_AXES ];
int axis;
int iwarn;
int naxis;
int nc;
int status;
/* Initialised the returned system status to indicate success. */
status = 0;
/* Check a file was specified on the command line, and attempt to open it
for read access. */
if( argc < 2 ) {
printf( "Usage: stcschan-demo1 <stcs-file> <axis1> <axis2> ...\n" );
status = 1;
} else {
fd = fopen( argv[ 1 ], "r" );
if( !fd ) {
printf("Failed to open input file '%s'.\n", argv[ 1 ] );
status = 1;
}
}
/* If a disk file was opened successfully... */
if( !status ) {
/* Start an AST object context. This means we do not need to annull
each AST Object individually. Instead, all Objects created within
this context will be annulled automatically by the corresponding
invocation of astEnd. */
astBegin;
/* Create an StcsChan. This is the object that converts external STC-S
descriptions into corresponding AST Objects. Tell it to use the
"source" function for obtaining lines of text from the disk file. Also
tell it to store all warnings generated by the conversion for later
use. Other attributes of the StcsChan class retain their default
values. */
channel = astStcsChan( source, NULL, "ReportLevel=3" );
/* Associate the descriptor for the input disk file with the StcsChan.
This makes it available to the "source" function. Since this
application is single threaded, we could instead have made "fd" a
global variable, but the ChannelData facility is used here to illustrate
how to pass data to a source or sink function safely in a multi-threaded
application. */
astPutChannelData( channel, fd );
/* The default behaviour of the astRead function when used on an StcsChan is
to read and return the AstroCoordArea as an AST Region. This behaviour
can be changed by assigning appropriate values to the StcsChan attributes
"StcsArea", "StcsCoords" and "StcsProps". Options exist to return the
AstroCoords as an AST PointList, and/or to return the individual
property values read from the STC-S text in the form of an AST KeyMap
(a sort of hashmap). For now, just take the default action of reading the
AstroCoordsArea. */
object = astRead( channel );
/* The astRead function is a generic function and so returns a generic
AstObject pointer. Check an Object was created successfully. */
if( !object ) {
printf( "Failed to read an AST Object from file '%s'.\n",
argv[ 1 ] );
status = 1;
/* Now check that the object read is actually an AST Region, rather than
some other class of AST Object. */
} else if( !astIsARegion( object ) ) {
printf( "Expected a Region but read a %s from file '%s'.\n",
astGetC( object, "Class" ), argv[ 1 ] );
status = 1;
/* We now now know we have a Region so it is safe to use the pointer
returned by astRead as a Region pointer. Do the cast now to avoid
repeated casting in future. */
} else {
region = (AstRegion *) object;
/* Get the number of axes in the AstroCoordSystem, and check it is not
larger than expected. */
naxis = astGetI( region, "Naxes" );
if( naxis > MAX_AXES ) {
printf( "The coordinate system read from file '%s' has "
"too many axes (%d). Up to %d axes are allowed.\n",
argv[ 1 ], naxis, MAX_AXES );
status = 1;
/* Now check that the correct number of axis values were supplied on the
command line. If not, issue a warning message and give details of the
label and units for each axis. Note, AST axis indices are one-based,
in the range 1 to "Naxes". */
} else if( argc != 2 + naxis ) {
printf( "The coordinate system read from file '%s' has "
"%d axes, but %d axis values were supplied on the "
"command line. ", argv[ 1 ], naxis, argc - 2 );
printf( "Values are required for the following axes:\n");
for( axis = 1; axis <= naxis; axis++ ) {
sprintf( attrib, "Label(%d)", axis );
printf( " Axis %d: %s ", axis, astGetC( region, attrib ) );
sprintf( attrib, "Unit(%d)", axis );
printf( "(%s)\n", astGetC( region, attrib ) );
}
status = 1;
/* If the correct number of axis values was supplied on the command line,
convert the supplied strings into floating point axis values. Each
class of axis has its own formatting and unformatting rules that are
controlled by various attributes such as "Format" and "Digits". Values
for these attributes could be stored in the Region if different
unformatting conventions were preferred. */
} else {
for( axis = 1; axis <= naxis; axis++ ) {
nc = astUnformat( region, axis, argv[ axis + 1 ],
inpos + axis - 1 );
if( nc != strlen( argv[ axis + 1 ] ) ) {
sprintf( attrib, "Label(%d)", axis );
printf( "Failed to interpret '%s' as a value for axis "
"%d (%s).\n", argv[ axis + 1 ], axis,
astGetC( region, attrib ) );
status = 1;
break;
} else {
printf("%g ", inpos[ axis - 1 ] );
}
}
printf("\n");
}
/* If we have obtained a full set of floating point axis values, use the
Region as a Mapping to transform the supplied position. When a Region
is used as a Mapping, the transformation leaves all axis values
unchanged for interior positions, but assigns the magic value AST__BAD
to all axes for exterior positions. */
if( !status ) {
astTranN( region, 1, naxis, 1, inpos, 1, naxis, 1, outpos );
/* Issue a message describing the position tested and indicating if it is
inside or outside the AstroCoordsArea. */
printf( "\nThe position ( %s=%s", astGetC( region, "Symbol(1)" ),
argv[ 2 ] );
for( axis = 2; axis <= naxis; axis++ ) {
sprintf( attrib, "Symbol(%d)", axis );
printf(", %s=%s", astGetC( region, attrib ), argv[ axis + 1 ] );
}
printf( " ) is " );
if( outpos[ 0 ] == AST__BAD ) {
printf( "OUTSIDE" );
} else {
printf( "INSIDE" );
}
printf( " the region read from file '%s'.\n\n", argv[ 1 ] );
}
}
/* We asked the StcsChan to record any warnings that were generated
whilst converting the STC-S description into a corresponding AST
Object (a Region in this case). We now see if any such warnings were
generated by the earlier call to astRead. */
warnings = astWarnings( channel );
/* If any warnings were generated, and if no other error has occurred so
far, display the warnings. */
if( warnings && !status && astOK ) {
printf( "\nThe following warnings were issued reading file "
"'%s':\n", argv[ 1 ] );
/* The warnings are stored in an AST KeyMap (a sort of hashmap). Each
warning message is associated with a key of the form "Warning_1",
"Warning_2", etc. Loop round successive keys, obtaining a value for
each key from the warnings KeyMap, and displaying it. */
iwarn = 1;
while( astOK ) {
sprintf( key, "Warning_%d", iwarn++ );
if( astMapGet0C( warnings, key, &message ) ) {
printf( "\n- %s\n", message );
} else {
break;
}
}
}
/* End the AST Object context. All Objects created since the
corresponding invocation of astbegin will be annulled automatically. */
astEnd;
/* Close the disk file. */
(void) fclose( fd );
}
/* If an error occurred in the AST library, set the retiurns system
status non-zero. */
if( !astOK ) status = 1;
return status;
}
/* This is a function that reads a line of text from the disk file and
returns it to the AST library. It is called from within the astRead
function. */
const char *source( void ){
static char buffer[ MAX_LINE_LEN + 2 ];
FILE *fd = astChannelData;
return fgets( buffer, MAX_LINE_LEN + 2, fd );
}
|