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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
include <ctype.h>
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
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 || UN_CLASS(un) == UN_DOP) {
call salloc (un1, UN_LEN, TY_STRUCT)
call un_decode1 (un1, Memc[units1], Memc[units1], SZ_LINE)
if (UN_CLASS(un1) == UN_VEL || UN_CLASS(un1) == UN_DOP)
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, k, nscan(), strdic(), strlen()
pointer sp, str
pointer stp, sym, stfind(), strefsbuf()
int class[UN_NUNITS]
real scale[UN_NUNITS]
data stp/NULL/
data class /UN_WAVE,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,UN_DOP/
data scale /UN_ANG,UN_NM,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,UN_Z/
begin
call smark (sp)
call salloc (str, SZ_FNAME, TY_CHAR)
iferr (call un_abbr (stp))
;
call strcpy (units, Memc[str], SZ_FNAME)
if (stp != NULL) {
sym = stfind (stp, Memc[str])
if (sym != NULL)
call strcpy (Memc[strefsbuf(stp,Memi[sym])],
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)
for (k=strlen(Memc[str]); k>0 &&
(IS_WHITE(Memc[str+k-1]) || Memc[str+k-1]=='\n'); k=k-1)
Memc[str+k-1] = EOS
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 || class[j] == UN_DOP) {
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)
case UN_DOP:
call strcat ("Redshift", 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, z
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 {
z = val2[i] / s
val2[i] = sqrt ((1 + z) / (1 - z)) * v
}
case UN_ENERGY:
do i = 1, nvals
val2[i] = s / val2[i]
case UN_DOP:
v = UN_VREF(un1)
do i = 1, nvals
val2[i] = (val2[i] / s + 1) * v
}
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 {
z = (val2[i] / v) ** 2
val2[i] = (z - 1) / (z + 1) * s
}
case UN_ENERGY:
do i = 1, nvals
val2[i] = s / val2[i]
case UN_DOP:
v = UN_VREF(un2)
do i = 1, nvals
val2[i] = (val2[i] / v - 1) * s
}
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, z
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 {
z = val2[i] / s
val2[i] = sqrt ((1 + z) / (1 - z)) * v
}
case UN_ENERGY:
do i = 1, nvals
val2[i] = s / val2[i]
case UN_DOP:
v = UN_VREF(un1)
do i = 1, nvals
val2[i] = (val2[i] / s + 1) * v
}
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 {
z = (val2[i] / v) ** 2
val2[i] = (z - 1) / (z + 1) * s
}
case UN_ENERGY:
do i = 1, nvals
val2[i] = s / val2[i]
case UN_DOP:
v = UN_VREF(un2)
do i = 1, nvals
val2[i] = (val2[i] / v - 1) * s
}
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
# UN_ABBR -- Load abbreviations into a symbol table.
procedure un_abbr (stp)
pointer stp #U Symbol table
int fd, open(), fscan(), nscan(), stpstr()
pointer sp, key, val
pointer sym, stopen(), stfind(), stenter(), strefsbuf()
errchk open
begin
if (stp != NULL)
return
fd = open (ABBREVIATIONS, READ_ONLY, TEXT_FILE)
stp = stopen ("unabbr", 20, 20, 40*SZ_LINE)
call smark (sp)
call salloc (key, SZ_LINE, TY_CHAR)
call salloc (val, SZ_LINE, TY_CHAR)
while (fscan (fd) != EOF) {
call gargwrd (Memc[key], SZ_LINE)
call gargwrd (Memc[val], SZ_LINE)
if (nscan() != 2)
next
if (Memc[key] == '#')
next
sym = stfind (stp, Memc[key])
if (sym == NULL) {
sym = stenter (stp, Memc[key], 1)
Memi[sym] = stpstr (stp, Memc[val], SZ_LINE)
} else
call strcpy (Memc[val], Memc[strefsbuf(stp,Memi[sym])], SZ_LINE)
}
call close (fd)
call sfree (sp)
end
|