aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/mef/mefrdhdr.x_save
blob: a46d5d041b0cabf3f84e03372f3f13a936268f0c (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
include <error.h>
include <mach.h>
include <ctype.h> 
include <fset.h>
include <mef.h>

# MEFRDHR.X -- Routines to read FITS header units.
#
# 	         mef_rdhdr (mef, group, extname, extver)
# 	         mef_rdblk (in, spp_buf)
# 	         mef_skip_data_unit (mef)
#	totpix = mef_totpix (mef)
# 	         mef_rd2end (mef, read_next_group)
# 	         mef_rdhdr_gn (mef,gn)
# 	         mef_rdhdr_exnv (mef,extname, extver)


# MEF_RDHR -- Read FITS header on a mef file that matches EXTNAME/EXTVER or 
# GROUP number. If both are specified, the former takes procedence.

procedure mef_rdhdr (mef, group, extname, extver)

pointer	mef		#I Mef descriptor
int	group 		#I Group number to read
char    extname[ARB]    #I Extname to read		
int	extver		#I Extver to read

int	open(),in, cur_extn, note(), gnum
int	spool
char	spp_buf[FITS_BLKSZ_NL]
bool	extnv, end_card, read_next_group, mef_rd1st()
bool    mef_cmp_extnv
errchk	open, read, mef_rd1st, mef_load_header

begin
	if (group == MEF_CGROUP(mef))
	    return

	gnum = group
	if (MEF_FD(mef) == NULL) {
	    MEF_FD(mef) = open (MEF_FNAME(mef), READ_ONLY, BINARY_FILE)
	    MEF_ENUMBER(mef) = -1 
	    MEF_CGROUP(mef) = -1 
	} 
	MEF_SKDATA(mef) = NO  

	in = MEF_FD(mef)

	extnv = extname[1] != EOS || extver != INDEFL
	if (extnv)
	   spool = open ("spool", NEW_FILE, SPOOL_FILE)

	if (gnum == -1 || extnv)
	    gnum = 0
#	else if (gnum != -1 && extnv)
#	    gnum = -1			# EXTNAME/EXTVER takes precedence
	   
	cur_extn = MEF_CGROUP(mef)
#	if (cur_extn < 0) 
#	    cur_extn = -1	        # Ready to read PHU
	read_next_group = true

	repeat {
	   # If we need to read the next group
	   if (read_next_group) {
	       # Read 1st block 
	       cur_extn = cur_extn+1

	       # See if this extension contains the correct 
	       # extname/extver values.

	       if (extnv) {
		   end_card = true
		   # We are not sure if extname or extver are in the
		   # 1st block.
	           call fseti (spool, F_CANCEL, YES)
		   call mef_load_header (mef, spool)
#		   iferr (call mef_load_header (mef, spool)) {
#		       call erract(EA_WARN)
#		   }

		   read_next_group = mef_cmp_extnv (mef, extname, extver)
		   MEF_POFF(mef) = note(in)
		   call mef_skip_data_unit (mef)
		   next
	       } else {
	           end_card = mef_rd1st (mef, spp_buf) 
		   if (gnum == cur_extn) 
		       read_next_group = false
	       }

	       if (read_next_group) {
		   if (!end_card)
		       call mef_rd2end (mef, read_next_group)
		   call mef_skip_data_unit (mef)
	       }
	   } else {			# This is the group we want
	       if (MEF_HDRP(mef) != NULL)
		   call mfree (MEF_HDRP(mef), TY_CHAR)
	       if (end_card) {
		   if (extnv) {
		       call mef_cp_spool (spool, mef)
                       cur_extn = cur_extn + 1
		   } else {
		       call malloc (MEF_HDRP(mef), MEF_HSIZE(mef)+1, TY_CHAR)
		       call amovc (spp_buf, Memc[MEF_HDRP(mef)], MEF_HSIZE(mef))
		       Memc[MEF_HDRP(mef)+MEF_HSIZE(mef)] = EOS
		   }
	       } else {
		   call malloc (MEF_HDRP(mef), FITS_BLKSZ_NL, TY_CHAR)
		   call amovc (spp_buf, Memc[MEF_HDRP(mef)], FITS_BLKSZ_NL)
		   call mef_rd2end (mef, read_next_group)
	       }
	       if (!extnv) {
		   if (MEF_NDIM(mef) != 0 || MEF_PCOUNT(mef) > 0) 
		       MEF_POFF(mef) = note(in)
		   else
		       MEF_POFF(mef) = INDEFL
	           call mef_skip_data_unit (mef)
	       }
	       MEF_CGROUP(mef) = cur_extn

	       # To indicate that data has been skipped.
	       MEF_SKDATA(mef) = YES  

	       return
	   }
	}
end


# MEF_RD1ST -- Handle the 1st FITS header block.
# Return true if the END card is in this 1st block.

bool procedure	mef_rd1st (mef, hbuf)

pointer mef		#I Mef descriptor
char	hbuf[ARB]	#O Buffer containing the first block of a unit

int	in, k, i, index, mef_kctype()
int	strncmp(), note()
pointer	sp, errmsg

errchk  mef_rdblk

begin
	in = MEF_FD(mef)

	# Read 1st block.
 	MEF_HOFF(mef) = note(in)
	call mef_rdblk (in, hbuf)

	MEF_EXTNAME(mef) = EOS
	MEF_EXTVER(mef) = INDEFL
	k = 1
        # Verify FITS header
	if (strncmp (hbuf[k], "SIMPLE  ", 8) != 0 &&
	    strncmp (hbuf[k], "XTENSION", 8) != 0 ) {
            call smark (sp)
            call salloc (errmsg, SZ_LINE, TY_CHAR)
            call sprintf (Memc[errmsg], SZ_LINE, "Extension %s[%d] is not FITS.")
                call pargstr(MEF_FNAME(mef))
                call pargi(MEF_CGROUP(mef))
            call error (13, Memc[errmsg])
#            iferr (call error (13, Memc[errmsg])) {
#                call sfree (sp)
#                call erract (EA_ERROR)
#            }
	} else {
	    call mef_gvalt (hbuf[k], MEF_EXTTYPE(mef), MEF_SZVALSTR)
	    if (strncmp (hbuf[k], "SIMPLE  ", 8) == 0)
	        call strcpy (MEF_FNAME(mef), MEF_EXTTYPE(mef), MEF_SZVALSTR)
	}
	k = k + LEN_CARDNL

	MEF_PCOUNT(mef) = 0

	for (i=2; i< 37; i=i+1) {
           switch (mef_kctype(hbuf[k], index)) {
	   case NAXIS:
	      MEF_NDIM(mef) = index
	   case NAXISN:
	      call mef_gvali (hbuf[k], MEF_NAXIS(mef,index))
	   case BITPIX:
	      call mef_gvali (hbuf[k], MEF_BITPIX(mef))
	   case EXTNAME:
	      call mef_gvalt (hbuf[k], MEF_EXTNAME(mef), MEF_SZVALSTR)
	   case EXTVER:
	      call mef_gvali (hbuf[k], MEF_EXTVER(mef))
	   case PCOUNT:
	      call mef_gvali (hbuf[k], MEF_PCOUNT(mef))
	   case OBJECT:
	      call mef_gvalt (hbuf[k], MEF_OBJECT(mef), MEF_SZVALSTR)
	   case END:
	      MEF_HSIZE(mef) = i*LEN_CARDNL
              return(true)	
	      break
	   default:
	      ;
	   }
	   k = k + LEN_CARDNL
	}
	return(false)
	    
end


# MEF_RDBLK -- Read one header FITS block from disk and add a newline 
# after each fits record (80 chars).

procedure mef_rdblk (in, spp_buf)

int	in		#I File descriptor
char    spp_buf[ARB]	#O Buffer with header

char    ibuf[FITS_BLKSZ_CHAR]
int     nchar, i, read(), k, j
char    line[LEN_CARD]

begin
	nchar = read (in, ibuf, FITS_BLKSZ_CHAR)
	if (nchar == EOF) 
	   call error(13, "EOF encountered")

	# Unpack the input buffer to spp char with new_line delimited records.
	line[LEN_CARDNL] = '\n'
	k = 1
	j = 1
	for (i=1; i<37; i=i+1) {
   	   call achtbc(ibuf[k], line, LEN_CARD)
	   call amovc (line, spp_buf[j], LEN_CARDNL)
	   k = k + 40		
	   j = j + LEN_CARDNL
	}
end	 


# MEF_CMP_EXTNV -- Compare the EXTNAME and EXTVER header values with the
#		  ones passed as arguments. Return false if matched.

bool procedure mef_cmp_extnv (mef, extname, extver)
pointer	mef
char	extname[ARB] 	#I extname value
int	extver		#I extver value

int	mef_strcmp_lwr()
bool	bxtn, bxtv, bval, bxtn_eq, bxtv_eq

begin
	bxtn = extname[1] != EOS
	bxtv = extver != INDEFL

	if (bxtn)
	    bxtn_eq = (mef_strcmp_lwr(MEF_EXTNAME(mef), extname) == 0)
	if (bxtv)
	    bxtv_eq = (MEF_EXTVER(mef) == extver)
	
	if (bxtn && bxtv)
	    # Both EXTNAME and EXTVER are defined.
	    bval = bxtn_eq && bxtv_eq
	else if (bxtn && !bxtv)
	    # Only EXTNAME is defined.
	    bval = bxtn_eq
	else if (!bxtn && bxtv)
	    # Only EXTVER is defined.
	    bval = bxtv_eq 
	else
	    bval = false

	return (!bval)
end

# MEF_SKIP_DATA_UNIT -- Skip data unit. The file is already position at the
# end of the last header block.

procedure mef_skip_data_unit (mef)

pointer	mef	#I Input mef descriptor

int	in, ndim, off, note(), mef_totpix()
errchk  seek

begin
	# See if data portion has already been skipped.
	if (MEF_SKDATA(mef) == YES)
	    return

	in = MEF_FD(mef)
	ndim = MEF_NDIM (mef)
        if (ndim > 0 || MEF_PCOUNT(mef) > 0) {
	   # Skip to the beginning of next extension
	   off = note(in)
	   if (off == EOF)
	      return
	   off = off + mef_totpix(mef)
	   call seek (in, off)
	}
end


# MEF_TOTPIX -- Returns the number of pixels in the data area in units
#		of chars.

int procedure mef_totpix (mef)

pointer	mef		#I Mef descriptor

int	ndim, totpix, i, bitpix

begin
	ndim = MEF_NDIM (mef)
	if (ndim == 0 && MEF_PCOUNT(mef) <= 0)
	    return (0)

	if (ndim == 0)
	    totpix = 0
	else {
	    totpix = MEF_NAXIS(mef,1) 
	    do i = 2, ndim
		  totpix = totpix *  MEF_NAXIS(mef,i)
	}
	bitpix = abs(MEF_BITPIX(mef))

	# If PCOUNT is not zero, add it to totpix
	totpix = MEF_PCOUNT(mef) + totpix

	if (bitpix <= NBITS_BYTE)
	    totpix = (totpix + 1) / SZB_CHAR
	else
	    totpix = totpix * (bitpix / (SZB_CHAR * NBITS_BYTE))

	# Set the number of characters in multiple of 1440.
	totpix = ((totpix + 1439)/1440) * 1440
	return (totpix)
end


# MEF_RD2END --  Read from block 2 to the end.

procedure mef_rd2end (mef, read_next_group)

pointer	mef			#I mef descriptor	
bool	read_next_group		#I if true, read current header to END

char	hbuf[FITS_BLKSZ_NL] 
int	in, k,i, nblks, strncmp(), size_last_block, hoffset
errchk  mef_rdblk

begin
	in = MEF_FD(mef)
	# We need to read the header only.
        if (read_next_group)
	   repeat {
	      k = 1
	      call mef_rdblk (in, hbuf)
	      for (i=1; i<37; i=i+1) {
	          if (strncmp (hbuf[k], "END     " , 8) == 0)
		     return
	          else
		     k = k + LEN_CARDNL
	      }
	   }
	   

	# This is the requested header, copy to user area.
	nblks = 2
	repeat {
	   k = 1
	   call mef_rdblk (in, hbuf)
	   # Copy the buffer into the user area.
	   for (i=1; i<37; i=i+1) {
	       if (strncmp (hbuf[k], "END     " , 8) == 0) {
                  size_last_block = i*LEN_CARDNL
	          call realloc (MEF_HDRP(mef), FITS_BLKSZ_NL*nblks+1, TY_CHAR)
	          hoffset = MEF_HDRP(mef)+FITS_BLKSZ_NL*(nblks-1)
		  call amovc (hbuf, Memc[hoffset], size_last_block)
		  Memc[hoffset+size_last_block] = EOS
		  MEF_HSIZE(mef) = (nblks-1)*FITS_BLKSZ_NL + size_last_block
		  return
	       } else
		  k = k + LEN_CARDNL
	   }
	   call realloc (MEF_HDRP(mef), FITS_BLKSZ_NL*nblks, TY_CHAR)
	   hoffset = MEF_HDRP(mef)+FITS_BLKSZ_NL*(nblks-1)
	   call amovc (hbuf, Memc[hoffset], FITS_BLKSZ_NL)
	   nblks = nblks + 1
	}
end


# MEF_STRCMP_LWR -- Compare 2 strings in lower case

int procedure mef_strcmp_lwr (s1, s2)

char s1[ARB], s2[ARB]

pointer sp, l1, l2
int	strcmp(), istat

begin
	call smark(sp)
	call salloc (l1, SZ_FNAME, TY_CHAR)
	call salloc (l2, SZ_FNAME, TY_CHAR)

	call strcpy (s1, Memc[l1], SZ_FNAME)
	call strcpy (s2, Memc[l2], SZ_FNAME)
	call strlwr(Memc[l1])
	call strlwr(Memc[l2])
	istat = strcmp (Memc[l1], Memc[l2])

	call sfree(sp)
	return (istat)
end


# MEF_KCTYPE -- Find the type of card that is based on the keyword name.

int procedure mef_kctype (card, index)

char	card[ARB]	#I FITS card
int	index		#O index value

int	strncmp()

begin
	if (strncmp (card, "SIMPLE  ", 8) == 0)
	   return (SIMPLE)
	if (strncmp (card, "NAXIS", 5) == 0) {
	   if (card[6] == ' ') {
	      call mef_gvali (card, index)
	      return (NAXIS)
    	   } else  if (IS_DIGIT(card[6])) {
	       index = TO_INTEG(card[6])
	       return (NAXISN)                      # NAXISn
     	   }
	}
	if (strncmp (card, "BITPIX  ", 8) == 0)
	   return (BITPIX)
	if (strncmp (card, "EXTNAME ", 8) == 0)
	   return (EXTNAME)
	if (strncmp (card, "EXTVER  ", 8) == 0)
	   return (EXTVER)
	if (strncmp (card, "EXTEND  ", 8) == 0)
	   return (EXTEND)
	if (strncmp (card, "PCOUNT  ", 8) == 0)
	   return (PCOUNT)
	if (strncmp (card, "FILENAME", 8) == 0)
	   return (FILENAME)
	if (strncmp (card, "INHERIT ", 8) == 0)
	   return (INHERIT)
	if (strncmp (card, "GCOUNT  ", 8) == 0)
	   return (GCOUNT)
	if (strncmp (card, "OBJECT  ", 8) == 0)
	   return (OBJECT)
	if (strncmp (card, "XTENSION", 8) == 0)
	   return (XTENSION)
	if (strncmp (card, "END     ", 8) == 0)
	   return (END)

        return(ERR)
end


# MEF_RDHDR_GN -- Read group based on group number

procedure mef_rdhdr_gn (mef,gn)

pointer	mef 		#I mef descriptor
int	gn		#I group number to read

char	extname[MEF_SZVALSTR]
int	extver

errchk  mef_rdhdr

begin
	extname[1] =EOS
	extver=INDEFL
	call mef_rdhdr (mef, gn, extname, extver)
end


# MEF_RDHDR_EXNV -- Read group based on the Extname and Extver values

procedure mef_rdhdr_exnv (mef,extname, extver)

pointer	mef		#I, mef descriptor
char	extname[ARB]	#I, extname value
int	extver		#I, extver value

errchk  mef_rdhdr

begin
	call mef_rdhdr (mef, 0, extname, extver)
end


# MEF_CP_SPOOL -- 

procedure mef_cp_spool (spool, mef)

int	spool 		#I spool file descriptor
pointer mef		#

pointer	hdr, lbuf, sp
int	fitslen, fstatl, user
int	stropen(), getline()

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

	call seek (spool, BOFL)
	fitslen = fstatl (spool, F_FILESIZE)
	fitslen = max (fitslen, MEF_HSIZE(mef))
	call malloc (hdr, fitslen, TY_CHAR)
	user = stropen (Memc[hdr], fitslen, NEW_FILE)

	# Append the saved FITS cards to saved cache.
	while (getline (spool, Memc[lbuf]) != EOF)
	    call putline (user, Memc[lbuf])

	call close (user)
	call close (spool)

	MEF_HDRP(mef) = hdr

	call sfree(sp)
end