aboutsummaryrefslogtreecommitdiff
path: root/noao/astutil/pdm/pdmgdata.x
blob: 79e8d49f736119747e911f161d4b74f9f3a33826 (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
include <mach.h>
include <ctype.h>
include <error.h>
include "pdm.h"

define	SZ_BUF	100

# PDM_GDATA -- Get Data from the input files.

int procedure pdm_gdata (pdmp, infile)

pointer	pdmp			# pointer to PDM data structure
char	infile[SZ_LINE]		# input data file name

int	fntopnb(), list, clgfil()
int	n, ncols, lineno, buflen
int	open(), getline(), nscan()
int	fd
pointer	nextfile
pointer	lbuf, ip, sp
errchk	realloc, fntopnb, open

begin
	# Get a line buffer.
	call smark (sp)
	call salloc (lbuf, SZ_LINE, TY_CHAR)
	call salloc (nextfile, SZ_LINE, TY_CHAR)

	# Open the input file as a list of files.
	list = fntopnb (infile, 0)

	# Initialize some variables.
	n = 0
	ncols = 0
	lineno = 0

	# For each input file in the list, read the data.
	while (clgfil (list, Memc[nextfile], SZ_FNAME) != EOF) {

	    # Open this input file.
	    fd = open (Memc[nextfile], READ_ONLY, TEXT_FILE)

	    # Read in the data from this file.
	    while (getline (fd, Memc[lbuf]) != EOF) {
	        # Skip white space and blank lines.
	        lineno = lineno + 1
	        for (ip = lbuf; IS_WHITE(Memc[ip]); ip = ip + 1)
		    ;
	        if (Memc[ip] == '\n' || Memc[ip] == EOS)
		    next

	        if (n == 0) {
		    buflen = SZ_BUF
		    iferr {
		        call calloc (PDM_XP(pdmp), buflen, TY_DOUBLE)
		        call calloc (PDM_DYP(pdmp), buflen, TY_DOUBLE)
		        call calloc (PDM_ODYP(pdmp), buflen, TY_DOUBLE)
		        call calloc (PDM_INUSEP(pdmp), buflen, TY_INT)
		        call calloc (PDM_ERRP(pdmp), buflen, TY_REAL)
		    } then
		        call erract (EA_FATAL)
	        } else if (n + 1 > buflen) {
		    buflen = buflen + SZ_BUF
		    call realloc (PDM_XP(pdmp), buflen, TY_DOUBLE)
		    call realloc (PDM_DYP(pdmp), buflen, TY_DOUBLE)
		    call realloc (PDM_ODYP(pdmp), buflen, TY_DOUBLE)
		    call realloc (PDM_INUSEP(pdmp), buflen, TY_INT)
		    call realloc (PDM_ERRP(pdmp), buflen, TY_REAL)
	        }

		# Read data from the file, put it in the data structure.
	        call sscan (Memc[ip])
	        call gargd (PDM_X(pdmp,n+1))
	        call gargd (PDM_ODY(pdmp,n+1))
	        call gargr (PDM_ERR(pdmp,n+1))
	        PDM_INUSE(pdmp,n+1) = 1
	        PDM_DY(pdmp,n+1) = PDM_ODY(pdmp,n+1)

	        # If this is line one, then determine the number of columns.
	        if (ncols == 0 && nscan() > 0)
		    ncols = nscan()

	        # Check this line against the number of columns and do the
	        # appropriate thing.

	        switch (nscan()) {
	        case 0:
		    call printf ("no args; %s, line %d: %s\n")
		        call pargstr (Memc[nextfile])
		        call pargi (lineno)
		        call pargstr (Memc[lbuf])
		    next
	        case 1:
		    if (ncols >= 2) {
		        call eprintf ("only one arg; %s, line %d: %s\n")
		            call pargstr (Memc[nextfile])
		            call pargi (lineno)
		            call pargstr (Memc[lbuf])
		        next
		    } else {
		        PDM_ODY(pdmp,n+1) = PDM_X(pdmp,n+1)
		        PDM_DY(pdmp,n+1) = PDM_X(pdmp,n+1)
		        PDM_X(pdmp,n+1) = n + 1.0d+0
			PDM_ERR(pdmp,n+1) = 0.0
		    }
		case 2:
		    if (ncols == 3) {
			call eprintf ("only two args; %s, line %d: %s\n")
			    call pargstr (Memc[nextfile])
			    call pargi (lineno)
			    call pargstr (Memc[lbuf])
			next
		    } else {
		        PDM_ODY(pdmp,n+1) = PDM_ODY(pdmp,n+1)
		        PDM_DY(pdmp,n+1) = PDM_ODY(pdmp,n+1)
		        PDM_X(pdmp,n+1) = PDM_X(pdmp,n+1)
			PDM_ERR(pdmp,n+1) = 0.0
		    }

	        }

	        n = n + 1
	    }
	    call close (fd)
	}

	call realloc (PDM_XP(pdmp), n, TY_DOUBLE)
	call realloc (PDM_DYP(pdmp), n, TY_DOUBLE)
	call realloc (PDM_ODYP(pdmp), n, TY_DOUBLE)
	call realloc (PDM_INUSEP(pdmp), n, TY_INT)
	call realloc (PDM_ERRP(pdmp), n, TY_REAL)

	call fntclsb (list)
	call sfree (sp)
	return (n)
end