aboutsummaryrefslogtreecommitdiff
path: root/pkg/obsolete/fits/fits_wimage.x
blob: 539114ea5002429ca6297d3bd38496f39106241b (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <error.h>
include <imhdr.h>
include "wfits.h"

# WFT_WRITE_IMAGE -- Procedure to convert IRAF image data to FITS format line by
# line.

procedure wft_write_image (im, fits, fits_fd)

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

int	npix, nlines, npix_record, i, stat, nrecords
long	v[IM_MAXDIM]
pointer	tempbuf, buf

int	wft_get_image_line()
errchk	malloc, mfree, wft_get_image_line, wft_lscale_line, wft_long_line
errchk	wft_init_write_pixels, wft_write_pixels, wft_write_last_record
errchk	wft_rscale_line, wft_dscale_line

include "wfits.com"

begin
	if (NAXIS(im) == 0)
	    return

	# Initialize.
	npix = NAXISN(im,1)
	nlines = 1
	do i = 2, NAXIS(im)
	    nlines = nlines * NAXISN(im, i)
	npix_record = len_record * FITS_BYTE / abs (FITS_BITPIX(fits))

	call amovkl (long(1), v, IM_MAXDIM)
	switch (FITS_BITPIX(fits)) {
	case FITS_REAL:

	    # Allocate temporary space.
	    call malloc (tempbuf, npix, TY_REAL)

	    # Initialize the pixel write.
	    call wft_init_write_pixels (npix_record, TY_REAL,
	        FITS_BITPIX(fits), blkfac)

	    # For the time being explicitly turn off ieee NaN mapping.
	    call ieemapr (NO, NO)

	    # Scale the lines, deal with the blanks via the ieee code which
	    # is currently turned off, and write the output records.

	    do i = 1, nlines {
	        iferr (stat =  wft_get_image_line (im, buf, v, PIXTYPE(im))) {
		    call erract (EA_WARN)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        }
	        if (stat == EOF )
		    return
	        if (stat != npix)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        if (SCALE(fits) == YES)
	            call wft_rscale_line (buf, Memr[tempbuf], npix,
		        1. / BSCALE(fits), -BZERO(fits), PIXTYPE(im))
		else
		    call wft_real_line (buf, Memr[tempbuf], npix, PIXTYPE(im)) 
	        call wft_write_pixels (fits_fd, Memr[tempbuf], npix)
	    }

	    # Free space.
	    call mfree (tempbuf, TY_REAL)

	case FITS_DOUBLE:

	    # Allocate temporary space.
	    call malloc (tempbuf, npix, TY_DOUBLE)

	    # Initialize the pixel write.
	    call wft_init_write_pixels (npix_record, TY_DOUBLE,
	        FITS_BITPIX(fits), blkfac)

	    # For the time being explicitly turn off ieee NaN mapping.
	    call ieemapd (NO, NO)

	    # Scale the lines, deal with the blanks via the ieee code which
	    # is currently turned off, and write the output records.

	    do i = 1, nlines {
	        iferr (stat =  wft_get_image_line (im, buf, v, PIXTYPE(im))) {
		    call erract (EA_WARN)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        }
	        if (stat == EOF )
		    return
	        if (stat != npix)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        if (SCALE(fits) == YES)
	            call wft_dscale_line (buf, Memd[tempbuf], npix,
		        1. / BSCALE(fits), -BZERO(fits), PIXTYPE(im))
		else
		    call wft_double_line (buf, Memd[tempbuf], npix,
		        PIXTYPE(im)) 
	        call wft_write_pixels (fits_fd, Memd[tempbuf], npix)
	    }

	    # Free space.
	    call mfree (tempbuf, TY_DOUBLE)

	default:

	    # Allocate temporary space.
	    call malloc (tempbuf, npix, TY_LONG)

	    # Scale the line, deal with the blanks, and write the output
	    # record. At the moement blanks are not dealt with.

	    call wft_init_write_pixels (npix_record, TY_LONG, FITS_BITPIX(fits),
	         blkfac)
	    do i = 1, nlines {
	        iferr (stat =  wft_get_image_line (im, buf, v, PIXTYPE(im))) {
		    call erract (EA_WARN)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        }
	        if (stat == EOF )
		    return
	        if (stat != npix)
		    call error (10, "WRT_IMAGE: Error writing IRAF image.")
	        if (SCALE(fits) == YES)
	            call wft_lscale_line (buf, Meml[tempbuf], npix,
		        1. / BSCALE(fits), -BZERO(fits), PIXTYPE(im))
	        else
		    call wft_long_line (buf, Meml[tempbuf], npix, PIXTYPE(im)) 
	        # call map_blanks (im, Meml[tempbuf], blank)
	        call wft_write_pixels (fits_fd, Meml[tempbuf], npix)
	    }
	    # Free space.
	    call mfree (tempbuf, TY_LONG)
	}

	# Write the final record.
	call wft_write_last_record (fits_fd, nrecords)
	if (short_header == YES || long_header == YES) {
	    call printf ("%d  Data logical (2880 byte) records written\n")
	        call pargi (nrecords)
	}
end


# WFT_GET_IMAGE_LINE -- Procedure to fetch the next image line.

int procedure wft_get_image_line (im, buf, v, datatype)

pointer	im			# IRAF image descriptor
pointer	buf			# pointer to image line
long	v[ARB]			# imio dimension descriptor
int	datatype		# IRAF image data type

int	npix
int	imgnll(), imgnlr(), imgnld(), imgnlx()
errchk	imgnll, imgnlr, imgnld, imgnlx

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_USHORT, TY_LONG:
	    npix = imgnll (im, buf, v)
	case TY_REAL:
	    npix = imgnlr (im, buf, v)
	case TY_DOUBLE:
	    npix = imgnld (im, buf, v)
	case TY_COMPLEX:
	    npix = imgnlx (im, buf, v)
	default:
	    call error (11, "GET_IMAGE_LINE: Unknown IRAF image type.")
	}

	return (npix)
end


# WFT_RSCALE_LINE -- This procedure converts the IRAF data to type real
# and scales by the FITS parameters bscale and bzero.

procedure wft_rscale_line (buf, outbuffer, npix, bscale, bzero, datatype)

pointer	buf			# pointer to IRAF image line
real	outbuffer[ARB]		# FITS integer buffer
int	npix			# number of pixels
double	bscale, bzero		# FITS bscale and bzero parameters
int	datatype		# data type of image

errchk	achtlr, altadr, amovr, achtdr, acthxr

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call achtlr (Meml[buf], outbuffer, npix)
	    call altadr (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_REAL:
	    call amovr (Memr[buf], outbuffer, npix)
	    call altadr (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_DOUBLE:
	    call achtdr (Memd[buf], outbuffer, npix)
	    call altadr (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_COMPLEX:
	    call achtxr (Memx[buf], outbuffer, npix)
	    call altadr (outbuffer, outbuffer, npix, bzero, bscale)
	default:
	    call error (12, "WFT_RSCALE_LINE: Unknown IRAF image type.")
	}
end


# WFT_DSCALE_LINE -- This procedure converts the IRAF data to type double with
# after scaling by the FITS parameters bscale and bzero.

procedure wft_dscale_line (buf, outbuffer, npix, bscale, bzero, datatype)

pointer	buf			# pointer to IRAF image line
double	outbuffer[ARB]		# FITS integer buffer
int	npix			# number of pixels
double	bscale, bzero		# FITS bscale and bzero parameters
int	datatype		# data type of image

errchk	achtld, altad, amovd, achtrd, achtxd

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call achtld (Meml[buf], outbuffer, npix)
	    call altad (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_REAL:
	    call achtrd (Memr[buf], outbuffer, npix)
	    call altad (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_DOUBLE:
	    call amovd (Memd[buf], outbuffer, npix)
	    call altad (outbuffer, outbuffer, npix, bzero, bscale)
	case TY_COMPLEX:
	    call achtxd (Memx[buf], outbuffer, npix)
	    call altad (outbuffer, outbuffer, npix, bzero, bscale)
	default:
	    call error (12, "WFT_DSCALE_LINE: Unknown IRAF image type.")
	}
end


# WFT_LSCALE_LINE -- This procedure converts the IRAF data to type long with
# after scaling by the FITS parameters bscale and bzero.

procedure wft_lscale_line (buf, outbuffer, npix, bscale, bzero, datatype)

pointer	buf			# pointer to IRAF image line
long	outbuffer[ARB]		# FITS integer buffer
int	npix			# number of pixels
double	bscale, bzero		# FITS bscale and bzero parameters
int	datatype		# data type of image

errchk  altal, amovl, altadr, achtrl, altad, achtdl, altax, achtxl

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call altal (Meml[buf], Meml[buf], npix, bzero, bscale)
	    call amovl (Meml[buf], outbuffer, npix)
	case TY_REAL:
	    call altarl (Memr[buf], outbuffer, npix, bzero, bscale)
	    #call altadr (Memr[buf], Memr[buf], npix, bzero, bscale)
	    #call achtrl (Memr[buf], outbuffer, npix)
	case TY_DOUBLE:
	    call altad (Memd[buf], Memd[buf], npix, bzero, bscale)
	    call achtdl (Memd[buf], outbuffer, npix)
	case TY_COMPLEX:
	    call altadx (Memx[buf], Memx[buf], npix, bzero, bscale)
	    call achtxl (Memx[buf], outbuffer, npix)
	default:
	    call error (12, "WFT_LSCALE_LINE: Unknown IRAF image type.")
	}
end


# WFT_REAL_LINE -- This procedure converts the IRAF image line to type long with
# no scaling.

procedure wft_real_line (buf, outbuffer, npix, datatype)

pointer	buf			# pointer to IRAF image line
real	outbuffer[ARB]		# buffer of FITS integers
int	npix			# number of pixels
int	datatype		# IRAF image datatype

errchk	achtlr, achtdr, amovr, achtxr

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call achtlr (Meml[buf], outbuffer, npix)
	case TY_REAL:
	    call amovr (Memr[buf], outbuffer, npix)
	case TY_DOUBLE:
	    call achtdr (Memd[buf], outbuffer, npix)
	case TY_COMPLEX:
	    call achtxr (Memx[buf], outbuffer, npix)
	default:
	    call error (13, "WFT_REAL_LINE: Unknown IRAF data type.")
	}
end


# WFT_DOUBLE_LINE -- This procedure converts the IRAF image line to type long
# with no scaling.

procedure wft_double_line (buf, outbuffer, npix, datatype)

pointer	buf			# pointer to IRAF image line
double  outbuffer[ARB]		# buffer of FITS integers
int	npix			# number of pixels
int	datatype		# IRAF image datatype

errchk	achtld, achtrd, amovd, achtxd

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call achtld (Meml[buf], outbuffer, npix)
	case TY_REAL:
	    call achtrd (Memr[buf], outbuffer, npix)
	case TY_DOUBLE:
	    call amovd (Memd[buf], outbuffer, npix)
	case TY_COMPLEX:
	    call achtxd (Memx[buf], outbuffer, npix)
	default:
	    call error (13, "WFT_DOUBLE_LINE: Unknown IRAF data type.")
	}
end


# WFT_LONG_LINE -- This procedure converts the IRAF image line to type long with
# no scaling.

procedure wft_long_line (buf, outbuffer, npix, datatype)

pointer	buf			# pointer to IRAF image line
long	outbuffer[ARB]		# buffer of FITS integers
int	npix			# number of pixels
int	datatype		# IRAF image datatype

errchk	amovl, achtrl, achtdl, achtxl

begin
	switch (datatype) {
	case TY_SHORT, TY_INT, TY_LONG, TY_USHORT:
	    call amovl (Meml[buf], outbuffer, npix)
	case TY_REAL:
	    call achtrl (Memr[buf], outbuffer, npix)
	case TY_DOUBLE:
	    call achtdl (Memd[buf], outbuffer, npix)
	case TY_COMPLEX:
	    call achtxl (Memx[buf], outbuffer, npix)
	default:
	    call error (13, "WFT_LONG_LINE: Unknown IRAF data type.")
	}
end


# ALTARL -- Procedure to linearly scale a real vector into a long vector
# using double precision constants to preserve precision.

procedure altarl (a, b, npix, k1, k2)

real	a[ARB]		# input vector
long	b[ARB]		# output vector
int	npix		# number of pixels
double	k1, k2		# scaling factors

int	i

begin
	do i = 1, npix
	    b[i] = (a[i] + k1) * k2
end


# ALTADR -- Procedure to linearly scale a real vector in double precision

procedure altadr (a, b, npix, k1, k2)

real	a[ARB]		# input vector
real	b[ARB]		# output vector
int	npix		# number of pixels
double	k1, k2		# scaling factors

int	i

begin
	do i = 1, npix
	    b[i] = (a[i] + k1) * k2
end


# ALTADX -- Procedure to linearly scale a complex vector in double precision

procedure altadx (a, b, npix, k1, k2)

complex	a[ARB]		# input vector
complex	b[ARB]		# output vector
int	npix		# number of pixels
double	k1, k2		# scaling factors

int	i

begin
	do i = 1, npix
	    b[i] = (a[i] + k1) * k2
end