aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/vtel/readvt.x
blob: 27e34be3df3742c500eceef587a8384ff00eac5e (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
include <mach.h>
include	<imhdr.h>
include	<fset.h>
include	"vt.h"

define	MAX_RANGES	100
define	VT_TBUF		15000

# READVT -- Read data from tape or disk and format the data into an IRAF image.
# Display header information to the user as a check if the 'verbose' flag is
# set. 

procedure t_readvt()

pointer	infile			# pointer to input filename(s)
pointer	outfile			# pointer to output filename(s)
bool	verbose			# verbose flag
bool	headeronly		# if set, just print the header
bool	robust			# if set, ignore wrong observation type
pointer	files			# file list for multiple tape files

int	listin			# list of input images
int	listout			# list of output images
bool	selfbuf, rootflag
int	nfiles, filenumber, stat
pointer	bp, sp, tapename, dfilename, diskfile, root
int	filerange[2 * MAX_RANGES + 1]

bool	clgetb()
int	get_next_number(), mtneedfileno()
int	strlen(), decode_ranges()
int	fntopnb(), imtopenp(), clgfil(), imtgetim(), clplen(), imtlen()
int	mtfile()
errchk	vt_rfd

begin
	call smark (sp)
	call salloc (infile, SZ_LINE, TY_CHAR)
	call salloc (outfile, SZ_LINE, TY_CHAR)
	call salloc (tapename, 2*SZ_LINE, TY_CHAR)
	call salloc (dfilename, 2*SZ_LINE, TY_CHAR)
	call salloc (diskfile, SZ_LINE, TY_CHAR)
	call salloc (root, SZ_LINE, TY_CHAR)
	call salloc (files, SZ_LINE, TY_CHAR)

	call fseti (STDOUT, F_FLUSHNL, YES)

	# Get parameters from the CL.
	verbose = clgetb ("verbose")
	headeronly = clgetb ("headeronly")
	robust = clgetb ("robust")

        call clgstr ("infile", Memc[infile], SZ_FNAME)

	# Set up the buffer structure, we may need it.
	call salloc (bp, VT_LENBSTRUCT, TY_STRUCT)
	call salloc (VT_BUFP(bp), VT_TBUF, TY_SHORT)
	VT_BP(bp) = VT_BUFP(bp)
	VT_BUFBOT(bp) = VT_BUFP(bp)

        if (mtfile (Memc[infile]) == NO) {
	    # This is not a tape file, expand as a list template.
	    listin = fntopnb (Memc[infile], 0)
	    rootflag = FALSE
	    filenumber = 1
	    if (!headeronly) {
	        listout = imtopenp ("outfile")

		# Compare the lengths of the two lists.  If equal, proceed,
		# otherwise if the outlist is of length one, use it as a root
		# name, otherwise error.
		
		if (imtlen (listout) == 1) {
		    rootflag = TRUE
		    stat = imtgetim (listout, Memc[root], SZ_FNAME)
		} else if (clplen (listin) != imtlen (listout)) {
		    call clpcls (listin)
	    	    call imtclose (listout)
	    	    call error (1, "Wrong number of elements in operand lists")
		}
	    }

	    while (clgfil (listin, Memc[diskfile], SZ_FNAME) != EOF) {
	        if (!headeronly) {
		    if (!rootflag)
	                stat = imtgetim (listout, Memc[dfilename], SZ_FNAME)
		    else {
			# Assemble an output filename from the root name.
			call sprintf (Memc[dfilename], SZ_FNAME, "%s")
			    call pargstr (Memc[root])
	                call sprintf (Memc[dfilename+strlen(Memc[root])],
		            SZ_FNAME, "%03d")
		            call pargi (filenumber)
			filenumber = filenumber + 1
		    }
		}

		# Of course, if the user is reading from disk, we can't
		# check record sizes.

		selfbuf = false
		iferr (call vt_rfd (diskfile, dfilename,
			selfbuf, verbose, headeronly, robust, bp)) {
		    call eprintf ("Error reading file %s\n")
		    	call pargstr (Memc[infile])
	    	}
	    }
	    call clpcls (listin)
	    if (!headeronly)
	        call imtclose (listout)

	} else if (mtneedfileno(Memc[infile]) == NO) {

	    # This is a tape file and the user specified which file.
	    if (!headeronly)
	        call clgstr ("outfile", Memc[outfile], SZ_FNAME)
	    selfbuf = true
	    iferr (call vt_rfd (infile, outfile, selfbuf, verbose,
		headeronly, robust, bp)) {
		call eprintf ("Error reading file %s\n")
		    call pargstr (Memc[infile])
	    }

        } else {

	    # This is a tape file and the user did not specify which file.
	    call clgstr ("files", Memc[files], SZ_LINE)
	    if (!headeronly)
	        call clgstr ("outfile", Memc[outfile], SZ_FNAME)

	    # Set up the file names, then do the read.
            if (decode_ranges (Memc[files], filerange, MAX_RANGES,
		nfiles) == ERR)
	        call error (0, "Illegal file number list.")

            while (get_next_number (filerange, filenumber) != EOF) {
	        # Assemble the appropriate tape file name.
		call mtfname (Memc[infile], filenumber, Memc[tapename],
		    SZ_FNAME)

	        # Assemble the appropriate disk file name.
	        if (!headeronly) {
	            call strcpy (Memc[outfile], Memc[dfilename], SZ_FNAME)
	            call sprintf (Memc[dfilename+strlen(Memc[outfile])],
		        SZ_FNAME, "%03d")
		        call pargi (filenumber)
		}

	        selfbuf = TRUE
	        iferr (call vt_rfd (tapename, dfilename, selfbuf,
			verbose, headeronly, robust, bp)) {
		    call eprintf ("Error reading file %s\n")
		        call pargstr (Memc[infile])
	        }
	    }
	}

	call sfree (sp)
end


# VT_RFD -- Do the actual read of a full disk gram.

procedure vt_rfd (in, out, selfbuf, verbose, headeronly, robust, bp)

pointer	in		# input file
pointer	out		# output file
bool	selfbuf		# do input buffering and correct for bad record lengths
bool	verbose		# verbose flag
bool	headeronly	# if set, just print the header
bool	robust		# if set, ignore wrong observation type

short	one
int	date, numchars
int	subraster, x1, y1, inputfd
pointer	table, bp, im, srp, hs, sp, hbuf
pointer	immap(), imps2s()
int	mtopen(), readheader()
errchk	readheader, loadsubswath, immap, imps2s
define	exit_	10

begin
	call smark (sp)
	call salloc (hbuf, SZ_VTHDR, TY_SHORT)
	call salloc (table, SZ_TABLE, TY_SHORT)
	call salloc (hs, VT_LENHSTRUCT, TY_STRUCT)

	if (verbose) {
	    call printf ("\nfile %s  ")
		call pargstr (Memc[in])
	}

	# Open input file.
	inputfd = mtopen (Memc[in], READ_ONLY, 0)

	# Read header.
	iferr (numchars = readheader (inputfd, hbuf, selfbuf))
	    call error (0, "Error reading header information.")
	call decodeheader (hbuf, hs, verbose)
	if (verbose)
	    call printf ("\n")

	# Check the observation type in the header.  If this value is not
	# zero (full disk) then write an error message, if the robust flag
	# is set go ahead and read the file.

	if (!robust) {
	    if (VT_HOBSTYPE[hs] != 0) {
	        call printf ("file %s is not a type zero scan (full disk)\n")
		    call pargstr (Memc[in])
	        call printf ("Use 'mscan' to read this type %d area scan\n")
		    call pargi (VT_HOBSTYPE[hs])
	        goto exit_      # close input file and exit
	    }
	} else {
	    if (VT_HOBSTYPE[hs] != 0) {
		call printf ("The header for file %s contains 'observation ")
		    call pargstr (Memc[in])
		call printf ("type = %d'\n")
		    call pargi (VT_HOBSTYPE[hs])
		call printf ("READVT expects the observation type ")
		call printf ("to be zero.\n")
		call printf ("This error will be ignored since the 'robust'")
		call printf (" flag is set\n")
	    }
	}

	if (headeronly)
	    goto exit_      # close input file and exit

	if (verbose) {
	    call printf ("\nwriting %s\n")
		call pargstr (Memc[out])
	}

	# Open the output image. Set it up.
	im  = immap (Memc[out], NEW_IMAGE, 0)
	IM_NDIM(im) = 2
	IM_LEN(im,1) = DIM_VTFD
	IM_LEN(im,2) = DIM_VTFD
	IM_PIXTYPE(im) = TY_SHORT

	# Set up the 8 header fields we need and store the information we
	# obtained from the raw data image header.

	call imaddi (im, "obs_time", VT_HTIME[hs])
	date = VT_HMONTH[hs] * 10000 + VT_HDAY[hs] * 100 + VT_HYEAR[hs]

	call imaddi (im, "obs_date", date )
	call imaddi (im, "wv_lngth", VT_HWVLNGTH[hs])
	call imaddi (im, "obs_type", VT_HOBSTYPE[hs])
	call imaddi (im, "av_intns", VT_HAVINTENS[hs])
	call imaddi (im, "num_cols", VT_HNUMCOLS[hs])
	call imaddi (im, "intg/pix", VT_HINTGPIX[hs])
	call imaddi (im, "rep_time", VT_HREPTIME[hs])

	# Set up lookuptable.
	one = 1
	call amovks (one, Mems[table], SZ_TABLE)
	call aclrs (Mems[table], HALF_DIF)
	call aclrs (Mems[table + SWTHWID_14 + HALF_DIF], HALF_DIF)
	call aclrs (Mems[table + SWTHWID_23 * 3], HALF_DIF)
	call aclrs (Mems[table + SZ_TABLE - HALF_DIF], HALF_DIF)

	# Now, fill the image with data.
	do subraster = 1, NUM_SRSTR {

	    # Calculate position of bottom left corner of this subraster
	    x1 = ((NUM_SRSTR_X - 1) - mod((subraster - 1), NUM_SRSTR_X)) *
		SRSTR_WID + 1
	    y1 = ((NUM_SRSTR_Y - 1) - ((subraster - mod((subraster - 1),
		NUM_SRSTR_Y)) / NUM_SRSTR_Y)) * SWTH_HIGH + 1

	    # Get subraster.
	    srp = imps2s (im, x1, x1+(SRSTR_WID - 1), y1, y1+(SWTH_HIGH - 1))

	    # Load the subraster with data.
	    iferr (call loadsubraster (inputfd, Mems[srp], SRSTR_WID, SWTH_HIGH,
		Mems[table], subraster, selfbuf, bp)) {
		call eprintf ("Error in loadsubraster, subraster = %d\n")
		    call pargi (subraster)
		break
	    }

	    if (verbose) {
		call printf("%d%% ")
		    call pargi ((subraster*100)/NUM_SRSTR)
		call flush (STDOUT)
	    }
	}

	if (verbose)
	    call printf ("\n")

	# Unmap image and close input file.
	call imunmap (im)
exit_
	call sfree (sp)
	call close (inputfd)
end


# LOADSUBRASTER -- Get data from the input and load it into this
# subraster, look in the table to see if each subswath should be
# filled with data or zeros.

procedure loadsubraster (inputfd, array, nx, ny, table, subraster, selfbuf, bp)

int	inputfd			# input file we are reading from
short	array[nx, ny]		# array to put the data in
int	nx			# x length of the array
int	ny			# y length of the array
short	table[SZ_TABLE]		# lookup table for data
int	subraster		# subraster number are we loading
bool	selfbuf			# buffering and record length checking?
pointer	bp			# pointer to buffer pointer structure

pointer	sp, bufpointer
int	i, subswath, tableindex
errchk	readsubswath

begin
	call smark (sp)
	call salloc (bufpointer, ny, TY_SHORT)

	for (subswath = nx;  subswath >= 1;  subswath = subswath - 1) {
	    tableindex = (subraster - 1) * nx + ((nx + 1) - subswath)

	    if (table[tableindex] == IS_DATA) {
		iferr (call readsubswath (inputfd, selfbuf, Mems[bufpointer],
			ny, bp)) {

		    call eprintf ("Error in readsubswath, subswath = %d\n")
			call pargi (subswath)
		}

		do i = ny, 1, -1
		    array[subswath,i] = Mems[bufpointer + ny - i]

	    } else {
		do i = 1, ny 
		    array[subswath,i] = 0
	    }
	}

	call sfree (sp)
end