1 |
function fits_ascii_encode, sum32 |
2 |
;+ |
3 |
; NAME: |
4 |
; FITS_ASCII_ENCODE() |
5 |
; PURPOSE: |
6 |
; Encode an unsigned longword as an ASCII string to insert 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 |
; CALLING SEQUENCE: |
11 |
; result = FITS_ASCII_ENCODE( sum32) |
12 |
; INPUTS: |
13 |
; sum32 - 32bit *unsigned longword* (e.g. as returned by CHECKSUM32) |
14 |
; RESULT: |
15 |
; A 16 character scalar string suitable for the CHECKSUM keyword |
16 |
; EXAMPLE: |
17 |
; A FITS header/data unit has a checksum of 868229149. Encode the |
18 |
; complement of this value (3426738146) into an ASCII string |
19 |
; |
20 |
; IDL> print,FITS_ASCII_ENCODE(3426738146U) |
21 |
; ===> "hcHjjc9ghcEghc9g" |
22 |
; |
23 |
; METHOD: |
24 |
; The 32bit value is interpreted as a sequence of 4 unsigned 8 bit |
25 |
; integers, and divided by 4. Add an offset of 48b (ASCII '0'). |
26 |
; Remove non-alphanumeric ASCII characters (byte values 58-64 and 91-96) |
27 |
; by simultaneously incrementing and decrementing the values in pairs. |
28 |
; Cyclicly shift the string one place to the right. |
29 |
; |
30 |
; REVISION HISTORY: |
31 |
; Written W. Landsman SSAI December 2002 |
32 |
;- |
33 |
if N_Params() LT 1 then begin |
34 |
print,'Syntax - result = FITS_ASCII_ENCODE( sum32)' |
35 |
return,'0' |
36 |
endif |
37 |
|
38 |
; Non-alphanumeric ASCII characters |
39 |
exclude = [58b,59b,60b,61b,62b,63b,64b,91b,92b,93b,94b,95b,96b] |
40 |
ch = bytarr(16) |
41 |
t = byte(sum32,0,4) |
42 |
byteorder,t,/htonl |
43 |
quot = t/4 + 48b |
44 |
for i=0,12,4 do ch[i] = quot |
45 |
|
46 |
remain = t mod 4 |
47 |
ch[0] = ch[0:3] + remain ;Insert the remainder in the first 4 bytes |
48 |
|
49 |
;Step through the 16 bytes, 8 at a time, removing nonalphanumeric characters |
50 |
repeat begin |
51 |
check = 0b |
52 |
for j=0,1 do begin |
53 |
il = j*8 |
54 |
for i=il,il+3 do begin |
55 |
bad = where( (exclude EQ ch[i]) or (exclude Eq ch[i+4]) , Nbad) |
56 |
if Nbad GT 0 then begin |
57 |
ch[i] = ch[i]+1b |
58 |
ch[i+4] = ch[i+4] -1b |
59 |
check=1b |
60 |
endif |
61 |
endfor |
62 |
endfor |
63 |
endrep until (check EQ 0b) |
64 |
|
65 |
return, string( shift(ch,1)) |
66 |
end |
67 |
|