1 |
function fits_test_checksum,hdr, data, ERRMSG = errmsg,FROM_IEEE=from_ieee |
2 |
;+ |
3 |
; NAME: |
4 |
; FITS_TEST_CHECKSUM() |
5 |
; PURPOSE: |
6 |
; Verify the values of the CHECKSUM and DATASUM keywords in a FITS header |
7 |
; EXPLANATION: |
8 |
; Follows the 23 May 2002 version of the FITS checksum proposal at |
9 |
; http://heasarc.gsfc.nasa.gov/docs/heasarc/fits/checksum.html |
10 |
; |
11 |
; CALLING SEQUENCE: |
12 |
; result = FITS_TEST_CHECKSUM(HDR, [ DATA, ERRMSG=, /FROM_IEEE ]) |
13 |
; INPUTS: |
14 |
; HDR - FITS header (vector string) |
15 |
; OPTIONAL DATA: |
16 |
; DATA - data array associated with the FITS header. If not supplied, or |
17 |
; set to a scalar, then there is assumed to be no data array |
18 |
; associated with the FITS header. |
19 |
; RESULT: |
20 |
; An integer -1, 0 or 1 indicating the following conditions: |
21 |
; 1 - CHECKSUM (and DATASUM) keywords are present with correct values |
22 |
; 0 - CHECKSUM keyword is not present |
23 |
; -1 - CHECKSUM or DATASUM keyword does not have the correct value |
24 |
; indicating possible data corruption. |
25 |
; OPTIONAL INPUT KEYWORD: |
26 |
; /FROM_IEEE - If this keyword is set, then the input is assumed to be in |
27 |
; big endian format (e.g. an untranslated FITS array). This |
28 |
; keyword only has an effect on little endian machines (e.g. |
29 |
; a Linux box). |
30 |
; OPTIONAL OUTPUT KEYWORD: |
31 |
; ERRMSG - will contain a scalar string giving the error condition. If |
32 |
; RESULT = 1 then ERRMSG will be an empty string. If this |
33 |
; output keyword is not supplied, then the error message will be |
34 |
; printed at the terminal. |
35 |
; NOTES: |
36 |
; The header and data must be *exactly* as originally written in the FITS |
37 |
; file. By default, some FITS readers may alter keyword values (e.g. |
38 |
; BSCALE) or append information (e.g. HISTORY or an inherited primary |
39 |
; header) and this will alter the checksum value. |
40 |
; PROCEDURES USED: |
41 |
; CHECKSUM32, FITS_ASCII_ENCODE(), SXPAR() |
42 |
; EXAMPLE: |
43 |
; Verify the CHECKSUM keywords in the primary header/data unit of a FITS |
44 |
; file 'test.fits' |
45 |
; |
46 |
; FITS_READ,'test.fits',data,hdr,/no_PDU,/NoSCALE |
47 |
; print,FITS_TEST_CHECKSUM(hdr,data) |
48 |
; |
49 |
; Note the use of the /No_PDU and /NoSCALE keywords to avoid any alteration |
50 |
; of the FITS header |
51 |
; REVISION HISTORY: |
52 |
; W. Landsman SSAI December 2002 |
53 |
; Return quietly if CHECKSUM keywords not found W. Landsman May 2003 |
54 |
; Add /NOSAVE to CHECKSUM32 calls when possible W. Landsman Sep 2004 |
55 |
;- |
56 |
if N_Params() LT 1 then begin |
57 |
print,'Syntax - result = FITS_TEST_CHECKSUM(Hdr, [Data,' + $ |
58 |
' ERRMSG=, /FROM_IEEE ])' |
59 |
return, 0 |
60 |
endif |
61 |
result = 1 |
62 |
printerr = not arg_present(errmsg) |
63 |
checksum = sxpar(hdr,'CHECKSUM', Count = N_checksum) |
64 |
datasum = sxpar(hdr,'DATASUM', Count = N_datasum) |
65 |
if (N_checksum EQ 0) then begin |
66 |
errmsg = 'CHECKSUM keyword not present in FITS header' |
67 |
if printerr then message,/con, errmsg |
68 |
return, 0 |
69 |
endif |
70 |
if N_datasum EQ 0 then datasum = '0' |
71 |
ch = shift(byte(checksum),-1) |
72 |
checksum32,ch-48b, sum32, /NOSAVE |
73 |
bhdr = byte(hdr) |
74 |
remain = N_elements(bhdr) mod 2880 |
75 |
if remain NE 0 then $ |
76 |
bhdr = [reform(bhdr,N_elements(bhdr)), replicate(32b, 2880 - remain) ] |
77 |
checksum32,bhdr, hsum, FROM_IEEE = from_ieee, /NOSAVE |
78 |
Ndata = N_elements(data) |
79 |
if Ndata GT 1 then begin |
80 |
checksum32, data, dsum, FROM_IEEE= from_ieee |
81 |
remain = Ndata mod 2880 |
82 |
if remain GT 0 then begin |
83 |
exten = sxpar( hdr, 'XTENSION', Count = N_exten) |
84 |
if N_exten GT 0 then if exten EQ 'TABLE ' then $ |
85 |
checksum32,[dsum,replicate(32b,2880-remain)],dsum,/NOSAVE |
86 |
endif |
87 |
checksum32, [dsum, hsum], hdusum, /NOSAVE |
88 |
dsum = strtrim(dsum,2) |
89 |
if dsum NE datasum then begin |
90 |
result = 1 |
91 |
errmsg = 'Computed Datasum: ' + dsum + $ |
92 |
' FITS header value: ' + datasum |
93 |
if printerr then message,/Con, errmsg |
94 |
endif |
95 |
endif else hdusum = hsum |
96 |
|
97 |
csum = FITS_ASCII_ENCODE(not hdusum) |
98 |
if csum NE '0000000000000000' then begin |
99 |
result = -1 |
100 |
errmsg = 'Computed Checksum: ' + csum + $ |
101 |
' FITS header value: ' + checksum |
102 |
if printerr then message,/Con, errmsg |
103 |
endif |
104 |
return, result |
105 |
end |