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

# MEFRDHR.X -- Routines to read FITS header units.
#
#     eof|stat = mef_rdhdr (mef, group, extname, extver)
# 	         mef_skip_data_unit (mef)
#	totpix = mef_totpix (mef)
#     eof|stat = mef_rdhdr_gn (mef,gn)
#     eof|stat = 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.

int 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
bool	extnv, read_next_group
int	mef_load_header(), mef_pixtype()
bool    mef_cmp_extnv
errchk	open, read, mef_load_header

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

	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
	spool = open ("spool", NEW_FILE, SPOOL_FILE)

	if (gnum == -1 || extnv)
	    gnum = 0
	   
	cur_extn = MEF_CGROUP(mef)
	read_next_group = true

	repeat {
	   # If we need to read the next group
	   if (read_next_group) {

	       cur_extn = cur_extn+1

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

	       call fseti (spool, F_CANCEL, YES)
	       if (mef_load_header (mef, spool, cur_extn) == EOF) {
		   call close (spool)
		   return (EOF)
	       }			

	       # We read the header already, marked the spot.
	       MEF_POFF(mef) = note(in)

	       if (extnv) {
		   read_next_group = mef_cmp_extnv (mef, extname, extver)
	       } else {
		   if (gnum == cur_extn) 
		       read_next_group = false
	       }
	       call mef_skip_data_unit (mef)
	       next

	   } else {			# This is the group we want
	       if (MEF_HDRP(mef) != NULL)
		   call mfree (MEF_HDRP(mef), TY_CHAR)

	       call mef_cp_spool (spool, mef)
	       MEF_CGROUP(mef) = cur_extn

	       # To indicate that data has been skipped.
	       MEF_SKDATA(mef) = YES  
	       break
	   }
	}
	call close (spool)
	MEF_DATATYPE(mef) = mef_pixtype(mef)
	return (cur_extn)
end

int procedure mef_pixtype (mef)
pointer mef, hdrp
bool    bfloat, lscale, lzero
bool    fxf_fpl_equald()
int	i, impixtype, ctod(), ip
double  bscale, bzero
char    sval[LEN_CARD]

begin
        hdrp= MEF_HDRP(mef)
	bscale = 1.0d0
	ip=1
	ifnoerr (call mef_findkw (hdrp, "BSCALE", sval))
	     i = ctod(sval,ip,bscale)
	bzero = 0.0d0
	ip=1
	ifnoerr (call mef_findkw (hdrp, "BZERO", sval))
	     i = ctod(sval,ip,bzero)

	lscale = fxf_fpl_equald (1.0d0, bscale, 1)
	lzero =  fxf_fpl_equald (0.0d0, bzero, 1)

	# Determine if scaling is necessary.
	bfloat = (!lscale || !lzero)

	switch (MEF_BITPIX(mef)) {
	case  8:
	    if (bfloat)
		impixtype = TY_REAL
	    else
		impixtype = TY_SHORT              # convert from byte to short
	case 16:
	    if (bfloat) {
		impixtype = TY_REAL
	    } else
		impixtype = TY_SHORT

	    if (lscale && fxf_fpl_equald (32768.0d0, bzero, 4)) {
		impixtype = TY_USHORT
	    }
	case 32:
	    if (bfloat)
		impixtype = TY_REAL
	    else
		impixtype = TY_INT
	case -32:
	    impixtype = TY_REAL
	case -64:
	    impixtype = TY_DOUBLE
	default:
	    impixtype = ERR
	}

	return(impixtype)

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_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

int procedure mef_rdhdr_gn (mef,gn)

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

char	extname[MEF_SZVALSTR]
int	extver
int	mef_rdhdr()

errchk  mef_rdhdr

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


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

int procedure mef_rdhdr_exnv (mef,extname, extver)

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

errchk  mef_rdhdr

begin
	return (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