next up previous
Next: Writing Objects to Files
Up: Saving and Restoring Objects (Channels)
Previous: Mixing Objects with other Text

Reading Objects from Files  

Thus far, we have only considered the default behaviour of a Channel in reading and writing Objects through a program's standard input and output streams. We will now consider how to access Objects stored in files more directly.

Because the AST library is designed to be used from more than one language, it has to be a little careful about reading and writing to files. This is due to the incompatibilities that often exist between the file I/O facilities provided by different languages. Fortunately, this ties in well with the principle that AST should also be independent of any particular data storage system, which we mention again in [*].

What this means in practice is that you must provide some simple Fortran routines that perform the actual transfer of data to and from files and similar external data stores. The routines you provide are supplied as the source and/or sink routine arguments to AST_CHANNEL when you create a Channel ([*]). An example is the best way to illustrate this.

Consider the following simple subroutine called SOURCE. It reads a single line of text from a Fortran I/O unit and then calls AST_PUTLINE to pass it to the AST library, together with its length. It sets this length to be negative if there is no more input:

      SUBROUTINE SOURCE( STATUS )
      INTEGER STATUS
      CHARACTER * ( 200 ) BUFFER

      READ( 1, '(A)', END = 99 ) BUFFER
      CALL AST_PUTLINE( BUFFER, LEN( BUFFER ), STATUS )
      RETURN

 99   CALL AST_PUTLINE( BUFFER, -1, STATUS )      
      END

Our main program might then look something like this (omitting error checking for brevity):

      EXTERNAL SOURCE

      ...

*  Open the input file.
      OPEN( UNIT = 1, FILE = 'infile.ast', STATUS = 'OLD' )

*  Create the Channel and read an Object from it.
      CHANNEL = AST_CHANNEL( SOURCE, AST_NULL, ' ', STATUS )
      OBJECT = AST_READ( CHANNEL, STATUS )

      ...

*  Annul the Channel and close the file when done.
      CALL AST_ANNUL( CHANNEL, STATUS )
      CLOSE( 1 )

Here, we first open the required input file. We then pass the name of our SOURCE routine as the first argument to AST_CHANNEL when creating a new Channel (ensuring that SOURCE also appears in an EXTERNAL statement). When we read an Object from this Channel using AST_READ, the SOURCE routine will be called to obtain the textual data from the file, the end-of-file being detected when it yields a negative line length.



next up previous
Next: Writing Objects to Files
Up: Saving and Restoring Objects (Channels)
Previous: Mixing Objects with other Text

AST A Library for Handling World Coordinate Systems in Astronomy
Starlink User Note 210
R.F. Warren-Smith & D.S. Berry
30th April 2003
E-mail:ussc@star.rl.ac.uk

Copyright (C) 2003 Central Laboratory of the Research Councils