aboutsummaryrefslogtreecommitdiff
path: root/pkg/dataio/fits/fits_files.x
blob: ce2c553c228d5856ad01eb3b19c0636522ae5f21 (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
include <ctype.h>
include <plset.h>

define	DEF_MAXNCOLS	1000
define	DEF_MAXNLINES	4096
define	MAX_NRANGES	100


# RFT_FLIST -- Decode a list of files and associated extensions into a pixel
# list.

pointer procedure rft_flist (file_list, first_file, last_file, nfiles) 

char	file_list[ARB]		# the input file list string
int	first_file		# the first file in the list
int	last_file		# the last file in the list
int	nfiles			# the number of files in the list

int	i, j, maxncols, maxnlines, nrfiles, rp, rbegin, rend, rstep
int	last_ext, ebegin, eend, estep, ep, nefiles
pointer	sp, extensions, str, axes, pl

bool	pl_linenotempty()
int	rft_gfranges()
pointer	pl_create()

begin
	# Allocate some working space.
	call smark (sp)
	call salloc (extensions, SZ_LINE, TY_CHAR)
	call salloc (str, SZ_LINE, TY_CHAR)
	call salloc (axes, 2, TY_INT)

	# Initialize the file list.
	pl = NULL
	maxncols = DEF_MAXNCOLS
	maxnlines = DEF_MAXNLINES

	repeat {

	    # Initialize the file counter parameters.
	    first_file = INDEFI
	    last_file = INDEFI
	    nfiles = 0
	    rp = 1

	    # Open the file list.
	    Memi[axes] = maxncols
	    Memi[axes+1] = maxnlines
	    pl = pl_create (2, Memi[axes], 1)

	    # Decode the file list.
	    nrfiles = rft_gfranges (file_list, rp, YES, maxnlines, rbegin,
	        rend, rstep, NO, Memc[extensions])
	    while (nrfiles > 0) {

		# Check the file number limits and terminate the loop if
		# the current list size is exceeded.
		if (IS_INDEFI(first_file))
		    first_file = rbegin
		else
		    first_file = min (rbegin, first_file)
		if (IS_INDEFI(last_file))
		    last_file = rend
		else
		    last_file = max (last_file, rend)
		if (last_file > maxnlines)
		    break

		# Initialize the extensions list decoding.
		ep = 1
		last_ext = INDEFI

		# Decode the associated extensions files. If the extensions
		# list is empty
		nefiles = rft_gfranges (Memc[extensions], ep, YES, maxncols,
		    ebegin, eend, estep, YES, Memc[str])
		while (nefiles > 0) {

		    # Check the  extensions number limits and quit if they
		    # are exceeded.
		    if (IS_INDEFI(last_ext))
		        last_ext = eend
		    else
		        last_ext = max (last_ext, eend)
		    if (last_ext > maxncols)
		        break

		    # Set the appropriate elements in the list.
		    if (rstep == 1) {
			if (estep == 1)
		    	    call pl_box (pl, ebegin, rbegin, eend,
			        rend, PIX_SET + PIX_VALUE(1))
			else {
			    do i = ebegin, eend, estep
			        call pl_box (pl, i, rbegin, i, rend, 
			            PIX_SET + PIX_VALUE(1))
			}
		    } else {
		        do i = rbegin, rend, rstep {
			    do j = ebegin, eend, estep
		                call pl_point (pl, j, i, PIX_SET +
			            PIX_VALUE(1))
			}
		    }

		    nefiles = rft_gfranges (Memc[extensions], ep, NO, maxncols,
		        ebegin, eend, estep, YES, Memc[str])
		}

		# Break if an extensions list decode error occurs.
		if (nefiles == 0)
		    break

	        nrfiles = rft_gfranges (file_list, rp, NO, maxnlines, rbegin,
		    rend, rstep, NO, Memc[extensions])
	    }

	    # Break if a file or extensions list decode error ocurred.
	    if (nrfiles == 0 || nefiles == 0)
		break

	    # If the file or extensions list is larger than the current maximum
	    # then free the list increase the default space and repeat the
	    # procedure.

	    if (!IS_INDEFI(last_file)) {
	        if (last_file > maxnlines) {
		    if (pl != NULL)
		        call pl_close (pl)
		    pl = NULL
		    maxnlines = maxnlines + DEF_MAXNLINES
	        } else {
		    do i = first_file, last_file {
		        Memi[axes] = 1
		        Memi[axes+1] = i
		        if (pl_linenotempty (pl, Memi[axes]))
			    nfiles = nfiles + 1
		    }
		}
	    }

	    if (!IS_INDEFI(last_ext)) {
		if (last_ext > maxncols) {
		    if (pl != NULL)
		        call pl_close (pl)
		    pl = NULL
		    maxncols = maxncols + DEF_MAXNCOLS
		}
	    }

	} until (pl != NULL)

	# Free space.
	call sfree (sp)

	# If the file list is empty or a decode error occurred return
	# a NULL list, otherwise return the pointer to the list.

	if (nfiles <= 0 || nrfiles == 0 || nefiles == 0) {
	    nfiles = 0
	    first_file = INDEFI
	    last_file = INDEFI
	    call pl_close (pl)
	    return (NULL)
	} else
	    return (pl)
end


# RFT_GFRANGES -- Parse a string containing a list of integer numbers or
# ranges, delimited by either spaces or commas.  Each range may have an
# associated extensions lists delimited by square brackets. Return as output
# each range in sequence. Range limits must be positive nonnegative integers.
# EOF is returned if the  end of the file_list is reached.  0 is returned if a
# conversion error takes place. Otherwise the number of elements in the
# range is returned.

int procedure rft_gfranges (range_string, ip, firstr, rmax, rbegin, rend,
	rstep, zeroindex, extensions)

char    range_string[ARB]       # range string to be decoded
int	ip			# the range string pointer
int	firstr			# first range to be returned
int	rmax			# the maximum file number
int	rbegin			# the begining of the range			
int	rend			# the end of the range
int	rstep			# the range step size
int	zeroindex		# allow zero indexing ?
char	extensions[ARB]		# the output extensions string

int	ep, itemp
int	ctoi()

begin
	# Initialize.
	if (zeroindex == YES) {
	    rbegin = 0
            rend = rmax - 1
	} else {
	    rbegin = 1
            rend = rmax
	}
        rstep = 1
	extensions[1] = EOS

	# Return default range if the range string is NULL.
	if (range_string[ip] == EOS) {
	    rbegin = 1
	    rend = rmax
	    if (firstr == YES)
	        return (rend)
	    else
	        return (EOF)
	}

        # Skip delimiters
        while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
            ip = ip + 1

        # Get first limit.
        # Must be a number, '-', 'x', *, or EOS.
        if (range_string[ip] == EOS) {                      # end of list
	    rbegin = 1
	    rend = rmax
	    if (firstr == YES)
	        return (rend)
	    else
		return (EOF)
	} else if (range_string[ip] == '*') {
	    ;
        } else if (range_string[ip] == '-') {
            ;
        } else if (range_string[ip] == 'x') {
            ;
        } else if (IS_DIGIT(range_string[ip])) {              # ,n..
            if (ctoi (range_string, ip, rbegin) == 0)
                return (0)
	    else if (zeroindex == NO) {
	        if (rbegin <= 0)
		    return (0)
	    } else {
	        if (rbegin < 0)
		    return (0)
	    }
        } else
            return (0)

	# Extract extensions file list.
	if (range_string[ip] == '[') {
	    ip = ip + 1
	    ep = 1
	    while (range_string[ip] != EOS) {
		if (range_string[ip] == ']') {
		    ip = ip + 1
		    break
		}
		extensions[ep] = range_string[ip]
		ip = ip + 1
		ep = ep + 1
	    }
	    extensions[ep] = EOS
	    if (range_string[ip-1] != ']')
		return (0)
	}

        # Skip delimiters
        while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
            ip = ip + 1

        # Get last limit
        # Must be '-', 'x', or '*' otherwise last = first.
	if (range_string[ip] == '*')
	    ;
        else if (range_string[ip] == 'x')
            ;
        else if (range_string[ip] == '-') {
            ip = ip + 1
            while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
                ip = ip + 1
            if (range_string[ip] == EOS)
                ;
            else if (IS_DIGIT(range_string[ip])) {
                if (ctoi (range_string, ip, rend) == 0)
                    return (0)
		else if (zeroindex == NO) {
		    if (rend <= 0)
		        return (0)
		} else {
		    if (rend < 0)
		        return (0)
		}
            } else if (range_string[ip] == 'x')
                    ;
            else
                return (0)
        } else
                rend = rbegin

	# Skip extensions files for now.
	if (range_string[ip] == '[') {
	    ip = ip + 1
	    ep = 1
	    while (range_string[ip] != EOS) {
		if (range_string[ip] == ']') {
		    ip = ip + 1
		    break
		}
		extensions[ep] = range_string[ip]
		ip = ip + 1
		ep = ep + 1
	    }
	    extensions[ep] = EOS
	    if (range_string[ip-1] != ']')
	        return (0)
	}

        # Skip delimiters
        while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
            ip = ip + 1

        # Get step.
        # Must be 'x' or assume default step.
        if (range_string[ip] == '*')
	    ip = ip + 1
        else if (range_string[ip] == 'x') {
            ip = ip + 1
            while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
                ip = ip + 1
            if (range_string[ip] == EOS)
                ;
            else if (IS_DIGIT(range_string[ip])) {
                if (ctoi (range_string, ip, rstep) == 0)
                    ;
	        else if (rstep <= 0)
		    return (0)
            } else if (range_string[ip] == '-')
                ;
            else
                return (0)
        }

	# Skip extensions files for now.
	if (range_string[ip] == '[') {
	    ip = ip + 1
	    ep = 1
	    while (range_string[ip] != EOS) {
		if (range_string[ip] == ']') {
		    ip = ip + 1
		    break
		}
		extensions[ep] = range_string[ip]
		ip = ip + 1
		ep = ep + 1
	    }
	    extensions[ep] = EOS
	    if (range_string[ip-1] != ']')
	        return (0)
	}

        # Output the range triple.
	if (rend < rbegin) {
	    itemp = rbegin
	    rbegin = rend
	    rend = itemp
	}
	if (zeroindex == YES) {
	    rbegin = rbegin + 1
	    rend = rend + 1
	}
        return (abs (rend - rbegin) / rstep + 1 )
end