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

include <error.h>
include <fset.h>
include "rfits.h"

define	NTYPES		7		# the number of image data types

# RFITS -- Read FITS format data.  Further documentation given in rfits.hlp

procedure t_rfits()

int	inlist, outlist, len_inlist, len_outlist
int	file_number, offset, stat, first_file, last_file
pointer	sp, infile, file_list, outfile, ext_list, in_fname, out_fname
pointer	pl, axes

bool	clgetb(), pl_linenotempty()
#char	clgetc()
int	rft_get_image_type(), clgeti(), mtfile(), strlen(), btoi(), fntlenb()
int	rft_read_fitz(), fntgfnb(), fstati(), mtneedfileno(), fntrfnb()
pointer	fntopnb(), rft_flist()
real	clgetr(), rft_fe()

include	"rfits.com"

begin
	# Set up the standard output to flush on a newline.
	if (fstati (STDOUT, F_REDIR) == NO)
	    call fseti (STDOUT, F_FLUSHNL, YES)

	# Allocate working space.
	call smark (sp)
	call salloc (infile, SZ_FNAME, TY_CHAR)
	call salloc (file_list, SZ_LINE, TY_CHAR)
	call salloc (outfile, SZ_FNAME, TY_CHAR)
	call salloc (ext_list, SZ_LINE, TY_CHAR)
	call salloc (in_fname, SZ_FNAME, TY_CHAR)
	call salloc (out_fname, SZ_FNAME, TY_CHAR)
	call salloc (axes, 2, TY_INT)

	# Get RFITS parameters.
	call clgstr ("fits_file", Memc[infile], SZ_FNAME)
	long_header = btoi (clgetb ("long_header"))
	short_header = btoi (clgetb ("short_header"))
	len_record = FITS_RECORD
	old_name = btoi (clgetb ("oldirafname"))
	make_image = btoi (clgetb ("make_image"))

	# Open the input file list.
	call clgstr ("file_list", Memc[ext_list], SZ_LINE)
	if (mtfile (Memc[infile]) == YES) {
	    inlist = NULL
	    if (mtneedfileno (Memc[infile]) == YES) {
	        call strcpy (Memc[ext_list], Memc[file_list], SZ_LINE)
	    } else {
		call sprintf (Memc[file_list], SZ_LINE, "1[%s]")
		    call pargstr (Memc[ext_list])
	    }
	} else {
	    inlist = fntopnb (Memc[infile], NO)
	    len_inlist = fntlenb (inlist)
	    if (len_inlist > 0) {
		if (Memc[ext_list] == EOS) {
	            call sprintf  (Memc[file_list], SZ_LINE, "1-%d[0]")
		        call pargi (len_inlist)
		        #call pargstr (Memc[ext_list])
		} else {
	            call sprintf  (Memc[file_list], SZ_LINE, "1-%d[%s]")
		        call pargi (len_inlist)
		        call pargstr (Memc[ext_list])
		}
	    } else {
	        call sprintf  (Memc[file_list], SZ_LINE, "0[%s]")
		    call pargstr (Memc[ext_list])
	    }
	}

	# Decode the ranges string.
	pl = rft_flist (Memc[file_list], first_file, last_file, len_inlist) 
	if (pl == NULL || len_inlist <= 0)
	    call error (1, "T_RFITS: Illegal file/extensions number list")

	# Open the output file list.
	if (make_image == YES) {
	    call clgstr ("iraf_file", Memc[outfile], SZ_FNAME)
	    if (Memc[outfile] == EOS) {
		if (old_name == YES)
		    call mktemp ("tmp$", Memc[outfile], SZ_FNAME) 
		else
		    call error (0, "T_RFITS: Undefined output file name")
	    }
	    outlist = fntopnb (Memc[outfile], NO)
	    len_outlist = fntlenb (outlist)
	    offset = clgeti ("offset")
	} else {
	    Memc[outfile] = EOS
	    outlist = NULL
	    len_outlist = 1
	}
	if ((len_outlist > 1) && (len_outlist != len_inlist))
	    call error (0,
	        "T_RFITS: Output and input lists have different lengths")

	# Get the remaining parameters. Use the string in_fname as a
	# temporary variable.
	#data_type = rft_get_image_type (clgetc ("datatype"))
	call clgstr ("datatype", Memc[in_fname], SZ_FNAME)
	data_type = rft_get_image_type (Memc[in_fname])
	scale = btoi (clgetb ("scale"))
	blank = clgetr ("blank")

	# Get the scan size parameter.
	fe = rft_fe (Memc[infile])

	# Read successive FITS files, convert and write into a numbered
	# succession of output IRAF files.

	do file_number = first_file, last_file {

	    # Get the next file number.
	    Memi[axes] = 1
	    Memi[axes+1] = file_number
	    if (! pl_linenotempty (pl, Memi[axes]))
		next

	    # Get the input file name.
	    if (inlist != NULL) {
		if (fntgfnb (inlist, Memc[in_fname], SZ_FNAME) == EOF)
		    call error (0, "T_RFITS: Error reading input file name")
	    } else {
		if (mtneedfileno (Memc[infile]) == YES)
		    call mtfname (Memc[infile], file_number, Memc[in_fname],
		        SZ_FNAME)
		else
	            call strcpy (Memc[infile], Memc[in_fname], SZ_FNAME)
	    }

	    # Get the output file name.
	    if (outlist == NULL) {
		Memc[out_fname] = EOS
	    } else if (len_inlist > len_outlist) {
		if (fntrfnb (outlist, 1, Memc[out_fname], SZ_FNAME) == EOF)
	            call strcpy (Memc[outfile], Memc[out_fname], SZ_FNAME)
	        if (len_inlist > 1) {
		    call sprintf (Memc[out_fname+strlen(Memc[out_fname])],
		        SZ_FNAME, "%04d")
		        call pargi (file_number + offset)
	        }
	    } else if (fntgfnb (outlist, Memc[out_fname], SZ_FNAME) == EOF)
		call error (0, "T_RFITS: Error reading output file name")

	    # Convert FITS file to the output IRAF file. If EOT is reached
	    # then exit. If an error is detected then print a warning and
	    # continue with the next file.

	    iferr (stat = rft_read_fitz (Memc[in_fname], Memc[out_fname],
	        pl, file_number))
		call erract (EA_FATAL)
	    if (stat == EOF)
		break
	}

	if (inlist != NULL)
	    call fntclsb (inlist) 
	if (outlist != NULL)
	    call fntclsb (outlist) 
	if (pl != NULL)
	    call pl_close (pl)

	call sfree (sp)
end


# RFT_GET_IMAGE_TYPE -- Convert a character to and IRAF image type.

int procedure rft_get_image_type (c)

char	c

int	type_codes[NTYPES], i
string	types "usilrdx"	
int	stridx()
data	type_codes /TY_USHORT, TY_SHORT, TY_INT, TY_LONG, TY_REAL,
		    TY_DOUBLE, TY_COMPLEX/
begin
	i = stridx (c, types)
	if (i == 0)
	    return (ERR)
	else
	    return (type_codes[stridx(c,types)])
end


# RFT_FE -- Fetch the maximum file size in MB for tape scanning mode.

real procedure rft_fe (file)

char	file[ARB]		# the input file name

pointer	gty
real	fe
int	mtfile(), gtygeti()
pointer	mtcap()
errchk	gtygeti()

begin
	if (mtfile (file) == NO)
	    return (0.0)
	iferr (gty = mtcap (file))
	    return (0.0)
	iferr (fe = gtygeti (gty, "fe"))
	    fe = 0.0
	call gtyclose (gty)
	return (fe)
end