aboutsummaryrefslogtreecommitdiff
path: root/pkg/obsolete/fits/fits_rheader.x
blob: 8efb55622b09a416d10b7d262c699e4f2d27932c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <ctype.h>
include <imhdr.h>
include <imio.h>
include <mach.h>
include	"rfits.h"

define	NEPSILON 10.0d0    # number of machine epsilon

# RFT_READ_HEADER -- Read a FITS header.
# If BSCALE and BZERO are different from 1.0 and 0.0  scale is set to true
# otherwise scale is false.
# EOT is detected by an EOF on the first read and EOF is returned to the calling
# routine.  Any error is passed to the calling routine.

int procedure rft_read_header (fits_fd, fits, im)

int	fits_fd			# FITS file descriptor
pointer	fits			# FITS data structure
pointer	im			# IRAF image descriptor

int	i, stat, nread, max_lenuser, fd_usr, ndiscard
char	card[LEN_CARD+1]
int	rft_decode_card(), rft_init_read_pixels(), rft_read_pixels(), strmatch()
int	stropen()
errchk	rft_decode_card, rft_init_read_pixels, rft_read_pixels
errchk	stropen, close

include "rfits.com"

begin
	# Initialization.
	SIMPLE(fits) = NO
	BITPIX(fits) = INDEFI
	NAXIS(im) = INDEFI
	do i = 1, IM_MAXDIM
	    IM_LEN(im,i) = INDEFL
	SCALE(fits) = NO
	FITS_BSCALE(fits) = 1.0d0
	FITS_BZERO(fits) = 0.0d0
	BLANKS(fits) = NO
	BLANK_VALUE(fits) = INDEFL
	NRECORDS(fits) = 0
	ndiscard = 0
	max_lenuser = (LEN_IMDES + IM_LENHDRMEM(im) - IMU) * SZ_STRUCT - 1

	# The FITS header is character data in FITS_BYTE form. Open the
	# header for reading. Open the user area which is a character
	# string as a file.

	i = rft_init_read_pixels (len_record, FITS_BYTE, LSBF, TY_CHAR)
	fd_usr = stropen (UNKNOWN(im), max_lenuser, NEW_FILE)

	# Loop until the END card is encountered.
	nread = 0
	repeat {

	    # Read the card.
	    i = rft_read_pixels (fits_fd, card, LEN_CARD, NRECORDS(fits), 1)
	    card[LEN_CARD + 1] = '\n'
	    card[LEN_CARD + 2] = EOS

	    # Decode the card images.
	    if ((i == EOF) && (nread == 0)) {
		return (EOF)
	    } else if ((nread == 0) && strmatch (card, "^SIMPLE  ") == 0) {
		call flush (STDOUT)
		call error (30,
		    "RFT_READ_HEADER: Not a FITS file (no SIMPLE keyword)")
	    } else if (i != LEN_CARD) {
	        call error (2, "RFT_READ_HEADER: Error reading FITS header")
	    } else
	        nread = nread + 1

	    # Remove contaminating control characters and replace with blanks.
	    call rft_control_to_blank (card, card, LEN_CARD)

	    # Print FITS card images if long_header option specified.
	    if (long_header == YES) {
		call printf ("%-80.80s\n")
		    call pargstr (card)
	    }

	    # Stat = YES if FITS END card is encountered.
	    stat = rft_decode_card (fits, im, fd_usr, card, ndiscard)

	} until (stat == YES)

	# Print optional short header.
	if (short_header == YES && long_header == NO) {
	    if (make_image == NO && old_name == YES) {
	        call printf ("(%s)  %-20.20s  ")
		    call pargstr (IRAFNAME(fits))
		    call pargstr (OBJECT(im))
	    } else {
	        call printf ("%-20.20s  ")
		    call pargstr (OBJECT(im))
	    }
	    do i = 1, NAXIS(im) {
		if (i == 1) {
		    call printf ("Size = %d")
		        call pargl (NAXISN(im,i))
		} else {
		    call printf (" x %d")
		        call pargl (NAXISN(im,i))
		}
	    }
	    call printf ("\n")
	}

	# Let the user know if there is not enough space in the user area.
	if (ndiscard > 0) {
	    call printf (
	        "Warning: User area too small %d card images discarded\n")
	    call pargi (ndiscard)
	    call rft_last_user (UNKNOWN(im), max_lenuser)
	}

	call close (fd_usr)
	return (OK)
end


# RFT_CONTROL_TO_BLANK -- Replace an ACSII control characters in the
# FITS card image with blanks.

procedure rft_control_to_blank (incard, outcard, len_card)

char	incard[ARB]		# the input FITS card image
char	outcard[ARB]		# the output FITS card image
int	len_card		# the length of the FITS card image

int	i

begin
	for (i = 1; i <= len_card; i = i + 1) {
	    if (IS_PRINT(incard[i]))
	        outcard[i] = incard[i]
	    else
		outcard[i] = ' '
	}
end


# RFT_DECODE_CARD -- Decode a FITS card and return YES when the END
# card is encountered.  The keywords understood are given in rfits.h.

int procedure rft_decode_card (fits, im, fd_usr, card, ndiscard)

pointer	fits		# FITS data structure
pointer	im		# IRAF image descriptor
int	fd_usr		# file descriptor of user area
char	card[ARB]	# FITS card
int	ndiscard	# Number of cards for which no space available

char	cval, str[LEN_CARD]
double	dval
int	nchar, ival, i, j, k, len
pointer	sp, comment

bool	rft_equald()
int	strmatch(), ctoi(), ctol(), ctod(), cctoc(), rft_hms()
errchk	putline

include	"rfits.com"

begin
	call smark (sp)
	call salloc (comment, SZ_LINE, TY_CHAR)

	i = COL_VALUE
	if (strmatch (card, "^END     ") != 0) {
	    call sfree (sp)
	    return(YES)
	} else if (strmatch (card, "^SIMPLE  ") != 0) {
	    if (SIMPLE(fits) == YES)
		call printf ("Warning: Duplicate SIMPLE keyword ignored\n")
	    else {
	        nchar = cctoc (card, i, cval)
	        if (cval != 'T')
		    call error (13, "RFT_DECODE_CARD: Non-standard FITS format")
		SIMPLE(fits) = YES
	    }
	} else if (strmatch (card, "^BITPIX  ") != 0) {
	    if (! IS_INDEFI(BITPIX(fits)))
		call printf ("Warning: Duplicate BITPIX keyword ignored\n")
	    else
	        nchar = ctoi (card, i, BITPIX(fits))
	} else if (strmatch (card, "^BLANK   ") != 0) {
	    BLANKS(fits) = YES
	    nchar = ctol (card, i, BLANK_VALUE(fits))
	} else if (strmatch (card, "^NAXIS   ") != 0) {
	    if (! IS_INDEFI(NAXIS(im)))
		call printf ("Warning: Duplicate NAXIS keyword ignored\n")
	    else
	        nchar = ctoi (card, i, NAXIS(im))
	    if (NAXIS(im) > IM_MAXDIM)
		call error (5, "RFT_DECODE_CARD: FITS NAXIS too large")
	} else if (strmatch (card, "^NAXIS") != 0) {
	    k = strmatch (card, "^NAXIS")
	    nchar = ctoi (card, k, j)
	    if (! IS_INDEFL(NAXISN(im,j))) {
		call printf ("Warning: Duplicate NAXIS%d keyword ignored\n")
		    call pargi (j)
	    } else
	        nchar = ctol (card, i, NAXISN(im, j))
	} else if (strmatch (card, "^GROUPS  ") != 0) {
	    nchar = cctoc (card, i, cval)
	    if (cval == 'T') {
		NAXIS(im) = 0
		call error (6, "RFT_DECODE_CARD: Group data not implemented")
	    }
	} else if (strmatch (card, "^TABLES  ") != 0) {
	    nchar = ctoi (card, i, ival)
	    if (ival > 0)
		call printf ("Warning: FITS special records not decoded\n")
	} else if (strmatch (card, "^BSCALE  ") != 0) {
	    nchar = ctod (card, i, dval)
	    if (nchar > 0)
	        FITS_BSCALE(fits) = dval
	    else
		call printf ("Warning: Error decoding BSCALE, BSCALE=1.0\n")
	    if (! rft_equald (dval, 1.0d0) && (scale == YES))
		SCALE(fits) = YES
	} else if (strmatch (card, "^BZERO   ") != 0) {
	    nchar = ctod (card, i, dval)
	    if (nchar > 0)
	        FITS_BZERO(fits) = dval
	    else
		call printf ("Warning: Error decoding BZERO, BZERO=0.0\n")
	    if (! rft_equald (dval, 0.0d0) && (scale == YES))
		SCALE(fits) = YES
	} else if (strmatch (card, "^OBJECT  ") != 0) {
	    call rft_get_fits_string (card, OBJECT(im), SZ_OBJECT)
	} else if (strmatch (card, "^IRAFNAME") != 0) {
	    call rft_get_fits_string (card, IRAFNAME(fits), SZ_FNAME)
	} else if (strmatch (card, "^ORIGIN  ") != 0) {
	    call rft_trim_card (card, card, LEN_CARD)
	    call strcat (card[i], HISTORY(im), SZ_HISTORY)
	} else if (strmatch (card, "^DATE    ") !=  0) {
	    call rft_trim_card (card, card, LEN_CARD)
	    call strcat (card[i], HISTORY(im), SZ_HISTORY)
	#} else if (strmatch (card, "^HISTORY ") != 0) {
	    #call rft_trim_card (card, card, LEN_CARD)
	    #call strcat (card[i - 2], HISTORY(im), SZ_HISTORY)
	} else if (strmatch (card, "^UT      ") != 0) {
	    len = rft_hms (card, str, Memc[comment], LEN_CARD)
	    if (len > 0) {
		call wft_encodec ("UT", str, len, card, Memc[comment])
		card[LEN_CARD+1] = '\n'
		card[LEN_CARD+2] = EOS
	    }
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	} else if (strmatch (card, "^ZD      ") != 0) {
	    len = rft_hms (card, str, Memc[comment], LEN_CARD)
	    if (len > 0) {
		call wft_encodec ("ZD", str, len, card, Memc[comment])
		card[LEN_CARD+1] = '\n'
		card[LEN_CARD+2] = EOS
	    }
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	} else if (strmatch (card, "^ST      ") != 0) {
	    len = rft_hms (card, str, Memc[comment], LEN_CARD)
	    if (len > 0) {
		call wft_encodec ("ST", str, len, card, Memc[comment])
		card[LEN_CARD+1] = '\n'
		card[LEN_CARD+2] = EOS
	    }
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	} else if (strmatch (card, "^RA      ") != 0) {
	    len = rft_hms (card, str, Memc[comment], LEN_CARD)
	    if (len > 0) {
		call wft_encodec ("RA", str, len, card, Memc[comment])
		card[LEN_CARD+1] = '\n'
		card[LEN_CARD+2] = EOS
	    }
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	} else if (strmatch (card, "^DEC     ") != 0) {
	    len = rft_hms (card, str, Memc[comment], LEN_CARD)
	    if (len > 0) {
		call wft_encodec ("DEC", str, len, card, Memc[comment])
		card[LEN_CARD+1] = '\n'
		card[LEN_CARD+2] = EOS
	    }
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	} else {
	    if (ndiscard > 1)
		ndiscard = ndiscard + 1
	    else {
	        iferr (call putline (fd_usr, card))
		    ndiscard = ndiscard + 1
	    }
	}

	call sfree (sp)

	return (NO)

end


# RFT_HMS -- Procedure to decode a FITS HMS card from the mountain.

int procedure rft_hms (card, str, comment, maxch)

char	card[ARB]		# FITS card
char	str[ARB]		# string
char	comment[ARB]		# comment string
int	maxch			# maximum number of characters

char	colon, minus
int	ip, nchar, fst, lst, deg, min
real	sec
int	stridx(), strldx(), strlen(), ctoi(), ctor()

begin
	# Return if not a FITS string parameter.
	if (card[COL_VALUE] != '\'')
	    return (0)

	# Set up key characters.
	colon = ':'
	minus = '-'

	# Get the FITS string.
	call rft_get_fits_string (card, str, maxch)

	# Get the comment string.
	call rft_get_comment (card, comment, maxch)

	# Test for blank string and for 2 colon delimiters.
	if (str[1] == EOS)
	    return (0)
	fst = stridx (colon, str)
	if (fst == 0)
	    return (0)
	lst = strldx (colon, str)
	if (lst == 0)
	    return (0)
	if (fst == lst)
	    return (0)

	# Decode the degrees field.
	ip = 1
	while (IS_WHITE(str[ip]))
	    ip = ip + 1
	if (str[ip] == '+' || str[ip] == '-')
	    ip = ip + 1
	nchar = ctoi (str, ip, deg)
	if (nchar == 0)
	    deg = 0

	# Decode the minutes field.
	ip = fst + 1
	while (IS_WHITE(str[ip]))
	    ip = ip + 1
	if (str[ip] == '+' || str[ip] == '-')
	    ip = ip + 1
	nchar = ctoi (str, ip, min)
	if (nchar == 0)
	    min = 0

	# Decode the seconds field.
	ip = lst + 1
	while (IS_WHITE(str[ip]))
	    ip = ip + 1
	if (str[ip] == '+' || str[ip] == '-')
	    ip = ip + 1
	nchar = ctor (str, ip, sec)
	if (nchar == 0)
	    sec = 0.0

	# Reformat the HMS card.
	if (stridx (minus, str) > 0 || deg < 0 || min < 0 || sec < 0.0) {
	    call sprintf (str, maxch, "%c%d:%02d:%05.2f")
		call pargc (minus)
	        call pargi (abs (deg))
	        call pargi (abs (min))
	        call pargr (abs (sec))
	} else {
	    call sprintf (str, maxch, "%2d:%02d:%05.2f")
	        call pargi (deg)
	        call pargi (abs (min))
	        call pargr (abs (sec))
	}

	return (strlen (str))
end


# RFT_GET_COMMENT -- Extract the comment field from a FITS card.

procedure rft_get_comment (card, comment, maxch)

char	card[ARB]		# FITS card
char	comment[ARB]		# comment string
int	maxch			# maximum number of characters

int	istart, j

begin
	istart = 0
	for (j = LEN_CARD; (j >= 1) && (card[j] != '\''); j = j - 1) {
	    if (card[j] == '/') {
		for (istart = j + 1; IS_WHITE(card[istart]) && istart <=
		    LEN_CARD; istart = istart + 1)
		    ;
		break
	    }
	}

	if (istart == 0)
	    comment[1] = EOS
	else
	    call strcpy (card[istart], comment, LEN_CARD - istart + 1 )
end


# RFT_GET_FITS_STRING -- Extract a string from a FITS card and trim trailing
# blanks. The EOS is marked by either ', /, or the end of the card.
# There may be an optional opening ' (FITS standard).

procedure rft_get_fits_string (card, str, maxchar)

char	card[ARB]		# FITS card
char	str[ARB]		# FITS string
int	maxchar			# maximum number of characters

int	j, istart, nchar

begin
	# Check for opening quote
	for (istart = COL_VALUE; istart <= LEN_CARD && card[istart] != '\'';
	    istart = istart + 1)
	    ;
	istart = istart + 1

	# Check for closing quote.
	for (j = istart; (j<LEN_CARD)&&(card[j]!='\''); j = j + 1)
	    ;
	for (j = j - 1; (j >= istart) && (card[j] == ' '); j = j - 1)
	    ;
	nchar = min (maxchar, j - istart + 1)

	# Copy the string.
	if (nchar <= 0)
	    str[1] = EOS
	else
	    call strcpy (card[istart], str, nchar)
end


# RFT_EQUALD -- Procedure to compare two double precision numbers for equality
# to within the machine precision for doubles.

bool procedure rft_equald (x, y)

double	x, y		# the two numbers to be compared for equality

int	ex, ey
double	x1, x2, normed_x, normed_y

begin
	if (x == y)
	    return (true)

	call rft_normd (x, normed_x, ex)
	call rft_normd (y, normed_y, ey)

	if (ex != ey)
	    return (false)
	else {
	    x1 = 1.0d0 + abs (normed_x - normed_y)
	    x2 = 1.0d0 + NEPSILON * EPSILOND
	    return (x1 <= x2)
	}
end


# RFT_NORMED -- Normalize a double precision number x to the value normed_x,
# in the range [1-10]. Expon is returned such that x = normed_x *
# (10.0d0 ** expon).

procedure rft_normd (x, normed_x, expon)

double	x			# number to be normailized
double	normed_x		# normalized number
int	expon			# exponent

double	ax

begin
	ax = abs (x)
	expon = 0

	if (ax > 0) {
	    while (ax < (1.0d0 - NEPSILON * EPSILOND)) {
		ax = ax * 10.0d0
		expon = expon - 1
	    }

	    while (ax >= (10.0d0 - NEPSILON * EPSILOND)) {
		ax = ax / 10.0d0
		expon = expon + 1
	    }
	}

	if (x < 0)
	    normed_x = -ax
	else
	    normed_x = ax
end


# RFT_TRIM_CARD -- Procedure to trim trailing whitespace from the card

procedure rft_trim_card (incard, outcard, maxch)

char	incard[ARB]		# input FITS card image 
char	outcard[ARB]		# output FITS card
int	maxch			# maximum size of card

int	ip

begin
	ip = maxch
	while (incard[ip] == ' ' || incard[ip] == '\t' || incard[ip] == '\0')
	    ip = ip - 1
	call amovc (incard, outcard, ip)
	outcard[ip+1] = '\n'
	outcard[ip+2] = EOS
end


# RFT_LAST_CARD -- Remove a partially written card from the data base

procedure rft_last_user (user, maxch) 

char	user[ARB]	# user area
int	maxch		# maximum number of characters

int	ip

begin
	ip = maxch
	while (user[ip] != '\n')
	    ip = ip - 1
	user[ip+1] = EOS
end