File: funits.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 (440 lines) | stat: -rw-r--r-- 9,853 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
include	<error.h>
include	"funits.h"


# FUN_OPEN -- Open funits package
# It is allowed to open an unknown funit type

pointer procedure fun_open (funits)

char	funits[ARB]		# Units string
pointer	fun			# Units pointer returned

begin
	call calloc (fun, FUN_LEN, TY_STRUCT)
	iferr (call fun_decode (fun, funits)) {
	    call fun_close (fun)
	    call erract (EA_ERROR)
	}
	return (fun)
end


# FUN_CLOSE -- Close funits package

procedure fun_close (fun)

pointer	fun			# Units pointer

begin
	call mfree (fun, TY_STRUCT)
end


# FUN_COPY -- Copy funits pointer

procedure fun_copy (fun1, fun2)

pointer	fun1, fun2		# Units pointers

begin
	if (fun2 == NULL)
	    call malloc (fun2, FUN_LEN, TY_STRUCT)
	call amovi (Memi[fun1], Memi[fun2], FUN_LEN)
end


# FUN_DECODE -- Decode funits string and set up funits structure.
# The main work is done in FUN_DECODE1 so that the funits string may
# be recursive; i.e. the funits string may contain other funits strings.

procedure fun_decode (fun, funits)

pointer	fun			# Units pointer
char	funits[ARB]		# Units string

bool	streq()
pointer	sp, funits1, temp
errchk	fun_decode1, fun_ctranr

begin
	if (streq (funits, FUN_USER(fun)))
	    return

	call smark (sp)
	call salloc (funits1, SZ_LINE, TY_CHAR)
	call salloc (temp, FUN_LEN, TY_STRUCT)

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

	iferr (call fun_decode1 (fun, funits, Memc[funits1], SZ_LINE)) {
	    call fun_copy (temp, fun)
	    call sfree (sp)
	    call erract (EA_ERROR)
	}

	call sfree (sp)
end


# FUN_DECODE1 -- Decode funits string and set up funits structure.
# Unknown funit strings are allowed.

procedure fun_decode1 (fun, funits, funits1, sz_funits1)

pointer	fun			# Units pointer
char	funits[ARB]		# Units string
char	funits1[sz_funits1]	# Secondary funits string to return
int	sz_funits1		# Size of secondary funits string

int	funmod, funtype
int	i, j,  nscan(), strdic()
real	funscale
pointer	sp, str

int	class[FUN_NUNITS]
real	scale[FUN_NUNITS]
data	class /FUN_FREQ,FUN_FREQ,FUN_FREQ,FUN_WAVE/
data	scale /FUN_J,FUN_FU,FUN_CGSH,FUN_CGSA/

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

	call strcpy (funits, Memc[str], SZ_FNAME)
	call strlwr (Memc[str])
	call sscan (Memc[str])
	funtype = 0
	funmod = 0
	do i = 1, 2 {
	    call gargwrd (Memc[str], SZ_FNAME)
	    if (nscan() != i)
		break

	    j = strdic (Memc[str], Memc[str], SZ_FNAME, FUN_DIC)
	    if (j > FUN_NUNITS) {
		if (funmod != 0)
		    break
		funmod = j - FUN_NUNITS
	    } else {
		funtype = j
		break
	    }
	}
	i = nscan()
	call gargr (funscale)
	if (nscan() != i+1)
	    funscale = 1

	if (funtype == 0) {
	    FUN_TYPE(fun) = 0
	    FUN_CLASS(fun) = FUN_UNKNOWN
	    FUN_LABEL(fun) = EOS
	    call strcpy (funits, FUN_UNITS(fun), SZ_UNITS)
	} else {
	    FUN_TYPE(fun) = funtype
	    FUN_CLASS(fun) = class[funtype]
	    FUN_MOD(fun) = funmod
	    FUN_SCALE(fun) = scale[funtype] * funscale
	    FUN_LABEL(fun) = EOS
	    FUN_UNITS(fun) = EOS
	    call strcpy (funits, FUN_USER(fun), SZ_UNITS)
	    switch (funmod) {
	    case FUN_LOG:
		call strcat ("Log ", FUN_LABEL(fun), SZ_UNITS)
	    case FUN_MAG:
		call strcat ("Mag ", FUN_LABEL(fun), SZ_UNITS)
	    }
	    call strcat ("Flux", FUN_LABEL(fun), SZ_UNITS)
	    if (funscale != 1) {
		call sprintf (FUN_UNITS(fun), SZ_UNITS, "%sx%.1g")
		    call pargstr (Memc[str])
		    call pargr (funscale)
	    } else {
		call sprintf (FUN_UNITS(fun), SZ_UNITS, "%s")
		    call pargstr (Memc[str])
	    }
	}

	call sfree (sp)
end


# FUN_COMPARE -- Compare two funits

bool procedure fun_compare (fun1, fun2)

pointer	fun1, fun2		# Units pointers to compare
bool	strne()

begin
	if (strne (FUN_UNITS(fun1), FUN_UNITS(fun2)))
	    return (false)
	if (strne (FUN_LABEL(fun1), FUN_LABEL(fun2)))
	    return (false)
	return (true)
end


# FUN_CTRANR -- Transform funits
# Error is returned if the transform cannot be made

procedure fun_ctranr (fun1, fun2, dun, dval, fval1, fval2, nvals)

pointer	fun1			# Input funits pointer
pointer	fun2			# Output funits pointer
pointer	dun			# Input units pointer
real	dval[nvals]		# Input dispersion values
real	fval1[nvals]		# Input flux values
real	fval2[nvals]		# Output flux values
int	nvals			# Number of values

int	i
real	s, lambda
pointer	ang, un_open()
bool	fun_compare()
errchk	un_open, un_ctranr

begin
	if (fun_compare (fun1, fun2)) {
	    call amovr (fval1, fval2, nvals)
	    return
	}

	if (FUN_CLASS(fun1) == FUN_UNKNOWN || FUN_CLASS(fun2) == FUN_UNKNOWN)
	    call error (1, "Cannot convert between selected funits")

	call amovr (fval1, fval2, nvals)

	s = FUN_SCALE(fun1)
	switch (FUN_MOD(fun1)) {
	case FUN_LOG:
	    do i = 1, nvals
		fval2[i] = 10. ** fval2[i]
	case FUN_MAG:
	    do i = 1, nvals
		fval2[i] = 10. ** (-0.4 * fval2[i])
	}
	switch (FUN_CLASS(fun1)) {
	case FUN_FREQ:
	    do i = 1, nvals
		fval2[i] = fval2[i] / s
	case FUN_WAVE:
	    if (FUN_CLASS(fun2) != FUN_WAVE) {
		s = s * FUN_VLIGHT
		ang = un_open ("angstroms")
		do i = 1, nvals {
		    call un_ctranr (dun, ang, dval[i], lambda, 1)
		    fval2[i] = fval2[i] / s * lambda**2
		}
		call un_close (ang)
	    } else {
		do i = 1, nvals
		    fval2[i] = fval2[i] / s
	    }
	}

	s = FUN_SCALE(fun2)
	switch (FUN_CLASS(fun2)) {
	case FUN_FREQ:
	    do i = 1, nvals
		fval2[i] = fval2[i] * s
	case FUN_WAVE:
	    if (FUN_CLASS(fun1) != FUN_WAVE) {
		s = s * FUN_VLIGHT
		ang = un_open ("angstroms")
		do i = 1, nvals {
		    call un_ctranr (dun, ang, dval[i], lambda, 1)
		    fval2[i] = fval2[i] * s / lambda**2
		}
		call un_close (ang)
	    } else {
		do i = 1, nvals
		    fval2[i] = fval2[i] * s
	    }
	}
	switch (FUN_MOD(fun2)) {
	case FUN_LOG:
	    do i = 1, nvals
		fval2[i] = log10 (fval2[i])
	case FUN_MAG:
	    do i = 1, nvals
		fval2[i] = -2.5 * log10 (fval2[i])
	}
end


# FUN_CHANGER -- Change funits
# Error is returned if the conversion cannot be made

procedure fun_changer (fun, funits, dun, dvals, fvals, nvals, update)

pointer	fun			# Units pointer (may be changed)
char	funits[ARB]		# Desired funits
pointer	dun			# Dispersion units pointer
real	dvals[nvals]		# Dispersion values
real	fvals[nvals]		# Flux Values
int	nvals			# Number of values
int	update			# Update funits pointer?

bool	streq(), fun_compare()
pointer	fun1, fun_open()
errchk	fun_open, fun_ctranr

begin

	# Check for same funit string
	if (streq (funits, FUN_USER(fun)))
	    return

	# Check for error in funits string, or the same funits.
	fun1 = fun_open (funits)
	if (fun_compare (fun1, fun)) {
	    call strcpy (funits, FUN_USER(fun), SZ_UNITS)
	    call fun_close (fun1)
	    return
	}

	iferr {
	    call fun_ctranr (fun, fun1, dun, dvals, fvals, fvals, nvals)
	    if (update == YES)
		call fun_copy (fun1, fun)
	    call fun_close(fun1)
	} then {
	    call fun_close(fun1)
	    call erract (EA_ERROR)
	}
end


# FUN_CTRAND -- Transform funits
# Error is returned if the transform cannot be made

procedure fun_ctrand (fun1, fun2, dun, dval, fval1, fval2, nvals)

pointer	fun1			# Input funits pointer
pointer	fun2			# Output funits pointer
pointer	dun			# Input dispersion units pointer
double	dval[nvals]		# Input dispersion values
double	fval1[nvals]		# Input flux values
double	fval2[nvals]		# Output flux values
int	nvals			# Number of values

int	i
double	s, lambda
pointer	ang, un_open()
bool	fun_compare()
errchk	un_open, un_ctrand

begin
	if (fun_compare (fun1, fun2)) {
	    call amovd (fval1, fval2, nvals)
	    return
	}

	if (FUN_CLASS(fun1) == FUN_UNKNOWN || FUN_CLASS(fun2) == FUN_UNKNOWN)
	    call error (1, "Cannot convert between selected funits")

	call amovd (fval1, fval2, nvals)

	s = FUN_SCALE(fun1)
	switch (FUN_MOD(fun1)) {
	case FUN_LOG:
	    do i = 1, nvals
		fval2[i] = 10. ** fval2[i]
	case FUN_MAG:
	    do i = 1, nvals
		fval2[i] = 10. ** (-0.4 * fval2[i])
	}
	switch (FUN_CLASS(fun1)) {
	case FUN_FREQ:
	    do i = 1, nvals
		fval2[i] = fval2[i] / s
	case FUN_WAVE:
	    if (FUN_CLASS(fun2) != FUN_WAVE) {
		s = s * FUN_VLIGHT
		ang = un_open ("angstroms")
		do i = 1, nvals {
		    call un_ctrand (dun, ang, dval[i], lambda, 1)
		    fval2[i] = fval2[i] / s * lambda**2
		}
		call un_close (ang)
	    } else {
		do i = 1, nvals
		    fval2[i] = fval2[i] / s
	    }
	}

	s = FUN_SCALE(fun2)
	switch (FUN_CLASS(fun2)) {
	case FUN_FREQ:
	    do i = 1, nvals
		fval2[i] = fval2[i] * s
	case FUN_WAVE:
	    if (FUN_CLASS(fun1) != FUN_WAVE) {
		s = s * FUN_VLIGHT
		ang = un_open ("angstroms")
		do i = 1, nvals {
		    call un_ctrand (dun, ang, dval[i], lambda, 1)
		    fval2[i] = fval2[i] * s / lambda**2
		}
		call un_close (ang)
	    } else {
		do i = 1, nvals
		    fval2[i] = fval2[i] * s
	    }
	}
	switch (FUN_MOD(fun2)) {
	case FUN_LOG:
	    do i = 1, nvals
		fval2[i] = log10 (fval2[i])
	case FUN_MAG:
	    do i = 1, nvals
		fval2[i] = -2.5 * log10 (fval2[i])
	}

end


# FUN_CHANGED -- Change funits
# Error is returned if the conversion cannot be made

procedure fun_changed (fun, funits, dun, dvals, fvals, nvals, update)

pointer	fun			# Units pointer (may be changed)
char	funits[ARB]		# Desired funits
pointer	dun			# Input dispersion pointer
double	dvals[nvals]		# Input dispersion values
double	fvals[nvals]		# Flux values
int	nvals			# Number of values
int	update			# Update funits pointer?

bool	streq(), fun_compare()
pointer	fun1, fun_open()
errchk	fun_open, fun_ctrand

begin

	# Check for same funit string
	if (streq (funits, FUN_USER(fun)))
	    return

	# Check for error in funits string, or the same funits.
	fun1 = fun_open (funits)
	if (fun_compare (fun1, fun)) {
	    call strcpy (funits, FUN_USER(fun), SZ_UNITS)
	    call fun_close (fun1)
	    return
	}

	iferr {
	    call fun_ctrand (fun, fun1, dun, dvals, fvals, fvals, nvals)
	    if (update == YES)
		call fun_copy (fun1, fun)
	    call fun_close(fun1)
	} then {
	    call fun_close(fun1)
	    call erract (EA_ERROR)
	}
end