aboutsummaryrefslogtreecommitdiff
path: root/noao/twodspec/multispec/t_msset.x
blob: 81d94f0cf3fae1f4d187f8a96211a8d3a1d87192 (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
include "ms.h"

# T_MS_SET -- Set profile parameters in database.

procedure t_ms_set ()

char	image[SZ_FNAME]
char	keyword[SZ_LINE]

char	comment[SZ_LINE]
int	i, nspectra, ms_id
pointer	ms

bool	streq(), clgetb()
int	clscan(), nscan(), ms_db_id()
pointer	msmap()

begin
	# Get the task parameters and access the database.
	call clgstr ("image", image, SZ_FNAME)
	ms = msmap (image, READ_WRITE, 0)
	call clgstr ("keyword", keyword, SZ_LINE)

	# Decode the keyword for the desired database quantity.
	if (streq (keyword, "nspectra")) {
	    # Set the value of MS_NSPECTRA in the MULTISPEC header record.
	    if (clgetb ("read_list"))
		i = clscan ("list")
	    else
	        i = clscan ("value") 
	    call gargi (nspectra)
	    if (nscan () != 1)
		call error (MS_ERROR, "Bad parameter value")

	    # It is an error to attempt to change the value previously set.
	    if (MS_NSPECTRA(ms) == 0)
	        MS_NSPECTRA(ms) = nspectra
	    else if (MS_NSPECTRA(ms) != nspectra)
	        call error (MS_ERROR, "Attempt to change number of spectra")
	} else {
	    # Keyword is one of the database record names.  Convert to
	    # a MULTISPEC parameter ID and call the appropriate procedure.

	    ms_id = ms_db_id (ms, keyword)
	    switch (ms_id) {
	    case COMMENTS:
	        call com_set (ms, comment)
	    case I0, X0, S0, S1, S2:
	        call par_set (ms, ms_id, comment)
	    }
	}

	# Finish up.
	call msphdr (ms)
	call msunmap (ms)
end


# COM_SET -- Add a comment to the MULTISPEC database comment block.
#
# This procedure does CLIO.

procedure com_set (ms, comment)

pointer	ms				# MULTISPEC data structure
char	comment[SZ_LINE]		# Input comment buffer.

int	i

bool	clgetb()
int	clscan()

begin
	# Desire whether to use list input or CL parameter input.
	if (clgetb ("read_list")) {
	    # Read a list of comment strings.
	    while (clscan ("list") != EOF) {
	        call gargstr (comment, SZ_LINE)
	        call history (ms, comment)
	    }
	} else {
	    # Read a comment line from the parameter "value".
	    i = clscan ("value")
	    call gargstr (comment, SZ_LINE)
	    call history (ms, comment)
	}
end


# PAR_SET -- Set the values of the model parameters.
#
# This procedure does CLIO.

procedure par_set (ms, ms_id, comment)

pointer	ms			# MULTISPEC data structure
int	ms_id			# MULTISPEC ID
char	comment[SZ_LINE]	# Comment buffer

int	i, line, nsamples, sample, last_sample, spectrum
int	lines[3, MAX_RANGES], spectra[3, MAX_RANGES]
real	value
pointer	sp, samples

int	clscan(), nscan(), clgranges(), get_next_number()
int	get_sample_line(), get_sample_lines()
bool	dbaccess(), clgetb()

begin
	if ((MS_NSAMPLES(ms) == 0) || (MS_NSPECTRA(ms) == 0))
	    return

	# Enter the parameter in the database if necessary.
	if (!dbaccess (MS_DB(ms), NAME(ms, ms_id)))
	    call dbenter (MS_DB(ms), NAME(ms, ms_id),
		MS_NSPECTRA(ms) * SZ_REAL, MS_NSAMPLES(ms))

	# Determine input source.
	if (clgetb ("read_list")) {
	    # Read values from a list.
	    last_sample = 0
	    while (clscan ("list") != EOF) {

		# Get line, spectrum, and value from the list.
	        call gargi (line)
	        call gargi (spectrum)
	        call gargr (value)

		# Check that the data is valid otherwise go to next input.
	        if (nscan () != 3)
		    next
	        if ((spectrum < 1) || (spectrum > MS_NSPECTRA(ms)))
		    next

		# If the last sample is not the same as the previous sample
		# flush the last parameter values if the last sample is not
		# zero and get the next line of parameter values.

		sample = get_sample_line (ms, line)
	        if (sample != last_sample) {
		    if (last_sample != 0)
		        call mspparam (ms, ms_id, last_sample)
	            call msgparam (ms, ms_id, sample)
		    last_sample = sample
	        }

		# Set the parameter value.
	        PARAMETER(ms, ms_id, spectrum) = value
	    }

	    # Flush the last line of parameter values.
	    call mspparam (ms, ms_id, last_sample)

	} else {
	    # Set the parameter values for the selected lines and spectra
	    # to the CL parameter "value".

	    i = clgranges ("lines", 1, MS_LEN(ms, 2), lines, MAX_RANGES)
	    i = clgranges ("spectra", 1, MS_NSPECTRA(ms), spectra, MAX_RANGES)
	    i = clscan ("value")

	    # Convert the image lines to sample lines.
	    call smark (sp)
	    call salloc (samples, MS_NSAMPLES(ms), TY_INT)
	    nsamples = get_sample_lines (ms, lines, Memi[samples])

	    # Check that the parameter value is a real number.
	    call gargr (value)
	    if (nscan () != 1)
		call error (MS_ERROR, "Bad parameter value")

	    # Go through the selected sample lines and spectra setting the
	    # parameter value.

	    do i = 1, nsamples {
		sample = Memi[samples + i - 1]
		call msgparam (ms, ms_id, sample)
		spectrum = 0
		while (get_next_number (spectra, spectrum) != EOF)
		    PARAMETER (ms, ms_id, spectrum) = value
		call mspparam (ms, ms_id, sample)
	    }
	}
		
	# Add a history comment.
	call sprintf (comment, SZ_LINE, "Values of parameter %s set.")
	    call pargstr (NAME(ms, ms_id))
	call history (ms, comment)
end