aboutsummaryrefslogtreecommitdiff
path: root/sys/imio/iki/stf/stfrdhdr.x
blob: 2c11fec96aced4997af1b5a96f1911d12e307cc5 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<finfo.h>
include	<imhdr.h>
include	<imio.h>
include	<mach.h>
include	"stf.h"

# STF_RDHEADER -- Read the STF format image header for a single group into the
# IMIO descriptor.  The standard fields are processed into the fields of the
# descriptor.  The GPB binary parameters are encoded as FITS cards and placed
# in the IMIO user area, followed by all extra cards in the FITS format STF
# group header.  Note that no distinction is made between the common FITS
# keywords and the GPB group parameters at the IMIO level and above.

procedure stf_rdheader (im, group, acmode)

pointer	im		# image descriptor
int	group		# group to be accessed
int	acmode		# access mode

long	pixoff
long	fi[LEN_FINFO]
real	datamin, datamax
pointer	sp, stf, lbuf, root, extn, op
int	compress, devblksz, ival, ch, i , junk
int	fits, fitslen, sz_userarea, sz_gpbhdr, len_hdrmem
long	totpix, mtime, ctime

real	imgetr()
int	fnroot(), strlen(), finfo(), imaccf()
errchk	stf_rfitshdr, stf_rgpb, open, realloc, imaddb, imaddi, imgetr

include <szpixtype.inc>

begin
	call smark (sp)
	call salloc (lbuf, SZ_LINE, TY_CHAR)
	call salloc (root, SZ_FNAME, TY_CHAR)
	call salloc (extn, SZ_FNAME, TY_CHAR)

	stf = IM_KDES(im)

	# Read the FITS header, setting the values of all reserved fields
	# in the STF descriptor and saving all the user FITS cards in the
	# save buffer "fits".

	call stf_rfitshdr (im, fits, fitslen)

	# Process the reserved keywords (set in the STF descriptor) into the
	# corresponding fields of the IMIO descriptor.

        # Set group keywords if STF_GROUPS is NO (BPS 12.06.91).
        if (STF_GROUPS(stf) == NO) {
            STF_GCOUNT(stf) = 1
            STF_PCOUNT(stf) = 0
            STF_PSIZE(stf) = 0
        }

	if (acmode != NEW_COPY) {
	    IM_NDIM(im) = STF_NAXIS(stf)		# IM_NDIM
	    do ival = 1, IM_MAXDIM			# IM_LEN
		IM_LEN(im,ival) = STF_LENAXIS(stf,ival)
	}

	ch = STF_DATATYPE(stf)				# IM_PIXTYPE
	switch (STF_BITPIX(stf)) {
	case 16:
	    if (ch == 'U')
		ival = TY_USHORT
	    else
	        ival = TY_SHORT
	case 32:
	    if (ch == 'R')
		ival = TY_REAL
	    else
		ival = TY_LONG
	case 64:
	    if (ch == 'R')
		ival = TY_DOUBLE
	    else
		ival = TY_COMPLEX
	default:
	    ival = ERR
	}
	IM_PIXTYPE(im) = ival

	call iki_parse (IM_HDRFILE(im), Memc[root], Memc[extn])
	call stf_mkpixfname (Memc[root], Memc[extn], IM_PIXFILE(im),
	    SZ_IMPIXFILE)

	if (finfo (IM_PIXFILE(im), fi) != ERR) {
	    mtime = FI_MTIME(fi)
	    ctime = FI_CTIME(fi)
	}

	IM_NBPIX(im)   = 0			# no. bad pixels
	IM_CTIME(im)   = ctime			# creation time
	IM_MTIME(im)   = mtime			# modify time
	IM_LIMTIME(im) = mtime - 1		# time max/min last updated
	IM_UABLOCKED(im) = YES			# ua cards blocked to 80 chars

	IM_HISTORY(im) = EOS
	junk = fnroot (IM_HDRFILE(im), Memc[lbuf], SZ_LINE)
	call strupr (Memc[lbuf])
	call sprintf (IM_TITLE(im), SZ_IMTITLE, "%s[%d/%d]")
	    call pargstr (Memc[lbuf])
	    call pargi (STF_GROUP(stf))
	    call pargi (STF_GCOUNT(stf))

	# Compute the size of each group in the pixel file, in chars.
	totpix = IM_LEN(im,1)
	do i = 2, IM_NDIM(im)
	    totpix = totpix * IM_LEN(im,i)

	STF_SZGROUP(stf) = totpix * pix_size[IM_PIXTYPE(im)] +
	    STF_PSIZE(stf) / (SZB_CHAR * NBITS_BYTE)

	# Write GPB related cards to the beginning of the IMIO user area.
	call imaddb (im, "GROUPS", STF_GROUPS(stf) == YES)
	call imaddi (im, "GCOUNT", STF_GCOUNT(stf))
	call imaddi (im, "PCOUNT", STF_PCOUNT(stf))
	call imaddi (im, "PSIZE", STF_PSIZE(stf))

	# Extract the group parameter block from the pixfile, encoding the
	# group parameters as FITS cards and appending to the cards above.
	# Get the values of DATAMIN and DATAMAX from the GPB so that we can
	# update the IMIO min/max fields.

	call stf_rgpb (im, group, acmode, datamin, datamax)

	# Reallocate the image descriptor to allow space for the spooled user
	# FITS cards plus a little extra for new parameters.

	sz_gpbhdr = strlen (Memc[IM_USERAREA(im)])
	sz_userarea = sz_gpbhdr + fitslen + SZ_EXTRASPACE

	IM_HDRLEN(im) = LEN_IMHDR +
	    (sz_userarea - SZ_EXTRASPACE + SZ_MII_INT-1) / SZ_MII_INT
	len_hdrmem = LEN_IMHDR +
	    (sz_userarea+1 + SZ_MII_INT-1) / SZ_MII_INT

	if (IM_LENHDRMEM(im) < len_hdrmem) {
	    IM_LENHDRMEM(im) = len_hdrmem
	    call realloc (im, IM_LENHDRMEM(im) + LEN_IMDES, TY_STRUCT)
	}

	# Append the saved FITS cards from the STF header to the user area.
	# Any cards which redefine GPB cards were already deleted when the
	# fits save buffer was created (we don't want the GPB cards since
	# we already output a FITS card for each GPB parameter above).

	op = IM_USERAREA(im) + sz_gpbhdr
	call amovc (Memc[fits], Memc[op], fitslen+1)

	# Set the IMIO min/max fields.  If the GPB datamin >= datamax the
	# values are invalidated by setting IM_LIMTIME to before the image
	# modification time.  Although datamin/datamax were returned by
	# stg_rgpb above, we refetch the values here to pick up the values
	# from the spooled main header in case there were no entries for
	# these keywords in the GPB (if there are values in the GPB they
	# will override those in the main header).

	if (imaccf (im, "DATAMIN") == YES)
	    datamin = imgetr (im, "DATAMIN")
	if (imaccf (im, "DATAMAX") == YES)
	    datamax = imgetr (im, "DATAMAX")

	IM_MIN(im) = datamin
	IM_MAX(im) = datamax
	if (datamin < datamax)
	    IM_LIMTIME(im) = IM_MTIME(im) + 1
	else
	    IM_LIMTIME(im) = IM_MTIME(im) - 1

	# Call up IMIO set set up the remaining image header fields used to
	# define the physical offsets of the pixels in the pixfile.

	compress = YES		# do not align image lines on blocks
	devblksz = 1		# disable all alignment

	pixoff = (group - 1) * STF_SZGROUP(stf) + 1
	call imioff (im, pixoff, compress, devblksz)

	call sfree (sp)
end