In addition to writing out coordinate values generated by your program
(), you may also need to accept
coordinates entered by a user, or perhaps read from a file. In this
case, you will probably want to allow ``free-format'' input, so that
the user has some flexibility in the format that can be used. You will
probably also want to detect any typing errors.
Let's assume that you want to read a number of lines of text, each
containing the world coordinates of a single point, and to split each
line into individual numerical coordinate values. Using the FrameSet
pointer ``wcsinfo'' obtained earlier (), you
could proceed as follows:
#include <stdio.h> char *t; char text[ MAXCHARS + 2 ]; double coord[ 10 ]; int iaxis, n, naxes; ... /* Obtain the number of coordinate axes (if not already known). */ naxes = astGetI( wcsinfo, "Naxes" ); /* Loop to read each line of input text, in this case from the standard input stream (your programming environment will probably provide a better way of reading text than this). Set the pointer "t" to the start of each line read. */ while ( t = fgets( text, MAXCHARS + 2, stdin ) ) { /* Attempt to read a coordinate for each axis. */ for ( iaxis = 1; iaxis <= naxes; iaxis++ ) { n = astUnformat( wcsinfo, iaxis, t, &coord[ iaxis - 1 ] ); /* If nothing was read and this is not the first axis or the end-of-string, try stepping over a separator and reading again. */ if ( !n && ( iaxis > 1 ) && *t ) n = astUnformat( wcsinfo, iaxis, ++t, &coord[ iaxis - 1 ] ); /* Quit if nothing was read, otherwise move on to the next coordinate. */ if ( !n ) break; t += n; } /* Test for the possible errors that may occur... */ /* Error detected by AST (a message will have been issued). */ if ( !astOK ) { break; /* Error in input data at character t[n]. */ } else if ( *t || !n ) { <handle the error, or report your own message here> break; } else { <coordinates were read OK> } }
This algorithm has the advantage of accepting free-format input in whatever style is appropriate for the world coordinates in use (under the control of the FrameSet whose pointer you provide). For example, wavelength values might be read as floating point numbers (e.g. ``1.047'' or ``4787''), whereas celestial positions could be given in sexagesimal format (e.g. ``12:34:56'' or ``12 34.5'') and would be converted into radians. Individual coordinate values may be separated by white space and/or any non-ambiguous separator character, such as a comma.
For more information on reading coordinate values using the
astUnformat function, see . For
details of how sexagesimal formats are handled, and the forms of input
that may be used for celestial coordinates, see
.
AST A Library for Handling World Coordinate Systems in Astronomy