📄 readfits.pro
字号:
;+; NAME:; READFITS; PURPOSE:; Read a FITS file into IDL data and header variables. ; EXPLANATION:; READFITS() can also read gzip or Unix compressed FITS files.; See http://idlastro.gsfc.nasa.gov/fitsio.html for other ways of; reading FITS files with IDL. ;; CALLING SEQUENCE:; Result = READFITS( Filename/Fileunit,[ Header, heap, /NOSCALE, EXTEN_NO=,; NSLICE=, /SILENT , STARTROW =, NUMROW = , HBUFFER=,; /CHECKSUM, /COMPRESS, /No_Unsigned, NaNVALUE = ];; INPUTS:; Filename = Scalar string containing the name of the FITS file ; (including extension) to be read. If the filename has; a *.gz extension, it will be treated as a gzip compressed; file. If it has a .Z extension, it will be treated as a; Unix compressed file.; OR; Fileunit - A scalar integer specifying the unit of an already opened; FITS file. The unit will remain open after exiting ; READFITS(). There are two possible reasons for choosing ; to specify a unit number rather than a file name:; (1) For a FITS file with many extensions, one can move to the ; desired extensions with FXPOSIT() and then use READFITS(). This; is more efficient than repeatedly starting at the beginning of ; the file.; (2) For reading a FITS file across a Web http: address after opening; the unit with the SOCKET procedure ;; OUTPUTS:; Result = FITS data array constructed from designated record.; If the specified file was not found, then Result = -1;; OPTIONAL OUTPUT:; Header = String array containing the header from the FITS file.; If you don't need the header, then the speed may be improved by; not supplying this parameter. Note however, that omitting ; the header can imply /NOSCALE, i.e. BSCALE and BZERO values; may not be applied.; heap = For extensions, the optional heap area following the main; data array (e.g. for variable length binary extensions).;; OPTIONAL INPUT KEYWORDS:; /CHECKSUM - If set, then READFITS() will call FITS_TEST_CHECKSUM to ; verify the data integrity if CHECKSUM keywords are present; in the FITS header. Cannot be used with the NSLICE, NUMROW; or STARTROW keywords, since verifying the checksum requires ; that all the data be read. See FITS_TEST_CHECKSUM() for more; information.;; /COMPRESS - Signal that the file is gzip compressed. By default, ; READFITS will assume that if the file name extension ends in ; '.gz' then the file is gzip compressed. The /COMPRESS keyword; is required only if the the gzip compressed file name does not ; end in '.gz' or .ftz; ;; EXTEN_NO - non-negative scalar integer specifying the FITS extension to; read. For example, specify EXTEN = 1 or /EXTEN to read the ; first FITS extension. ; ; HBUFFER - Number of lines in the header, set this to slightly larger; than the expected number of lines in the FITS header, to ; improve performance when reading very large FITS headers. ; Should be a multiple of 36 -- otherwise it will be modified; to the next higher multiple of 36. Default is 180;; /NOSCALE - If present and non-zero, then the ouput data will not be; scaled using the optional BSCALE and BZERO keywords in the ; FITS header. Default is to scale.;; /NO_UNSIGNED - By default, if the header indicates an unsigned integer ; (BITPIX = 16, BZERO=2^15, BSCALE=1) then FITS_READ will output ; an IDL unsigned integer data type (UINT). But if /NO_UNSIGNED; is set, then the data is converted to type LONG. ;; NSLICE - An integer scalar specifying which N-1 dimensional slice of a ; N-dimensional array to read. For example, if the primary ; image of a file 'wfpc.fits' contains a 800 x 800 x 4 array, ; then ;; IDL> im = readfits('wfpc.fits',h, nslice=2); is equivalent to ; IDL> im = readfits('wfpc.fits',h); IDL> im = im[*,*,2]; but the use of the NSLICE keyword is much more efficient.;; NUMROW - Scalar non-negative integer specifying the number of rows ; of the image or table extension to read. Useful when one ; does not want to read the entire image or table. This; keyword is only for extensions and is ignored for primary; arrays.;; POINT_LUN - Position (in bytes) in the FITS file at which to start; reading. Useful if READFITS is called by another procedure; which needs to directly read a FITS extension. Should ; always be a multiple of 2880, and not be used with EXTEN_NO; keyword.;; /SILENT - Normally, READFITS will display the size the array at the; terminal. The SILENT keyword will suppress this;; STARTROW - Non-negative integer scalar specifying the row; of the image or extension table at which to begin reading. ; Useful when one does not want to read the entire table. This; keyword is ignored when reading a primary data array.;; NaNVALUE - This keyword is included only for backwards compatibility; with routines that require IEEE "not a number" values to be; converted to a regular value.;; EXAMPLE:; Read a FITS file test.fits into an IDL image array, IM and FITS ; header array, H. Do not scale the data with BSCALE and BZERO.;; IDL> im = READFITS( 'test.fits', h, /NOSCALE);; If the file contains a FITS extension, it could be read with;; IDL> tab = READFITS( 'test.fits', htab, /EXTEN );; The function TBGET() can be used for further processing of a binary ; table, and FTGET() for an ASCII table.; To read only rows 100-149 of the FITS extension,;; IDL> tab = READFITS( 'test.fits', htab, /EXTEN, ; STARTR=100, NUMR = 50 );; To read in a file that has been compressed:;; IDL> tab = READFITS('test.fits.gz',h);; ERROR HANDLING:; If an error is encountered reading the FITS file, then ; (1) the system variable !ERROR_STATE.CODE is set negative ; (via the MESSAGE facility); (2) the error message is displayed (unless /SILENT is set),; and the message is also stored in !!ERROR_STATE.MSG; (3) READFITS returns with a value of -1; RESTRICTIONS:; (1) Cannot handle random group FITS;; NOTES:; (1) If data is stored as integer (BITPIX = 16 or 32), and BSCALE; and/or BZERO keywords are present, then the output array is scaled to ; floating point (unless /NOSCALE is present) using the values of BSCALE; and BZERO. In the header, the values of BSCALE and BZERO are then ; reset to 1. and 0., while the original values are written into the ; new keywords O_BSCALE and O_BZERO. If the BLANK keyword was; present, then any input integer values equal to BLANK in the input; integer image are unchanged by BSCALE or BZERO; ; (2) The use of the NSLICE keyword is incompatible with the NUMROW; or STARTROW keywords.;; (3) READFITS() underwent a substantial rewrite in February 2000 to ; take advantage of new features in IDL V5.3; 1. The /swap_if_little_endian keyword is now used to OPENR rather; than calling IEEE_TO_HOST for improved performance; 2. The /compress keyword is now used with OPENR to allow gzip files; to be read on any machine architecture.; 3. Removed NANvalue keyword, since in V5.3, NaN is recognized on; all machine architectures; 4. Assume unsigned integers are always allowed; 5. Use STRJOIN to display image size; 6. Use !ERROR_STATE.MSG rather than !ERR_STRING; ;; (4) On some Unix shells, one may get a "Broken pipe" message if reading; a Unix compressed (.Z) file, and not reading to the end of the file ; (i.e. the decompression has not gone to completion). This is an ; informative message only, and should not affect the output of READFITS. ; PROCEDURES USED:; Functions: SXPAR(); Procedures: MRD_SKIP, SXADDPAR, SXDELPAR;; MODIFICATION HISTORY:; Original Version written in 1988, W.B. Landsman Raytheon STX; Revision History prior to October 1998 removed ; Major rewrite to eliminate recursive calls when reading extensions; W.B. Landsman Raytheon STX October 1998; Add /binary modifier needed for Windows W. Landsman April 1999; Read unsigned datatypes, added /no_unsigned W. Landsman December 1999; Output BZERO = 0 for unsigned data types W. Landsman January 2000; Update to V5.3 (see notes) W. Landsman February 2000; Fixed logic error in use of NSLICE keyword W. Landsman March 2000; Fixed byte swapping for Unix compress files on little endian machines; W. Landsman April 2000; Added COMPRESS keyword, catch IO errors W. Landsman September 2000; Option to read a unit number rather than file name W.L October 2001; Fix undefined variable problem if unit number supplied W.L. August 2002; Don't read entire header unless needed W. Landsman Jan. 2003; Added HBUFFER keyword W. Landsman Feb. 2003; Added CHECKSUM keyword W. Landsman May 2003; Restored NaNVALUE keyword for backwards compatibility,; William Thompson, 16-Aug-2004, GSFC; Recognize .ftz extension as compressed W. Landsman September 2004; Fix unsigned integer problem introduced Sep 2004 W. Landsman Feb 2005; Don't modify header for unsigned integers, preserve double precision; BSCALE value W. Landsman March 2006; Use gzip instead of compress for Unix compress files W.Landsman Sep 2006; Call MRD_SKIP to skip bytes on different file types W. Landsman Oct 2006; Make ndata 64bit for very large files E. Hivon/W. Landsman May 2007; Fixed bug introduced March 2006 in applying Bzero C. Magri/W.L. Aug 2007; Check possible 32bit overflow when using NSKIP W. Landsman Mar 2008; Always reset BSCALE, BZERO even for unsigned integers W. Landsman May 2008; Make ndata 64bit for very large extensions J. Schou/W. Landsman Jan 2009;-function READFITS, filename, header, heap, CHECKSUM=checksum, $ COMPRESS = compress, HBUFFER=hbuf, EXTEN_NO = exten_no, $ NOSCALE = noscale, NSLICE = nslice, $ NO_UNSIGNED = no_unsigned, NUMROW = numrow, $ POINTLUN = pointlun, SILENT = silent, STARTROW = startrow, $ NaNvalue = NaNvalue On_error,2 ;Return to user compile_opt idl2 On_IOerror, BAD; Check for filename input if N_params() LT 1 then begin print,'Syntax - im = READFITS( filename, [ h, heap, /NOSCALE, /SILENT,' print,' EXTEN_NO =, STARTROW = , NUMROW=, NSLICE = ,' print,' HBUFFER = ,/NO_UNSIGNED, /CHECKSUM, /COMPRESS]' return, -1 endif unitsupplied = size(filename,/TNAME) NE 'STRING'; Set default keyword values silent = keyword_set( SILENT ) do_checksum = keyword_set( CHECKSUM ) if N_elements(exten_no) EQ 0 then exten_no = 0; Check if this is a Unix compressed file. (gzip files are handled ; separately using the /compress keyword to OPENR). unixZ = 0 if unitsupplied then unit = filename else begin len = strlen(filename) ext = strlowcase(strmid(filename,len-3,3)) gzip = (ext EQ '.gz') or (ext EQ 'ftz') compress = keyword_set(compress) or gzip[0] unixZ = (strmid(filename, len-2, 2) EQ '.Z') and $ (!VERSION.OS_FAMILY EQ 'unix') ; Go to the start of the file. openr, unit, filename, ERROR=error,/get_lun, $ COMPRESS = compress, /swap_if_little_endian if error NE 0 then begin message,/con,' ERROR - Unable to locate file ' + filename return, -1 endif; Handle Unix compressed files. We use gzip -d which seems to have wider ; availability than uncompress. On some Unix machines, users might wish to ; force use of /bin/sh in the line spawn, ucmprs+filename, unit=unit,/sh if unixZ then begin free_lun, unit spawn, 'gzip -cd '+filename, unit=unit gzip = 1b endif endelse if keyword_set(POINTLUN) then mrd_skip, unit, pointlun doheader = arg_present(header) or do_checksum if doheader then begin if N_elements(hbuf) EQ 0 then hbuf = 180 else begin remain = hbuf mod 36 if remain GT 0 then hbuf = hbuf + 36-remain endelse endif else hbuf = 36 for ext = 0L, exten_no do begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -