Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.astro.louisville.edu/software/sbig/archive/xmccd-4.1/xmccd-4.1e/docs/cfitsio/fitsio.pdf
Äàòà èçìåíåíèÿ: Sat Mar 20 08:28:08 2010
Äàòà èíäåêñèðîâàíèÿ: Thu Feb 27 22:33:40 2014
Êîäèðîâêà:

Ïîèñêîâûå ñëîâà: ï ï ï ï ï ï ï ï ï
FITSIO User's Guide
A Subroutine Interface to FITS Format Files for Fortran Programmers

Version 3.0

HEASARC Code 662 Goddard Space Flight Center Greenb elt, MD 20771 USA

April 2009


ii


Contents
1 Introduction 2 Creating FITSIO/CFITSIO 2.1 2.2 2.3 2.4 2.5 2.6 2.7 3 Building the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Linking Programs with FITSIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting Started with FITSIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Example Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Legal Stuff ......................................... 1 3 3 6 7 8 8 9

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 13 15

A FITS Primer

4 FITSIO Conventions and Guidelines 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

CFITSIO Size Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Multiple Access to the Same FITS File . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Current Header Data Unit (CHDU) . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Subroutine Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Subroutine Families and Datatyp es . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Implicit Data Typ e Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Data Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Error Status Values and the Error Message Stack . . . . . . . . . . . . . . . . . . . . 18 Variable-Length Array Facility in Binary Tables . . . . . . . . . . . . . . . . . . . . . 19

4.10 Supp ort for IEEE Sp ecial Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 4.11 When the Final Size of the FITS HDU is Unknown . . . . . . . . . . . . . . . . . . . 21 4.12 Local FITS Conventions supp orted by FITSIO . . . . . . . . . . . . . . . . . . . . . 21 iii


iv

CONTENTS 4.12.1 Supp ort for Long String Keyword Values. . . . . . . . . . . . . . . . . . . . . 21 4.12.2 Arrays of Fixed-Length Strings in Binary Tables . . . . . . . . . . . . . . . . 22 4.12.3 Keyword Units Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.12.4 HIERARCH Convention for Extended Keyword Names . . . . . . . . . . . . 23 4.13 Optimizing Code for Maximum Processing Sp eed . . . . . . . . . . . . . . . . . . . . 24 4.13.1 Background Information: How CFITSIO Manages Data I/O . . . . . . . . . 25 29

5

Basic Interface Routines 5.1 5.2 5.3 5.4

FITSIO Error Status Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 File I/O Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Keyword I/O Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Data I/O Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 35

6

Advanced Interface Subroutines 6.1 6.2 6.3 6.4

FITS File Op en and Close Subroutines: . . . . . . . . . . . . . . . . . . . . . . . . . 35 HDU-Level Op erations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 . . . . . . . . . . . . . . . . . . . . . 41

Define or Redefine the structure of the CHDU

FITS Header I/O Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.4.7 6.4.8 Header Space and Position Routines . . . . . . . . . . . . . . . . . . . . . . . 43 Read or Write Standard Header Routines . . . . . . . . . . . . . . . . . . . . 43 Write Keyword Subroutines Insert Keyword Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Read Keyword Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Modify Keyword Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Up date Keyword Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . 50

Delete Keyword Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 . . . . . . . . . . . . . . . . . . . . . 51 . . . . . . . . . . . . . 52

6.5 6.6 6.7

Data Scaling and Undefined Pixel Parameters

FITS Primary Array or IMAGE Extension I/O Subroutines

FITS ASCI I and Binary Table Data I/O Subroutines . . . . . . . . . . . . . . . . . . 55 6.7.1 6.7.2 6.7.3 6.7.4 Column Information Subroutines . . . . . . . . . . . . . . . . . . . . . . . . 55

Low-Level Table Access Subroutines . . . . . . . . . . . . . . . . . . . . . . . 58 Edit Rows or Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Read and Write Column Data Routines . . . . . . . . . . . . . . . . . . . . . 60 . . . . . . . . . . . . . . . . . . . . . . . . . 64

6.8

Row Selection and Calculator Routines


CONTENTS 6.9 Celestial Coordinate System Subroutines

v . . . . . . . . . . . . . . . . . . . . . . . . 65

6.10 File Checksum Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 6.11 Date and Time Utility Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 6.12 General Utility Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 7 8 The CFITSIO Iterator Function Extended File Name Syntax 8.1 8.2 75 77

Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Filetyp e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 8.2.1 8.2.2 8.2.3 Notes ab out HTTP proxy servers . . . . . . . . . . . . . . . . . . . . . . . . . 80 Notes ab out the root filetyp e . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Notes ab out the shmem filetyp e: . . . . . . . . . . . . . . . . . . . . . . . . . 82

8.3 8.4 8.5 8.6 8.7 8.8 8.9

Base Filename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Output File Name when Op ening an Existing File . . . . . . . . . . . . . . . . . . . 85 Template File Name when Creating a New File . . . . . . . . . . . . . . . . . . . . . 86 Image Tile-Compression Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . 86

HDU Location Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Image Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Image Transform Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 . . . . . . . . . . . . . . . . . . . . . . 90

8.10 Column and Keyword Filtering Sp ecification

8.11 Row Filtering Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 8.11.1 General Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 8.11.2 Bit Masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.11.3 Vector Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

8.11.4 Good Time Interval Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 8.11.5 Spatial Region Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 8.11.6 Example Row Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.12 Binning or Histogramming Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . 102 9 Template Files 9.1 9.2 9.3 105

Detailed Template Line Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Auto-indexing of Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Template Parser Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107


vi 9.4 9.5 9.6

CONTENTS Formal Template Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 111 119 125

10 Summary of all FITSIO User-Interface Subroutines 11 Parameter Definitions 12 FITSIO Error Status Codes


Chapter 1

Introduction
This document describ es the Fortran-callable subroutine interface that is provided as part of the CFITSIO library (which is written in ANSI C). This is a companion document to the CFITSIO User's Guide which should b e consulted for further information ab out the underlying CFITSIO library. In the remainder of this document, the terms FITSIO and CFITSIO are interchangeable and refer to the same library. FITSIO/CFITSIO is a machine-indep endent library of routines for reading and writing data files in the FITS (Flexible Image Transp ort System) data format. It can also read IRAF format image files and raw binary data arrays by converting them on the fly into a virtual FITS format file. This library was written to provide a p owerful yet simple interface for accessing FITS files which will run on most commonly used computers and workstations. FITSIO supp orts all the features describ ed in the official NOST definition of the FITS format and can read and write all the currently defined typ es of extensions, including ASCI I tables (TABLE), Binary tables (BINTABLE) and IMAGE extensions. The FITSIO subroutines insulate the programmer from having to deal with the complicated formatting details in the FITS file, however, it is assumed that users have a general knowledge ab out the structure and usage of FITS files. The CFITSIO package was initially develop ed by the HEASARC (High Energy Astrophysics Science Archive Research Center) at the NASA Goddard Space Flight Center to convert various existing and newly acquired astronomical data sets into FITS format and to further analyze data already in FITS format. New features continue to b e added to CFITSIO in large part due to contributions of ideas or actual code from users of the package. The Integral Science Data Center in Switzerland, and the XMM/ESTEC pro ject in The Netherlands made esp ecially significant contributions that resulted in many of the new features that app eared in v2.0 of CFITSIO. The latest version of the CFITSIO source code, documentation, and example programs are available on the World-Wide Web or via anonymous ftp from: http://heasarc.gsfc.nasa.gov/fitsio ftp://legacy.gsfc.nasa.gov/software/fitsio/c

1


2

CHAPTER 1. INTRODUCTION

Any questions, bug rep orts, or suggested enhancements related to the CFITSIO package should b e sent to the primary author: D H N G r E A r . A S e William Pence SARC, Code 662 A/Goddard Space Flight Center enbelt, MD 20771, USA Telephone: (301) 286-4599 E-mail: William.D.Pence@nasa.gov

This User's Guide assumes that readers already have a general understanding of the definition and structure of FITS format files. Further information ab out FITS formats is available from the FITS Supp ort Office at http://fits.gsfc.nasa.gov. In particular, the 'NOST FITS Standard' gives the authoritative definition of the FITS data format, and the `FITS User's Guide' provides additional historical background and practical advice on using FITS files. CFITSIO users may also b e interested in the FTOOLS package of programs that can b e used to manipulate and analyze FITS format files. Information ab out FTOOLS can b e obtained on the Web or via anonymous ftp at: http://heasarc.gsfc.nasa.gov/ftools ftp://legacy.gsfc.nasa.gov/software/ftools/release


Chapter 2

Creating FITSIO/CFITSIO
2.1 Building the Library

To use the FITSIO subroutines one must first build the CFITSIO library, which requires a C compiler. gcc is ideal, or most other ANSI-C compilers will also work. The CFITSIO code is contained in ab out 40 C source files (*.c) and header files (*.h). On VAX/VMS systems 2 assemblycode files (vmsieeed.mar and vmsieeer.mar) are also needed. The Fortran interface subroutines to the C CFITSIO routines are located in the f77 wrap1.c, through f77 wrap4.c files. These are relatively simple 'wrapp ers' that translate the arguments in the Fortran subroutine into the appropriate format for the corresp onding C routine. This translation is p erformed transparently to the user by a set of C macros located in the cfortran.h file. Unfortunately cfortran.h does not supp ort every combination of C and Fortran compilers so the Fortran interface is not supp orted on all platforms. (see further notes b elow). A standard combination of C and Fortran compilers will b e assumed by default, but one may also sp ecify a particular Fortran compiler by doing: > setenv CFLAGS -DcompilerName=1

(where 'compilerName' is the name of the compiler) b efore running the configure command. The currently recognized compiler names are: g I C p N f h a s C 7 B L g A 2 p p u R 7 M I i G c u o n A F R P F f F x l F Y o 2 P o 9 o F l o F r F E r 0 r o o r o t o R t F t r F t r r r F r o r t o r t a t o a r a r r a r n r r n t n a t n a

an tran ran n ran n 3


4 m D v C P A A S i E m O o b b X p C s N w s s F s F F V e o o o F o o E r f f r o r r X S t t t r t t F t U P r t r r o a N r a r a a r t I o n a n n t i X F n

CHAPTER 2.

CREATING FITSIO/CFITSIO

r o F o

a n o r

n Fortran rtran tran

Alternatively, one may edit the CFLAGS line in the Makefile to add the '-DcompilerName' flag after running the './configure' command. The CFITSIO library is built on Unix systems by typing: > > > ./configure [--prefix=/target/installation/path] make (or 'make shared') make install (this step is optional)

at the op erating system prompt. The configure command customizes the Makefile for the particular system, then the `make' command compiles the source files and builds the library. Typ e `./configure' and not simply `configure' to ensure that the configure script in the current directory is run and not some other system-wide configure script. The optional 'prefix' argument to configure gives the path to the directory where the CFITSIO library and include files should b e installed via the later 'make install' command. For example, > ./configure --prefix=/usr1/local will cause the 'make install' command to copy the CFITSIO lib cfitsio file to /usr1/local/lib and the necessary include file to /usr1/local/include (assuming of course that the process has p ermission to write to these directories). By default this also builds the set of Fortran-callable wrapp er routines whose calling sequences are describ ed later in this document. The 'make shared' option builds a shared or dynamic version of the CFITSIO library. When using the shared library the executable code is not copied into your program at link time and instead the program locates the necessary library code at run time, normally through LD LIBRARY PATH or some other method. The advantages of using a shared library are: 1. 2. L L l e P l t s N e e i n o i h e o ss disk space if you build more than 1 program ss memory if more than one copy of a program using brary is running at the same time since the system ough to share copies of the shared library at run t ssibly easier maintenance since a new version of th brary can be installed without relinking all the so at uses it (as long as the subroutine names and cal quences remain unchanged). run-time penalty.

3.

t i i e f l

he shared s smart me. shared tware ing

4.


2.1. BUILDING THE LIBRARY The disadvantages are: 1. M s 2. T r e o p h e i r e e a t e c r d h hassle at ially or h e may be a ing the sh er really runtime. You have to either build the programs ave LD_LIBRARY_PATH set right. slight start up penalty, depending on where you are ared library and the program from and if your CPU is slow or really heavily loaded.

5

On HP/UX systems, the environment variable CFLAGS should b e set to -Ae b efore running configure to enable "extended ANSI" features. It may not b e p ossible to statically link programs that use CFITSIO on some platforms (namely, on Solaris 2.6) due to the network drivers (which provide FTP and HTTP access to FITS files). It is p ossible to make b oth a dynamic and a static version of the CFITSIO library, but network file access will not b e p ossible using the static version. On VAX/VMS and ALPHA/VMS systems the make gfloat.com command file may b e executed to build the cfitsio.olb ob ject library using the default G-floating p oint option for double variables. The make dfloat.com and make ieee.com files may b e used instead to build the library with the other floating p oint options. Note that the getcwd function that is used in the group.c module may require that programs using CFITSIO b e linked with the ALPHA$LIBRARY:VAXCRTL.OLB library. See the example link line in the next section of this document. On Windows IBM-PC typ e platforms the situation is more complicated b ecause of the wide variety of Fortran compilers that are available and b ecause of the inherent complexities of calling the CFITSIO C routines from Fortran. Two different versions of the CFITSIO dll library are available, compiled with the Borland C++ compiler and the Microsoft Visual C++ compiler, resp ectively, in the files cfitsiodll 2xxx b orland.zip and cfitsiodll 2xxx vcc.zip, where '2xxx' represents the current release numb er. Both these dll libraries contain a set of Fortran wrapp er routines which may b e compatible with some, but probably not all, available Fortran compilers. To test if they are compatible, compile the program testf77.f and try linking to these dll libraries. If these libraries do not work with a particular Fortran compiler, then there are 2 p ossible solutions. The first solution would b e to modify the file cfortran.h for that particular combination of C and Fortran compilers, and then rebuild the CFITSIO dll library. This will require, however, a some exp ertise in mixed language programming. The other solution is to use the older v5.03 Fortran-77 implementation of FITSIO that is still available from the FITSIO web-site. This version is no longer supp orted, but it does provide the basic functions for reading and writing FITS files and should b e compatible with most Fortran compilers. CFITSIO has currently b een tested on the following platforms: OP S S S S D D E u u i i e E R n n l l c C ATING SYSTEM OS Solaris icon Graphics IRIX icon Graphics IRIX64 Alpha OSF/1 station Ultrix CO g g g M g g M c c c I c c P c c c P c c ILER and and and S and

cc (3.0.1) cc cc cc


6 D D H I L M W W W O M e E P B i k i i i S a c C M n L n n n / c Alpha OpenVMS VAX/VMS UX AIX ux inux dows 95/98/NT dows 95/98/NT/ME/XP dows 95/98/NT 2 OS 7.1 or greater c g g g g D B M C g M c c c c c R o i y c e

CHAPTER 2.

CREATING FITSIO/CFITSIO

c c c c 3 r c g c t

and cc

la ro wi + ro

nd C++ V4.5 soft/Compaq Visual C++ v5.0, v6.0 n gcc EMX werks 10.+

CFITSIO will probably run on most other Unix platforms. Cray sup ercomputers are currently not supp orted.

2.2

Testing the Library

The CFITSIO library should b e tested by building and running the testprog.c program that is included with the release. On Unix systems typ e: % % % % m t d c a e i m k s f p e testp tprog > f testp testpr rog testprog.lis rog.lis testprog.out og.fit testprog.std

On VMS systems, (assuming cc is the name of the C compiler command), typ e: $ cc testprog.c $ link testprog, cfitsio/lib, alpha$library:vaxcrtl/lib $ run testprog The testprog program should produce a FITS file called `testprog.fit' that is identical to the `testprog.std' FITS file included with this release. The diagnostic messages (which were pip ed to the file testprog.lis in the Unix example) should b e identical to the listing contained in the file testprog.out. The 'diff ' and 'cmp' commands shown ab ove should not rep ort any differences in the files. (There may b e some minor formatting differences, such as the presence or absence of leading zeros, or 3 digit exp onents in numb ers, which can b e ignored). The Fortran wrapp ers in CFITSIO may b e tested with the testf77 program. On Unix systems the fortran compilation and link command may b e called 'f77' or 'g77', dep ending on the system. % f77 -o testf77 testf77.f -L. -lcfitsio -lnsl -lsocket or % f77 -f -o testf77 testf77.f -L. -lcfitsio (under SUN O/S)


2.3. LINKING PROGRAMS WITH FITSIO or % f77 -o testf77 testf77.f -Wl,-L. -lcfitsio -lm -lnsl -lsocket (HP/UX) or % g77 -o testf77 -s testf77.f -lcfitsio -lcc_dynamic -lncurses (Mac OS-X) % testf77 > testf77.lis % diff testf77.lis testf77.out % cmp testf77.fit testf77.std

7

On machines running SUN O/S, Fortran programs must b e compiled with the '-f ' option to force double precision variables to b e aligned on 8-byte b oundaries to make the fortran-declared variables compatible with C. A similar compiler option may b e required on other platforms. Failing to use this option may cause the program to crash on FITSIO routines that read or write double precision variables. Also note that on some systems, the output listing of the testf77 program may differ slightly from the testf77.std template, if leading zeros are not printed by default b efore the decimal p oint when using F format. A few other utility programs are included with CFITSIO: speed - measures the maximum throughput (in MB per second) for writing and reading FITS files with CFITSIO listhead - lists all the header keywords in any FITS file fitscopy - copies any FITS file (especially useful in conjunction with the CFITSIO's extended input filename syntax) cookbook - a sample program that performs common read and write operations on a FITS file. iter_a, iter_b, iter_c - examples of the CFITSIO iterator routine The first 4 of these utility programs can b e compiled and linked by typing % make program_name

2.3

Linking Programs with FITSIO

When linking applications software with the FITSIO library, several system libraries usually need to b e sp ecified on the link comman Unix systems, the most reliable way to determine what libraries are required is to typ e 'make testprog' and see what libraries the configure script has added. The typical libraries that may need to b e added are -lm (the math library) and -lnsl and -lsocket (needed only for FTP and HTTP file access). These latter 2 libraries are not needed on VMS and Windows platforms, b ecause FTP file access is not currently supp orted on those platforms.


8

CHAPTER 2.

CREATING FITSIO/CFITSIO

Note that when upgrading to a newer version of CFITSIO it is usually necessary to recompile, as well as relink, the programs that use CFITSIO, b ecause the definitions in fitsio.h often change.

2.4

Getting Started with FITSIO

In order to effectively use the FITSIO library as quickly as p ossible, it is recommended that new users follow these steps: 1. Read the following `FITS Primer' chapter for a brief overview of the structure of FITS files. This is esp ecially imp ortant for users who have not previously dealt with the FITS table and image extensions. 2. Write a simple program to read or write a FITS file using the Basic Interface routines. 3. Refer to the cookb ook.f program that is included with this release for examples of routines that p erform various common FITS file op erations. 4. Read Chapters 4 and 5 to b ecome familiar with the conventions and advanced features of the FITSIO interface. 5. Scan through the more extensive set of routines that are provided in the `Advanced Interface'. These routines p erform more sp ecialized functions than are provided by the Basic Interface routines.

2.5

Example Program

The following listing shows an example of how to use the FITSIO routines in a Fortran program. Refer to the cookb ook.f program that is included with the FITSIO distribution for examples of other FITS programs. program writeimage C Create a FITS primary array containing a 2-D image i i c l n n h o t t a g e e r i g g a c e e c a r r t l st i, er si a j f m t , i p u g l l s r e e , o n , u u a e n p m x i , e t t f * e , p 8 n blocksize,bitpix,naxis,naxes(2) ixel,nelements,array(300,200) 0 d

C

status=0 Name of the FITS file to be created: filename='ATESTFILE.FITS' Get an unused Logical Unit Number to use to create the FITS file call ftgiou(unit,status) create the new empty FITS file blocksize=1

C

C


2.6. LEGAL STUFF call ftinit(unit,filename,blocksize,status) C i s b n n n e n i i a a a x i m t x x x t t p p i e e e i l i s s s n a e x = ( ( d l = = 2 1 2 = ize parameters about the FITS image (300 x 200 16-bit integers) .true. 16 )=300 )=200 .true.

9

C

write the required header keywords call ftphpr(unit,simple,bitpix,naxis,naxes,0,1,extend,status) initialize the values in the image with a linear ramp function do j=1,naxes(2) do i=1,naxes(1) array(i,j)=i+j end do end do w g f n c r r p e a i o i l l t u x e l e the array to the FITS file p=1 el=1 ments=naxes(1)*naxes(2) ftpprj(unit,group,fpixel,nelements,array,status)

C

C

C

write another optional keyword to the header call ftpkyj(unit,'EXPOSURE',1500,'Total Exposure Time',status) c c c e l a a n ose the file and free the unit number ll ftclos(unit, status) ll ftfiou(unit, status) d

C

2.6

Legal Stuff

Copyright (Unpublished­all rights reserved under the copyright laws of the United States), U.S. Government as represented by the Administrator of the National Aeronautics and Space Administration. No copyright is claimed in the United States under Title 17, U.S. Code. Permission to freely use, copy, modify, and distribute this software and its documentation without fee is hereby granted, provided that this copyright notice and disclaimer of warranty app ears in all copies. (However, see the restriction on the use of the gzip compression code, b elow). DISCLAIMER: THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EI-


10

CHAPTER 2.

CREATING FITSIO/CFITSIO

THER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NASA BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, CONTRACT, TORT , OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER." The file compress.c contains (slightly modified) source code that originally came from gzip-1.2.4, copyright (C) 1992-1993 by Jean-loup Gailly. This gzip code is distributed under the GNU General Public License and thus requires that any software that uses the CFITSIO library (which in turn uses the gzip code) must conform to the provisions in the GNU General Public License. A copy of the GNU license is included at the b eginning of compress.c file. Similarly, the file wcsutil.c contains 2 slightly modified routines from the Classic AIPS package that are also distributed under the GNU General Public License. Alternate versions of the compress.c and wcsutil.c files (called compress alternate.c and wcsutil alternate.c) are provided for users who want to use the CFITSIO library but are unwilling or unable to publicly release their software under the terms of the GNU General Public License. These alternate versions contains non-functional stubs for the file compression and uncompression routines and the world coordinate transformation routines used by CFITSIO. Replace the file `compress.c' with `compress alternate.c' and 'wcsutil.c' with 'wcsutil alternate.c b efore compiling the CFITSIO library. This will produce a version of CFITSIO which does not supp ort reading or writing compressed FITS files, or doing image coordinate transformations, but is otherwise identical to the standard version.

2.7

Acknowledgments

The development of many of the p owerful features in CFITSIO was made p ossible through collaborations with many p eople or organizations from around the world. The following, in particular, have made esp ecially significant contributions: Programmers from the Integral Science Data Center, Switzerland (namely, Jurek Borkowski, Bruce O'Neel, and Don Jennings), designed the concept for the plug-in I/O drivers that was introduced with CFITSIO 2.0. The use of `drivers' greatly simplified the low-level I/O, which in turn made other new features in CFITSIO (e.g., supp ort for compressed FITS files and supp ort for IRAF format image files) much easier to implement. Jurek Borkowski wrote the Shared Memory driver, and Bruce O'Neel wrote the drivers for accessing FITS files over the network using the FTP, HTTP, and ROOT protocols. The ISDC also provided the template parsing routines (written by Jurek Borkowski) and the


2.7. ACKNOWLEDGMENTS

11

hierarchical grouping routines (written by Don Jennings). The ISDC DAL (Data Access Layer) routines are layered on top of CFITSIO and make extensive use of these features. Uwe Lammers (XMM/ESA/ESTEC, The Netherlands) designed the high-p erformance lexical parsing algorithm that is used to do on-the-fly filtering of FITS tables. This algorithm essentially pre-compiles the user-supplied selection expression into a form that can b e rapidly evaluated for each row. Peter Wilson (RSTX, NASA/GSFC) then wrote the parsing routines used by CFITSIO based on Lammers' design, combined with other techniques such as the CFITSIO iterator routine to further enhance the data processing throughput. This effort also b enefited from a much earlier lexical parsing routine that was develop ed by Kent Blackburn (NASA/GSFC). More recently, Craig Markwardt (NASA/GSFC) implemented additional functions (median, average, stddev) and other enhancements to the lexical parser. The CFITSIO iterator function is loosely based on similar ideas develop ed for the XMM Data Access Layer. Peter Wilson (RSTX, NASA/GSFC) wrote the complete set of Fortran-callable wrapp ers for all the CFITSIO routines, which in turn rely on the CFORTRAN macro develop ed by Burkhard Burow. The syntax used by CFITSIO for filtering or binning input FITS files is based on ideas develop ed for the AXAF Science Center Data Model by Jonathan McDowell, Antonella Fruscione, Aneta Siemiginowska and Bill Joye. See http://heasarc.gsfc.nasa.gov/docs/journal/axaf7.html for further description of the AXAF Data Model. The file decompression code were taken directly from the gzip (GNU zip) program develop ed by Jean-loup Gailly and others. Doug Mink, SAO, provided the routines for converting IRAF format images into FITS format. Martin Reinecke (Max Planck Institute, Garching)) provided the modifications to cfortran.h that are necessary to supp ort 64-bit integer values when calling C routines from fortran programs. The cfortran.h macros were originally develop ed by Burkhard Burow (CERN). In addition, many other p eople have made valuable contributions to the development of CFITSIO. These include (with ap ologies to others that may have inadvertently b een omitted): Steve Allen, Carl Akerlof, Keith Arnaud, Morten Krabb e Barfoed, Kent Blackburn, G Bodammer, Romke Bontekoe, Lucio Chiapp etti, Keith Costorf, Robin Corb et, John Davis, Richard Fink, Ning Gan, Emily Greene, Joe Harrington, Cheng Ho, Phil Hodge, Jim Ingham, Yoshitaka Ishisaki, Diab Jerius, Mark Levine, Todd Karakaskian, Edward King, Scott Koch, Claire Larkin, Rob Managan, Eric Mandel, John Mattox, Carsten Meyer, Emi Miyata, Stefan Mochnacki, Mike Noble, Oliver Ob erdorf, Clive Page, Arvind Parmar, Jeff Pedelty, Tim Pearson, Maren Purves, Scott Randall, Chris Rogers, Arnold Rots, Barry Schlesinger, Robin Stebbins, Andrew Szymkowiak, Allyn Tennant, Peter Teub en, James Theiler, Doug Tody, Shiro Ueno, Steve Walton, Archie Warnock, Alan Watson, Dan Whipple, Wim Wimmers, Peter Young, Jianjun Xu, and Nelson Zarate.


12

CHAPTER 2.

CREATING FITSIO/CFITSIO


Chapter 3

A FITS Primer
This section gives a brief overview of the structure of FITS files. Users should refer to the documentation available from the NOST, as describ ed in the introduction, for more detailed information on FITS formats. FITS was first develop ed in the late 1970's as a standard data interchange format b etween various astronomical observatories. Since then FITS has b ecome the defacto standard data format supp orted by most astronomical data analysis software packages. A FITS file consists of one or more Header + Data Units (HDUs), where the first HDU is called the `Primary HDU', or `Primary Array'. The primary array contains an N-dimensional array of pixels, such as a 1-D sp ectrum, a 2-D image, or a 3-D data cub e. Six different primary datatyp es are supp orted: Unsigned 8-bit bytes, 16, 32, and 64-bit signed integers, and 32 and 64-bit floating p oint reals. FITS also has a convention for storing unsigned integers (see the later section entitled `Unsigned Integers' for more details). The primary HDU may also consist of only a header with a null array containing no data pixels. Any numb er of additional HDUs may follow the primary array; these additional HDUs are called FITS `extensions'. There are currently 3 typ es of extensions defined by the FITS standard: · Image Extension - a N-dimensional array of pixels, like in a primary array · ASCI I Table Extension - rows and columns of data in ASCI I character format · Binary Table Extension - rows and columns of data in binary representation In each case the HDU consists of an ASCI I Header Unit followed by an optional Data Unit. For historical reasons, each Header or Data unit must b e an exact multiple of 2880 8-bit bytes long. Any unused space is padded with fill characters (ASCI I blanks or zeros). Each Header Unit consists of any numb er of 80-character keyword records or `card images' which have the general form: KEYNAME = value / comment string NULLKEY = / comment: This keyword has no value 13


14

CHAPTER 3.

A FITS PRIMER

The keyword names may b e up to 8 characters long and can only contain upp ercase letters, the digits 0-9, the hyphen, and the underscore character. The keyword name is (usually) followed by an equals sign and a space character (= ) in columns 9 - 10 of the record, followed by the value of the keyword which may b e either an integer, a floating p oint numb er, a character string (enclosed in single quotes), or a b oolean value (the letter T or F). A keyword may also have a null or undefined value if there is no sp ecified value string, as in the second example. The last keyword in the header is always the `END' keyword which has no value or comment fields. There are many rules governing the exact format of a keyword record (see the NOST FITS Standard) so it is b etter to rely on standard interface software like FITSIO to correctly construct or to parse the keyword records rather than try to deal directly with the raw FITS formats. Each Header Unit b egins with a series of required keywords which dep end on the typ e of HDU. These required keywords sp ecify the size and format of the following Data Unit. The header may contain other optional keywords to describ e other asp ects of the data, such as the units or scaling values. Other COMMENT or HISTORY keywords are also frequently added to further document the data file. The optional Data Unit immediately follows the last 2880-byte block in the Header Unit. Some HDUs do not have a Data Unit and only consist of the Header Unit. If there is more than one HDU in the FITS file, then the Header Unit of the next HDU immediately follows the last 2880-byte block of the previous Data Unit (or Header Unit if there is no Data Unit). The main required keywords in FITS primary arrays or image extensions are: · BITPIX ­ defines the datatyp e of the array: 8, 16, 32, 64, -32, -64 for unsigned 8­bit byte, 16­bit signed integer, 32­bit signed integer, 64­bit signed integer, 32­bit IEEE floating p oint, and 64­bit IEEE double precision floating p oint, resp ectively. · NAXIS ­ the numb er of dimensions in the array, usually 0, 1, 2, 3, or 4. · NAXISn ­ (n ranges from 1 to NAXIS) defines the size of each dimension. FITS tables start with the keyword XTENSION = `TABLE' (for ASCI I tables) or XTENSION = `BINTABLE' (for binary tables) and have the following main keywords: · TFIELDS ­ numb er of fields or columns in the table · NAXIS2 ­ numb er of rows in the table · TTYPEn ­ for each column (n ranges from 1 to TFIELDS) gives the name of the column · TFORMn ­ the datatyp e of the column · TUNITn ­ the physical units of the column (optional) Users should refer to the FITS Supp ort Office at http://fits.gsfc.nasa.gov for further information ab out the FITS format and related software packages.


Chapter 4

FITSIO Conventions and Guidelines
4.1 CFITSIO Size Limitations

CFITSIO places few restrictions on the size of FITS files that it reads or writes. There are a few limits, however, which may affect some extreme cases: 1. The maximum numb er of FITS files that may b e simultaneously op ened by CFITSIO is set by NMAXFILES as defined in fitsio2.h. It is currently set = 300 by default. CFITSIO will allocate ab out 80 * NMAXFILES bytes of memory for internal use. Note that the underlying C compiler or op erating system, may have a smaller limit on the numb er of op ened files. The C symb olic constant FOPEN MAX is intended to define the maximum numb er of files that may op en at once (including any other text or binary files that may b e op en, not just FITS files). On some systems it has b een found that gcc supp orts a maximum of 255 op ened files. 2. By default, CFITSIO can handle FITS files up to 2.1 GB in size (2**31 bytes). This file size limit is often imp osed by 32-bit op erating systems. More recently, as 64-bit op erating systems b ecome more common, an industry-wide standard (at least on Unix systems) has b een develop ed to supp ort larger sized files (see http://ftp.sas.com/standards/large.file/). Starting with version 2.1 of CFITSIO, larger FITS files up to 6 terabytes in size may b e read and written on supp orted platforms. In order to supp ort these larger files, CFITSIO must b e compiled with the '-D LARGEFILE SOURCE' and `-D FILE OFFSET BITS=64' compiler flags. Some platforms may also require the `-D LARGE FILES' compiler flag. This causes the compiler to allocate 8bytes instead of 4-bytes for the `off t' datatyp e which is used to store file offset p ositions. It app ears that in most cases it is not necessary to also include these compiler flags when compiling programs that link to the CFITSIO library. If CFITSIO is compiled with the -D LARGEFILE SOURCE and -D FILE OFFSET BITS=64 flags on a platform that supp orts large files, then it can read and write FITS files that contain up to 2**31 2880-byte FITS records, or approximately 6 terabytes in size. It is still required that the value of the NAXISn and PCOUNT keywords in each extension b e within the range of a signed 4byte integer (max value = 2,147,483,648). Thus, each dimension of an image (given by the NAXISn keywords), the total width of a table (NAXIS1 keyword), the numb er of rows in a table (NAXIS2 keyword), and the total size of the variable-length array heap in binary tables (PCOUNT keyword) must b e less than this limit. 15


16

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

Currently, supp ort for large files within CFITSIO has b een tested on the Linux, Solaris, and IBM AIX op erating systems.

4.2

Multiple Access to the Same FITS File

CFITSIO supp orts simultaneous read and write access to multiple HDUs in the same FITS file. Thus, one can op en the same FITS file twice within a single program and move to 2 different HDUs in the file, and then read and write data or keywords to the 2 extensions just as if one were accessing 2 completely separate FITS files. Since in general it is not p ossible to physically op en the same file twice and then exp ect to b e able to simultaneously (or in alternating succession) write to 2 different locations in the file, CFITSIO recognizes when the file to b e op ened (in the call to fits op en file) has already b een op ened and instead of actually op ening the file again, just logically links the new file to the old file. (This only applies if the file is op ened more than once within the same program, and does not prevent the same file from b eing simultaneously op ened by more than one program). Then b efore CFITSIO reads or writes to either (logical) file, it makes sure that any modifications made to the other file have b een completely flushed from the internal buffers to the file. Thus, in principle, one could op en a file twice, in one case p ointing to the first extension and in the other p ointing to the 2nd extension and then write data to b oth extensions, in any order, without danger of corrupting the file, There may b e some efficiency p enalties in doing this however, since CFITSIO has to flush all the internal buffers related to one file b efore switching to the other, so it would still b e prudent to minimize the numb er of times one switches back and forth b etween doing I/O to different HDUs in the same file.

4.3

Current Header Data Unit (CHDU)

In general, a FITS file can contain multiple Header Data Units, also called extensions. CFITSIO only op erates within one HDU at any given time, and the currently selected HDU is called the Current Header Data Unit (CHDU). When a FITS file is first created or op ened the CHDU is automatically defined to b e the first HDU (i.e., the primary array). CFITSIO routines are provided to move to and op en any other existing HDU within the FITS file or to app end or insert a new HDU in the FITS file which then b ecomes the CHDU.

4.4

Subroutine Names

All FITSIO subroutine names b egin with the letters 'ft' to distinguish them from other subroutines and are 5 or 6 characters long. Users should not name their own subroutines b eginning with 'ft' to avoid conflicts. (The SPP interface routines all b egin with 'fs'). Subroutines which read or get information from the FITS file have names b eginning with 'ftg...'. Subroutines which write or put information into the FITS file have names b eginning with 'ftp...'.


4.5. SUBROUTINE FAMILIES AND DATATYPES

17

4.5

Subroutine Families and Datatyp es

Many of the subroutines come in families which differ only in the datatyp e of the associated parameter(s) . The datatyp e of these subroutines is indicated by the last letter of the subroutine name (e.g., 'j' in 'ftpkyj') as follows: x b i j k e f d g c m l s b c s i l r r d d c d l c i h h n o e e o o o o o h t a o t n a a u u m u g a

r r e g l l b b p b i r

acter*1 (unsigned byte) t integer (I*2) ger (I*4, 32-bit integer) long integer (I*8, 64-bit integer) exponential floating point (R*4) fixed-format floating point (R*4) le precision real floating-point (R*8) le precision fixed-format floating point (R*8) lex reals (pairs of R*4 values) le precision complex (pairs of R*8 values) cal (L*4) acter string

When dealing with the FITS byte datatyp e, it is imp ortant to rememb er that the raw values (b efore any scaling by the BSCALE and BZERO, or TSCALn and TZEROn keyword values) in byte arrays (BITPIX = 8) or byte columns (TFORMn = 'B') are interpreted as unsigned bytes with values ranging from 0 to 255. Some Fortran compilers supp ort a non-standard byte datatyp e such as INTEGER*1, LOGICAL*1, or BYTE, which can sometimes b e used instead of CHARACTER*1 variables. Many machines p ermit passing a numeric datatyp e (such as INTEGER*1) to the FITSIO subroutines which are exp ecting a CHARACTER*1 datatyp e, but this technically violates the Fortran-77 standard and is not supp orted on all machines (e.g., on a VAX/VMS machine one must use the VAX-sp ecific %DESCR function). One feature of the as though it were the same as a `2B' to read and write CFITSIO routines is that they can op erate on a `X' (bit) column in a binary table a `B' (byte) column. For example a `11X' datatyp e column can b e interpreted column (i.e., 2 unsigned 8-bit bytes). In some instances, it can b e more efficient whole bytes at a time, rather than reading or writing each individual bit.

The double precision complex datatyp e is not a standard Fortran-77 datatyp e. If a particular Fortran compiler does not directly supp ort this datatyp e, then one may instead pass an array of pairs of double precision values to these subroutines. The first value in each pair is the real part, and the second is the imaginary part.

4.6

Implicit Data Typ e Conversion
can p erform implicit data typ e conversion. the program does not need to b e the same conversion is supp orted for numerical and enclosed in quotes) when reading a FITS

The FITSIO routines that read and write numerical data This means that the data typ e of the variable or array in as the data typ e of the value in the FITS file. Data typ e string data typ es (if the string contains a valid numb er


18

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

header keyword value and for numeric values when reading or writing values in the primary array or a table column. CFITSIO returns status = NUM OVERFLOW if the converted data value exceeds the range of the output data typ e. Implicit data typ e conversion is not supp orted within binary tables for string, logical, complex, or double complex data typ es. In addition, any table column may b e read as if it contained string values. In the case of numeric columns the returned string will b e formatted using the TDISPn display format if it exists.

4.7

Data Scaling

When reading numerical data values in the primary array or a table column, the values will b e scaled automatically by the BSCALE and BZERO (or TSCALn and TZEROn) header keyword values if they are present in the header. The scaled data that is returned to the reading program will have output value = (FITS value) * BSCALE + BZERO (a corresp onding formula using TSCALn and TZEROn is used when reading from In the case of integer output values the floating p oint scaled value is truncated to rounded to the nearest integer). The ftpscl and fttscl subroutines may b e used scaling parameters defined in the header (e.g., to turn off the scaling so that the pr the raw unscaled values from the FITS file). table columns). an integer (not to override the ogram can read

When writing numerical data to the primary array or to a table column the data values will generally b e automatically inversely scaled by the value of the BSCALE and BZERO (or TSCALn and TZEROn) header keyword values if they they exist in the header. These keywords must have b een written to the header b efore any data is written for them to have any effect. Otherwise, one may use the ftpscl and fttscl subroutines to define or override the scaling keywords in the header (e.g., to turn off the scaling so that the program can write the raw unscaled values into the FITS file). If scaling is p erformed, the inverse scaled output value that is written into the FITS file will have FITS value = ((input value) - BZERO) / BSCALE (a corresp onding formula using TSCALn and TZEROn is used when writing to table columns). Rounding to the nearest integer, rather than truncation, is p erformed when writing integer datatyp es to the FITS file.

4.8

Error Status Values and the Error Message Stack

The last parameter in nearly every FITSIO subroutine is the error status value which is b oth an input and an output parameter. A returned p ositive value for this parameter indicates an error was detected. A listing of all the FITSIO status code values is given at the end of this document. The FITSIO library uses an `inherited status' convention for the status parameter which means that if a subroutine is called with a p ositive input value of the status parameter, then the subroutine will


4.9. VARIABLE-LENGTH ARRAY FACILITY IN BINARY TABLES

19

exit immediately without changing the value of the status parameter. Thus, if one passes the status value returned from each FITSIO routine as input to the next FITSIO subroutine, then whenever an error is detected all further FITSIO processing will cease. This convention can simplify the error checking in application programs b ecause it is not necessary to check the value of the status parameter after every single FITSIO subroutine call. If a program contains a sequence of several FITSIO calls, one can just check the status value after the last call. Since the returned status values are generally distinctive, it should b e p ossible to determine which subroutine originally returned the error status. FITSIO also maintains an internal stack of error messages (80-character maximum length) which in many cases provide a more detailed explanation of the cause of the error than is provided by the error status numb er alone. It is recommended that the error message stack b e printed out whenever a program detects a FITSIO error. To do this, call the FTGMSG routine rep eatedly to get the successive messages on the stack. When the stack is empty FTGMSG will return a blank string. Note that this is a `First In ­ First Out' stack, so the oldest error message is returned first by ftgmsg.

4.9

Variable-Length Array Facility in Binary Tables

FITSIO provides easy-to-use supp ort for reading and writing data in variable length fields of a binary table. The variable length columns have TFORMn keyword values of the form `1Pt(len)' or `1Qt(len)' where `t' is the datatyp e code (e.g., I, J, E, D, etc.) and `len' is an integer sp ecifying the maximum length of the vector in the table. If the value of `len' is not sp ecified when the table is created (e.g., if the TFORM keyword value is simply sp ecified as '1PE' instead of '1PE(400) ), then FITSIO will automatically scan the table when it is closed to determine the maximum length of the vector and will app end this value to the TFORMn value. The same routines which read and write data in an ordinary fixed length binary table extension are also used for variable length fields, however, the subroutine parameters take on a slightly different interpretation as describ ed b elow. All the data in a variable length field is written into an area called the `heap' which follows the main fixed-length FITS binary table. The size of the heap, in bytes, is sp ecified with the PCOUNT keyword in the FITS header. When creating a new binary table, the initial value of PCOUNT should usually b e set to zero. FITSIO will recompute the size of the heap as the data is written and will automatically up date the PCOUNT keyword value when the table is closed. When writing variable length data to a table, CFITSIO will automatically extend the size of the heap area if necessary, so that any following HDUs do not get overwritten. By default the heap data area starts immediately after the last row of the fixed-length table. This default starting location may b e overridden by the THEAP keyword, but this is not recommended. If additional rows of data are added to the table, CFITSIO will automatically shift the the heap down to make room for the new rows, but it is obviously b e more efficient to initially create the table with the necessary numb er of blank rows, so that the heap does not needed to b e constantly moved. When writing to a variable length field, the entire array of values for a given row of the table must b e written with a single call to FTPCLx. The total length of the array is calculated from


20

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

(NELEM+FELEM-1). One cannot app end more elements to an existing field at a later time; any attempt to do so will simply overwrite all the data which was previously written. Note also that the new data will b e written to a new area of the heap and the heap space used by the previous write cannot b e reclaimed. For this reason it is advised that each row of a variable length field only b e written once. An exception to this general rule occurs when setting elements of an array as undefined. One must first write a dummy value into the array with FTPCLx, and then call FTPCLU to flag the desired elements as undefined. (Do not use the FTPCNx family of routines with variable length fields). Note that the rows of a table, whether fixed or variable length, do not have to b e written consecutively and may b e written in any order. When writing to a variable length ASCI I character field (e.g., TFORM = '1PA') only a single character string written. FTPCLS writes the whole length of the input string (minus any trailing blank characters), thus the NELEM and FELEM parameters are ignored. If the input string is completely blank then FITSIO will write one blank character to the FITS file. Similarly, FTGCVS and FTGCFS read the entire string (truncated to the width of the character string argument in the subroutine call) and also ignore the NELEM and FELEM parameters. The FTPDES subroutine is useful in situations where multiple rows of a variable length column have the identical array of values. One can simply write the array once for the first row, and then use FTPDES to write the same descriptor values into the other rows (use the FTGDES routine to read the first descriptor value); all the rows will then p oint to the same storage location thus saving disk space. When reading from a variable length array field one can only read as many elements as actually exist in that row of the table; reading does not automatically continue with the next row of the table as occurs when reading an ordinary fixed length table field. Attempts to read more than this will cause an error status to b e returned. One can determine the numb er of elements in each row of a variable column with the FTGDES subroutine.

4.10

Supp ort for IEEE Sp ecial Values

The ANSI/IEEE-754 floating-p oint numb er standard defines certain sp ecial values that are used to represent such quantities as Not-a-Numb er (NaN), denormalized, underflow, overflow, and infinity. (See the App endix in the NOST FITS standard or the NOST FITS User's Guide for a list of these values). The FITSIO subroutines that read floating p oint data in FITS files recognize these IEEE sp ecial values and by default interpret the overflow and infinity values as b eing equivalent to a NaN, and convert the underflow and denormalized values into zeros. In some cases programmers may want access to the raw IEEE values, without any modification by FITSIO. This can b e done by calling the FTGPVx or FTGCVx routines while sp ecifying 0.0 as the value of the NULLVAL parameter. This will force FITSIO to simply pass the IEEE values through to the application program, without any modification. This does not work for double precision values on VAX/VMS machines, however, where there is no easy way to bypass the default interpretation of the IEEE sp ecial values.


4.11. WHEN THE FINAL SIZE OF THE FITS HDU IS UNKNOWN

21

4.11

When the Final Size of the FITS HDU is Unknown

It is not required to know the total size of a FITS data array or table b efore b eginning to write the data to the FITS file. In the case of the primary array or an image extension, one should initially create the array with the size of the highest dimension (largest NAXISn keyword) set to a dummy value, such as 1. Then after all the data have b een written and the true dimensions are known, then the NAXISn value should b e up dated using the fits up date key routine b efore moving to another extension or closing the FITS file. When writing to FITS tables, CFITSIO automatically keeps track of the highest row numb er that is written to, and will increase the size of the table if necessary. CFITSIO will also automatically insert space in the FITS file if necessary, to ensure that the data 'heap', if it exists, and/or any additional HDUs that follow the table do not get overwritten as new rows are written to the table. As a general rule it is b est to sp ecify the initial numb er of rows = 0 when the table is created, then let CFITSIO keep track of the numb er of rows that are actually written. The application program should not manually up date the numb er of rows in the table (as given by the NAXIS2 keyword) since CFITSIO does this automatically. If a table is initially created with more than zero rows, then this will usually b e considered as the minimum size of the table, even if fewer rows are actually written to the table. Thus, if a table is initially created with NAXIS2 = 20, and CFITSIO only writes 10 rows of data b efore closing the table, then NAXIS2 will remain equal to 20. If however, 30 rows of data are written to this table, then NAXIS2 will b e increased from 20 to 30. The one exception to this automatic up dating of the NAXIS2 keyword is if the application program directly modifies the value of NAXIS2 (up or down) itself just b efore closing the table. In this case, CFITSIO does not up date NAXIS2 again, since it assumes that the application program must have had a good reason for changing the value directly. This is not recommended, however, and is only provided for backward compatibility with software that initially creates a table with a large numb er of rows, than decreases the NAXIS2 value to the actual smaller value just b efore closing the table.

4.12

Lo cal FITS Conventions supp orted by FITSIO
p orts several local FITS conventions which are not defined in the official NOST FITS which are not necessarily recognized or supp orted by other FITS software packages. should b e cautious ab out using these features, esp ecially if the FITS files that are exp ected to b e processed by other software systems which do not use the CFITSIO

CFITSIO sup standard and Programmers produced are interface.

4.12.1

Support for Long String Keyword Values.

The length of a standard FITS string keyword is limited to 68 characters b ecause it must fit entirely within a single FITS header keyword record. In some instances it is necessary to encode strings longer than this limit, so FITSIO supp orts a local convention in which the string value is continued over multiple keywords. This continuation convention uses an amp ersand character at the end of each substring to indicate that it is continued on the next keyword, and the continuation keywords all have the name CONTINUE without an equal sign in column 9. The string value may b e


22

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

continued in this way over as many additional CONTINUE keywords as is required. The following lines illustrate this continuation convention which is used in the value of the STRKEY keyword: L S C C O T O O N R N N G K T T S E I I TRN= 'O Y = 'T NUE ' NUE 'F G h v I I i a T P s l S 1.0' / The O is a very long string ue that is continued ov header.' / This is ano G k e t I e r h P Long String Convention may be used. yword&' / Optional Comment 3 keywords in the & ' er optional comment.

It is recommended that the LONGSTRN keyword, as shown here, always b e included in any HDU that uses this longstring convention. A subroutine called FTPLSW has b een provided in CFITSIO to write this keyword if it does not already exist. This long string convention is supp orted by the following FITSIO subroutines that deal with stringvalued keywords: f f f f f f t t t t t t g p i m u d k k k k k k y l l l l e s s s s s y r w i m u d e r n o p e a i s d d l d t e i a e a e( rt fy te te s a a t a a tring keyword ppend) a string keyword string keyword he value of an existing string keyword n existing keyword, or write a new keyword keyword file, so that is ensure enough

These routines will transparently read, write, or delete a long string value in the FITS programmers in general do not have to b e concerned ab out the details of the convention used to encode the long string in the FITS header. When reading a long string, one must that the character string parameter used in these subroutine calls has b een declared long to hold the entire string, otherwise the returned string value will b e truncated.

Note that the more commonly used FITSIO subroutine to write string valued keywords (FTPKYS) does NOT supp ort this long string convention and only supp orts strings up to 68 characters in length. This has b een done delib erately to prevent programs from inadvertently writing keywords using this non-standard convention without the explicit intent of the programmer or user. The FTPKLS subroutine must b e called instead to write long strings. This routine can also b e used to write ordinary string values less than 68 characters in length.

4.12.2

Arrays of Fixed-Length Strings in Binary Tables

CFITSIO supp orts 2 ways to sp ecify that a character column in a binary table contains an array of fixed-length strings. The first way, which is officially supp orted by the FITS Standard document, uses the TDIMn keyword. For example, if TFORMn = '60A' and TDIMn = '(12,5)' then that column will b e interpreted as containing an array of 5 strings, each 12 characters long. FITSIO also supp orts a local convention for the format of the TFORMn keyword value of the form 'rAw' where 'r' is an integer sp ecifying the total width in characters of the column, and 'w' is an integer sp ecifying the (fixed) length of an individual unit string within the vector. For example, TFORM1 = '120A10' would indicate that the binary table column is 120 characters wide and consists of 12 10-character length strings. This convention is recognized by the FITSIO


4.12. LOCAL FITS CONVENTIONS SUPPORTED BY FITSIO

23

subroutines that read or write strings in binary tables. The Binary Table definition document sp ecifies that other optional characters may follow the datatyp e code in the TFORM keyword, so this local convention is in compliance with the FITS standard, although other FITS readers are not required to recognize this convention. The Binary Table definition document that was approved by the IAU in 1994 contains an app endix describing an alternate convention for sp ecifying arrays of fixed or variable length strings in a binary table character column (with the form 'rA:SSTRw/nnn)'. This app endix was not officially voted on by the IAU and hence is still provisional. FITSIO does not currently supp ort this prop osal.

4.12.3

Keyword Units Strings

One deficiency of the current FITS Standard is that it does not define a sp ecific convention for recording the physical units of a keyword value. The TUNITn keyword can b e used to sp ecify the physical units of the values in a table column, but there is no analogous convention for keyword values. The comment field of the keyword is often used for this purp ose, but the units are usually not sp ecified in a well defined format that FITS readers can easily recognize and extract. To solve this deficiency, FITSIO uses a local convention in which the keyword units are enclosed in square brackets as the first token in the keyword comment field; more sp ecifically, the op ening square bracket immediately follows the slash '/' comment field delimiter and a single space character. The following examples illustrate keywords that use this convention: E V L F X _ A L P H M U OSURE= 1800.0 ELIO = 16.23 BDA = 5400. X = 4.9033487787637465E/ / / 30 [ [ [ / s] elapsed exposure time km s**(-1)] heliocentric velocity angstrom] central wavelength [J/cm**2/s] average flux

In general, the units named in the IAU(1988) Style Guide are recommended, with the main exception that the preferred unit for angle is 'deg' for degrees. The FTPUNT and FTGUNT subroutines in FITSIO write and read, resp ectively, the keyword unit strings in an existing keyword.

4.12.4

HIERARCH Convention for Extended Keyword Names

CFITSIO supp orts the HIERARCH keyword convention which allows keyword names that are longer then 8 characters and may contain the full range of printable ASCI I text characters. This convention was develop ed at the Europ ean Southern Observatory (ESO) to supp ort hierarchical FITS keyword such as: HIERARCH ESO INS FOCU POS = -0.00002500 / Focus position Basically, this convention uses the FITS keyword 'HIERARCH' to indicate that this convention is b eing used, then the actual keyword name ('ESO INS FOCU POS' in this example) b egins in column 10 and can contain any printable ASCI I text characters, including spaces. The


24

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

equals sign marks the end of the keyword name and is followed by the usual value and comment fields just as in standard FITS keywords. Further details of this convention are describ ed at http://arcdev.hq.eso.org/dicb/dicd/dic-1-1.4.html (search for HIERARCH). This convention allows a much broader range of keyword names than is allowed by the FITS Standard. Here are more examples of such keywords: HIERARCH LongKeyword = 47.5 / Keyword has > 8 characters, and mixed case HIERARCH XTE$TEMP = 98.6 / Keyword contains the '$' character HIERARCH Earth is a star = F / Keyword contains embedded spaces CFITSIO will transparently read and write these keywords, so application programs do not in general need to know anything ab out the sp ecific implementation details of the HIERARCH convention. In particular, application programs do not need to sp ecify the `HIERARCH' part of the keyword name when reading or writing keywords (although it may b e included if desired). When writing a keyword, CFITSIO first checks to see if the keyword name is legal as a standard FITS keyword (no more than 8 characters long and containing only letters, digits, or a minus sign or underscore). If so it writes it as a standard FITS keyword, otherwise it uses the hierarch convention to write the keyword. The maximum keyword name length is 67 characters, which leaves only 1 space for the value field. A more practical limit is ab out 40 characters, which leaves enough room for most keyword values. CFITSIO returns an error if there is not enough room for b oth the keyword name and the keyword value on the 80-character card, except for string-valued keywords which are simply truncated so that the closing quote character falls in column 80. In the current implementation, CFITSIO preserves the case of the letters when writing the keyword name, but it is case-insensitive when reading or searching for a keyword. The current implementation allows any ASCI I text character (ASCI I 32 to ASCI I 126) in the keyword name except for the '=' character. A space is also required on either side of the equal sign.

4.13

Optimizing Co de for Maximum Pro cessing Sp eed

CFITSIO has b een carefully designed to obtain the highest p ossible sp eed when reading and writing FITS files. In order to achieve the b est p erformance, however, application programmers must b e careful to call the CFITSIO routines appropriately and in an efficient sequence; inappropriate usage of CFITSIO routines can greatly slow down the execution sp eed of a program. The maximum p ossible I/O sp eed of CFITSIO dep ends of course on the typ e of computer system that it is running on. As a rough guide, the current generation of workstations can achieve sp eeds of 2 ­ 10 MB/s when reading or writing FITS images and similar, or slightly slower sp eeds with FITS binary tables. Reading of FITS files can occur at even higher rates (30MB/s or more) if the FITS file is still cached in system memory following a previous read or write op eration on the same file. To more accurately predict the b est p erformance that is p ossible on any particular system, a diagnostic program called "sp eed.c" is included with the CFITSIO distribution which can b e run to approximately measure the maximum p ossible sp eed of writing and reading a test FITS file. The following 2 sections provide some background on how CFITSIO internally manages the data I/O and describ es some strategies that may b e used to optimize the processing sp eed of software that uses CFITSIO.


4.13. OPTIMIZING CODE FOR MAXIMUM PROCESSING SPEED

25

4.13.1

Background Information: How CFITSIO Manages Data I/O

Many CFITSIO op erations involve transferring only a small numb er of bytes to or from the FITS file (e.g, reading a keyword, or writing a row in a table); it would b e very inefficient to physically read or write such small blocks of data directly in the FITS file on disk, therefore CFITSIO maintains a set of internal Input­Output (IO) buffers in RAM memory that each contain one FITS block (2880 bytes) of data. Whenever CFITSIO needs to access data in the FITS file, it first transfers the FITS block containing those bytes into one of the IO buffers in memory. The next time CFITSIO needs to access bytes in the same block it can then go to the fast IO buffer rather than using a much slower system disk access routine. The numb er of available IO buffers is determined by the NIOBUF parameter (in fitsio2.h) and is currently set to 40. Whenever CFITSIO reads or writes data it first checks to see if that block of the FITS file is already loaded into one of the IO buffers. If not, and if there is an empty IO buffer available, then it will load that block into the IO buffer (when reading a FITS file) or will initialize a new block (when writing to a FITS file). If all the IO buffers are already full, it must decide which one to reuse (generally the one that has b een accessed least recently), and flush the contents back to disk if it has b een modified b efore loading the new block. The one ma jor exception to the ab ove process occurs whenever a large contiguous set of bytes are accessed, as might occur when reading or writing a FITS image. In this case CFITSIO bypasses the internal IO buffers and simply reads or writes the desired bytes directly in the disk file with a single call to a low-level file read or write routine. The minimum threshold for the numb er of bytes to read or write this way is set by the MINDIRECT parameter and is currently set to 3 FITS blocks = 8640 bytes. This is the most efficient way to read or write large chunks of data and can achieve IO transfer rates of 5 ­ 10MB/s or greater. Note that this fast direct IO process is not applicable when accessing columns of data in a FITS table b ecause the bytes are generally not contiguous since they are interleaved by the other columns of data in the table. This explains why the sp eed for accessing FITS tables is generally slower than accessing FITS images. Given this background information, the general strategy for efficiently accessing FITS files should now b e apparent: when dealing with FITS images, read or write large chunks of data at a time so that the direct IO mechanism will b e invoked; when accessing FITS headers or FITS tables, on the other hand, once a particular FITS block has b een loading into one of the IO buffers, try to access all the needed information in that block b efore it gets flushed out of the IO buffer. It is imp ortant to avoid the situation where the same FITS block is b eing read then flushed from a IO buffer multiple times. The following section gives more sp ecific suggestions for optimizing the use of CFITSIO. 1. When dealing with a FITS primary array or IMAGE extension, it is more efficient to read or write large chunks of the image at a time (at least 3 FITS blocks = 8640 bytes) so that the direct IO mechanism will b e used as describ ed in the previous section. Smaller chunks of data are read or written via the IO buffers, which is somewhat less efficient b ecause of the extra copy op eration and additional b ookkeeping steps that are required. In principle it is more efficient to read or write as big an array of image pixels at one time as p ossible, however, if the array b ecomes so large that the op erating system cannot store it all in RAM, then the p erformance may b e degraded b ecause of the increased swapping of virtual memory to disk. 2. When dealing with FITS tables, the most imp ortant efficiency factor in the software design is


26

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES

to read or write the data in the FITS file in a single pass through the file. An example of p oor program design would b e to read a large, 3-column table by sequentially reading the entire first column, then going back to read the 2nd column, and finally the 3rd column; this obviously requires 3 passes through the file which could triple the execution time of an I/O limited program. For small tables this is not imp ortant, but when reading multi-megabyte sized tables these inefficiencies can b ecome significant. The more efficient procedure in this case is to read or write only as many rows of the table as will fit into the available internal I/O buffers, then access all the necessary columns of data within that range of rows. Then after the program is completely finished with the data in those rows it can move on to the next range of rows that will fit in the buffers, continuing in this way until the entire file has b een processed. By using this procedure of accessing all the columns of a table in parallel rather than sequentially, each block of the FITS file will only b e read or written once. The optimal numb er of rows to read or write at one time in a given table dep ends on the width of the table row, on the numb er of I/O buffers that have b een allocated in FITSIO, and also on the numb er of other FITS files that are op en at the same time (since one I/O buffer is always reserved for each op en FITS file). Fortunately, a FITSIO routine is available that will return the optimal numb er of rows for a given table: call ftgrsz(unit, nrows, status). It is not critical to use exactly the value of nrows returned by this routine, as long as one does not exceed it. Using a very small value however can also lead to p oor p erformance b ecause of the overhead from the larger numb er of subroutine calls. The optimal numb er of rows returned by ftgrsz is valid only as long as the application program is only reading or writing data in the sp ecified table. Any other calls to access data in the table header would cause additional blocks of data to b e loaded into the I/O buffers displacing data from the original table, and should b e avoided during the critical p eriod while the table is b eing read or written. 3. Use binary table extensions rather than ASCI I table extensions for b etter efficiency when dealing with tabular data. The I/O to ASCI I tables is slower b ecause of the overhead in formatting or parsing the ASCI I data fields, and b ecause ASCI I tables are ab out twice as large as binary tables with the same information content. 4. Design software so that it reads the FITS header keywords in the same order in which they occur in the file. When reading keywords, FITSIO searches forward starting from the p osition of the last keyword that was read. If it reaches the end of the header without finding the keyword, it then goes back to the start of the header and continues the search down to the p osition where it started. In practice, as long as the entire FITS header can fit at one time in the available internal I/O buffers, then the header keyword access will b e very fast and it makes little difference which order they are accessed. 5. Avoid the use of scaling (by using the BSCALE and BZERO or TSCAL and TZERO keywords) in FITS files since the scaling op erations add to the processing time needed to read or write the data. In some cases it may b e more efficient to temp orarily turn off the scaling (using ftpscl or fttscl) and then read or write the raw unscaled values in the FITS file. 6. Avoid using the 'implicit datatyp e conversion' capability in FITSIO. For instance, when reading a FITS image with BITPIX = -32 (32-bit floating p oint pixels), read the data into a single precision floating p oint data array in the program. Forcing FITSIO to convert the data to a different datatyp e can significantly slow the program.


4.13. OPTIMIZING CODE FOR MAXIMUM PROCESSING SPEED

27

7. Where feasible, design FITS binary tables using vector column elements so that the data are written as a contiguous set of bytes, rather than as single elements in multiple rows. For example, it is faster to access the data in a table that contains a single row and 2 columns with TFORM keywords equal to '10000E' and '10000J', than it is to access the same amount of data in a table with 10000 rows which has columns with the TFORM keywords equal to '1E' and '1J'. In the former case the 10000 floating p oint values in the first column are all written in a contiguous block of the file which can b e read or written quickly, whereas in the second case each floating p oint value in the first column is interleaved with the integer value in the second column of the same row so CFITSIO has to explicitly move to the p osition of each element to b e read or written. 8. Avoid the use of variable length vector columns in binary tables, since any reading or writing of these data requires that CFITSIO first look up or compute the starting address of each row of data in the heap. In practice, this is probably not a significant efficiency issue. 9. When copying data from one FITS table to another, it is faster to transfer the raw bytes instead of reading then writing each column of the table. The FITSIO subroutines FTGTBS and FTPTBS (for ASCI I tables), and FTGTBB and FTPTBB (for binary tables) will p erform low-level reads or writes of any contiguous range of bytes in a table extension. These routines can b e used to read or write a whole row (or multiple rows) of a table with a single subroutine call. These routines are fast b ecause they bypass all the usual data scaling, error checking and machine dep endent data conversion that is normally done by FITSIO, and they allow the program to write the data to the output file in exactly the same byte order. For these same reasons, use of these routines can b e somewhat risky b ecause no validation or machine dep endent conversion is p erformed by these routines. In general these routines are only recommended for optimizing critical pieces of code and should only b e used by programmers who thoroughly understand the internal byte structure of the FITS tables they are reading or writing. 10. Another strategy for improving the sp eed of writing a FITS table, similar to the previous one, is to directly construct the entire byte stream for a whole table row (or multiple rows) within the application program and then write it to the FITS file with ftptbb. This avoids all the overhead normally present in the column-oriented CFITSIO write routines. This technique should only b e used for critical applications, b ecause it makes the code more difficult to understand and maintain, and it makes the code more system dep endent (e.g., do the bytes need to b e swapp ed b efore writing to the FITS file?). 11. Finally, external factors such as the typ e of magnetic disk controller (SCSI or IDE), the size of the disk cache, the average seek sp eed of the disk, the amount of disk fragmentation, and the amount of RAM available on the system can all have a significant impact on overall I/O efficiency. For critical applications, a system administrator should review the prop osed system hardware to identify any p otential I/O b ottlenecks.


28

CHAPTER 4. FITSIO CONVENTIONS AND GUIDELINES


Chapter 5

Basic Interface Routines
This section defines a basic set of subroutines that can b e used to p erform the most common typ es of read and write op erations on FITS files. New users should start with these subroutines and then, as needed, explore the more advance routines describ ed in the following chapter to p erform more complex or sp ecialized op erations. A th th to right arrow symb ol (>) is used to separate the input parameters from the output parameters in e definition of each routine. This symb ol is not actually part of the calling sequence. Note that e status parameter is b oth an input and an output parameter and must b e initialized = 0 prior calling the FITSIO subroutines.

Refer to Chapter 9 for the definition of all the parameters used by these interface routines.

5.1

FITSIO Error Status Routines

1 Return the current version numb er of the fitsio library. The version numb er will b e incremented with each new release of CFITSIO. FTVERS( > version) 2 Return the descriptive text string corresp onding to a FITSIO error status code. The 30character length string contains a brief description of the cause of the error. FTGERR(status, > errtext) 3 Return the top (oldest) 80-character error message from the internal FITSIO stack of error messages and shift any remaining messages on the stack up one level. Any FITSIO error will generate one or more messages on the stack. Call this routine rep eatedly to get each message in sequence. The error stack is empty when a blank string is returned. FTGMSG( > errmsg) 29


30

CHAPTER 5.

BASIC INTERFACE ROUTINES

4 The FTPMRK routine puts an invisible marker on the CFITSIO error stack. The FTCMRK routine can then b e used to delete any more recent error messages on the stack, back to the p osition of the marker. This preserves any older error messages on the stack. FTCMSG simply clears the entire error message stack. These routines are called without any arguments. FTPMRK FTCMRK FTCMSG 5 Print out the error message corresp onding to the input status value and all the error messages on the FITSIO stack to the sp ecified file stream (stream can b e either the string 'STDOUT' or 'STDERR'). If the input status value = 0 then this routine does nothing. FTRPRT (stream, > status) 6 Write an 80-character message to the FITSIO error stack. Application programs should not normally write to the stack, but there may b e some situations where this is desirable. FTPMSG(errmsg)

5.2

File I/O Routines

1 Op en an existing FITS file with readonly or readwrite access. This routine always op ens the primary array (the first HDU) of the file, and does not move to a following extension, if one was sp ecified as part of the filename. Use the FTNOPN routine to automatically move to the extension. This routine will also op en IRAF images (.imh format files) and raw binary data arrays with READONLY access by first converting them on the fly into virtual FITS images. See the `Extended File Name Syntax' chapter for more details. The FTDKOPEN routine simply op ens the sp ecified file without trying to interpret the filename using the extended filename syntax. FTOPEN(unit,filename,rwmode, > blocksize,status) FTDKOPEN(unit,filename,rwmode, > blocksize,status) 2 Op en an existing FITS file with readonly or readwrite access and move to a following extension, if one was sp ecified as part of the filename. (e.g., 'filename.fits+2' or 'filename.fits[2]' will move to the 3rd HDU in the file). Note that this routine differs from FTOPEN in that it does not have the redundant blocksize argument. FTNOPN(unit,filename,rwmode, > status) 3 Op en an existing FITS file with readonly or readwrite access and then move to the first HDU containing significant data, if a) an HDU name or numb er to op en was not explicitly sp ecified


5.2. FILE I/O ROUTINES

31

as part of the filename, and b) if the FITS file contains a null primary array (i.e., NAXIS = 0). In this case, it will look for the first IMAGE HDU with NAXIS ¿ 0, or the first table that does not contain the strings `GTI' (Good Time Interval) or `OBSTABLE' in the EXTNAME keyword value. FTTOPN is similar, except it will move to the first significant table HDU (skipping over any image HDUs) in the file if a sp ecific HDU name or numb er is not sp ecified. FTIOPN will move to the first non-null image HDU, skipping over any tables. FTDOPN(unit,filename,rwmode, > status) FTTOPN(unit,filename,rwmode, > status) FTIOPN(unit,filename,rwmode, > status) 4 Op en and initialize a new empty FITS file. A template file may also b e sp ecified to define the structure of the new file (see section 4.2.4). The FTDKINIT routine simply creates the sp ecified file without trying to interpret the filename using the extended filename syntax. FTINIT(unit,filename,blocksize, > status) FTDKINIT(unit,filename,blocksize, > status) 5 Close a FITS file previously op ened with ftop en or ftinit FTCLOS(unit, > status) 6 Move to a sp ecified (absolute) HDU in the FITS file (nhdu = 1 for the FITS primary array) FTMAHD(unit,nhdu, > hdutype,status) 7 Create a primary array (if none already exists), or insert a new IMAGE extension immediately following the CHDU, or insert a new Primary Array at the b eginning of the file. Any following extensions in the file will b e shifted down to make room for the new extension. If the CHDU is the last HDU in the file then the new image extension will simply b e app ended to the end of the file. One can force a new primary array to b e inserted at the b eginning of the FITS file by setting status = -9 prior to calling the routine. In this case the existing primary array will b e converted to an IMAGE extension. The new extension (or primary array) will b ecome the CHDU. The FTI IMGLL routine is identical to the FTI IMG routine except that the 4th parameter (the length of each axis) is an array of 64-bit integers rather than an array of 32-bit integers. FTIIMG(unit,bitpix,naxis,naxes, > status) FTIIMGLL(unit,bitpix,naxis,naxesll, > status) 8 Insert a new ASCI I TABLE extension immediately following the CHDU. Any following extensions will b e shifted down to make room for the new extension. If there are no other following extensions then the new table extension will simply b e app ended to the end of the file. The new extension will b ecome the CHDU. The FTITABLL routine is identical to the FTITAB routine except that the 2nd and 3rd parameters (that give the size of the table) are 64-bit integers rather than 32-bit integers.


32

CHAPTER 5.

BASIC INTERFACE ROUTINES

FTITAB(unit,rowlen,nrows,tfields,ttype,tbcol,tform,tunit,extname, > status) FTITABLL(unit,rowlenll,nrowsll,tfields,ttype,tbcol,tform,tunit,extname, > status) 9 Insert a new binary table extension immediately following the CHDU. Any following extensions will b e shifted down to make room for the new extension. If there are no other following extensions then the new bintable extension will simply b e app ended to the end of the file. The new extension will b ecome the CHDU. The FTIBINLL routine is identical to the FTIBIN routine except that the 2nd parameter (that gives the length of the table) is a 64-bit integer rather than a 32-bit integer. FTIBIN(unit,nrows,tfields,ttype,tform,tunit,extname,varidat > status) FTIBINLL(unit,nrowsll,tfields,ttype,tform,tunit,extname,varidat > status)

5.3

Keyword I/O Routines

1 Put (app end) an 80-character record into the CHU. FTPREC(unit,card, > status) 2 Put (app end) a new keyword of the appropriate datatyp e into the CHU. The E and D versions of this routine have the added feature that if the 'decimals' parameter is negative, then the 'G' display format rather then the 'E' format will b e used when constructing the keyword value, taking the absolute value of 'decimals' for the precision. This will suppress trailing zeros, and will use a fixed format rather than an exp onential format, dep ending on the magnitude of the value. FTPKY[JKLS](unit,keyword,keyval,comment, > status) FTPKY[EDFG](unit,keyword,keyval,decimals,comment, > status) 3 Get the nth key no = b eginning header; it 80-character header record from the CHU. The first keyword in the header is at 1; if key no = 0 then this subroutine simple moves the internal p ointer to the of the header so that subsequent keyword op erations will start at the top of the also returns a blank card value in this case.

FTGREC(unit,key_no, > card,status) 4 Get a keyword value (with the appropriate datatyp e) and comment from the CHU FTGKY[EDJKLS](unit,keyword, > keyval,comment,status) 5 Delete an existing keyword record. FTDKEY(unit,keyword, > status)


5.4. DATA I/O ROUTINES

33

5.4

Data I/O Routines

The following routines read or write data values in the current HDU of the FITS file. Automatic datatyp e conversion will b e attempted for numerical datatyp es if the sp ecified datatyp e is different from the actual datatyp e of the FITS array or table column. 1 Write elements into the primary data array or image extension. FTPPR[BIJKED](unit,group,fpixel,nelements,values, > status) 2 Read elements from the primary data array or image extension. Undefined array elements will b e returned with a value = nullval, unless nullval = 0 in which case no checks for undefined pixels will b e p erformed. The anyf parameter is set to true (= .true.) if any of the returned elements were undefined. FTGPV[BIJKED](unit,group,fpixel,nelements,nullval, > values,anyf,status) 3 Write elements into an ASCI I or binary table column. The `felem' parameter applies only to vector columns in binary tables and is ignored when writing to ASCI I tables. FTPCL[SLBIJKEDCM](unit,colnum,frow,felem,nelements,values, > status) 4 Read elements from an ASCI I or binary table column. Undefined array elements will b e returned with a value = nullval, unless nullval = 0 (or = ' ' for ftgcvs) in which case no checking for undefined values will b e p erformed. The ANYF parameter is set to true if any of the returned elements are undefined. Any column, regardless of it's intrinsic datatyp e, may b e read as a string. It should b e noted however that reading a numeric column as a string is 10 - 100 times slower than reading the same column as a numb er due to the large overhead in constructing the formatted strings. The display format of the returned strings will b e determined by the TDISPn keyword, if it exists, otherwise by the datatyp e of the column. The length of the returned strings can b e determined with the ftgcdw routine. The following TDISPn display formats are currently supp orted: I O Z F E D G w w w w w w w . . . . . . . m m m d d d d I O H F E E G n c e i x x e t t x x p p n e a a e o o e g l d d n n r er integer ecimal integer floating point ential floating point ential floating point al; uses Fw.d if significance not lost, else Ew.d

where w is the width in characters of the displayed values, m is the minimum numb er of digits displayed, and d is the numb er of digits to the right of the decimal. The .m field is optional.


34

CHAPTER 5.

BASIC INTERFACE ROUTINES

FTGCV[SBIJKEDCM](unit,colnum,frow,felem,nelements,nullval, > values,anyf,status) 5 Get the table column numb er and full name of the column whose name matches the input template string. See the `Advanced Interface Routines' chapter for a full description of this routine. FTGCNN(unit,casesen,coltemplate, > colname,colnum,status)


Chapter 6

Advanced Interface Subroutines
This chapter defines all the available subroutines in the FITSIO user interface. For completeness, the basic subroutines describ ed in the previous chapter are also rep eated here. A right arrow symb ol is used here to separate the input parameters from the output parameters in the definition of each subroutine. This symb ol is not actually part of the calling sequence. An alphab etical list and definition of all the parameters is given at the end of this section.

6.1

FITS File Op en and Close Subroutines:

1 Op en an existing FITS file with readonly or readwrite access. The FTDKOPEN routine simply op ens the sp ecified file without trying to interpret the filename using the extended filename syntax. FTDOPN op ens the file and also moves to the first HDU containing significant data, if no sp ecific HDU is sp ecified as part of the filename. FTTOPN and FTIOPN are similar except that they will move to the first table HDU or image HDU, resp ectively, if a HDU name or numb er is not sp ecified as part of the filename. FTOPEN(unit,filename,rwmode, > blocksize,status) FTDKOPEN(unit,filename,rwmode, > blocksize,status) FTDOPN(unit,filename,rwmode, > status) FTTOPN(unit,filename,rwmode, > status) FTIOPN(unit,filename,rwmode, > status) 2 Op en an existing FITS file with readonly or readwrite access and move to a following extension, if one was sp ecified as part of the filename. (e.g., 'filename.fits+2' or 'filename.fits[2]' will move to the 3rd HDU in the file). Note that this routine differs from FTOPEN in that it does not have the redundant blocksize argument. FTNOPN(unit,filename,rwmode, > status) 3 Reop en a FITS file that was previously op ened with FTOPEN, FTNOPN, or FTINIT. The newunit numb er may then b e treated as a separate file, and one may simultaneously read 35


36

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

or write to 2 (or more) different extensions in the same file. The FTOPEN and FTNOPN routines (ab ove) automatically detects cases where a previously op ened file is b eing op ened again, and then internally call FTREOPEN, so programs should rarely need to explicitly call this routine. FTREOPEN(unit, > newunit, status) 4 Op en and initialize a new empty FITS file. The FTDKINIT routine simply creates the sp ecified file without trying to interpret the filename using the extended filename syntax. FTINIT(unit,filename,blocksize, > status) FTDKINIT(unit,filename,blocksize, > status) 5 Create a new FITS file, using a template file to define its initial size and structure. The template may b e another FITS HDU or an ASCI I template file. If the input template file name is blank, then this routine b ehaves the same as FTINIT. The currently supp orted format of the ASCI I template file is describ ed under the fits parse template routine (in the general Utilities section), but this may change slightly later releases of CFITSIO. FTTPLT(unit, filename, tplfilename, > status) 6 Flush internal buffers of data to the output FITS file previously op ened with ftop en or ftinit. The routine usually never needs to b e called, but doing so will ensure that if the program subsequently ab orts, then the FITS file will have at least b een closed prop erly. FTFLUS(unit, > status) 7 Close a FITS file previously op ened with ftop en or ftinit FTCLOS(unit, > status) 8 Close and DELETE a FITS file previously op ened with ftop en or ftinit. This routine may b e useful in cases where a FITS file is created, but an error occurs which prevents the complete file from b eing written. FTDELT(unit, > status) 9 Get the value of an unused I/O unit numb er which may then b e used as input to FTOPEN or FTINIT. This routine searches for the first unused unit numb er in the range from with 99 down to 50. This routine just keeps an internal list of the allocated unit numb ers and does not physically check that the Fortran unit is available (to b e compatible with the SPP version of FITSIO). Thus users must not indep endently allocate any unit numb ers in the range 50 - 99 if this routine is also to b e used in the same program. This routine is provided for convenience only, and it is not required that the unit numb ers used by FITSIO b e allocated by this routine.


6.1. FITS FILE OPEN AND CLOSE SUBROUTINES: FTGIOU( > iounit, status)

37

10 Free (deallocate) an I/O unit numb er which was previously allocated with FTGIOU. All previously allocated unit numb ers may b e deallocated at once by calling FTFIOU with iounit = -1. FTFIOU(iounit, > status) 11 Return the Fortran unit numb er that corresp onds to the C fitsfile p ointer value, or vice versa. These 2 C routines may b e useful in mixed language programs where b oth C and Fortran subroutines need to access the same file. For example, if a FITS file is op ened with unit 12 by a Fortran subroutine, then a C routine within the same program could get the fitfile p ointer value to access the same file by calling 'fptr = CUnit2FITS(12)'. These routines return a value of zero if an error occurs. int CFITS2Unit(fitsfile *ptr); fitsfile* CUnit2FITS(int unit); 11 Parse the input filename and return the HDU numb er that would b e moved to if the file were op ened with FTNOPN. The returned HDU numb er b egins with 1 for the primary array, so for example, if the input filename = `myfile.fits[2]' then hdunum = 3 will b e returned. FITSIO does not op en the file to check if the extension actually exists if an extension numb er is sp ecified. If an extension *name* is included in the file name sp ecification (e.g. `myfile.fits[EVENTS]' then this routine will have to op en the FITS file and look for the p osition of the named extension, then close file again. This is not p ossible if the file is b eing read from the stdin stream, and an error will b e returned in this case. If the filename does not sp ecify an explicit extension (e.g. 'myfile.fits') then hdunum = -99 will b e returned, which is functionally equivalent to hdunum = 1. This routine is mainly used for backward compatibility in the ftools software package and is not recommended for general use. It is generally b etter and more efficient to first op en the FITS file with FTNOPN, then use FTGHDN to determine which HDU in the file has b een op ened, rather than calling FTEXTN followed by a call to FTNOPN. FTEXTN(filename, > nhdu, status) 12 Return the name of the op ened FITS file. FTFLNM(unit, > filename, status) 13 Return the I/O mode of the op en FITS file (READONLY = 0, READWRITE = 1). FTFLMD(unit, > iomode, status) 14 Return the file typ e of the op ened FITS file (e.g. 'file://', 'ftp://', etc.).


38

CHAPTER 6. FTURLT(unit, > urltype, status)

ADVANCED INTERFACE SUBROUTINES

15 Parse the input filename or URL into its comp onent parts: the file typ e (file://, ftp://, http://, etc), the base input file name, the name of the output file that the input file is to b e copied to prior to op ening, the HDU or extension sp ecification, the filtering sp ecifier, the binning sp ecifier, and the column sp ecifier. Blank strings will b e returned for any comp onents that are not present in the input file name. FTIURL(filename, > filetype, infile, outfile, extspec, filter, binspec, colspec, status) 16 Parse the input file name and return the root file name. The root name includes the file typ e if sp ecified, (e.g. 'ftp://' or 'http://') and the full path name, to the extent that it is sp ecified in the input filename. It does not include the HDU name or numb er, or any filtering sp ecifications. FTRTNM(filename, > rootname, status) 16 Test if the input file or a compressed version of the file (with a .gz, .Z, .z, or .zip extension) exists on disk. The returned value of the 'exists' parameter will have 1 of the 4 following values: 2: 1: 0: -1: t t n t s h h e h m e e i e e file d disk f ther th input m, or m o i e f e es not exi le does ex file nor ile name i m file, or st, ist a co s no af but a compressed version does exist mpressed version of the file exist t a disk file (could be a ftp, http, ile piped in on the STDIN stream)

FTEXIST(filename, > exists, status);

6.2

HDU-Level Op erations
file is first op ened or created, the internal buffers in FITSIO automatically p oint to in the file. The following routines may b e used to move to another HDU in the file. HDU numb ering convention used in FITSIO denotes the primary array as the first extension in a FITS file is the second HDU, and so on.

When a FITS the first HDU Note that the HDU, the first

1 Move to a sp ecified (absolute) HDU in the FITS file (nhdu = 1 for the FITS primary array) FTMAHD(unit,nhdu, > hdutype,status) 2 Move to a new (existing) HDU forward or backwards relative to the CHDU


6.2. HDU-LEVEL OPERATIONS FTMRHD(unit,nmove, > hdutype,status)

39

3 Move to the (first) HDU which has the sp ecified extension typ e and EXTNAME (or HDUNAME) and EXTVER keyword values. The hdutyp e parameter may have a value of IMAGE HDU (0), ASCI I TBL (1), BINARY TBL (2), or ANY HDU (-1) where ANY HDU means that only the extname and extver values will b e used to locate the correct extension. If the input value of extver is 0 then the EXTVER keyword is ignored and the first HDU with a matching EXTNAME (or HDUNAME) keyword will b e found. If no matching HDU is found in the file then the current HDU will remain unchanged and a status = BAD HDU NUM (301) will b e returned. FTMNHD(unit, hdutype, extname, extver, > status) 4 Get the numb er of the current HDU in the FITS file (primary array = 1) FTGHDN(unit, > nhdu) 5 Return the typ e of the current HDU in the FITS file. The p ossible values for hdutyp e are IMAGE HDU (0), ASCI I TBL (1), or BINARY TBL (2). FTGHDT(unit, > hdutype, status) 6 Return the total numb er of HDUs in the FITS file. The CHDU remains unchanged. FTTHDU(unit, > hdunum, status) 7 Create (app end) a new empty HDU following the last extension that has b een previously accessed by the program. This will overwrite any extensions in an existing FITS file if the program has not already moved to that (or a later) extension using the FTMAHD or FTMRHD routines. For example, if an existing FITS file contains a primary array and 5 extensions and a program (1) op ens the FITS file, (2) moves to extension 4, (3) moves back to the primary array, and (4) then calls FTCRHD, then the new extension will b e written following the 4th extension, overwriting the existing 5th extension. FTCRHD(unit, > status) 8 Create a primary array (if none already exists), or insert a new IMAGE extension immediately following the CHDU, or insert a new Primary Array at the b eginning of the file. Any following extensions in the file will b e shifted down to make room for the new extension. If the CHDU is the last HDU in the file then the new image extension will simply b e app ended to the end of the file. One can force a new primary array to b e inserted at the b eginning of the FITS file by setting status = -9 prior to calling the routine. In this case the existing primary array will b e converted to an IMAGE extension. The new extension (or primary array) will b ecome the CHDU. The FTI IMGLL routine is identical to the FTI IMG routine except that the 4th parameter (the length of each axis) is an array of 64-bit integers rather than an array of 32-bit integers.


40

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

FTIIMG(unit,bitpix,naxis,naxes, > status) FTIIMGLL(unit,bitpix,naxis,naxesll, > status) 9 Insert a new ASCI I TABLE extension immediately following the CHDU. Any following extensions will b e shifted down to make room for the new extension. If there are no other following extensions then the new table extension will simply b e app ended to the end of the file. The new extension will b ecome the CHDU. The FTITABLL routine is identical to the FTITAB routine except that the 2nd and 3rd parameters (that give the size of the table) are 64-bit integers rather than 32-bit integers. FTITAB(unit,rowlen,nrows,tfields,ttype,tbcol,tform,tunit,extname, > status) FTITABLL(unit,rowlenll,nrowsll,tfields,ttype,tbcol,tform,tunit,extname, > status) 10 Insert a new binary table extension immediately following the CHDU. Any following extensions will b e shifted down to make room for the new extension. If there are no other following extensions then the new bintable extension will simply b e app ended to the end of the file. The new extension will b ecome the CHDU. The FTIBINLL routine is identical to the FTIBIN routine except that the 2nd parameter (that gives the length of the table) is a 64-bit integer rather than a 32-bit integer. FTIBIN(unit,nrows,tfields,ttype,tform,tunit,extname,varidat > status) FTIBINLL(unit,nrowsll,tfields,ttype,tform,tunit,extname,varidat > status)

11 Resize an image by modifing the size, dimensions, and/or datatyp e of the current primary array or image extension. If the new image, as sp ecified by the input arguments, is larger than the current existing image in the FITS file then zero fill data will b e inserted at the end of the current image and any following extensions will b e moved further back in the file. Similarly, if the new image is smaller than the current image then any following extensions will b e shifted up towards the b eginning of the FITS file and the image data will b e truncated to the new size. This routine rewrites the BITPIX, NAXIS, and NAXISn keywords with the appropriate values for new image. The FTRSIMLL routine is identical to the FTRSIM routine except that the 4th parameter (the length of each axis) is an array of 64-bit integers rather than an array of 32-bit integers. FTRSIM(unit,bitpix,naxis,naxes,status) FTRSIMLL(unit,bitpix,naxis,naxesll,status) 12 Delete the CHDU in the FITS file. Any following HDUs will b e shifted forward in the file, to fill in the gap created by the deleted HDU. In the case of deleting the primary array (the first HDU in the file) then the current primary array will b e replace by a null primary array containing the minimum set of required keywords and no data. If there are more extensions in the file following the one that is deleted, then the the CHDU will b e redefined to p oint to


6.3. DEFINE OR REDEFINE THE STRUCTURE OF THE CHDU

41

the following extension. If there are no following extensions then the CHDU will b e redefined to p oint to the previous HDU. The output HDUTYPE parameter indicates the typ e of the new CHDU after the previous CHDU has b een deleted. FTDHDU(unit, > hdutype,status) 13 Copy all or part of the input FITS file and app end it to the end of the output FITS file. If 'previous' (an integer parameter) is not equal to 0, then any HDUs preceding the current HDU in the input file will b e copied to the output file. Similarly, 'current' and 'following' determine whether the current HDU, and/or any following HDUs in the input file will b e copied to the output file. If all 3 parameters are not equal to zero, then the entire input file will b e copied. On return, the current HDU in the input file will b e unchanged, and the last copied HDU will b e the current HDU in the output file. FTCPFL(iunit, ounit, previous, current, following, > status) 14 Copy the entire CHDU from the FITS file associated with IUNIT to the CHDU of the FITS file associated with OUNIT. The output HDU must b e empty and not already contain any keywords. Space will b e reserved for MOREKEYS additional keywords in the output header if there is not already enough space. FTCOPY(iunit,ounit,morekeys, > status) 15 Copy the header (and not the data) from the CHDU associated with inunit to the CHDU associated with outunit. If the current output HDU is not completely empty, then the CHDU will b e closed and a new HDU will b e app ended to the output file. This routine will automatically transform the necessary keywords when copying a primary array to and image extension, or an image extension to a primary array. An empty output data unit will b e created (all values = 0). FTCPHD(inunit, outunit, > status) 16 Copy just the data from the CHDU associated with IUNIT to the CHDU associated with OUNIT. This will overwrite any data previously in the OUNIT CHDU. This low level routine is used by FTCOPY, but it may also b e useful in certain application programs which want to copy the data from one FITS file to another but also want to modify the header keywords in the process. all the required header keywords must b e written to the OUNIT CHDU b efore calling this routine FTCPDT(iunit,ounit, > status)

6.3

Define or Redefine the structure of the CHDU

It should rarely b e necessary to call the subroutines in this section. FITSIO internally calls these routines whenever necessary, so any calls to these routines by application programs will likely b e redundant.


42

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

1 This routine forces FITSIO to scan the current header keywords that define the structure of the HDU (such as the NAXISn, PCOUNT and GCOUNT keywords) so that it can initialize the internal buffers that describ e the HDU structure. This routine may b e used instead of the more complicated calls to ftp def, ftadef or ftb def. This routine is also very useful for reinitializing the structure of an HDU, if the numb er of rows in a table, as sp ecified by the NAXIS2 keyword, has b een modified from its initial value. FTRDEF(unit, > status) (DEPRECATED) When writing GROUPed FITS 0, ftp def must b e called with tatus=308 when trying to write rather than this routine. (DEPRECATED)

2 Define the structure of the primary array or IMAGE extension. files that by convention set the NAXIS1 keyword equal to naxes(1) = 1, NOT 0, otherwise FITSIO will rep ort an error s data to a group. Note: it is usually simpler to call FTRDEF

FTPDEF(unit,bitpix,naxis,naxes,pcount,gcount, > status)

3 Define the structure of an ASCI I table (TABLE) extension. Note: it is usually simpler to call FTRDEF rather than this routine. FTADEF(unit,rowlen,tfields,tbcol,tform,nrows > status) (DEPRECATED) 4 Define the structure of a binary table (BINTABLE) extension. Note: it is usually simpler to call FTRDEF rather than this routine. FTBDEF(unit,tfields,tform,varidat,nrows > status) (DEPRECATED) 5 Define the size of the Current Data Unit, overriding the length of the data unit as previously defined by ftp def, ftadef, or ftb def. This is useful if one does not know the total size of the data unit until after the data have b een written. The size (in bytes) of an ASCI I or Binary table is given by NAXIS1 * NAXIS2. (Note that to determine the value of NAXIS1 it is often more convenient to read the value of the NAXIS1 keyword from the output file, rather than computing the row length directly from all the TFORM keyword values). Note: it is usually simpler to call FTRDEF rather than this routine. FTDDEF(unit,bytlen, > status) (DEPRECATED) 6 Define the zero indexed byte offset of the 'heap' measured from the start of the binary table data. By default the heap is assumed to start immediately following the regular table data, i.e., at location NAXIS1 x NAXIS2. This routine is only relevant for binary tables which contain variable length array columns (with TFORMn = 'Pt'). This subroutine also automatically writes the value of theap to a keyword in the extension header. This subroutine must b e called after the required keywords have b een written (with ftphbn) and after the table structure has b een defined (with ftb def ) but b efore any data is written to the table. FTPTHP(unit,theap, > status)


6.4. FITS HEADER I/O SUBROUTINES

43

6.4
6.4.1

FITS Header I/O Subroutines
Header Space and Position Routines

1 Reserve space in the CHU for MOREKEYS more header keywords. This subroutine may b e called to reserve space for keywords which are to b e written at a later time, after the data unit or subsequent extensions have b een written to the FITS file. If this subroutine is not explicitly called, then the initial size of the FITS header will b e limited to the space available at the time that the first data is written to the associated data unit. FITSIO has the ability to dynamically add more space to the header if needed, however it is more efficient to preallocate the required space if the size is known in advance. FTHDEF(unit,morekeys, > status) 2 Return the numb er of existing keywords in the CHU (NOT including the END keyword which is not considered a real keyword) and the remaining space available to write additional keywords in the CHU. (returns KEYSADD = -1 if the header has not yet b een closed). Note that FITSIO will attempt to dynamically add space for more keywords if required when app ending new keywords to a header. FTGHSP(iunit, > keysexist,keysadd,status) 3 Return the numb er of keywords in the header and the current p osition in the header. This returns the numb er of the keyword record that will b e read next (or one greater than the p osition of the last keyword that was read or written). A value of 1 is returned if the p ointer is p ositioned at the b eginning of the header. FTGHPS(iunit, > keysexist,key_no,status)

6.4.2

Read or Write Standard Header Routines

These subroutines provide a simple method of reading or writing most of the keyword values that are normally required in a FITS files. These subroutines are provided for convenience only and are not required to b e used. If preferred, users may call the lower-level subroutines describ ed in the previous section to individually read or write the required keywords. Note that in most cases, the required keywords such as NAXIS, TFIELD, TTYPEn, etc, which define the structure of the HDU must b e written to the header b efore any data can b e written to the image or table. 1 Put the primary header or IMAGE extension keywords into the CHU. There are 2 available routines: The simpler FTPHPS routine is equivalent to calling ftphpr with the default values of SIMPLE = true, p count = 0, gcount = 1, and EXTEND = true. PCOUNT, GCOUNT and EXTEND keywords are not required in the primary header and are only written if p count is not equal to zero, gcount is not equal to zero or one, and if extend is TRUE, resp ectively. When writing to an IMAGE extension, the SIMPLE and EXTEND parameters are ignored.


44

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

FTPHPS(unit,bitpix,naxis,naxes, > status) FTPHPR(unit,simple,bitpix,naxis,naxes,pcount,gcount,extend, > status) 2 Get primary header or IMAGE extension keywords from the CHU. When reading from an IMAGE extension the SIMPLE and EXTEND parameters are ignored. FTGHPR(unit,maxdim, > simple,bitpix,naxis,naxes,pcount,gcount,extend, status) 3 Put the ASCI I table header keywords into the CHU. The optional TUNITn and EXTNAME keywords are written only if the input string values are not blank. FTPHTB(unit,rowlen,nrows,tfields,ttype,tbcol,tform,tunit,extname, > status) 4 Get the ASCI I table header keywords from the CHU FTGHTB(unit,maxdim, > rowlen,nrows,tfields,ttype,tbcol,tform,tunit, extname,status) 5 Put the binary table header keywords into the CHU. The optional TUNITn and EXTNAME keywords are written only if the input string values are not blank. The p count parameter, which sp ecifies the size of the variable length array heap, should initially = 0; FITSIO will automatically up date the PCOUNT keyword value if any variable length array data is written to the heap. The TFORM keyword value for variable length vector columns should have the form 'Pt(len)' or '1Pt(len)' where `t' is the data typ e code letter (A,I,J,E,D, etc.) and `len' is an integer sp ecifying the maximum length of the vectors in that column (len must b e greater than or equal to the longest vector in the column). If `len' is not sp ecified when the table is created (e.g., the input TFORMn value is just '1Pt') then FITSIO will scan the column when the table is first closed and will app end the maximum length to the TFORM keyword value. Note that if the table is subsequently modified to increase the maximum length of the vectors then the modifying program is resp onsible for also up dating the TFORM keyword value. FTPHBN(unit,nrows,tfields,ttype,tform,tunit,extname,varidat, > status) 6 Get the binary table header keywords from the CHU FTGHBN(unit,maxdim, > nrows,tfields,ttype,tform,tunit,extname,varidat, status)


6.4. FITS HEADER I/O SUBROUTINES

45

6.4.3

Write Keyword Subroutines

1 Put (app end) an 80-character record into the CHU. FTPREC(unit,card, > status) 2 Put (app end) a COMMENT keyword into the CHU. Multiple COMMENT keywords will b e written if the input comment string is longer than 72 characters. FTPCOM(unit,comment, > status) 3 Put (app end) a HISTORY keyword into the CHU. Multiple HISTORY keywords will b e written if the input history string is longer than 72 characters. FTPHIS(unit,history, > status) 4 Put (app end) the DATE keyword into the CHU. The keyword value will contain the current system date as a character string in 'dd/mm/yy' format. If a DATE keyword already exists in the header, then this subroutine will simply up date the keyword value in-place with the current date. FTPDAT(unit, > status) 5 Put (app end) a new keyword of the appropriate datatyp e into the CHU. Note that FTPKYS will only write string values up to 68 characters in length; longer strings will b e truncated. The FTPKLS routine can b e used to write longer strings, using a non-standard FITS convention. The E and D versions of this routine have the added feature that if the 'decimals' parameter is negative, then the 'G' display format rather then the 'E' format will b e used when constructing the keyword value, taking the absolute value of 'decimals' for the precision. This will suppress trailing zeros, and will use a fixed format rather than an exp onential format, dep ending on the magnitude of the value. FTPKY[JKLS](unit,keyword,keyval,comment, > status) FTPKY[EDFG](unit,keyword,keyval,decimals,comment, > status) 6 Put (app end) a string valued keyword into the CHU which may b e longer than 68 characters in length. This uses the Long String Keyword convention that is describ ed in the "Usage Guidelines and Suggestions" section of this document. Since this uses a non-standard FITS convention to encode the long keyword string, programs which use this routine should also call the FTPLSW routine to add some COMMENT keywords to warn users of the FITS file that this convention is b eing used. FTPLSW also writes a keyword called LONGSTRN to record the version of the longstring convention that has b een used, in case a new convention is adopted at some p oint in the future. If the LONGSTRN keyword is already present in the header, then FTPLSW will simply return and will not write duplicate keywords.


46

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

FTPKLS(unit,keyword,keyval,comment, > status) FTPLSW(unit, > status) 7 Put (app end) a new keyword with an undefined, or null, value into the CHU. The value string of the keyword is left blank in this case. FTPKYU(unit,keyword,comment, > status) 8 Put (app end) a numb ered sequence of keywords into the CHU. One may app end the same comment to every keyword (and eliminate the need to have an array of identical comment strings, one for each keyword) by including the amp ersand character as the last non-blank character in the (first) COMMENTS string parameter. This same string will then b e used for the comment field in all the keywords. (Note that the SPP version of these routines only supp orts a single comment string). FTPKN[JKLS](unit,keyroot,startno,no_keys,keyvals,comments, > status) FTPKN[EDFG](unit,keyroot,startno,no_keys,keyvals,decimals,comments, > status) 9 Copy an indexed keyword from one HDU to another, modifying the index numb er of the keyword name in the process. For example, this routine could read the TLMIN3 keyword from the input HDU (by giving keyroot = "TLMIN" and innum = 3) and write it to the output HDU with the keyword name TLMIN4 (by setting outnum = 4). If the input keyword does not exist, then this routine simply returns without indicating an error. FTCPKY(inunit, outunit, innum, outnum, keyroot, > status) 10 Put (app end) a 'triple precision' keyword into the CHU in F28.16 format. The floating p oint keyword value is constructed by concatenating the input integer value with the input double precision fraction value (which must have a value b etween 0.0 and 1.0). The FTGKYT routine should b e used to read this keyword value, b ecause the other keyword reading subroutines will not preserve the full precision of the value. FTPKYT(unit,keyword,intval,dblval,comment, > status) 11 Write keywords to the CHDU that are defined in an ASCI I template file. The format of the template file is describ ed under the ftgthd routine b elow. FTPKTP(unit, filename, > status) 12 App end the physical units string to an existing keyword. This routine uses a local convention, shown in the following example, in which the keyword units are enclosed in square brackets in the b eginning of the keyword comment field. VELOCITY= 12.3 / [km/s] orbital speed

FTPUNT(unit,keyword,units, > status)


6.4. FITS HEADER I/O SUBROUTINES

47

6.4.4

Insert Keyword Subroutines

1 Insert a new keyword record into the CHU at the sp ecified p osition (i.e., immediately preceding the (keyno)th keyword in the header.) This 'insert record' subroutine is somewhat less efficient then the 'app end record' subroutine (FTPREC) describ ed ab ove b ecause the remaining keywords in the header have to b e shifted down one slot. FTIREC(unit,key_no,card, > status) 2 Insert a new keyword into the CHU. The new keyword is inserted immediately following the last keyword that has b een read from the header. The FTIKLS subroutine works the same as the FTIKYS subroutine, except it also supp orts long string values greater than 68 characters in length. These 'insert keyword' subroutines are somewhat less efficient then the 'app end keyword' subroutines describ ed ab ove b ecause the remaining keywords in the header have to b e shifted down one slot. F F F F T T T T I I I I K K K K E Y L Y Y [ S [ ( J ( E u K u D n L n F i S i G t ] t ] , ( , ( card, unit,k keywor unit,k > ey d, ey status) word,keyval,comment, > status) keyval,comment, > status) word,keyval,decimals,comment, > status)

3 Insert a new keyword with an undefined, or null, value into the CHU. The value string of the keyword is left blank in this case. FTIKYU(unit,keyword,comment, > status)

6.4.5

Read Keyword Subroutines

These routines return the value of the sp ecified keyword(s). Wild card characters (*, ?, or #) may b e used when sp ecifying the name of the keyword to b e read: a ' ?' will match any single character at that p osition in the keyword name and a '*' will match any length (including zero) string of characters. The '#' character will match any consecutive string of decimal digits (0 - 9). Note that when a wild card is used in the input keyword name, the routine will only search for a match from the current header p osition to the end of the header. It will not resume the search from the top of the header back to the original header p osition as is done when no wildcards are included in the keyword name. If the desired keyword string is 8-characters long (the maximum length of a keyword name) then a '*' may b e app ended as the ninth character of the input name to force the keyword search to stop at the end of the header (e.g., 'COMMENT *' will search for the next COMMENT keyword). The ffgrec routine may b e used to set the starting p osition when doing wild card searches. 1 Get the nth key no = b eginning header; it 80-character header record from the CHU. The first keyword in the header is at 1; if key no = 0 then this subroutine simple moves the internal p ointer to the of the header so that subsequent keyword op erations will start at the top of the also returns a blank card value in this case.


48

CHAPTER 6. FTGREC(unit,key_no, > card,status)

ADVANCED INTERFACE SUBROUTINES

2 Get the name, value (as a string), and comment of the nth keyword in CHU. This routine also checks that the returned keyword name (KEYWORD) contains only legal ASCI I characters. Call FTGREC and FTPSVC to bypass this error check. FTGKYN(unit,key_no, > keyword,value,comment,status) 3 Get the 80-character header record for the named keyword FTGCRD(unit,keyword, > card,status) 4 Get the next keyword whose name matches one of the strings in 'inclist' but does not match any of the strings in 'exclist'. The strings in inclist and exclist may contain wild card characters (*, ?, and #) as describ ed at the b eginning of this section. This routine searches from the current header p osition to the end of the header, only, and does not continue the search from the top of the header back to the original p osition. The current header p osition may b e reset with the ftgrec routine. Note that nexc may b e set = 0 if there are no keywords to b e excluded. This routine returns status = 202 if a matching keyword is not found. FTGNXK(unit,inclist,ninc,exclist,nexc, > card,status) 5 Get the literal keyword value as a character string. Regardless of the datatyp e of the keyword, this routine simply returns the string of characters in the value field of the keyword along with the comment field. FTGKEY(unit,keyword, > value,comment,status) 6 Get a keyword value (with the appropriate datatyp e) and comment from the CHU FTGKY[EDJKLS](unit,keyword, > keyval,comment,status) 7 Get a sequence of numb ered keyword values. These routines do not supp ort wild card characters in the root name. FTGKN[EDJKLS](unit,keyroot,startno,max_keys, > keyvals,nfound,status) 8 Get the value of a floating p oint keyword, returning the integer and fractional parts of the value in separate subroutine arguments. This subroutine may b e used to read any keyword but is esp ecially useful for reading the 'triple precision' keywords written by FTPKYT. FTGKYT(unit,keyword, > intval,dblval,comment,status)


6.4. FITS HEADER I/O SUBROUTINES

49

9 Get the physical units string in an existing keyword. This routine uses a local convention, shown in the following example, in which the keyword units are enclosed in square brackets in the b eginning of the keyword comment field. A blank string is returned if no units are defined for the keyword. VELOCITY= 12.3 / [km/s] orbital speed

FTGUNT(unit,keyword, > units,status)

6.4.6

Modify Keyword Subroutines

Wild card characters, as describ ed in the Read Keyword section, ab ove, may b e used when sp ecifying the name of the keyword to b e modified. 1 Modify (overwrite) the nth 80-character header record in the CHU FTMREC(unit,key_no,card, > status) 2 Modify (overwrite) the 80-character header record for the named keyword in the CHU. This can b e used to overwrite the name of the keyword as well as its value and comment fields. FTMCRD(unit,keyword,card, > status) 3 Modify (overwrite) the name of an existing keyword in the CHU preserving the current value and comment fields. FTMNAM(unit,oldkey,keyword, > status) 4 Modify (overwrite) the comment field of an existing keyword in the CHU FTMCOM(unit,keyword,comment, > status) 5 Modify the value and comment fields of an existing keyword in the CHU. The FTMKLS subroutine works the same as the FTMKYS subroutine, except it also supp orts long string values greater than 68 characters in length. Optionally, one may modify only the value field and leave the comment field unchanged by setting the input COMMENT parameter equal to the amp ersand character (&). The E and D versions of this routine have the added feature that if the 'decimals' parameter is negative, then the 'G' display format rather then the 'E' format will b e used when constructing the keyword value, taking the absolute value of 'decimals' for the precision. This will suppress trailing zeros, and will use a fixed format rather than an exp onential format, dep ending on the magnitude of the value. FTMKY[JKLS](unit,keyword,keyval,comment, > status) FTMKLS(unit,keyword,keyval,comment, > status) FTMKY[EDFG](unit,keyword,keyval,decimals,comment, > status)


50

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

6 Modify the value of an existing keyword to b e undefined, or null. The value string of the keyword is set to blank. Optionally, one may leave the comment field unchanged by setting the input COMMENT parameter equal to the amp ersand character (&). FTMKYU(unit,keyword,comment, > status)

6.4.7

Update Keyword Subroutines

1 Up date an 80-character record in the CHU. If the sp ecified keyword already exists then that header record will b e replaced with the input CARD string. If it does not exist then the new record will b e added to the header. The FTUKLS subroutine works the same as the FTUKYS subroutine, except it also supp orts long string values greater than 68 characters in length. FTUCRD(unit,keyword,card, > status) 2 Up date the value and comment fields of a keyword in the CHU. The sp ecified keyword is modified if it already exists (by calling FTMKYx) otherwise a new keyword is created by calling FTPKYx. The E and D versions of this routine have the added feature that if the 'decimals' parameter is negative, then the 'G' display format rather then the 'E' format will b e used when constructing the keyword value, taking the absolute value of 'decimals' for the precision. This will suppress trailing zeros, and will use a fixed format rather than an exp onential format, dep ending on the magnitude of the value. FTUKY[JKLS](unit,keyword,keyval,comment, > status) FTUKLS(unit,keyword,keyval,comment, > status) FTUKY[EDFG](unit,keyword,keyval,decimals,comment, > status) 3 Up date the value of an existing keyword to b e undefined, or null, or insert a new undefined-value keyword if it doesn't already exist. The value string of the keyword is left blank in this case. FTUKYU(unit,keyword,comment, > status)

6.4.8

Delete Keyword Subroutines

1 Delete an existing keyword record. The space previously occupied by the keyword is reclaimed by moving all the following header records up one row in the header. The first routine deletes a keyword at a sp ecified p osition in the header (the first keyword is at p osition 1), whereas the second routine deletes a sp ecifically named keyword. Wild card characters, as describ ed in the Read Keyword section, ab ove, may b e used when sp ecifying the name of the keyword to b e deleted (b e careful!). FTDREC(unit,key_no, > status) FTDKEY(unit,keyword, > status)


6.5. DATA SCALING AND UNDEFINED PIXEL PARAMETERS

51

6.5

Data Scaling and Undefined Pixel Parameters

These subroutines define or modify the internal parameters used by FITSIO to either scale the data or to represent undefined pixels. Generally FITSIO will scale the data according to the values of the BSCALE and BZERO (or TSCALn and TZEROn) keywords, however these subroutines may b e used to override the keyword values. This may b e useful when one wants to read or write the raw unscaled values in the FITS file. Similarly, FITSIO generally uses the value of the BLANK or TNULLn keyword to signify an undefined pixel, but these routines may b e used to override this value. These subroutines do not create or modify the corresp onding header keyword values. 1 Reset the scaling factors in the primary array or image extension; does not change the BSCALE and BZERO keyword values and only affects the automatic scaling p erformed when the data elements are written/read to/from the FITS file. When reading from a FITS file the returned data value = (the value given in the FITS array) * BSCALE + BZERO. The inverse formula is used when writing data values to the FITS file. (NOTE: BSCALE and BZERO must b e declared as Double Precision variables). FTPSCL(unit,bscale,bzero, > status) 2 Reset the scaling parameters for a table column; does not change the TSCALn or TZEROn keyword values and only affects the automatic scaling p erformed when the data elements are written/read to/from the FITS file. When reading from a FITS file the returned data value = (the value given in the FITS array) * TSCAL + TZERO. The inverse formula is used when writing data values to the FITS file. (NOTE: TSCAL and TZERO must b e declared as Double Precision variables). FTTSCL(unit,colnum,tscal,tzero, > status) 3 Define the integer value to b e used to signify undefined pixels in the primary array or image extension. This is only used if BITPIX = 8, 16, 32. or 64 This does not create or change the value of the BLANK keyword in the header. FTPNULLL is identical to FTPNUL except that the blank value is a 64-bit integer instead of a 32-bit integer. FTPNUL(unit,blank, > status) FTPNULLL(unit,blankll, > status) 4 Define the string to b e used to signify undefined pixels in a column in an ASCI I table. This does not create or change the value of the TNULLn keyword. FTSNUL(unit,colnum,snull > status) 5 Define the value to b e used to signify undefined pixels in an integer column in a binary table (where TFORMn = 'B', 'I', 'J', or 'K'). This does not create or change the value of the TNULLn keyword. FTTNULLL is identical to FTTNUL except that the tnull value is a 64-bit integer instead of a 32-bit integer.


52

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

FTTNUL(unit,colnum,tnull > status) FTTNULLL(unit,colnum,tnullll > status)

6.6

FITS Primary Array or IMAGE Extension I/O Subroutines

These subroutines put or get data values in the primary data array (i.e., the first HDU in the FITS file) or an IMAGE extension. The data array is represented as a single one-dimensional array of pixels regardless of the actual dimensionality of the array, and the FPIXEL parameter gives the p osition within this 1-D array of the first pixel to read or write. Automatic data typ e conversion is p erformed for numeric data (except for complex data typ es) if the data typ e of the primary array (defined by the BITPIX keyword) differs from the data typ e of the array in the calling subroutine. The data values are also scaled by the BSCALE and BZERO header values as they are b eing written or read from the FITS array. The ftpscl subroutine MUST b e called to define the scaling parameters when writing data to the FITS array or to override the default scaling value given in the header when reading the FITS array. Two sets of subroutines are provided to read the data array which differ in the way undefined pixels are handled. The first set of routines (FTGPVx) simply return an array of data elements in which undefined pixels are set equal to a value sp ecified by the user in the 'nullval' parameter. An additional feature of these subroutines is that if the user sets nullval = 0, then no checks for undefined pixels will b e p erformed, thus increasing the sp eed of the program. The second set of routines (FTGPFx) returns the data element array and, in addition, a logical array which defines whether the corresp onding data pixel is undefined. The latter set of subroutines may b e more convenient to use in some circumstances, however, it requires an additional array of logical values which can b e unwieldy when working with large data arrays. Also for programmer convenience, sets of subroutines to directly read or write 2 and 3 dimensional arrays have b een provided, as well as a set of subroutines to read or write any contiguous rectangular subset of pixels within the n-dimensional array. 1 Get the data typ e of the image (= BITPIX value). Possible returned values are: 8, 16, 32, 64, -32, or -64 corresp onding to unsigned byte, signed 2-byte integer, signed 4-byte integer, signed 8-byte integer, real, and double. The second subroutine is similar to FTGIDT, except that if the image pixel values are scaled, with non-default values for the BZERO and BSCALE keywords, then this routine will return the 'equivalent' data typ e that is needed to store the scaled values. For example, if BITPIX = 16 and BSCALE = 0.1 then the equivalent data typ e is floating p oint, and -32 will b e returned. There are 2 sp ecial cases: if the image contains unsigned 2-byte integer values, with BITPIX = 16, BSCALE = 1, and BZERO = 32768, then this routine will return a non-standard value of 20 for the bitpix value. Similarly if the image contains unsigned 4-byte integers, then bitpix will b e returned with a value of 40. FTGIDT(unit, > bitpix,status) FTGIET(unit, > bitpix,status) 2 Get the dimension (numb er of axes = NAXIS) of the image


6.6. FITS PRIMARY ARRAY OR IMAGE EXTENSION I/O SUBROUTINES FTGIDM(unit, > naxis,status)

53

3 Get the size of all the dimensions of the image. The FTGISZLL routine returns an array of 64-bit integers instead of 32-bit integers. FTGISZ(unit, maxdim, > naxes,status) FTGISZLL(unit, maxdim, > naxesll,status) 4 Get the parameters that define the typ e and size of the image. This routine simply combines calls to the ab ove 3 routines. The FTGIPRLL routine returns an array of 64-bit integers instead of 32-bit integers. FTGIPR(unit, maxdim, > bitpix, naxis, naxes, int *status) FTGIPRLL(unit, maxdim, > bitpix, naxis, naxesll, int *status) 5 Put elements into the data array FTPPR[BIJKED](unit,group,fpixel,nelements,values, > status) 6 Put elements into the data array, substituting the appropriate FITS null value for all elements which are equal to the value of NULLVAL. For integer FITS arrays, the null value defined by the previous call to FTPNUL will b e substituted; for floating p oint FITS arrays (BITPIX = -32 or -64) then the sp ecial IEEE NaN (Not-a-Numb er) value will b e substituted. FTPPN[BIJKED](unit,group,fpixel,nelements,values,nullval > status) 7 Set data array elements as undefined FTPPRU(unit,group,fpixel,nelements, > status) 8 Get elements from the data array. Undefined array elements will b e returned with a value = nullval, unless nullval = 0 in which case no checks for undefined pixels will b e p erformed. FTGPV[BIJKED](unit,group,fpixel,nelements,nullval, > values,anyf,status) 9 Get elements and nullflags from data array. Any undefined array elements will have the corresp onding flagvals element set equal to .TRUE. FTGPF[BIJKED](unit,group,fpixel,nelements, > values,flagvals,anyf,status) 10 Put values into group parameters FTPGP[BIJKED](unit,group,fparm,nparm,values, > status)


54

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

11 Get values from group parameters FTGGP[BIJKED](unit,group,fparm,nparm, > values,status) The following 4 subroutines transfer FITS images with 2 or 3 dimensions to or from a data array which has b een declared in the calling program. The dimensionality of the FITS image is passed by the naxis1, naxis2, and naxis3 parameters and the declared dimensions of the program array are passed in the dim1 and dim2 parameters. Note that the program array does not have to have the same dimensions as the FITS array, but must b e at least as big. For example if a FITS image with NAXIS1 = NAXIS2 = 400 is read into a program array which is dimensioned as 512 x 512 pixels, then the image will just fill the lower left corner of the array with pixels in the range 1 - 400 in the X an Y directions. This has the effect of taking a contiguous set of pixel value in the FITS array and writing them to a non-contiguous array in program memory (i.e., there are now some blank pixels around the edge of the image in the program array). 11 Put 2-D image into the data array FTP2D[BIJKED](unit,group,dim1,naxis1,naxis2,image, > status) 12 Put 3-D cub e into the data array FTP3D[BIJKED](unit,group,dim1,dim2,naxis1,naxis2,naxis3,cube, > status) 13 Get 2-D image from the data array. Undefined pixels in the array will b e set equal to the value of 'nullval', unless nullval=0 in which case no testing for undefined pixels will b e p erformed. FTG2D[BIJKED](unit,group,nullval,dim1,naxis1,naxis2, > image,anyf,status) 14 Get 3-D cub e from the data array. Undefined pixels in the array will b e set equal to the value of 'nullval', unless nullval=0 in which case no testing for undefined pixels will b e p erformed. FTG3D[BIJKED](unit,group,nullval,dim1,dim2,naxis1,naxis2,naxis3, > cube,anyf,status) The following subroutines transfer a rectangular subset of the pixels in a FITS N-dimensional image to or from an array which has b een declared in the calling program. The fpixels and lpixels parameters are integer arrays which sp ecify the starting and ending pixels in each dimension of the FITS image that are to b e read or written. (Note that these are the starting and ending pixels in the FITS image, not in the declared array). The array parameter is treated simply as a large one-dimensional array of the appropriate datatyp e containing the pixel values; The pixel values in the FITS array are read/written from/to this program array in strict sequence without any gaps; it is up to the calling routine to correctly interpret the dimensionality of this array. The two families of FITS reading routines (FTGSVx and FTGSFx subroutines) also have an 'incs' parameter which defines the data sampling interval in each dimension of the FITS array. For example, if incs(1)=2


6.7. FITS ASCI I AND BINARY TABLE DATA I/O SUBROUTINES

55

and incs(2)=3 when reading a 2-dimensional FITS image, then only every other pixel in the first dimension and every 3rd pixel in the second dimension will b e returned in the 'array' parameter. [Note: the FTGSSx family of routines which were present in previous versions of FITSIO have b een sup erseded by the more general FTGSVx family of routines.] 15 Put an arbitrary data subsection into the data array. FTPSS[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,array, > status) 16 Get an arbitrary data subsection from the data array. Undefined pixels in the array will b e set equal to the value of 'nullval', unless nullval=0 in which case no testing for undefined pixels will b e p erformed. FTGSV[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,incs,nullval, > array,anyf,status) 17 Get an arbitrary data subsection from the data array. Any Undefined pixels in the array will have the corresp onding 'flagvals' element set equal to .TRUE. FTGSF[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,incs, > array,flagvals,anyf,status)

6.7
6.7.1

FITS ASCI I and Binary Table Data I/O Subroutines
Column Information Subroutines

1 Get the numb er of rows or columns in the current FITS table. The numb er of rows is given by the NAXIS2 keyword and the numb er of columns is given by the TFIELDS keyword in the header of the table. The FTGNRWLL routine is identical to FTGNRW except that the numb er of rows is returned as a 64-bit integer rather than a 32-bit integer. FTGNRW(unit, > nrows, status) FTGNRWLL(unit, > nrowsll, status) FTGNCL(unit, > ncols, status) 2 Get the table column numb er (and name) of the column whose name matches an input template name. The table column names are defined by the TTYPEn keywords in the FITS header. If a column does not have a TTYPEn keyword, then these routines assume that the name consists of all blank characters. These 2 subroutines p erform the same function except that FTGCNO only returns the numb er of the matching column whereas FTGCNN also returns the name of the column. If CASESEN = .true. then the column name match will b e case-sensitive. The input column name template (COLTEMPLATE) is (1) either the exact name of the column to b e searched for, or (2) it may contain wild cards characters (*, ?, or #), or (3)


56

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

it may contain the numb er of the desired column (where the numb er is expressed as ASCI I digits). The first 2 wild cards b ehave similarly to UNIX filename matching: the '*' character matches any sequence of characters (including zero characters) and the ' ?' character matches any single character. The # wildcard will match any consecutive string of decimal digits (0-9). As an example, the template strings 'AB?DE', 'AB*E', and 'AB*CDE' will all match the string 'ABCDE'. If more than one column name in the table matches the template string, then the first match is returned and the status value will b e set to 237 as a warning that a unique match was not found. To find the other cases that match the template, simply call the subroutine again leaving the input status value equal to 237 and the next matching name will then b e returned. Rep eat this process until a status = 219 (column name not found) is returned. If these subroutines fail to match the template to any of the columns in the table, they lastly check if the template can b e interpreted as a simple p ositive integer (e.g., '7', or '512') and if so, they return that column numb er. If no matches are found then a status = 219 error is returned. Note that the FITS Standard recommends that only letters, digits, and the underscore character b e used in column names (with no emb edded spaces in the name). Trailing blank characters are not significant. FTGCNO(unit,casesen,coltemplate, > colnum,status) FTGCNN(unit,casesen,coltemplate, > colname,colnum,status) 3 Get the datatyp e of a column in an ASCI I or binary table. This routine returns an integer code value corresp onding to the datatyp e of the column. (See the FTBNFM and FTASFM subroutines in the Utilities section of this document for a list of the code values). The vector rep eat count (which is alway 1 for ASCI I table columns) is also returned. If the sp ecified column has an ASCI I character datatyp e (code = 16) then the width of a unit string in the column is also returned. Note that this routine supp orts the local convention for sp ecifying arrays of strings within a binary table character column, using the syntax TFORM = 'rAw' where 'r' is the total numb er of characters (= the width of the column) and 'w' is the width of a unit string within the column. Thus if the column has TFORM = '60A12' then this routine will return datacode = 16, rep eat = 60, and width = 12. (The TDIMn keyword may also b e used to sp ecify the unit string length; The pair of keywords TFORMn = '60A' and TDIMn = '(12,5)' would have the same effect as TFORMn = '60A12'). The second routine, FTEQTY is similar except that in the case of scaled integer columns it returns the 'equivalent' data typ e that is needed to store the scaled values, and not necessarily the physical data typ e of the unscaled values as stored in the FITS table. For example if a '1I' column in a binary table has TSCALn = 1 and TZEROn = 32768, then this column effectively contains unsigned short integer values, and thus the returned value of typ ecode will b e the code for an unsigned short integer, not a signed short integer. Similarly, if a column has TTYPEn = '1I' and TSCALn = 0.12, then the returned typ ecode will b e the code for a 'real' column. FTGTCL(unit,colnum, > datacode,repeat,width,status) FTEQTY(unit,colnum, > datacode,repeat,width,status)


6.7. FITS ASCI I AND BINARY TABLE DATA I/O SUBROUTINES

57

4 Return the display width of a column. This is the length of the string that will b e returned when reading the column as a formatted string. The display width is determined by the TDISPn keyword, if present, otherwise by the data typ e of the column. FTGCDW(unit, colnum, > dispwidth, status) 5 Get information ab out an existing ASCI I table column. (NOTE: TSCAL and TZERO must b e declared as Double Precision variables). All the returned parameters are scalar quantities. FTGACL(unit,colnum, > ttype,tbcol,tunit,tform,tscal,tzero,snull,tdisp,status) 6 Get information ab out an existing binary table column. (NOTE: TSCAL and TZERO must b e declared as Double Precision variables). DATATYPE is a character string which returns the datatyp e of the column as defined by the TFORMn keyword (e.g., 'I', 'J','E', 'D', etc.). In the case of an ASCI I character column, DATATYPE will have a value of the form 'An' where 'n' is an integer expressing the width of the field in characters. For example, if TFORM = '160A8' then FTGBCL will return DATATYPE='A8' and REPEAT=20. All the returned parameters are scalar quantities. FTGBCL(unit,colnum, > ttype,tunit,datatype,repeat,tscal,tzero,tnull,tdisp,status) 7 Put (app end) a TDIMn keyword whose value has the form '(l,m,n...)' where l, m, n... are the dimensions of a multidimensional array column in a binary table. FTPTDM(unit,colnum,naxis,naxes, > status) 8 Return the numb er of and size of the dimensions of a table column. Normally this information is given by the TDIMn keyword, but if this keyword is not present then this routine returns NAXIS = 1 and NAXES(1) equal to the rep eat count in the TFORM keyword. FTGTDM(unit,colnum,maxdim, > naxis,naxes,status) 9 Decode the input TDIMn keyword string (e.g. '(100,200)') and return the numb er of and size of the dimensions of a binary table column. If the input tdimstr character string is null, then this routine returns naxis = 1 and naxes[0] equal to the rep eat count in the TFORM keyword. This routine is called by FTGTDM. FTDTDM(unit,tdimstr,colnum,maxdim, > naxis,naxes, status) 10 Return the optimal numb er of rows to read or write at one time for maximum I/O efficiency. Refer to the "Optimizing Code" section in Chapter 5 for more discussion on how to use this routine. FFGRSZ(unit, > nrows,status)


58

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

6.7.2

Low-Level Table Access Subroutines

The following subroutines provide low-level access to the data in ASCI I or binary tables and are mainly useful as an efficient way to copy all or part of a table from one location to another. These routines simply read or write the sp ecified numb er of consecutive bytes in an ASCI I or binary table, without regard for column b oundaries or the row length in the table. The first two subroutines read or write consecutive bytes in a table to or from a character string variable, while the last two subroutines read or write consecutive bytes to or from a variable declared as a numeric data typ e (e.g., INTEGER, INTEGER*2, REAL, DOUBLE PRECISION). These routines do not p erform any machine dep endent data conversion or byte swapping, except that conversion to/from ASCI I format is p erformed by the FTGTBS and FTPTBS routines on machines which do not use ASCI I character codes in the internal data representations (e.g., on IBM mainframe computers). 1 Read a consecutive string of characters from an ASCI I table into a character variable (spanning columns and multiple rows if necessary) This routine should not b e used with binary tables b ecause of complications related to passing string variables b etween C and Fortran. FTGTBS(unit,frow,startchar,nchars, > string,status) 2 Write a consecutive string of characters to an ASCI I table from a character variable (spanning columns and multiple rows if necessary) This routine should not b e used with binary tables b ecause of complications related to passing string variables b etween C and Fortran. FTPTBS(unit,frow,startchar,nchars,string, > status) 3 Read a consecutive array of bytes from an ASCI I or binary table into a numeric variable (spanning columns and multiple rows if necessary). The array parameter may b e declared as any numerical datatyp e as long as the array is at least 'nchars' bytes long, e.g., if nchars = 17, then declare the array as INTEGER*4 ARRAY(5). FTGTBB(unit,frow,startchar,nchars, > array,status) 4 Write a consecutive array of bytes to ning columns and multiple rows if numerical datatyp e as long as the then declare the array as INTEGE an ASCI I or binary table from a numeric variable (spannecessary) The array parameter may b e declared as any array is at least 'nchars' bytes long, e.g., if nchars = 17, R*4 ARRAY(5).

FTPTBB(unit,frow,startchar,nchars,array, > status)

6.7.3

Edit Rows or Columns

1 Insert blank rows into an existing ASCI I or binary table (in the CDU). All the rows FOLLOWING row FROW are shifted down by NROWS rows. If FROW or FROWLL equals 0 then the blank rows are inserted at the b eginning of the table. These routines modify the NAXIS2 keyword to reflect the new numb er of rows in the table.


6.7. FITS ASCI I AND BINARY TABLE DATA I/O SUBROUTINES FTIROW(unit,frow,nrows, > status) FTIROWLL(unit,frowll,nrowsll, > status) 2 Delete rows from an existing ASCI I is the numb er of rows are deleted, rows in the table are shifted up to to reflect the new numb er of row

59

or binary table (in the CDU). The NROWS (or NROWSLL) starting with row FROW (or FROWLL), and any remaining fill in the space. These routines modify the NAXIS2 keyword s in the table.

FTDROW(unit,frow,nrows, > status) FTDROWLL(unit,frowll,nrowsll, > status) 3 Delete a list of rows from an ASCI I or binary table (in the CDU). In the first routine, 'rowrange' is a character string listing the rows or row ranges to delete (e.g., '2-4, 5, 8-9'). In the second routine, 'rowlist' is an integer array of row numb ers to b e deleted from the table. nrows is the numb er of row numb ers in the list. The first row in the table is 1 not 0. The list of row numb ers must b e sorted in ascending order. FTDRRG(unit,rowrange, > status) FTDRWS(unit,rowlist,nrows, > status) 4 Insert a blank column (or columns) into an existing ASCI I or binary table (in the CDU). COLNUM sp ecifies the column numb er that the (first) new column should occupy in the table. NCOLS sp ecifies how many columns are to b e inserted. Any existing columns from this p osition and higher are moved over to allow room for the new column(s). The index numb er on all the following keywords will b e incremented if necessary to reflect the new p osition of the column(s) in the table: TBCOLn, TFORMn, TTYPEn, TUNITn, TNULLn, TSCALn, TZEROn, TDISPn, TDIMn, TLMINn, TLMAXn, TDMINn, TDMAXn, TCTYPn, TCRPXn, TCRVLn, TCDLTn, TCROTn, and TCUNIn. FTICOL(unit,colnum,ttype,tform, > status) FTICLS(unit,colnum,ncols,ttype,tform, > status) 5 Modify the vector length of a binary table column (e.g., change a column from TFORMn = '1E' to '20E'). The vector length may b e increased or decreased from the current value. FTMVEC(unit,colnum,newveclen, > status) 6 Delete a column from an existing ASCI I or binary table (in the CDU). The index numb er of all the keywords listed ab ove (for FTICOL) will b e decremented if necessary to reflect the new p osition of the column(s) in the table. Those index keywords that refer to the deleted column will also b e deleted. Note that the physical size of the FITS file will not b e reduced by this op eration, and the empty FITS blocks if any at the end of the file will b e padded with zeros. FTDCOL(unit,colnum, > status)


60

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

7 Copy a column from one HDU to another (or to the same HDU). If createcol = TRUE, then a new column will b e inserted in the output table, at p osition `outcolumn', otherwise the existing output column will b e overwritten (in which case it must have a compatible datatyp e). Note that the first column in a table is at colnum = 1. FTCPCL(inunit,outunit,incolnum,outcolnum,createcol, > status);

6.7.4

Read and Write Column Data Routines

These subroutines put or get data values in the current ASCI I or Binary table extension. Automatic data typ e conversion is p erformed for numerical data typ es (B,I,J,E,D) if the data typ e of the column (defined by the TFORM keyword) differs from the data typ e of the calling subroutine. The data values are also scaled by the TSCALn and TZEROn header values as they are b eing written to or read from the FITS array. The fttscl subroutine MUST b e used to define the scaling parameters when writing data to the table or to override the default scaling values given in the header when reading from the table. In the case of binary tables with vector elements, the 'felem' parameter defines the starting pixel within the element vector. This parameter is ignored with ASCI I tables. Similarly, in the case of binary tables the 'nelements' parameter sp ecifies the total numb er of vector values read or written (continuing on subsequent rows if required) and not the numb er of table elements. Two sets of subroutines are provided to get the column data which differ in the way undefined pixels are handled. The first set of routines (FTGCV) simply return an array of data elements in which undefined pixels are set equal to a value sp ecified by the user in the 'nullval' parameter. An additional feature of these subroutines is that if the user sets nullval = 0, then no checks for undefined pixels will b e p erformed, thus increasing the sp eed of the program. The second set of routines (FTGCF) returns the data element array and in addition a logical array of flags which defines whether the corresp onding data pixel is undefined. Any column, regardless of it's intrinsic datatyp e, may b e read as a string. It should b e noted however that reading a numeric column as a string is 10 - 100 times slower than reading the same column as a numb er due to the large overhead in constructing the formatted strings. The display format of the returned strings will b e determined by the TDISPn keyword, if it exists, otherwise by the datatyp e of the column. The length of the returned strings can b e determined with the ftgcdw routine. The following TDISPn display formats are currently supp orted: I O Z F E D G w w w w w w w . . . . . . . m m m d d d d I O H F E E G n c e i x x e t t x x p p n e a a e o o e g l d d n n r er integer ecimal integer floating point ential floating point ential floating point al; uses Fw.d if significance not lost, else Ew.d

where w is the width in characters of the displayed values, m is the minimum numb er of digits displayed, and d is the numb er of digits to the right of the decimal. The .m field is optional.


6.7. FITS ASCI I AND BINARY TABLE DATA I/O SUBROUTINES

61

1 Put elements into an ASCI I or binary table column (in the CDU). (The SPP FSPCLS routine has an additional integer argument after the VALUES character string which sp ecifies the size of the 1st dimension of this 2-D CHAR array). The alternate version of these routines, whose names end in 'LL' after the datatyp e character, supp ort large tables with more then 2*31 rows. When calling these routines, the frow and felem parameters *must* b e 64-bit integer*8 variables, instead of normal 4-byte integers. FTPCL[SLBIJKEDCM](unit,colnum,frow,felem,nelements,values, > status) FTPCL[LBIJKEDCM]LL(unit,colnum,frow,felem,nelements,values, > status) 2 Put elements into an ASCI I or binary table column (in the CDU) substituting the appropriate FITS null value for any elements that are equal to NULLVAL. For ASCI I TABLE extensions, the null value defined by the previous call to FTSNUL will b e substituted; For integer FITS columns, in a binary table the null value defined by the previous call to FTTNUL will b e substituted; For floating p oint FITS columns a sp ecial IEEE NaN (Not-a-Numb er) value will b e substituted. The alternate version of these routines, whose names end in 'LL' after the datatyp e character, supp ort large tables with more then 2*31 rows. When calling these routines, the frow and felem parameters *must* b e 64-bit integer*8 variables, instead of normal 4-byte integers. FTPCN[SBIJKED](unit,colnum,frow,felem,nelements,values,nullval > status) FTPCN[SBIJKED]LL(unit,colnum,(I*8) frow,(I*8) felem,nelements,values, nullval > status) 3 Put bit values into a binary byte ('B') or bit ('X') table column (in the CDU). LRAY is an array of logical values corresp onding to the sequence of bits to b e written. If LRAY is true then the corresp onding bit is set to 1, otherwise the bit is set to 0. Note that in the case of 'X' columns, FITSIO will write to all 8 bits of each byte whether they are formally valid or not. Thus if the column is defined as '4X', and one calls FTPCLX with fbit=1 and nbit=8, then all 8 bits will b e written into the first byte (as opp osed to writing the first 4 bits into the first row and then the next 4 bits into the next row), even though the last 4 bits of each byte are formally not defined. FTPCLX(unit,colnum,frow,fbit,nbit,lray, > status) 4 Set table elements in a column as undefined FTPCLU(unit,colnum,frow,felem,nelements, > status) 5 Get elements from an ASCI I or binary table column (in the CDU). These routines return the values of the table column array elements. Undefined array elements will b e returned with a value = nullval, unless nullval = 0 (or = ' ' for ftgcvs) in which case no checking for undefined values will b e p erformed. The ANYF parameter is set to true if any of the returned elements are undefined. (Note: the ftgcl routine simple gets an array of logical data values without any


62

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

checks for undefined values; use the ftgcfl routine to check for undefined logical elements). (The SPP FSGCVS routine has an additional integer argument after the VALUES character string which sp ecifies the size of the 1st dimension of this 2-D CHAR array). The alternate version of these routines, whose names end in 'LL' after the datatyp e character, supp ort large tables with more then 2*31 rows. When calling these routines, the frow and felem parameters *must* b e 64-bit integer*8 variables, instead of normal 4-byte integers. FTGCL(unit,colnum,frow,felem,nelements, > values,status) FTGCV[SBIJKEDCM](unit,colnum,frow,felem,nelements,nullval, > values,anyf,status) FTGCV[BIJKEDCM]LL(unit,colnum,(I*8) frow, (I*8) felem, nelements, nullval, > values,anyf,status) 6 Get elements and null flags from an ASCI I or binary table column (in the CHDU). These routines return the values of the table column array elements. Any undefined array elements will have the corresp onding flagvals element set equal to .TRUE. The ANYF parameter is set to true if any of the returned elements are undefined. (The SPP FSGCFS routine has an additional integer argument after the VALUES character string which sp ecifies the size of the 1st dimension of this 2-D CHAR array). The alternate version of these routines, whose names end in 'LL' after the datatyp e character, supp ort large tables with more then 2*31 rows. When calling these routines, the frow and felem parameters *must* b e 64-bit integer*8 variables, instead of normal 4-byte integers. FTGCF[SLBIJKEDCM](unit,colnum,frow,felem values,flagvals,anyf,st FTGCF[BIJKED]LL(unit,colnum, (I*8) frow, values,flagvals,anyf,st ,nelements, > atus) (I*8) felem,nelements, > atus)

7 Get an arbitrary data subsection from an N-dimensional array in a binary table vector column. Undefined pixels in the array will b e set equal to the value of 'nullval', unless nullval=0 in which case no testing for undefined pixels will b e p erformed. The first and last rows in the table to b e read are sp ecified by fpixels(naxis+1) and lpixels(naxis+1), and hence are treated as the next higher dimension of the FITS N-dimensional array. The INCS parameter sp ecifies the sampling interval in each dimension b etween the data elements that will b e returned. FTGSV[BIJKED](unit,colnum,naxis,naxes,fpixels,lpixels,incs,nullval, > array,anyf,status) 8 Get an arbitrary data subsection from an N-dimensional array in a binary table vector column. Any Undefined pixels in the array will have the corresp onding 'flagvals' element set equal to .TRUE. The first and last rows in the table to b e read are sp ecified by fpixels(naxis+1) and lpixels(naxis+1), and hence are treated as the next higher dimension of the FITS Ndimensional array. The INCS parameter sp ecifies the sampling interval in each dimension b etween the data elements that will b e returned.


6.7. FITS ASCI I AND BINARY TABLE DATA I/O SUBROUTINES FTGSF[BIJKED](unit,colnum,naxis,naxes,fpixels,lpixels,incs, > array,flagvals,anyf,status)

63

9 Get bit values from a byte ('B') or bit (`X`) table column (in the CDU). LRAY is an array of logical values corresp onding to the sequence of bits to b e read. If LRAY is true then the corresp onding bit was set to 1, otherwise the bit was set to 0. Note that in the case of 'X' columns, FITSIO will read all 8 bits of each byte whether they are formally valid or not. Thus if the column is defined as '4X', and one calls FTGCX with fbit=1 and nbit=8, then all 8 bits will b e read from the first byte (as opp osed to reading the first 4 bits from the first row and then the first 4 bits from the next row), even though the last 4 bits of each byte are formally not defined. FTGCX(unit,colnum,frow,fbit,nbit, > lray,status) 10 Read any consecutive set of bits from an 'X' or 'B' column and interpret them as an unsigned n-bit integer. NBIT must b e less than or equal to 16 when calling FTGCXI, and less than or equal to 32 when calling FTGCXJ; there is no limit on the value of NBIT for FTGCXD, but the returned double precision value only has 48 bits of precision on most 32-bit word machines. The NBITS bits are interpreted as an unsigned integer unless NBITS = 16 (in FTGCXI) or 32 (in FTGCXJ) in which case the string of bits are interpreted as 16-bit or 32-bit 2's complement signed integers. If NROWS is greater than 1 then the same set of bits will b e read from sequential rows in the table starting with row FROW. Note that the numb ering convention used here for the FBIT parameter adopts 1 for the first element of the vector of bits; this is the Most Significant Bit of the integer value. FTGCX[IJD](unit,colnum,frow,nrows,fbit,nbit, > array,status) 11 Get the descriptor for a variable length column in a binary table. The descriptor consists of 2 integer parameters: the numb er of elements in the array and the starting offset relative to the start of the heap. The first routine returns a single descriptor whereas the second routine returns the descriptors for a range of rows in the table. FTGDES(unit,colnum,rownum, > nelements,offset,status) FTGDESLL(unit,colnum,rownum, > nelementsll,offsetll,status) FFGDESS(unit,colnum,firstrow,nrows > nelements,offset, status) FFGDESSLL(unit,colnum,firstrow,nrows > nelementsll,offsetll, status) 12 Write the descriptor for a variable length column in a binary table. These subroutines can b e used in conjunction with FTGDES to enable 2 or more arrays to p oint to the same storage location to save storage space if the arrays are identical. FTPDES(unit,colnum,rownum,nelements,offset, > status) FTPDESLL(unit,colnum,rownum,nelementsll,offsetll, > status)


64

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

6.8

Row Selection and Calculator Routines

These routines all parse and evaluate an input string containing a user defined arithmetic expression. The first 3 routines select rows in a FITS table, based on whether the expression evaluates to true (not equal to zero) or false (zero). The other routines evaluate the expression and calculate a value for each row of the table. The allowed expression syntax is describ ed in the row filter section in the earlier `Extended File Name Syntax' chapter of this document. The expression may also b e written to a text file, and the name of the file, prep ended with a '@' character may b e supplied for the 'expr' parameter (e.g. '@filename.txt'). The expression in the file can b e arbitrarily complex and extend over multiple lines of the file. Lines that b egin with 2 slash characters ('//') will b e ignored and may b e used to add comments to the file. 1 Evaluate a b oolean expression over the indicated rows, returning an array of flags indicating which rows evaluated to TRUE/FALSE FTFROW(unit,expr,firstrow, nrows, > n_good_rows, row_status, status) 2 Find the first row which satisfies the input b oolean expression FTFFRW(unit, expr, > rownum, status) 3 Evaluate an expression on all rows of a table. If the input and output files are not the same, copy the TRUE rows to the output file. If the files are the same, delete the FALSE rows (preserve the TRUE rows). FTSROW(inunit, outunit, expr, > status) 4 Calculate an expression for the indicated rows of a table, returning the results, cast as datatyp e (TSHORT, TDOUBLE, etc), in array. If nulval==NULL, UNDEFs will b e zeroed out. For vector results, the numb er of elements returned may b e less than nelements if nelements is not an even multiple of the result dimension. Call FTTEXP to obtain the dimensions of the results. FTCROW(unit,datatype,expr,firstrow,nelements,nulval, > array,anynul,status) 5 Evaluate an expression and write the result either to a column (if the expression is a function of other columns in the table) or to a keyword (if the expression evaluates to a constant and is not a function of other columns in the table). In the former case, the parName parameter is the name of the column (which may or may not already exist) into which to write the results, and parInfo contains an optional TFORM keyword value if a new column is b eing created. If a TFORM value is not sp ecified then a default format will b e used, dep ending on the expression. If the expression evaluates to a constant, then the result will b e written to the keyword name given by the parName parameter, and the parInfo parameter may b e used


6.9. CELESTIAL COORDINATE SYSTEM SUBROUTINES

65

to supply an optional comment for the keyword. If the keyword does not already exist, then the name of the keyword must b e preceded with a '#' character, otherwise the result will b e written to a column with that name. FTCALC(inunit, expr, outunit, parName, parInfo, > status) 6 This calculator routine is similar to the previous routine, except that the expression is only evaluated over the sp ecified row ranges. nranges sp ecifies the numb er of row ranges, and firstrow and lastrow give the starting and ending row numb er of each range. FTCALC_RNG(inunit, expr, outunit, parName, parInfo, nranges, firstrow, lastrow, > status) 7 Evaluate the given expression and return information on the result. FTTEXP(unit, expr, > datatype, nelem, naxis, naxes, status)

6.9

Celestial Co ordinate System Subroutines

The FITS community has adopted a set of keyword conventions that define the transformations needed to convert b etween pixel locations in an image and the corresp onding celestial coordinates on the sky, or more generally, that define world coordinates that are to b e associated with any pixel location in an n-dimensional FITS array. CFITSIO is distributed with a couple of self-contained World Coordinate System (WCS) routines, however, these routines DO NOT supp ort all the latest WCS conventions, so it is STRONGLY RECOMMENDED that software develop ers use a more robust external WCS library. Several recommended libraries are: WCSLIB - supported by Mark Calabretta WCSTools - supported by Doug Mink AST library - developed by the U.K. Starlink project More information ab out the WCS keyword conventions and links to all of these WCS libraries can b e found on the FITS Supp ort Office web site at http://fits.gsfc.nasa.gov under the WCS link. The functions provided in these external WCS libraries will need access to the WCS information contained in the FITS file headers. One convenient way to pass this information to the external library is to use FITSIO to copy the header keywords into one long character string, and then pass this string to an interface routine in the external library that will extract the necessary WCS information (e.g., see the astFitsChan and astPutCards routines in the Starlink AST library). The followin approved as RECATED. and instead g FITSIO routines DO NOT supp ort the more recent WCS conventions that have b een part of the FITS standard. Consequently, the following routines ARE NOW DEPIt is STRONGLY RECOMMENDED that software develop ers not use these routines, use an external WCS library, as describ ed ab ove.


66

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES atibility with existing software. They supp ort -ARC, -NCP, -GLS, -MER, and -AIT (these ese routines are based on similar functions in units of degrees.

These routines are included mainly for backward comp the following standard map pro jections: -SIN, -TAN, are the legal values for the coordtyp e parameter). Th Classic AIPS. All the angular quantities are given in

1 Get the values of all the standard FITS celestial coordinate system keywords from the header of a FITS image (i.e., the primary array or an image extension). These values may then b e passed to the subroutines that p erform the coordinate transformations. If any or all of the WCS keywords are not present, then default values will b e returned. If the first coordinate axis is the declination-like coordinate, then this routine will swap them so that the longitudinal-like coordinate is returned as the first axis. If the file uses the newer 'CDj i' WCS transformation matrix keywords instead of old style 'CDELTn' and 'CROTA2' keywords, then this routine will calculate and return the values of the equivalent old-style keywords. Note that the conversion from the new-style keywords to the old-style values is sometimes only an approximation, so if the approximation is larger than an internally defined threshold level, then CFITSIO will still return the approximate WCS keyword values, but will also return with status = 506, to warn the calling program that approximations have b een made. It is then up to the calling program to decide whether the approximations are sufficiently accurate for the particular application, or whether more precise WCS transformations must b e p erformed using new-style WCS keywords directly. FTGICS(unit, > xrval,yrval,xrpix,yrpix,xinc,yinc,rot,coordtype,status) 2 Get the values of all the standard FITS celestial coordinate system keywords from the header of a FITS table where the X and Y (or RA and DEC coordinates are stored in 2 separate columns of the table. These values may then b e passed to the subroutines that p erform the coordinate transformations. FTGTCS(unit,xcol,ycol, > xrval,yrval,xrpix,yrpix,xinc,yinc,rot,coordtype,status) 3 Calculate the celestial coordinate corresp onding to the input X and Y pixel location in the image. FTWLDP(xpix,ypix,xrval,yrval,xrpix,yrpix,xinc,yinc,rot, coordtype, > xpos,ypos,status) 4 Calculate the X and Y pixel location corresp onding to the input celestial coordinate in the image. FTXYPX(xpos,ypos,xrval,yrval,xrpix,yrpix,xinc,yinc,rot, coordtype, > xpix,ypix,status)


6.10. FILE CHECKSUM SUBROUTINES

67

6.10

File Checksum Subroutines

The following routines either compute or validate the checksums for the CHDU. The DATASUM keyword is used to store the numerical value of the 32-bit, 1's complement checksum for the data unit alone. If there is no data unit then the value is set to zero. The numerical value is stored as an ASCI I string of digits, enclosed in quotes, b ecause the value may b e too large to represent as a 32-bit signed integer. The CHECKSUM keyword is used to store the ASCI I encoded COMPLEMENT of the checksum for the entire HDU. Storing the complement, rather than the actual checksum, forces the checksum for the whole HDU to equal zero. If the file has b een modified since the checksums were computed, then the HDU checksum will usually not equal zero. These checksum keyword conventions are based on a pap er by Rob Seaman published in the proceedings of the ADASS IV conference in Baltimore in Novemb er 1994 and a later revision in June 1995. 1 Compute and write the DATASUM and CHECKSUM keyword values for the CHDU into the current header. The DATASUM value is the 32-bit checksum for the data unit, expressed as a decimal integer enclosed in single quotes. The CHECKSUM keyword value is a 16-character string which is the ASCI I-encoded value for the complement of the checksum for the whole HDU. If these keywords already exist, their values will b e up dated only if necessary (i.e., if the file has b een modified since the original keyword values were computed). FTPCKS(unit, > status) 2 Up date the CHECKSUM keyword value in the CHDU, assuming that the DATASUM keyword exists and already has the correct value. This routine calculates the new checksum for the current header unit, adds it to the data unit checksum, encodes the value into an ASCI I string, and writes the string to the CHECKSUM keyword. FTUCKS(unit, > status) 3 Verify the CHDU by computing the checksums and comparing them with the keywords. The data unit is verified correctly if the computed checksum equals the value of the DATASUM keyword. The checksum for the entire HDU (header plus data unit) is correct if it equals zero. The output DATAOK and HDUOK parameters in this subroutine are integers which will have a value = 1 if the data or HDU is verified correctly, a value = 0 if the DATASUM or CHECKSUM keyword is not present, or value = -1 if the computed checksum is not correct. FTVCKS(unit, > dataok,hduok,status) 4 Compute and return the checksum values for the CHDU (as double precision variables) without creating or modifying the CHECKSUM and DATASUM keywords. This routine is used internally by FTVCKS, but may b e useful in other situations as well. FTGCKS(unit, > datasum,hdusum,status)


68

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

5 Encode a checksum value (stored in a double precision variable) into a 16-character string. If COMPLEMENT = .true. then the 32-bit sum value will b e complemented b efore encoding. FTESUM(sum,complement, > checksum) 6 Decode a 16 character checksum string into a double precision value. If COMPLEMENT = .true. then the 32-bit sum value will b e complemented after decoding. FTDSUM(checksum,complement, > sum)

6.11

Date and Time Utility Routines

The following routines help to construct or parse the FITS date/time strings. Starting in the year 2000, the FITS DATE keyword values (and the values of other `DATE-' keywords) must have the form 'YYYY-MM-DD' (date only) or 'YYYY-MM-DDThh:mm:ss.ddd...' (date and time) where the numb er of decimal places in the seconds value is optional. These times are in UTC. The older 'dd/mm/yy' date format may not b e used for dates after 01 January 2000. 1 Get the current system date. The returned year has 4 digits (1999, 2000, etc.) FTGSDT( > day, month, year, status ) 2 Get the current system date and time string ('YYYY-MM-DDThh:mm:ss'). The time will b e in UTC/GMT if available, as indicated by a returned timeref value = 0. If the returned value of timeref = 1 then this indicates that it was not p ossible to convert the local time to UTC, and thus the local time was returned. FTGSTM(> datestr, timeref, status) 3 Construct a date string from the input date values. If the year is b etween 1900 and 1998, inclusive, then the returned date string will have the old FITS format ('dd/mm/yy'), otherwise the date string will have the new FITS format ('YYYY-MM-DD'). Use FTTM2S instead to always return a date string using the new FITS format. FTDT2S( year, month, day, > datestr, status) 4 Construct a new-format date + time string ('YYYY-MM-DDThh:mm:ss.ddd...'). If the year, month, and day values all = 0 then only the time is encoded with format 'hh:mm:ss.ddd...'. The decimals parameter sp ecifies how many decimal places of fractional seconds to include in the string. If `decimals' is negative, then only the date will b e return ('YYYY-MM-DD'). FTTM2S( year, month, day, hour, minute, second, decimals, > datestr, status)


6.12. GENERAL UTILITY SUBROUTINES

69

5 Return the date as read from the input string, where the string may b e in either the old ('dd/mm/yy') or new ('YYYY-MM-DDThh:mm:ss' or 'YYYY-MM-DD') FITS format. FTS2DT(datestr, > year, month, day, status) 6 Return the date and time as read from the input string, where the string may b e in either the old or new FITS format. The returned hours, minutes, and seconds values will b e set to zero if the input string does not include the time ('dd/mm/yy' or 'YYYY-MM-DD') . Similarly, the returned year, month, and date values will b e set to zero if the date is not included in the input string ('hh:mm:ss.ddd...'). FTS2TM(datestr, > year, month, day, hour, minute, second, status)

6.12

General Utility Subroutines

The following utility subroutines may b e useful for certain applications: 1 Return the starting byte address of the CHDU and the next HDU. FTGHAD(iunit, > curaddr, nextaddr) 2 Convert a character string to upp ercase (op erates in place). FTUPCH(string) 3 Compare the input template string against the reference string to see if they match. The template string may contain wildcard characters: '*' will match any sequence of characters (including zero characters) and ' ?' will match any single character in the reference string. The '#' character will match any consecutive string of decimal digits (0 - 9). If CASESN = .true. then the match will b e case sensitive. The returned MATCH parameter will b e .true. if the 2 strings match, and EXACT will b e .true. if the match is exact (i.e., if no wildcard characters were used in the match). Both strings must b e 68 characters or less in length. FTCMPS(str_template, string, casesen, > match, exact) 4 Test that the keyword name contains only legal characters: A-Z,0-9, hyphen, and underscore. FTTKEY(keyword, > status) 5 Test that the keyword record contains only legal printable ASCI I characters FTTREC(card, > status)


70

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

6 Test whether the current header contains any NULL (ASCI I 0) characters. These characters are illegal in the header, but they will go undetected by most of the CFITSIO keyword header routines, b ecause the null is interpreted as the normal end-of-string terminator. This routine returns the p osition of the first null character in the header, or zero if there are no nulls. For example a returned value of 110 would indicate that the first NULL is located in the 30th character of the second keyword in the header (recall that each header record is 80 characters long). Note that this is one of the few FITSIO routines in which the returned value is not necessarily equal to the status value). FTNCHK(unit, > status) 7 Parse a header keyword record and return the name of the keyword and the length of the name. The keyword name normally occupies the first 8 characters of the record, except under the HIERARCH convention where the name can b e up to 70 characters in length.

FTGKNM(card, > keyname, keylength, staThe '\#' character will match any consecutive str of decimal digits (0 - 9). tus) 8 Parse a header keyword record. This subroutine parses the input header record to return the value (as a character string) and comment strings. If the keyword has no value (columns 9-10 not equal to '= '), then the value string is returned blank and the comment string is set equal to column 9 - 80 of the input string. FTPSVC(card, > value,comment,status) 9 Construct a sequence keyword name (ROOT + nnn). This subroutine app ends the sequence numb er to the root string to create a keyword name (e.g., 'NAXIS' + 2 = 'NAXIS2') FTKEYN(keyroot,seq_no, > keyword,status) 10 Construct a sequence keyword name (n + ROOT). This subroutine concatenates the sequence numb er to the front of the root string to create a keyword name (e.g., 1 + 'CTYP' = '1CTYP') FTNKEY(seq_no,keyroot, > keyword,status) 11 Determine the datatyp e of a keyword value string. This subroutine parses the keyword value string (usually columns 11-30 of the header record) to determine its datatyp e. FTDTYP(value, > dtype,status) 11 Return the class of input header record. The record is classified into one of the following categories (the class values are defined in fitsio.h). Note that this is one of the few FITSIO routines that does not return a status value.


6.12. GENERAL UTILITY SUBROUTINES Class Value TYP_STRUC_KEY 10 Keywords PLE, BITPIX, NAXIS, NAXISn, EXTEND, BLOCKED, UPS, PCOUNT, GCOUNT, END NSION, TFIELDS, TTYPEn, TBCOLn, TFORMn, THEAP, the first 4 COMMENT keywords in the primary array t define the FITS format. experimental keywords used in the compressed ge format ZIMAGE, ZCMPTYPE, ZNAMEn, ZVALn, LEn, ZBITPIX, ZNAXISn, ZSCALE, ZZERO, ZBLANK ALE, BZERO, TSCALn, TZEROn NK, TNULLn Mn INn, TLMAXn, TDMINn, TDMAXn, DATAMIN, DATAMAX IT, TUNITn SPn NAME, EXTVER, EXTLEVEL, HDUNAME, HDUVER, HDULEVEL CKSUM, DATASUM PEn, CUNITn, CRVALn, CRPIXn, CROTAn, CDELTn _is, PVj_ms, LONPOLEs, LATPOLEs YPn, TCTYns, TCUNIn, TCUNns, TCRVLn, TCRVns, TCRPX Pks, TCDn_k, TCn_ks, TPVn_m, TPn_ms, TCDLTn, TCROT YPn, jCTYns, jCUNIn, jCUNns, jCRVLn, jCRVns, iCRPX Pns, jiCDn, jiCDns, jPVn_m, jPn_ms, jCDLTn, jCROT j,m,n are integers, s is any letter) INOXs, EPOCH, MJD-OBSs, RADECSYS, RADESYSs MENT, HISTORY, (blank keyword) TINUE other keywords

71

TYP_CMPRS_KEY

20

T T T T T T T T T

Y Y Y Y Y Y Y Y Y

P P P P P P P P P

_ _ _ _ _ _ _ _ _

S N D R U D H C W

C U I A N I D K C

A L M N I S U S S

L L _ G T P I U _

_ _ K _ _ _ D M K

K K E K K K _ _ E

E E Y E E E K K Y

Y Y

30 40 50 Y 60 Y 70 Y 80 EY 90 EY 100 110

T T T T

Y Y Y Y

P P P P

_ _ _ _

R C C U

E O O S

F M N E

S M T R

Y _ _ _

S K K K

_ E E E

KEY 120 Y 130 Y 140 Y 150

S G X a t T i Z B B T T B T E C C C T T j i ( E C C a

I R T n h h m T S L D L U D X H T D C C C C i Q O O l

M O E d a e a I C A I M N I T E Y j T R T R , U M N l

n, n n, n

class = FTGKCL (char *card) 12 Parse the 'TFORM' binary table column format string. This subroutine parses the input TFORM character string and returns the integer datatyp e code, the rep eat count of the field, and, in the case of character string fields, the length of the unit string. The following datatyp e codes are returned (the negative of the value is returned if the column contains variable-length arrays): D b b l A s i r d a i y o S h n e o t t t g C o t a u a , e i I r e l b type X ,B cal, L I character, A t integer, I ger, J ,E le precision, D DATACODE value 1 11 14 16 21 41 42 82


72

CHAPTER 6. complex double complex 83 163

ADVANCED INTERFACE SUBROUTINES

FTBNFM(tform, > datacode,repeat,width,status) 13 Parse the 'TFORM' keyword value that defines the column format in an ASCI I table. This routine parses the input TFORM character string and returns the datatyp e code, the width of the column, and (if it is a floating p oint column) the numb er of decimal places to the right of the decimal p oint. The returned datatyp e codes are the same as for the binary table, listed ab ove, with the following additional rules: integer columns that are b etween 1 and 4 characters wide are defined to b e short integers (code = 21). Wider integer columns are defined to b e regular integers (code = 41). Similarly, Fixed decimal p oint columns (with TFORM = 'Fw.d') are defined to b e single precision reals (code = 42) if w is b etween 1 and 7 characters wide, inclusive. Wider 'F' columns will return a double precision data code (= 82). 'Ew.d' format columns will have datacode = 42, and 'Dw.d' format columns will have datacode = 82. FTASFM(tform, > datacode,width,decimals,status) 14 Calculate the starting column p ositions and total ASCI I table width based on the input array of ASCI I table TFORM values. The SPACE input parameter defines how many blank spaces to leave b etween each column (it is recommended to have one space b etween columns for b etter human readability). FTGABC(tfields,tform,space, > rowlen,tbcol,status) 15 Parse a template string and return a formatted 80-character string suitable for app ending to (or deleting from) a FITS header file. This subroutine is useful for parsing lines from an ASCI I template file and reformatting them into legal FITS header records. The formatted string may then b e passed to the FTPREC, FTMCRD, or FTDKEY subroutines to app end or modify a FITS header record. FTGTHD(template, > card,hdtype,status) The input TEMPLATE character string generally should contain 3 tokens: (1) the KEYNAME, (2) the VALUE, and (3) the COMMENT string. The TEMPLATE string must adhere to the following format: - The KEYNAME token must b egin in columns 1-8 and b e a maximum of 8 characters long. If the first 8 characters of the template line are blank then the remainder of the line is considered to b e a FITS comment (with a blank keyword name). A legal FITS keyword name may only contain the characters A-Z, 0-9, and '-' (minus sign) and underscore. This subroutine will automatically convert any lowercase characters to upp ercase in the output string. If KEYNAME = 'COMMENT' or 'HISTORY' then the remainder of the line is considered to b e a FITS COMMENT or HISTORY record, resp ectively.


6.12. GENERAL UTILITY SUBROUTINES

73

- The VALUE token must b e separated from the KEYNAME token by one or more spaces and/or an '=' character. The datatyp e of the VALUE token (numeric, logical, or character string) is automatically determined and the output CARD string is formatted accordingly. The value token may b e forced to b e interpreted as a string (e.g. if it is a string of numeric digits) by enclosing it in single quotes. - The COMMENT token is optional, but if present must b e separated from the VALUE token by at least one blank space. A leading '/' character may b e used to mark the b eginning of the comment field, otherwise the comment field b egins with the first non-blank character following the value token. - One exception to the ab ove rules is that if the first non-blank character in the template string is a minus sign ('-') followed by a single token, or a single token followed by an equal sign, then it is interpreted as the name of a keyword which is to b e deleted from the FITS header. - The second exception is that if the template string starts with a minus sign and is followed by 2 tokens then the second token is interpreted as the new name for the keyword sp ecified by first token. In this case the old keyword name (first token) is returned in characters 1-8 of the returned CARD string, and the new keyword name (the second token) is returned in characters 41-48 of the returned CARD string. These old and new names may then b e passed to the FTMNAM subroutine which will change the keyword name. The HDTYPE output parameter indicates how the returned CARD string should b e interpreted: hdtype ------2 interpretation ------------------------------------------------Modify the name of the keyword given in CARD(1:8) to the new name given in CARD(41:48) CARD(1:8) contains the name of a keyword to be deleted from the FITS header. a k t i p e h n pend the CARD string to the FITS header if the yword does not already exist, otherwise update e value/comment if the keyword is already present the header.

-1

0

1

simply append this keyword to the FITS header (CARD is either a HISTORY or COMMENT keyword). This is a FITS END record; it should not be written to the FITS header because FITSIO automatically appends the END record when the header is closed.

2

EXAMPLES: The following lines illustrate valid input template strings:


74 I R E l S S s # # N V V v T A A a VAL 7 This is L 34 L=-12.45E-03 l F This is a This L1 = 'Hello wo L2 '123.5' t l3 123+ / t he following t ATE he following t AME OBJECT a . T b i r h h e n 6 h o s l i i m

CHAPTER 6.

ADVANCED INTERFACE SUBROUTINES

VA VA va t D t N

integer keyword / This is a floating point keyword is is a floating point keyword in exponential notation olean keyword a comment keyword with a blank keyword name d' / this is a string keyword s is also a string keyword s is also a string keyword with the value '123+ ' plate line deletes the DATE keyword

emplate line modifies the NAME keyword to OBJECT

16 Parse the input string containing a list of rows or row ranges, and return integer arrays containing the first and last row in each range. For example, if rowlist = "3-5, 6, 8-9" then it will return numranges = 3, rangemin = 3, 6, 8 and rangemax = 5, 6, 9. At most, 'maxranges' numb er of ranges will b e returned. 'maxrows' is the maximum numb er of rows in the table; any rows or ranges larger than this will b e ignored. The rows must b e sp ecified in increasing order, and the ranges must not overlap. A minus sign may b e use to sp ecify all the rows to the upp er or lower b ound, so "50-" means all the rows from 50 to the end of the table, and "-" means all the rows in the table, from 1 - maxrows. FTRWRG(rowlist, maxrows, maxranges, > numranges, rangemin, rangemax, status)


Chapter 7

The CFITSIO Iterator Function
The fits iterate data function in CFITSIO provides a unique method of executing an arbitrary user-supplied `work' function that op erates on rows of data in FITS tables or on pixels in FITS images. Rather than explicitly reading and writing the FITS images or columns of data, one instead calls the CFITSIO iterator routine, passing to it the name of the user's work function that is to b e executed along with a list of all the table columns or image arrays that are to b e passed to the work function. The CFITSIO iterator function then does all the work of allocating memory for the arrays, reading the input data from the FITS file, passing them to the work function, and then writing any output data back to the FITS file after the work function exits. Because it is often more efficient to process only a subset of the total table rows at one time, the iterator function can determine the optimum amount of data to pass in each iteration and rep eatedly call the work function until the entire table b een processed. For many applications this single CFITSIO iterator function can effectively replace all the other CFITSIO routines for reading or writing data in FITS images or tables. Using the iterator has several imp ortant advantages over the traditional method of reading and writing FITS data files: · It cleanly separates the data I/O from the routine that op erates on the data. This leads to a more modular and `ob ject oriented' programming style. · It simplifies the application program by eliminating the need to allocate memory for the data arrays and eliminates most of the calls to the CFITSIO routines that explicitly read and write the data. · It ensures that the data are processed as efficiently as p ossible. This is esp ecially imp ortant when processing tabular data since the iterator function will calculate the most efficient numb er of rows in the table to b e passed at one time to the user's work function on each iteration. · Makes it p ossible for larger pro jects to develop a library of work functions that all have a uniform calling sequence and are all indep endent of the details of the FITS file format. There are basically 2 steps in using the CFITSIO iterator function. The first step is to design the work function itself which must have a prescrib ed set of input parameters. One of these parameters 75


76

CHAPTER 7.

THE CFITSIO ITERATOR FUNCTION

is a structure containing p ointers to the arrays of data; the work function can p erform any desired op erations on these arrays and does not need to worry ab out how the input data were read from the file or how the output data get written back to the file. The second step is to design the driver routine that op ens all the necessary FITS files and initializes the input parameters to the iterator function. The driver program calls the CFITSIO iterator function which then reads the data and passes it to the user's work function. Further details on using the iterator function can b e found in the companion CFITSIO User's Guide, and in the iter a.f, iter b.f and iter c.f example programs.


Chapter 8

Extended File Name Syntax
8.1 Overview

CFITSIO supp orts an extended syntax when sp ecifying the name of the data file to b e op ened or created that includes the following features: · CFITSIO can read IRAF format images which have header file names that end with the '.imh' extension, as well as reading and writing FITS files, This feature is implemented in CFITSIO by first converting the IRAF image into a temp orary FITS format file in memory, then op ening the FITS file. Any of the usual CFITSIO routines then may b e used to read the image header or data. Similarly, raw binary data arrays can b e read by converting them on the fly into virtual FITS images. · FITS files on the Internet can b e read (and sometimes written) using the FTP, HTTP, or ROOT protocols. · FITS files can b e pip ed b etween tasks on the stdin and stdout streams. · FITS files can b e read and written in shared memory. This can p otentially achieve much b etter data I/O p erformance compared to reading and writing the same FITS files on magnetic disk. · Compressed FITS files in gzip or Unix COMPRESS format can b e directly read. · Output FITS files can b e written directly in compressed gzip format, thus saving disk space. · FITS table columns can b e created, modified, or deleted 'on-the-fly' as the table is op ened by CFITSIO. This creates a virtual FITS file containing the modifications that is then op ened by the application program. · Table rows may b e selected, or filtered out, on the fly when the table is op ened by CFITSIO, based on an arbitrary user-sp ecified expression. Only rows for which the expression evaluates to 'TRUE' are retained in the copy of the table that is op ened by the application program. · Histogram images may b e created on the fly by binning the values in table columns, resulting in a virtual N-dimensional FITS image. The application program then only sees the FITS image (in the primary array) instead of the original FITS table. 77


78

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

The latter 3 features in particular add very p owerful data processing capabilities directly into CFITSIO, and hence into every task that uses CFITSIO to read or write FITS files. For example, these features transform a very simple program that just copies an input FITS file to a new output file (like the `fitscopy' program that is distributed with CFITSIO) into a multipurp ose FITS file processing tool. By app ending fairly simple qualifiers onto the name of the input FITS file, the user can p erform quite complex table editing op erations (e.g., create new columns, or filter out rows in a table) or create FITS images by binning or histogramming the values in table columns. In addition, these functions have b een coded using new state-of-the art algorithms that are, in some cases, 10 - 100 times faster than previous widely used implementations. Before describing the complete syntax for the extended FITS file names in the next section, here are a few examples of FITS file names that give a quick overview of the allowed syntax: · 'myfile.fits': the simplest case of a FITS file on disk in the current directory. · 'myfile.imh': op ens an IRAF format image file and converts it on the fly into a temp orary FITS format image in memory which can then b e read with any other CFITSIO routine. · rawfile.dat[i512,512]: op ens a raw binary data array (a 512 x 512 short integer array in this case) and converts it on the fly into a temp orary FITS format image in memory which can then b e read with any other CFITSIO routine. · myfile.fits.gz: if this is the name of a new output file, the '.gz' suffix will cause it to b e compressed in gzip format when it is written to disk. · 'myfile.fits.gz[events, 2]': op ens and uncompresses the gzipp ed file myfile.fits then moves to the extension which has the keywords EXTNAME = 'EVENTS' and EXTVER = 2. · '-': a dash (minus sign) signifies that the input file is to b e read from the stdin file stream, or that the output file is to b e written to the stdout stream. · 'ftp://legacy.gsfc.nasa.gov/test/vela.fits': FITS files in any ftp archive site on the Internet may b e directly op ened with read-only access. · 'http://legacy.gsfc.nasa.gov/software/test.fits': any valid URL to a FITS file on the Web may b e op ened with read-only access. · 'root://legacy.gsfc.nasa.gov/test/vela.fits': similar to ftp access except that it provides write as well as read access to the files across the network. This uses the root protocol develop ed at CERN. · 'shmem://h2[events]': op ens the FITS file in a shared memory segment and moves to the EVENTS extension. · 'mem://': creates a scratch output file in core computer memory. The resulting 'file' will disapp ear when the program exits, so this is mainly useful for testing purp oses when one does not want a p ermanent copy of the output file. · 'myfile.fits[3; Images(10)]': op ens a copy of the image contained in the 10th row of the 'Images' column in the binary table in the 3th extension of the FITS file. The application just sees this single image as the primary array.


8.1. OVERVIEW

79

· 'myfile.fits[1:512:2, 1:512:2]': op ens a section of the input image ranging from the 1st to the 512th pixel in X and Y, and selects every second pixel in b oth dimensions, resulting in a 256 x 256 pixel image in this case. · 'myfile.fits[EVENTS][col Rad = sqrt(X**2 + Y**2)]': creates and op ens a temp orary file on the fly (in memory or on disk) that is identical to myfile.fits except that it will contain a new column in the EVENTS extension called 'Rad' whose value is computed using the indicated expression which is a function of the values in the X and Y columns. · 'myfile.fits[EVENTS][PHA > 5]': creates and op ens a temp orary FITS files that is identical to 'myfile.fits' except that the EVENTS table will only contain the rows that have values of the PHA column greater than 5. In general, any arbitrary b oolean expression using a C or Fortran-like syntax, which may combine AND and OR op erators, may b e used to select rows from a table. · 'myfile.fits[EVENTS][bin (X,Y)=1,2048,4]': creates a temp orary FITS primary array image which is computed on the fly by binning (i.e, computing the 2-dimensional histogram) of the values in the X and Y columns of the EVENTS extension. In this case the X and Y coordinates range from 1 to 2048 and the image pixel size is 4 units in b oth dimensions, so the resulting image is 512 x 512 pixels in size. · The final example combines many of these feature into one complex expression (it is broken into several lines for clarity): 'ftp://legacy.gsfc.nasa.gov/data/sample.fits.gz[EVENTS] [col phacorr = pha * 1.1 - 0.3][phacorr >= 5.0 && phacorr <= 14.0] [bin (X,Y)=32]' In this case, CFITSIO (1) copies and uncompresses the FITS file from the ftp site on the legacy machine, (2) moves to the 'EVENTS' extension, (3) calculates a new column called 'phacorr', (4) selects the rows in the table that have phacorr in the range 5 to 14, and finally (5) bins the remaining rows on the X and Y column coordinates, using a pixel size = 32 to create a 2D image. All this processing is completely transparent to the application program, which simply sees the final 2-D image in the primary array of the op ened file. The full extended CFITSIO FITS file name can contain several different comp onents dep ending on the context. These comp onents are describ ed in the following sections: When creating a new file: filetype://BaseFilename(templateName) When opening an existing primary array or image HDU: filetype://BaseFilename(outName)[HDUlocation][ImageSection] When opening an existing table HDU: filetype://BaseFilename(outName)[HDUlocation][colFilter][rowFilter][binSpec]


80

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

The filetyp e, BaseFilename, outName, HDUlocation, and ImageSection comp onents, if present, must b e given in that order, but the colFilter, rowFilter, and binSp ec sp ecifiers may follow in any order. Regardless of the order, however, the colFilter sp ecifier, if present, will b e processed first by CFITSIO, followed by the rowFilter sp ecifier, and finally by the binSp ec sp ecifier.

8.2

Filetyp e

The typ e of file determines the medium on which the file is located (e.g., disk or network) and, hence, which internal device driver is used by CFITSIO to read and/or write the file. Currently supp orted typ es are file:// ftp:// - file on local magnetic disk (default) - a readonly file accessed with the anonymous FTP protocol. It also supports ftp://username:password@hostname/... for accessing password-protected ftp sites. // - a readonly file accessed with the HTTP protocol. It supports username:password just like the ftp driver. Proxy HTTP servers are supported using the http_proxy environment variable (see following note). // - uses the CERN root protocol for writing as well as reading files over the network. :// - opens or creates a file which persists in the computer's shared memory. / - opens a temporary file in core memory. The file disappears when the program exits so this is mainly useful for test purposes when a permanent output file is not desired.

http:

root: shmem mem:/

If the filetyp e is not sp ecified, then typ e file:// is assumed. The double slashes '//' are optional and may b e omitted in most cases.

8.2.1

Notes about HTTP proxy servers

A proxy HTTP server may b e used by defining the address (URL) and p ort numb er of the proxy server with the http proxy environment variable. For example setenv http_proxy http://heasarc.gsfc.nasa.gov:3128 will cause CFITSIO to use p ort 3128 on the heasarc proxy server whenever reading a FITS file with HTTP.

8.2.2

Notes about the root filetype

The original rootd server can b e obtained from: ftp://root.cern.ch/root/rootd.tar.gz but, for it to work correctly with CFITSIO one has to use a modified version which supp orts a command


8.2. FILETYPE

81

to return the length of the file. This modified version is available in rootd sub directory in the CFITSIO ftp area at ftp://legacy.gsfc.nasa.gov/software/fitsio/c/root/rootd.tar.gz. This small server is started either by inetd when a client requests a connection to a rootd server or by hand (i.e. from the command line). The rootd server works with the ROOT TNetFile class. It allows remote access to ROOT database files in either read or write mode. By default TNetFile assumes p ort 432 (which requires rootd to b e started as root). To run rootd via inetd add the following line to /etc/services: rootd 432/tcp

and to /etc/inetd.conf, add the following line: rootd stream tcp nowait root /user/rdm/root/bin/rootd rootd -i Force inetd to reread its conf file with "kill -HUP ¡pid inetd¿". You can also start rootd by hand running directly under your private account (no root system privileges needed). For example to start rootd listening on p ort 5151 just typ e: rootd -p 5151 Notice: no & is needed. Rootd will go into background by itself. Roo td arguments: i p port# d level

s s l 0 1 2 3

ay pe ev = = = =

s we were started by inetd cifies a different port to listen on el of debug info written to syslog no debug (default) minimum medium maximum

Rootd can also b e configured for anonymous usage (like anonymous ftp). To setup rootd to accept anonymous logins do the following (while b eing logged in as root): - Add the following line to /etc/passwd: rootd:*:71:72:Anonymous rootd:/var/spool/rootd:/bin/false where you may modify the uid, gid (71, 72) and the home directory to suite your system. - Add the following line to /etc/group: rootd:*:72:rootd


82

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

where the gid must match the gid in /etc/passwd. - Create the directories: mkdir /var/spool/rootd mkdir /var/spool/rootd/tmp chmod 777 /var/spool/rootd/tmp Where /var/spool/rootd must match the rootd home directory as specified in the rootd /etc/passwd entry. - To make writeable directories for anonymous do, for example: mkdir /var/spool/rootd/pub chown rootd:rootd /var/spool/rootd/pub That's all. Several additional remarks: you can login to an anonymous server either with the names "anonymous" or "rootd". The password should b e of typ e user@host.do.main. Only the @ is enforced for the time b eing. In anonymous mode the top of the file tree is set to the rootd home directory, therefore only files b elow the home directory can b e accessed. Anonymous mode only works when the server is started via inetd.

8.2.3

Notes about the shmem filetype:

Shared memory files are currently supp orted on most Unix platforms, where the shared memory segments are managed by the op erating system kernel and `live' indep endently of processes. They are not deleted (by default) when the process which created them terminates, although they will disapp ear if the system is reb ooted. Applications can create shared memory files in CFITSIO by calling: fit_create_file(&fitsfileptr, "shmem://h2", &status); where the root `file' names are currently restricted to b e 'h0', 'h1', 'h2', 'h3', etc., up to a maximum numb er defined by the the value of SHARED MAXSEG (equal to 16 by default). This is a prototyp e implementation of the shared memory interface and a more robust interface, which will have fewer restrictions on the numb er of files and on their names, may b e develop ed in the future. When op ening an already existing FITS file in shared memory one calls the usual CFITSIO routine: fits_open_file(&fitsfileptr, "shmem://h7", mode, &status) The file mode can b e READWRITE or READONLY just as with disk files. More than one process can op erate on READONLY mode files at the same time. CFITSIO supp orts prop er file locking (b oth in READONLY and READWRITE modes), so calls to fits op en file may b e locked out until another other process closes the file.


8.3. BASE FILENAME

83

When an application is finished accessing a FITS file in a shared memory segment, it may close it (and the file will remain in the system) with fits close file, or delete it with fits delete file. Physical deletion is p ostp oned until the last process calls ffclos/ffdelt. fits delete file tries to obtain a READWRITE lock on the file to b e deleted, thus it can b e blocked if the ob ject was not op ened in READWRITE mode. A shared memory management utility program called `smem', is included with the tribution. It can b e built by typing `make smem'; then typ e `smem -h' to get a list of Executing smem without any options causes it to list all the shared memory segm residing in the system and managed by the shared memory driver. To get a list of memory ob jects, run the system utility program `ip cs [-a]'. CFITSIO disvalid options. ents currently all the shared

8.3

Base Filename

The base filename is the name of the file optionally including the director/sub directory path, and in the case of `ftp', `http', and `root' filetyp es, the machine identifier. Examples: m ! / f y d d i f a a t i t t s l a a . e . / g . f m s f i y f i t f c ts s ile.fits .nasa.gov/ftp/sampledata/myfile.fits.gz

When creating a new output file on magnetic disk (of typ e file://) if the base filename b egins with an exclamation p oint (!) then any existing file with that same basename will b e deleted prior to creating the new FITS file. Otherwise if the file to b e created already exists, then CFITSIO will return an error and will not overwrite the existing file. Note that the exclamation p oint, ' !', is a sp ecial UNIX character, so if it is used on the command line rather than entered at a task prompt, it must b e preceded by a backslash to force the UNIX shell to pass it verbatim to the application program. If the output disk file name gzip compression algorithm by the file. Note that this b efore it is compressed and ends with the suffix '.gz', then CFITSIO will compress the file using the b efore writing it to disk. This can reduce the amount of disk space used feature requires that the uncompressed file b e constructed in memory written to disk, so it can fail if there is insufficient available memory.

An input FITS file may b e compressed with the gzip or Unix compress algorithms, in which case CFITSIO will uncompress the file on the fly into a temp orary file (in memory or on disk). Compressed files may only b e op ened with read-only p ermission. When sp ecifying the name of a compressed FITS file it is not necessary to app end the file suffix (e.g., `.gz' or `.Z'). If CFITSIO cannot find the input file name without the suffix, then it will automatically search for a compressed file with the same root name. In the case of reading ftp and http typ e files, CFITSIO generally looks for a compressed version of the file first, b efore trying to op en the uncompressed file. By default, CFITSIO copies (and uncompressed if necessary) the ftp or http FITS file into memory on the local machine b efore op ening it. This will fail if the local machine does not have enough memory to hold the whole FITS file, so in this case, the output filename sp ecifier (see the next section) can b e used to further control how CFITSIO reads ftp and http files.


84

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

If the input file is an IRAF image file (*.imh file) then CFITSIO will automatically convert it on the fly into a virtual FITS image b efore it is op ened by the application program. IRAF images can only b e op ened with READONLY file access. Similarly, if the input file is a raw binary data array, then CFITSIO will convert it on the fly a virtual FITS image with the basic set of required header keywords b efore it is op ened by application program (with READONLY access). In this case the data typ e and dimensions of image must b e sp ecified in square brackets following the filename (e.g. rawfile.dat[ib512,512]). first character (case insensitive) defines the datatyp e of the array: b i u j r or f d 8 6 6 2 2 4 b b b b b b i i i i i i t t t t t t u s u s f f n i n i l l s g s g o o i n i n a a g e g e t t n d n d i i ed in ed in ng ng b t i t p p y e n e o o t g t g i i e e e e n n into the the Th e

1 1 3 3 6

r ger r t t

An optional second character sp ecifies the byte order of the array values: b or B indicates big endian (as in FITS files and the native format of SUN UNIX workstations and Mac PCs) and l or L indicates little endian (native format of DEC OSF workstations and IBM PCs). If this character is omitted then the array is assumed to have the native byte order of the local machine. These datatyp e characters are then followed by a series of one or more integer values separated by commas which define the size of each dimension of the raw array. Arrays with up to 5 dimensions are currently supp orted. Finally, a byte offset to the p osition of the first pixel in the data file may b e sp ecified by separating it with a ':' from the last dimension value. If omitted, it is assumed that the offset = 0. This parameter may b e used to skip over any header information in the file that precedes the binary data. Further examples: raw.dat[b10000] 1-d raw.dat[rb400,400,12] 3-d img.fits[ib512,512:2880] rea aF i i d I m m s T ensional ensional the 512 S file, s 1 f x k 0000 loat 512 ippi pixel byte array ing point big-endian array short integer array in ng over the 2880 byte header

One sp ecial case of input file is where the filename = `-' (a dash or minus sign) or 'stdin' or 'stdout', which signifies that the input file is to b e read from the stdin stream, or written to the stdout stream if a new output file is b eing created. In the case of reading from stdin, CFITSIO first copies the whole stream into a temp orary FITS file (in memory or on disk), and subsequent reading of the FITS file occurs in this copy. When writing to stdout, CFITSIO first constructs the whole file in memory (since random access is required), then flushes it out to the stdout stream when the file is closed. In addition, if the output filename = '-.gz' or 'stdout.gz' then it will b e gzip compressed b efore b eing written to stdout. This ability to read and write on the stdin and stdout steams allows FITS files to b e pip ed b etween tasks in memory rather than having to create temp orary intermediate FITS files on disk. For example if task1 creates an output FITS file, and task2 reads an input FITS file, the FITS file may b e pip ed b etween the 2 tasks by sp ecifying


8.4. OUTPUT FILE NAME WHEN OPENING AN EXISTING FILE task1 - | task2 -

85

where the vertical bar is the Unix piping symb ol. This assumes that the 2 tasks read the name of the FITS file off of the command line.

8.4

Output File Name when Op ening an Existing File

An optional output filename may b e sp ecified in parentheses immediately following the base file name to b e op ened. This is mainly useful in those cases where CFITSIO creates a temp orary copy of the input FITS file b efore it is op ened and passed to the application program. This happ ens by default when op ening a network FTP or HTTP-typ e file, when reading a compressed FITS file on a local disk, when reading from the stdin stream, or when a column filter, row filter, or binning sp ecifier is included as part of the input file sp ecification. By default this temp orary file is created in memory. If there is not enough memory to create the file copy, then CFITSIO will exit with an error. In these cases one can force a p ermanent file to b e created on disk, instead of a temp orary file in memory, by supplying the name in parentheses immediately following the base file name. The output filename can include the ' !' clobb er flag. Thus, if the input filename to CFITSIO is: file1.fits.gz(file2.fits) then CFITSIO will uncompress `file1.fits.gz' into the local disk file `file2.fits' b efore op ening it. CFITSIO does not automatically delete the output file, so it will still exist after the application program exits. In some cases, several different temp orary FITS files will b e created in sequence, for instance, if one op ens a remote file using FTP, then filters rows in a binary table extension, then create an image by binning a pair of columns. In this case, the remote file will b e copied to a temp orary local file, then a second temp orary file will b e created containing the filtered rows of the table, and finally a third temp orary file containing the binned image will b e created. In cases like this where multiple files are created, the outfile sp ecifier will b e interpreted the name of the final file as describ ed b elow, in descending priority: · as the name of the final image file if an image within a single binary table cell is op ened or if an image is created by binning a table column. · as the name of the file containing the filtered table if a column filter and/or a row filter are sp ecified. · as the name of the local copy of the remote FTP or HTTP file. · as the name of the uncompressed version of the FITS file, if a compressed FITS file on local disk has b een op ened. · otherwise, the output filename is ignored. The output file sp ecifier is useful when reading FTP or HTTP-typ e FITS files since it can b e used to create a local disk copy of the file that can b e reused in the future. If the output file name = `*' then a local file with the same name as the network file will b e created. Note that CFITSIO will b ehave differently dep ending on whether the remote file is compressed or not as shown by the following examples:


86

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

· `ftp://remote.machine/tmp/myfile.fits.gz(*)' - the remote compressed file is copied to the local compressed file `myfile.fits.gz', which is then uncompressed in local memory b efore b eing op ened and passed to the application program. · `ftp://remote.machine/tmp/myfile.fits.gz(myfile.fits)' - the remote compressed file is copied and uncompressed into the local file `myfile.fits'. This example requires less local memory than the previous example since the file is uncompressed on disk instead of in memory. · `ftp://remote.machine/tmp/myfile.fits(myfile.fits.gz)' - this will usually produce an error since CFITSIO itself cannot compress files. The exact b ehavior of CFITSIO in the latter case dep ends on the typ e of ftp server running on the remote machine and how it is configured. In some cases, if the file `myfile.fits.gz' exists on the remote machine, then the server will copy it to the local machine. In other cases the ftp server will automatically create and transmit a compressed version of the file if only the uncompressed version exists. This can get rather confusing, so users should use a certain amount of caution when using the output file sp ecifier with FTP or HTTP file typ es, to make sure they get the b ehavior that they exp ect.

8.5

Template File Name when Creating a New File

When a new FITS file is created with a call to fits create file, the name of a template file may b e supplied in parentheses immediately following the name of the new file to b e created. This template is used to define the structure of one or more HDUs in the new file. The template file may b e another FITS file, in which case the newly created file will have exactly the same keywords in each HDU as in the template FITS file, but all the data units will b e filled with zeros. The template file may also b e an ASCI I text file, where each line (in general) describ es one FITS keyword record. The format of the ASCI I template file is describ ed b elow.

8.6

Image Tile-Compression Sp ecification

When sp ecifying the name of the output FITS file to b e created, the user can indicate that images should b e written in tile-compressed format (see section 5.5, "Primary Array or IMAGE Extension I/O Routines") by enclosing the compression parameters in square brackets following the root disk file name. Here are some examples of the syntax for sp ecifying tile-compressed output images: myfile.fit[compress] - use Rice algorithm and default tile size

myfile.fit[compress GZIP] - use the specified compression algorithm; myfile.fit[compress Rice] only the first letter of the algorithm myfile.fit[compress PLIO] name is required. myfile.fit[compress Rice 100,100] - use 100 x 100 pixel tile size myfile.fit[compress Rice 100,100;2] - as above, and use noisebits = 2


8.7. HDU LOCATION SPECIFICATION

87

8.7

HDU Lo cation Sp ecification

The optional HDU location sp ecifier defines which HDU (Header-Data Unit, also known as an `extension') within the FITS file to initially op en. It must immediately follow the base file name (or the output file name if present). If it is not sp ecified then the first HDU (the primary array) is op ened. The HDU location sp ecifier is required if the colFilter, rowFilter, or binSp ec sp ecifiers are present, b ecause the primary array is not a valid HDU for these op erations. The HDU may b e sp ecified either by absolute p osition numb er, starting with 0 for the primary array, or by reference to the HDU name, and optionally, the version numb er and the HDU typ e of the desired extension. The location of an image within a single cell of a binary table may also b e sp ecified, as describ ed b elow. The absolute p osition of the extension is sp ecified either by enclosed the numb er in square brackets (e.g., `[1]' = the first extension following the primary array) or by preceded the numb er with a plus sign (`+1'). To sp ecify the HDU by name, give the name of the desired HDU (the value of the EXTNAME or HDUNAME keyword) and optionally the extension version numb er (value of the EXTVER keyword) and the extension typ e (value of the XTENSION keyword: IMAGE, ASCI I or TABLE, or BINTABLE), separated by commas and all enclosed in square brackets. If the value of EXTVER and XTENSION are not sp ecified, then the first extension with the correct value of EXTNAME is op ened. The extension name and typ e are not case sensitive, and the extension typ e may b e abbreviated to a single letter (e.g., I = IMAGE extension or primary array, A or T = ASCI I table extension, and B = binary table BINTABLE extension). If the HDU location sp ecifier is equal to `[PRIMARY]' or `[P]', then the primary array (the first HDU) will b e op ened. FITS images are most commonly stored in the primary array or an image extension, but images can also b e stored as a vector in a single cell of a binary table (i.e. each row of the vector column contains a different image). Such an image can b e op ened with CFITSIO by sp ecifying the desired column name and the row numb er after the binary table HDU sp ecifier as shown in the following examples. The column name is separated from the HDU sp ecifier by a semicolon and the row numb er is enclosed in parentheses. In this case CFITSIO copies the image from the table cell into a temp orary primary array b efore it is op ened. The application program then just sees the image in the primary array, without any extensions. The particular row to b e op ened may b e sp ecified either by giving an absolute integer row numb er (starting with 1 for the first row), or by sp ecifying a b oolean expression that evaluates to TRUE for the desired row. The first row that satisfies the expression will b e used. The row selection expression has the same syntax as describ ed in the Row Filter Sp ecifier section, b elow. Examples: d HDU following the primary array ve, but using the FTOOLS-style notation he extension that has EXTNAME = 'EVENTS' me as above, but also requires EXTVER = 2 me, but also requires XTENSION = 'BINTABLE' opens the image in row 17 of the 'images' column in the 3rd extension of the file. myfile.fits[3; images(exposure > 100)] - as above, but opens the image in the first row that has an 'exposure' column value m m m m m m y y y y y y f f f f f f i i i i i i l l l l l l e e e e e e . . . . . . f f f f f f i i i i i i t t t t t t s s s s s s [ + [ [ [ [ 3 3 E E e 3 ] - open the 3 - same as ab VENTS] - open VENTS, 2] - s vents,2,b] - s ; images(17)] r o t a a -


88

CHAPTER 8. greater than 100.

EXTENDED FILE NAME SYNTAX

8.8

Image Section

A virtual file containing a rectangular subsection of an image can b e extracted and op ened by sp ecifying the range of pixels (start:end) along each axis to b e extracted from the original image. One can also sp ecify an optional pixel increment (start:end:step) for each axis of the input image. A pixel step = 1 will b e assumed if it is not sp ecified. If the start pixel is larger then the end pixel, then the image will b e flipp ed (producing a mirror image) along that dimension. An asterisk, '*', may b e used to sp ecify the entire range of an axis, and '-*' will flip the entire axis. The input image can b e in the primary array, in an image extension, or contained in a vector cell of a binary table. In the later 2 cases the extension name or numb er must b e sp ecified b efore the image section sp ecifier. Examples: myfile.fits[1:512:2, 2:51 consisting of the even numb primary array myfile.fits[*, 5 in t The the 1 h i s 2 e m t :256] input age wil arting 2:2] - op the odd n ered rows of the fi n b i s e u ( l n m 2 e a 256x256 pixel image bered columns (1st axis) and nd axis) of the image in the . of all hrough axis s ending t 5 i p h 1 n i e 2 c x columns . e el.

open a image, l be fl pixel i

image consisting ut only rows 256 t pped along the 2nd greater than the

myfile.fits[*:2, 512:256:2] - same as above but keeping only every other row and column in the input image. myfile.fits[-*, *] - copy the entire image, flipping it along the first axis. myfile.fits[3][1:256,1:256] - opens a subsection of the image that is in the 3rd extension of the file. myfile.fits[4; ima of the first 1 image resides column in the g 0 i t es(12)][1:10,1:10] - open an ima pixels in both dimensions. The n the 12th row of the 'images' v able in the 4th extension of the ge consisting original ector file.

When CFITSIO op ens an image section it first creates a temp orary file containing the image section plus a copy of any other HDUs in the file. This temp orary file is then op ened by the application program, so it is not p ossible to write to or modify the input file when sp ecifying an image section. Note that CFITSIO automatically up dates the world coordinate system keywords in the header of the image section, if they exist, so that the coordinate associated with each pixel in the image section will b e computed correctly.


8.9. IMAGE TRANSFORM FILTERS

89

8.9

Image Transform Filters

CFITSIO can apply a user-sp ecified mathematical function to the value of every pixel in a FITS image, thus creating a new virtual image in computer memory that is then op ened and read by the application program. The original FITS image is not modified by this process. The image transformation sp ecifier is app ended to the input FITS file name and is brackets. It b egins with the letters 'PIX' to distinguish it from other typ es of FI are recognized by CFITSIO. The image transforming function may use any of op erators listed in the following 'Row Filtering Sp ecification' section of this examples of image transform filters are: [ [ [ [ p p p p i i i i x x x x X s X X * qr + >0 2.0] t(X)] #ZEROPT ? log10(X) : -99.] m t a i t e u a d f h l ltiply each pixel b ke the square root d the value of the the pixel value is an 0, compute the b se set the pixel = y 2.0 of each pixel ZEROPT keyword greater ase 10 log, -99. The expression 1) the original the p osition of keyword values enclosed in square TS file filters that the mathematical document. Some

Use the letter 'X' in the expression to represent the current pixel value in the image. is evaluated indep endently for each pixel in the image and may b e a function of pixel value, 2) the value of other pixels in the image at a given relative offset from the pixel that is b eing evaluated, and 3) the value of any header keywords. Header are represented by the name of the keyword preceded by the '#' sign.

To access the the value of adjacent pixels in the image, sp ecify the (1-D) offset from the current pixel in curly brackets. For example [pix (x{-1} + x + x{+1}) / 3]

will replace each pixel value with the running mean of the values of that pixel and it's 2 neighb oring pixels. Note that in this notation the image is treated as a 1-D array, where each row of the image (or higher dimensional cub e) is app ended one after another in one long array of pixels. It is p ossible to refer to pixels in the rows ab ove or b elow the current pixel by using the value of the NAXIS1 header keyword. For example [pix (x{-#NAXIS1} + x + x{#NAXIS1}) / 3] will compute the mean of each image pixel and the pixels immediately ab ove and b elow it in the adjacent rows of the image. The following more complex example creates a smoothed virtual image where each pixel is a 3 x 3 b oxcar average of the input image pixels: [pix (X + X{-1} + X{+1} + X{-#NAXIS1} + X{-#NAXIS1 - 1} + X{-#NAXIS1 + 1} + X{#NAXIS1} + X{#NAXIS1 - 1} + X{#NAXIS1 + 1}) / 9.]


90

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

If the pixel offset extends b eyond the first or last pixel in the image, the function will evaluate to undefined, or NULL. For complex or commonly used image filtering op erations, one can write the expression into an external text file and then imp ort it into the filter using the syntax '[pix @filename.txt]'. The mathematical expression can extend over multiple lines of text in the file. Any lines in the external text file that b egin with 2 slash characters ('//') will b e ignored and may b e used to add comments into the file. By default, the datatyp e of the resulting image will b e the same as the original image, but one may force a different datatyp e by app ended a code letter to the 'pix' keyword: p p p p p i i i i i x x x x x b i j r d 8 6 2 2 4 b b b b b i i i i i t t t t t b i i f f y n n l l t t t o o e e e a a im ger im ger im t im t im a a a a a g g g g g e e e e e w w w w w i i i i i t t t t t h h h h h B B B B B I I I I I T T T T T P P P P P I I I I I X X X X X = 8 = 16 = 32 = -32 = -64

1 3 3 6

Also by default, any other HDUs in the input file will b e copied without change to the output virtual FITS file, but one may discard the other HDUs by adding the numb er '1' to the 'pix' keyword (and following any optional datatyp e code letter). For example: myfile.fits[3][pixr1 sqrt(X)] will create a virtual FITS file containing only a primary array image with 32-bit floating p oint pixels that have a value equal to the square root of the pixels in the image that is in the 3rd extension of the 'myfile.fits' file.

8.10

Column and Keyword Filtering Sp ecification

The optional column/keyword filtering sp ecifier is used to modify the column structure and/or the header keywords in the HDU that was selected with the previous HDU location sp ecifier. This filtering sp ecifier must b e enclosed in square brackets and can b e distinguished from a general row filter sp ecifier (describ ed b elow) by the fact that it b egins with the string 'col ' and is not immediately followed by an equals sign. The original file is not changed by this filtering op eration, and instead the modifications are made on a copy of the input FITS file (usually in memory), which also contains a copy of all the other HDUs in the file. This temp orary file is passed to the application program and will p ersist only until the file is closed or until the program exits, unless the outfile sp ecifier (see ab ove) is also supplied. The column/keyword filter can b e used to p erform the following op erations. More than one op eration may b e sp ecified by separating them with semi-colons. · Copy only a sp ecified list of columns columns to the filtered input file. The list of column name should b e separated by semi-colons. Wild card characters may b e used in the column names to match multiple columns. If the expression contains b oth a list of columns to b e


8.10. COLUMN AND KEYWORD FILTERING SPECIFICATION

91

included and columns to b e deleted, then all the columns in the original table except the explicitly deleted columns will app ear in the filtered table (i.e., there is no need to explicitly list the columns to b e included if any columns are b eing deleted). · Delete a column or keyword by listing the name preceded by a minus sign or an exclamation mark (!), e.g., '-TIME' will delete the TIME column if it exists, otherwise the TIME keyword. An error is returned if neither a column nor keyword with this name exists. Note that the exclamation p oint, ' !', is a sp ecial UNIX character, so if it is used on the command line rather than entered at a task prompt, it must b e preceded by a backslash to force the UNIX shell to ignore it. · Rename an existing column or keyword with the syntax 'NewName == OldName'. An error is returned if neither a column nor keyword with this name exists. · App end a new column or keyword to the table. To create a column, give the new name, optionally followed by the datatyp e in parentheses, followed by a single equals sign and an expression to b e used to compute the value (e.g., 'newcol(1J) = 0' will create a new 32-bit integer column called 'newcol' filled with zeros). The datatyp e is sp ecified using the same syntax that is allowed for the value of the FITS TFORMn keyword (e.g., 'I', 'J', 'E', 'D', etc. for binary tables, and 'I8', F12.3', 'E20.12', etc. for ASCII tables). If the datatyp e is not sp ecified then an appropriate datatyp e will b e chosen dep ending on the form of the expression (may b e a character string, logical, bit, long integer, or double column). An appropriate vector count (in the case of binary tables) will also b e added if not explicitly sp ecified. When creating a new keyword, the keyword name must b e preceded by a p ound sign '#', and the expression must evaluate to a scalar (i.e., cannot have a column name in the expression). The comment string for the keyword may b e sp ecified in parentheses immediately following the keyword name (instead of supplying a datatyp e as in the case of creating a new column). If the keyword name ends with a p ound sign '#', then cfitsio will substitute the numb er of the most recently referenced column for the # character . This is esp ecially useful when writing a column-related keyword like TUNITn for a newly created column, as shown in the following examples. · Recompute (overwrite) the values in an existing column or keyword by giving the name followed by an equals sign and an arithmetic expression. The expression that is used when app ending or recomputing columns or keywords can b e arbitrarily complex and may b e a function of other header keyword values and other columns (in the same row). The full syntax and available functions for the expression are describ ed b elow in the row filter sp ecification section. If the expression contains b oth a list of columns to b e included and columns to b e deleted, then all the columns in the original table except the explicitly deleted columns will app ear in the filtered table. If no columns to b e deleted are sp ecified, then only the columns that are explicitly listed will b e included in the filtered output table. To include all the columns, add the '*' wildcard sp ecifier at the end of the list, as shown in the examples. For complex or commonly used op erations, one can also place the op erations into an external text file and imp ort it into the column filter using the syntax '[col @filename.txt]'. The op erations can


92

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

extend over multiple lines of the file, but multiple op erations must still b e separated by semicolons. Any lines in the external text file that b egin with 2 slash characters ('//') will b e ignored and may b e used to add comments into the file. Examples: [col Time;rate] - only the Time and rate columns will appear in the filtered input file. - include the Time column and any other columns whose name ends with 'raw'. - deletes the TIME column and renames the status column to 'Good'

[col Time;*raw]

[col -TIME; Good == STATUS]

[col PI=PHA * 1.1 + 0.2; #TUNIT#(column units) = 'counts';*] - creates new PI column from PHA values and also writes the TUNITn keyword for the new column. The final '*' expression means preserve all the columns in the input table in the virtual output table; without the '*' the output table would only contain the single 'PI' column. [col rate = rate/exposure; TUNIT#(&) = 'counts/s';*] - recomputes the rate column by dividing it by the EXPOSURE keyword value. This also modifies the value of the TUNITn keyword for this column. The use of the '&' character for the keyword comment string means preserve the existing comment string for that keyword. The final '*' preserves all the columns in the input table in the virtual output table.

8.11

Row Filtering Sp ecification

When entering the name of a FITS table that is to b e op ened by a program, an optional row filter may b e sp ecified to select a subset of the rows in the table. A temp orary new FITS file is created on the fly which contains only those rows for which the row filter expression evaluates to true. (The primary array and any other extensions in the input file are also copied to the temp orary file). The original FITS file is closed and the new virtual file is op ened by the application program. The row filter expression is enclosed in square brackets following the file name and extension name (e.g., 'file.fits[events][GRADE==50]' selects only those rows where the GRADE column value equals 50).


8.11. ROW FILTERING SPECIFICATION

93

When dealing with tables where each row has an associated time and/or 2D spatial p osition, the row filter expression can also b e used to select rows based on the times in a Good Time Intervals (GTI) extension, or on spatial p osition as given in a SAO-style region file.

8.11.1

General Syntax

The row filtering expression can b e an arbitrarily complex series of op erations p erformed on constants, keyword values, and column data taken from the sp ecified FITS TABLE extension. The expression must evaluate to a b oolean value for each row of the table, where a value of FALSE means that the row will b e excluded. For complex or commonly used filters, one can place the expression into a into the row filter using the syntax '[@filename.txt]'. The expression can and extend over multiple lines of the file. Any lines in the external text file characters ('//') will b e ignored and may b e used to add comments into th text file and imp ort it b e arbitrarily complex that b egin with 2 slash e file.

Keyword and column data are referenced by name. Any string of characters not surrounded by quotes (ie, a constant string) or followed by an op en parentheses (ie, a function name) will b e initially interpreted as a column name and its contents for the current row inserted into the expression. If no such column exists, a keyword of that name will b e searched for and its value used, if found. To force the name to b e interpreted as a keyword (in case there is b oth a column and keyword with the same name), precede the keyword name with a single p ound sign, '#', as in '#NAXIS2'. Due to the generalities of FITS column and keyword names, if the column or keyword name contains a space or a character which might app ear as an arithmetic term then enclose the name in '$' characters as in $MAX PHA$ or #$MAX-PHA$. Names are case insensitive. To access a table entry in a row other than the current one, follow the column's name with a row offset within curly braces. For example, 'PHA{-3}' will evaluate to the value of column PHA, 3 rows ab ove the row currently b eing processed. One cannot sp ecify an absolute row numb er, only a relative offset. Rows that fall outside the table will b e treated as undefined, or NULLs. Boolean op erators can b e used in the expression in either their Fortran or C forms. The following b oolean op erators are available: " " " " " e l g o n q e r r e u s e " g al" s than" ater than" ation" . . . . .n e l g o o q t t r t . . . . . . . . . . E L G O N Q T T R O . . . . T == < > || .! " " " " " n l g a a o e r n p t s e d p equal" s than/equal" ater than/equal" " rox. equal(1e-7) . . . . " ne le ge an ~ . . . d. . . . . N L G A E E E N . . . D. ! < > & = = =< = => &

Note that the exclamation p oint, ' !', is a sp ecial UNIX character, so if it is used on the command line rather than entered at a task prompt, it must b e preceded by a backslash to force the UNIX shell to ignore it. The expression may radians, not degrees. (function names are such as divide by zer also include arithmetic op erators and functions. Trigonometric functions use The following arithmetic op erators and functions can b e used in the expression case insensitive). A null value will b e returned in case of illegal op erations o, sqrt(negative) log(negative), log10(negative), arccos(.gt. 1), arcsin(.gt. 1).


94 " " " " " " " " " " " " " " " " " " " a m n a s a a h h r e n m r m c i a s d u e b i r r y y o x a o a i u f n u d l g s n c c p p u p t d n n m g b

CHAPTER 8.

EXTENDED FILE NAME SYNTAX / * c t a a s r c s l r r m s

ition" + "subtraction" tiplication" * "division" ation" "exponentiation" olute value" abs(x) "cosine" e" sin(x) "tangent" cosine" arccos(x) "arc sine" tangent" arctan(x) "arc tangent" erbolic cos" cosh(x) "hyperbolic sin" erbolic tan" tanh(x) "round to nearest int" nd down to int" floor(x) "round up to int" onential" exp(x) "square root" ural log" log(x) "common log" ulus" x%y "random # [0.0,1.0)" dom Gaussian" randomn() "random Poisson" imum" min(x,y) "maximum" ulative sum" accum(x) "sequential difference" then-else" b?x:y ular separation" angsep(ra1,dec1,ra2,de2) (all in string" strmid(s,p,n) "string search"

* o a r r i o e q o a a a e

s n c c n u i r g n n x q

( ( s t h n l t 1 d d ( d

^ x) x) in an (x d( (x (x 0( om om x, if

( 2 ) x ) ) x ( p y f

x) (y,x) )

) ) (x) ) (x)

degrees) strstr(s,r)

Three different random numb er functions are provided: random(), with no arguments, produces a uniform random deviate b etween 0 and 1; randomn(), also with no arguments, produces a normal (Gaussian) random deviate with zero mean and unit standard deviation; randomp(x) produces a Poisson random deviate whose exp ected numb er of counts is X. X may b e any p ositive real numb er of exp ected counts, including fractional values, but the return value is an integer. When the random functions are used in a vector expression, by default the same random value will b e used when evaluating each element of the vector. If different random numb ers are desired, then the name of a vector column should b e supplied as the single argument to the random function (e.g., "flux + 0.1 * random(flux)", where "flux' is the name of a vector column). This will create a vector of random numb ers that will b e used in sequence when evaluating each element of the vector expression. An alternate syntax for the min and max functions has only a single argument which should b e a vector value (see b elow). The result will b e the minimum/maximum element contained within the vector. The accum(x) function forms the cumulative sum of x, element by element. Vector columns are supp orted simply by p erforming the summation process through all the values. Null values are treated as 0. The seqdiff(x) function forms the sequential difference of x, element by element. The first value of seqdiff is the first value of x. A single null value in x causes a pair of nulls in the output. The seqdiff and accum functions are functional inverses, i.e., seqdiff(accum(x)) == x as long as no null values are present. The angsep function computes the angular separation in degrees b etween 2 celestial p ositions, where the first 2 parameters give the RA-like and Dec-like coordinates (in decimal degrees) of the first p osition, and the 3rd and 4th parameters give the coordinates of the second p osition. The substring function strmid(S,P,N) extracts a substring from S, starting at string p osition P,


8.11. ROW FILTERING SPECIFICATION

95

with a substring length N. The first character p osition in S is lab eled as 1. If P is 0, or refers to a p osition b eyond the end of S, then the extracted substring will b e NULL. S, P, and N may b e functions of other columns. The string search function strstr(S,R) searches for the first occurrence of the substring R in S. The result is an integer, indicating the character p osition of the first match (where 1 is the first character p osition of S). If no match is found, then strstr() returns a NULL value. The following typ e casting op erators are available, where the enclosing parentheses are required and taken from the C language usage. Also, the integer to real casts values to double precision: "real to integer" "integer to real" (int) x (float) i (INT) x (FLOAT) i

In addition, several constants are built in for use in numerical expressions: #pi #deg #null 3.1415... #pi/180 undefined value #e #row #snull 2.7182... current row number undefined string

A string constant must b e enclosed in quotes as in 'Crab'. The "null" constants are useful for conditionally setting table values to a NULL, or undefined, value (eg., "col1==-99 ? #NULL : col1"). There is also a function for testing if two values are close to each other, i.e., if they are "near" each other to within a user sp ecified tolerance. The arguments, value 1 and value 2 can b e integer or real and represent the two values who's proximity is b eing tested to b e within the sp ecified tolerance, also an integer or real: near(value_1, value_2, tolerance) When a NULL, or undefined, value is encountered in the FITS table, the expression will evaluate to NULL unless the undefined value is not actually required for evaluation, e.g. "TRUE .or. NULL" evaluates to TRUE. The following two functions allow some NULL detection and handling: "a null value?" "define a value for null" ISNULL(x) DEFNULL(x,y)

The former returns a b oolean value of TRUE if the argument x is NULL. The later "defines" a value to b e substituted for NULL values; it returns the value of x if x is not NULL, otherwise it returns the value of y.

8.11.2

Bit Masks

Bit masks can b e used to select out rows from bit columns (TFORMn = #X) in FITS files. To represent the mask, binary, octal, and hex formats are allowed:


96 binary: octal: hex:

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

b0110xx1010000101xxxx0001 o720x1 -> (b111010000xxx001) h0FxD -> (b00001111xxxx1101)

In all the representations, an x or X is allowed in the mask as a wild card. Note that the x represents a different numb er of wild card bits in each representation. All representations are case insensitive. To construct the b oolean expression using the mask as the b oolean equal op erator describ ed ab ove on a bit table column. For example, if you had a 7 bit column named flags in a FITS table and wanted all rows having the bit pattern 0010011, the selection expression would b e: flags == b0010011 or flags .eq. b10011 It is also p ossible to test if a range of bits is less than, less than equal, greater than and greater than equal to a particular b oolean value: flags <= bxxx010xx flags .gt. bxxx100xx flags .le. b1xxxxxxx Notice the use of the x bit value to limit the range of bits b eing compared. It is not necessary to sp ecify the leading (most significant) zero (0) bits in the mask, as shown in the second expression ab ove. Bit wise AND, OR and NOT op erations are also p ossible on two or more bit fields using the '&'(AND), '|'(OR), and the ' !'(NOT) op erators. All of these op erators result in a bit field which can then b e used with the equal op erator. For example: (!flags) == b1101100 (flags & b1000001) == bx000001 Bit fields can b e app ended as well using the '+' op erator. Strings can b e concatenated this way, too.

8.11.3

Vector Columns

Vector columns can also b e used in building the expression. No sp ecial syntax is required if one wants to op erate on all elements of the vector. Simply use the column name as for a scalar column. Vector columns can b e freely intermixed with scalar columns or constants in virtually all expressions. The result will b e of the same dimension as the vector. Two vectors in an expression, though, need to have the same numb er of elements and have the same dimensions. The only places a vector column cannot b e used (for now, anyway) are the SAO region functions and the NEAR b oolean function.


8.11. ROW FILTERING SPECIFICATION

97

Arithmetic and logical op erations are all p erformed on an element by element basis. Comparing two vector columns, eg "COL1 == COL2", thus results in another vector of b oolean values indicating which elements of the two vectors are equal. Eight functions are available that op erate on a vector and return a scalar result: " " " " m a s # ini ver umm of mum" age" ation" values" M A S N I V U E N E M L ( R ( E V A V M ) GE(V) ) (V) " " " " m m s # aximum" edian" tandard deviation" of non-null values" M M S N A E T V X D D A ( I D L V A E I ) N(V) V(V) D(V)

where V represents the name of a vector column or a manually constructed vector using curly brackets as describ ed b elow. The first 6 of these functions ignore any null values in the vector when computing the result. The STDDEV() function computes the sample standard deviation, i.e. it is prop ortional to 1/SQRT(N-1) instead of 1/SQRT(N), where N is NVALID(V). The SUM function literally sums all the elements in x, returning a scalar value. If x is a b oolean vector, SUM returns the numb er of TRUE elements. The NELEM function returns the numb er of elements in vector x whereas NVALID return the numb er of non-null elements in the vector. (NELEM also op erates on bit and string columns, returning their column widths.) As an example, to test whether all elements of two vectors satisfy a given logical comparison, one can use the expression SUM( COL1 > COL2 ) == NELEM( COL1 ) which will return TRUE if all elements of COL1 are greater than their corresp onding elements in COL2. To sp ecify a single element of a vector, give the column name followed by a comma-separated list of coordinates enclosed in square brackets. For example, if a vector column named PHAS exists in the table as a one dimensional, 256 comp onent list of numb ers from which you wanted to select the 57th comp onent for use in the expression, then PHAS[57] would do the trick. Higher dimensional arrays of data may app ear in a column. But in order to interpret them, the TDIMn keyword must app ear in the header. Assuming that a (4,4,4,4) array is packed into each row of a column named ARRAY4D, the (1,2,3,4) comp onent element of each row is accessed by ARRAY4D[1,2,3,4]. Arrays up to dimension 5 are currently supp orted. Each vector index can itself b e an expression, although it must evaluate to an integer value within the b ounds of the vector. Vector columns which contain spaces or arithmetic op erators must have their names enclosed in "$" characters as with $ARRAY-4D$[1,2,3,4]. A more C-like syntax for sp ecifying vector indices is also available. The element used in the preceding example alternatively could b e sp ecified with the syntax ARRAY4D[4][3][2][1]. Note the reverse order of indices (as in C), as well as the fact that the values are still ones-based (as in Fortran ­ adopted to avoid ambiguity for 1D vectors). With this syntax, one does not need to sp ecify all of the indices. To extract a 3D slice of this 4D array, use ARRAY4D[4]. Variable-length vector columns are not supp orted. Vectors can b e manually constructed within the expression using a comma-separated list of elements surrounded by curly braces ('{}'). For example, '{1,3,6,1}' is a 4-element vector containing the


98

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

values 1, 3, 6, and 1. The vector can contain only b oolean, integer, and real values (or expressions). The elements will b e promoted to the highest datatyp e present. Any elements which are themselves vectors, will b e expanded out with each of its elements b ecoming an element in the constructed vector.

8.11.4

Good Time Interval Filtering

A common filtering method involves selecting rows which have a time value which lies within what is called a Good Time Interval or GTI. The time intervals are defined in a separate FITS table extension which contains 2 columns giving the start and stop time of each good interval. The filtering op eration accepts only those rows of the input table which have an associated time which falls within one of the time intervals defined in the GTI extension. A high level function, gtifilter(a,b,c,d), is available which evaluates each row of the input table and returns TRUE or FALSE dep ending whether the row is inside or outside the good time interval. The syntax is gtifilter( [ "gtifile" [, expr [, "STARTCOL", "STOPCOL" ] ] ] ) where each "[]" demarks optional parameters. Note that the quotes around the gtifile and START/STOP column are required. Either single or double quotes may b e used. In cases where this expression is entered on the Unix command line, enclose the entire expression in double quotes, and then use single quotes within the expression to enclose the 'gtifile' and other terms. It is also usually p ossible to do the reverse, and enclose the whole expression in single quotes and then use double quotes within the expression. The gtifile, if sp ecified, can b e blank ("") which will mean to use the first extension with the name "*GTI*" in the current file, a plain extension sp ecifier (eg, "+2", "[2]", or "[STDGTI]") which will b e used to select an extension in the current file, or a regular filename with or without an extension sp ecifier which in the latter case will mean to use the first extension with an extension name "*GTI*". Expr can b e any arithmetic expression, including simply the time column name. A vector time expression will produce a vector b oolean result. STARTCOL and STOPCOL are the names of the START/STOP columns in the GTI extension. If one of them is sp ecified, they b oth must b e. In its simplest form, no parameters need to b e provided ­ default values will b e used. The expression "gtifilter()" is equivalent to gtifilter( "", TIME, "*START*", "*STOP*" ) This will search the current file for a GTI extension, filter the TIME column in the current table, using START/STOP times taken from columns in the GTI extension with names containing the strings "START" and "STOP". The wildcards ('*') allow slight variations in naming conventions such as "TSTART" or "STARTTIME". The same default values apply for unsp ecified parameters when the first one or two parameters are sp ecified. The function automatically searches for TIMEZERO/I/F keywords in the current and GTI extensions, applying a relative time offset, if necessary.


8.11. ROW FILTERING SPECIFICATION

99

8.11.5

Spatial Region Filtering

Another common filtering method selects rows based on whether the spatial p osition associated with each row is located within a given 2-dimensional region. The syntax for this high-level filter is regfilter( "regfilename" [ , Xexpr, Yexpr [ , "wcs cols" ] ] ) where each "[]" demarks optional parameters. The region file name is required and must b e enclosed in quotes. The remaining parameters are optional. The region file is an ASCI I text file which contains a list of one or more geometric shap es (circle, ellipse, b ox, etc.) which defines a region on the celestial sphere or an area within a particular 2D image. The region file is typically generated using an image display program such as fv/POW (distribute by the HEASARC), or ds9 (distributed by the Smithsonian Astrophysical Observatory). Users should refer to the documentation provided with these programs for more details on the syntax used in the region files. In its simplest form, (e.g., regfilter("region.reg") ) the coordinates in the default 'X' and 'Y' columns will b e used to determine if each row is inside or outside the area sp ecified in the region file. Alternate p osition column names, or expressions, may b e entered if needed, as in regfilter("region.reg", XPOS, YPOS) Region filtering can b e applied most unambiguously if the p ositions in the region file and in the table to b e filtered are b oth give in terms of absolute celestial coordinate units. In this case the locations and sizes of the geometric shap es in the region file are sp ecified in angular units on the sky (e.g., p ositions given in R.A. and Dec. and sizes in arcseconds or arcminutes). Similarly, each row of the filtered table will have a celestial coordinate associated with it. This association is usually implemented using a set of so-called 'World Coordinate System' (or WCS) FITS keywords that define the coordinate transformation that must b e applied to the values in the 'X' and 'Y' columns to calculate the coordinate. Alternatively, one can p erform spatial filtering using unitless 'pixel' coordinates for the regions and row p ositions. In this case the user must b e careful to ensure that the p ositions in the 2 files are self-consistent. A typical problem is that the region file may b e generated using a binned image, but the unbinned coordinates are given in the event table. The ROSAT events files, for example, have X and Y pixel coordinates that range from 1 - 15360. These coordinates are typically binned by a factor of 32 to produce a 480x480 pixel image. If one then uses a region file generated from this image (in image pixel units) to filter the ROSAT events file, then the X and Y column values must b e converted to corresp onding pixel units as in: regfilter("rosat.reg", X/32.+.5, Y/32.+.5) Note that this binning conversion is not necessary if the region file is sp ecified using celestial coordinate units instead of pixel units b ecause CFITSIO is then able to directly compare the celestial coordinate of each row in the table with the celestial coordinates in the region file without having to know anything ab out how the image may have b een binned.


100 The last "wcs cols" parameter shou of the 2 columns (space or comma supplied, the filter will scan the X each expression, those columns will

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

ld rarely b e needed. If supplied, this string contains the names separated) which have the associated WCS keywords. If not and Y expressions for column names. If only one is found in b e used, otherwise an error will b e returned.

These region shap es are supp orted (names are case insensitive): P L P R B D C A E E S o i o e o i i n l l e i n l c x a r n l l c nt e ygon tangle m c u i i t o l l p p o n e u s t r d s e an ( ( ( ( ( ( ( ( ( nulus ( ( X X X X X X X X X X X 1 1 1 1 c c c c c c c , , , , , , , , , , , Y Y Y Y Y Y Y Y Y Y Y 1 1 1 1 c c c c c c c ) , , , , , , , , , , <- One pixe ) <- One pixe , ... ) <- Rest are ,A) | bounda Hght, A ) V within Hght, A ) o , R A ut A in ma l square region l wide region interiors with ries considered the region

X X X W W R R R R A

2, 2, 2, dt dt ) in x, in mi

Y2 Y2 Y2 h, h, ,R Ry x, n,

) ) y, Routx, Routy, Ain, Aout ) x)

where (Xc,Yc) is the coordinate of the shap e's center; (X#,Y#) are the coordinates of the shap e's edges; Rxxx are the shap es' various Radii or semi-ma jor/minor axes; and Axxx are the angles of rotation (or b ounding angles for Sector) in degrees. For rotated shap es, the rotation angle can b e left off, indicating no rotation. Common alternate names for the regions can also b e used: rotb ox = b ox; rotrectangle = rectangle; (rot)rhombus = (rot)diamond; and pie = sector. When a shap e's name is preceded by a minus sign, '-', the defined region is instead the area *outside* its b oundary (ie, the region is inverted). All the shap es within a single region file are OR'd together to create the region, and the order is significant. The overall way of looking at region files is that if the first region is an excluded region then a dummy included region of the whole detector is inserted in the front. Then each region sp ecification as it is processed overrides any selections inside of that region sp ecified by previous regions. Another way of thinking ab out this is that if a previous excluded region is completely inside of a subsequent included region the excluded region is ignored. The p ositional coordinates may b e given either in pixel units, decimal degrees or hh:mm:ss.s, dd:mm:ss.s units. The shap e sizes may b e given in pixels, degrees, arcminutes, or arcseconds. Look at examples of region file produced by fv/POW or ds9 for further details of the region file format. There are three functions that are primarily for use with SAO region files and the FSAOI task, but they can b e used directly. They return a b oolean true or false dep ending on whether a two dimensional p oint is in the region or not: "point in a circular region" circle(xcntr,ycntr,radius,Xcolumn,Ycolumn) "point in an elliptical region" ellipse(xcntr,ycntr,xhlf_wdth,yhlf_wdth,rotation,Xcolumn,Ycolumn) "point in a rectangular region" box(xcntr,ycntr,xfll_wdth,yfll_wdth,rotation,Xcolumn,Ycolumn)


8.11. ROW FILTERING SPECIFICATION

101

where (xc (xh (xf (ra (ro

r,ycntr) are the (x,y) position of the center o _wdth,yhlf_wdth) are the (x,y) half widths of t _wdth,yfll_wdth) are the (x,y) full widths of t us) is half the diameter of the circle tion) is the angle(degrees) that the region is respect to (xcntr,ycntr) (Xcoord,Ycoord) are the (x,y) coordinates to test, u names NOTE: each parameter can itself be an expression, no column name or constant.

n l l d t

t f l i a

f the region he region he region rotated with sually column t merely a

8.11.6

Example Row Filters
-E t t p b x h h r i t a e e n ract all binary stars brighter n fifth magnitude (note that initial space is necessary to vent it from being treated as a ning specification)

[ binary && mag <= 5.0]

[#row >= 125 && #row <= 175] [IMAGE[4,5] .gt. 100]

- Extract row numbers 125 through 175 - Extract all rows that have the (4,5) component of the IMAGE column greater than 100 x b e r x p P i e t s s e t e E m v ract all rows h olute value of s than a half tabulated in d r c C e e a t , s l a t w e v h h g i e e r ng the sine of theta re the angles ees

[abs(sin(theta * #deg)) < 0.5] - E a l a [SUM( SPEC > 3*BACKGRND )>=1] -E s S t l

ct all rows containing a rum, held in vector column with at least one value 3 greater than the background held in a keyword, BACKGRND

[VCOL=={1,4,2}]

- Extract all rows whose vector column VCOL contains the 3-elements 1, 4, and 2. - Extract rows using the expression contained within the text file rowFilter.txt

[@rowFilter.txt]


102 [g e c S c t x o T o i t l A l f e u R u i n m T m l s n / n

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

ter()] - Search the current file for a GTI ion, filter the TIME in the current table, using STOP times taken from s in the GTI extension -E ( w i x a i n a s e o tract rows which have a coordinate s given in the X and Y columns) thin the spatial region specified the pow.reg region file. me as above, except that the and Ys columns will be used to termine the coordinate of each w in the table.

[regfilter("pow.reg")]

[regfilter("pow.reg", Xs, Ys)] - S X d r

8.12

Binning or Histogramming Sp ecification

The optional binning sp ecifier is enclosed in square brackets and can b e distinguished from a general row filter sp ecification by the fact that it b egins with the keyword 'bin' not immediately followed by an equals sign. When binning is sp ecified, a temp orary N-dimensional FITS primary array is created by computing the histogram of the values in the sp ecified columns of a FITS table extension. After the histogram is computed the input FITS file containing the table is then closed and the temp orary FITS primary array is op ened and passed to the application program. Thus, the application program never sees the original FITS table and only sees the image in the new temp orary file (which has no additional extensions). Obviously, the application program must b e exp ecting to op en a FITS image and not a FITS table in this case. The data typ e of the FITS histogram image may b e sp ecified by app ending 'b' (for 8-bit byte), 'i' (for 16-bit integers), 'j' (for 32-bit integer), 'r' (for 32-bit floating p oints), or 'd' (for 64-bit double precision floating p oint) to the 'bin' keyword (e.g. '[binr X]' creates a real floating p oint image). If the datatyp e is not explicitly sp ecified then a 32-bit integer image will b e created by default, unless the weighting option is also sp ecified in which case the image will have a 32-bit floating p oint data typ e by default. The histogram image may have from 1 to 4 dimensions (axes), dep ending on the numb er of columns that are sp ecified. The general form of the binning sp ecification is: [bin{bijrd} Xcol=min:max:binsize, Ycol= ..., Zcol=..., Tcol=...; weight]

in which up to 4 columns, each corresp onding to an axis of the image, are listed. The column names are case insensitive, and the column numb er may b e given instead of the name, preceded by a p ound sign (e.g., [bin #4=1:512]). If the column name is not sp ecified, then CFITSIO will first try to use the 'preferred column' as sp ecified by the CPREF keyword if it exists (e.g., 'CPREF = 'DETX,DETY'), otherwise column names 'X', 'Y', 'Z', and 'T' will b e assumed for each of the 4 axes, resp ectively. In cases where the column name could b e confused with an arithmetic expression, enclose the column name in parentheses to force the name to b e interpreted literally.


8.12.

BINNING OR HISTOGRAMMING SPECIFICATION

103

Each column name may b e followed by an equals sign and then the lower and upp er range of the histogram, and the size of the histogram bins, separated by colons. Spaces are allowed b efore and after the equals sign but not within the 'min:max:binsize' string. The min, max and binsize values may b e integer or floating p oint numb ers, or they may b e the names of keywords in the header of the table. If the latter, then the value of that keyword is substituted into the expression. Default values for the min, max and binsize quantities will b e used if not explicitly given in the binning expression as shown in these examples: [ [ [ [ [ [ [ [ [ b b b b b b b b b i i i i i i i i i n n n n n n n n n x x x x x x x 4 ] = = = = = = ] ] : 1 1 1 : 2 5 : : : 5 ] 1 : 5 ] 1 2:2] 2] 12] 2] u u u u u u u d d s s s s s s s e e e e e e e e e f f default default default default default default default ault 2-D ault 2-D m m b m m m m i i i a i a i i i m m n x n x n n n a a imum v imum v size imum v imum v imum a imum, ge, bi ge alue alue a a n m n lue and bin size lue and bin size d maximum values aximum and bin size size = 4 in both axes

CFITSIO will use the value of the TLMINn, TLMAXn, and TDBINn keywords, if they exist, for the default min, max, and binsize, resp ectively. If they do not exist then CFITSIO will use the actual minimum and maximum values in the column for the histogram min and max values. The default binsize will b e set to 1, or (max - min) / 10., whichever is smaller, so that the histogram will have at least 10 bins along each axis. A shortcut notation is allowed if all the columns/axes have the same binning sp ecification. In this case all the column names may b e listed within parentheses, followed by the (single) binning sp ecification, as in: [bin (X,Y)=1:512:2] [bin (X,Y) = 5] The optional weighting factor is the last item in the binning sp ecifier and, if present, is separated from the list of columns by a semi-colon. As the histogram is accumulated, this weight is used to incremented the value of the appropriated bin in the histogram. If the weighting factor is not sp ecified, then the default weight = 1 is assumed. The weighting factor may b e a constant integer or floating p oint numb er, or the name of a keyword containing the weighting value. Or the weighting factor may b e the name of a table column in which case the value in that column, on a row by row basis, will b e used. In some cases, the column or keyword may give the reciprocal of the actual weight value that is needed. In this case, precede the weight keyword or column name by a slash '/' to tell CFITSIO to use the reciprocal of the value when constructing the histogram. For complex or commonly used histograms, one can also place its description into a text file and imp ort it into the binning sp ecification using the syntax '[bin @filename.txt]'. The file's contents can extend over multiple lines, although it must still conform to the no-spaces rule for the min:max:binsize syntax and each axis sp ecification must still b e comma-separated. Any lines in


104

CHAPTER 8.

EXTENDED FILE NAME SYNTAX

the external text file that b egin with 2 slash characters ('//') will b e ignored and may b e used to add comments into the file. Examples: [bini detx, dety] -2 o d r f e a n n r a D, D fa ng 16-bit integer histogram ETX and DETY columns, using ult values for the histogram e and binsize f s v k DETX ize = 16 alues eyword

[bin (detx, dety)=16; /exposure] - 2 a i a v [bin time=TSTART:TSTOP:0.1]

D, 32-bit real histogram o d DETY columns with a bin both axes. The histogram e divided by the EXPOSURE lue.

- 1-D lightcurve, range determined by the TSTART and TSTOP keywords, with 0.1 unit size bins. f n 0 D image the PHA d 1000 b 00. to 8 using default binning column for the X axis, ins in the range 100. for the Y axis.

[bin pha, time=8000.:8100.:0.1] - 2 o a 8 [bin @binFilter.txt]

- Use the contents of the text file binFilter.txt for the binning specifications.


Chapter 9

Template Files
When a new FITS file is created with a call to fits create file, the name of a template file may b e supplied in parentheses immediately following the name of the new file to b e created. This template is used to define the structure of one or more HDUs in the new file. The template file may b e another FITS file, in which case the newly created file will have exactly the same keywords in each HDU as in the template FITS file, but all the data units will b e filled with zeros. The template file may also b e an ASCI I text file, where each line (in general) describ es one FITS keyword record. The format of the ASCI I template file is describ ed in the following sections.

9.1

Detailed Template Line Format

The format of each ASCI I template line closely follows the format of a FITS keyword record: KEYWORD = KEYVALUE / COMMENT except that free format may b e used (e.g., the equals sign may app ear at any p osition in the line) and TAB characters are allowed and are treated the same as space characters. The KEYVALUE and COMMENT fields are optional. The equals sign character is also optional, but it is recommended that it b e included for clarity. Any template line that b egins with the p ound '#' character is ignored by the template parser and may b e use to insert comments into the template file itself. The KEYWORD name field is limited to 8 characters in length and only the letters A-Z, digits 0-9, and the hyphen and underscore characters may b e used, without any emb edded spaces. Lowercase letters in the template keyword name will b e converted to upp ercase. Leading spaces in the template line preceding the keyword name are generally ignored, except if the first 8 characters of a template line are all blank, then the entire line is treated as a FITS comment keyword (with a blank keyword name) and is copied verbatim into the FITS header. The KEYVALUE field may have any allowed FITS data typ e: character string, logical, integer, real, complex integer, or complex real. The character string values need not b e enclosed in single quote characters unless they are necessary to distinguish the string from a different data typ e (e.g. 2.0 is a real but '2.0' is a string). The keyword has an undefined (null) value if the template record only contains blanks following the "=" or b etween the "=" and the "/" comment field delimiter. 105


106

CHAPTER 9. TEMPLATE FILES

String keyword values longer than 68 characters (the maximum length that will fit in a single FITS keyword record) are p ermitted using the CFITSIO long string convention. They can either b e sp ecified as a single long line in the template, or by using multiple lines where the continuing lines contain the 'CONTINUE' keyword, as in this example: LONGKEY = 'This is a long string value that is contin&' CONTINUE 'ued over 2 records' / comment field goes here The format of template lines with CONTINUE keyword is very strict: 3 spaces must follow CONTINUE and the rest of the line is copied verbatim to the FITS file. The start of the optional COMMENT field must b e preceded by "/", which is used to separate it from the keyword value field. Exceptions are if the KEYWORD name field contains COMMENT, HISTORY, CONTINUE, or if the first 8 characters of the template line are blanks. More than one Header-Data Unit (HDU) may b e defined in the template file. The start of an HDU definition is denoted with a SIMPLE or XTENSION template line: 1) SIMPLE b egins a Primary HDU definition. SIMPLE may only appear as the first keyword in the template file. If the template file b egins with XTENSION instead of SIMPLE, then a default empty Primary HDU is created, and the template is then assumed to define the keywords starting with the first extension following the Primary HDU. 2) XTENSION marks the b eginning of a new extension HDU definition. The previous HDU will b e closed at this p oint and processing of the next extension b egins.

9.2

Auto-indexing of Keywords
, it is said to b e 'auto-indexed'. Each "#" value, which gets reset = 1 at the start of GROUP definition). The FIRST indexed 'incrementor'; each subsequent occurrence incremented. This b ehavior can b e rather the TTYPE keyword is the incrementor in

If a template keyword name ends with a "#" character character will b e replaced by the current integer index each new HDU in the file (or 7 in the sp ecial case of a keyword in each template HDU definition is used as the of this SAME keyword will cause the index value to b e subtle, as illustrated in the following examples in which b oth cases: T T T T T F T F Y O Y O P R P R E M E M # # # # = = = = T 1 R 1 IME D ATE E

will create TTYPE1, TFORM1, TTYPE2, and TFORM2 keywords. But if the template looks like, T T T T T T F F Y Y O O P P R R E E M M # # # # = = = = T R 1 1 IME ATE D E


9.3. TEMPLATE PARSER DIRECTIVES

107

this results in a FITS files with TTYPE1, TTYPE2, TFORM2, and TFORM2, which is probably not what was intended!

9.3

Template Parser Directives

In addition to the template lines which define individual keywords, the template parser recognizes 3 sp ecial directives which are each preceded by the backslash character: \include, \group, and \end. Th e read end and 'include' directive must b e followed by a filename. It forces the parser to temp orarily stop ing the current template file and b egin reading the include file. Once the parser reaches the of the include file it continues parsing the current template file. Include files can b e nested, HDU definitions can span multiple template files.

The start of a GROUP definition is denoted with the 'group' directive, and the end of a GROUP definition is denoted with the 'end' directive. Each GROUP contains 0 or more memb er blocks (HDUs or GROUPs). Memb er blocks of typ e GROUP can contain their own memb er blocks. The GROUP definition itself occupies one FITS file HDU of sp ecial typ e (GROUP HDU), so if a template sp ecifies 1 group with 1 memb er HDU like: \ g x # \ group rpdescr = 'demo' tension bintable this bintable has 0 cols, 0 rows end

then the parser creates a FITS file with 3 HDUs : 1) dummy PHDU 2) GROUP HDU (has 1 member, which is bintable in HDU number 3) 3) bintable (member of GROUP in HDU number 2) Technically sp eaking, the GROUP HDU is a BINTABLE with 6 columns. Applications can define additional columns in a GROUP HDU using TFORMn and TTYPEn (where n is 7, 8, ....) keywords or their auto-indexing equivalents. For a more complicated example of a template file using the group directives, look at the sample.tpl file that is included in the CFITSIO distribution.

9.4

Formal Template Syntax

The template syntax can formally b e defined as follows: TEMPLATE = BLOCK [ BLOCK ... ]


108 BLOCK = { HDU | GROUP } GROUP = \GROUP [ BLOCK ... ] \END

CHAPTER 9. TEMPLATE FILES

HDU = XTENSION [ LINE ... ] { XTENSION | \GROUP | \END | EOF } LINE = [ KEYWORD [ = ] ] [ VALUE ] [ / COMMENT ] X ... - X can be present 1 or more times { X | Y } - X or Y [X] - X is optional At the topmost level, the template defines 1 or more template blocks. Blocks can b e either HDU (Header Data Unit) or a GROUP. For each block the parser creates 1 (or more for GROUPs) FITS file HDUs.

9.5

Errors

In general the fits execute template() function tries to b e as atomic as p ossible, so either everything is done or nothing is done. If an error occurs during parsing of the template, fits execute template() will (try to) delete the top level BLOCK (with all its children if any) in which the error occurred, then it will stop reading the template file and it will return with an error.

9.6

Examples

1. This template file will create a 200 x 300 pixel image, with 4-byte integer pixel values, in the primary HDU: S B N N N O I I A A A B M T X X X J P P I I I E L I S S S C E X = 1 2 T = = 2 = = = T 32 / 100 / 200 / NGC 25 n l l 3 u e e / mber o ngth o ngth o name f f f o d f s f i i e o m r c b e s o s n t n e sions axis d axis rved object

The allowed values of BITPIX are 8, 16, 32, -32, or -64, representing, resp ectively, 8-bit integer, 16-bit integer, 32-bit integer, 32-bit floating p oint, or 64 bit floating p oint pixels. 2. To create a FITS table, the template first needs to include XTENSION = TABLE or BINTABLE to define whether it is an ASCI I or binary table, and NAXIS2 to define the numb er of rows in the table. Two template lines are then needed to define the name (TTYPEn) and FITS data format (TFORMn) of the columns, as in this example: xtension = bintable


9.6. EXAMPLES n t t t t t t t a t f t f t u f x y o y o y n o i p r p r p i r s e m e m e t m 2 # # # # # # # = = = = = = = = 4 N 1 N j R c e 0 ame 0a points ate ounts/s

109

The ab ove example defines a null primary array followed by a 40-row binary table extension with 3 columns called 'Name', 'Np oints', and 'Rate', with data formats of '10A' (ASCI I character string), '1J' (integer) and '1E' (floating p oint), resp ectively. Note that the other required FITS keywords (BITPIX, NAXIS, NAXIS1, PCOUNT, GCOUNT, TFIELDS, and END) do not need to b e explicitly defined in the template b ecause their values can b e inferred from the other keywords in the template. This example also illustrates that the templates are generally case-insensitive (the keyword names and TFORMn values are converted to upp er-case in the FITS file) and that string keyword values generally do not need to b e enclosed in quotes.


110

CHAPTER 9. TEMPLATE FILES


Chapter 10

Summary of all FITSIO User-Interface Subroutines
Error Status Routines page 29 F F F F F F F F T T T T T T T T V G G R P P C C E E M P M M M M R R S R S R S R S R G T G K G K ( > version) (status, > errtext) ( > errmsg) (stream, > status) (errmsg)

FITS File Op en and Close Subroutines: page 35 FTOPEN(unit,filename,rwmode, > blocksize,status) FTDKOPEN(unit,filename,rwmode, > blocksize,status) FTNOPN(unit,filename,rwmode, > status) FTDOPN(unit,filename,rwmode, > status) FTTOPN(unit,filename,rwmode, > status) FTIOPN(unit,filename,rwmode, > status) FTREOPEN(unit, > newunit, status) FTINIT(unit,filename,blocksize, > status) FTDKINIT(unit,filename,blocksize, > status) FTTPLT(unit, filename, tplfilename, > status) FTFLUS(unit, > status) FTCLOS(unit, > status) FTDELT(unit, > status) FTGIOU( > iounit, status) FTFIOU(iounit, > status) CFITS2Unit(fitsfile *ptr) (C routine) 111


112

CHAPTER 10. C F F F F F U T T T F T n E F F U I i X L L R U t T N M L R 2 N M D T L

SUMMARY OF ALL FITSIO USER-INTERFACE SUBROUTINES

FITS(int unit) (C routine) (filename, > nhdu, status) (unit, > filename, status) (unit, > iomode, status) (unit, > urltype, status) (filename, > filetype, infile, outfile, extspec, filter, binspec, colspec, status) FTRTNM(filename, > rootname, status) FTEXIST(filename, > exist, status) HDU-Level Op erations: page 38 F F F F F F F F F F F F F F F F T T T T T T T T T T T T T T T T M M G M G T C I I I R D C C C C A R H N H H R I T B S H P O P P H H D H D D H M A I I D F P H D D D N D T U D G B N M U L Y D T (unit,nhdu, > hdutype,status) (unit,nmove, > hdutype,status) (unit, > nhdu) (unit, hdutype, extname, extver, > status) (unit, > hdutype, status) (unit, > hdunum, status) (unit, > status) (unit,bitpix,naxis,naxes, > status) (unit,rowlen,nrows,tfields,ttype,tbcol,tform,tunit,extname, > status) (unit,nrows,tfields,ttype,tform,tunit,extname,varidat > status) (unit,bitpix,naxis,naxes,status) (unit, > hdutype,status) (iunit,ounit,previous, current, following, > status) (iunit,ounit,morekeys, > status) (inunit, outunit, > status) (iunit,ounit, > status)

Subroutines to sp ecify or modify the structure of the CHDU: page 41 F F F F F F T T T T T T R P A B D P D D D D D T E E E E E H F F F F F P ( ( ( ( ( ( u u u u u u n n n n n n i i i i i i t t t t t t , , , , , , > bi ro tf by th status) (DEPRECATED) tpix,naxis,naxes,pcount,gcount, > status) (DEPRECATED) wlen,tfields,tbcol,tform,nrows > status) (DEPRECATED) ields,tform,varidat,nrows > status) (DEPRECATED) tlen, > status) (DEPRECATED) eap, > status)

Header Space and Position Subroutines: page 43 FTHDEF(unit,morekeys, > status) FTGHSP(iunit, > keysexist,keysadd,status) FTGHPS(iunit, > keysexist,key_no,status) Read or Write Standard Header Subroutines: page 43


113 FTPHPS(unit,bitpix,naxis,naxes, > status) FTPHPR(unit,simple,bitpix,naxis,naxes,pcoun FTGHPR(unit,maxdim, > simple,bitpix,naxis,n status) FTPHTB(unit,rowlen,nrows,tfields,ttype,tbco status) FTGHTB(unit,maxdim, > rowlen,nrows,tfields, extname,status) FTPHBN(unit,nrows,tfields,ttype,tform,tunit FTGHBN(unit,maxdim, > nrows,tfields,ttype,t status) Write Keyword Subroutines: page 45 F F F F F F F F F F F F F F F T T T T T T T T T T T T T T T P P P P P P P P P P P C P P P R C H D K K K L K K K P K K U E O I A Y Y L S Y N N K Y T N C M S T [ [ S W U [ [ Y T P T ( ( ( ( J E ( ( ( J E i ( ( ( u u u u K D u u u K D n u u u n n n n L F n n n L F u n n n i i i i S G i i i S G n i i i t,card, > status) t,comment, > status) t,history, > status) t, > status) ](unit,keyword,keyval,comment, > status) ](unit,keyword,keyval,decimals,comment, > status) t,keyword,keyval,comment, > status) t, > status) t,keyword,comment, > status) ](unit,keyroot,startno,no_keys,keyvals,comments, > status) ](unit,keyroot,startno,no_keys,keyvals,decimals,comments, > status) it, outunit, innum, outnum, keyroot, > status) t,keyword,intval,dblval,comment, > status) t, filename, > status) t,keyword,units, > status)

t,gcount,extend, > status) axes,pcount,gcount,extend, l,tform,tunit,extname, > ttype,tbcol,tform,tunit, ,extname,varidat > status) form,tunit,extname,varidat,

Insert Keyword Subroutines: page 47 F F F F F T T T T T I I I I I R K K K K E Y L Y Y C [ S [ U ( J ( E ( u K u D u n L n F n i S i G i t ] t ] t , ( , ( , k u k u k e n e n e y i y i y _ t w t w n , o , o o k r k r , e d e d c y , y , a w k w c r o e o o d r y r m ,> d,k val d,k men status) eyval,comment, > status) ,comment, > status) eyval,decimals,comment, > status) t, > status)

Read Keyword Subroutines: page 47 F F F F T T T T G G G G R K C N E Y R X C N D K ( ( ( ( u u u u n n n n i i i i t t t t , , , , k k k i e e e n y y y c _ _ w l n n o i o o r s , , d t > card,status) > keyword,value,comment,status) , > card,status) ,ninc,exclist,nexc, > card,status)


114

CHAPTER 10. F F F F F T T T T T G G G G G K K K K U E Y N Y N Y [ [ T T ( E E ( ( u D D u u n J J n n i K K i i t L L t t , S S , , k ] ] k k e ( ( e e y u u y y

SUMMARY OF ALL FITSIO USER-INTERFACE SUBROUTINES w n n w w o i i o o r t t r r d , , d d , k k , , > ey ey > > value,comment,status) word, > keyval,comment,status) root,startno,max_keys, > keyvals,nfound,status) intval,dblval,comment,status) units,status)

Modify Keyword Subroutines: page 49 F F F F F F F F T T T T T T T T M M M M M M M M R C N C K K K K E R A O Y L Y Y C D M M [ S [ U ( ( ( ( J ( E ( u u u u K u D u n n n n L n F n i i i i S i G i t t t t ] t ] t , , , , ( , ( , k k o k u k u k e e l e n e n e y y d y i y i y _ w k w t w t w n o e o , o , o o r y r k r k r , d , d e d e d c , k , y , y , a c e c w k w c r a y o o e o o d r w m r y r m , > status) d, > status) ord, > status) ment, > status) d,keyval,comment, > status) val,comment, > status) d,keyval,decimals,comment, > status) ment, > status)

Up date Keyword Subroutines: page 50 F F F F F T T T T T U U U U U C K K K K R Y L Y Y D [ S [ U ( J ( E ( u K u D u n L n F n i S i G i t ] t ] t , ( , ( , k u k u k e n e n e y i y i y w t w t w o , o , o r k r k r d e d e d , y , y , c w k w c a o e o o r r y r m d, d, va d, me > status) keyval,comment, > status) l,comment, > status) keyval,decimals,comment, > status) nt, > status)

Delete Keyword Subroutines: page 50 FTDREC(unit,key_no, > status) FTDKEY(unit,keyword, > status) Define Data Scaling Parameters and Undefined Pixel Flags: page 51 F F F F F T T T T T P T P S T S S N N N C C U U U L L L L L ( ( ( ( ( u u u u u n n n n n i i i i i t t t t t , , , , , b c b c c s o l o o c l a l l a n n n n l u k u u e m , m m ,b ,t > ,s ,t zero, > statu scal,tzero, > status) null > status null > status s) status) ) )

FITS Primary Array or IMAGE Extension I/O Subroutines: page 52 F F F F T T T T G G G G I I I I D E D S T T M Z ( ( ( ( u u u u n n n n i i i i t t t t , , , , > > > m b b n ax i i a d t t x i p p i m i i s , x, x, ,s > status) status) tatus) naxes,status)


115 maxdim, > bitpix,naxis,naxes,status) ](unit,group,fpixel,nelements,values, > status) ](unit,group,fpixel,nelements,values,nullval > status) group,fpixel,nelements, > status) ](unit,group,fpixel,nelements,nullval, > values,anyf,status) ](unit,group,fpixel,nelements, > values,flagvals,anyf,status) ](unit,group,fparm,nparm,values, > status) ](unit,group,fparm,nparm, > values,status) ](unit,group,dim1,naxis1,naxis2,image, > status) ](unit,group,dim1,dim2,naxis1,naxis2,naxis3,cube, > status) ](unit,group,nullval,dim1,naxis1,naxis2, > image,anyf,status) ](unit,group,nullval,dim1,dim2,naxis1,naxis2,naxis3, > cube,anyf,status) FTPSS[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,array, > status) FTGSV[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,incs,nullval, > array,anyf,status) FTGSF[BIJKED](unit,group,naxis,naxes,fpixels,lpixels,incs, > array,flagvals,anyf,status) Table Column Information Subroutines: page 55 F F F F F F F F T T T T T T T T G G G G G E G G N N C C T Q C A R C N N C T D C W L O N L Y W L (unit, > nrows, status) (unit, > ncols, status) (unit,casesen,coltemplate, > colnum,status) (unit,casesen,coltemplate, > colnam,colnum,status) (unit,colnum, > datacode,repeat,width,status) (unit,colnum, > datacode,repeat,width,status) (unit,colnum, > dispwidth,status) (unit,colnum, > ttype,tbcol,tunit,tform,tscal,tzero,snull,tdisp,status) (unit,colnum, > ttype,tunit,datatype,repeat,tscal,tzero,tnull,tdisp,status) (unit,colnum,naxis,naxes, > status) (unit,colnum,maxdim, > naxis,naxes,status) (unit,tdimstr,colnum,maxdim, > naxis,naxes, status) (unit, > nrows,status) F F F F F F F F F F F F T T T T T T T T T T T T G P P P G G P G P P G G I P P P P P G G 2 3 2 3 P R N R V F P P D D D D R [ [ U [ [ [ [ [ [ [ [ ( B B ( B B B B B B B B u I I u I I I I I I I I n J J n J J J J J J J J i K K i K K K K K K K K t E E t E E E E E E E E , D D , D D D D D D D D

FTGBCL F F F F T T T F P G D G T T T R D D D S M M M Z

Low-Level Table Access Subroutines: page 58 F F F F T T T T G P G P T T T T B B B B S S B B ( ( ( ( u u u u n n n n i i i i t t t t , , , , f f f f r r r r o o o o w w w w , , , , s s s s t t t t a a a a r r r r t t t t c c c c ha ha ha ha r r r r ,n ,n ,n ,n c c c c h h h h ar ar ar ar s s s s , , , , > st > ar string,status) ring, > status) array,status) ray, > status)

Edit Rows or Columns page 58


116

CHAPTER 10. F F F F F F F F F T T T T T T T T T I D D D I I M D C R R R R C C V C P O O R W O L E O C W W G S L S C L L ( ( ( ( ( ( ( ( ( u u u u u u u u i n n n n n n n n n i i i i i i i i u t t t t t t t t n , , , , , , , , i f f r r c c c c t r r o o o o o o , o o w w l l l l o

SUMMARY OF ALL FITSIO USER-INTERFACE SUBROUTINES w w r l n n n n u , , a i u u u u t n n n s m m m m u r r g t , , , , n ow ow e, ,n tt nc ne > it s, > status) s, > status) > status) rows, > status) ype,tform, > status) ols,ttype,tform, > status) wveclen, > status) status) ,incolnum,outcolnum,createcol, > status);

Read and Write Column Data Routines page 60 F F F F F F T T T T T T P P P P G G C C C C C C L N L L L V [ [ X U ( [ S B ( ( u S L I u u n B B J n n i I I K i i t J J E t t , K K D , , c E E ] c c o D CM](unit,colnum,frow,felem,nelements,values, > status) unit,colnum,frow,felem,nelements,values,nullval > status) lnum,frow,fbit,nbit,lray, > status) lnum,frow,felem,nelements, > status) num,frow,felem,nelements, > values,status) M](unit,colnum,frow,felem,nelements,nullval, > values,anyf,status) EDCM](unit,colnum,frow,felem,nelements, > values,flagvals,anyf,status) ](unit,colnum,naxis,naxes,fpixels,lpixels,incs,nullval, > array,anyf,status) ](unit,colnum,naxis,naxes,fpixels,lpixels,incs, > array,flagvals,anyf,status) olnum,frow,fbit,nbit, > lray,status) nit,colnum,frow,nrows,fbit,nbit, > array,status) colnum,rownum, > nelements,offset,status) colnum,rownum,nelements,offset, > status) D ( o o l C

FTGCF[SLBIJK FTGSV[BIJKED FTGSF[BIJKED F F F F T T T T G G G P C C D D X X E E ( [ S S u I ( ( n J u u i D n n t ] i i , ( t t c u , ,

Row Selection and Calculator Routines: page 64 F F F F OW(unit,expr,firstrow, nrows, > n_good_rows, row_status, status) RW(unit, expr, > rownum, status) OW(inunit, outunit, expr, > status ) OW(unit,datatype,expr,firstrow,nelements,nulval, > array,anynul,status) FTCALC(inunit, expr, outunit, parName, parInfo, > status) FTCALC_RNG(inunit, expr, outunit, parName, parInfo, nranges, firstrow, lastrow, > status) FTTEXP(unit, expr, > datatype, nelem, naxis, naxes, status) T T T T F F S C R F R R

Celestial Coordinate System Subroutines: page 65 FTGICS(unit, > xrval,yrval,xrpix,yrpix,xinc,yinc,rot,coordtype,status)


117 FTGTCS(unit,xcol,ycol, > xrval,yrval,xrpix,yrp FTWLDP(xpix,ypix,xrval,yrval coordtype, FTXYPX(xpos,ypos,xrval,yrval coordtype, File Checksum Subroutines: page 67 F F F F F F T T T T T T P U V G E D C C C C S S K K K K U U S S S S M M ( ( ( ( ( ( u u u u s c n n n n u h i i i i m e t t t t , c , , , , c k > > > > om su status) status) dataok,hduok,status) datasum,hdusum,status) plement, > checksum) m,complement, > sum)

ix ,x > ,x >

,xinc,yinc,rot,coordtype,status) rpix,yrpix,xinc,yinc,rot, xpos,ypos,status) rpix,yrpix,xinc,yinc,rot, xpix,ypix,status)

Time and Date Utility Subroutines: page 68 > day, month, year, status ) > datestr, timeref, status) year, month, day, > datestr, status) year, month, day, hour, minute, second, decimals, > datestr, status) FTS2DT(datestr, > year, month, day, status) FTS2TM(datestr, > year, month, day, hour, minute, second, status) General Utility Subroutines: page 69 F F F F F F F F F F F c F F F F F T T T T T T T T T T T l T T T T T G U C T T N G P K N D a A B G G R H P M K R C K S E K T s S N A T W A C P E E H N V Y E Y s F F B H R D(unit, > curaddr,nextaddr) H(string) S(str_template,string,casesen, > match,exact) Y(keyword, > status) C(card, > status) K(unit, > status) M(unit, > keyword, keylength, status) C(card, > value,comment,status) N(keyroot,seq_no, > keyword,status) Y(seq_no,keyroot, > keyword,status) P(value, > dtype,status) = FTGKCL(card) M(tform, > datacode,width,decimals,status) M(tform, > datacode,repeat,width,status) C(tfields,tform,space, > rowlen,tbcol,status) D(template, > card,hdtype,status) G(rowlist, maxrows, maxranges, > numranges, rangemin, rangemax, status) F F F F T T T T G G D T S S T M D T 2 2 T M S S ( ( ( (


118

CHAPTER 10.

SUMMARY OF ALL FITSIO USER-INTERFACE SUBROUTINES


Chapter 11

Parameter Definitions
a a b b b b n r i l l l y r t a a o f a p n n c - (logical) set to TRUE if any of the returned data values are undefined y - (any datatype except character) array of bytes to be read or written. ix - (integer) bits per pixel: 8, 16, 32, -32, or -64 k - (integer) value used for undefined pixels in integer primary array k - (integer*8) value used for undefined pixels in integer primary array ksize - (integer) 2880-byte logical record blocking factor (if 0 < blocksize < 11) or the actual block size in bytes (if 10 < blocksize < 28800). As of version 3.3 of FITSIO, blocksizes greater than 2880 are no longer supported. le - (double precision) scaling factor for the primary array en - (integer) length of the data unit, in bytes o - (double precision) zero point for primary array scaling - (character*80) header record to be read or written sen - (logical) will string matching be case sensitive? ksum - (character*16) encoded checksum string ame - (character) ASCII name of the column um - (integer) number of the column (first column = 1) emplate - (character) template string to be matched to column names ent - (character) the keyword comment field ents - (character array) keyword comment fields id - (integer) the type of computer that the program is running on lement - (logical) should the checksum be complemented? dtype - (character) type of coordinate projection (-SIN, -TAN, -ARC, -NCP, -GLS, -MER, or -AIT) - 3D data cube of the appropriate datatype ddr - (integer) starting address (in bytes) of the CHDU ent - (integer) if not equal to 0, copy the current HDU code - (integer) symbolic code of the binary table column datatype ok - (integer) was the data unit verification successful (=1) or not (= -1). Equals zero if the DATASUM keyword is not present. sum - (double precision) 32-bit 1's complement checksum for the data unit type - (character) datatype (format) of the binary table column 119

b b b c c c c c c c c c c c c c c d d

s y z a a h o o o o o o o o u u u a a

c t e r s e l l l m m m m o b r r t t

a l r d e c n n t m m p p r e a r a a

data data


120 datestr d d d d d d d a b e i i i t y l c m m s y

CHAPTER 11.

PARAMETER DEFINITIONS

e e c e e e e e f f f f f f f f f f g g h

r r a x x x x x b e i l o p p p r r c r d

r r s a c i t t i l l a l a i i o o o o t

hdu hdu hdu h h i i i i i o m n n n s u a c c t

- (string) FITS date/time string: 'YYYY-MM-DDThh:mm:ss.ddd', 'YYYY-MM-dd', or 'dd/mm/yy' - (integer) current day of the month val - (double precision) fractional part of the keyword value imals - (integer) number of decimal places to be displayed 1 - (integer) actual size of the first dimension of the image or cube array 2 - (integer) actual size of the second dimension of the cube array pwidth - (integer) - the display width (length of string) for a column pe - (character) datatype of the keyword ('C', 'L', 'I', or 'F') C = character string L = logical I = integer F = floating point number msg - (character*80) oldest error message on the internal stack text - (character*30) descriptive error message corresponding to error number esen - (logical) true if column name matching is case sensitive ct - (logical) do the strings match exactly, or were wildcards used? list (character array) list of names to be excluded from search sts - flag indicating whether the file or compressed file exists on disk end - (logical) true if there may be extensions following the primary data name - (character) value of the EXTNAME keyword (if not blank) t - (integer) first bit in the field to be read or written em - (integer) first pixel of the element vector (ignored for ASCII tables) ename - (character) name of the FITS file gvals - (logical array) True if corresponding data element is undefined lowing - (integer) if not equal to 0, copy all following HDUs in the input file rm - (integer) sequence number of the first group parameter to read or write xel - (integer) the first pixel position xels - (integer array) the first included pixel in each dimension w - (integer) beginning row number (first row of table = 1) wll - (integer*8) beginning row number (first row of table = 1) unt - (integer) value of the GCOUNT keyword (usually = 1) up - (integer) sequence number of the data group (=0 for non-grouped data) ype - (integer) header record type: -1=delete; 0=append or replace; 1=append; 2=this is the END keyword ok - (integer) was the HDU verification successful (=1) or not (= -1). Equals zero if the CHECKSUM keyword is not present. sum - (double precision) 32 bit 1's complement checksum for the entire CHDU type - (integer) type of HDU: 0 = primary array or IMAGE, 1 = ASCII table, 2 = binary table, -1 = any HDU type or unknown type tory - (character) the HISTORY keyword comment string r - (integer) hour from 0 - 23 ge - 2D image of the appropriate datatype list (character array) list of names to be included in search s - (integer array) sampling interval for pixels in each FITS dimension val - (integer) integer part of the keyword value


121 i i k k k k k k k k l l m m m m m m n n n n n n n n n n n n n n n n n n n n n n o o o o p p o u e e e e e e e e r p a a a i o o a a a a a a b c c e e e h i m f o p r r u e f f l u c r u n y y y y y y y y a i t x x n n r x x x x x x i h o l l x d n o o _ a o o l x f f d n o e n i _ l r s s v v w y x c d _ u t e e e i i i i t a l e e c u c v u k r w w l t s s k i u v it - (integer) value of an unused I/O unit number t - (integer) logical unit number associated with the input FITS file, 1-199 no - (integer) sequence number (starting with 1) of the keyword record ength - (integer) length of the keyword name oot - (character) root string for the keyword name add -(integer) number of new keyword records which can fit in the CHU exist - (integer) number of existing keyword records in the CHU al - value of the keyword in the appropriate datatype als - (array) value of the keywords in the appropriate datatype ord - (character*8) name of a keyword - (logical array) array of logical values corresponding to the bit array els - (integer array) the last included pixel in each dimension h - (logical) do the 2 strings match? im - (integer) dimensioned size of the NAXES, TTYPE, TFORM or TUNIT arrays keys - (integer) maximum number of keywords to search for te - (integer) minute of an hour (0 - 59) h - (integer) current month of the year (1 - 12) keys - (integer) will leave space in the header for this many more keywords s - (integer array) size of each dimension in the FITS array sll - (integer*8 array) size of each dimension in the FITS array s - (integer) number of dimensions in the FITS array s1 - (integer) length of the X/first axis of the FITS array s2 - (integer) length of the Y/second axis of the FITS array s3 - (integer) length of the Z/third axis of the FITS array - (integer) number of bits in the field to read or write rs - (integer) number of characters to read and return s - (integer) number of columns ments - (integer) number of data elements to read or write mentsll - (integer*8) number of data elements to read or write (integer) number of names in the exclusion list (may = 0) - (integer) absolute number of the HDU (1st HDU = 1) (integer) number of names in the inclusion list e - (integer) number of HDUs to move (+ or -), relative to current position nd - (integer) number of keywords found (highest keyword number) eys - (integer) number of keywords to write in the sequence m - (integer) number of group parameters to read or write s - (integer) number of rows in the table sll - (integer*8) number of rows in the table val - value to represent undefined pixels, of the appropriate datatype addr - (integer) starting address (in bytes) of the HDU following the CHDU et - (integer) byte offset in the heap to the first element of the array etll - (integer*8) byte offset in the heap to the first element of the array ey - (character) old name of keyword to be modified t - (integer) logical unit number associated with the output FITS file 1-199 nt - (integer) value of the PCOUNT keyword (usually = 0) ious - (integer) if not equal to 0, copy all previous HDUs in the input file


122 r r r r r r r r s s s s s s s s s s s s t t t t t e o o o o o o w e e i n p t t t t t t u b d e f f p t w w w w w m c q m u a a a a r r r m c i m i o ea le le li nu ra od on _n pl ll ce rt rt tu _t ea in ol sp pl el rm

CHAPTER 11.

PARAMETER DEFINITIONS

theap t t t t t t u u v v v n n t s u z n n a a a u u y c n e i i l l r l l p a i r t t u u i l l e l t o s e e d

versi

t - (integer) length of element vector (e.g. 12J); ignored for ASCII table (double precision) celestial coordinate rotation angle (degrees) n - (integer) length of a table row, in characters or bytes nll - (integer*8) length of a table row, in characters or bytes st - (integer array) list of row numbers to be deleted in increasing order m - (integer) number of the row (first row = 1) nge- (string) list of rows or row ranges to be deleted e - (integer) file access mode: 0 = readonly, 1 = readwrite d (double)- second within minute (0 - 60.9999999999) (leap second!) o - (integer) the sequence number to append to the keyword root name e - (logical) does the FITS file conform to all the FITS standards - (character) value used to represent undefined values in ASCII table - (integer) number of blank spaces to leave between ASCII table columns char - (integer) first character in the row to be read no - (integer) value of the first keyword sequence number (usually 1) s - (integer) returned error status code (0 = OK) emplate (character) template string to be matched to reference string m - (character) output stream for the report: either 'STDOUT' or 'STDERR' g - (character) character string (double precision) 32 bit unsigned checksum value - (integer array) column number of the first character in the field(s) - (character) Fortran type display format for the table column ate-(character) template string for a FITS header record ds - (integer) number of fields (columns) in the table - (character array) format of the column(s); allowed values are: For ASCII tables: Iw, Aw, Fww.dd, Eww.dd, or Dww.dd For binary tables: rL, rX, rB, rI, rJ, rA, rAw, rE, rD, rC, rM where 'w'=width of the field, 'd'=no. of decimals, 'r'=repeat count Note that the 'rAw' form is non-standard extension to the TFORM keyword syntax that is not specifically defined in the Binary Tables definition document. - (integer) zero indexed byte offset of starting address of the heap relative to the beginning of the binary table data - (integer) value used to represent undefined values in binary table ll - (integer*8) value used to represent undefined values in binary table - (character array) label for table column(s) - (double precision) scaling factor for table column - (character array) physical unit for table column(s) - (double precision) scaling zero point for table column - (integer) logical unit number associated with the FITS file (1-199) - (character) the keyword units string (e.g., 'km/s') - (character) the keyword value string s - array of data values of the appropriate datatype at - (integer) size in bytes of the 'variable length data area' following the binary table data (usually = 0) on - (real) current revision number of the library


123 w x x x x x x y y y y y y y i c i p p r r c e i p p r r d o n i o p v o a n i o p v t l c x s i a l r c x s i a hxlxl( i d d d ( ( i i d d d ( ( i n o o o d d n n o o o d d n t u u u o o t t u u u o o t e b b b u u e e b b b u u e g l l l b b g g l l l b b g e e e e l l e e e e e l l er) width r) number precisio precisio precisio e precisi e precisi r) number r) last 2 precisio precisio precisio e precisi e precisi of of n) X n) X n) X on) on) of dig n) Y n) y n) y on) on) the the axi axi axi X ax X ax the its axi axi axi Y ax Y ax c c s s s i i c o s s s i i haracter string field olumn containing the X c coordinate increment at pixel location celestial coordinate (u s reference pixel array s coordinate value at th olumn containing the X c f the year (00 - 99) coordinate increment at pixel location celestial coordinate (u s reference pixel array s coordinate value at th

( ( ( (

oordinate values reference pixel (deg) s l e o ually RA) (deg) ocation reference pixel (deg) ordinate values

( ( ( ( (

reference pixel (deg) sually DEC) (deg) location e reference pixel (deg)


124

CHAPTER 11.

PARAMETER DEFINITIONS


Chapter 12

FITSIO Error Status Codes
Status codes in the range -99 to -999 FITSIO use. 0 01 03 04 05 06 07 08 10 11 12 13 14 15 16 2 2 2 2 2 2 5 5 5 5 5 5 1 2 3 4 5 6 1 2 3 4 5 6 O i t e e e e e e i C C i N e i t d m f p b n n s I n K n o r r r n r r n a o l U r n r r a a a a u o h P o , p o r r r d r r t n u l L r v i i t i r and 1 to 999 are reserved for future

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

no error ut and output files are the same many FITS files open at once; all internal b or opening existing file or creating new FITS file; (does a file with or writing record to FITS file -of-file encountered while reading record fro or reading record from file or closing FITS file ernal array dimensions exceeded not modify file with readonly access ld not allocate memory egal logical unit number; must be between 1 L input pointer to routine or seeking position in file a e v c l s l d e h e e id URL prefix on to register too r initialization ing driver is not d to parse input error in range l file name many IO drivers failed registered file URL ist

uffers full this name already exist?) m FITS file

199, inclusive

d argument in shared memory driver ll pointer passed as an argument more free shared memory handles ared memory driver is not initialized C error returned by a system call memory in shared memory driver 125


126 157 158 159

CHAPTER 12.

FITSIO ERROR STATUS CODES

resource deadlock would occur attempt to open/create lock file failed shared memory block cannot be resized at the moment

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 5 5 5

1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 1 1 2 3

h s s k k i i k k c i i i i i i n n c i c c c c c t t c c c c c t i t c t b u u E

e p p e e l l e e o l l l l l l e e o l o o o o o h h o o o o o h l h o h i n n N

a e e y y l l y y u l l l l l l g g l l u u u u u e e u u u u u e l i l e n r r D

der not empty; can't write required keywords cified keyword name was not found in the header cified header record number is out of bounds word value field is blank word value string is missing the closing quote character egal indexed keyword name (e.g. 'TFORM1000') egal character in keyword name or header record word does not have expected name. Keyword out of sequence? word does not have expected integer value ld not find the required END header keyword egal BITPIX keyword value egal NAXIS keyword value egal NAXISn keyword value: must be 0 or positive integer egal PCOUNT keyword value egal GCOUNT keyword value egal TFIELDS keyword value ative ASCII or binary table width value (NAXIS1) ative number of rows in ASCII or binary table (NAXIS2) umn name (TTYPE keyword) not found egal SIMPLE keyword value ld not find the required SIMPLE header keyword ld not find the required BITPIX header keyword ld not find the required NAXIS header keyword ld not find all the required NAXISn keywords in the header ld not find the required XTENSION header keyword CHDU is not an ASCII table extension CHDU is not a binary table extension ld not find the required PCOUNT header keyword ld not find the required GCOUNT header keyword ld not find the required TFIELDS header keyword ld not find all the required TBCOLn keywords in the header ld not find all the required TFORMn keywords in the header CHDU is not an IMAGE extension egal TBCOL keyword value; out of range s operation only allowed for ASCII or BINARY table extension umn is too wide to fit within the specified width of the ASCII table specified column name template matched more than one column name ary table row width is not equal to the sum of the field widths ecognizable type of FITS extension ecognizable FITS record keyword contains non-blank characters in columns 9-80


127 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 6 6 6 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 4 4 4 4 4 4 4 4 4 4 5 4 5 1 2 3 1 2 4 6 7 8 9 0 1 2 4 7 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 H D u u i i c a a i i a a i i v a n f a i NO H ME GR BA TO HD BA ID BA BA e a n n l l o t t l l t t l l a t u i t l T D M O D O U D E D D a t a r l l l t t l l t t l l l t m r t l _ U B U _ _ _ _ N _ _ d a b e e e u e e e e e e e e u e b s e e G _ E P G M A O T G G er fill area contains non-blank characters fill area contains non-blank on non-zero values le to parse the TFORM keyword value string cognizable TFORM datatype code gal TDIMn keyword value g m m m g g m m g g e m e t m g R A R _ R A L P I R R al HDU number; less than 1 or greater than internal buffer size n number out of range (1 - 999) pt to move to negative file record number pted to read or write a negative number of bytes in the FITS file al starting row number for table read or write operation al starting element number for table read or write operation pted to read or write character string in non-character table column pted to read or write logical value in non-logical table column al ASCII table TFORM format code for attempted operation al binary table TFORM format code for attempted operation for undefined pixels has not been defined pted to read or write descriptor in a non-descriptor field r of array dimensions out of range pixel number is greater than the last pixel number pt to set BSCALE or TSCALn scaling parameter = 0 al axis length less than 1 O L _ N O N R T C O O U R N O U Y E I A U U P E O T P _ A O L P P _ A T _ _ H D N _ _ _ T D _ F I D Y A Y F O D U _ B _ O U L M U N E 340 EMBER ND D Grouping function error

S_TRACKED TRACKED

POINTERS ATTACH DETACH malloc failed read error from file null pointer passed as an argument. Passing null pointer as a name of template file raises this error line read seems to be empty (used internally) cannot unread more then 1 line (or single line twice) too deep include file nesting (infinite loop, template includes itself ?)

360 NGP_NO_MEMORY 361 NGP_READ_ERR 362 NGP_NUL_PTR

363 NGP_EMPTY_CURLINE 364 NGP_UNREAD_QUEUE_FULL 365 NGP_INC_NESTING


128 366 NGP_ERR_FOPEN 367 NGP_EOF 368 NGP_BAD_ARG 369 NGP_TOKEN_NOT_EXPECT 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2 3 3 3 3 3 3 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 0 1 2 3 4 5 6 1 2 3 4 5 6 e e c a c c e e e b i n e e e s e v d b p c b e u r a r r a t a a r r r a l u r r r y x e a a a e a r n e p r r n t n n r r r d l m r r r n p c t d r l d r s q p

CHAPTER 12.

FITSIO ERROR STATUS CODES

fopen() failed, cannot open template file end of file encountered and not expected bad arguments passed. Usually means internal parser error. Should not happen token not expected here ing tring

or attempting to convert an integer to a formatted character str or attempting to convert a real value to a formatted character s not convert a quoted string keyword to an integer empted to read a non-logical keyword value as a logical value not convert a quoted string keyword to a real value not convert a quoted string keyword to a double precision value or attempting to read character string as an integer or attempting to read character string as a real value or attempting to read character string as a double precision val keyword datatype code egal number of decimal places while formatting floating point va erical overflow during implicit datatype conversion or compressing image or uncompressing image or in date or time conversion tax error in parser expression ression did not evaluate to desired type tor result too large to return in array a parser failed not sent an out column data encounter while parsing column se error: output file not of proper type estial angle too large for projection celestial coordinate or pixel value or in celestial coordinate calculation upported type of celestial projection uired celestial coordinate keywords not found roximate wcs keyword values were returned

ue lue