Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.astro.louisville.edu/software/sbig/archive/xmccd-4.1/xmccd-4.1e/docs/cfitsio/cfitsio.pdf
Äàòà èçìåíåíèÿ: Tue May 31 02:23:38 2011
Äàòà èíäåêñèðîâàíèÿ: Thu Feb 27 22:37:03 2014
Êîäèðîâêà:

Ïîèñêîâûå ñëîâà: m 81
CFITSIO User's Reference Guide
An Interface to FITS Format Files for C Programmers

Version 3.2

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

December 2010


ii


Contents
1 Introduction 1.1 1.2 1.3 1.4 2 A Brief Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sources of FITS Software and Information . . . . . . . . . . . . . . . . . . . . . . . . Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Legal Stuff ......................................... 1 1 1 2 4 5 5 5 7 7 7 7 9 9 9

Creating the CFITSIO Library 2.1 Building the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 2.1.2 2.1.3 2.1.4 2.2 2.3 2.4 2.5 2.6 Unix Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Windows PCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Macintosh PCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Testing the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Linking Programs with CFITSIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using CFITSIO in Multi-threaded Environments . . . . . . . . . . . . . . . . . . . . Getting Started with CFITSIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Example Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 13 15

3 4

A FITS Primer Programming Guidelines 4.1 4.2 4.3 4.4 4.5

CFITSIO Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Current Header Data Unit (CHDU) . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Function Names and Variable Datatyp es . . . . . . . . . . . . . . . . . . . . . . . . . 18 Supp ort for Unsigned Integers and Signed Bytes . . . . . . . . . . . . . . . . . . . . 20 Dealing with Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 iii


iv 4.6 4.7 4.8 4.9

CONTENTS Implicit Data Typ e Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Data Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Supp ort for IEEE Sp ecial Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Error Status Values and the Error Message Stack . . . . . . . . . . . . . . . . . . . . 24

4.10 Variable-Length Arrays in Binary Tables . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.11 Multiple Access to the Same FITS File . . . . . . . . . . . . . . . . . . . . . . . . . . 26 4.12 When the Final Size of the FITS HDU is Unknown . . . . . . . . . . . . . . . . . . . 27 4.13 CFITSIO Size Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5 Basic CFITSIO Interface Routines 5.1 5.2 5.3 5.4 29

CFITSIO Error Status Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 FITS File Access Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 HDU Access Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Header Keyword Read/Write Routines . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.4.1 5.4.2 Keyword Reading Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Keyword Writing Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

5.5 5.6 5.7

Primary Array or IMAGE Extension I/O Routines . . . . . . . . . . . . . . . . . . . 40 Image Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 ASCI I and Binary Table Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 5.7.1 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 Create New Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Column Information Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Routines to Edit Rows or Columns . . . . . . . . . . . . . . . . . . . . . . . . 52 Read and Write Column Data Routines . . . . . . . . . . . . . . . . . . . . . 53 Row Selection and Calculator Routines . . . . . . . . . . . . . . . . . . . . . 55 Column Binning or Histogramming Routines . . . . . . . . . . . . . . . . . . 57

5.8

Utility Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.8.1 5.8.2 5.8.3 File Checksum Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Date and Time Utility Routines . . . . . . . . . . . . . . . . . . . . . . . . . 61 General Utility Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 73

6

The CFITSIO Iterator Function 6.1 6.2 6.3

The Iterator Work Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 The Iterator Driver Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Guidelines for Using the Iterator Function . . . . . . . . . . . . . . . . . . . . . . . . 77


CONTENTS 6.4 7

v

Complete List of Iterator Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 81

World Coordinate System Routines 7.1

Self-contained WCS Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 85

8

Hierarchical Grouping Routines 8.1 8.2

Grouping Table Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Group Memb er Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 91

9

Sp ecialized CFITSIO Interface Routines 9.1 9.2 9.3

FITS File Access Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 HDU Access Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Sp ecialized Header Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . 97 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.3.6 9.3.7 Header Information Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Read and Write the Required Keywords . . . . . . . . . . . . . . . . . . . . . 97 Write Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Insert Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Read Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Modify Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Up date Keyword Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

9.4 9.5 9.6

Define Data Scaling and Undefined Pixel Parameters . . . . . . . . . . . . . . . . . . 105 Sp ecialized FITS Primary Array or IMAGE Extension I/O Routines . . . . . . . . . 106 Sp ecialized FITS ASCI I and Binary Table Routines . . . . . . . . . . . . . . . . . . 110 9.6.1 9.6.2 9.6.3 9.6.4 General Column Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Low-Level Table Access Routines . . . . . . . . . . . . . . . . . . . . . . . . . 111 Write Column Data Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Read Column Data Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 117

10 Extended File Name Syntax

10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 10.2 Filetyp e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 10.2.1 Notes ab out HTTP proxy servers . . . . . . . . . . . . . . . . . . . . . . . . . 120 10.2.2 Notes ab out the stream filetyp e driver . . . . . . . . . . . . . . . . . . . . . . 121 10.2.3 Notes ab out the gsiftp filetyp e . . . . . . . . . . . . . . . . . . . . . . . . . . 122


vi

CONTENTS 10.2.4 Notes ab out the root filetyp e . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 10.2.5 Notes ab out the shmem filetyp e: . . . . . . . . . . . . . . . . . . . . . . . . . 124 10.3 Base Filename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 10.4 Output File Name when Op ening an Existing File . . . . . . . . . . . . . . . . . . . 126 10.5 Template File Name when Creating a New File . . . . . . . . . . . . . . . . . . . . . 128 10.6 Image Tile-Compression Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . 128

10.7 HDU Location Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 10.8 Image Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 10.9 Image Transform Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 10.10 Column and Keyword Filtering Sp ecification . . . . . . . . . . . . . . . . . . . . . . 132

10.11 Row Filtering Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 10.11.1 General Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 10.11.2 Bit Masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 10.11.3 Vector Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

10.11.4 Good Time Interval Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 10.11.5 Spatial Region Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 10.11.6 Example Row Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 10.12 Binning or Histogramming Sp ecification . . . . . . . . . . . . . . . . . . . . . . . . . 144 11 Template Files 147

11.1 Detailed Template Line Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 11.2 Auto-indexing of Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 11.3 Template Parser Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 11.4 Formal Template Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 11.5 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 11.6 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 12 Local FITS Conventions 153

12.1 64-Bit Long Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 12.2 Long String Keyword Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 12.3 Arrays of Fixed-Length Strings in Binary Tables . . . . . . . . . . . . . . . . . . . . 155 12.4 Keyword Units Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 12.5 HIERARCH Convention for Extended Keyword Names . . . . . . . . . . . . . . . . 156

12.6 Tile-Compressed Image Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156


CONTENTS 13 Optimizing Programs

vii 159

13.1 How CFITSIO Manages Data I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 13.2 Optimization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 A Index of Routines B Parameter Definitions C CFITSIO Error Status Codes 163 169 175


viii

CONTENTS


Chapter 1

Introduction
1.1 A Brief Overview

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 is written in ANSI C and provides a p owerful yet simple interface for accessing FITS files which will run on most commonly used computers and workstations. CFITSIO 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 CFITSIO routines 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. CFITSIO also contains a set of Fortran callable wrapp er routines which allow Fortran programs to call the CFITSIO routines. See the companion "FITSIO User's Guide" for the definition of the Fortran subroutine calling sequences. These wrapp ers replace the older Fortran FITSIO library which is no longer supp orted. 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.

1.2

Sources of FITS Software and Information

The latest version of the CFITSIO source code, documentation, and example programs are available on the World-Wide Web or via anonymous ftp from: 1


2 http://heasarc.gsfc.nasa.gov/fitsio ftp://legacy.gsfc.nasa.gov/software/fitsio/c

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. The HEASARC also provides a very sophisticated FITS file analysis program called `Fv' which can b e used to display and edit the contents of any FITS file as well as construct new FITS files from scratch. The display functions in Fv allow users to interactively adjust the brightness and contrast of images, pan, zoom, and blink images, and measure the p ositions and brightnesses of ob jects within images. FITS tables can b e displayed like a spread sheet, and then modified using p owerful calculator and sorting functions. Fv is freely available for most Unix platforms, Mac PCs, and Windows PCs. 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. Fv and FTOOLS are available from their resp ective Web sites at: http://fv.gsfc.nasa.gov http://heasarc.gsfc.nasa.gov/ftools

1.3

Acknowledgments

The development of the many p owerful features in CFITSIO was made p ossible through collab orations 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. Also, in 2009, Bruce O'Neel was the key develop er of the thread-safe version of CFITSIO.


1.3. ACKNOWLEDGMENTS

3

The ISDC also provided the template parsing routines (written by Jurek Borkowski) and the 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. Giuliano Taffoni and Andrea Barisani, at INAF, University of Trieste, Italy, implemented the I/O driver routines for accessing FITS files on the computational grids using the gridftp protocol. 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. The new compressed image data format (where the image is tiled and the compressed byte stream from each tile is stored in a binary table) was implemented in collab oration with Richard White (STScI), Perry Greenfield (STScI) and Doug Tody (NOAO). 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, Gretchen Green, 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, Richard Mathar, John Mattox, Carsten Meyer, Emi Miyata, Stefan Mochnacki, Mike Noble, Oliver Ob erdorf, Clive Page, Arvind Parmar, Jeff Pedelty, Tim Pearson, Philipp e Prugniel, Maren Purves, Scott Randall, Chris Rogers, Arnold Rots, Rob Seaman, 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


4 Wimmers, Peter Young, Jianjun Xu, and Nelson Zarate.

CHAPTER 1. INTRODUCTION

1.4

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. DISCLAIMER: THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER 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."


Chapter 2

Creating the CFITSIO Library
2.1 Building the Library
On

The CFITSIO code is contained in ab out 40 C source files (*.c) and header files (*.h). VAX/VMS systems 2 assembly-code files (vmsieeed.mar and vmsieeer.mar) are also needed. CFITSIO has currently b een tested on the following platforms (not up-to-date): OP S S S S D D D D H I L M W W W M M E u u i i e E e E P B i k i i i a a R n n l l c C c C M n L n n n c c ATING SYSTEM OS Solaris icon Graphics IRIX icon Graphics IRIX64 Alpha OSF/1 station Ultrix Alpha OpenVMS VAX/VMS UX AIX ux inux dows 95/98/NT dows 95/98/NT/ME/XP dows 95/98/NT OS 7.1 or greater OS-X 10.1 or greater CO g g g M g g c g g g g D B M C M c M c c c I c c c c c c c R o i y e c P c c c P c c ILER and and and S and

cc (3.0.1) cc cc cc

c and cc c c c 3 rland C++ V4.5 crosoft/Compaq Visual C++ v5.0, v6.0 gwin gcc trowerks 10.+ (gcc)

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

2.1.1

Unix Systems

The CFITSIO library is built on Unix systems by typing: 5


6 > > >

CHAPTER 2.

CREATING THE CFITSIO LIBRARY

./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 files to /usr1/local/include (assuming of course that the process has p ermission to write to these directories). 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.

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.

On Mac OS X platforms the 'make shared' command works like on other UNIX platforms, but a .dylib file will b e created instead of .so. If installed in a nonstandard location, add its location to the DYLD LIBRARY PATH environment variable so that the library can b e found at run time. On HP/UX systems, the environment variable CFLAGS should b e set to -Ae b efore running configure to enable "extended ANSI" features.


2.2. TESTING THE LIBRARY By default, a set of Fortran-callable wrapp er routines are also built and library. If these wrapp er routines are not needed (i.e., the CFITSIO to any Fortran applications which call FITSIO subroutines) then they build by typing 'make all-nofitsio' instead of simply typing 'make'. This CFITSIO library slightly.

7 included in the CFITSIO library will not b e linked may b e omitted from the will reduce the size of the

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.

2.1.2

VMS

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.

2.1.3

Windows PCs
CFITSIO is available for IBM-PC users of the Borland or Microsoft files cfitsiodll 3xxx b orland.zip and cfitsiodll 3xxx vcc.zip, where release numb er. These zip archives also contains other files and CFITSIO DLL library.

A precompiled DLL version of Visual C++ compilers in the '3xxx' represents the current instructions on how to use the

The CFITSIO library may also b e built from the source code using the makefile.b c or makefile.vcc files. Finally, the makep c.bat file gives an example of building CFITSIO with the Borland C++ v4.5 or v5.5 compiler using older DOS commands.

2.1.4

Macintosh PCs

When building on Mac OS-X, users should follow the Unix instructions, ab ove. See the README.MacOS file for instructions on building a Universal Binary that supp orts b oth Intel and PowerPC CPUs.

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


8

CHAPTER 2.

CREATING THE CFITSIO LIBRARY

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 test 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 format 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 with: % f77 -o testf77 testf77.f -L. -lcfitsio -lnsl -lsocket or % f77 -f -o testf77 testf77.f -L. -lcfitsio or % f77 -o testf77 testf77.f -Wl,-L. -lcfitsio -lm -lnsl -lsocket (HP/UX) % testf77 > testf77.lis % diff testf77.lis testf77.out % cmp testf77.fit testf77.std On machines running SUN O/S, Fortran programs must b e compiled with the '-f double precision variables to b e aligned on 8-byte b oundarys to make the fortran-d compatible with C. A similar compiler option may b e required on other platform this option may cause the program to crash on FITSIO routines that read or write variables. ' option to force eclared variables s. Failing to use double precision (under SUN O/S)

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; the first four of this programs can b e compiled an linked by typing `make program name' where `program name' is the actual name of the program: 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).


2.3. LINKING PROGRAMS WITH CFITSIO 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

9

2.3

Linking Programs with CFITSIO

When linking applications software with the CFITSIO library, several system libraries usually need to b e sp ecified on the link command line. On 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 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. 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

Using CFITSIO in Multi-threaded Environments

CFITSIO can b e used either with the POSIX pthreads interface or the Op enMP interface for multithreaded parallel programs. When used in a multithreaded environment, the CFITSIO library *must* b e built using the -D REENTRANT compiler directive. This can b e done using the following build commands: >./configure --enable-reentrant > make Multiple threads can then call any of the CFITSIO routines to read or write separate FITS files. Multiple threads can also read data from the same FITS file simultaneously, but different threads must never try to write to the same FITS file.

2.5

Getting Started with CFITSIO

In order to effectively use the CFITSIO library it is recommended that new users b egin by reading the "CFITSIO Quick Start Guide". It contains all the basic information needed to write programs that p erform most typ es of op erations on FITS files. The set of example FITS utility programs that are available from the CFITSIO web site are also very useful for learning how to use CFITSIO. To learn even more ab out the capabilities of the CFITSIO library the following steps are recommended: 1. Read the following short `FITS Primer' chapter for an overview of the structure of FITS files. 2. Review the Programming Guidelines in Chapter 4 to b ecome familiar with the conventions used by the CFITSIO interface.


10

CHAPTER 2.

CREATING THE CFITSIO LIBRARY

3. Refer to the cookb ook.c, listhead.c, and fitscopy.c programs that are included with this release for examples of routines that p erform various common FITS file op erations. Typ e 'make program name' to compile and link these programs on Unix systems. 4. Write a simple program to read or write a FITS file using the Basic Interface routines describ ed in Chapter 5. 5. Scan through the more sp ecialized routines that are describ ed in the following chapters to b ecome familiar with the functionality that they provide.

2.6

Example Program

The following listing shows an example of how to use the CFITSIO routines in a C program. Refer to the cookb ook.c program that is included with the CFITSIO distribution for other example routines. This program creates a new FITS file, containing a FITS image. An `EXPOSURE' keyword is written to the header, then the image data are written to the FITS file b efore closing the FITS file. #include "fitsio.h" /* required by every program that uses CFITSIO */ main() { fitsfile *fptr; /* pointer to the FITS file; defined in fitsio.h */ int status, ii, jj; long fpixel = 1, naxis = 2, nelements, exposure; long naxes[2] = { 300, 200 }; /* image is 300 pixels wide by 200 rows */ short array[200][300]; status = 0; /* initialize status before calling fitsio routines */ fits_create_file(&fptr, "testfile.fits", &status); /* create new file */ /* Create the primary array image (16-bit short integer pixels */ fits_create_img(fptr, SHORT_IMG, naxis, naxes, &status); /* Write a keyword; must pass the ADDRESS of the value */ exposure = 1500.; fits_update_key(fptr, TLONG, "EXPOSURE", &exposure, "Total Exposure Time", &status); /* Initialize the values in the image with a linear ramp function */ for (jj = 0; jj < naxes[1]; jj++) for (ii = 0; ii < naxes[0]; ii++) array[jj][ii] = ii + jj; nelements = naxes[0] * naxes[1]; /* number of pixels to write */

/* Write the array of integers to the image */


2.6. EXAMPLE PROGRAM fits_write_img(fptr, TSHORT, fpixel, nelements, array[0], &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); return( status ); } /* close the file */ /* print out any error messages */

11


12

CHAPTER 2.

CREATING THE CFITSIO LIBRARY


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 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 data typ es are supp orted: Unsigned 8-bit bytes, 16-bit, 32-bit, and 64-bit signed integers, and 32 and 64-bit floating p oint reals. FITS also has a convention for storing 16 and 32-bit 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, ab ove 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 CFITSIO 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 data typ e of the array: 8, 16, 32, 64, -32, -64 for unsigned 8­bit byte, 16­bit signed integer, 32­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 data typ 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

Programming Guidelines
4.1 CFITSIO Definitions

Any program that uses the CFITSIO interface must include the fitsio.h header file with the statement #include "fitsio.h" This header file contains the prototyp es for all the CFITSIO user interface routines as well as the definitions of various constants used in the interface. It also defines a C structure of typ e `fitsfile' that is used by CFITSIO to store the relevant parameters that define the format of a particular FITS file. Application programs must define a p ointer to this structure for each FITS file that is to b e op ened. This structure is initialized (i.e., memory is allocated for the structure) when the FITS file is first op ened or created with the fits op en file or fits create file routines. This fitsfile p ointer is then passed as the first argument to every other CFITSIO routine that op erates on the FITS file. Application programs must not directly read or write elements in this fitsfile structure b ecause the definition of the structure may change in future versions of CFITSIO. A numb er of symb olic constants are also defined in fitsio.h for the convenience of application programmers. Use of these symb olic constants rather than the actual numeric value will help to make the source code more readable and easier for others to understand. String Lengths, for use when allocating character arrays: # # # # # # # d d d d d d d e e e e e e e f f f f f f f i i i i i i i n n n n n n n e e e e e e e F F F F F F F L L L L L L L E E E E E E E N N N N N N N _ _ _ _ _ _ _ F K C V C E S I E A A O R T L Y R L M R A E W D U M M T NAME 10 ORD 7 8 E 7 ENT 7 SG 8 US 3 25 /* max l 2 /* max l 1 /* max l 1 /* max l 3 /* max l 1 /* max l 1 /* max l e e e e e e e n n n n n n n g g g g g g g t t t t t t t h h h h h h h o o o o o o o f f f f f f f a a a a a a a f k F k k C C i e I e e F F l y T y y I I e w S w w T T name ord header card ord value str ord comment s SIO error mes SIO status te * * * ing * tring * sage * xt string / / / / / / */

Note that FLEN_KEYWORD is longer than the nominal 8-character keyword 15


16

CHAPTER 4.

PROGRAMMING GUIDELINES

name length because the HIERARCH convention supports longer keyword names. Access modes when opening a FITS file: #define READONLY 0 #define READWRITE 1 BITPIX data type code values for FITS images: # # # # # # d d d d d d e e e e e e f f f f f f i i i i i i n n n n n n e e e e e e B S L L F D Y H O O L O T O N N O U E R G G A B _ T _ L T L I _ I O _ E M I M N I _ G M G G M I 8 16 32 _IMG 64 G -32 MG -64 G / / / / / / * * * * * * 8 6 2 4 2 4 e I I I s b T b T b T b b b b b b i i i i i i t unsigned integers t signed integers t signed integers t signed integers t single precision f t double precision f * * * * l l / / / / oating point */ oating point */

1 3 6 3 6

The following 4 data type c #define SBYTE_IMG 10 /* /* #define USHORT_IMG 20 /* /* #define ULONG_IMG 40 /* /*

od 8 B 16 B 32 B

are it si PIX = it un PIX = it un PIX =

also supported by CFITSIO: gned integers, equivalent to */ 8, BSCALE = 1, BZERO = -128 */ signed integers, equivalent to */ 16, BSCALE = 1, BZERO = 32768 */ signed integers, equivalent to */ 32, BSCALE = 1, BZERO = 2147483648 */

Codes for the data type of binary table columns and/or for the data type of variables when reading or writing keywords or data: DATATYPE #define TBIT #define TBYTE #define TLOGICAL # # # # # # # # T # # # # d d d d d d d d h d d d d e e e e e e e e e e e e e f f f f f f f f i i i i i i i i n n n n n n n n e e e e e e e e T T T T T T T T S S L L F D C D T H O O L O O B R O N N O U M L I R G G A B P C NG T 1 11 14 6 1 1 1 2 2 3 3 / / / / / / / / / / / / * * * * * * * * * * * * 8-b log an ASC sig sig 64sin dou com dou i i d I n n b g b p b TFORM 'X t unsigned byte, 'B cals (int for keywords char for table cols 'L I string, 'A ed short, 'I ed long, it long signed integer 'K le precision float, 'E le precision float, 'D lex (pair of floats) 'C le complex (2 doubles) 'M CODE ' */ ' */ */ ' */ ' */ ' */ */ ' */ ' */ ' */ ' */ ' */

1 2 4 LONG 8 T 4 LE 8 LEX 8 OMPLEX 16

following data type codes are fine TINT 31 /* int fine TSBYTE 12 /* 8-bit fine TUINT 30 /* unsig fine TUSHORT 20 /* unsig

also supported by CFITSIO: */ signed byte, 'S' */ ned int 'V' */ ned short 'U' */


4.2. CURRENT HEADER DATA UNIT (CHDU) #define TULONG 40 /* unsigned long */

17

The following data type code is only for use with fits\_get\_coltype #define TINT32BIT 41 /* signed 32-bit int, 'J' */

HDU type code values (value returned when moving to new HDU): # # # # d d d d e e e e f f f f i i i i n n n n e e e e I A B A M S I N A C N Y G I A _ E I R H _ _ Y D HDU 0 TBL 1 _TBL 2 U -1 / / / / * * * * P A B m r S i a i C n t m I a c ary Array I table H ry table H hes any ty o D D p r U U e I * * o MAGE HDU */ / / f HDU */

Column name and string matching case-sensitivity: #define CASESEN 1 #define CASEINSEN 0 /* do case-sensitive string match */ /* do case-insensitive string match */

Logical states (if TRUE and FALSE are not already defined): #define TRUE 1 #define FALSE 0 Values to represent undefined floating point numbers: #define FLOATNULLVALUE -9.11912E-36F #define DOUBLENULLVALUE -9.1191291391491E-36 Image compression algorithm definitions # # # # d d d d e e e e f f f f i i i i n n n n e e e e R G P H I Z L C C I I O E P O M _ _ _ P 1 1 1 RESS_1 1 2 3 4 1 1 1 1

4.2

Current Header Data Unit (CHDU)

The concept of the Current Header and Data Unit, or CHDU, is fundamental to the use of the CFITSIO library. A simple FITS image may only contain a single Header and Data unit (HDU), but in general FITS files can contain multiple Header Data Units (also known as `extensions'), concatenated one after the other in the file. The user can sp ecify which HDU should b e initially op ened at run time by giving the HDU name or numb er after the root file name. For example, 'myfile.fits[4]' op ens the 5th HDU in the file (note that the numb ering starts with 0), and 'myfile.fits[EVENTS] op ens the HDU with the name 'EVENTS' (as defined by the EXTNAME or HDUNAME keywords). If no HDU is sp ecified then CFITSIO op ens the first HDU (the primary


18

CHAPTER 4.

PROGRAMMING GUIDELINES

array) by default. The CFITSIO routines which read and write data only op erate within the op ened HDU, Other CFITSIO routines are provided to move to and op en any other existing HDU within the FITS file or to app end or insert new HDUs in the FITS file.

4.3

Function Names and Variable Datatyp es
as a longer descriptive name. The subroutine name in the Fortran-77 it is recommended that it b e used code.

Most of the CFITSIO routines have b oth a short name as well short name is only 5 or 6 characters long and is similar to the version of FITSIO. The longer name is more descriptive and instead of the short name to more clearly document the source

Many of the CFITSIO routines come in families which differ only in the data typ e of the associated parameter(s). The data typ e of these routines is indicated by the suffix of the routine name. The short routine names have a 1 or 2 character suffix (e.g., 'j' in 'ffpkyj') while the long routine names have a 4 character or longer suffix as shown in the following table: L N _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ o a b b s s l l u u u i f f d f c f d f l s n m i y b h n n s l i n l i b i m i b i o t g e t t y t g g h n n t t x l x p x l x g r Short Names ----x b sb i j jj ui uj uk k e f d g c fc m cmp fm l s D T b u s s l 8 u u u i r r d d c c d d l c a y i n i h o n n n n e e o o o o o o o h t p t s g o n b s s s t a a u u m m u u g a a e igned byte ned byte rt integer g integer yte LONGLONG integer (see note below) igned short integer igned long integer igned int integer integer l exponential floating point (float) l fixed-decimal format floating point (float) ble precision real floating-point (double) ble precision fixed-format floating point (double) plex reals (pairs of float values) plex reals, fixed-format floating point ble precision complex (pairs of double values) ble precision complex, fixed-format floating point ical (int) racter string

s -

t

lng t g t

flt dbl cmp cmp dbl

The logical data typ e corresp onds to `int' for logical keyword values, and `byte' for logical binary table columns. In other words, the value when writing a logical keyword must b e stored in an `int' variable, and must b e stored in a `char' array when reading or writing to `L' columns in a binary table. Implicit data typ e conversion is not supp orted for logical table columns, but is for keywords, so a logical keyword may b e read and cast to any numerical data typ e; a returned value = 0 indicates false, and any other value = true.


4.3. FUNCTION NAMES AND VARIABLE DATATYPES

19

The `int' data typ e may b e 2 bytes long on some old PC compilers, but otherwise it is nearly always 4 bytes long. Some 64-bit machines, like the Alpha/OSF, define the `short', `int', and `long' integer data typ es to b e 2, 4, and 8 bytes long, resp ectively. Because there is no universal C compiler standard for the name of the 8-byte integer datatyp e, the fitsio.h include file typ edef 's 'LONGLONG' to b e equivalent to an appropriate 8-byte integer data typ e on each supp orted platform. For maximum software p ortability it is recommended that this LONGLONG datatyp e b e used to define 8-byte integer variables rather than using the native data typ e name on a particular platform. On most 32-bit Unix and Mac OS-X op erating systems LONGLONG is equivalent to the intrinsic 'long long' 8-byte integer datatyp e. On 64-bit systems (which currently includes Alpha OSF/1, 64-bit Sun Solaris, 64-bit SGI MIPS, and 64-bit Itanium and Opteron PC systems), LONGLONG is simply typ edef 'ed to b e equivalent to 'long'. Microsoft Visual C++ Version 6.0 does not define a 'long long' data typ e, so LONGLONG is typ edef 'ed to b e equivalent to the ' int64' data typ e on 32-bit windows systems when using Visual C++. A related issue that affects the p ortability of software is how to print out the value of a 'LONGLONG' variable with printf. Develop ers may find it convenient to use the following preprocessing statements in their C programs to handle this in a machine-p ortable manner: #if defined(_MSC_VER) /* Microsoft Visual C++ */ printf("%I64d", longlongvalue); #elif (USE_LL_SUFFIX == 1) printf("%lld", longlongvalue); #else printf("%ld", longlongvalue); #endif Similarly, the name of the C utility routine that converts a character string of digits into a 8-byte integer value is platform dep endent: #if defined(_MSC_VER) /* Microsoft Visual C++ */ /* VC++ 6.0 does not seem to have an 8-byte conversion routine */ #elif (USE_LL_SUFFIX == 1) longlongvalue = atoll(*string); #else longlongvalue = atol(*string); #endif When dealing with the FITS byte data typ 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 C compilers define a 'char' variable as signed, so it is imp ortant to explicitly declare a numeric char variable as 'unsigned char' to avoid any ambiguity


20 One feature of the as though it were the same as a `2B' to read and write

CHAPTER 4.

PROGRAMMING GUIDELINES

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' data typ 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 complex and double precision complex data typ es are not directly supp orted in ANSI C so these data typ es should b e interpreted as pairs of float or double values, resp ectively, where the first value in each pair is the real part, and the second is the imaginary part.

4.4

Supp ort for Unsigned Integers and Signed Bytes

Although FITS does not directly supp ort unsigned integers as one of its fundamental data typ es, FITS can still b e used to efficiently store unsigned integer data values in images and binary tables. The convention used in FITS files is to store the unsigned integers as signed integers with an associated offset (sp ecified by the BZERO or TZEROn keyword). For example, to store unsigned 16-bit integer values in a FITS image the image would b e defined as a signed 16-bit integer (with BITPIX keyword = SHORT IMG = 16) with the keywords BSCALE = 1.0 and BZERO = 32768. Thus the unsigned values of 0, 32768, and 65535, for example, are physically stored in the FITS image as -32768, 0, and 32767, resp ectively; CFITSIO automatically adds the BZERO offset to these values when they are read. Similarly, in the case of unsigned 32-bit integers the BITPIX keyword would b e equal to LONG IMG = 32 and BZERO would b e equal to 2147483648 (i.e. 2 raised to the 31st p ower). The CFITSIO interface routines will efficiently and transparently apply the appropriate offset in these cases so in general application programs do not need to b e concerned with how the unsigned values are actually stored in the FITS file. As a convenience for users, CFITSIO has several predefined constants for the value of BITPIX (USHORT IMG, ULONG IMG) and for the TFORMn value in the case of binary tables (`U' and `V') which programmers can use when creating FITS files containing unsigned integer values. The following code fragment illustrates how to write a FITS 1-D primary array of unsigned 16-bit integers: unsigned short uarray[100]; int naxis, status; long naxes[10], group, firstelem, nelements; ... status = 0; naxis = 1; naxes[0] = 100; fits_create_img(fptr, USHORT_IMG, naxis, naxes, &status); firstelem = 1; nelements = 100; fits_write_img(fptr, TUSHORT, firstelem, nelements, uarray, &status); ... In the ab ove example, the 2nd parameter in fits create img tells CFITSIO to write the header


4.4. SUPPORT FOR UNSIGNED INTEGERS AND SIGNED BYTES

21

keywords appropriate for an array of 16-bit unsigned integers (i.e., BITPIX = 16 and BZERO = 32768). Then the fits write img routine writes the array of unsigned short integers (uarray) into the primary array of the FITS file. Similarly, a 32-bit unsigned integer image may b e created by setting the second parameter in fits create img equal to `ULONG IMG' and by calling the fits write img routine with the second parameter = TULONG to write the array of unsigned long image pixel values. An analogous set of routines are available for reading or writing unsigned integer values and signed byte values in a FITS binary table extension. When sp ecifying the TFORMn keyword value which defines the format of a column, CFITSIO recognized 3 additional data typ e codes b esides those already defined in the FITS standard: `U' meaning a 16-bit unsigned integer column, `V' for a 32bit unsigned integer column, and 'S' for a signed byte column. These non-standard data typ e codes are not actually written into the FITS file but instead are just used internally within CFITSIO. The following code fragment illustrates how to use these features: unsigned short uarray[100]; unsigned int varray[100]; int colnum, tfields, status; long nrows, firstrow, firstelem, nelements, pcount; char extname[] = "Test_table"; / c c c * h h h . d r r r . efine the *ttype[] *tform[] *tunit[] n = = = am { { { e, "C "1 " d o U " /* extension name */

a a a .

ata type, and physical units for the 2 columns */ l_1", "Col_2", "Col_3" }; ", "1V", "1S"}; /* special CFITSIO codes */ , " ", " " };

s n t p f

t r f c i

a o i o t

t w e u s

/* us s lds nt _cr

write the header keywords */ = 0; = 1; =3 = 0; eate_tbl(fptr, BINARY_TBL, nrows, tfields, ttype, tform, tunit, extname, &status); write the unsigned shorts to the 1st column */ = 1; w = 1; em = 1; ts = 100; ite_col(fptr, TUSHORT, colnum, firstrow, firstelem, nelements, uarray, &status);

c f f n f

o i i e i

l r r l t

n s s e s

/* um tro tel men _wr

/* now write the unsigned longs to the 2nd column */ colnum = 2;


22

CHAPTER 4.

PROGRAMMING GUIDELINES

fits_write_col(fptr, TUINT, colnum, firstrow, firstelem, nelements, varray, &status); ... Note that the non-standard TFORM values for the 3 columns, `U' and `V', tell CFITSIO to write the keywords appropriate for unsigned 16-bit and unsigned 32-bit integers, resp ectively (i.e., TFORMn = '1I' and TZEROn = 32678 for unsigned 16-bit integers, and TFORMn = '1J' and TZEROn = 2147483648 for unsigned 32-bit integers). The 'S' TFORMn value tells CFITSIO to write the keywords appropriate for a signed 8-bit byte column with TFORMn = '1B' and TZEROn = -128. The calls to fits write col then write the arrays of unsigned integer values to the columns.

4.5

Dealing with Character Strings

The character string values in a FITS header or in an ASCI I column in a FITS table extension are generally padded out with non-significant space characters (ASCI I 32) to fill up the header record or the column width. When reading a FITS string value, the CFITSIO routines will strip off these non-significant trailing spaces and will return a null-terminated string value containing only the significant characters. Leading spaces in a FITS string are considered significant. If the string contains all blanks, then CFITSIO will return a single blank character, i.e, the first blank is considered to b e significant, since it distinguishes the string from a null or undefined string, but the remaining trailing spaces are not significant. Similarly, when writing string values to a FITS file the CFITSIO routines exp ect to get a nullterminated string as input; CFITSIO will pad the string with blanks if necessary when writing it to the FITS file. When calling CFITSIO routines that return a character string it is vital that the size of the char array b e large enough to hold the entire string of characters, otherwise CFITSIO will overwrite whatever memory locations follow the char array, p ossibly causing the program to execute incorrectly. This typ e of error can b e difficult to debug, so programmers should always ensure that the char arrays are allocated enough space to hold the longest p ossible string, including the terminating NULL character. The fitsio.h file contains the following defined constants which programmers are strongly encouraged to use whenever they are allocating space for char arrays: # # # # # # # d d d d d d d e e e e e e e f f f f f f f i i i i i i i n n n n n n n e e e e e e e F F F F F F F L L L L L L L E E E E E E E N N N N N N N _ _ _ _ _ _ _ F K C V C E S I E A A O R T L Y R L M R A E W D U M M T NAME 10 ORD 7 8 E 7 ENT 7 SG 8 US 3 25 /* max lengt 2 /* max lengt 1 /* length of 1 /* max lengt 3 /* max lengt 1 /* max lengt 1 /* max lengt h h a h h h h of a of a FIT of a of a of a of a f k S k k C C i e h e e F F l y e y y I I e w a w w T T n o d o o S S a r e r r I I m d r d d O O e* * ca va co er st / / r l m r a

d u m o t

*/ e string */ ent string */ r message */ us text string */

For example, when declaring a char array to hold the value string of FITS keyword, use the following statement: char value[FLEN_VALUE];


4.6. IMPLICIT DATA TYPE CONVERSION

23

Note that FLEN KEYWORD is longer than needed for the nominal 8-character keyword name b ecause the HIERARCH convention supp orts longer keyword names.

4.6

Implicit Data Typ e Conversion

The CFITSIO routines that read and write numerical data can p erform implicit data typ e conversion. This means that the data typ e of the variable or array in the program does not need to b e the same as the data typ e of the value in the FITS file. Data typ e conversion is supp orted for numerical and string data typ es (if the string contains a valid numb er enclosed in quotes) when reading a FITS 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 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 table columns). In the case of integer output values the floating p oint scaled value is truncated to an integer (not rounded to the nearest integer). The fits set bscale and fits set tscale routines (describ ed in the `Advanced' chapter) may b e used to override the scaling parameters defined in the header (e.g., to turn off the scaling so that the program can read the raw unscaled values from the FITS file). 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) 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 immediate effect. One may also use the fits set bscale and fits set tscale routines 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 data typ es to the FITS file.


24

CHAPTER 4.

PROGRAMMING GUIDELINES

4.8

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 CFITSIO routines 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 CFITSIO. This can b e done by calling the fits read img or fits read col routines while sp ecifying 0.0 as the value of the NULLVAL parameter. This will force CFITSIO to simply pass the IEEE values through to the application program without any modification. This is not fully supp orted on VAX/VMS machines, however, where there is no easy way to bypass the default interpretation of the IEEE sp ecial values. This is also not supp orted when reading floating-p oint images that have b een compressed with the FITS tiled image compression convention that is discussed in section 5.6; the pixels values in tile compressed images are represented by scaled integers, and a reserved integer value (not a NaN) is used to represent undefined pixels.

4.9

Error Status Values and the Error Message Stack

Nearly all the CFITSIO routines return an error status value in 2 ways: as the value of the last parameter in the function call, and as the returned value of the function itself. This provides some flexibility in the way programmers can test if an error occurred, as illustrated in the following 2 code fragments: if ( fits_write_record(fptr, card, &status) ) printf(" Error occurred while writing keyword."); or, fits_write_record(fptr, card, &status); if ( status ) printf(" Error occurred while writing keyword."); A listing of all the CFITSIO status code values is given at the end of this document. Programmers are encouraged to use the symb olic mnemonics (defined in fitsio.h) rather than the actual integer status values to improve the readability of their code. The CFITSIO library uses an `inherited status' convention for the status parameter which means that if a routine is called with a p ositive input value of the status parameter as input, then the routine will exit immediately without changing the value of the status parameter. Thus, if one passes the status value returned from each CFITSIO routine as input to the next CFITSIO routine, then whenever an error is detected all further CFITSIO 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 CFITSIO routine call. If a program contains a sequence


4.10. VARIABLE-LENGTH ARRAYS IN BINARY TABLES

25

of several CFITSIO 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 routine originally returned the error status. CFITSIO 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 CFITSIO error. The function fits rep ort error will print out the entire error message stack, or alternatively one may call fits read errmsg to get the error messages one at a time.

4.10

Variable-Length Arrays in Binary Tables

CFITSIO 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)' where `t' is the data typ 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. (CFITSIO also supp orts the exp erimental 'Q' datatyp e, which is identical to the 'P' typ e except that is supp orts is a 64-bit address space and hence much larger data structures). 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 CFITSIO 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 that read and write data in an ordinary fixed length binary table extension are also used for variable length fields, however, the routine 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 by 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. CFITSIO 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 row of data 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 fits write col. The total length of the array is given by nelements + firstelem - 1. Additional elements cannot b e app ended to an existing vector at a later time since any attempt to do so will simply overwrite all the previously written data and the new data will b e written to a new area of the heap. The fits compress heap routine is provided to compress the heap and recover any unused space. To avoid having to deal with this issue, it


26

CHAPTER 4.

PROGRAMMING GUIDELINES

is recommended that rows in a variable length field should only b e written once. An exception to this general rule occurs when setting elements of an array as undefined. It is allowed to first write a dummy value into the array with fits write col, and then call fits write col nul to flag the desired elements as undefined. 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 can b e written. The `firstelem' and `nelements' parameter values in the fits write col routine are ignored and the numb er of characters to write is simply determined by the length of the input null-terminated character string. The fits write descript routine 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 fits write descript to write the same descriptor values into the other rows; 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 fits read descript routine.

4.11

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 of course 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. Some restriction apply: a FITS file cannot b e op ened the first time with READONLY access, and then op ened a second time with READWRITE access, b ecause this may b e phyically imp ossible (e.g., if the file resides on read-only media such as a CDROM). Also, in multi-threaded environoments, one should never op en the same file with write access in different threads.


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

27

4.12

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.13

CFITSIO Size Limitations

CFITSIO places very few restrictions on the size of FITS files that it reads or writes. There are a few limits, however, that 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. It used to b e common for computer systems to only supp ort disk files up to 2**31 bytes = 2.1 GB in size, but most systems now supp ort larger files. CFITSIO can optionally read and write these so-called 'large files' that are greater than 2.1 GB on platforms where they are supp orted, but this usually requires that sp ecial compiler option flags b e sp ecified to turn on this


28

CHAPTER 4.

PROGRAMMING GUIDELINES

option. On linux and solaris systems the compiler flags are '-D LARGEFILE SOURCE' and `D FILE OFFSET BITS=64'. These flags may also work on other platforms but this has not b een tested. Starting with version 3.0 of CFITSIO, the default Makefile that is distributed with CFITSIO will include these 2 compiler flags when building on Solaris and Linux PC systems. Users on other platforms will need to add these compiler flags manually if they want to supp ort large files. In most cases it app ears that it is not necessary to include these compiler flags when compiling application code that call the CFITSIO library routines. When CFITSIO is built with large file supp ort (e.g., on Solaris and Linux PC system by default) then it can read and write FITS data files on disk that have any of these conditions: · FITS files larger than 2.1 GB in size · FITS images containing greater than 2.1 G pixels · FITS images that have one dimension with more than 2.1 G pixels (as given by one of the NAXISn keyword) · FITS tables containing more than 2.1E09 rows (given by the NAXIS2 keyword), or with rows that are more than 2.1 GB wide (given by the NAXIS1 keyword) · FITS binary tables with a variable-length array heap that is larger than 2.1 GB (given by the PCOUNT keyword) The current maximum FITS file size supp orted by CFITSIO is ab out 6 terabytes (containing 2**31 FITS blocks, each 2880 bytes in size). Currently, supp ort for large files in CFITSIO has b een tested on the Linux, Solaris, and IBM AIX op erating systems. Note that when writing application programs that are intended to supp ort large files it is imp ortant to use 64-bit integer variables to store quantities such as the dimensions of images, or the numb er of rows in a table. These programs must also call the sp ecial versions of some of the CFITSIO routines that have b een adapted to supp ort 64-bit integers. The names of these routines end in 'll' ('el' 'el') to distinguish them from the 32-bit integer version (e.g., fits get num rowsll).


Chapter 5

Basic CFITSIO Interface Routines
This chapter describ es the basic routines in tions normally needed to read and write m b e used for most applications and that the only b e used in sp ecial circumstances when the CFITSIO user interface that provide all the funcost FITS files. It is recommended that these routines more advanced routines describ ed in the next chapter necessary.

The following conventions are used in this chapter in the description of each function: 1. Most functions have 2 names: a long descriptive name and a short concise name. Both names are listed on the first line of the following descriptions, separated by a slash (/) character. Programmers may use either name in their programs but the long names are recommended to help document the code and make it easier to read. 2. A right arrow symb ol (>) is used in the function descriptions to separate the input parameters from the output parameters in the definition of each routine. This symb ol is not actually part of the C calling sequence. 3. The function parameters are defined in more detail in the alphab etical listing in App endix B. 4. The first argument in almost all the functions is a p ointer to a structure of typ e `fitsfile'. Memory for this structure is allocated by CFITSIO when the FITS file is first op ened or created and is freed when the FITS file is closed. 5. The last argument in almost all the functions is the error status parameter. It must b e equal to 0 on input, otherwise the function will immediately exit without doing anything. A non-zero output value indicates that an error occurred in the function. In most cases the status value is also returned as the value of the function itself.

5.1

CFITSIO Error Status Routines

1 Return a descriptive text string (30 char max.) corresp onding to a CFITSIO error status code. void fits_get_errstatus / ffgerr (int status, > char *err_text) 2 Return the top (oldest) 80-character error message from the internal CFITSIO stack of error messages and shift any remaining messages on the stack up one level. Call this routine 29


30

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES rep eatedly to get each message in sequence. The function returns a value = 0 and a null error message when the error stack is empty. int fits_read_errmsg / ffgmsg (char *err_msg)

3 Print out the error message corresp onding to the input status value and all the error messages on the CFITSIO stack to the sp ecified file stream (normally to stdout or stderr). If the input status value = 0 then this routine does nothing. void fits_report_error / ffrprt (FILE *stream, status) 4 The fits write errmark routine puts an invisible marker on the CFITSIO error stack. fits clear errmark routine can then b e used to delete any more recent error messages on stack, back to the p osition of the marker. This preserves any older error messages on stack. The fits clear errmsg routine simply clears all the messages (and marks) from stack. These routines are called without any arguments. void fits_write_errmark / ffpmrk (void) void fits_clear_errmark / ffcmrk (void) void fits_clear_errmsg / ffcmsg (void) Th th th th e e e e

5.2

FITS File Access Routines

1 Op en an existing data file. int fits_open_file / ffopen (fitsfile **fptr, char *filename, int iomode, > int *status) int fits_open_diskfile / ffdkopen (fitsfile **fptr, char *filename, int iomode, > int *status) int fits_open_data / ffdopn (fitsfile **fptr, char *filename, int iomode, > int *status) int fits_open_table / fftopn (fitsfile **fptr, char *filename, int iomode, > int *status) int fits_open_image / ffiopn (fitsfile **fptr, char *filename, int iomode, > int *status) The iomode parameter determines the read/write access allowed in the file and can have values of READONLY (0) or READWRITE (1). The filename parameter gives the name of the file to b e op ened, followed by an optional argument giving the name or index numb er of the extension within the FITS file that should b e moved to and op ened (e.g., myfile.fits+3


5.2. FITS FILE ACCESS ROUTINES

31

or myfile.fits[3] moves to the 3rd extension within the file, and myfile.fits[events] moves to the extension with the keyword EXTNAME = 'EVENTS'). The fits op en diskfile routine is similar to the fits op en file routine except that it does not supp ort the extended filename syntax in the input file name. This routine simply tries to op en the sp ecified input file on magnetic disk. This routine is mainly for use in cases where the filename (or directory path) contains square or curly bracket characters that would confuse the extended filename parser. The fits op en data routine is similar to the fits op en file routine except that it will move to the first HDU containing significant data, if a HDU name or numb er to op en was not explicitly sp ecified as part of the filename. In this case, it will look for the first IMAGE HDU with NAXIS greater than 0, or the first table that does not contain the strings `GTI' (Good Time Interval extension) or `OBSTABLE' in the EXTNAME keyword value. The fits op en table and fits op en image routines are similar to fits op en data except they will move to the first significant table HDU or image HDU in the file, resp ectively, if a HDU name or numb er is not sp ecified as part of the filename. IRAF images (.imh format files) and raw binary data arrays may also b e op ened with READONLY access. CFITSIO will automatically test if the input file is an IRAF image, and if, so will convert it on the fly into a virtual FITS image b efore it is op ened by the application program. If the input file is a raw binary data array of numb ers, then the data typ e and dimensions of the array must b e sp ecified in square brackets following the name of the file (e.g. 'rawfile.dat[i512,512]' op ens a 512 x 512 short integer image). See the `Extended File Name Syntax' chapter for more details on how to sp ecify the raw file name. The raw file is converted on the fly into a virtual FITS image in memory that is then op ened by the application program with READONLY access. Programs can read the input file from the 'stdin' file stream if a dash character ('-') is given as the filename. Files can also b e op ened over the network using FTP or HTTP protocols by supplying the appropriate URL as the filename. The input file can b e modified in various ways to create a virtual file (usually stored in memory) that is then op ened by the application program by supplying a filtering or binning sp ecifier in square brackets following the filename. Some of the more common filtering methods are illustrated in the following paragraphs, but users should refer to the 'Extended File Name Syntax' chapter for a complete description of the full file filtering syntax. When op ening an image, a rectangular subset of the physical image may b e op ened by listing the first and last pixel in each dimension (and optional pixel skipping factor): myimage.fits[101:200,301:400] will create and op en a 100x100 pixel virtual image of that section of the physical image, and myimage.fits[*,-*] op ens a virtual image that is the same size as the physical image but has b een flipp ed in the vertical direction. When in the ble in a valu op ening a table, the filtering syntax can b e used to ad virtual table: myfile.fits[events][col !time; PI which the TIME column has b een deleted and a new e 1.2 times that of the PHA column. Similarly, one d or delete = PHA*1. PI column can filter columns or keywords 2] op ens a virtual tahas b een added with a table to keep only


32

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES those rows that satisfy a selection criterion: myfile.fits[events][pha > 50] creates and op ens a virtual table containing only those rows with a PHA value greater than 50. A large numb er of b oolean and mathematical op erators can b e used in the selection expression. One can also filter table rows using 'Good Time Interval' extensions, and spatial region filters as in myfile.fits[events][gtifilter()] and myfile.fits[events][regfilter( "stars.rng")]. Finally, table columns may b e binned or histogrammed to generate a virtual image. For example, myfile.fits[events][bin (X,Y)=4] will result in a 2-dimensional image calculated by binning the X and Y columns in the event table with a bin size of 4 in each dimension. The TLMINn and TLMAXn keywords will b e used by default to determine the range of the image. A single program can op en the same FITS file fitsfile p ointers as though they were completely program can op en a FITS file twice, move to 2 read and write data in those extensions in any more than once and then treat the resulting indep endent FITS files. Using this facility, a different extensions within the file, and then order.

2 Create and op en a new empty output FITS file. int fits_create_file / ffinit (fitsfile **fptr, char *filename, > int *status) int fits_create_diskfile / ffdkinit (fitsfile **fptr, char *filename, > int *status) An error will b e returned if the sp ecified file already exists, unless with an exclamation p oint (!). In that case CFITSIO will overwrite with the same name. Note that the exclamation p oint is a sp ecial is used on the command line it must b e preceded by a backslash to accept the character as part of the filename. the filename is prefixed (delete) any existing file UNIX character so if it force the UNIX shell to

The output file will b e written to the 'stdout' file stream if a dash character ('-') or the string 'stdout' is given as the filename. Similarly, '-.gz' or 'stdout.gz' will cause the file to b e gzip compressed b efore it is written out to the stdout stream. Optionally, the name of a template file that is used to define the structure of the new file may b e sp ecified in parentheses following the output file name. The template file may b e another FITS file, in which case the new file, at the time it is op ened, will b e an exact copy of the template file except that the data structures (images and tables) will b e filled with zeros. Alternatively, the template file may b e an ASCI I format text file containing directives that define the keywords to b e created in each HDU of the file. See the 'Extended File Name Syntax' section for a complete description of the template file syntax. The fits create diskfile routine is similar to the fits create file routine except that it does not supp ort the extended filename syntax in the input file name. This routine simply tries to create the sp ecified file on magnetic disk. This routine is mainly for use in cases where the filename (or directory path) contains square or curly bracket characters that would confuse the extended filename parser.


5.3. HDU ACCESS ROUTINES

33

3 Close a previously op ened FITS file. The first routine simply closes the file, whereas the second one also DELETES THE FILE, which can b e useful in cases where a FITS file has b een partially created, but then an error occurs which prevents it from b eing completed. int fits_close_file / ffclos (fitsfile *fptr, > int *status) int fits_delete_file / ffdelt (fitsfile *fptr, > int *status) 4 Return the name, I/O mode (READONLY or READWRITE), and/or the file typ e (e.g. 'file://', 'ftp://') of the op ened FITS file. int fits_file_name / ffflnm (fitsfile *fptr, > char *filename, int *status) int fits_file_mode / ffflmd (fitsfile *fptr, > int *iomode, int *status) int fits_url_type / ffurlt (fitsfile *fptr, > char *urltype, int *status)

5.3

HDU Access Routines

The following functions p erform op erations on Header-Data Units (HDUs) as a whole. 1 Move to a different HDU in the file. The first routine moves to a sp ecified absolute HDU numb er (starting with 1 for the primary array) in the FITS file, and the second routine moves a relative numb er HDUs forward or backward from the current HDU. A null p ointer may b e given for the hdutyp e parameter if it's value is not needed. The third routine moves to the (first) HDU which has the sp ecified extension typ e and EXTNAME and EXTVER keyword values (or HDUNAME and HDUVER keywords). The hdutyp e parameter may have a value of IMAGE HDU, ASCI I TBL, BINARY TBL, or ANY HDU 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 will b e returned. int fits_movabs_hdu / ffmahd (fitsfile *fptr, int hdunum, > int *hdutype, int *status) int fits_movrel_hdu / ffmrhd (fitsfile *fptr, int nmove, > int *hdutype, int *status) int fits_movnam_hdu / ffmnhd (fitsfile *fptr, int hdutype, char *extname, int extver, > int *status) 2 Return the total numb er of HDUs in the FITS file. This returns the numb er of completely defined HDUs in the file. If a new HDU has just b een added to the FITS file, then that last


34

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES HDU will only b e counted if it has b een closed, or if data has b een written to the HDU. The current HDU remains unchanged by this routine. int fits_get_num_hdus / ffthdu (fitsfile *fptr, > int *hdunum, int *status)

3 Return the numb er of the current HDU (CHDU) in the FITS file (where the primary array = 1). This function returns the HDU numb er rather than a status value. int fits_get_hdu_num / ffghdn (fitsfile *fptr, > int *hdunum) 4 Return the typ e of the current HDU in the FITS file. The p ossible values for hdutyp e are: IMAGE HDU, ASCI I TBL, or BINARY TBL. int fits_get_hdu_type / ffghdt (fitsfile *fptr, > int *hdutype, int *status) 5 Copy all or part of the HDUs in the FITS file associated with infptr and app end them to the end of the FITS file associated with outfptr. If 'previous' is true (not 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. Thus, if all 3 parameters are true, then the entire input file will b e copied. On exit, the current HDU in the input file will b e unchanged, and the last HDU in the output file will b e the current HDU. int fits_copy_file / ffcpfl (fitsfile *infptr, fitsfile *outfptr, int previous, int current, int following, > int *status) 6 Copy the current HDU from the FITS file associated with infptr and app end it to the end of the FITS file associated with outfptr. Space may b e reserved for MOREKEYS additional keywords in the output header. int fits_copy_hdu / ffcopy (fitsfile *infptr, fitsfile *outfptr, int morekeys, > int *status) 7 Write the current HDU in the input FITS file to the output FILE stream (e.g., to stdout). int fits_write_hdu / ffwrhdu (fitsfile *infptr, FILE *stream, > int *status) 8 Copy the header (and not associated with outfptr. I will b e closed and a new unit will b e created with the data) from the CHDU associated with infptr to the CHDU f the current output HDU is not completely empty, then the CHDU HDU will b e app ended to the output file. An empty output data all values initially = 0).


5.4. HEADER KEYWORD READ/WRITE ROUTINES int fits_copy_header / ffcphd (fitsfile *infptr, fitsfile *outfptr, > int *status)

35

9 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 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 hdutyp e parameter returns the typ e of the new CHDU. A null p ointer may b e given for hdutyp e if the returned value is not needed. int fits_delete_hdu / ffdhdu (fitsfile *fptr, > int *hdutype, int *status)

5.4

Header Keyword Read/Write Routines

These routines read or write keywords in the Current Header Unit (CHU). 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). When a wild card is used the routine will only search for a match from the current header p osition to the end of the header and 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. The fits read record routine may b e used to set the starting p osition when doing wild card searches. A status value of KEY NO EXIST is returned if the sp ecified keyword to b e read is not found in the header.

5.4.1

Keyword Reading Routines

1 Return the numb er of existing keywords (not counting the END keyword) and the amount of space currently available for more keywords. It returns morekeys = -1 if the header has not yet b een closed. Note that CFITSIO will dynamically add space if required when writing new keywords to a header so in practice there is no limit to the numb er of keywords that can b e added to a header. A null p ointer may b e entered for the morekeys parameter if it's value is not needed. int fits_get_hdrspace / ffghsp (fitsfile *fptr, > int *keysexist, int *morekeys, int *status) 2 Return the sp ecified keyword. In the first routine, the datatyp e parameter sp ecifies the desired returned data typ e of the keyword value and can have one of the following symb olic constant values: TSTRING, TLOGICAL (== int), TBYTE, TSHORT, TUSHORT, TINT, TUINT,


36

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES TLONG, TULONG, TLONGLONG, TFLOAT, TDOUBLE, TCOMPLEX, and TDBLCOMPLEX. Within the context of this routine, TSTRING corresp onds to a 'char*' data typ e, i.e., a p ointer to a character array. Data typ e conversion will b e p erformed for numeric values if the keyword value does not have the same data typ e. If the value of the keyword is undefined (i.e., the value field is blank) then an error status = VALUE UNDEFINED will b e returned. The second routine returns the keyword value as a character string (a literal copy of what is in the value field) regardless of the intrinsic data typ e of the keyword. The third routine returns the entire 80-character header record of the keyword, with any trailing blank characters stripp ed off. The fourth routine returns the (next) header record that contains the literal string of characters sp ecified by the 'string' argument. If a NULL comment p ointer is supplied then the comment string will not b e returned. int fits_read_key / ffgky (fitsfile *fptr, int datatype, char *keyname, > DTYPE *value, char *comment, int *status) int fits_read_keyword / ffgkey (fitsfile *fptr, char *keyname, > char *value, char *comment, int *status) int fits_read_card / ffgcrd (fitsfile *fptr, char *keyname, > char *card, int *status) int fits_read_str / ffgstr (fitsfile *fptr, char *string, > char *card, int *status)

3 Return the nth header record in the CHU. The first keyword in the header is at keynum = 1; if keynum = 0 then these routines simply reset the internal CFITSIO p ointer to the b eginning of the header so that subsequent keyword op erations will start at the top of the header (e.g., prior to searching for keywords using wild cards in the keyword name). The first routine returns the entire 80-character header record (with trailing blanks truncated), while the second routine parses the record and returns the name, value, and comment fields as separate (blank truncated) character strings. If a NULL comment p ointer is given on input, then the comment string will not b e returned. int fits_read_record / ffgrec (fitsfile *fptr, int keynum, > char *card, int *status) int fits_read_keyn / ffgkyn (fitsfile *fptr, int keynum, > char *keyname, char *value, char *comment, int *status) 4 Return 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


5.4. HEADER KEYWORD READ/WRITE ROUTINES

37

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 ffgrec routine. Note that nexc may b e set = 0 if there are no keywords to b e excluded. This routine returns status = KEY NO EXIST if a matching keyword is not found. int fits_find_nextkey / ffgnxk (fitsfile *fptr, char **inclist, int ninc, char **exclist, int nexc, > char *card, int *status) 5 Return the physical units string from 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 null string is returned if no units are defined for the keyword. VELOCITY= 12.3 / [km/s] orbital speed

int fits_read_key_unit / ffgunt (fitsfile *fptr, char *keyname, > char *unit, int *status) 6 Concatenate the header keywords in the CHDU into a single long string of characters. This provides a convenient way of passing all or part of the header information in a FITS HDU to other subroutines. Each 80-character fixed-length keyword record is app ended to the output character string, in order, with no intervening separator or terminating characters. The last header record is terminated with a NULL character. These routine allocates memory for the returned character array, so the calling program must free the memory when finished. There are 2 related routines: fits hdr2str simply concatenates all the existing keywords in the header; fits convert hdr2str is similar, except that if the CHDU is a tile compressed image (stored in a binary table) then it will first convert that header back to that of the corresp onding normal FITS image b efore concatenating the keywords. Selected keywords may b e excluded from the returned character string. If the second parameter (nocomments) is TRUE (nonzero) then any COMMENT, HISTORY, or blank keywords in the header will not b e copied to the output string. The 'exclist' parameter may b e used to supply a list of keywords that are to b e excluded from the output character string. Wild card characters (*, ?, and #) may b e used in the excluded keyword names. If no additional keywords are to b e excluded, then set nexc = 0 and sp ecify NULL for the the **exclist parameter. int fits_hdr2str / ffhdr2str (fitsfile *fptr, int nocomments, char **exclist, int nexc, > char **header, int *nkeys, int *status) int fits_convert_hdr2str / ffcnvthdr2str (fitsfile *fptr, int nocomments, char **exclist, int nexc, > char **header, int *nkeys, int *status)


38

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

5.4.2

Keyword Writing Routines

1 Write a keyword of the appropriate data typ e into the CHU. The first routine simply app ends a new keyword whereas the second routine will up date the value and comment fields of the keyword if it already exists, otherwise it app ends a new keyword. Note that the address to the value, and not the value itself, must b e entered. The datatyp e parameter sp ecifies the data typ e of the keyword value with one of the following values: TSTRING, TLOGICAL (== int), TBYTE, TSHORT, TUSHORT, TINT, TUINT, TLONG, TLONGLONG, TULONG, TFLOAT, TDOUBLE. Within the context of this routine, TSTRING corresp onds to a 'char*' data typ e, i.e., a p ointer to a character array. A null p ointer may b e entered for the comment parameter in which case the keyword comment field will b e unmodified or left blank. int fits_write_key / ffpky (fitsfile *fptr, int datatype, char *keyname, DTYPE *value, char *comment, > int *status) int fits_update_key / ffuky (fitsfile *fptr, int datatype, char *keyname, DTYPE *value, char *comment, > int *status) 2 Write a keyword with a null or undefined value (i.e., the value field in the keyword is left blank). The first routine simply app ends a new keyword whereas the second routine will up date the value and comment fields of the keyword if it already exists, otherwise it app ends a new keyword. A null p ointer may b e entered for the comment parameter in which case the keyword comment field will b e unmodified or left blank. int fits_write_key_null / ffpkyu (fitsfile *fptr, char *keyname, char *comment, > int *status) int fits_update_key_null / ffukyu (fitsfile *fptr, char *keyname, char *comment, > int *status) 3 Write (app end) a COMMENT or HISTORY keyword to the CHU. The comment or history string will b e continued over multiple keywords if it is longer than 70 characters. int fits_write_comment / ffpcom (fitsfile *fptr, char *comment, > int *status) int fits_write_history / ffphis (fitsfile *fptr, char *history, > int *status) 4 Write the DATE keyword to the CHU. The keyword value will contain the current system date as a character string in 'yyyy-mm-ddThh:mm:ss' format. If a DATE keyword already exists in the header, then this routine will simply up date the keyword value with the current date.


5.4. HEADER KEYWORD READ/WRITE ROUTINES int fits_write_date / ffpdat (fitsfile *fptr, > int *status)

39

5 Write a user sp ecified keyword record into the CHU. This is a low­level routine which can b e used to write any arbitrary record into the header. The record must conform to the all the FITS format requirements. int fits_write_record / ffprec (fitsfile *fptr, char *card, > int *status) 6 Up date an 80-character record in the CHU. If a keyword with the input name already exists, then it is overwritten by the value of card. This could modify the keyword name as well as the value and comment fields. If the keyword doesn't already exist then a new keyword card is app ended to the header. int fits_update_card / ffucrd (fitsfile *fptr, char *keyname, char *card, > int *status) 7 Modify (overwrite) the comment field of an existing keyword. int fits_modify_comment / ffmcom (fitsfile *fptr, char *keyname, char *comment, > int *status) 8 Write the physical units string into 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

int fits_write_key_unit / ffpunt (fitsfile *fptr, char *keyname, char *unit, > int *status) 9 Rename an existing keyword, preserving the current value and comment fields. int fits_modify_name / ffmnam (fitsfile *fptr, char *oldname, char *newname, > int *status) 10 Delete a keyword record. The space 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 may b e used when sp ecifying the name of the keyword to b e deleted. The third routine deletes the (next) keyword that contains the literal character string sp ecified by the 'string' argument.


40

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_delete_record / ffdrec (fitsfile *fptr, int keynum,

> int *status)

int fits_delete_key / ffdkey (fitsfile *fptr, char *keyname, > int *status) int fits_delete_str / ffdstr (fitsfile *fptr, char *string, > int *status)

5.5

Primary Array or IMAGE Extension I/O Routines

These routines read or write data values in the primary data array (i.e., the first HDU in a FITS file) or an IMAGE extension. There are also routines to get information ab out the data typ e and size of the image. Users should also read the following chapter on the CFITSIO iterator function which provides a more `ob ject oriented' method of reading and writing images. The iterator function is a little more complicated to use, but the advantages are that it usually takes less code to p erform the same op eration, and the resulting program often runs faster b ecause the FITS files are read and written using the most efficient block size. C programmers should note calls, is more similar to the FITS image has NAXIS1 = image should b e declared as that the ordering of arrays in FITS files, and hence in all the CFITSIO dimensionality of arrays in Fortran rather than C. For instance if a 100 and NAXIS2 = 50, then a 2-D array just large enough to hold the array[50][100] and not as array[100][50].

The `datatyp e' parameter sp ecifies the data typ e of the `nulval' and `array' p ointers and can have one of the following values: TBYTE, TSBYTE, TSHORT, TUSHORT, TINT, TUINT, TLONG, TLONGLONG, TULONG, TFLOAT, TDOUBLE. Automatic data typ e conversion is p erformed if the data typ e of the FITS array (as defined by the BITPIX keyword) differs from that sp ecified by 'datatyp e'. The data values are also automatically scaled by the BSCALE and BZERO keyword values as they are b eing read or written in the FITS array. 1 Get the data typ e or equivalent data typ e of the image. The first routine returns the physical data typ e of the FITS image, as given by the BITPIX keyword, with allowed values of BYTE IMG (8), SHORT IMG (16), LONG IMG (32), LONGLONG IMG (64), FLOAT IMG (-32), and DOUBLE IMG (-64). The second routine is similar, except that if the image pixel values are scaled, with non-default values for the BZERO and BSCALE keywords, then the 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 FLOAT IMG. Similarly if BITPIX = 16, BSCALE = 1, and BZERO = 32768, then the the pixel values span the range of an unsigned short integer and the returned data typ e will b e USHORT IMG. int fits_get_img_type / ffgidt (fitsfile *fptr, > int *bitpix, int *status) int fits_get_img_equivtype / ffgiet (fitsfile *fptr, > int *bitpix, int *status)


5.5. PRIMARY ARRAY OR IMAGE EXTENSION I/O ROUTINES

41

2 Get the numb er of dimensions, and/or the size of each dimension in the image . The numb er of axes in the image is given by naxis, and the size of each dimension is given by the naxes array (a maximum of maxdim dimensions will b e returned). int fits_get_img_dim / ffgidm (fitsfile *fptr, > int *naxis, int *status) int fits_get_img_size / ffgisz (fitsfile *fptr, int maxdim, > long *naxes, int *status) int fits_get_img_sizell / ffgiszll (fitsfile *fptr, int maxdim, > LONGLONG *naxes, int *status) int fits_get_img_param / ffgipr (fitsfile *fptr, int maxdim, > int *bitpix, int *naxis, long *naxes, int *status) int fits_get_img_paramll / ffgiprll (fitsfile *fptr, int maxdim, > int *bitpix, int *naxis, LONGLONG *naxes, int *status) 3 Create a new primary array or IMAGE extension with a sp ecified data typ e and size. If the FITS file is currently empty then a primary array is created, otherwise a new IMAGE extension is app ended to the file. int fits_create_img / ffcrim ( fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status) int fits_create_imgll / ffcrimll ( fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status) 4 Copy an n-dimensional image in a particular row and column of a binary table (in a vector column) to or from a primary array or image extension. The 'cell2image' routine will app end a new image extension (or primary array) to the output file. Any WCS keywords associated with the input column image will b e translated into the appropriate form for an image extension. Any other keywords in the table header that are not sp ecifically related to defining the binary table structure or to other columns in the table will also b e copied to the header of the output image. The 'image2cell' routine will copy the input image into the sp ecified row and column of the current binary table in the output file. The binary table HDU must exist b efore calling this routine, but it may b e empty, with no rows or columns of data. The sp ecified column (and row) will b e created if it does not already exist. The 'copykeyflag' parameter controls which keywords are copied from the input image to the header of the output table: 0 = no keywords will b e copied, 1 = all keywords will b e copied (except those keywords that would b e invalid in the table header), and 2 = copy only the WCS keywords.


42

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_copy_cell2image (fitsfile *infptr, fitsfile *outfptr, char *colname, long rownum, > int *status) int fits_copy_image2cell (fitsfile *infptr, fitsfile *outfptr, char *colname, long rownum, int copykeyflag > int *status)

5 Write a rectangular subimage (or the whole image) to the FITS data array. The fpixel and lpixel arrays give the coordinates of the first (lower left corner) and last (upp er right corner) pixels in FITS image to b e written to. int fits_write_subset / ffpss (fitsfile *fptr, int datatype, long *fpixel, long *lpixel, DTYPE *array, > int *status) 6 Write pixels into the FITS data array. 'fpixel' is an array of length NAXIS which gives the coordinate of the starting pixel to b e written to, such that fpixel[0] is in the range 1 to NAXIS1, fpixel[1] is in the range 1 to NAXIS2, etc. The first pair of routines simply writes the array of pixels to the FITS file (doing data typ e conversion if necessary) whereas the second routines will substitute the appropriate FITS null value for any elements which are equal to the input value of nulval (note that this parameter gives the address of the null value, not the null value itself ). For integer FITS arrays, the FITS null value is defined by the BLANK keyword (an error is returned if the BLANK keyword doesn't exist). For floating p oint FITS arrays the sp ecial IEEE NaN (Not-a-Numb er) value will b e written into the FITS file. If a null p ointer is entered for nulval, then the null value is ignored and this routine b ehaves the same as fits write pix. int fits_write_pix / ffppx (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements, DTYPE *array, int *status); int fits_write_pixll / ffppxll (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements, DTYPE *array, int *status); int fits_write_pixnull / ffppxn (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements, DTYPE *array, DTYPE *nulval, > int *status); int fits_write_pixnullll / ffppxnll (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements, DTYPE *array, DTYPE *nulval, > int *status); 7 Set FITS data array elements equal to the appropriate null pixel value. For integer FITS arrays, the FITS null value is defined by the BLANK keyword (an error is returned if the BLANK


5.5. PRIMARY ARRAY OR IMAGE EXTENSION I/O ROUTINES

43

keyword doesn't exist). For floating p oint FITS arrays the sp ecial IEEE NaN (Not-a-Numb er) value will b e written into the FITS file. Note that 'firstelem' is a scalar giving the offset to the first pixel to b e written in the equivalent 1-dimensional array of image pixels. int fits_write_null_img / ffpprn (fitsfile *fptr, LONGLONG firstelem, LONGLONG nelements, > int *status) 8 Read a rectangular subimage (or the whole image) from the FITS data array. The fpixel and lpixel arrays give the coordinates of the first (lower left corner) and last (upp er right corner) pixels to b e read from the FITS image. Undefined FITS array elements will b e returned with a value = *nullval, (note that this parameter gives the address of the null value, not the null value itself ) unless nulval = 0 or *nulval = 0, in which case no checks for undefined pixels will b e p erformed. int fits_read_subset / ffgsv (fitsfile *fptr, int datatype, long *fpixel, long *lpixel, long *inc, DTYPE *nulval, > DTYPE *array, int *anynul, int *status) 9 Read pixels from the FITS data array. 'fpixel' is the starting pixel location and is an array of length NAXIS such that fpixel[0] is in the range 1 to NAXIS1, fpixel[1] is in the range 1 to NAXIS2, etc. The nelements parameter sp ecifies the numb er of pixels to read. If fpixel is set to the first pixel, and nelements is set equal to the NAXIS1 value, then this routine would read the first row of the image. Alternatively, if nelements is set equal to NAXIS1 * NAXIS2 then it would read an entire 2D image, or the first plane of a 3-D datacub e. The first 2 routines will return any undefined pixels in the FITS array equal to the value of *nullval (note that this parameter gives the address of the null value, not the null value itself ) unless nulval = 0 or *nulval = 0, in which case no checks for undefined pixels will b e p erformed. The second 2 routines are similar except that any undefined pixels will have the corresp onding nullarray element set equal to TRUE (= 1). int fits_read_pix / ffgpxv (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements, DTYPE *nulval, > DTYPE *array, int *anynul, int *status) int fits_read_pixll / ffgpxvll (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements, DTYPE *nulval, > DTYPE *array, int *anynul, int *status) int fits_read_pixnull / ffgpxf (fitsfile *fptr, int datatype, long *fpixel, LONGLONG nelements, > DTYPE *array, char *nullarray, int *anynul, int *status) int fits_read_pixnullll / ffgpxfll (fitsfile *fptr, int datatype, LONGLONG *fpixel, LONGLONG nelements, > DTYPE *array, char *nullarray, int *anynul, int *status)


44

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

10 Copy a rectangular section of an image and write it to a new FITS primary image or image extension. The new image HDU is app ended to the end of the output file; all the keywords in the input image will b e copied to the output image. The common WCS keywords will b e up dated if necessary to corresp ond to the coordinates of the section. The format of the section expression is same as sp ecifying an image section using the extended file name syntax (see "Image Section" in Chapter 10). (Examples: "1:100,1:200", "1:100:2, 1:*:2", "*, -*"). int fits_copy_image_section / ffcpimg (fitsfile *infptr, fitsfile *outfptr, char *section, int *status)

5.6

Image Compression

CFITSIO transparently supp orts the 2 methods of image compression describ ed b elow. 1) The entire FITS file may b e externally compressed with the gzip or Unix compress utility programs, producing a *.gz or *.Z file, resp ectively. When reading compressed files of this typ e, CFITSIO first uncompresses the entire file into memory b efore p erforming the requested read op erations. Output files can b e directly written in the gzip compressed format if the user-sp ecified filename ends with `.gz'. In this case, CFITSIO initially writes the uncompressed file in memory and then compresses it and writes it to disk when the FITS file is closed, thus saving user disk space. Read and write access to these compressed FITS files is generally quite fast since all the I/O is p erformed in memory; the main limitation with this technique is that there must b e enough available memory (or swap space) to hold the entire uncompressed FITS file. 2) CFITSIO also supp orts the FITS tiled image compression convention in which the image is sub divided into a grid of rectangular tiles, and each tile of pixels is individually compressed. The details of this FITS compression convention are describ ed at the FITS Supp ort Office web site at http://fits.gsfc.nasa.gov/fits registry.html Basically, the compressed image tiles are stored in rows of a variable length array column in a FITS binary table, however CFITSIO recognizes that this binary table extension contains an image and treats it as if it were an IMAGE extension. This tile-compressed format is esp ecially well suited for compressing very large images b ecause a) the FITS header keywords remain uncompressed for rapid read access, and b ecause b) it is p ossible to extract and uncompress sections of the image without having to uncompress the entire image. This format is also much more effective in compressing floating p oint images than simply compressing the image using gzip or compress b ecause it approximates the floating p oint values with scaled integers which can then b e compressed more efficiently. Currently CFITSIO supp orts 3 general purp ose compression algorithms plus one other sp ecialpurp ose compression technique that is designed for data masks with p ositive integer pixel values. The 3 general purp ose algorithms are GZIP, Rice, and HCOMPRESS, and the sp ecial purp ose algorithm is the IRAF pixel list compression technique (PLIO). In principle, any numb er of other compression algorithms could also b e supp orted by the FITS tiled image compression convention. The FITS the GZIP, HCOMPR 16 rows of image can b e Rice, and PLI ESS algorithm the image p er sub divided into any desired rectangular grid of compression tiles. With O algorithms, the default is to take each row of the image as a tile. The is inherently 2-dimensional in nature, so the default in this case is to take tile. In most cases it makes little difference what tiling pattern is used, so


5.6. IMAGE COMPRESSION

45

the default tiles are usually adequate. In the case of very small images, it could b e more efficient to compress the whole image as a single tile. Note that the image dimensions are not required to b e an integer multiple of the tile dimensions; if not, then the tiles at the edges of the image will b e smaller than the other tiles. The 4 supp orted image compression algorithms are all 'loss-less' when applied to integer FITS images; the pixel values are preserved exactly with no loss of information during the compression and uncompression process. In addition, the HCOMPRESS algorithm supp orts a 'lossy' compression mode that will produce larger amount of image compression. This is achieved by sp ecifying a non-zero value for the HCOMPRESS "scale" parameter. Since the amount of compression that is achieved dep ends directly on the RMS noise in the image, it is usually more convention to sp ecify the HCOMPRESS scale factor relative to the RMS noise. Setting s = 2.5 means use a scale factor that is 2.5 times the calculated RMS noise in the image tile. In some cases it may b e desirable to sp ecify the exact scaling to b e used, instead of sp ecifying it relative to the calculated noise value. This may b e done by sp ecifying the negative of desired scale value (typically in the range -2 to -100). Very high compression factors (of 100 or more) can b e achieved by using large HCOMPRESS scale values, however, this can produce undesirable "blocky" artifacts in the compressed image. A variation of the HCOMPRESS algorithm (called HSCOMPRESS) can b e used in this case to apply a small amount of smoothing of the image when it is uncompressed to help cover up these artifacts. This smoothing is purely cosmetic and does not cause any significant change to the image pixel values. Floating p oint FITS images (which have BITPIX = -32 or -64) usually contain too much "noise" in the least significant bits of the mantissa of the pixel values to b e effectively compressed with any lossless algorithm. Consequently, floating p oint images are first quantized into scaled integer pixel values (and thus throwing away much of the noise) b efore b eing compressed with the sp ecified algorithm (either GZIP, Rice, or HCOMPRESS). This technique produces much higher compression factors than simply using the GZIP utility to externally compress the whole FITS file, but it also means that the original floating value pixel values are not exactly preserved. When done prop erly, this integer scaling technique will only discard the insignificant noise while still preserving all the real information in the image. The amount of precision that is retained in the pixel values is controlled by the "quantization level" parameter, q. Larger values of q will result in compressed images whose pixels more closely match the floating p oint pixel values, but at the same time the amount of compression that is achieved will b e reduced. Users should exp eriment with different values for this parameter to determine the optimal value that preserves all the useful information in the image, without needlessly preserving all the "noise" which will hurt the compression efficiency. The default value for the quantization scale factor is 16., which means that scaled integer pixel values will b e quantized such that the difference b etween adjacent integer values will b e 1/16th of the noise level in the image background. CFITSIO uses an optimized algorithm to accurately estimate the noise in the image. As an example, if the RMS noise in the background pixels of an image = 32.0, then the spacing b etween adjacent scaled integer pixel values will equal 2.0 by default. Note that the RMS noise is indep endently calculated for each tile of the image, so the resulting integer scaling factor may fluctuate slightly for each tile. In some cases it may b e desirable to sp ecify the exact quantization level to b e used, instead of sp ecifying it relative to the calculated noise value. This may b e done by sp ecifying the negative of desired quantization level for the value of q. In the previous example, one could sp ecify q = -2.0 so that the quantized integer levels


46

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

differ by 2.0. Larger negative values for q means that the levels are more coarsely spaced, and will produce higher compression factors. There are 2 methods for sp ecifying all the parameters needed to write a FITS image in the tile compressed format. The parameters may either b e sp ecified at run time as part of the file name of the output compressed FITS file, or the writing program may call a set of help er CFITSIO subroutines that are provided for sp ecifying the parameter values, as describ ed b elow: 1) At run time, 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 by enclosing the compression parameters in square brackets following the root disk file name in the following format: [compress NAME T1,T2; q QLEVEL, s HSCALE] where NAME =a m T1,T2 = t QLEVEL = q HSCALE = H l a i u C g y l a O orithm name: GZIP, Rice, HCOMPRESS, HSCOMPRSS or PLIO be abbreviated to the first letter (or HS for HSCOMPRESS) e dimension (e.g. 100,100 for square tiles 100 pixels wide) ntization level for floating point FITS images MPRESS scale factor; default = 0 which is lossless.

Here are a few examples of this extended syntax: myfile.fit[compress] - use the default compression algorithm (Rice) and the default tile size (row by row) Z i L C I c I O P e O M ] - use the specified compression algorithm; ] only the first letter of the algorithm ] name is required. P] - use Rice and 100 x 100 pixel tiles

m m m m

y y y y

f f f f

i i i i

l l l l

e e e e

. . . .

f f f f

i i i i

t t t t

[ [ [ [

c c c c

o o o o

m m m m

p p p p

r r r r

e e e e

s s s s

s s s s

G R P H

myfile.fit[compress R 100,100]

myfile.fit[compress R; q 10.0] - quantization level = (RMS-noise) / 10. myfile.fit[compress HS; s 2.0] - HSCOMPRESS (with smoothing) and scale = 2.0 * RMS-noise 2) Before calling the CFITSIO routine to write the image header keywords (e.g., fits create image) the programmer can call the routines describ ed b elow to sp ecify the compression algorithm and the tiling pattern that is to b e used. There are routines for sp ecifying the various compression parameters and similar routines to return the current values of the parameters: int fits_set_compression_type(fitsfile *fptr, int comptype, int *status) int fits_set_tile_dim(fitsfile *fptr, int ndim, long *tilesize, int *status) int fits_set_quantize_level(fitsfile *fptr, float qlevel, int *status)


5.6. IMAGE COMPRESSION int fits_set_hcomp_scale(fitsfile *fptr, float scale, int *status) int fits_set_hcomp_smooth(fitsfile *fptr, int smooth, int *status) Set smooth = 1 to apply smoothing when uncompressing the image i i i i i n n n n n t t t t t f f f f f i i i i i t t t t t s s s s s _ _ _ _ _ g g g g g e e e e e t t t t t _ _ _ _ _ c t q h h o i u c c m l a o o p e n m m r _ t p p e d i _ _ s i z s s s m e c m i ( _ a o o f l l o n i e e t _ t v ( h t s e f ( y f l i f p i ( t i e l f s t (fitsfile *fptr, int *comptype, int *status) e *fptr, int ndim, long *tilesize, int *status) itsfile *fptr, float *level, int *status) file *fptr, float *scale, int *status) sfile *fptr, int *smooth, int *status)

47

4 symb olic constants are defined for use as the value of the `comptyp e' parameter: GZIP 1, RICE 1, HCOMPRESS 1 or PLIO 1. Entering NULL for comptyp e will turn off the tile-compression and cause normal FITS images to b e written. No sp ecial action is required by software when read tile-compressed images b ecause all the CFITSIO routines that read normal uncompressed FITS images also transparently read images in the tilecompressed format; CFITSIO essentially treats the binary table that contains the compressed tiles as if it were an IMAGE extension. The following 2 routines are available for compressing or or decompressing an image: int fits_img_compress(fitsfile *infptr, fitsfile *outfptr, int *status); int fits_img_decompress (fitsfile *infptr, fitsfile *outfptr, int *status); Before calling the compression routine, the compression parameters must first b e defined in one of the 2 way describ ed in the previous paragraphs. There is also a routine to determine if the current HDU contains a tile compressed image (it returns 1 or 0): int fits_is_compressed_image(fitsfile *fptr, int *status); A small example program called 'imcopy' is included with CFITSIO that can b e used to compress (or uncompress) any FITS image. This program can b e used to exp eriment with the various compression options on existing FITS images as shown in these examples: 1) imcopy infile.fit 'outfile.fit[compress]' This will use the default compression algorithm (Rice) and the default tile size (row by row) 2) imcopy infile.fit 'outfile.fit[compress GZIP]' T t R n h i i a i l c m s e e e will us size (r , GZIP, needs t e o a o th wb nd be e GZIP y row) PLIO. speci compression algorithm and the default . The allowed compression algorithms are Only the first letter of the algorithm fied.


48 3)

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES imcopy infile.fit 'outfile.fit[compress G 100,100]' This will use the GZIP compression algorithm and 100 X 100 pixel tiles.

4)

imcopy infile.fit 'outfile.fit[compress R 100,100; q 10.0]' This will use the Rice compression algorithm, 100 X 100 pixel tiles, and quantization level = RMSnoise / 10.0 (assuming the input image has a floating point data type).

5)

imcopy infile.fit outfile.fit If the input file is in tile-compressed format, then it will be uncompressed to the output file. Otherwise, it simply copies the input image to the output image.

6)

imcopy 'infile.fit[1001:1500,2001:2500]' outfile.fit This extracts a 500 X 500 pixel section of the much larger input image (which may be in tile-compressed format). The output is a normal uncompressed FITS image.

7)

imcopy 'infile.fit[1001:1500,2001:2500]' outfile.fit.gz Same as above, except the output file is externally compressed using the gzip algorithm.

5.7

ASCI I and Binary Table Routines

These routines p erform read and write op erations on columns of data in FITS ASCI I or Binary tables. Note that in the following discussions, the first row and column in a table is at p osition 1 not 0. Users should also read the following chapter on the CFITSIO iterator function which provides a more `ob ject oriented' method of reading and writing table columns. The iterator function is a little more complicated to use, but the advantages are that it usually takes less code to p erform the same op eration, and the resulting program often runs faster b ecause the FITS files are read and written using the most efficient block size.

5.7.1

Create New Table

1 Create a new ASCI I or bintable table extension. If the FITS file is currently empty then a dummy primary array will b e created b efore app ending the table extension to it. The tbltyp e


5.7. ASCI I AND BINARY TABLE ROUTINES

49

parameter defines the typ e of table and can have values of ASCI I TBL or BINARY TBL. The naxis2 parameter gives the initial numb er of rows to b e created in the table, and should normally b e set = 0. CFITSIO will automatically increase the size of the table as additional rows are written. A non-zero numb er of rows may b e sp ecified to reserve space for that many rows, even if a fewer numb er of rows will b e written. The tunit and extname parameters are optional and a null p ointer may b e given if they are not defined. The FITS Standard recommends that only letters, digits, and the underscore character b e used in column names (the ttyp e parameter) with no emb edded spaces. Trailing blank characters are not significant.

int fits_create_tbl / ffcrtb (fitsfile *fptr, int tbltype, LONGLONG naxis2, int tfields, char *ttype[], char *tform[], char *tunit[], char *extname, int *status)

5.7.2

Column Information Routines

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. int fits_get_num_rows / ffgnrw (fitsfile *fptr, > long *nrows, int *status); int fits_get_num_rowsll / ffgnrwll (fitsfile *fptr, > LONGLONG *nrows, int *status); int fits_get_num_cols / ffgncl (fitsfile *fptr, > int *ncols, int *status); 2 Get the table column numb er (and name) of the column whose name matches an input template name. If casesen = CASESEN then the column name match will b e case-sensitive, whereas if casesen = CASEINSEN then the case will b e ignored. As a general rule, the column names should b e treated as case INsensitive. The input column name template may b e either the exact name of the column to b e searched for, or it may contain wild card characters (*, ?, or #), or it may contain the integer numb er of the desired column (with the first column = 1). The `*' wild card 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). 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 COL NOT UNIQUE as a warning that a unique match was not found. To find the other cases that match the template, call the routine again leaving the input status value equal to COL NOT UNIQUE and the next matching name will then b e returned. Rep eat this process until a status = COL NOT FOUND is returned. The FITS Standard recommends that only letters, digits, and the underscore character b e used in column names (with no emb edded spaces). Trailing blank characters are not significant.


50

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_get_colnum / ffgcno (fitsfile *fptr, int casesen, char *templt, > int *colnum, int *status) int fits_get_colname / ffgcnn (fitsfile *fptr, int casesen, char *templt, > char *colname, int *colnum, int *status)

3 Return the data typ e, vector rep eat value, and the width in bytes of a column in an ASCI I or binary table. Allowed values for the data typ e in ASCI I tables are: TSTRING, TSHORT, TLONG, TFLOAT, and TDOUBLE. Binary tables also supp ort these typ es: TLOGICAL, TBIT, TBYTE, TCOMPLEX and TDBLCOMPLEX. The negative of the data typ e code value is returned if it is a variable length array column. Note that in the case of a 'J' 32-bit integer binary table column, this routine will return data typ e = TINT32BIT (which in fact is equivalent to TLONG). With most current C compilers, a value in a 'J' column has the same size as an 'int' variable, and may not b e equivalent to a 'long' variable, which is 64-bits long on an increasing numb er of compilers. The 'rep eat' parameter returns the vector rep eat count on the binary table TFORMn keyword value. (ASCI I table columns always have rep eat = 1). The 'width' parameter returns the width in bytes of a single column element (e.g., a '10D' binary table column will have width = 8, an ASCI I table 'F12.2' column will have width = 12, and a binary table'60A' character string column will have width = 60); Note that CFITSIO supp orts the local convention for sp ecifying arrays of fixed length 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 means that each row of the table contains 5 12-character substrings within the 60-character field, and thus in this case this routine will return typ ecode = TSTRING, 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 numb er of substrings in any binary table character string field can b e calculated by (rep eat/width). A null p ointer may b e given for any of the output parameters that are not needed. The second routine, fit get eqcoltyp e 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 TUSHORT, not TSHORT. Similarly, if a column has TTYPEn = '1I' and TSCALn = 0.12, then the returned typ ecode will b e TFLOAT. int fits_get_coltype / ffgtcl (fitsfile *fptr, int colnum, > int *typecode, long *repeat, long *width, int *status) int fits_get_coltypell / ffgtclll


5.7. ASCI I AND BINARY TABLE ROUTINES (fitsfile *fptr, int colnum, > int *typecode, LONGLONG *repeat, LONGLONG *width, int *status) int fits_get_eqcoltype / ffeqty (fitsfile *fptr, int colnum, > int *typecode, long *repeat, long *width, int *status) int fits_get_eqcoltypell / ffeqtyll (fitsfile *fptr, int colnum, > int *typecode, LONGLONG *repeat, LONGLONG *width, int *status)

51

4 Return the display width of a column. This is the length of the string that will b e returned by the fits read col routine 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.

int fits_get_col_display_width / ffgcdw (fitsfile *fptr, int colnum, > int *dispwidth, int *status) 5 Return the numb er of and size of the dimensions of a table column in a binary table. Normally this information is given by the TDIMn keyword, but if this keyword is not present then this routine returns naxis = 1 and naxes[0] equal to the rep eat count in the TFORM keyword. int fits_read_tdim / ffgtdm (fitsfile *fptr, int colnum, int maxdim, > int *naxis, long *naxes, int *status) int fits_read_tdimll / ffgtdmll (fitsfile *fptr, int colnum, int maxdim, > int *naxis, LONGLONG *naxes, int *status) 6 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 fits read tdim. int fits_decode_tdim / ffdtdm (fitsfile *fptr, char *tdimstr, int colnum, int maxdim, > int *naxis, long *naxes, int *status) int fits_decode_tdimll / ffdtdmll (fitsfile *fptr, char *tdimstr, int colnum, int maxdim, > int *naxis, LONGLONG *naxes, int *status) 7 Write 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.


52

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_write_tdim / ffptdm (fitsfile *fptr, int colnum, int naxis, long *naxes, > int *status) int fits_write_tdimll / ffptdmll (fitsfile *fptr, int colnum, int naxis, LONGLONG *naxes, > int *status)

5.7.3

Routines to Edit Rows or Columns

1 Insert or delete rows in an ASCI I or binary table. When inserting rows all the rows following row FROW are shifted down by NROWS rows; if FROW = 0 then the blank rows are inserted at the b eginning of the table. The first delete routine deletes NROWS consecutive rows starting with row FIRSTROW. The second delete routine takes an input string that lists the rows or row ranges (e.g., '5-10,12,20-30'), whereas the third delete routine takes an input integer array that sp ecifies each individual row to b e deleted. In b oth latter cases, the input list of rows to delete must b e sorted in ascending order. These routines up date the NAXIS2 keyword to reflect the new numb er of rows in the table. int fits_insert_rows / ffirow (fitsfile *fptr, LONGLONG firstrow, LONGLONG nrows, > int *status) int fits_delete_rows / ffdrow (fitsfile *fptr, LONGLONG firstrow, LONGLONG nrows, > int *status) int fits_delete_rowrange / ffdrrg (fitsfile *fptr, char *rangelist, > int *status) int fits_delete_rowlist / ffdrws (fitsfile *fptr, long *rowlist, long nrows, > int *status) int fits_delete_rowlistll / ffdrwsll (fitsfile *fptr, LONGLONG *rowlist, LONGLONG nrows, > int *status) 2 Insert or delete column(s) in an ASCI I or binary table. When inserting, 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 shifted over to allow room for the new column(s). The index numb er on all the following keywords will b e incremented or decremented 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. int fits_insert_col / fficol (fitsfile *fptr, int colnum, char *ttype, char *tform, > int *status)


5.7. ASCI I AND BINARY TABLE ROUTINES int fits_insert_cols / fficls (fitsfile *fptr, int colnum, int ncols, char **ttype, char **tform, > int *status) int fits_delete_col / ffdcol(fitsfile *fptr, int colnum, > int *status)

53

3 Copy a column from one HDU to another (or to the same HDU). If create col = 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 data typ e). If outcolnum is greater than the numb er of column in the table, then the new column will b e app ended to the end of the table. Note that the first column in a table is at colnum = 1. The standard indexed keywords that related to the column (e.g., TDISPn, TUNITn, TCRPXn, TCDLTn, etc.) will also b e copied. int fits_copy_col / ffcpcl (fitsfile *infptr, fitsfile *outfptr, int incolnum, int outcolnum, int create_col, > int *status); 4 Copy 'nrows' consecutive rows from one table to another, b eginning with row 'firstrow'. These rows will b e app ended to any existing rows in the output table. Note that the first row in a table is at row = 1. int fits_copy_rows / ffcprw (fitsfile *infptr, fitsfile *outfptr, LONGLONG firstrow, LONGLONG nrows, > int *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. int fits_modify_vector_len / ffmvec (fitsfile *fptr, int colnum, LONGLONG newveclen, > int *status)

5.7.4

Read and Write Column Data Routines

The following routines write or read data values in the current ASCI I or binary table extension. If a write op eration extends b eyond the current size of the table, then the numb er of rows in the table will automatically b e increased and the NAXIS2 keyword value will b e up dated. Attempts to read b eyond the end of the table will result in an error. Automatic data typ e conversion is p erformed for numerical data typ es (only) if the data typ e of the column (defined by the TFORMn keyword) differs from the data typ e of the array in the calling routine. ASCI I and binary tables supp ort the following data typ e values: TSTRING, TBYTE, TSBYTE, TSHORT, TUSHORT, TINT, TUINT, TLONG, TLONGLONG, TULONG, TFLOAT, or TDOUBLE. Binary tables also supp ort TLOGICAL (internally mapp ed to the `char' data typ e), TCOMPLEX, and TDBLCOMPLEX.


54

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

Individual bits in a binary table 'X' or 'B' column may b e read/written to/from a *char array by sp ecifying the TBIT datatyp e. The *char array will b e interpreted as an array of logical TRUE (1) or FALSE (0) values that corresp ond to the value of each bit in the FITS 'X' or 'B' column. Alternatively, the values in a binary table 'X' column may b e read/written 8 bits at a time to/from an array of 8-bit integers by sp ecifying the TBYTE datatyp e. Note that within the context of these routines, the TSTRING data typ e corresp onds to a C 'char**' data typ e, i.e., a p ointer to an array of p ointers to an array of characters. This is different from the keyword reading and writing routines where TSTRING corresp onds to a C 'char*' data typ e, i.e., a single p ointer to an array of characters. When reading strings from a table, the char arrays obviously must have b een allocated long enough to hold the whole FITS table string. Numerical data values are automatically scaled by the TSCALn and TZEROn keyword values (if they exist). In the case of binary tables with vector elements, the 'felem' parameter defines the starting element (b eginning with 1, not 0) within the cell (a cell is defined as the intersection of a row and a column and may contain a single value or a vector of values). The felem parameter is ignored when dealing with ASCI I tables. Similarly, in the case of binary tables the 'nelements' parameter sp ecifies the total numb er of vector values to b e read or written (continuing on subsequent rows if required) and not the numb er of table cells. 1 Write elements into an ASCI I or binary table column. The first routine simply writes the array of values to the FITS file (doing data typ e conversion if necessary) whereas the second routine will substitute the appropriate FITS null value for all elements which are equal to the input value of nulval (note that this parameter gives the address of nulval, not the null value itself ). For integer columns the FITS null value is defined by the TNULLn keyword (an error is returned if the keyword doesn't exist). For floating p oint columns the sp ecial IEEE NaN (Not-a-Numb er) value will b e written into the FITS file. If a null p ointer is entered for nulval, then the null value is ignored and this routine b ehaves the same as the first routine. The third routine simply writes undefined pixel values to the column. The fourth routine fills every column in the table with null values, in the sp ecified rows (ignoring any columns that do not have a defined null value). int fits_write_col / ffpcl (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, > int *status) int f ( L > its_ fits ONGL int write_colnull / ffpcn file *fptr, int datatype, int colnum, LONGLONG firstrow, ONG firstelem, LONGLONG nelements, DTYPE *array, DTYPE *nulval, *status)

int fits_write_col_null / ffpclu (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, > int *status)


5.7. ASCI I AND BINARY TABLE ROUTINES int fits_write_nullrows / ffprwu (fitsfile *fptr, LONGLONG firstrow, LONGLONG nelements, > int *status)

55

2 Read elements from an ASCI I or binary table column. The data typ e parameter sp ecifies the data typ e of the `nulval' and `array' p ointers; Undefined array elements will b e returned with a value = *nullval, (note that this parameter gives the address of the null value, not the null value itself ) unless nulval = 0 or *nulval = 0, in which case no checking for undefined pixels will b e p erformed. The second routine is similar except that any undefined pixels will have the corresp onding nullarray element set equal to TRUE (= 1). Any column, regardless of it's intrinsic data typ 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 data typ e of the column. The length of the returned strings (not including the null terminating character) can b e determined with the fits get col display width 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. int fits_read_col / ffgcv (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, DTYPE *nulval, DTYPE *array, int *anynul, int *status) int fits_read_colnull / ffgcf (fitsfile *fptr, int datatype, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, char *nullarray, int *anynul, int *status)

5.7.5

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 `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


56

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

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. Up on return, *n good rows contains the numb er of rows that evaluate to TRUE. int fits_find_rows / fffrow (fitsfile *fptr, char *expr, long firstrow, long nrows, > long *n_good_rows, char *row_status, int *status) 2 Find the first row which satisfies the input b oolean expression int fits_find_first_row / ffffrw (fitsfile *fptr, char *expr, > long *rownum, int *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 output table is not empty, then this routine will app end the new selected rows after the existing rows. If the files are the same, delete the FALSE rows (preserve the TRUE rows). int fits_select_rows / ffsrow (fitsfile *infptr, fitsfile *outfptr,

char *expr,

> int *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 fits test expr to obtain the dimensions of the results. int fits_calc_rows / ffcrow (fitsfile *fptr, int datatype, char *expr, long firstrow, long nelements, void *nulval, > void *array, int *anynul, int *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 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.


5.7. ASCI I AND BINARY TABLE ROUTINES int fits_calculator / ffcalc (fitsfile *infptr, char *expr, fitsfile *outfptr, char *parName, char *parInfo, > int *status)

57

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. int fit (fi ch > s_calculator_rng / ffcalc_rng tsfile *infptr, char *expr, fitsfile *outfptr, char *parName, ar *parInfo, int nranges, long *firstrow, long *lastrow int *status)

7 Evaluate the given expression and return information on the result. int fits_test_expr / fftexp (fitsfile *fptr, char *expr, > int *datatype, long *nelem, int *naxis, long *naxes, int *status)

5.7.6

Column Binning or Histogramming Routines

The following routines may b e useful when p erforming histogramming op erations on column(s) of a table to generate an image in a primary array or image extension. 1 Calculate the histogramming parameters (min, max, and bin size for each axis of the histogram, based on a variety of p ossible input parameters. If the input names of the columns to b e binned are null, then the routine will first look for the CPREF = "NAME1, NAME2, ..." keyword which lists the preferred columns. If not present, then the routine will assume the column names X, Y, Z, and T for up to 4 axes (as sp ecified by the NAXIS parameter). MININ and MAXIN are input arrays that give the minimum and maximum value for the histogram, along each axis. Alternatively, the name of keywords that give the min, max, and binsize may b e give with the MINNAME, MAXNAME, and BINNAME array parameters. If the value = DOUBLENULLVALUE and no keyword names are given, then the routine will use the TLMINn and TLMAXn keywords, if present, or the actual min and/or max values in the column. BINSIZEIN is an array giving the binsize along each axis. If the value = DOUBLENULLVALUE, and a keyword name is not sp ecified with BINNAME, then this routine will first look for the TDBINn keyword, or else will use a binsize = 1, or a binsize that produces 10 histogram bins, which ever is smaller. int fits_calc_binning Input parameters: (fitsfile *fptr, /* IO - pointer to table to be binned int naxis, /* I - number of axes/columns in the binned image

*/ */


58 char colname[4][FLEN_ double *minin, /* double *maxin, /* double *binsizein, /* char minname[4][FLEN_ char maxname[4][FLEN_ char binname[4][FLEN_ Output parameters: int *colnum, /* O long *naxes, /* O float *amin, /* O float *amax, /* O float *binsize, /* O int *status) VA I I I VA VA VA -

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES LU LU LU LU c n l u w E], /* optiona optiona optiona E], /* I E], /* I E], /* I l m w p d u b e e t m e r r h n r b b o n o o o f I l l l lo up bi o o o optional wer bound per bound n size alo ptional ke ptional ke ptional ke b a h h b e c i i i c v v n y y y o a a g w w w lumn names lue for eac lue, for ea each axis ords for mi ords for ma ords for bi * axis * h axis * * * * size * / / / / / / /

h c n x n

o u o p i

umbers, to f bins in e und of the und of the histogram

binned */ h histogram a stogram axes stogram axes ns/pixels on

x * * e

is */ / / ach axis */

2 Copy the relevant keywords from the header of the table that is b eing binned, to the the header of the output histogram image. This will not copy the table structure keywords (e.g., NAXIS, TFORMn, TTYPEn, etc.) nor will it copy the keywords that apply to other columns of the table that are not used to create the histogram. This routine will translate the names of the World Coordinate System (WCS) keywords for the binned columns into the form that is need for a FITS image (e.g., the TCTYPn table keyword will b e translated to the CTYPEn image keyword). int fit (fi fi in in in in s t t t t t t _copy_pixlist2image sfile *infptr, /* I - pointer sfile *outfptr, /* I - pointer firstkey, /* I - first HD naxis, /* I - number o *colnum, /* I - numbers *status) /* IO - error s

t t U f o t

o input HDU */ o output HDU */ keyword to start with */ axes in the image */ f the columns to be binned atus */

*/

3 Write a set of default WCS keywords to the histogram header, IF the WCS keywords do not already exist. This will create a linear WCS where the coordinate typ es are equal to the original column names. int fits_write_keys_histo (fitsfile *fptr, /* I fitsfile *histptr, /* I int naxis, /* I int *colnum, /* I int *status)

p p n c

o o u o

i i m l

n n b u

t t e m

e e r n

r r o n

t t f u

o table t o output axes in mbers of

o h t t

be binned istogram image HDU he histogram image he binned columns

* * * *

/ / / /

4 Up date the WCS keywords in a histogram image header that give the location of the reference pixel (CRPIXn), and the pixel size (CDELTn), in the binned image.


5.8. UTILITY ROUTINES int fits_rebin_wcs (fitsfile *fptr, int naxis, float *amin, float *binsize, int *status)

59

/ / / /

* * * *

I I I I

-

p n f b

o u i i

i m r n

n b s n

t e t i

er ro pi ng

t f x f

o table to axes in th el include actor for e

b e i a

e binned histogram image n each axis ch axis

* * * *

/ / / /

5 Bin the values in the input table columns, and write the histogram array to the output FITS image (histptr). int fits_make_hist (fitsfile *fptr, /* I - pointer to table with X and Y cols; fitsfile *histptr, /* I - pointer to output FITS image int bitpix, /* I - datatype for image: 16, 32, -32, etc int naxis, /* I - number of axes in the histogram image long *naxes, /* I - size of axes in the histogram image int *colnum, /* I - column numbers (array length = naxis) float *amin, /* I - minimum histogram value, for each axis float *amax, /* I - maximum histogram value, for each axis float *binsize, /* I - bin size along each axis float weight, /* I - binning weighting factor (FLOATNULLVALUE /* for no weighting) int wtcolnum, /* I - keyword or col for weight (or NULL) int recip, /* I - use reciprocal of the weight? 0 or 1 char *selectrow, /* I - optional array (length = no. of /* rows in the table). If the element is true /* then the corresponding row of the table will /* be included in the histogram, otherwise the /* row will be skipped. Ingnored if *selectrow /* is equal to NULL. int *status)

* * * * * * * * * * * * * * * * * * *

/ / / / / / / / / / / / / / / / / / /

5.8
5.8.1

Utility Routines
File Checksum Routines

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


60

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

conference in Baltimore in Novemb er 1994 and a later revision in June 1995. See App endix B for the definition of the parameters used in these routines. 1 Compute and write the DATASUM and CHECKSUM keyword values for the CHDU into the current header. If the 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). int fits_write_chksum / ffpcks (fitsfile *fptr, > int *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. int fits_update_chksum / ffupck (fitsfile *fptr, > int *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 routine 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.

int fits_verify_chksum / ffvcks (fitsfile *fptr, > int *dataok, int *hduok, int *status) 4 Compute and return the checksum values for the CHDU without creating or modifying the CHECKSUM and DATASUM keywords. This routine is used internally by ffvcks, but may b e useful in other situations as well. int fits_get_chksum/ /ffgcks (fitsfile *fptr, > unsigned long *datasum, unsigned long *hdusum, int *status) 5 Encode a checksum value into a 16-character string. If complm is non-zero (true) then the 32-bit sum value will b e complemented b efore encoding. int fits_encode_chksum / ffesum (unsigned long sum, int complm, > char *ascii);


5.8. UTILITY ROUTINES

61

6 Decode a 16-character checksum string into a unsigned long value. If is non-zero (true). then the 32-bit sum value will b e complemented after decoding. The checksum value is also returned as the value of the function. unsigned long fits_decode_chksum / ffdsum (char *ascii, int complm, > unsigned long *sum);

5.8.2

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. See App endix B for the definition of the parameters used in these routines. 1 Get the current system date. C already provides standard library routines for getting the current date and time, but this routine is provided for compatibility with the Fortran FITSIO library. The returned year has 4 digits (1999, 2000, etc.) int fits_get_system_date/ffgsdt ( > int *day, int *month, int *year, int *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. int fits_get_system_time/ffgstm (> char *datestr, int *timeref, int *status) 3 Construct a sive, then the date s to always date string from the input date values. If the year is b etween 1900 and 1998, incluthe returned date string will have the old FITS format ('dd/mm/yy'), otherwise tring will have the new FITS format ('YYYY-MM-DD'). Use fits time2str instead return a date string using the new FITS format.

int fits_date2str/ffdt2s (int year, int month, int day, > char *datestr, int *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').


62

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_time2str/fftm2s (int year, int month, int day, int hour, int minute, double second, int decimals, > char *datestr, int *status)

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. Null p ointers may b e supplied for any unwanted output date parameters. int fits_str2date/ffs2dt (char *datestr, > int *year, int *month, int *day, int *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...'). Null p ointers may b e supplied for any unwanted output date and time parameters. int fits_str2time/ffs2tm (char *datestr, > int *year, int *month, int *day, int *hour, int *minute, double *second, int *status)

5.8.3

General Utility Routines

The following utility routines may b e useful for certain applications. 1 Return the revision numb er of the CFITSIO library. The revision numb er will b e incremented with each new release of CFITSIO. float fits_get_version / ffvers ( > float *version) 2 Write an 80-character message to the CFITSIO error stack. Application programs should not normally write to the stack, but there may b e some situations where this is desirable. void fits_write_errmsg / ffpmsg (char *err_msg) 3 Convert a character string to upp ercase (op erates in place). void fits_uppercase / ffupch (char *string) 4 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.


5.8. UTILITY ROUTINES The '#' character will match any consecutive string CASESEN = TRUE then the match will b e case sen will b e ignored if casesen = CASEINSEN = FALSE. b e TRUE if the 2 strings match, and EXACT will b e wildcard characters were used in the match). Both s length.

63 of decimal digits (0 - 9). If casesen = sitive, otherwise the case of the letters The returned MATCH parameter will TRUE if the match is exact (i.e., if no trings must b e 68 characters or less in

void fits_compare_str / ffcmps (char *templt, char *string, int casesen, > int *match, int *exact) 5 Split a string containing name tokens by a sequ by a comma and/or parentheses, brackets, the returned name. a list of names (typically file names or column names) into individual ence of calls to fits split names. The names in the list must b e delimited spaces. This routine ignores spaces and commas that occur within or curly brackets. It also strips any leading and trailing blanks from

This routine is similar to the ANSI C 'strtok' function: The first call to fits split names has a non-null input string. It finds the first name in the string and terminates it by overwriting the next character of the string with a null terminator and returns a p ointer to the name. Each subsequent call, indicated by a NULL value of the input string, returns the next name, searching from just past the end of the previous name. It returns NULL when no further names are found. char *fits_split_names(char *namelist) The following example shows how a string would b e split into 3 names: myfile[1][bin (x,y)=4], file2.fits ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^ 1st name 2nd name file3.fits ^^^^^^^^^^ 3rd name

6 Test that the keyword name contains only legal characters (A-Z,0-9, hyphen, and underscore) or that the keyword record contains only legal printable ASCI I characters int fits_test_keyword / fftkey (char *keyname, > int *status) int fits_test_record / fftrec (char *card, > int *status) 7 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 CFITSIO routines in which the returned value is not necessarily equal to the status value).


64

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES int fits_null_check / ffnchk (char *card, > int *status)

8 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. int fits_get_keyname / ffgknm (char *card, > char *keyname, int *keylength, int *status) 9 Parse a header keyword record, returning the value (as a literal character string) and comment strings. If the keyword has no value (columns 9-10 not equal to '= '), then a null value string is returned and the comment string is set equal to column 9 - 80 of the input string. int fits_parse_value / ffpsvc (char *card, > char *value, char *comment, int *status) 10 Construct an array indexed keyword name (ROOT + nnn). This routine app ends the sequence numb er to the root string to create a keyword name (e.g., 'NAXIS' + 2 = 'NAXIS2') int fits_make_keyn / ffkeyn (char *keyroot, int value, > char *keyname, int *status) 11 Construct a sequence keyword name (n + ROOT). This routine concatenates the sequence numb er to the front of the root string to create a keyword name (e.g., 1 + 'CTYP' = '1CTYP')

int fits_make_nkey / ffnkey (int value, char *keyroot, > char *keyname, int *status) 12 Determine the data typ e of a keyword value string. This routine parses the keyword value string to determine its data typ e. Returns 'C', 'L', 'I', 'F' or 'X', for character string, logical, integer, floating p oint, or complex, resp ectively. int fits_get_keytype / ffdtyp (char *value, > char *dtype, int *status) 13 Determine the integer data typ e of an integer keyword value string. The returned datatyp e value is the minimum integer datatyp e (starting from top of the following list and working down) required to store the integer value: Data Type TSBYTE: TBYTE: Range -128 to 127 128 to 255


5.8. UTILITY ROUTINES T T T T T S U I U L H S N I O O H T N N RT: ORT: T GLONG -3 3 -2 2 -9 2 2 1 1 2 7 7 4 4 2 6 6 7 7 3 8 8 4 4 3 to to 836 836 720 32767 65535 48 to 2147483647 48 to 4294967295 36854775808 to 9223372036854775807

65

The *neg parameter returns 1 if the input value is negative and returns 0 if it is non-negative. int fits_get_inttype / ffinttyp (char *value, > int *datatype, int *neg, int *status) 14 Return the class of an 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 CFITSIO routines that does not return a status value. 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 keywords defined in the the WCS papers, including: PEn, CUNITn, CRVALn, CRPIXn, CROTAn, CDELTn _is, PVj_ms, LONPOLEs, LATPOLEs YPn, TCTYns, TCUNIn, TCUNns, TCRVLn, TCRVns, TCRPXn, Pks, TCDn_k, TCn_ks, TPVn_m, TPn_ms, TCDLTn, TCROTn YPn, jCTYns, jCUNIn, jCUNns, jCRVLn, jCRVns, iCRPXn, Pns, jiCDn, jiCDns, jPVn_m, jPn_ms, jCDLTn, jCROTn j,m,n are integers, s is any letter) INOXs, EPOCH, MJD-OBSs, RADECSYS, RADESYSs, DATE-OBS MENT, HISTORY, (blank keyword) TINUE other keywords

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 W 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 C 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 S Y j T R T R , U M N l

int fits_get_keyclass / ffgkcl (char *card)


66

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES

15 Parse the 'TFORM' binary table column format string. This routine parses the input TFORM character string and returns the integer data typ e code, the rep eat count of the field, and, in the case of character string fields, the length of the unit string. See App endix B for the allowed values for the returned typ ecode parameter. A null p ointer may b e given for any output parameters that are not needed. int fits_binary_tform / ffbnfm (char *tform, > int *typecode, long *repeat, long *width, int *status) int fits_binary_tformll / ffbnfmll (char *tform, > int *typecode, LONGLONG *repeat, long *width, int *status) 16 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 data typ 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 data typ e codes are the same as for the binary table, with the following additional rules: integer columns that are b etween 1 and 4 characters wide are defined to b e short integers (code = TSHORT). Wider integer columns are defined to b e regular integers (code = TLONG). Similarly, Fixed decimal p oint columns (with TFORM = 'Fw.d') are defined to b e single precision reals (code = TFLOAT) if w is b etween 1 and 7 characters wide, inclusive. Wider 'F' columns will return a double precision data code (= TDOUBLE). 'Ew.d' format columns will have datacode = TFLOAT, and 'Dw.d' format columns will have datacode = TDOUBLE. A null p ointer may b e given for any output parameters that are not needed. int fits_ascii_tform / ffasfm (char *tform, > int *typecode, long *width, int *decimals, int *status) 17 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). int fits_get_tbcol / ffgabc (int tfields, char **tform, int space, > long *rowlen, long *tbcol, int *status) 18 Parse a template header record and return a formatted 80-character string suitable for app ending to (or deleting from) a FITS header file. This routine 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 fits write record, ffmcrd, or fits delete key routines to app end or modify a FITS header record.


5.8. UTILITY ROUTINES int fits_parse_template / ffgthd (char *templt, > char *card, int *keytype, int *status)

67

The input templt 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. A legal FITS keyword name may only contain the characters A-Z, 0-9, and '-' (minus sign) and underscore. This routine will automatically convert any lowercase characters to upp ercase in the output string. 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). - The VALUE token must b e separated from the KEYNAME token by one or more spaces and/or an '=' character. The data typ 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 and a '/' character. - One exception to the ab ove rules is that if the first non-blank character in the first 8 characters of 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 (without an equals sign b etween them) 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 ffmnam routine which will change the keyword name. The keytyp e output parameter indicates how the returned CARD string should b e interpreted: keytype -------2 interpretation ------------------------------------------------Rename the keyword with name = the first 8 characters of CARD to the new name given in characters 41 - 48 of CARD. delete the keyword with this name from the FITS header.

-1


68 0

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES append the CARD string to the FITS header if the keyword does not already exist, otherwise update the keyword value and/or comment field if is already exists. This is a HISTORY or COMMENT keyword; append it to the header END record; do not explicitly write it to the FITS file.

1 2

EXAMPLES: The following lines illustrate valid input template strings: I R E l S S s # # N V V v T A A a VAL 7 / This i L 34 L=-12.45E-03 l F / This is This L1 = 'Hello wo L2 '123.5' t l3 123+ / t he following t ATE he following t AME OBJECT s . / a i r h h e an integer keyword 6 / This is a This is a floating boolean keyword s a comment keyword ld' / this is a s is is also a string is is also a string mplate line deletes

floating point keyword point keyword in exponential notation w t k k t i r e e h t i y y e h a bl ng key word word w DATE ank keyword name word ith the value '123+ keyword '

VA VA va t D t N

emplate line modifies the NAME keyword to OBJECT

19 Translate a keyword name into a new name, based on a set of patterns. This routine is useful for translating keywords in cases such as adding or deleting columns in a table, or copying a column from one table to another, or extracting an array from a cell in a binary table column into an image extension. In these cases, it is necessary to translate the names of the keywords associated with the original table column(s) into the appropriate keyword name in the final file. For example, if column 2 is deleted from a table, then the value of 'n' in all the TFORMn and TTYPEn keywords for columns 3 and higher must b e decremented by 1. Even more complex translations are sometimes needed to convert the WCS keywords when extracting an image out of a table column cell into a separate image extension. The user passes an array of patterns to b e matched. Input pattern numb er i is pattern[i][0], and output pattern numb er i is pattern[i][1]. Keywords are matched against the input patterns. If a match is found then the keyword is re-written according to the output pattern. Order is imp ortant. The first match is accepted. The fastest match will b e made when templates with the same first character are group ed together. Several characters have sp ecial meanings: i n m a # ? ,j c g c n a single digits, preserved in outpu olumn number of one or more digits eneric number of one or more digit oordinate designator, preserved in umber of one or more digits ny character t template , preserved in output template s, preserved in output template output template


5.8. UTILITY ROUTINES * - only allowed in first character position, to match all keywords; only useful as last pattern in the list i, j, n, and m are returned by the routine.

69

For example, the input pattern "iCTYPn" will match "1CTYP5" (if n value is 5); the output pattern "CTYPEi" will b e re-written as "CTYPE1". Notice that "i" is preserved. The following output patterns are sp ecial: "-" - do not copy a keyword that matches the corresp onding input pattern "+" - copy the input unchanged The inrec string could b e just the 8-char keyword name, or the entire 80-char header record. Characters 9 - 80 in the input string simply get app ended to the translated keyword name. If n range = 0, then only keywords with 'n' equal to n value will b e considered as a pattern match. If n range = +1, then all values of 'n' greater than or equal to n value will b e a match, and if -1, then values of 'n' less than or equal to n value will match. int fits_translate_keyword( char *inrec, /* char *outrec, /* /* /* char *patterns[][2],/* /* int npat, /* int n_value, /* int n_offset, /* /* int n_range, /* /* int *pat_num, /* int *i, /* int *j, /* int *m, /* int *n, /* int *status) /*

I-i O-o a m I-p t I-n I-b I-o v I-c v O-m O-v O-v O-v O-v IO -

nput string */ utput converted string, or */ null string if input does not */ atch any of the patterns */ ointer to input / output string */ emplates */ umber of templates passed */ ase 'n' template value of interest */ ffset to be applied to the 'n' */ alue in the output string */ ontrols range of 'n' template */ alues of interest (-1,0, or +1) */ atched pattern number (0 based) or -1 */ alue of i, if any, else 0 */ alue of j, if any, else 0 */ alue of m, if any, else 0 */ alue of n, if any, else 0 */ error status */

Here is an example of some of the patterns used to convert the keywords associated with an image in a cell of a table column into the keywords appropriate for an IMAGE extension: c " " " " " h T T T T T a Z U N D D r E N U M M *patt ROn", ITn", LLn", INn", AXn", erns[][2] = "BZERO" "BUNIT" "BLANK" "DATAMIN" "DATAMAX" {{"TSCALn", , , , , , "BSCALE" }, /* Standard FITS keywords */

{ { { { {

} } } } }


70 { { { { { { { { { { { { { { { { { { { " " " " " " " " " " " " " " " " " " " i i i i i i i i i i i i i i i i i W W C C C C C C C C C C j j V S C C C C C T T U U R R D D R R P C n n R S R A S Y Y N N V V L E P P C D _ _ D Y O X N P n I n L n T n X n n n m m n n T n n n a n a n a n a n a a a a a a a n a a " " " " " " " " " " " " " " " " " " " , , , , , , , , , , , , , , , , , , , " " " " " " " " " " " " " " " " " " " C C C C C C C C C C P C P P C C C W W T T U U R R D D R R C D V S R S R C C Y Y N N V V E E P P i i i i D Y O S S P P I I A A L L I I _ _ _ _ E E T A N E E T T L L T T X X j j m m R R A X A i i i i i i i i i i a a a a i i i E M " a " a " a " a " a " " " " a a " S E

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES }, /* Coordinate labels */ }, }, /* Coordinate units */ }, }, /* WCS keywords */ }, }, }, }, }, }, }, }, }, }, }, }, "}, "}};

" " " " "

" " a a

20 Translate the keywords in the input HDU into the keywords that are appropriate for the output HDU. This is a driver routine that calls the previously describ ed routine. int f f i c i i i ts_translate_keywords( sfile *infptr, /* I sfile *outfptr, /* I firstkey, /* I r *patterns[][2],/* I npat, /* I n_value, /* I n_offset, /* I /* int n_range, /* I /* int *status) /* IO f i i n h n n n i t t t a t t t

ointer to input HDU */ ointer to output HDU */ irst HDU record number to start with */ ointer to input / output keyword templates */ umber of templates passed */ ase 'n' template value of interest */ ffset to be applied to the 'n' */ value in the output string */ controls range of 'n' template */ values of interest (-1,0, or +1) */ - error status */

p p f p n b o

21 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. int fits_parse_range / ffrwrg(char *rowlist, LONGLONG maxrows, int maxranges, >


5.8. UTILITY ROUTINES int *numranges, long *rangemin, long *rangemax, int *status)

71

int fits_parse_rangell / ffrwrgll(char *rowlist, LONGLONG maxrows, int maxranges, > int *numranges, LONGLONG *rangemin, LONGLONG *rangemax, int *status) 22 Check that the Header fill bytes (if any) are all blank. These are the bytes that may follow END keyword and b efore the b eginning of data unit, or the end of the HDU if there is no data unit. int ffchfl(fitsfile *fptr, > int *status) 23 Check that the Data fill bytes (if any) are all zero (for IMAGE or BINARY Table HDU) or all blanks (for ASCI I table HDU). These file bytes may b e located after the last valid data byte in the HDU and b efore the physical end of the HDU. int ffcdfl(fitsfile *fptr, > int *status) 24 Estimate the root-mean-squared (RMS) noise in an image. These routines are mainly for use with the Hcompress image compression algorithm. They return an estimate of the RMS noise in the background pixels of the image. This robust algorithm (written by Richard White, STScI) first attempts to estimate the RMS value as 1.68 times the median of the absolute differences b etween successive pixels in the image. If the median = 0, then the algorithm falls back to computing the RMS of the difference b etween successive pixels, after several N-sigma rejection cycles to remove extreme values. The input parameters are: the array of image pixel values (either float or short values), the numb er of values in the array, the value that is used to represent null pixels (enter a very large numb er if there are no null pixels). int fits_rms_flo > int fits_rms_sho > at do rt do ( u ( u f b s b l l h l o e o e at *r rt *r f m f m d s d s a , a , ta[] int ta[] int , int *sta , int *sta npix, float in_null_value, tus) npix, short in_null_value, tus)


72

CHAPTER 5. BASIC CFITSIO INTERFACE ROUTINES


Chapter 6

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 73


74

CHAPTER 6.

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. The following 2 sections describ e these steps in more detail. There are also several example programs included with the CFITSIO distribution which illustrate how to use the iterator function.

6.1

The Iterator Work Function

The user-supplied iterator work function must have the following set of input parameters (the function can b e given any desired name): int user_fn( long totaln, long offset, long firstn, long nvalues, int narrays, iteratorCol *data, void *userPointer ) · totaln ­ the total numb er of table rows or image pixels that will b e passed to the work function during 1 or more iterations. · offset ­ the offset applied to the first table row or image pixel to b e passed to the work function. In other words, this is the numb er of rows or pixels that are skipp ed over b efore starting the iterations. If offset = 0, then all the table rows or image pixels will b e passed to the work function. · firstn ­ the numb er of the first table row or image pixel (starting with 1) that is b eing passed in this particular call to the work function. · nvalues ­ the numb er of table rows or image pixels that are b eing passed in this particular call to the work function. nvalues will always b e less than or equal to totaln and will have the same value on each iteration, except p ossibly on the last call which may have a smaller value. · narrays ­ the numb er of arrays of data that are b eing passed to the work function. There is one array for each image or table column. · *data ­ array of structures, one for each column or image. Each structure contains a p ointer to the array of data as well as other descriptive parameters ab out that array. · *userPointer ­ a user supplied p ointer that can b e used to pass ancillary information from the driver function to the work function. This p ointer is passed to the CFITSIO iterator function which then passes it on to the work function without any modification. It may p oint to a single numb er, to an array of values, to a structure containing an arbitrary set of parameters of different typ es, or it may b e a null p ointer if it is not needed. The work function must cast this p ointer to the appropriate data typ e b efore using it it.


6.1. THE ITERATOR WORK FUNCTION

75

The totaln, offset, narrays, data, and userPointer parameters are guaranteed to have the same value on each iteration. Only firstn, nvalues, and the arrays of data p ointed to by the data structures may change on each iterative call to the work function. Note that the iterator treats an image as a long 1-D array of pixels regardless of it's intrinsic dimensionality. The total numb er of pixels is just the product of the size of each dimension, and the order of the pixels is the same as the order that they are stored in the FITS file. If the work function needs to know the numb er and size of the image dimensions then these parameters can b e passed via the userPointer structure. The iteratorCol structure is currently defined as follows: typedef struct /* structure for the iterator function column information */ { /* structure elements required as input to fits_iterate_data: */ f i c i i i n h n n tsfile *fpt t coln ar coln t data t ioty r u a t p ; m m y e / ; / e[70]; / pe; / ; / * * * * * p c n o t o o a u y i l m t p n u e p e ter to mn num (TTYP ut dat : Inpu the HDU containing the column or image */ ber in the table; ignored for images */ En) of the column; null for images */ a type (converted if necessary) */ tCol, InputOutputCol, or OutputCol */

/* output structure elements that may be useful for the work function: */ void long l l c c o o h h n n a a g g r r *array; repeat; t t u t l l n d m m i i i a t s n x [ p / / / ; / ; / 70]; / [70]; / * * * * * * * pointer to the array (and th binary table vector repeat v equal to 1 for images legal minimum data value, if legal maximum data value, if physical unit string (BUNIT suggested display format; nu e null value) * alue; set * * any * any * or TUNITn) * ll if none * / / / / / / /

} iteratorCol; Instead of directly reading or writing the elements in this structure, it is recommended that programmers use the access functions that are provided for this purp ose. The first five elements in this structure must b e initially defined by the driver routine b efore calling the iterator routine. The CFITSIO iterator routine uses this information to determine what column or array to pass to the work function, and whether the array is to b e input to the work function, output from the work function, or b oth. The CFITSIO iterator function fills in the values of the remaining structure elements b efore passing it to the work function. The array structure element is a p ointer to the actual data array and it must b e cast to the correct data typ e b efore it is used. The `rep eat' structure element give the numb er of data values in each row of the table, so that the total numb er of data values in the array is given by rep eat * nvalues. In the case of image arrays and ASCI I tables, rep eat will always b e equal to 1. When the data typ e is a character string, the array p ointer is actually a p ointer to an array of string p ointers (i.e., char **array). The other output structure elements are provided for convenience in case that


76

CHAPTER 6.

THE CFITSIO ITERATOR FUNCTION

information is needed within the work function. Any other information may b e passed from the driver routine to the work function via the userPointer parameter. Up on completion, the work routine must return an integer status value, with 0 indicating success and any other value indicating an error which will cause the iterator function to immediately exit at that p oint. Return status values in the range 1 ­ 1000 should b e avoided since these are reserved for use by CFITSIO. A return status value of -1 may b e used to force the CFITSIO iterator function to stop at that p oint and return control to the driver routine after writing any output arrays to the FITS file. CFITSIO does not considered this to b e an error condition, so any further processing by the application program will continue normally.

6.2

The Iterator Driver Function
driver function must op en the necessary FITS files and p osition them to the correct t also initialize the following parameters in the iteratorCol structure (defined ab ove) for or image b efore calling the CFITSIO iterator function. Several `constructor' routines in CFITSIO for this purp ose.

The iterator HDU. It mus each column are provided

· *fptr ­ The fitsfile p ointer to the table or image. · colnum ­ the numb er of the column in the table. This value is ignored in the case of images. If colnum equals 0, then the column name will b e used to identify the column to b e passed to the work function. · colname ­ the name (TTYPEn keyword) of the column. This is only required if colnum = 0 and is ignored for images. · datatyp e ­ The desired data typ e of the array to b e passed to the work function. For numerical data the data typ e does not need to b e the same as the actual data typ e in the FITS file, in which case CFITSIO will do the conversion. Allowed values are: TSTRING, TLOGICAL, TBYTE, TSBYTE, TSHORT, TUSHORT, TINT, TLONG, TULONG, TFLOAT, TDOUBLE. If the input value of data typ e equals 0, then the existing data typ e of the column or image will b e used without any conversion. · iotyp e ­ defines whether the data array is to b e input to the work function (i.e, read from the FITS file), or output from the work function (i.e., written to the FITS file) or b oth. Allowed values are InputCol, OutputCol, or InputOutputCol. Variable-length array columns are supp orted as InputCol or InputOutputCol typ es, but may not b e used for an OutputCol typ e. After the driver routine has initialized all these parameters, it can then call the CFITSIO iterator function: int fits_iterate_data(int narrays, iteratorCol *data, long offset, long nPerLoop, int (*workFn)( ), void *userPointer, int *status); · narrays ­ the numb er of columns or images that are to b e passed to the work function.


6.3. GUIDELINES FOR USING THE ITERATOR FUNCTION

77

· *data ­ p ointer to array of structures containing information ab out each column or image. · offset ­ if p ositive, this numb er of rows at the b eginning of the table (or pixels in the image) will b e skipp ed and will not b e passed to the work function. · nPerLoop - sp ecifies the numb er of table rows (or numb er of image pixels) that are to b e passed to the work function on each iteration. If nPerLoop = 0 then CFITSIO will calculate the optimum numb er for greatest efficiency. If nPerLoop is negative, then all the rows or pixels will b e passed at one time, and the work function will only b e called once. If any variable length arrays are b eing processed, then the nPerLoop value is ignored, and the iterator will always process one row of the table at a time. · *workFn - the name (actually the address) of the work function that is to b e called by fits iterate data. · *userPointer - this is a user supplied p ointer that can b e used to pass ancillary information from the driver routine to the work function. It may p oint to a single numb er, an array, or to a structure containing an arbitrary set of parameters. · *status - The CFITSIO error status. Should = 0 on input; a non-zero output value indicates an error. When fits iterate data is called it first allocates memory to hold all the requested columns of data or image pixel arrays. It then reads the input data from the FITS tables or images into the arrays then passes the structure with p ointers to these data arrays to the work function. After the work function returns, the iterator function writes any output columns of data or images back to the FITS files. It then rep eats this process for any remaining sets of rows or image pixels until it has processed the entire table or image or until the work function returns a non-zero status value. The iterator then frees the memory that it initially allocated and returns control to the driver routine that called it.

6.3

Guidelines for Using the Iterator Function

The totaln, offset, firstn, and nvalues parameters that are passed to the work function are useful for determining how much of the data has b een processed and how much remains left to do. On the very first call to the work function firstn will b e equal to offset + 1; the work function may need to p erform various initialization tasks b efore starting to process the data. Similarly, firstn + nvalues - 1 will b e equal to totaln on the last iteration, at which p oint the work function may need to p erform some clean up op erations b efore exiting for the last time. The work function can also force an early termination of the iterations by returning a status value = -1. The narrays and iteratorCol.datatyp e arguments allow the work function to double check that the numb er of input arrays and their data typ es have the exp ected values. The iteratorCol.fptr and iteratorCol.colnum structure elements can b e used if the work function needs to read or write the values of other keywords in the FITS file associated with the array. This should generally only b e done during the initialization step or during the clean up step after the last set of data has b een processed. Extra FITS file I/O during the main processing loop of the work function can seriously degrade the sp eed of the program.


78

CHAPTER 6.

THE CFITSIO ITERATOR FUNCTION

If variable-length array columns are b eing processed, then the iterator will op erate on one row of the table at a time. In this case the the rep eat element in the interatorCol structure will b e set equal to the numb er of elements in the current row that is b eing processed. One imp ortant feature of the iterator is that the first element in each array that is passed to the work function gives the value that is used to represent null or undefined values in the array. The real data then b egins with the second element of the array (i.e., array[1], not array[0]). If the first array element is equal to zero, then this indicates that all the array elements have defined values and there are no undefined values. If array[0] is not equal to zero, then this indicates that some of the data values are undefined and this value (array[0]) is used to represent them. In the case of output arrays (i.e., those arrays that will b e written back to the FITS file by the iterator function after the work function exits) the work function must set the first array element to the desired null value if necessary, otherwise the first element should b e set to zero to indicate that there are no null values in the output array. CFITSIO defines 2 values, FLOATNULLVALUE and DOUBLENULLVALUE, that can b e used as default null values for float and double data typ es, resp ectively. In the case of character string data typ es, a null string is always used to represent undefined strings. In some applications it may b e necessary to recursively call the iterator function. An example of this is given by one of the example programs that is distributed with CFITSIO: it first calls a work function that writes out a 2D histogram image. That work function in turn calls another work function that reads the `X' and `Y' columns in a table to calculate the value of each 2D histogram image pixel. Graphically, the program structure can b e describ ed as: driver --> iterator --> work1_fn --> iterator --> work2_fn Finally, it should b function do not all bination of sources image array is used e noted that the table columns or image arrays that are passed to the work have to come from the same FITS file and instead may come from any comas long as they have the same length. The length of the first table column or by the iterator if they do not all have the same length.

6.4

Complete List of Iterator Routines

All of the iterator routines are listed b elow. Most of these routines do not have a corresp onding short function name. 1 Iterator `constructor' functions that set the value of elements in the iteratorCol structure that define the columns or arrays. These set the fitsfile p ointer, column name, column numb er, datatyp e, and iotyp e, resp ectively. The last 2 routines allow all the parameters to b e set with one function call (one supplies the column name, the other the column numb er). int fits_iter_set_file(iteratorCol *col, fitsfile *fptr); int fits_iter_set_colname(iteratorCol *col, char *colname); int fits_iter_set_colnum(iteratorCol *col, int colnum);


6.4. COMPLETE LIST OF ITERATOR ROUTINES

79

int fits_iter_set_datatype(iteratorCol *col, int datatype); int fits_iter_set_iotype(iteratorCol *col, int iotype); int fits_iter_set_by_name(iteratorCol *col, fitsfile *fptr, char *colname, int datatype, int iotype); int fits_iter_set_by_num(iteratorCol *col, fitsfile *fptr, int colnum, int datatype, int iotype); 2 Iterator `accessor' functions that return the value of the element in the iteratorCol structure that describ es a particular data column or array fitsfile * fits_iter_get_file(iteratorCol *col); char * fits_iter_get_colname(iteratorCol *col); int fits_iter_get_colnum(iteratorCol *col); int fits_iter_get_datatype(iteratorCol *col); int fits_iter_get_iotype(iteratorCol *col); void * fits_iter_get_array(iteratorCol *col); long fits_iter_get_tlmin(iteratorCol *col); long fits_iter_get_tlmax(iteratorCol *col); long fits_iter_get_repeat(iteratorCol *col); char * fits_iter_get_tunit(iteratorCol *col); char * fits_iter_get_tdisp(iteratorCol *col); 3 The CFITSIO iterator function int fits_iterate_data(int narrays, long nPerLoop, int (*workFn)( long total long nvalu void *user void *userPointer, int *status); iteratorCol *data, long offset, n, long offset, long firstn, es, int narrays, iteratorCol *data, Pointer),


80

CHAPTER 6.

THE CFITSIO ITERATOR FUNCTION


Chapter 7

World Coordinate System Routines
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 a few 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 keywords contained in the FITS file headers. One convenient way to pass this information to the external library is to use the fits hdr2str routine in CFITSIO (defined b elow) to copy the header keywords into one long string, and then pass this string to an interface routine in the external library that will extract the necessary WCS information (e.g., the 'wcspih' routine in the WCSLIB library and the 'astFitsChan' and 'astPutCards' functions in the AST library). 1 Concatenate the header keywords in the CHDU into a single long string of characters. Each 80-character fixed-length keyword record is app ended to the output character string, in order, with no intervening separator or terminating characters. The last header record is terminated with a NULL character. This routine allocates memory for the returned character array, so the calling program must free the memory when finished. There are 2 header; fits (stored in a image b efor related routines: fits hdr2str simply concatenates all the existing keywords in the convert hdr2str is similar, except that if the CHDU is a tile compressed image binary table) then it will first convert that header back to that of a normal FITS e concatenating the keywords. 81


82

CHAPTER 7.

WORLD COORDINATE SYSTEM ROUTINES

Selected keywords may b e excluded from the returned character string. If the second parameter (nocomments) is TRUE (nonzero) then any COMMENT, HISTORY, or blank keywords in the header will not b e copied to the output string. The 'exclist' parameter may b e used to supply a list of keywords that are to b e excluded from the output character string. Wild card characters (*, ?, and #) may b e used in the excluded keyword names. If no additional keywords are to b e excluded, then set nexc = 0 and sp ecify NULL for the the **exclist parameter. int fits_hdr2str (fitsfile *fptr, int nocomments, char **exclist, int nexc, > char **header, int *nkeys, int *status) int fits_convert_hdr2str / ffcnvthdr2str (fitsfile *fptr, int nocomments, char **exclist, int nexc, > char **header, int *nkeys, int *status) 2 The following CFITSIO routine is sp ecifically designed for use in conjunction with the WCSLIB library. It is not exp ected that applications programmers will call this routine directly, but it is documented here for completeness. This routine extracts arrays from a binary table that contain WCS information using the -TAB table lookup convention. See the documentation provided with the WCSLIB library for more information. int fits_read_wcstab (fitsfile *fptr, int nwtb, wtbarr *wtb, int *status);

7.1

Self-contained WCS Routines

The following routines DO NOT supp ort the more recent WCS conventions that have b een approved as part of the FITS standard. Consequently, the following routines ARE NOW DEPRECATED. It is STRONGLY RECOMMENDED that software develop ers not use these routines, and instead use an external WCS library, as describ ed in the previous section. 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 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.

1 Get the values of the basic set of 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 fits pix to world and fits world to pix routines 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


7.1.

SELF-CONTAINED WCS ROUTINES

83

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 = APPROX WCS KEY, 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. int fit (fi do do s t u u _ s b b r f l l e i e e ad_img_coord le *fptr, > d *xrefpix, do *rot, char * / o u c ffgics uble *xrefval, double *yrefval, ble *yrefpix, double *xinc, double *yinc, oordtype, int *status)

2 Get the values of 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 (as in the Event List table format that is often used by high energy astrophysics missions). These values may then b e passed to the fits pix to world and fits world to pix routines that p erform the coordinate transformations. int fit (fi do do s t u u _ s b b r f l l e i e e ad_tbl_coord / ffgtcs le *fptr, int xcol, int ycol, > double *xrefval, *yrefval, double *xrefpix, double *yrefpix, double *xinc, *yinc, double *rot, char *coordtype, int *status)

3 Calculate the celestial coordinate corresp onding to the input X and Y pixel location in the image. int fit (do do do in s u u u t _pix_to_world / ffwldp ble xpix, double ypix, double xrefval, double yrefval, ble xrefpix, double yrefpix, double xinc, double yinc, ble rot, char *coordtype, > double *xpos, double *ypos, *status)

4 Calculate the X and Y pixel location corresp onding to the input celestial coordinate in the image. int fit (do do do in s u u u t _world_to_pix / ffxypx ble xpos, double ypos, double xrefval, double yrefval, ble xrefpix, double yrefpix, double xinc, double yinc, ble rot, char *coordtype, > double *xpix, double *ypix, *status)


84

CHAPTER 7.

WORLD COORDINATE SYSTEM ROUTINES


Chapter 8

Hierarchical Grouping Routines
These functions allow for the creation and manipulation of FITS HDU Groups, as defined in "A Hierarchical Grouping Convention for FITS" by Jennings, Pence, Folk and Schlesinger: http://fits.gsfc.nasa.gov/group.html A group is a collection of HDUs whose association is defined by a grouping table. HDUs which are part of a group are referred to as member HDUs or simply as members. Grouping table memb er HDUs may themselves b e grouping tables, thus allowing for the construction of op enended hierarchies of HDUs. Grouping tables contain one row for each memb er HDU. The grouping table columns provide identification information that allows applications to reference or "p oint to" the memb er HDUs. Memb er HDUs are exp ected, but not required, to contain a set of GRPIDn/GRPLCn keywords in their headers for each grouping table that they are referenced by. In this sense, the GRPIDn/GRPLCn keywords "link" the memb er HDU back to its Grouping table. Note that a memb er HDU need not reside in the same FITS file as its grouping table, and that a given HDU may b e referenced by up to 999 grouping tables simultaneously. Grouping tables are implemented as FITS binary tables with up to six pre-defined column TTYPEn values: 'MEMBER XTENSION', 'MEMBER NAME', 'MEMBER VERSION', 'MEMBER POSITION', 'MEMBER URI TYPE' and 'MEMBER LOCATION'. The first three columns allow memb er HDUs to b e identified by reference to their XTENSION, EXTNAME and EXTVER keyword values. The fourth column allows memb er HDUs to b e identified by HDU p osition within their FITS file. The last two columns identify the FITS file in which the memb er HDU resides, if different from the grouping table FITS file. Additional user defined "auxiliary" columns may also b e included with any grouping table. When a grouping table is copied or modified the presence of auxiliary columns is always taken into account by the grouping supp ort functions; however, the grouping supp ort functions cannot directly make use of this data. If a grouping table column is defined but the corresp onding memb er HDU information is unavailable then a null value of the appropriate data typ e is inserted in the column field. Integer columns (MEMBER POSITION, MEMBER VERSION) are defined with a TNULLn value of zero (0). Character field columns (MEMBER XTENSION, MEMBER NAME, MEMBER URI TYPE, 85


86

CHAPTER 8.

HIERARCHICAL GROUPING ROUTINES

MEMBER LOCATION) utilize an ASCI I null character to denote a null field value. The grouping supp ort functions b elong to two basic categories: those that work with grouping table HDUs (ffgt**) and those that work with memb er HDUs (ffgm**). Two functions, fits copy group() and fits remove group(), have the option to recursively copy/delete entire groups. Care should b e taken when employing these functions in recursive mode as p oorly defined groups could cause unpredictable results. The problem of a grouping table directly or indirectly referencing itself (thus creating an infinite loop) is protected against; in fact, neither function will attempt to copy or delete an HDU twice.

8.1

Grouping Table Routines

1 Create (app end) a grouping table at the end of the current FITS file p ointed to by fptr. The grpname parameter provides the grouping table name (GRPNAME keyword value) and may b e set to NULL if no group name is to b e sp ecified. The grouptyp e parameter sp ecifies the desired structure of the grouping table and may take on the values: GT ID ALL URI (all columns created), GT ID REF (ID by reference columns), GT ID POS (ID by p osition columns), GT ID ALL (ID by reference and p osition columns), GT ID REF URI (ID by reference and FITS file URI columns), and GT ID POS URI (ID by p osition and FITS file URI columns). int fits_create_group / ffgtcr (fitsfile *fptr, char *grpname, int grouptype, > int *status) 2 Create (insert) a grouping table just after the CHDU of the current FITS file p ointed to by fptr. All HDUs b elow the the insertion p oint will b e shifted downwards to make room for the new HDU. The grpname parameter provides the grouping table name (GRPNAME keyword value) and may b e set to NULL if no group name is to b e sp ecified. The grouptyp e parameter sp ecifies the desired structure of the grouping table and may take on the values: GT ID ALL URI (all columns created), GT ID REF (ID by reference columns), GT ID POS (ID by p osition columns), GT ID ALL (ID by reference and p osition columns), GT ID REF URI (ID by reference and FITS file URI columns), and GT ID POS URI (ID by p osition and FITS file URI columns) . int fits_insert_group / ffgtis (fitsfile *fptr, char *grpname, int grouptype, > int *status) 3 Change the structure of an existing grouping table p ointed to by gfptr. The grouptyp e parameter (see fits create group() for valid parameter values) sp ecifies the new structure of the grouping table. This function only adds or removes grouping table columns, it does not add or delete group memb ers (i.e., table rows). If the grouping table already has the desired structure then no op erations are p erformed and function simply returns with a (0) success status code. If the requested structure change creates new grouping table columns, then the column values for all existing memb ers will b e filled with the null values appropriate to the column typ e.


8.1. GROUPING TABLE ROUTINES int fits_change_group / ffgtch (fitsfile *gfptr, int grouptype, > int *status)

87

4 Remove the group defined by the grouping table p ointed to by gfptr, and optionally all the group memb er HDUs. The rmopt parameter sp ecifies the action to b e taken for all memb ers of the group defined by the grouping table. Valid values are: OPT RM GPT (delete only the grouping table) and OPT RM ALL (recursively delete all HDUs that b elong to the group). Any groups containing the grouping table gfptr as a memb er are up dated, and if rmopt == OPT RM GPT all memb ers have their GRPIDn and GRPLCn keywords up dated accordingly. If rmopt == OPT RM ALL, then other groups that contain the deleted memb ers of gfptr are up dated to reflect the deletion accordingly. int fits_remove_group / ffgtrm (fitsfile *gfptr, int rmopt, > int *status) 5 Copy (app end) the group defined by the grouping table p ointed to by infptr, and optionally all group memb er HDUs, to the FITS file p ointed to by outfptr. The cp opt parameter sp ecifies the action to b e taken for all memb ers of the group infptr. Valid values are: OPT GCP GPT (copy only the grouping table) and OPT GCP ALL (recursively copy ALL the HDUs that b elong to the group defined by infptr). If the cp opt == OPT GCP GPT then the memb ers of infptr have their GRPIDn and GRPLCn keywords up dated to reflect the existence of the new grouping table outfptr, since they now b elong to the new group. If cp opt == OPT GCP ALL then the new grouping table outfptr only contains p ointers to the copied memb er HDUs and not the original memb er HDUs of infptr. Note that, when cp opt == OPT GCP ALL, all memb ers of the group defined by infptr will b e copied to a single FITS file p ointed to by outfptr regardless of their file distribution in the original group. int fits_copy_group / ffgtcp (fitsfile *infptr, fitsfile *outfptr, int cpopt, > int *status) 6 Merge the two groups defined by the grouping table HDUs infptr and outfptr by combining their memb ers into a single grouping table. All memb er HDUs (rows) are copied from infptr to outfptr. If mgopt == OPT MRG COPY then infptr continues to exist unaltered after the merge. If the mgopt == OPT MRG MOV then infptr is deleted after the merge. In b oth cases, the GRPIDn and GRPLCn keywords of the memb er HDUs are up dated accordingly. int fits_merge_groups / ffgtmg (fitsfile *infptr, fitsfile *outfptr, int mgopt, > int *status) 7 "Compact" the group defined by grouping table p ointed to by gfptr. The compaction is achieved by merging (via fits merge groups()) all direct memb er HDUs of gfptr that are themselves grouping tables. The cmopt parameter defines whether the merged grouping table HDUs remain after merging (cmopt == OPT CMT MBR) or if they are deleted after merging (cmopt == OPT CMT MBR DEL). If the grouping table contains no direct memb er HDUs that are themselves grouping tables then this function does nothing. Note that this function is not recursive, i.e., only the direct memb er HDUs of gfptr are considered for merging.


88

CHAPTER 8.

HIERARCHICAL GROUPING ROUTINES

int fits_compact_group / ffgtcm (fitsfile *gfptr, int cmopt, > int *status) 8 Verify the integrity of the grouping table p ointed to by gfptr to make sure that all group memb ers are accessible and that all links to other grouping tables are valid. The firstfailed parameter returns the memb er ID (row numb er) of the first memb er HDU to fail verification (if p ositive value) or the first group link to fail (if negative value). If gfptr is successfully verified then firstfailed contains a return value of 0. int fits_verify_group / ffgtvf (fitsfile *gfptr, > long *firstfailed, int *status) 9 Op en a grouping table that contains the memb er HDU p ointed to by mfptr. The grouping table to op en is defined by the grpid parameter, which contains the keyword index value of the GRPIDn/GRPLCn keyword(s) that link the memb er HDU mfptr to the grouping table. If the grouping table resides in a file other than the memb er HDUs file then an attempt is first made to op en the file readwrite, and failing that readonly. A p ointer to the op ened grouping table HDU is returned in gfptr. Note that it is p ossible, although unlikely and undesirable, for the GRPIDn/GRPLCn keywords in a memb er HDU header to b e non-continuous, e.g., GRPID1, GRPID2, GRPID5, GRPID6. In such cases, the grpid index value sp ecified in the function call shall identify the (grpid)th GRPID value. In the ab ove example, if grpid == 3, then the group sp ecified by GRPID5 would b e op ened. int fits_open_group / ffgtop (fitsfile *mfptr, int group, > fitsfile **gfptr, int *status) 10 Add a memb er HDU to an existing grouping table p ointed to by gfptr. The memb er HDU may either b e p ointed to mfptr (which must b e p ositioned to the memb er HDU) or, if mfptr == NULL, identified by the hdup os parameter (the HDU p osition numb er, Primary array == 1) if b oth the grouping table and the memb er HDU reside in the same FITS file. The new memb er HDU shall have the appropriate GRPIDn and GRPLCn keywords created in its header. Note that if the memb er HDU is already a memb er of the group then it will not b e added a second time. int fits_add_group_member / ffgtam (fitsfile *gfptr, fitsfile *mfptr, int hdupos, > int *status)

8.2

Group Memb er Routines

1 Return the numb er of memb er HDUs in a grouping table gfptr. The numb er memb er HDUs is just the NAXIS2 value (numb er of rows) of the grouping table.


8.2. GROUP MEMBER ROUTINES int fits_get_num_members / ffgtnm (fitsfile *gfptr, > long *nmembers, int *status)

89

2 Return the numb er of groups to which the HDU p ointed to by mfptr is linked, as defined by the numb er of GRPIDn/GRPLCn keyword records that app ear in its header. Note that each time this function is called, the indices of the GRPIDn/GRPLCn keywords are checked to make sure they are continuous (ie no gaps) and are re-enumerated to eliminate gaps if found.

int fits_get_num_groups / ffgmng (fitsfile *mfptr, > long *nmembers, int *status) 3 Op en a memb er of the grouping table p ointed to by gfptr. The memb er to op en is identified by its row numb er within the grouping table as given by the parameter 'memb er' (first memb er == 1) . A fitsfile p ointer to the op ened memb er HDU is returned as mfptr. Note that if the memb er HDU resides in a FITS file different from the grouping table HDU then the memb er file is first op ened readwrite and, failing this, op ened readonly. int fits_open_member / ffgmop (fitsfile *gfptr, long member, > fitsfile **mfptr, int *status) 4 Copy (app end) a memb er HDU of the grouping table p ointed to by gfptr. The memb er HDU is identified by its row numb er within the grouping table as given by the parameter 'memb er' (first memb er == 1). The copy of the group memb er HDU will b e app ended to the FITS file p ointed to by mfptr, and up on return mfptr shall p oint to the copied memb er HDU. The cp opt parameter may take on the following values: OPT MCP ADD which adds a new entry in gfptr for the copied memb er HDU, OPT MCP NADD which does not add an entry in gfptr for the copied memb er, and OPT MCP REPL which replaces the original memb er entry with the copied memb er entry. int fits_copy_member / ffgmcp (fitsfile *gfptr, fitsfile *mfptr, long member, int cpopt, > int *status) 5 Transfer a group memb er HDU from the grouping table p ointed to by infptr to the grouping table p ointed to by outfptr. The memb er HDU to transfer is identified by its row numb er within infptr as sp ecified by the parameter 'memb er' (first memb er == 1). If tfopt == OPT MCP ADD then the memb er HDU is made a memb er of outfptr and remains a memb er of infptr. If tfopt == OPT MCP MOV then the memb er HDU is deleted from infptr after the transfer to outfptr. int fits_transfer_member / ffgmtf (fitsfile *infptr, fitsfile *outfptr, long member, int tfopt, > int *status)


90

CHAPTER 8.

HIERARCHICAL GROUPING ROUTINES

6 Remove a memb er HDU from the grouping table p ointed to by gfptr. The memb er HDU to b e deleted is identified by its row numb er in the grouping table as sp ecified by the parameter 'memb er' (first memb er == 1). The rmopt parameter may take on the following values: OPT RM ENTRY which removes the memb er HDU entry from the grouping table and updates the memb er's GRPIDn/GRPLCn keywords, and OPT RM MBR which removes the memb er HDU entry from the grouping table and deletes the memb er HDU itself. int fits_remove_member / ffgmrm (fitsfile *fptr, long member, int rmopt, > int *status)


Chapter 9

Specialized CFITSIO Interface Routines
The basic interface routines describ ed previously are recommended for most uses, but th describ ed in this chapter are also available if necessary. Some of these routines p erform cialized function that cannot easily b e done with the basic interface routines while others the functionality of the basic routines but have a slightly different calling sequence. See B for the definition of each function parameter. e routines more sp eduplicate App endix

9.1

FITS File Access Routines

1 Op en an existing FITS file residing in core computer memory. This routine is analogous to fits op en file. The 'filename' is currently ignored by this routine and may b e any arbitrary string. In general, the application must have preallocated an initial block of memory to hold the FITS file prior to calling this routine: 'memptr' p oints to the starting address and 'memsize' gives the initial size of the block of memory. 'mem realloc' is a p ointer to an optional function that CFITSIO can call to allocate additional memory, if needed (only if mode = READWRITE), and is modeled after the standard C 'realloc' function; a null p ointer may b e given if the initial allocation of memory is all that will b e required (e.g., if the file is op ened with mode = READONLY). The 'deltasize' parameter may b e used to suggest a minimum amount of additional memory that should b e allocated during each call to the memory reallocation function. By default, CFITSIO will reallocate enough additional space to hold the entire currently defined FITS file (as given by the NAXISn keywords) or 1 FITS block (= 2880 bytes), which ever is larger. Values of deltasize less than 2880 will b e ignored. Since the memory reallocation op eration can b e computationally exp ensive, allocating a larger initial block of memory, and/or sp ecifying a larger deltasize value may help to reduce the numb er of reallocation calls and make the application program run faster. Note that values of the memptr and memsize p ointers will b e up dated by CFITSIO if the location or size of the FITS file in memory should change as a result of allocating more memory. int fits_open_memfile / ffomem (fitsfile **fptr, const char *filename, int mode, void **memptr, 91


92

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

size_t *memsize, size_t deltasize, void *(*mem_realloc)(void *p, size_t newsize), int *status) 2 Create a new FITS file residing in core computer memory. This routine is analogous to fits create file. In general, the application must have preallocated an initial block of memory to hold the FITS file prior to calling this routine: 'memptr' p oints to the starting address and 'memsize' gives the initial size of the block of memory. 'mem realloc' is a p ointer to an optional function that CFITSIO can call to allocate additional memory, if needed, and is modeled after the standard C 'realloc' function; a null p ointer may b e given if the initial allocation of memory is all that will b e required. The 'deltasize' parameter may b e used to suggest a minimum amount of additional memory that should b e allocated during each call to the memory reallocation function. By default, CFITSIO will reallocate enough additional space to hold 1 FITS block (= 2880 bytes) and values of deltasize less than 2880 will b e ignored. Since the memory reallocation op eration can b e computationally exp ensive, allocating a larger initial block of memory, and/or sp ecifying a larger deltasize value may help to reduce the numb er of reallocation calls and make the application program run faster. Note that values of the memptr and memsize p ointers will b e up dated by CFITSIO if the location or size of the FITS file in memory should change as a result of allocating more memory. int fit (fi si vo s t z i _ s e d create_memfile / ffimem file **fptr, void **memptr, _t *memsize, size_t deltasize, *(*mem_realloc)(void *p, size_t newsize), int *status) file that was previously op ened with fits op en file or fits create file. The new may then b e treated as a separate file, and one may simultaneously read or more) different extensions in the same file. The fits op en file routine (ab ove) detects cases where a previously op ened file is b eing op ened again, and then fits reop en file, so programs should rarely need to explicitly call this routine.

3 Reop en a FITS fitsfile p ointer write to 2 (or automatically internally call

int fits_reopen_file / ffreopen (fitsfile *openfptr, fitsfile **newfptr, > int *status) 4 Create a new FITS file, using a template file to define its initial may b e another FITS HDU or an ASCI I template file. If the is null, then this routine b ehaves the same as fits create file. of the ASCI I template file is describ ed under the fits parse Utilities section) size and structure. The template input template file name p ointer The currently supp orted format template routine (in the general

int fits_create_template / fftplt (fitsfile **fptr, char *filename, char *tpltfile > int *status) 5 Parse the input filename or URL into its comp onent parts, namely: · the file typ e (file://, ftp://, http://, etc),


9.1. FITS FILE ACCESS ROUTINES · 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, · the column sp ecifier, · and the image pixel filtering sp ecifier.

93

Null strings will b e returned for any comp onents that are not present in the input file name. These routines are mainly for internal use by other CFITSIO routines. int fits_parse_input_url / ffiurl (char *filename, > char *filetype, char *infile, char *outfile, char *extspec, char *filter, char *binspec, char *colspec, int *status) int fit (ch *e in s a x t _parse_input_filename / ffifile r *filename, > char *filetype, char *infile, char *outfile, char tspec, char *filter, char *binspec, char *colspec, char *pixspec, *status)

6 Parse the input filename and return the HDU numb er that would b e moved to if the file were op ened with fits op en file. 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. CFITSIO 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 fits op en file, then use fits get hdu num to determine which HDU in the file has b een op ened, rather than calling fits parse input url followed by a call to fits op en file. int fits_parse_extnum / ffextn (char *filename, > int *hdunum, int *status) 7 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. int fits_parse_rootname / ffrtnm (char *filename, > char *rootname, int *status);


94

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

8 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)

int fits_file_exists / ffexist (char *filename, > int *exists, int *status); 9 Flush any internal buffers of data to the output FITS file. These routines rarely need to b e called, but can b e useful in cases where other processes need to access the same FITS file in real time, either on disk or in memory. These routines also help to ensure that if the application program subsequently ab orts then the FITS file will have b een closed prop erly. The first routine, fits flush file is more rigorous and completely closes, then reop ens, the current HDU, b efore flushing the internal buffers, thus ensuring that the output FITS file is identical to what would b e produced if the FITS was closed at that p oint (i.e., with a call to fits close file). The second routine, fits flush buffer simply flushes the internal CFITSIO buffers of data to the output FITS file, without up dating and closing the current HDU. This is much faster, but there may b e circumstances where the flushed file does not completely reflect the final state of the file as it will exist when the file is actually closed. A typical use of these routines would b e to flush the state of a FITS table to disk after each row of the table is written. It is recommend that fits flush file b e called after the first row is written, then fits flush buffer may b e called after each subsequent row is written. Note that this latter routine will not automatically up date the NAXIS2 keyword which records the numb er of rows of data in the table, so this keyword must b e explicitly up dated by the application program after each row is written. int fits_flush_file / ffflus (fitsfile *fptr, > int *status) int fits_flush_buffer / ffflsh (fitsfile *fptr, 0, > int *status) (Note: The second argument must be 0).

9.2

HDU Access Routines

1 Get the byte offsets in the FITS file to the start of the header and the start and end of the data in the CHDU. The difference b etween headstart and dataend equals the size of the CHDU.


9.2. HDU ACCESS ROUTINES

95

If the CHDU is the last HDU in the file, then dataend is also equal to the size of the entire FITS file. Null p ointers may b e input for any of the address parameters if their values are not needed. int fits_get_hduaddr / ffghad (only supports files up to 2.1 GB in size) (fitsfile *fptr, > long *headstart, long *datastart, long *dataend, int *status) int fits_get_hduaddrll / ffghadll (supports large files) (fitsfile *fptr, > LONGLONG *headstart, LONGLONG *datastart, LONGLONG *dataend, int *status) 2 Create (app end) a new empty HDU at the end of the FITS file. This is now the CHDU but it is completely empty and has no header keywords. It is recommended that fits create img or fits create tbl b e used instead of this routine. int fits_create_hdu / ffcrhd (fitsfile *fptr, > int *status) 3 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 = PREPEND PRIMARY prior to calling the routine. In this case the old primary array will b e converted to an IMAGE extension. The new extension (or primary array) will b ecome the CHDU. Refer to Chapter 9 for a list of pre-defined bitpix values. int fits_insert_img / ffiimg (fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status) int fits_insert_imgll / ffiimgll (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status) 4 Insert a new ASCI I or 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 table extension will simply b e app ended to the end of the file. If the FITS file is currently empty then this routine will create a dummy primary array b efore app ending the table to it. The new extension will b ecome the CHDU. The tunit and extname parameters are optional and a null p ointer may b e given if they are not defined. When inserting an ASCI I table with fits insert atbl, a null p ointer may given for the *tb col parameter in which case each column of the table will b e separated by a single space character. Similarly, if the input value of rowlen is 0, then CFITSIO will calculate the default rowlength based on the tb col and ttyp e values. When inserting a binary table with fits insert btbl, if there are following extensions in the file and if the table contains variable length array columns then p count must sp ecify the exp ected final size of the data heap, otherwise p count must = 0.


96

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

int fits_insert_atbl / ffitab (fitsfile *fptr, LONGLONG rowlen, LONGLONG nrows, int tfields, char *ttype[], long *tbcol, char *tform[], char *tunit[], char *extname, > int *status) int fits_insert_btbl / ffibin (fitsfile *fptr, LONGLONG nrows, int tfields, char **ttype, char **tform, char **tunit, char *extname, long pcount, > int *status) 5 Modify the size, dimensions, and/or data typ 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 the new image. int fits_resize_img / ffrsim (fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status) int fits_resize_imgll / ffrsimll (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status) 6 Copy the data (and not the header) from the CHDU associated with infptr to the CHDU associated with outfptr. This will overwrite any data previously in the output CHDU. This low level routine is used by fits copy hdu, but it may also b e useful in certain application programs that want to copy the data from one FITS file to another but also want to modify the header keywords. The required FITS header keywords which define the structure of the HDU must b e written to the output CHDU b efore calling this routine. int fits_copy_data / ffcpdt (fitsfile *infptr, fitsfile *outfptr, > int *status) 7 Read or write a sp ecified numb er of bytes starting at the sp ecified byte offset from the start of the extension data unit. These low level routine are intended mainly for accessing the data in non-standard, conforming extensions, and should not b e used for standard IMAGE, TABLE, or BINTABLE extensions. int f ( int f ( i f i f t i t i s t s t _ s _ s r f w f e i r i a l i l d e t e _ext / f *fptr, e_ext / *fptr, f L f L g O f O e N p N x G e G t L x L n ONG offset, LONGLONG nbytes, void *buffer) tn ONG offset, LONGLONG nbytes, void *buffer)

8 This routine forces CFITSIO to rescan the current header keywords that define the structure of the HDU (such as the NAXIS and BITPIX keywords) so that it reinitializes the internal


9.3. SPECIALIZED HEADER KEYWORD ROUTINES

97

buffers that describ e the HDU structure. This routine is useful for reinitializing the structure of an HDU if any of the required keywords (e.g., NAXISn) have b een modified. In practice it should rarely b e necessary to call this routine b ecause CFITSIO internally calls it in most situations. int fits_set_hdustruc / ffrdef (fitsfile *fptr, > int *status)

(DEPRECATED)

9.3
9.3.1

Sp ecialized Header Keyword Routines
Header Information Routines

1 Reserve space in the CHU for MOREKEYS more header keywords. This routine may b e called to allocate space for additional keywords at the time the header is created (prior to writing any data). CFITSIO can dynamically add more space to the header when needed, however it is more efficient to preallocate the required space if the size is known in advance. int fits_set_hdrsize / ffhdef (fitsfile *fptr, int morekeys, > int *status) 2 Return the p osition next (or returned numb er of keywords in the header (not counting the END keyword) and the current in the header. The p osition is the numb er of the keyword record that will b e read one greater than the p osition of the last keyword that was read). A value of 1 is if the p ointer is p ositioned at the b eginning of the header.

int fits_get_hdrpos / ffghps (fitsfile *fptr, > int *keysexist, int *keynum, int *status)

9.3.2

Read and Write the Required Keywords

1 Write the required extension header keywords into the CHU. These routines are not required, and instead the appropriate header may b e constructed by writing each individual keyword in the prop er sequence. The simpler fits write imghdr routine is equivalent to calling fits write grphdr with the default values of simple = TRUE, p count = 0, gcount = 1, and extend = TRUE. The 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. It is recommended that fits create image or fits create tbl b e used instead of these routines to write the required header keywords. The general fits write exthdr routine may b e used to write the header of any conforming FITS extension. int fits_write_imghdr / ffphps


98

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

(fitsfile *fptr, int bitpix, int naxis, long *naxes, > int *status) int fits_write_imghdrll / ffphpsll (fitsfile *fptr, int bitpix, int naxis, LONGLONG *naxes, > int *status) int fits_write_grphdr / ffphpr (fitsfile *fptr, int simple, int bitpix, int naxis, long *naxes, LONGLONG pcount, LONGLONG gcount, int extend, > int *status) int fits_write_grphdrll / ffphprll (fitsfile *fptr, int simple, int bitpix, int naxis, LONGLONG *naxes, LONGLONG pcount, LONGLONG gcount, int extend, > int *status) int fits_write_exthdr /ffphext (fitsfile *fptr, char *xtension, int bitpix, int naxis, long *naxes, LONGLONG pcount, LONGLONG gcount, > int *status)

2 Write the ASCI I table header keywords into the CHU. The optional TUNITn and EXTNAME keywords are written only if the input p ointers are not null. A null p ointer may given for the *tb col parameter in which case a single space will b e inserted b etween each column of the table. Similarly, if rowlen is given = 0, then CFITSIO will calculate the default rowlength based on the tb col and ttyp e values. int fits_write_atblhdr / ffphtb (fitsfile *fptr, LONGLONG rowlen, LONGLONG nrows, int tfields, char **ttype, long *tbcol, char **tform, char **tunit, char *extname, > int *status) 3 Write the binary table header keywords into the CHU. The optional TUNITn and EXTNAME keywords are written only if the input p ointers are not null. The p count parameter, which sp ecifies the size of the variable length array heap, should initially = 0; CFITSIO 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 CFITSIO 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. int fits_write_btblhdr / ffphbn (fitsfile *fptr, LONGLONG nrows, int tfields, char **ttype, char **tform, char **tunit, char *extname, LONGLONG pcount, > int *status)


9.3. SPECIALIZED HEADER KEYWORD ROUTINES

99

4 Read the required keywords from the CHDU (image or table). When reading from an IMAGE extension the SIMPLE and EXTEND parameters are ignored. A null p ointer may b e supplied for any of the returned parameters that are not needed. int fits_read_imghdr / ffghpr (fitsfile *fptr, int maxdim, > int *simple, int *bitpix, int *naxis, long *naxes, long *pcount, long *gcount, int *extend, int *status) int fits_read_imghdrll / ffghprll (fitsfile *fptr, int maxdim, > int *simple, int *bitpix, int *naxis, LONGLONG *naxes, long *pcount, long *gcount, int *extend, int *status) int fit (fi in ch int fit (fi in ch int fit (fi ch lo int fit (fi ch lo s t t a s t t a s t a n s t a n _read_atblhdr / f sfile *fptr,int m *tfields, char * r *extname, int _read_atblhdrll / sfile *fptr,int m *tfields, char * r *extname, int _ s r g _ s r g read_btblhdr / f file *fptr, int **ttype, char * *pcount, int *s read_btblhdrll / file *fptr, int **ttype, char * *pcount, int *s f a * * g x t s h d t t t i y a b m, > long *rowlen, long *nrows, pe, LONGLONG *tbcol, char **tform, char **tunit, tus)

ffghtbll axdim, > LONGLONG *rowlen, LONGLONG *nrows, *ttype, long *tbcol, char **tform, char **tunit, *status) f m * t g a t a h x f t b d o u n im, > long *nrows, int *tfields, rm, char **tunit, char *extname, s)

ffghbnll maxdim, > LONGLONG *nrows, int *tfields, *tform, char **tunit, char *extname, tatus)

9.3.3

Write Keyword Routines

These routines simply app end a new keyword to the header and do not check to see if a keyword with the same name already exists. In general it is preferable to use the fits up date key routine to ensure that the same keyword is not written more than once to the header. See App endix B for the definition of the parameters used in these routines. 1 Write (app end) a new keyword of the appropriate data typ e into the CHU. A null p ointer may b e entered for the comment parameter, which will cause the comment field of the keyword to b e left blank. The flt, dbl, cmp, and dblcmp 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.


100

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

int fits_write_key_str / ffpkys (fitsfile *fptr, char *keyname, char *value, char *comment, > int *status) int fits_write_key_[log, lng] / ffpky[lj] (fitsfile *fptr, char *keyname, DTYPE numval, char *comment, > int *status) int fits_write_key_[flt, dbl, fixflg, fixdbl] / ffpky[edfg] (fitsfile *fptr, char *keyname, DTYPE numval, int decimals, char *comment, > int *status) int fits_write_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffpk[yc,ym,fc,fm] (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals, char *comment, > int *status) 2 Write (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`Local FITS Conventions' section in Chapter 4. Since this uses a non-standard FITS convention to encode the long keyword string, programs which use this routine should also call the fits write key longwarn routine to add some COMMENT keywords to warn users of the FITS file that this convention is b eing used. The fits write key longwarn routine 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 fits write key longwarn will simply return without doing anything. int fits_write_key_longstr / ffpkls (fitsfile *fptr, char *keyname, char *longstr, char *comment, > int *status) int fits_write_key_longwarn / ffplsw (fitsfile *fptr, > int *status) 3 Write (app end) a numb ered sequence of keywords into the CHU. The starting index numb er (nstart) must b e greater than 0. 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. One may also enter a null p ointer for the comment parameter to leave the comment field of the keyword blank. int fits_write_keys_str / ffpkns (fitsfile *fptr, char *keyroot, int nstart, int nkeys, char **value, char **comment, > int *status)


9.3. SPECIALIZED HEADER KEYWORD ROUTINES int fits_write_keys_[log, lng] / ffpkn[lj] (fitsfile *fptr, char *keyroot, int nstart, int nkeys, DTYPE *numval, char **comment, int *status) int fits_write_keys_[flt, dbl, fixflg, fixdbl] / ffpkne[edfg] (fitsfile *fptr, char *keyroot, int nstart, int nkey, DTYPE *numval, int decimals, char **comment, > int *status)

101

4 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. int fits_copy_key / ffcpky (fitsfile *infptr, fitsfile *outfptr, int innum, int outnum, char *keyroot, > int *status) 5 Write (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 ffgkyt routine should b e used to read this keyword value, b ecause the other keyword reading routines will not preserve the full precision of the value. int fits_write_key_triple / ffpkyt (fitsfile *fptr, char *keyname, long intval, double frac, char *comment, > int *status) 6 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 fits parse template routine. int fits_write_key_template / ffpktp (fitsfile *fptr, const char *filename, > int *status)

9.3.4

Insert Keyword Routines

These insert routines are somewhat less efficient than the `up date' or `write' keyword routines b ecause the following keywords in the header must b e shifted down to make room for the inserted keyword. See App endix B for the definition of the parameters used in these routines. 1 Insert a new keyword record into the CHU at the sp ecified p osition (i.e., immediately preceding the (keynum)th keyword in the header.) int fits_insert_record / ffirec (fitsfile *fptr, int keynum, char *card, > int *status)


102

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

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 `longstr' version has the same functionality as the `str' version except that it also supp orts the local long string keyword convention for strings longer than 68 characters. A null p ointer may b e entered for the comment parameter which will cause the comment field to b e left blank. The flt, dbl, cmp, and dblcmp 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. int fits_insert_card / ffikey (fitsfile *fptr, char *card, > &status) int fits_insert_key_[str, longstr] / ffi[kys, kls] (fitsfile *fptr, char *keyname, char *value, char *comment, > int *status) int fits_insert_key_[log, lng] / ffiky[lj] (fitsfile *fptr, char *keyname, DTYPE numval, char *comment, > int *status) int fits_insert_key_[flt, fixflt, dbl, fixdbl] / ffiky[edfg] (fitsfile *fptr, char *keyname, DTYPE numval, int decimals, char *comment, > int *status) int fits_insert_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffik[yc,ym,fc,fm] (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals, char *comment, > int *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. int fits_insert_key_null / ffikyu (fitsfile *fptr, char *keyname, char *comment, > int *status)

9.3.5

Read Keyword Routines

Wild card characters may b e used when sp ecifying the name of the keyword to b e read. 1 Read a keyword value (with the appropriate data typ e) and comment from the CHU. If NULL comment p ointer is given on input, then the comment string will not b e returned. the value of the keyword is not defined (i.e., the value field is blank) then an error status VALUE UNDEFINED will b e returned and the input value will not b e changed (except th ffgkys will reset the value to a null string). a If = at


9.3. SPECIALIZED HEADER KEYWORD ROUTINES int fits_read_key_str / ffgkys (fitsfile *fptr, char *keyname, > char *value, char *comment, int *status); NOTE: after calling the following routine, programs must explicitly free the memory allocated for 'longstr' after it is no longer needed or call fits_free_memory. int fits_read_key_longstr / ffgkls (fitsfile *fptr, char *keyname, > char **longstr, char *comment, int *status) int fits_free_memory / fffree (char *longstr, int *status); int fits_read_key_[log, lng, flt, dbl, cmp, dblcmp] / ffgky[ljedcm] (fitsfile *fptr, char *keyname, > DTYPE *numval, char *comment, int *status) int fits_read_key_lnglng / ffgkyjj (fitsfile *fptr, char *keyname, > LONGLONG *numval, char *comment, int *status)

103

2 Read a sequence of indexed keyword values (e.g., NAXIS1, NAXIS2, ...). The input starting index numb er (nstart) must b e greater than 0. If the value of any of the keywords is not defined (i.e., the value field is blank) then an error status = VALUE UNDEFINED will b e returned and the input value for the undefined keyword(s) will not b e changed. These routines do not supp ort wild card characters in the root name. If there are no indexed keywords in the header with the input root name then these routines do not return a non-zero status value and instead simply return nfound = 0. int fits_read_keys_str / ffgkns (fitsfile *fptr, char *keyname, int nstart, int nkeys, > char **value, int *nfound, int *status) int fits_read_keys_[log, lng, flt, dbl] / ffgkn[ljed] (fitsfile *fptr, char *keyname, int nstart, int nkeys, > DTYPE *numval, int *nfound, int *status) 3 Read the value of a floating p oint keyword, returning the integer and fractional parts of the value in separate routine arguments. This routine may b e used to read any keyword but is esp ecially useful for reading the 'triple precision' keywords written by ffpkyt. int fits_read_key_triple / ffgkyt (fitsfile *fptr, char *keyname, > long *intval, double *frac, char *comment, int *status)


104

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

9.3.6

Modify Keyword Routines

These routines modify the value of an existing keyword. An error is returned if the keyword does not exist. Wild card characters may b e used when sp ecifying the name of the keyword to b e modified. See App endix B for the definition of the parameters used in these routines. 1 Modify (overwrite) the nth 80-character header record in the CHU. int fits_modify_record / ffmrec (fitsfile *fptr, int keynum, char *card, > int *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. int fits_modify_card / ffmcrd (fitsfile *fptr, char *keyname, char *card, > int *status) 5 Modify the value and comment fields of an existing keyword in the CHU. The `longstr' version has the same functionality as the `str' version except that it also supp orts the local long string keyword convention for strings longer than 68 characters. 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 (&) or by entering a null p ointer for the comment parameter. The flt, dbl, cmp, and dblcmp 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. int fits_modify_key_[str, longstr] / ffm[kys, kls] (fitsfile *fptr, char *keyname, char *value, char *comment, > int *status); int fits_modify_key_[log, lng] / ffmky[lj] (fitsfile *fptr, char *keyname, DTYPE numval, char *comment, > int *status) int fits_modify_key_[flt, dbl, fixflt, fixdbl] / ffmky[edfg] (fitsfile *fptr, char *keyname, DTYPE numval, int decimals, char *comment, > int *status) int fits_modify_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffmk[yc,ym,fc,fm] (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals, char *comment, > int *status) 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 (&) or by entering a null p ointer.


9.4. DEFINE DATA SCALING AND UNDEFINED PIXEL PARAMETERS int fits_modify_key_null / ffmkyu (fitsfile *fptr, char *keyname, char *comment, > int *status)

105

9.3.7

Update Keyword Routines

1 These up date routines modify the value, and optionally the comment field, of the keyword if it already exists, otherwise the new keyword is app ended to the header. A separate routine is provided for each keyword data typ e. The `longstr' version has the same functionality as the `str' version except that it also supp orts the local long string keyword convention for strings longer than 68 characters. A null p ointer may b e entered for the comment parameter which will leave the comment field unchanged or blank. The flt, dbl, cmp, and dblcmp 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. int fits_update_key_[str, longstr] / ffu[kys, kls] (fitsfile *fptr, char *keyname, char *value, char *comment, > int *status) int fits_update_key_[log, lng] / ffuky[lj] (fitsfile *fptr, char *keyname, DTYPE numval, char *comment, > int *status) int fits_update_key_[flt, dbl, fixflt, fixdbl] / ffuky[edfg] (fitsfile *fptr, char *keyname, DTYPE numval, int decimals, char *comment, > int *status) int fits_update_key_[cmp, dblcmp, fixcmp, fixdblcmp] / ffuk[yc,ym,fc,fm] (fitsfile *fptr, char *keyname, DTYPE *numval, int decimals, char *comment, > int *status)

9.4

Define Data Scaling and Undefined Pixel Parameters

These routines set or modify the internal parameters used by CFITSIO to either scale the data or to represent undefined pixels. Generally CFITSIO will scale the data according to the values of the BSCALE and BZERO (or TSCALn and TZEROn) keywords, however these routines 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, CFITSIO 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 routines do not create or modify the corresp onding header keyword values. See App endix B for the definition of the parameters used in these routines. 1 Reset the scaling factors in the primary array or image extension; does not change the BSCALE


106 and BZERO elements are data value = is used when

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

keyword values and only affects the automatic scaling p erformed when the data written/read to/from the FITS file. When reading from a FITS file the returned (the value given in the FITS array) * BSCALE + BZERO. The inverse formula writing data values to the FITS file.

int fits_set_bscale / ffpscl (fitsfile *fptr, double scale, double zero, > int *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. int fits_set_tscale / fftscl (fitsfile *fptr, int colnum, double scale, double zero, > int *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, or 32. This does not create or change the value of the BLANK keyword in the header. int fits_set_imgnull / ffpnul (fitsfile *fptr, LONGLONG nulval, > int *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. int fits_set_atblnull / ffsnul (fitsfile *fptr, int colnum, char *nulstr, > int *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', or 'J'). This does not create or change the value of the TNULLn keyword. int fits_set_btblnull / fftnul (fitsfile *fptr, int colnum, LONGLONG nulval, > int *status)

9.5

Sp ecialized FITS Primary Array or IMAGE Extension I/O Routines

These routines read or write data values in the primary data array (i.e., the first HDU in the FITS file) or an IMAGE extension. Automatic data typ e conversion is p erformed for if the data typ e


9.5. SPECIALIZED FITS PRIMARY ARRAY OR IMAGE EXTENSION I/O ROUTINES 107 of the FITS array (as defined by the BITPIX keyword) differs from the data typ e of the array in the calling routine. The data values are automatically scaled by the BSCALE and BZERO header values as they are b eing written or read from the FITS array. Unlike the basic routines describ ed in the previous chapter, most of these routines sp ecifically supp ort the FITS random groups format. See App endix B for the definition of the parameters used in these routines. The more primitive reading and writing routines (i. e., ffppr , ffppn , ffppn, ffgpv , or ffgpf ) simply treat the primary array as a long 1-dimensional array of pixels, ignoring the intrinsic dimensionality of the array. When dealing with a 2D image, for example, the application program must calculate the pixel offset in the 1-D array that corresp onds to any particular X, Y coordinate in the image. C programmers should note that the ordering of arrays in FITS files, and hence in all the CFITSIO calls, is more similar to the dimensionality of arrays in Fortran rather than C. For instance if a FITS image has NAXIS1 = 100 and NAXIS2 = 50, then a 2-D array just large enough to hold the image should b e declared as array[50][100] and not as array[100][50]. For convenience, higher-level routines are also provided to sp ecifically deal with 2D images (ffp2d and ffg2d ) and 3D data cub es (ffp3d and ffg3d ). 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 dimensions of the program array may b e larger than the dimensions of the FITS array. 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). The most general set of routines (ffpss , ffgsv , and ffgsf ) may b e used to 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 fpixel and lpixel parameters are integer arrays which sp ecify the starting and ending pixel coordinate in each dimension (starting with 1, not 0) of the FITS image that is to b e read or written. It is imp ortant to note that these are the starting and ending pixels in the FITS image, not in the declared array in the program. The array parameter in these routines is treated simply as a large one-dimensional array of the appropriate data typ 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 FITS reading routines (ffgsv and ffgsf ) also have an `inc' parameter which defines the data sampling interval in each dimension of the FITS array. For example, if inc[0]=2 and inc[1]=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 to the 'array' parameter. Two typ es of routines are provided to read the data array which differ in the way undefined pixels are handled. The first typ e of routines (e.g., ffgpv ) simply return an array of data elements in which undefined pixels are set equal to a value sp ecified by the user in the `nulval' parameter. An additional feature of these routines is that if the user sets nulval = 0, then no checks for undefined pixels will b e p erformed, thus reducing the amount of CPU processing. The second typ e of routines (e.g., ffgpf ) returns the data element array and, in addition, a char array that indicates whether the value of the corresp onding data pixel is undefined (= 1) or defined (= 0). The latter typ e of routines 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.


108

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

1 Write elements into the FITS data array. int fits_write_img / ffppr (fitsfile *fptr, int datatype, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, int *status); int fits ffpp (fit DTY _ r s P w [ f E rite_img_[ b,i,ui,k,u ile *fptr, *array, > byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / k,j,uj,jj,e,d] long group, LONGLONG firstelem, LONGLONG nelements, int *status);

int fits_write_imgnull / ffppn (fitsfile *fptr, int datatype, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, DTYPE *nulval, > int *status); int fits_write_imgnull_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / ffppn[b,i,ui,k,uk,j,uj,jj,e,d] (fitsfile *fptr, long group, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, DTYPE nulval, > int *status); 2 Set data array elements as undefined. int fits_write_img_null / ffppru (fitsfile *fptr, long group, LONGLONG firstelem, LONGLONG nelements, > int *status) 3 Write values into group parameters. This routine only applies to the `Random Group ed' FITS format which has b een used for applications in radio interferometry, but is officially deprecated for future use. int fits ffpg (fit >D _ p s T w [ f Y r b i P i , l E te_grppa i,ui,k,u e *fptr, *array, r_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / k,j,uj,jj,e,d] long group, long firstelem, long nelements, int *status)

4 Write a 2-D or 3-D image into the data array. int fits ffp2 (fit LON int fits ffp3 (fit LON _ d s G _ d s G w [ f L w [ f L r b i O r b i O i , l N i , l N t i e G t i e G e_2d_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / ,ui,k,uk,j,uj,jj,e,d] *fptr, long group, LONGLONG dim1, LONGLONG naxis1, naxis2, DTYPE *array, > int *status) e_3d_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / ,ui,k,uk,j,uj,jj,e,d] *fptr, long group, LONGLONG dim1, LONGLONG dim2, LONGLONG naxis1, naxis2, LONGLONG naxis3, DTYPE *array, > int *status)


9.5. SPECIALIZED FITS PRIMARY ARRAY OR IMAGE EXTENSION I/O ROUTINES 109 5 Write an arbitrary data subsection into the data array. int fits ffps (fit lon _ s s g write_subset_[byt, sht, [b,i,ui,k,uk,j,uj,jj,e, file *fptr, long group, *fpixel, long *lpixel, usht, int, uint, lng, ulng, lnglng, flt, dbl] / d] long naxis, long *naxes, DTYPE *array, > int *status)

6 Read elements from the FITS data array. int fits_read_img / ffgpv (fitsfile *fptr, int datatype, long firstelem, long nelements, DTYPE *nulval, > DTYPE *array, int *anynul, int *status) int fits ffgp (fit DTY _ v s P r [ f E ead_img_[b b,i,ui,k,u ile *fptr, nulval, > yt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / k,j,uj,jj,e,d] long group, long firstelem, long nelements, DTYPE *array, int *anynul, int *status)

int fits_read_imgnull / ffgpf (fitsfile *fptr, int datatype, long firstelem, long nelements, > DTYPE *array, char *nullarray, int *anynul, int *status) int f f ( > its_ fgpf fits DTY r [ f P e b i E ad_imgnull_[byt, sht, usht, int, uint, lng, ulng, flt, dbl] / ,i,ui,k,uk,j,uj,jj,e,d] le *fptr, long group, long firstelem, long nelements, *array, char *nullarray, int *anynul, int *status)

7 Read values from group parameters. This routine only applies to the `Random Group ed' FITS format which has b een used for applications in radio interferometry, but is officially deprecated for future use. int f f ( > its_ fggp fits DTY r [ f P e b i E ad_grppar_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / ,i,ui,k,uk,j,uj,jj,e,d] le *fptr, long group, long firstelem, long nelements, *array, int *status)

8 Read 2-D or 3-D image from the data array. Undefined pixels in the array will b e set equal to the value of 'nulval', unless nulval=0 in which case no testing for undefined pixels will b e p erformed. int f f ( L i f f O t g i N s 2 t G _ d s L r [ f O e b i N a , l G d_2d_[by i,ui,k,u e *fptr, naxis2, t, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / k,j,uj,jj,e,d] long group, DTYPE nulval, LONGLONG dim1, LONGLONG naxis1, > DTYPE *array, int *anynul, int *status)


110 int f f ( L > i f f O t g i N D s 3 t G T _ d s L Y r [ f O P e b i N E

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES uint, lng, ulng, lnglng, flt, dbl] / nulval, LONGLONG dim1, GLONG naxis2, LONGLONG naxis3, tatus)

ad_3d_[byt, sht, usht, int, ,i,ui,k,uk,j,uj,jj,e,d] le *fptr, long group, DTYPE G dim2, LONGLONG naxis1, LON *array, int *anynul, int *s

9 Read an arbitrary data subsection from the data array. int f f ( l > f f ( l c i f f o t g i n D t g i n a s s t g T s s t g r _read_subset_[byt, sht, ush v[b,i,ui,k,uk,j,uj,jj,e,d] sfile *fptr, int group, int *fpixel, long *lpixel, lon YPE *array, int *anynul, in t, int, uint, lng, ulng, lnglng, flt, dbl] / naxis, long *naxes, g *inc, DTYPE nulval, t *status) ht, int, uint, lng, ulng, lnglng, flt, dbl] / xis, long *naxes, inc, > DTYPE *array, *status)

int

i f f o h

_read_subsetnull_[byt, sht, us f[b,i,ui,k,uk,j,uj,jj,e,d] sfile *fptr, int group, int na *fpixel, long *lpixel, long * *nullarray, int *anynul, int

9.6
9.6.1

Sp ecialized FITS ASCI I and Binary Table Routines
General Column Routines

1 Get information ab out an existing ASCI I or binary table column. A null p ointer may b e given for any of the output parameters that are not needed. DATATYPE is a character string which returns the data typ 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, typ ecode 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 ffgb cl will return typ echar='A8' and rep eat=20. All the returned parameters are scalar quantities. int fits_get_acolparms / ffgacl (fitsfile *fptr, int colnum, > char *ttype, long *tbcol, char *tunit, char *tform, double *scale, double *zero, char *nulstr, char *tdisp, int *status) int fit (fi ch lo s t a n _ s r g get_bcolparms / ffgbcl file *fptr, int colnum, > char *ttype, char *tunit, *typechar, long *repeat, double *scale, double *zero, *nulval, char *tdisp, int *status)

int fits_get_bcolparmsll / ffgbclll (fitsfile *fptr, int colnum, > char *ttype, char *tunit, char *typechar, LONGLONG *repeat, double *scale, double *zero,


9.6. SPECIALIZED FITS ASCI I AND BINARY TABLE ROUTINES LONGLONG *nulval, char *tdisp, int *status)

111

2 Return 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.

int fits_get_rowsize / ffgrsz (fitsfile *fptr, long *nrows, *status) 3 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 routine also automatically writes the value of theap to a keyword in the extension header. This routine must b e called after the required keywords have b een written (with ffphbn) but b efore any data is written to the table. int fits_write_theap / ffpthp (fitsfile *fptr, long theap, > int *status) 4 Test the contents of the binary table variable array heap, returning the size of the heap, the numb er of unused bytes that are not currently p ointed to by any of the descriptors, and the numb er of bytes which are p ointed to by multiple descriptors. It also returns valid = FALSE if any of the descriptors p oint to invalid addresses out of range of the heap. int fits_test_heap / fftheap (fitsfile *fptr, > LONGLONG *heapsize, LONGLONG *unused, LONGLONG *overlap, int *validheap, int *status) 5 Re-pack the vectors in the binary table variable array heap to recover any unused space. Normally, when a vector in a variable length array column is rewritten the previously written array remains in the heap as wasted unused space. This routine will repack the arrays that are still in use, thus eliminating any bytes in the heap that are no longer in use. Note that if several vectors p oint to the same bytes in the heap, then this routine will make duplicate copies of the bytes for each vector, which will actually expand the size of the heap. int fits_compress_heap / ffcmph (fitsfile *fptr, > int *status)

9.6.2

Low-Level Table Access Routines

The following 2 routines 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,


112

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

without regard for column b oundaries or the row length in the table. These routines do not p erform any machine dep endent data conversion or byte swapping. See App endix B for the definition of the parameters used in these routines. 1 Read or write a consecutive array of bytes from an ASCI I or binary table int fits_read_tblbytes / ffgtbb (fitsfile *fptr, LONGLONG firstrow, LONGLONG firstchar, LONGLONG nchars, > unsigned char *values, int *status) int fits_write_tblbytes / ffptbb (fitsfile *fptr, LONGLONG firstrow, LONGLONG firstchar, LONGLONG nchars, unsigned char *values, > int *status)

9.6.3

Write Column Data Routines

1 Write elements into an ASCI I or binary table column (in the CDU). The data typ e of the array is implied by the suffix of the routine name. int fits_write_col_str / ffpcls (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, char **array, > int *status) int fits_write_col_[log,byt,sht,usht,int,uint,lng,ulng,lnglng,flt,dbl,cmp,dblcmp] / ffpcl[l,b,i,ui,k,uk,j,uj,jj,e,d,c,m] (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, DTYPE *array, > int *status) 2 Write elements into an ASCI I or binary table column substituting the appropriate FITS null value for any elements that are equal to the nulval parameter. int fits ffpc (fit LON _ n s G w [ f L r l i O i , l N t b e G e_colnull_[ ,i,ui,k,uk, *fptr, int nelements, log, byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / j,uj,jj,e,d] colnum, LONGLONG firstrow, LONGLONG firstelem, DTYPE *array, DTYPE nulval, > int *status)

3 Write string elements into a binary table column (in the CDU) substituting the FITS null value for any elements that are equal to the nulstr string. int fits_write_colnull_str / ffpcns (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, LONGLONG nelements, char **array, char *nulstr, > int *status)


9.6. SPECIALIZED FITS ASCI I AND BINARY TABLE ROUTINES

113

4 Write bit values into a binary byte ('B') or bit ('X') table column (in the CDU). Larray is an array of characters corresp onding to the sequence of bits to b e written. If an element of larray is true (not equal to zero) then the corresp onding bit in the FITS table is set to 1, otherwise the bit is set to 0. The 'X' column in a FITS table is always padded out to a multiple of 8 bits where the bit array starts with the most significant bit of the byte and works down towards the 1's bit. For example, a '4X' array, with the first bit = 1 and the remaining 3 bits = 0 is equivalent to the 8-bit unsigned byte decimal value of 128 ('1000 0000B'). In the case of 'X' columns, CFITSIO can 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 ffp clx with firstbit=1 and nbits=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 and should all b e set = 0. It should also b e noted that it is more efficient to write 'X' columns an entire byte at a time, instead of bit by bit. Any of the CFITSIO routines that write to columns (e.g. fits write col byt) may b e used for this purp ose. These routines will interpret 'X' columns as though they were 'B' columns (e.g., '1X' through '8X' is equivalent to '1B', and '9X' through '16X' is equivalent to '2B'). int fits_write_col_bit / ffpclx (fitsfile *fptr, int colnum, LONGLONG firstrow, long firstbit, long nbits, char *larray, > int *status) 5 Write the descriptor for a variable length column in a binary table. This routine can b e used in conjunction with ffgdes to enable 2 or more arrays to p oint to the same storage location to save storage space if the arrays are identical. int fits_write_descript / ffpdes (fitsfile *fptr, int colnum, LONGLONG rownum, LONGLONG repeat, LONGLONG offset, > int *status)

9.6.4

Read Column Data Routines

Two typ es of routines are provided to get the column data which differ in the way undefined pixels are handled. The first set of routines (ffgcv) 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. If 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 (ffgcf ) returns the data element array and in addition a logical array of flags which defines whether the corresp onding data pixel is undefined. See App endix B for the definition of the parameters used in these routines. Any column, regardless of it's intrinsic data typ 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 data typ e of the column. The length of the returned strings (not including the null terminating character) can b e determined with the fits get col display width routine. The following TDISPn display formats are currently supp orted:


114 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

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

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. 1 Read 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 = nulval, unless nulval = 0 (or = ' ' for ffgcvs) 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. int fit (fi LO in s t N t _read_col_str / ffgcvs sfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, GLONG nelements, char *nulstr, > char **array, int *anynul, *status) _read_col_[log,b v[l,b,i,ui,k,uk, sfile *fptr, int GLONG nelements, *status) yt,sht,usht,int,uint,lng,ulng, lnglng, flt, dbl, cmp, dblcmp] / j,uj,jj,e,d,c,m] colnum, LONGLONG firstrow, LONGLONG firstelem, DTYPE nulval, > DTYPE *array, int *anynul,

int fits ffgc (fit LON int

2 Read 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 nullarray element set equal to TRUE. The anynul parameter is set to true if any of the returned elements are undefined. int fit (fi LO in s t N t _read_colnull_str / ffgcfs sfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstelem, GLONG nelements, > char **array, char *nullarray, int *anynul, *status) _ f s G r read_colnull_[log,byt,sht,usht,int,uint,lng,ulng,lnglng,flt,dbl,cmp,dblcmp] / [l,b,i,ui,k,uk,j,uj,jj,e,d,c,m] file *fptr, int colnum, LONGLONG firstrow, LONG firstelem, LONGLONG nelements, > DTYPE *array, *nullarray, int *anynul, int *status)

int fits ffgc (fit LON cha

3 Read 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 'nulval', unless nulval=0 in which


9.6. SPECIALIZED FITS ASCI I AND BINARY TABLE ROUTINES case no testing for undefined pixels will b e p erformed. The fir to b e read are sp ecified by fpixel(naxis+1) and lpixel(naxis+1), next higher dimension of the FITS N-dimensional array. The sampling interval in each dimension b etween the data elements int fits ffgs (fit lon int

115

st and last rows in the table and hence are treated as the INC parameter sp ecifies the that will b e returned.

_read_subset_[byt, sht, usht, int, uint, lng, ulng, lnglng, flt, dbl] / v[b,i,ui,k,uk,j,uj,jj,e,d] sfile *fptr, int colnum, int naxis, long *naxes, long *fpixel, g *lpixel, long *inc, DTYPE nulval, > DTYPE *array, int *anynul, *status)

4 Read 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 'nullarray' element set equal to TRUE. The first and last rows in the table to b e read are sp ecified by fpixel(naxis+1) and lpixel(naxis+1), and hence are treated as the next higher dimension of the FITS Ndimensional array. The INC parameter sp ecifies the sampling interval in each dimension b etween the data elements that will b e returned. int fits ffgs (fit lon cha _ f s g r read_subsetnull_[byt, sht, [b,i,ui,k,uk,j,uj,jj,e,d] file *fptr, int colnum, int *fpixel, long *lpixel, lon *nullarray, int *anynul, i usht, int, uint, lng, ulng, lnglng, flt, dbl] / naxis, long *naxes, g *inc, > DTYPE *array, nt *status)

5 Read bit values from a byte ('B') or bit (`X`) table column (in the CDU). Larray is an array of logical values corresp onding to the sequence of bits to b e read. If larray is true then the corresp onding bit was set to 1, otherwise the bit was set to 0. The 'X' column in a FITS table is always padded out to a multiple of 8 bits where the bit array starts with the most significant bit of the byte and works down towards the 1's bit. For example, a '4X' array, with the first bit = 1 and the remaining 3 bits = 0 is equivalent to the 8-bit unsigned byte value of 128. Note that in the case of 'X' columns, CFITSIO can 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 ffgcx with firstbit=1 and nbits=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. It should also b e noted that it is more efficient to read 'X' columns an entire byte at a time, instead of bit by bit. Any of the CFITSIO routines that read columns (e.g. fits read col byt) may b e used for this purp ose. These routines will interpret 'X' columns as though they were 'B' columns (e.g., '8X' is equivalent to '1B', and '16X' is equivalent to '2B'). int fits_read_col_bit / ffgcx (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG firstbit, LONGLONG nbits, > char *larray, int *status) 6 Read any consecutive set of bits from an 'X' or 'B' column and interpret them as an unsigned n-bit integer. nbits must b e less than 16 or 32 in ffgcxui and ffgcxuk, resp ectively. If nrows


116

CHAPTER 9.

SPECIALIZED CFITSIO INTERFACE ROUTINES

is greater than 1, then the same set of bits will b e read from each row, starting with firstrow. The bits are numb ered with 1 = the most significant bit of the first element of the column. int fits_read_col_bit_[usht, uint] / ffgcx[ui,uk] (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG, nrows, long firstbit, long nbits, > DTYPE *array, int *status) 7 Return 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 pair of routine returns a single descriptor whereas the second pair of routine returns the descriptors for a range of rows in the table. The only difference b etween the 2 routines in each pair is that one returns the parameters as 'long' integers, whereas the other returns the values as 64-bit 'LONGLONG' integers. int fits_read_descript / ffgdes (fitsfile *fptr, int colnum, LONGLONG rownum, > long *repeat, long *offset, int *status) int fits_read_descriptll / ffgdesll (fitsfile *fptr, int colnum, LONGLONG rownum, > LONGLONG *repeat, LONGLONG *offset, int *status) int fits_read_descripts / ffgdess (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG nrows > long *repeat, long *offset, int *status) int fits_read_descriptsll / ffgdessll (fitsfile *fptr, int colnum, LONGLONG firstrow, LONGLONG nrows > LONGLONG *repeat, LONGLONG *offset, int *status)


Chapter 10

Extended File Name Syntax
10.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 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 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. 117


118

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

The latter 3 table filtering 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 with 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. See also the stream:// driver which provides a more efficient, but more restricted method of reading or writing to the stdin or stdout streams. · 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.


10.1. OVERVIEW

119

· 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 virtual file that is op ened by the application just contains this single image in the primary array. · 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 input image in this case. · myfile.fits[EVENTS][col Rad = the fly that is identical to myfile.fits extension called 'Rad' whose value function of the values in the X and sqrt(X**2 + Y**2)]: creates and op ens a virtual file on except that it will contain a new column in the EVENTS is computed using the indicated expression which is a Y columns.

· myfile.fits[EVENTS][PHA > 5]: creates and op ens a virtual 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)[compress] When opening an existing primary array or image HDU: filetype://BaseFilename(outName)[HDUlocation][ImageSection][pixFilter]


120

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

When opening an existing table HDU: filetype://BaseFilename(outName)[HDUlocation][colFilter][rowFilter][binSpec] The filetyp e, present, must in any order. first by CFIT BaseFilename, outName, HDUlocation, ImageSection, and pixFilter comp onents, if b e given in that order, but the colFilter, rowFilter, and binSp ec sp ecifiers may follow Regardless of the order, however, the colFilter sp ecifier, if present, will b e processed SIO, followed by the rowFilter sp ecifier, and finally by the binSp ec sp ecifier.

10.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 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. http:// - 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). stream:// - special driver to read an input FITS file from the stdin stream, and/or write an output FITS file to the stdout stream. This driver is fragile and has limited functionality (see the following note). gsiftp:// - access files on a computational grid using the gridftp protocol in the Globus toolkit (see following note). root:// - uses the CERN root protocol for writing as well as reading files over the network (see following note). shmem:// - opens or creates a file which persists in the computer's shared memory (see following note). mem:// - 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. 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. file:// ftp://

10.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


10.2. FILETYPE setenv http_proxy http://heasarc.gsfc.nasa.gov:3128

121

will cause CFITSIO to use p ort 3128 on the heasarc proxy server whenever reading a FITS file with HTTP.

10.2.2

Notes about the stream filetype driver

The stream driver can b e used to efficiently read a FITS file from the stdin file stream or write a FITS to the stdout file stream. However, b ecause these input and output streams must b e accessed sequentially, the FITS file reading or writing application must also read and write the file sequentially, at least within the tolerances describ ed b elow. CFITSIO supp orts 2 different methods for accessing FITS files on the stdin and stdout streams. The original method, which is invoked by sp ecifying a dash character, "-", as the name of the file when op ening or creating it, works by storing a complete copy of the entire FITS file in memory. In this case, when reading from stdin, CFITSIO will copy the entire stream into memory b efore doing any processing of the file. Similarly, when writing to stdout, CFITSIO will create a copy of the entire FITS file in memory, b efore finally flushing it out to the stdout stream when the FITS file is closed. Buffering the entire FITS file in this way allows the application to randomly access any part of the FITS file, in any order, but it also requires that the user have sufficient available memory (or virtual memory) to store the entire file, which may not b e p ossible in the case of very large files. The newer stream filetyp e provides a more stdin or stdout streams. Instead of storin only uses a set of internal buffer which by of the FITS file. The application program to end, within this 100K buffer. Generally following restrictions: memory-efficient method of accessing FITS files on the g a copy of the entire FITS file in memory, CFITSIO default can store 40 FITS blocks, or ab out 100K bytes must process the FITS file sequentially from b eginning sp eaking the application program must conform to the

· The program must finish reading or writing the header keywords b efore reading or writing any data in the HDU. · The HDU can contain at most ab out 1400 header keywords. This is the maximum that can fit in the nominal 40 FITS block buffer. In principle, this limit could b e increased by recompiling CFITSIO with a larger buffer limit, which is set by the NIOBUF parameter in fitsio2.h. · The program must read or write the data in a sequential manner from the b eginning to the end of the HDU. Note that CFITSIO's internal 100K buffer allows a little latitude in meeting this requirement. · The program cannot move back to a previous HDU in the FITS file. · Reading or writing of variable length array columns in binary tables is not supp orted on streams, b ecause this requires moving back and forth b etween the fixed-length p ortion of the binary table and the following heap area where the arrays are actually stored. · Reading or writing of tile-compressed images is not supp orted on streams, b ecause the images are internally stored using variable length arrays.


122

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

10.2.3

Notes about the gsiftp filetype

DEPENDENCIES: Globus toolkit (2.4.3 or higher) (GT) should b e installed. There are two different ways to install GT: 1) goto the globus toolkit web page www.globus.org and follow the download and compilation instructions; 2) goto the Virtual Data Toolkit web page http://vdt.cs.wisc.edu/ and follow the instructions (STRONGLY SUGGESTED); Once a globus client has b een installed in your system with a sp ecific flavour it is p ossible to compile and install the CFITSIO libraries. Sp ecific configuration flags must b e used: 1) ­with-gsiftp[[=PATH]] Enable Globus Toolkit gsiftp protocol supp ort PATH=GLOBUS LOCATION i.e. the location of your globus installation 2) ­with-gsiftp-flavour[[=PATH] defines the sp ecific Globus flavour ex. gcc32 Both the flags must b e used and it is mandatory to set b oth the PATH and the flavour. USAGE: To access files on a gridftp server it is necessary to use a gsiftp prefix: example: gsiftp://remote server fqhn/directory/filename The gridftp driver uses a local buffer on a temp orary file the file is located in the /tmp directory. If you have sp ecial p ermissions on /tmp or you do not have a /tmp directory, it is p ossible to force another location setting the GSIFTP TMPFILE environment variable (ex. exp ort GSIFTP TMPFILE=/your/location/yourtmpfile). Grid FTP supp orts multi channel transfer. By default a single channel transmission is available. However, it is p ossible to modify this b ehavior setting the GSIFTP STREAMS environment variable (ex. exp ort GSIFTP STREAMS=8).

10.2.4

Notes about the root filetype
al rootd server can b e obtained from: ftp://root.cern.ch/root/rootd.tar.gz but, for correctly with CFITSIO one has to use a modified version which supp orts a command the length of the file. This modified version is available in rootd sub directory in the ftp area at

The origin it to work to return CFITSIO

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:


10.2. FILETYPE rootd stream tcp nowait root /user/rdm/root/bin/rootd rootd -i Force inetd running dir start rootd will go into Roo to reread ectly und listening backgrou

123

its conf file with kill -HUP . You can also start rootd by hand er your private account (no root system privileges needed). For example to on p ort 5151 just typ e: rootd -p 5151 Notice that no & is needed. Rootd nd by itself.

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 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


124

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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.

10.2.5

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. 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

10.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:


10.3. BASE FILENAME 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

125

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. 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 data typ 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


126

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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 data typ 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 task1 - | task2 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.

10.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.


10.4. OUTPUT FILE NAME WHEN OPENING AN EXISTING FILE

127

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. The output filename "mem://" is also allowed, which will write the output file into memory, and also allow write access to the file. This 'file' will disapp ear when it is closed, but this may b e useful for some applications which only need to modify a temp orary copy of the file. 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: · 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


128

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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.

10.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 in the following Template Files chapter.

10.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

10.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.


10.7. HDU LOCATION SPECIFICATION

129

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. An optional p ound sign character ("#") may b e app ended to the extension name or numb er to signify that any other extensions in the file should b e ignored during any subsequent file filtering op erations. For example, when doing row filtering op erations on a table extension, CFITSIO normally creates a copy of the filtered table in memory, along with a verbatim copy of all the other extensions in the input FITS file. If the p ound sign is app ended to the table extension name, then only that extension, and none of the other extensions in the file, will by copied to memory, as in the following example: myfile.fit[events#][TIME > 10000] 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 greater than 100. 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 -


130

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

10.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. (If a `#' character is app ended to the name or numb er of the image HDU, as in "myfile.fits[1#][1:200,1:200]", then the other HDUs in the input file will not b e copied into memory). 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.


10.9. IMAGE TRANSFORM FILTERS

131

10.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.]


132

CHAPTER 10.

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.

10.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. (If a `#' character is app ended to the name or numb er of the table HDU then only the primary array, and none of the other HDUs in the input file will b e copied into memory). 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


10.10. COLUMN AND KEYWORD FILTERING SPECIFICATION

133

names to match multiple columns. 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 (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 data typ 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 data typ 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 ASCI I tables). If the data typ e is not sp ecified then an appropriate data typ 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 data typ 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.


134

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

For complex or commonly used op erations, one can 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 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.

10.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. (If a `#' character is app ended to the name or numb er of the table HDU then only the primary


10.11. ROW FILTERING SPECIFICATION

135

array, and none of the other HDUs in the input file will b e copied into 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). 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.

10.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.


136 The expression may radians, not degrees. (function names are such as divide by zer " " " " " " " " " " " " " " " " " " " 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 10.

EXTENDED FILE NAME SYNTAX

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).

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"

* c t a a s r c s l r r m s

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

^ (x (x si ta h( nd l( t( 10 do do (x di

) ) n n x ( x x ( m m , f

( 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.


10.11. ROW FILTERING SPECIFICATION

137

In the if-then-else expression, "b?x:y", b is an explicit b oolean value or expression. There is no automatic typ e conversion from numeric to b oolean values, so one needs to use "iVal!=0" instead of merely "iVal" as the b oolean argument. x and y can b e any scalar data typ e (including string). 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, 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 inclosing 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.


138

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

10.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: binary: octal: hex: 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.

10.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


10.11. ROW FILTERING SPECIFICATION

139

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. 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 V is a b oolean vector, SUM returns the numb er of TRUE elements. The NELEM function returns the numb er of elements in vector V 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].


140

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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 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 data typ 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.

10.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" ] ] ] ) or 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


10.11. ROW FILTERING SPECIFICATION

141

TIMEZERO/I/F keywords in the current and GTI extensions, applying a relative time offset, if necessary.

10.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


142

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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. 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 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 semima 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 low-level functions that are primarily for use with regfilter function, but they can b e called directly. They return a b oolean true or false dep ending on whether a two dimensional p oint is in the region or not. The p ositional coordinates must b e given in pixel units: "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)


10.11. ROW FILTERING SPECIFICATION

143

"point in a rectangular region" box(xcntr,ycntr,xfll_wdth,yfll_wdth,rotation,Xcolumn,Ycolumn) 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

10.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

[@rowFilter.txt]


144

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

contained within the text file rowFilter.txt [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 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

10.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 data typ 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


10.12.

BINNING OR HISTOGRAMMING SPECIFICATION

145

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. 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.


146

CHAPTER 10.

EXTENDED FILE NAME SYNTAX

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 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 11

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.

11.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. 147


148

CHAPTER 11. 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.

11.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


11.3. TEMPLATE PARSER DIRECTIVES

149

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

11.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.

11.4

Formal Template Syntax

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


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

CHAPTER 11. 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.

11.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.

11.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


11.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

151

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.


152

CHAPTER 11. TEMPLATE FILES


Chapter 12

Local FITS Conventions
CFITSIO sup standard and Programmers produced are interface. 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

12.1

64-Bit Long Integers

CFITSIO supp orts reading and writing FITS images or table columns containing 64-bit integer data values. Supp ort for 64-bit integers was added to the official FITS Standard in Decemb er 2005. FITS 64-bit images have BITPIX = 64, and the 64-bit binary table columns have TFORMn = 'K'. CFITSIO also supp orts the 'Q' variable-length array table column format which is analogous to the 'P' column format except that the array descriptor is stored as a pair of 64-bit integers. For the convenience of C programmers, the fitsio.h include file defines (with a typ edef statement) the 'LONGLONG' datatyp e to b e equivalent to an appropriate 64-bit integer datatyp e on each platform. Since there is currently no universal standard for the name of the 64-bit integer datatyp e (it might b e defined as 'long long', 'long', or ' int64' dep ending on the platform) C programmers may prefer to use the 'LONGLONG' datatyp e when declaring or allocating 64-bit integer quantities when writing code which needs to run on multiple platforms. Note that CFITSIO will implicitly convert the datatyp e when reading or writing FITS 64-bit integer images and columns with data arrays of a different integer or floating p oint datatyp e, but there is an increased risk of loss of numerical precision or numerical overflow in this case.

12.2

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 CFITSIO supp orts a local convention in which the string value is continued over multiple keywords. This continuation convention uses an amp ersand character at 153


154

CHAPTER 12.

LOCAL FITS CONVENTIONS the continuation The string value is required. The of the STRKEY

the end of each substring to indicate that it is continued on the next keyword, and keywords all have the name CONTINUE without an equal sign in column 9. may b e continued in this way over as many additional CONTINUE keywords as following lines illustrate this continuation convention which is used in the value 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' / is a very ue that is header.' / The OGIP Lon long string continued ov This is ano g k e t

String Convention may be used. eyword&' / Optional Comment r 3 keywords in the & ' her optional comment.

It is recommended that the LONGSTRN keyword, as shown here, always b e included in any HDU that uses this longstring convention as a warning to any software that must read the keywords. A routine called fits write key longwarn 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 CFITSIO routines: f f f f f f i i i i i i t t t t t t s s s s s s _ _ _ _ _ _ w i m u r d r n o p e e i s d d a l t e i a d e e r f t _ t _ t y e k e k _ _ _ e _ e k k k y k y e e e _ e _ y y y l y l _ _ _ o o l l l n n o o o g g n n n s s g g g t t s s s r r t t t - wr r - in r - mo r - mo - re - de i s d d a l t e i i d e a t y y a te e r f f long stri a long str a long str a long str long stri a keyword n i i i n g n n n g k g g g k e k k k e y e e e y w y y y w o w w w o r o o o r d r r r d v d d d v a v v v a l a a a l u l l l u e ue ue ue e

The fits read key longstr routine is unique among all the CFITSIO routines in that it internally allocates memory for the long string value; all the other CFITSIO routines that deal with arrays require that the calling program pre-allocate adequate space to hold the array of data. Consequently, programs which use the fits read key longstr routine must b e careful to free the allocated memory for the string when it is no longer needed. The following 2 routines also have limited supp ort for this long string convention, fits_modify_key_str - modify an existing string keyword value fits_update_key_str - update a string keyword value in that they will correctly overwrite an existing long string value, but the new string value is limited to a maximum of 68 characters in length. The more commonly used CFITSIO routines to write string valued keywords (fits up date key and fits write key) do not supp ort this long string convention and only supp ort 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 fits write key longstr routine 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.


12.3. ARRAYS OF FIXED-LENGTH STRINGS IN BINARY TABLES

155

12.3

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. CFITSIO 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 CFITSIO routines that read or write strings in binary tables. The Binary Table definition document sp ecifies that other optional characters may follow the data typ e code in the TFORM keyword, so this local convention is in compliance with the FITS standard although other FITS readers may not 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. CFITSIO does not currently supp ort this prop osal.

12.4

Keyword Units Strings

One limitation 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 problem, CFITSIO 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 fits read key unit and fits write key unit routines in CFITSIO read and write, resp ectively, the keyword unit strings in an existing keyword.


156

CHAPTER 12.

LOCAL FITS CONVENTIONS

12.5

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 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.

12.6

Tile-Compressed Image Format

CFITSIO supp orts a convention for compressing n-dimensional images and storing the resulting byte stream in a variable-length column in a FITS binary table. The general principle used in this convention is to first divide the n-dimensional image into a rectangular grid of subimages or


12.6. TILE-COMPRESSED IMAGE FORMAT

157

`tiles'. Each tile is then compressed as a continuous block of data, and the resulting compressed byte stream is stored in a row of a variable length column in a FITS binary table. By dividing the image into tiles it is generally p ossible to extract and uncompress subsections of the image without having to uncompress the whole image. The default tiling pattern treats each row of a 2-dimensional image (or higher dimensional cub e) as a tile, such that each tile contains NAXIS1 pixels (except the default with the HCOMPRESS algorithm is to compress the whole 2D image as a single tile). Any other rectangular tiling pattern may also b e defined. In the case of relatively small images it may b e sufficient to compress the entire image as a single tile, resulting in an output binary table with 1 row. In the case of 3-dimensional data cub es, it may b e advantageous to treat each plane of the cub e as a separate tile if application software typically needs to access the cub e on a plane by plane basis. See section 5.6 "Image Compression" for more information on using this tile-compressed image format.


158

CHAPTER 12.

LOCAL FITS CONVENTIONS


Chapter 13

Optimizing Programs
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.

13.1

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 by default. 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 159


160

CHAPTER 13.

OPTIMIZING PROGRAMS

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 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.

13.2

Optimization Strategies

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 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 IO 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 IO 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


13.2. OPTIMIZATION STRATEGIES

161

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 and on the numb er of IO buffers that have b een allocated in CFITSIO. The CFITSIO Iterator routine will automatically use the optimal-sized buffer, but there is also a CFITSIO routine that will return the optimal numb er of rows for a given table: fits get rowsize. 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 program is only reading table header would caus from the original table, read or written. rows returned or writing data e additional blo and should b e by fits get rowsize is valid only as long as the application in the sp ecified table. Any other calls to access data in the cks of data to b e loaded into the IO buffers displacing data avoided during the critical p eriod while the table is b eing

3. Use the CFITSIO Iterator routine. This routine provides a more `ob ject oriented' way of reading and writing FITS files which automatically uses the most appropriate data buffer size to achieve the maximum I/O throughput. 4. 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. 5. Design software so that it reads the FITS header keywords in the same order in which they occur in the file. When reading keywords, CFITSIO 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 IO buffers, then the header keyword access will b e very fast and it makes little difference which order they are accessed. 6. Avoid the use of scaling (by using the BSCALE and BZERO or TSCAL an in FITS files since the scaling op erations add to the processing time needed data. In some cases it may b e more efficient to temp orarily turn off the scaling or fits set tscale) and then read or write the raw unscaled values in the FITS d TZERO keywords) to read or write the (using fits set bscale file.

7. Avoid using the `implicit data typ e conversion' capability in CFITSIO. 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 CFITSIO to convert the data to a different data typ e can slow the program. 8. 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


162

CHAPTER 13.

OPTIMIZING PROGRAMS

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. 9. 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. 10. 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 CFITSIO routines fits read tblbytes and fits write tblbytes 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 for even greater efficiency) of a table with a single function 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 CFITSIO, and they allow the program to write the data to the output file in exactly the same byte order. For these same reasons, these routines can corrupt the FITS data file if used incorrectly b ecause no validation or machine dep endent conversion is p erformed by these routines. These routines are only recommended for optimizing critical pieces of code and should only b e used by programmers who thoroughly understand the internal format of the FITS tables they are reading or writing. 11. 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 fits write tblbytes. 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?). 12. Finally, external factors such as the sp eed of the magnetic disk, the size of the disk cache, 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, the entire hardware and software system should b e reviewed to identify any p otential I/O b ottlenecks.


App endix A

Index of Routines
fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts add group memb er ascii tform binary tform calculator calculator rng calc binning calc rows change group clear errmark clear errmsg close file compact group compare str compress heap convert hdr2str copy cell2image copy col copy data copy file copy group copy hdu copy header copy image2cell copy image section copy key copy memb er copy pixlist2image copy rows create diskfile create file create group create hdu 88 66 66 56 57 57 56 86 30 30 33 87 63 111 37, 82 41 53 96 34 87 34 34 41 44 101 89 58 53 32 32 86 95 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts create img create memfile create tbl create template date2str decode chksum decode tdim delete col delete file delete hdu delete key delete record delete rowlist delete rowrange delete rows delete str encode chksum file exists file mode file name find first row find nextkey find rows flush buffer flush file free memory get acolparms get b colparms get chksum get col display width get colname get colnum 163 41 92 49 92 61 61 51 52 33 35 39 39 52 52 52 39 60 94 33 33 56 37 56 94 94 102 110 110 60 51 49 49 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts get coltyp e get compression typ e get eqcoltyp e get errstatus get hdrp os get hdrspace get hdu num get hdu typ e get hduaddr get hduaddrll get img dim get img equivtyp e get img param get img size get img typ e get inttyp e get keyclass get keyname get keytyp e get noise bits get num cols get num groups get num hdus get num memb ers get num rows get rowsize get system time get tile dim get tb col get version hdr2str insert atbl 50 46 50 29 97 35 34 34 95 95 41 40 41 41 40 65 65 64 64 46 49 89 34 88 49 111 61 46 66 62 37, 82 95


164 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts insert btbl insert col insert cols insert group insert img insert key null insert key TYP insert record insert rows iterate data make hist make keyn make nkey merge groups modify card modify comment modify key null modify key TYP modify name modify record modify vector len movabs hdu movnam hdu movrel hdu null check op en data op en diskfile op en file op en image op en table op en group op en memb er op en memfile parse extnum parse input filename parse input url parse range parse rootname parse template parse value pix to world read 2d TYP read 3d TYP read atblhdr read btblhdr read card read col read col bit 95 52 52 86 95 102 102 101 52 79 59 64 64 87 104 39 104 104 39 104 53 33 33 33 63 30 30 30 30 30 88 89 91 93 93 93 70 93 66 64 83 109 109 99 99 36 55 115 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r s s s s s s s s

APPENDIX A. INDEX OF ROUTINES ead col TYP ead colnull ead colnull TYP ead descript ead descripts ead errmsg ead ext ead grppar TYP ead img ead img coord ead img TYP ead imghdr ead imgnull ead imgnull TYP ead key ead key longstr ead key triple ead key unit ead key TYP ead keyn ead keys TYP ead keyword ead pix ead pixnull ead record ead str ead subset TYP ead subsetnull TYP ead tbl coord ead tblbytes ead tdim ead wcstab ebin wcs emove group emove memb er eop en file ep ort error esize img ms float ms short elect rows et atblnull et bscale et btblnull et compression typ e et hdrsize et hdustruc et imgnull 114 55 114 116 116 30 96 109 109 83 109 99 109 109 36 102 103 37 102 36 103 36 43 43 36 36 110 115 110 115 83 112 51 82 58 87 90 92 30 96 71 71 56 106 106 106 46 97 97 106 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts set noise bits set tile dim set tscale split names str2date str2time test expr test heap test keyword test record time2str transfer memb er translate keyword up date card up date chksum up date key up date key longstr up date key null up date key TYP upp ercase url typ e verify chksum verify group world to pix write 2d TYP write 3d TYP write atblhdr write btblhdr write chksum write col write col bit write col TYP write col null write colnull write colnull TYP write comment write date write descript write errmark write errmsg write ext write exthdr write grphdr write grppar TYP write hdu write history write img write img null 46 46 106 63 61 61 57 111 63 63 61 89 69 39 60 38 105 38 105 62 33 60 88 83 108 108 98 98 60 54 113 112 54 54 112 38 38 113 30 62 96 97 97 108 34 38 108 108


165 fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts ts w w w w w w w w w w w w w w w w w w w w w w w w r r r r r r r r r r r r r r r r r r r r r r r r ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite ite img TYP imghdr imgnull imgnull TYP key key longstr key longwarn key null key template key triple key unit key TYP keys TYP keys histo null img nullrows pix pixnull record subset subset TYP tblbytes tdim theap 108 97 108 108 38 100 100 38 101 101 39 99 100 58 43 54 42 42 39 42 109 112 51 111


166 ffasfm ffbnfm ffcalc ffcalc rng ffclos ffcmph ffcmps ffcmrk ffcmsg ffcopy ffcp cl ffcp dt ffcpfl ffcphd ffcpimg ffcpky ffcprw ffcrhd ffcrim ffcrow ffcrtb ffdcol ffdelt ffdhdu ffdkey ffdkinit ffdkop en ffdopn ffdrec ffdrow ffdrrg ffdrws ffdstr ffdsum ffdt2s ffdtdm ffdtyp ffeqty ffesum ffexest ffextn ffffrw ffflmd ffflnm ffflsh ffflus fffree fffrow 66 66 56 57 33 111 63 30 30 34 53 96 34 34 44 101 53 95 41 56 49 52 33 35 39 32 30 30 39 52 52 52 39 61 61 51 64 50 60 94 93 56 33 33 94 94 102 56 ffg2d ffg3d ffgab c ffgacl ffgb cl ffgcdw ffgcf ffgcf ffgcks ffgcnn ffgcno ffgcrd ffgcv ffgcv ffgcx ffgdes ffgdess ffgerr ffgextn ffggp ffghad ffghbn ffghdn ffghdt ffghpr ffghps ffghsp ffghtb ffgics ffgidm ffgidt ffgiet ffgipr ffgisz ffgkcl ffgkey ffgkls ffgkn ffgknm ffgky ffgkyn ffgkyt ffgky ffgmcp ffgmng ffgmop ffgmrm ffgmsg 109 109 66 110 110 51 55 114 60 49 49 36 55 114 115 116 116 29 96 109 95 99 34 34 99 97 35 99 83 41 40 40 41 41 65 36 102 103 64 36 36 103 102 89 89 89 90 30 ffgmtf ffgncl ffgnrw ffgnxk ffgpf ffgpf ffgpv ffgpv ffgpxv ffgpxf ffgrec ffgrsz ffgsdt ffgsf ffgstm ffgstr ffgsv ffgtam ffgtbb ffgtch ffgtcl ffgtcm ffgtcp ffgtcr ffgtcs ffgtdm ffgthd ffgtis ffgtmg ffgtnm ffgtop ffgtrm ffgtvf ffgunt ffhdef ffibin fficls fficol ffifile ffiimg ffikls ffi kyu ffi ky ffimem ffinit ffinttyp ffiopn ffirec 89 49 49 37 109 109 109 109 43 43 36 111 61 110 115 61 36 110 115 88 112 86 50 87 87 86 83 51 66 86 87 88 88 87 88 37 97 95 52 52 93 95 102 102 102 92 32 65 30 101

APPENDIX A. INDEX OF ROUTINES ffirow ffitab ffiter ffiu r l ffkeyn ffmahd ffmcom ffmcrd ffmkls ffmkyu ffmky ffmnam ffmnhd ffmrec ffmrhd ffmvec ffnchk ffnkey ffomem ffop en ffp2d ffp3d ffp cks ffp cl ffp cls ffp cl ffp clu ffp cn ffp cn ffp com ffp dat ffp des ffp extn ffpgp ffphbn ffphext ffphis ffphpr ffphps ffphtb ffpkls ffpkn ffpktp ffpky ffpkyt ffpkyu ffpky ffplsw 52 95 79 93 64 33 39 104 104 104 104 39 33 104 33 53 63 64 91 30 108 108 60 54 112 113 54 54 112 38 38 113 96 108 98 97 38 97 97 98 100 100 101 38 101 38 99 100 ffpmrk ffpmsg ffpnul ffppn ffppn ffppr ffpprn ffppru ffppr ffppx ffppxn ffprec ffprwu ffpscl ffpss ffpss ffpsvc ffptbb ffptdm ffpthp ffpunt ffrdef ffreop en ffrprt ffrsim ffrtnm ffrwrg ffs2dt ffs2tm ffsnul ffsrow fftexp ffthdu fftheap fftkey fftm2s fftnul fftopn fftplt fftrec fftscl ffucrd ffukls ffuky ffukyu ffuky ffup ch ffup ck 30 62 106 108 108 108 43 108 108 42 42 39 54 106 42 109 64 112 51 111 39 97 92 30 96 93 70 61 61 106 56 57 34 111 63 61 106 30 92 63 106 39 105 38 38 105 62 60


167 ffurlt ffvcks ffvers ffwldp ffwrhdu ffxypx 33 60 62 83 34 83


168

APPENDIX A. INDEX OF ROUTINES


App endix B

Parameter Definitions
a a a b b n r s i i y r c n t n a i s p ul y i pec ix s a e t b e r n h i to TRUE (=1) if any returned values are undefined, else FALSE ay of numerical data values to read or write oded checksum string input table binning specifier s per pixel. The following symbolic mnemonics are predefined: BYTE_IMG = 8 (unsigned char) SHORT_IMG = 16 (signed short integer) LONG_IMG = 32 (signed long integer) LONGLONG_IMG = 64 (signed long 64-bit integer) FLOAT_IMG = -32 (float) DOUBLE_IMG = -64 (double). e LONGLONG_IMG type is experimental and is not officially cognized in the FITS Standard document. o additional values, USHORT_IMG and ULONG_IMG are also available r creating unsigned integer images. These are equivalent to eating a signed integer image with BZERO offset keyword values 32768 or 2147483648, respectively, which is the convention that TS uses to store unsigned integers. ader record to be read or written (80 char max, null-terminated) SESEN (=1) for case-sensitive string matching, else CASEINSEN (=0) ouping table "compact" option parameter. Allowed values are: T_CMT_MBR and OPT_CMT_MBR_DEL. me of the column (null-terminated) lumn number (first column = 1) e input file column specification; used to delete, create, or rename ble columns e keyword comment field (72 char max, null-terminated) ould the checksum be complemented? mpression algorithm to use: GZIP_1, RICE_1, HCOMPRESS_1, or PLIO_1 pe of coordinate projection (-SIN, -TAN, -ARC, -NCP, LS, -MER, or -AIT) ouping table copy option parameter. Allowed values are: 169 t r c e t

card casesen cmopt colname colnum colspec c c c c o o o o m m m o m p p r e l t d

-

nt m ype type-

cpopt

T r T f c o F h C g O n c t t t s c t g

h e w o r f I e A r P a o h a h h o y G r


170 O O olo -i -w n -3 -a t- a -s T T -F o -c -n e-d -d h-d P P I v f a o 2 d d p L F I r a u i e e d a

APPENDIX B. PARAMETER DEFINITIONS T_GCP_GPT, OPT_GCP_MBR, OPT_GCP_ALL, OPT_MCP_ADD, OPT_MCP_NADD, T_MCP_REPL, amd OPT_MCP_MOV. f TRUE, then insert a new column in the table, otherwise erwrite the existing column. TRUE, then the current HDU will be copied s the data unit verification successful (=1) or t (= -1). Equals zero if the DATASUM keyword is not present. -bit 1's complement checksum for the data unit dress (in bytes) of the end of the HDU dress (in bytes) of the start of the data unit ecifies the data type of the value. Allowed value are: TSTRING, OGICAL, TBYTE, TSBYTE, TSHORT, TUSHORT, TINT, TUINT, TLONG, TULONG, LOAT, TDOUBLE, TCOMPLEX, and TDBLCOMPLEX TS date/time string: 'YYYY-MM-DDThh:mm:ss.ddd', 'YYYY-MM-dd', 'dd/mm/yy' lendar day (UTC) (1-31) mber of decimal places to be displayed ncrement for allocating more memory clared size of the first dimension of the image or cube array clared size of the second dimension of the data cube array isplay width of a column = length of string that will be read ta type of the keyword ('C', 'L', 'I', 'F' or 'X') C = character string L = logical I = integer F = floating point number X = complex, e.g., "(1.23, -4.56)" ror message on the internal stack (80 chars max) ror message string corresponding to error number (30 chars max) UE (=1) if the strings match exactly; LSE (=0) if wildcards are used ray of pointers to keyword names to be excluded from search ag indicating whether the file or compressed file exists on disk olean or arithmetic expression UE (=1) if FITS file may have extensions, else FALSE (=0) lue of the EXTNAME keyword (null-terminated) e extension or HDU specifier; a number or name, version, and type lue of the EXTVER keyword = integer version number ll name of the FITS file, including optional HDU and filtering specs pe of file (file://, ftp://, http://, etc.) e input file filtering specifier arting byte in the row (first byte of row = 1) member HDU ID (if positive) or grouping table GRPIDn index lue (if negative) that failed grouping table verification. rst element in a vector (ignored for ASCII tables) arting row number (first row of table = 1)

create_c current dataok d d d d a a a a t t t t a a a a s e s t u n t y m d ar pe

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

i t 1 2 p p

mals asiz

widt e

err_msg - er err_text - er exact - TR FA exclist - ar exists - fl expr - bo extend - TR extname - va extspec - th extver - va filename - fu filetype - ty filter - th firstchar- st firstfailed va firstelem- fi firstrow - st


171 following- if fpixel - co FI th ra fptr - po frac - fa gcount - nu gfptr - fi group - GR da grouptype - G cr GT grpname - va hdunum - se hduok - wa no hdusum - 32 hdutype - HD header - re headstart- st heapsize - si history - th hour - ho inc - sa inclist - ar incolnum - in infile - th infptr - po intval - in iomode - fi keyname - na keynum - po keyroot - ro keysexist- nu keytype - he longstr lpixel - ar - co FI th ra - TR - ma - ro TRUE, any HDUs following the current HDU will be copied ordinate of the first pixel to be read or written in the TS array. The array must be of length NAXIS and have values such at fpixel[0] is in the range 1 to NAXIS1, fpixel[1] is in the nge 1 to NAXIS2, etc. inter to a 'fitsfile' structure describing the FITS file. ctional part of the keyword value mber of groups in the primary array (usually = 1) tsfile* pointer to a grouping table HDU. PIDn/GRPLCn index value identifying a grouping table HDU, or ta group number (=0 for non-grouped data) rouping table parameter that specifies the columns to be eated in a grouping table HDU. Allowed values are: GT_ID_ALL_URI, _ID_REF, GT_ID_POS, GT_ID_ALL, GT_ID_REF_URI, and GT_ID_POS_URI. lue to use for the GRPNAME keyword value. quence number of the HDU (Primary array = 1) s the HDU verification successful (=1) or t (= -1). Equals zero if the CHECKSUM keyword is not present. bit 1's complement checksum for the entire CHDU U type: IMAGE_HDU (0), ASCII_TBL (1), BINARY_TBL (2), ANY_HDU (-1) turned character string containing all the keyword records arting address (in bytes) of the CHDU ze of the binary table heap, in bytes e HISTORY keyword comment string (70 char max, null-terminated) ur within day (UTC) (0 - 23) mpling interval for pixels in each FITS dimension ray of pointers to matching keyword names put column number; range = 1 to TFIELDS e input filename, including path if specified inter to a 'fitsfile' structure describing the input FITS file. teger part of the keyword value le access mode: either READONLY (=0) or READWRITE (=1) me of a keyword (8 char max, null-terminated) sition of keyword in header (1st keyword = 1) ot string for the keyword name (5 char max, null-terminated) mber of existing keyword records in the CHU ader record type: -1=delete; 0=append or replace; 1=append; 2=this is the END keyword bitrarily long string keyword value (null-terminated) ordinate of the last pixel to be read or written in the TS array. The array must be of length NAXIS and have values such at lpixel[0] is in the range 1 to NAXIS1, lpixel[1] is in the nge 1 to NAXIS2, etc. UE (=1) if the 2 strings match, else FALSE (=0) ximum number of values to return w number of a grouping table member HDU.

match maxdim member


172 m m 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 n n n n o o o e e e f g i o o _ a a a a a a b c e e e e f k i m m o o r s u u u u f p v m m m p o n n r g m x x x x x y h l w w x o e n e o c i o t l l l m f e e p _ s t p u t e o e e i i i i t a e f v c u y c m v o s w a l v s v s n r

APPENDIX B. PARAMETER DEFINITIONS tr - pointer to the a FITS file in memory realloc - pointer to a function for reallocating more memory ize - size of the memory block allocated for the FITS file r - fitsfile* pointer to a grouping table member HDU. t - grouping table merge option parameter. Allowed values are: OPT_MRG_COPY, and OPT_MRG_MOV. te - minute within hour (UTC) (0 - 59) h - calendar month (UTC) (1 - 12) keys - space in the header for this many more keywords od_rows - number of rows evaluating to TRUE list - string containing a comma or space delimited list of names s - size of each dimension in the FITS array s - number of dimensions in the FITS array s1 - length of the X/first axis of the FITS array s2 - length of the Y/second axis of the FITS array s3 - length of the Z/third axis of the FITS array es - number of bytes or characters to read or write rs - number of characters to read or write ments- number of data elements to read or write ptr - returned pointer to the reopened file eclen- new value for the column vector repeat parameter - number of names in the exclusion list (may = 0) nd - number of keywords found (highest keyword number) s - number of keywords in the sequence - number of names in the inclusion list bers - Number of grouping table members (NAXIS2 value). e - number of HDUs to move (+ or -), relative to current position mments - if equal to TRUE, then no commentary keywords will be copied ebits- number of bits to ignore when compressing floating point images s - number of rows in the table rt - first integer value array- set to TRUE (=1) if corresponding data element is undefined al - numerical value to represent undefined pixels tr - character string used to represent undefined values in ASCII table al - numerical data value, of the appropriate data type et - byte offset in the heap or data unit to the first element of the vector fptr - pointer to a currently open FITS file lap - number of bytes in the binary table heap pointed to by more than 1 descriptor olnum- output column number; range = 1 to TFIELDS + 1 ile - and optional output filename; the input file will be copied to this prior to opening the file ptr - pointer to a 'fitsfile' structure describing the output FITS file. nt - value of the PCOUNT keyword = size of binary table heap ious - if TRUE, any previous HDUs in the input file will be copied. at - length of column vector (e.g. 12J); == 1 for ASCII table

outc outf o p p r u c r e t o e p f u v e


173 rmopt r r r r r r r s s s s s s s t t t t t t o o o o o o o c e e i p t u b d d e f f o t w w w w w a c c m a a m c i i m i o tname l l n r _ l o t p c t o s m p e p e i u a s e n i l e u n st m nge tat d on e s -g O -r -c -l -s -n -l us -l -s -s -T -n -r -3 -b -F -t -t -n -g O -f A B w V c d 1 rouping table remove option parameter. Allowed values are: PT_RM_GPT, OPT_RM_ENTRY, OPT_RM_MBR, and OPT_RM_ALL. oot filename, minus any extension or filtering specifications elestial coordinate rotation angle (degrees) ength of a table row, in characters or bytes orted list of row numbers to be deleted from the table umber of the row (first row = 1) ist of rows or row ranges: '3,6-8,12,56-80' or '500-' array of True/False results for each row that was evaluated inear scaling factor; true value = (FITS value) * scale + zero econd within minute (0 - 60.9999999999) (leap second!) ection of image to be copied (e.g. 21:80,101:200) RUE (=1) if FITS file conforms to the Standard, else FALSE (=0) umber of blank spaces to leave between ASCII table columns eturned error status code (0 = OK) 2 bit unsigned checksum value yte position in row to start of column (1st col has tbcol = 1) ortran style display format for the table column he value of the TDIMn keyword emplate string used in comparison (null-terminated) umber of fields (columns) in the table rouping table member transfer option parameter. Allowed values are: PT_MCP_ADD, and OPT_MCP_MOV. ormat of the column (null-terminated); allowed values are: SCII tables: Iw, Aw, Fww.dd, Eww.dd, or Dww.dd inary tables: rL, rX, rB, rI, rJ, rA, rAw, rE, rD, rC, rM here 'w'=width of the field, 'd'=no. of decimals, 'r'=repeat count. ariable length array columns are denoted by a '1P' before the data type haracter (e.g., '1PJ'). When creating a binary table, 2 addition tform ata type codes are recognized by CFITSIO: 'rU' and 'rV' for unsigned 6-bit and unsigned 32-bit integer, respectively. e e r h a h y a h r l r e b y m t e o indexed byte offset of starting address of the heap ative to the beginning of the binary table data ay of length NAXIS that specifies the dimensions of image compression tiles el or name for table column (null-terminated) sical unit for table column (null-terminated) bolic code of the table column data type a type code of the table column. The negative of value indicates a variable length array column. Datatype typecode Mnemonic bit, X 1 TBIT byte, B 11 TBYTE logical, L 14 TLOGICAL ASCII character, A 16 TSTRING

l p str lt lds t

tform

theap til t t t t t u y y y n p p

-z r esize - a t pe -l it -p echar - s ecode - d t


174

APPENDIX B. PARAMETER DEFINITIONS short integer, I 21 TSHORT integer, J 41 TINT32BIT (same as TLONG) long long integer, K 81 TLONGLONG real, E 42 TFLOAT double precision, D 82 TDOUBLE complex, C 83 TCOMPLEX double complex, M 163 TDBLCOMPLEX he physical unit string (e.g., 'km/s') for a keyword umber of unused bytes in the binary table heap he file type of the FITS file (file://, ftp://, mem://, etc.) eturned value = FALSE if any of the variable length array ddress are outside the valid range of addresses in the heap he keyword value string (70 char max, null-terminated) urrent version number of the CFITSIO library idth of the character string field umber of the column containing the X coordinate values axis coordinate increment at reference pixel (deg) axis pixel location axis celestial coordinate (usually RA) (deg) axis reference pixel array location axis coordinate value at the reference pixel (deg) umber of the column containing the X coordinate values alendar year (e.g. 1999, 2000, etc) axis coordinate increment at reference pixel (deg) axis pixel location axis celestial coordinate (usually DEC) (deg) axis reference pixel array location axis coordinate value at the reference pixel (deg) caling offset; true value = (FITS value) * scale + zero

u u u v v v w x x x x x x y y y y y y y z

n n r a a e i c i p p r r c e i p p r r e

i u l l l r d o n i o e e o a n i o e e r

t sed type idhea u s t l c x s f f l r c x s f f o e ion h

p-

pix val

pix val

t n t r a t c w n X X X X X n c Y y y Y Y s


App endix C

CFITSIO Error Status Codes
The following table lists all the error status codes used by CFITSIO. Programmers are encouraged to use the symb olic mnemonics (defined in the file fitsio.h) rather than the actual integer status values to improve the readability of their code. Symbolic Const -------------S T F F W E R F A R M B N S B T D N U R A O I I R N E I R E E A U E A O R O R A M O L L I D A L R A M D L E D O I _ L N E _ E E T _ D E A D O _ L K _ _ V M _ G _ M _ _ E O _ _ Y O R F _ _ U M E A P E F A N N _ F E N _ N Y I I E R A R T A _ I N O O E _ R O T L _ L N R L N _ C R P L Y T T R F R T O Y A E P R _ Y I H S A E _ _ _ R I O _ O _ L P U O P _ N I E R Value ----0 101 103 104 105 106 107 108 110 111 112 N 113 114 115 116 2 2 2 2 2 2 1 2 3 4 5 6 Meaning --------------------------------------, no error put and output files are the same ied to open too many FITS files at once uld not open the named file uld not create the named file ror writing to FITS file ied to move past end of file ror reading from FITS file uld not close the file ray dimensions exceed internal limit nnot write to readonly file uld not allocate memory valid fitsfile pointer LL input pointer to routine ror seeking position in file v i i t i r a e v c l s l d e h e e id URL prefix on f to register too m r initialization f ing driver is not d to parse input f error in range li i a a r i s l n i e l t e y l g e name IO drivers ed istered URL

F O C O L R C _ F L T T R R D I N _ S

ILES PENED REATED R E L B I O R _ O I L C SED G E ATIO

PTR

O i t c c e t e c a C C i N e i t d m f p

K n r o o r r r o r a o n U r n r r a a a

E R T G E E

F I _ _ R _

I V F D R E

X E A R O R

1 RS 1 ILED 1 IVER 1 R 1 ROR 1

SHARED_BADARG SHARED_NULPTR SHARED_TABFULL

151 152 153

bad argument in shared memory driver null pointer passed as an argument no more free shared memory handles 175


176 S S S S S S H K K V N B B B N N B B B B B B N N C B N N N N N N N N N N N N N B N C C B U H H H H H H E E E A O A A A O O A A A A A A E E O A O O O O O O O O O O O O O A O O O A N A A A A A A A Y Y L _ D D D T _ D D D D D D G G L D _ _ _ _ _ T T _ _ _ _ _ T D T L L D K R R R R R R D _ _ U Q _ _ _ _ E _ _ _ _ _ _ _ _ _ _ S B N N X _ _ P G T T T _ _ _ _ _ _ N E E E E E E E N O E U I K O P N B N N P G T W R N S I I A A T A B C C F B F I T T T N R O D D D D D D R O U _ O N E R O D I A A C C F I O O I M T X X E T T O O I C O M B A O O O W _ _ _ _ _ _ _ _ T U T D Y D S T X X O O I D W T M P P I E N A A U U E O R A C B O T W N N I N A N N N E _ N E E C E _ P I E U U E T S _ P L I S S S B B N N L L M G O L _ _ _ _ O P O G O O O X B D T C M A F R T I O E I E E I I E _ S U F N R M N L S IT R 1 1 1 1 1 1 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 5 5 5 5 5 5 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 4 5 6 7 8 9 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 s I n r a s h k k k s i i r k c i i i i i i n n c i P S T C H t t c c c c c t T t c m s u h P o e t h e e e e t l l e e o l l l l l l e e o l r e h o D h h o o o o o h B h o o u n

APPENDIX C. CFITSIO ERROR STATUS CODES ared memory driver is not initialized C error returned by a system call memory in shared memory driver source deadlock would occur tempt to open/create lock file failed ared memory block cannot be resized at the moment a y y y r l l q y u l l l l l l g g l l i c i u U e e u u u u u e C e l r m r der already contains keywords word not found in header word record number is out of bounds word value field is blank ing is missing the closing quote egal indexed keyword name (e.g. 'TFORM1000') egal character in keyword name or card uired keywords out of order word value is not a positive integer ldn't find END keyword egal BITPIX keyword value egal NAXIS keyword value egal NAXISn keyword value egal PCOUNT keyword value egal GCOUNT keyword value egal TFIELDS keyword value ative table row size ative number of rows in table umn with this name not found in table egal value of SIMPLE keyword mary array doesn't start with SIMPLE ond keyword not BITPIX rd keyword not NAXIS ldn't find all the NAXISn keywords doesn't start with XTENSION keyword CHDU is not an ASCII table extension CHDU is not a binary table extension ldn't find PCOUNT keyword ldn't find GCOUNT keyword ldn't find TFIELDS keyword ldn't find TBCOLn keyword ldn't find TFORMn keyword CHDU is not an IMAGE extension OLn keyword value < 0 or > rowlength CHDU is not a table umn is too wide to fit in table e than 1 column name matches template of column widths not = NAXIS1 ecognizable FITS extension type

E IZE

EMPTY T NDS INED

X_KEY HAR R INT I S S N N L H X

T T DS

FOUND LE E X

I L L T T D

ON E E

S

E L E W U W E

I N I X

DE IQUE DTH T


177 U E B B B B B B B B N N B B N N B B N N B B Z N N H M G B T H B I B B N N A A A A A A A A E E A A O O A A O O A A E E O D E R A O D A D A A K D D D D D D D D D G G D D T T D D _ T D D R G T U M O D O U D E D D N _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ N _ _ _ O _ _ _ B U _ _ _ _ N _ _ O J H D T T T H H C F B R E A L A B U V D P _ A G A E P G M A O T G G W U E A F F D E D O I Y O L S O T T L A I I S X R L R _ R A L P I R R N N A T O O I A U L L T W E C G A A L R M X C I O R _ N O N R T C O O _ K D A R R M P _ _ E E _ M I I B B I E _ A S U E N O U Y E I A U U REC ER_FILL _FILL M M_DTYPE _PTR N N _ S N _ I C L L UM UM POS U N _ A E E M U C L _ _ 2 2 2 2 2 2 2 2 5 5 5 5 6 6 6 6 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 4 5 1 2 3 4 1 2 4 6 7 8 9 0 1 2 4 7 0 1 2 3 4 4 4 4 4 4 4 4 4 4 5 u E H I i u i i H c t t i i t t A B n t i f i i 0 1 2 3 4 5 6 7 8 9 0 n N e l l n l n D o r r l l h h S i u h l i l l k D a l l r l v U l i i l l i i C n l i l r l l nown record; 1st keyword not SIMPLE or XTENSION keyword is not blank der fill area contains non-blank chars egal data fill bytes (not zero or blank) egal TFORM format code ecognizable TFORM data type code egal TDIMn keyword value alid BINTABLE heap pointer is out of range number < 1 umn number < 1 or > tfields ed to move to negative byte location in file ed to read or write negative number of bytes egal starting row number in table egal starting element number in vector s is not an ASCII string column s is not a logical data type column II table column has wrong format ary table column has wrong format l value has not been defined s is not a variable length column egal number of dimensions in array st pixel number greater than last pixel egal BSCALE or TSCALn keyword = 0 egal axis length < 1

M O _ F F

L CO OR OR

_LEN N NUM LE

3 3 3 3 3 3 3 L 3 MAT 3 MAT 3 3 3 3 3 3 3 E MBER ND D

P A O T P _ A O L P P

_ D T _ _ H D N _ _ _

T Y _ F I D Y

3 3 3 3 3 S_TRACKED 3 TRACKED 3 3 POINTERS 3 ATTACH 3 DETACH 3

A _ F O D U _

B M O U

L E U N

Grouping function error

NGP_NO_MEMORY NGP_READ_ERR NGP_NUL_PTR

360 361 362

NGP_EMPTY_CURLINE

363

NGP_UNREAD_QUEUE_FULL 364

malloc failed read error from file null pointer passed as an ar Passing null pointer as a template file raises this line read seems to be empty internally) cannot unread more then 1 li

g n e (

u a r u

m m r s

e e o e

nt. of r d

ne (or single


178

APPENDIX C. CFITSIO ERROR STATUS CODES line twice) oo deep include file nesting (infinite loop, template includes itself ?) open() failed, cannot open template file nd of file encountered and not expected ad arguments passed. Usually means internal parser error. Should not happen oken not expected here

NGP_INC_NESTING NGP_ERR_FOPEN NGP_EOF NGP_BAD_ARG

365 366 367 368

t f e b t a a a a a a a a a l a v

NGP_TOKEN_NOT_EXPECT 369 B B B B B B B B B B B N D D A A A A A A A A A A A U A A D D D D D D D D D D D M T T _ _ _ _ _ _ _ _ _ _ _ _ A A I F I L F D C C C D D O _ _ 2 2 N O L O 2 2 2 A E V C D C C T G O U I F D T C E O E 401 402 403 Y 404 405 406 407 408 409 410 411 412 ION_ERR 41 SSION_ERR 41 420 N D G _ D D O V R P E C T _ _ O _ _ A T V U C O X Y E T O U _ P C P L T ERR E TOR UT PUT 4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 0 0 0 0 0 0 1 2 3 4 5 6 1 2 3 4 5 6 b b c c c c b b b i b o 3 4

K I A B

E C T L

Y ALKE KEY EKEY

A I R M C

T M F P O

YPE LOW RESS MPRE

d int to formatted string conversion d float to formatted string conversion n't interpret keyword value as integer n't interpret keyword value as logical n't interpret keyword value as float n't interpret keyword value as double d formatted string to int conversion d formatted string to float conversion d formatted string to double conversion legal datatype code value d number of decimal places specified erflow during data type conversion error compressing image error uncompressing image

BAD_DATE P P P P P P A B W B N A A A A A A A N A C A O P R R R R R R G D S D _ P S S S S S S L _ _ _ W R E E E E E E E W E W C O _ _ _ _ _ _ _ C R C S X S B L N B B T S R S _ _ Y A R O A A O _ O _ K W

error in date or time conversion s e v d b O c b e u c a y x e a a u e a r n e p n p c t d t l d r s l p tax error in parser expression ression did not evaluate to des tor result too large to return a parser failed not sent an out data encounter while parsing c put file not of proper type

ired type in array column olumn

_BIG AL ROJ Y S_KEY

estial angle too large for projection celestial coordinate or pixel value or in celestial coordinate calculation upported type of celestial projection estial coordinate keywords not found roximate wcs keyword values were returned