File: units0.x

package info (click to toggle)
iraf-rvsao 2.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,456 kB
  • sloc: ansic: 963; lisp: 651; fortran: 397; makefile: 27
file content (450 lines) | stat: -rw-r--r-- 9,889 bytes parent folder | download
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File Util/units.x
# April 28, 1994

include	<error.h>
include	"units.h"


# UN_OPEN -- Open units package
# It is allowed to open an unknown unit type

pointer procedure un_open (units)

char	units[ARB]		# Units string
pointer	un			# Units pointer returned

begin
	call calloc (un, UN_LEN, TY_STRUCT)
	iferr (call un_decode (un, units)) {
	    call un_close (un)
	    call erract (EA_ERROR)
	}
	return (un)
end


# UN_CLOSE -- Close units package

procedure un_close (un)

pointer	un			# Units pointer

begin
	if (un == NULL)
	    return
	call mfree (un, TY_STRUCT)
end


# UN_COPY -- Copy units pointer

procedure un_copy (un1, un2)

pointer	un1, un2		# Units pointers

begin
	if (un2 == NULL)
	    call malloc (un2, UN_LEN, TY_STRUCT)
	call amovi (Memi[un1], Memi[un2], UN_LEN)
end


# UN_DECODE -- Decode units string and set up units structure.
# The main work is done in UN_DECODE1 so that the units string may
# be recursive; i.e. the units string may contain other units strings.
# In particular, this is required for the velocity units to specify
# a reference wavelength.

procedure un_decode (un, units)

pointer	un			# Units pointer
char	units[ARB]		# Units string

bool	streq()
pointer	sp, units1, temp, un1, un2
errchk	un_decode1, un_ctranr

begin
	if (streq (units, UN_USER(un)))
	    return

	call smark (sp)
	call salloc (units1, SZ_LINE, TY_CHAR)
	call salloc (temp, UN_LEN, TY_STRUCT)

	# Save a copy to restore in case of an error.
	call un_copy (un, temp)

	iferr {
	    # Decode the primary units
	    call un_decode1 (un, units, Memc[units1], SZ_LINE)

	    # Decode velocity reference wavelength if necessary.
	    if (UN_CLASS(un) == UN_VEL) {
		call salloc (un1, UN_LEN, TY_STRUCT)
		call un_decode1 (un1, Memc[units1], Memc[units1], SZ_LINE)
		if (UN_CLASS(un1) == UN_VEL)
		    call error (1,
			"Velocity reference units may not be velocity")
		call salloc (un2, UN_LEN, TY_STRUCT)
		call un_decode1 (un2, "angstroms", Memc[units1], SZ_LINE)
		call un_ctranr (un1, un2, UN_VREF(un), UN_VREF(un), 1)
	    }
	} then {
	    call un_copy (temp, un)
	    call sfree (sp)
	    call erract (EA_ERROR)
	}

	call sfree (sp)
end


# UN_DECODE1 -- Decode units string and set up units structure.
# Return any secondary units string.  Unknown unit strings are allowed.

procedure un_decode1 (un, units, units1, sz_units1)

pointer	un			# Units pointer
char	units[ARB]		# Units string
char	units1[sz_units1]	# Secondary units string to return
int	sz_units1		# Size of secondary units string

int	unlog, uninv, untype
int	i, j,  nscan(), strdic()
pointer	sp, str

int	class[UN_NUNITS]
real	scale[UN_NUNITS]
data	class /UN_WAVE,UN_WAVE,UN_WAVE,UN_WAVE,UN_WAVE,UN_WAVE,
		UN_FREQ,UN_FREQ,UN_FREQ,UN_FREQ,UN_VEL,UN_VEL,
		UN_ENERGY,UN_ENERGY,UN_ENERGY/
data	scale /UN_ANG,UN_MMIC,UN_MIC,UN_MM,UN_CM,UN_M,UN_HZ,UN_KHZ,
		UN_MHZ,UN_GHZ,UN_MPS,UN_KPS,UN_EV,UN_KEV,UN_MEV/

begin
	call smark (sp)
	call salloc (str, SZ_FNAME, TY_CHAR)

	call strcpy (units, Memc[str], SZ_FNAME)
	call strlwr (Memc[str])
	call sscan (Memc[str])
	untype = 0
	unlog = NO
	uninv = NO
	do i = 1, 3 {
	    call gargwrd (Memc[str], SZ_FNAME)
	    if (nscan() != i)
		break

	    j = strdic (Memc[str], Memc[str], SZ_FNAME, UN_DIC)
	    if (j > UN_NUNITS) {
	        j = j - UN_NUNITS
	        if (j == 1) {
		    if (unlog == YES)
			break
		    unlog = YES
		} else if (j == 2) {
		    if (uninv == YES)
			break
		    uninv = YES
		}
	    } else {
		if (class[j] == UN_VEL) {
		    call gargr (UN_VREF(un))
		    call gargstr (units1, sz_units1)
		    if (nscan() != i+2)
			call error (1, "Error in velocity reference wavelength")
		} else
		    UN_VREF(un) = 0.
		untype = j
		break
	    }
	}

	if (untype == 0) {
	    UN_TYPE(un) = 0
	    UN_CLASS(un) = UN_UNKNOWN
	    UN_LABEL(un) = EOS
	    call strcpy (units, UN_UNITS(un), SZ_UNITS)
	} else {
	    UN_TYPE(un) = untype
	    UN_CLASS(un) = class[untype]
	    UN_LOG(un) = unlog
	    UN_INV(un) = uninv
	    UN_SCALE(un) = scale[untype]
	    UN_LABEL(un) = EOS
	    UN_UNITS(un) = EOS
	    call strcpy (units, UN_USER(un), SZ_UNITS)

	    if (unlog == YES)
		call strcat ("Log ", UN_LABEL(un), SZ_UNITS)
	    if (uninv == YES)
		call strcat ("inverse ", UN_UNITS(un), SZ_UNITS)
	    call strcat (Memc[str], UN_UNITS(un), SZ_UNITS)
	    switch (class[j]) {
	    case UN_WAVE:
		if (uninv == NO)
		    call strcat ("Wavelength", UN_LABEL(un), SZ_UNITS)
		else
		    call strcat ("Wavenumber", UN_LABEL(un), SZ_UNITS)
	    case UN_FREQ:
		call strcat ("Frequency", UN_LABEL(un), SZ_UNITS)
	    case UN_VEL:
		call strcat ("Velocity", UN_LABEL(un), SZ_UNITS)
	    case UN_ENERGY:
		call strcat ("Energy", UN_LABEL(un), SZ_UNITS)
	    }
	}

	call sfree (sp)
end


# UN_COMPARE -- Compare two units

bool procedure un_compare (un1, un2)

pointer	un1, un2		# Units pointers to compare
bool	strne()

begin
	if (strne (UN_UNITS(un1), UN_UNITS(un2)))
	    return (false)
	if (strne (UN_LABEL(un1), UN_LABEL(un2)))
	    return (false)
	if (UN_VREF(un1) != UN_VREF(un2))
	    return (false)
	return (true)
end


# UN_CTRANR -- Transform units
# Error is returned if the transform cannot be made

procedure un_ctranr (un1, un2, val1, val2, nvals)

pointer	un1			# Input units pointer
pointer	un2			# Output units pointer
real	val1[nvals]		# Input values
real	val2[nvals]		# Output values
int	nvals			# Number of values

int	i
real	s, v
bool	un_compare()

begin
	if (un_compare (un1, un2)) {
	    call amovr (val1, val2, nvals)
	    return
	}

	if (UN_CLASS(un1) == UN_UNKNOWN || UN_CLASS(un2) == UN_UNKNOWN)
	    call error (1, "Cannot convert between selected units")

	call amovr (val1, val2, nvals)

	s = UN_SCALE(un1)
	if (UN_LOG(un1) == YES)
	    do i = 1, nvals
		val2[i] = 10. ** val2[i]
	if (UN_INV(un1) == YES)
	    do i = 1, nvals
		val2[i] = 1. / val2[i]
	switch (UN_CLASS(un1)) {
	case UN_WAVE:
	    do i = 1, nvals
		val2[i] = val2[i] / s
	case UN_FREQ:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	case UN_VEL:
	    v = UN_VREF(un1)
	    do i = 1, nvals
		val2[i] = (val2[i] / s + 1) * v
	case UN_ENERGY:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	}

	s = UN_SCALE(un2)
	switch (UN_CLASS(un2)) {
	case UN_WAVE:
	    do i = 1, nvals
		val2[i] = val2[i] * s
	case UN_FREQ:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	case UN_VEL:
	    v = UN_VREF(un2)
	    do i = 1, nvals
		val2[i] = (val2[i] / v - 1) * s
	case UN_ENERGY:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	}
	if (UN_INV(un2) == YES)
	    do i = 1, nvals
		val2[i] = 1. / val2[i]
	if (UN_LOG(un2) == YES)
	    do i = 1, nvals
		val2[i] = log10 (val2[i])
end


# UN_CHANGER -- Change units
# Error is returned if the conversion cannot be made

procedure un_changer (un, units, vals, nvals, update)

pointer	un			# Units pointer (may be changed)
char	units[ARB]		# Desired units
real	vals[nvals]		# Values
int	nvals			# Number of values
int	update			# Update units pointer?

bool	streq(), un_compare()
pointer	un1, un_open()
errchk	un_open, un_ctranr

begin

	# Check for same unit string
	if (streq (units, UN_USER(un)))
	    return

	# Check for error in units string, or the same units.
	un1 = un_open (units)
	if (un_compare (un1, un)) {
	    call strcpy (units, UN_USER(un), SZ_UNITS)
	    call un_close (un1)
	    return
	}

	iferr {
	    call un_ctranr (un, un1, vals, vals, nvals)
	    if (update == YES)
		call un_copy (un1, un)
	    call un_close(un1)
	} then {
	    call un_close(un1)
	    call erract (EA_ERROR)
	}
end


# UN_CTRAND -- Transform units
# Error is returned if the transform cannot be made

procedure un_ctrand (un1, un2, val1, val2, nvals)

pointer	un1			# Input units pointer
pointer	un2			# Output units pointer
double	val1[nvals]		# Input values
double	val2[nvals]		# Output values
int	nvals			# Number of values

int	i
double	s, v
bool	un_compare()

begin
	if (un_compare (un1, un2)) {
	    call amovd (val1, val2, nvals)
	    return
	}

	if (UN_CLASS(un1) == UN_UNKNOWN || UN_CLASS(un2) == UN_UNKNOWN)
	    call error (1, "Cannot convert between selected units")

	call amovd (val1, val2, nvals)

	s = UN_SCALE(un1)
	if (UN_LOG(un1) == YES)
	    do i = 1, nvals
		val2[i] = 10. ** val2[i]
	if (UN_INV(un1) == YES)
	    do i = 1, nvals
		val2[i] = 1. / val2[i]
	switch (UN_CLASS(un1)) {
	case UN_WAVE:
	    do i = 1, nvals
		val2[i] = val2[i] / s
	case UN_FREQ:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	case UN_VEL:
	    v = UN_VREF(un1)
	    do i = 1, nvals
		val2[i] = (val2[i] / s + 1) * v
	case UN_ENERGY:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	}

	s = UN_SCALE(un2)
	switch (UN_CLASS(un2)) {
	case UN_WAVE:
	    do i = 1, nvals
		val2[i] = val2[i] * s
	case UN_FREQ:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	case UN_VEL:
	    v = UN_VREF(un2)
	    do i = 1, nvals
		val2[i] = (val2[i] / v - 1) * s
	case UN_ENERGY:
	    do i = 1, nvals
		val2[i] = s / val2[i]
	}
	if (UN_INV(un2) == YES)
	    do i = 1, nvals
		val2[i] = 1. / val2[i]
	if (UN_LOG(un2) == YES)
	    do i = 1, nvals
		val2[i] = log10 (val2[i])
end


# UN_CHANGED -- Change units
# Error is returned if the conversion cannot be made

procedure un_changed (un, units, vals, nvals, update)

pointer	un			# Units pointer (may be changed)
char	units[ARB]		# Desired units
double	vals[nvals]		# Values
int	nvals			# Number of values
int	update			# Update units pointer?

bool	streq(), un_compare()
pointer	un1, un_open()
errchk	un_open, un_ctrand

begin

	# Check for same unit string
	if (streq (units, UN_USER(un)))
	    return

	# Check for error in units string, or the same units.
	un1 = un_open (units)
	if (un_compare (un1, un)) {
	    call strcpy (units, UN_USER(un), SZ_UNITS)
	    call un_close (un1)
	    return
	}

	iferr {
	    call un_ctrand (un, un1, vals, vals, nvals)
	    if (update == YES)
		call un_copy (un1, un)
	    call un_close(un1)
	} then {
	    call un_close(un1)
	    call erract (EA_ERROR)
	}
end
# Apr 28 1994	Do not free pointer if null