aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbfhp.x
blob: 2bd834b15554881e56d1cf3e7b12cec3f5c5b9df (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
include <ctype.h>
include <tbset.h>
include "tbtables.h"
include "tblfits.h"		# defines FITS_KEYWORD_MISSING

# These specify the precision to be used for writing floating-point keywords.
# The fact that they're negative is a flag to CFITSIO to use g format.
define	NDECIMALS_SINGLE	(-7)
define	NDECIMALS_DOUBLE	(-15)

# Put a parameter into a FITS table header.  If the keyword already
# exists, it will be updated; otherwise, it will be added.
#
# Phil Hodge,  6-Jul-1995  Subroutine created
# Phil Hodge, 20-Feb-1997  Change decimals of output in tbfhpd and tbfhpr:
#			in tbfhpd change 15 to 14; in tbfhpr change 7 to 6.
# Phil Hodge, 14-Jan-1998  Change decimals of output in tbfhpd and tbfhpr
#			for new keywords, 15 to 14 and 7 to 6 respectively.
# Phil Hodge, 20-Jul-1998  In tbfhpt, include explicit test for history,
#			comment, or blank, and use appropriate fitsio routine.
# Phil Hodge, 29-Aug-2000  Change 14 and 6 in tbfhpd and tbfhpr respectively
#			to -15 and -7, so cfitsio will use g format.
# Phil Hodge, 29-Jan-2002  In tbfhpt, for keyword = blank, sprintf only eight
#			spaces before the value, to agree with cfitsio.

# tbfhpd -- put double-precision header parameter

procedure tbfhpd (tp, keyword, value)

pointer tp		# i: pointer to table descriptor
char	keyword[ARB]	# i: name of parameter to put
double	value		# i: value of parameter
#--
pointer sp
pointer sval		# for getting the value as a string
pointer comment		# for comment string
int	status		# zero is OK
bool	bval
errchk	tbferr

begin
	call smark (sp)
	call salloc (sval, SZ_LINE, TY_CHAR)
	call salloc (comment, SZ_LINE, TY_CHAR)

	status = 0

	# Get the current value to see if the keyword already exists,
	# and if so, to check the data type.
	call fsgkey (TB_FILE(tp), keyword, Memc[sval], Memc[comment], status)

	if (status == 0) {

	    # Modify existing keyword value, leaving comment unchanged.
	    if (Memc[sval] == '\'') {
		call sprintf (Memc[sval], SZ_LINE, "%-25.15g")
		    call pargd (value)
		call fsmkys (TB_FILE(tp), keyword, Memc[sval],
			Memc[comment], status)
	    } else if (Memc[sval] == 'T' || Memc[sval] == 'F') {
		bval = (value != 0.d0)
		call fsmkyl (TB_FILE(tp), keyword, bval, Memc[comment], status)
	    } else {
		# FITSIO should be able to handle other type conversions.
		call fsmkyd (TB_FILE(tp), keyword, value, NDECIMALS_DOUBLE,
			Memc[comment], status)
	    }

	} else if (status == FITS_KEYWORD_MISSING) {

	    status = 0
	    call ftcmsg()
	    call fspkyd (TB_FILE(tp), keyword, value, NDECIMALS_DOUBLE,
			"", status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	}

	if (status != 0)
	    call tbferr (status)

	call sfree (sp)
end

# tbfhpr -- put single-precision header parameter

procedure tbfhpr (tp, keyword, value)

pointer tp		# i: pointer to table descriptor
char	keyword[ARB]	# i: name of parameter to put
real	value		# i: value of parameter
#--
pointer sp
pointer sval		# for getting the value as a string
pointer comment		# for comment string
int	status		# zero is OK
bool	bval
errchk	tbferr

begin
	call smark (sp)
	call salloc (sval, SZ_LINE, TY_CHAR)
	call salloc (comment, SZ_LINE, TY_CHAR)

	status = 0

	# Get the current value to see if the keyword already exists,
	# and if so, to check the data type.
	call fsgkey (TB_FILE(tp), keyword, Memc[sval], Memc[comment], status)

	if (status == 0) {

	    # Modify existing keyword value, leaving comment unchanged.
	    if (Memc[sval] == '\'') {
		call sprintf (Memc[sval], SZ_LINE, "%-15.7g")
		    call pargr (value)
		call fsmkys (TB_FILE(tp), keyword, Memc[sval],
			Memc[comment], status)
	    } else if (Memc[sval] == 'T' || Memc[sval] == 'F') {
		bval = (value != 0.)
		call fsmkyl (TB_FILE(tp), keyword, bval, Memc[comment], status)
	    } else {
		# FITSIO should be able to handle other type conversions.
		call fsmkye (TB_FILE(tp), keyword, value, NDECIMALS_SINGLE,
			Memc[comment], status)
	    }

	} else if (status == FITS_KEYWORD_MISSING) {

	    status = 0
	    call ftcmsg()
	    call fspkye (TB_FILE(tp), keyword, value, NDECIMALS_SINGLE,
			"", status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	}

	if (status != 0)
	    call tbferr (status)

	call sfree (sp)
end

# tbfhpi -- put integer header parameter

procedure tbfhpi (tp, keyword, value)

pointer tp		# i: pointer to table descriptor
char	keyword[ARB]	# i: name of parameter to put
int	value		# i: value of parameter
#--
pointer sp
pointer sval		# for getting the value as a string
pointer comment		# for comment string
int	status		# zero is OK
bool	bval
errchk	tbferr

begin
	call smark (sp)
	call salloc (sval, SZ_LINE, TY_CHAR)
	call salloc (comment, SZ_LINE, TY_CHAR)

	status = 0

	# Get the current value to see if the keyword already exists,
	# and if so, to check the data type.
	call fsgkey (TB_FILE(tp), keyword, Memc[sval], Memc[comment], status)

	if (status == 0) {

	    # Modify existing keyword value, leaving comment unchanged.
	    if (Memc[sval] == '\'') {
		call sprintf (Memc[sval], SZ_LINE, "%-10d")
		    call pargi (value)
		call fsmkys (TB_FILE(tp), keyword, Memc[sval],
			Memc[comment], status)
	    } else if (Memc[sval] == 'T' || Memc[sval] == 'F') {
		bval = (value != 0)
		call fsmkyl (TB_FILE(tp), keyword, bval, Memc[comment], status)
	    } else {
		# FITSIO should be able to handle other type conversions.
		call fsmkyj (TB_FILE(tp), keyword, value, Memc[comment], status)
	    }

	} else if (status == FITS_KEYWORD_MISSING) {

	    status = 0
	    call ftcmsg()
	    call fspkyj (TB_FILE(tp), keyword, value, "", status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	}

	if (status != 0)
	    call tbferr (status)

	call sfree (sp)
end

# tbfhpb -- put Boolean header parameter

procedure tbfhpb (tp, keyword, value)

pointer tp		# i: pointer to table descriptor
char	keyword[ARB]	# i: name of parameter to put
bool	value		# i: value of parameter
#--
pointer sp
pointer sval		# for getting the value as a string
pointer comment		# for comment string
int	status		# zero is OK
errchk	tbferr

begin
	call smark (sp)
	call salloc (sval, SZ_LINE, TY_CHAR)
	call salloc (comment, SZ_LINE, TY_CHAR)

	status = 0

	# Get the current value to see if the keyword already exists,
	# and if so, to check the data type.
	call fsgkey (TB_FILE(tp), keyword, Memc[sval], Memc[comment], status)

	if (status == 0) {

	    # Modify existing keyword value, leaving comment unchanged.
	    if (Memc[sval] == '\'') {
		if (value)
		    call strcpy ("TRUE", Memc[sval], SZ_LINE)
		else
		    call strcpy ("FALSE", Memc[sval], SZ_LINE)
		call fsmkys (TB_FILE(tp), keyword, Memc[sval],
			Memc[comment], status)
	    } else {
		# FITSIO should be able to handle other type conversions.
		call fsmkyl (TB_FILE(tp), keyword, value, Memc[comment], status)
	    }

	} else if (status == FITS_KEYWORD_MISSING) {

	    status = 0
	    call ftcmsg()
	    call fspkyl (TB_FILE(tp), keyword, value, "", status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	}

	if (status != 0)
	    call tbferr (status)

	call sfree (sp)
end

# tbfhpt -- put text-string header parameter

procedure tbfhpt (tp, keyword, text)

pointer tp		# i: pointer to table descriptor
char	keyword[ARB]	# i: name of parameter to put
char	text[ARB]	# i: value of parameter
#--
pointer sp
pointer sval		# for getting the value as a string
pointer comment		# for comment string
char	uckey[SZ_KEYWORD]	# keyword converted to upper case
bool	iscomment	# true if the keyword is history, comment, or blank
int	k		# loop index
int	status		# zero is OK
int	strlen()
bool	streq()
errchk	tbferr

begin
	status = 0

	# Convert to upper case and trim trailing blanks.
	call strcpy (keyword, uckey, SZ_KEYWORD)
	call strupr (uckey)
	do k = strlen (uckey), 1, -1 {
	    if (IS_WHITE(uckey[k]))
		uckey[k] = EOS
	    else
		break
	}

	# If the keyword is history or comment, add a new keyword record.
	if (streq (uckey, "HISTORY")) {
	    iscomment = true
	    call fsphis (TB_FILE(tp), text, status)
	} else if (streq (uckey, "COMMENT")) {
	    iscomment = true
	    call fspcom (TB_FILE(tp), text, status)
	} else if (uckey[1] == EOS) {
	    iscomment = true
	    call smark (sp)
	    call salloc (comment, SZ_PARREC, TY_CHAR)
	    call sprintf (Memc[comment], SZ_PARREC, "        %s")
		call pargstr (text)
	    call fsprec (TB_FILE(tp), Memc[comment], status)
	    call sfree (sp)
	} else {
	    iscomment = false
	}
	if (iscomment) {
	    if (status != 0)
		call tbferr (status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	    return
	}

	call smark (sp)
	call salloc (sval, SZ_LINE, TY_CHAR)
	call salloc (comment, SZ_LINE, TY_CHAR)

	# Get current value to see if the keyword already exists.
	call fsgkys (TB_FILE(tp), keyword, Memc[sval], Memc[comment], status)

	if (status == 0) {
	    # Modify existing keyword value, leaving comment unchanged.
	    call fsmkys (TB_FILE(tp), keyword, text, Memc[comment], status)
	} else if (status == FITS_KEYWORD_MISSING) {
	    status = 0
	    call ftcmsg()
	    call fspkys (TB_FILE(tp), keyword, text, "", status)
	    TB_NPAR(tp) = TB_NPAR(tp) + 1
	}

	if (status != 0)
	    call tbferr (status)

	call sfree (sp)
end