1*e6d6c189SCody Peter Mello# To: bug-gnu-utils@prep.ai.mit.edu
2*e6d6c189SCody Peter Mello# Cc: arnold@gnu.ai.mit.edu
3*e6d6c189SCody Peter Mello# Date: Mon, 20 Nov 1995 11:39:29 -0500
4*e6d6c189SCody Peter Mello# From: "R. Hank Donnelly" <emory!head-cfa.harvard.edu!donnelly>
5*e6d6c189SCody Peter Mello#
6*e6d6c189SCody Peter Mello# Operating system: Linux1.2.13 (Slackware distrib)
7*e6d6c189SCody Peter Mello# GAWK version: 2.15 (?)
8*e6d6c189SCody Peter Mello# compiler: GCC (?)
9*e6d6c189SCody Peter Mello#
10*e6d6c189SCody Peter Mello# The following enclosed script does not want to fully process the input data
11*e6d6c189SCody Peter Mello# file. It correctly executes the operations on the first record, and then dies
12*e6d6c189SCody Peter Mello# on the second one. My true data file is much longer but this is
13*e6d6c189SCody Peter Mello# representative and it does fail on a file even as short as this one.
14*e6d6c189SCody Peter Mello# The failure appears to occur in the declared function add2output. Between the
15*e6d6c189SCody Peter Mello# steps of incrementing NF by one and setting $NF to the passed variable
16*e6d6c189SCody Peter Mello# the passed variable appears to vanish (i.e. NF does go from 68 to 69
17*e6d6c189SCody Peter Mello# and before incrementing it "variable" equals what it should but after
18*e6d6c189SCody Peter Mello# "variable" has no value at all.)
19*e6d6c189SCody Peter Mello#
20*e6d6c189SCody Peter Mello# The scripts have been developed using nawk on a Sun (where they run fine)
21*e6d6c189SCody Peter Mello# I have tried gawk there but get a different crash which I have not yet traced
22*e6d6c189SCody Peter Mello# down. Ideally I would like to keep the script the same so that it would run
23*e6d6c189SCody Peter Mello# on either gawk or nawk (that way I can step back and forth between laptop and
24*e6d6c189SCody Peter Mello# workstation.
25*e6d6c189SCody Peter Mello#
26*e6d6c189SCody Peter Mello# Any ideas why the laptop installation is having problems?
27*e6d6c189SCody Peter Mello# Hank
28*e6d6c189SCody Peter Mello#
29*e6d6c189SCody Peter Mello#
30*e6d6c189SCody Peter Mello# #!/usr/bin/gawk -f
31*e6d6c189SCody Peter Mello
32*e6d6c189SCody Peter MelloBEGIN {
33*e6d6c189SCody Peter Mello	# set a few values
34*e6d6c189SCody Peter Mello	FS = "\t"
35*e6d6c189SCody Peter Mello	OFS = "\t"
36*e6d6c189SCody Peter Mello	pi = atan2(0, -1)
37*e6d6c189SCody Peter Mello# distance from HRMA to focal plane in mm
38*e6d6c189SCody Peter Mello	fullradius = 10260.54
39*e6d6c189SCody Peter Mello
40*e6d6c189SCody Peter Mello	# set locations of parameters on input line
41*e6d6c189SCody Peter Mello	nf_nrg = 1
42*e6d6c189SCody Peter Mello	nf_order = 3
43*e6d6c189SCody Peter Mello	nf_item = 4
44*e6d6c189SCody Peter Mello	nf_suite = 5
45*e6d6c189SCody Peter Mello	nf_grating = 8
46*e6d6c189SCody Peter Mello	nf_shutter = 9
47*e6d6c189SCody Peter Mello	nf_type = 13
48*e6d6c189SCody Peter Mello	nf_src = 14
49*e6d6c189SCody Peter Mello	nf_target = 15
50*e6d6c189SCody Peter Mello	nf_voltage = 16
51*e6d6c189SCody Peter Mello	nf_flux = 17
52*e6d6c189SCody Peter Mello	nf_filt1 = 20
53*e6d6c189SCody Peter Mello	nf_filt1_th = 21
54*e6d6c189SCody Peter Mello	nf_filt2 = 22
55*e6d6c189SCody Peter Mello	nf_filt2_th = 23
56*e6d6c189SCody Peter Mello	nf_bnd = 24
57*e6d6c189SCody Peter Mello	nf_hrma_polar = 27
58*e6d6c189SCody Peter Mello	nf_hrma_az = 28
59*e6d6c189SCody Peter Mello	nf_detector = 30
60*e6d6c189SCody Peter Mello	nf_acis_read = 32
61*e6d6c189SCody Peter Mello	nf_acis_proc = 33
62*e6d6c189SCody Peter Mello	nf_acis_frame = 34
63*e6d6c189SCody Peter Mello	nf_hxda_aplist = 36
64*e6d6c189SCody Peter Mello	nf_hxda_y_range = 37
65*e6d6c189SCody Peter Mello	nf_hxda_z_range = 38
66*e6d6c189SCody Peter Mello	nf_hxda_y_step = 39
67*e6d6c189SCody Peter Mello	nf_hxda_z_step = 40
68*e6d6c189SCody Peter Mello	nf_sim_z = 41
69*e6d6c189SCody Peter Mello	nf_fam_polar = 43
70*e6d6c189SCody Peter Mello	nf_fam_az = 44
71*e6d6c189SCody Peter Mello	nf_fam_dither_type = 45
72*e6d6c189SCody Peter Mello	nf_mono_init = 51
73*e6d6c189SCody Peter Mello	nf_mono_range = 52
74*e6d6c189SCody Peter Mello	nf_mono_step = 53
75*e6d6c189SCody Peter Mello	nf_defocus = 54
76*e6d6c189SCody Peter Mello	nf_acis_temp = 55
77*e6d6c189SCody Peter Mello	nf_tight = 59
78*e6d6c189SCody Peter Mello	nf_offset_y = 64
79*e6d6c189SCody Peter Mello	nf_offset_z = 65
80*e6d6c189SCody Peter Mello
81*e6d6c189SCody Peter Mello	while( getline < "xrcf_mnemonics.dat" > 0 ) {
82*e6d6c189SCody Peter Mello		mnemonic[$1] = $2
83*e6d6c189SCody Peter Mello	}
84*e6d6c189SCody Peter Mello
85*e6d6c189SCody Peter Mello#	"date" | getline date_line
86*e6d6c189SCody Peter Mello# ADR: use a fixed date so that testing will work
87*e6d6c189SCody Peter Mello	date_line = "Sun Mar 10 23:00:27 EST 1996"
88*e6d6c189SCody Peter Mello        split(date_line, in_date, " ")
89*e6d6c189SCody Peter Mello        out_date = in_date[2] " " in_date[3] ", " in_date[6]
90*e6d6c189SCody Peter Mello}
91*e6d6c189SCody Peter Mello
92*e6d6c189SCody Peter Mellofunction add2output( variable ) {
93*e6d6c189SCody Peter Mello#print("hi1") >> "debug"
94*e6d6c189SCody Peter Mello	NF++
95*e6d6c189SCody Peter Mello#print("hi2") >> "debug"
96*e6d6c189SCody Peter Mello 	$NF = variable
97*e6d6c189SCody Peter Mello#print("hi3") >> "debug"
98*e6d6c189SCody Peter Mello}
99*e6d6c189SCody Peter Mello
100*e6d6c189SCody Peter Mellofunction error( ekey, message ) {
101*e6d6c189SCody Peter Mello	print "Error at input line " NR ", anode " ekey >> (ENVIRON["WORKDIR"] "/test.temp.errors.cleanup")
102*e6d6c189SCody Peter Mello	print "   " message "." >> (ENVIRON["WORKDIR"] "/test.temp.errors.cleanup")
103*e6d6c189SCody Peter Mello}
104*e6d6c189SCody Peter Mello
105*e6d6c189SCody Peter Mellofunction hxda_na() {
106*e6d6c189SCody Peter Mello	$nf_hxda_aplist = $nf_hxda_y_range = $nf_hxda_z_range = "N/A"
107*e6d6c189SCody Peter Mello	$nf_hxda_y_step = $nf_hxda_z_step = "N/A"
108*e6d6c189SCody Peter Mello}
109*e6d6c189SCody Peter Mello
110*e6d6c189SCody Peter Mellofunction acis_na() {
111*e6d6c189SCody Peter Mello	$nf_acis_read = $nf_acis_proc = $nf_acis_frame = $nf_acis_temp = "N/A"
112*e6d6c189SCody Peter Mello}
113*e6d6c189SCody Peter Mello
114*e6d6c189SCody Peter Mellofunction hrc_na() {
115*e6d6c189SCody Peter Mello#        print ("hi") >> "debug"
116*e6d6c189SCody Peter Mello}
117*e6d6c189SCody Peter Mello
118*e6d6c189SCody Peter Mellofunction fpsi_na() {
119*e6d6c189SCody Peter Mello	acis_na()
120*e6d6c189SCody Peter Mello	hrc_na()
121*e6d6c189SCody Peter Mello	$nf_sim_z = $nf_fam_polar = $nf_fam_az = $nf_fam_dither_type = "N/A"
122*e6d6c189SCody Peter Mello}
123*e6d6c189SCody Peter Mello
124*e6d6c189SCody Peter Mellofunction mono_na() {
125*e6d6c189SCody Peter Mello	$nf_mono_init = $nf_mono_range = $nf_mono_step = "N/A"
126*e6d6c189SCody Peter Mello}
127*e6d6c189SCody Peter Mello
128*e6d6c189SCody Peter Mello# this gives the pitch and yaw of the HRMA and FAM
129*e6d6c189SCody Peter Mello# positive pitch is facing the source "looking down"
130*e6d6c189SCody Peter Mello# positive yaw is looking left
131*e6d6c189SCody Peter Mello# 0 az is north 90 is up
132*e6d6c189SCody Peter Mello# this also adds in the FAM X,Y,Z positions
133*e6d6c189SCody Peter Mello
134*e6d6c189SCody Peter Mellofunction polaz2yawpitch(polar, az) {
135*e6d6c189SCody Peter Mello	theta = az * pi / 180
136*e6d6c189SCody Peter Mello	phi = polar * pi / 180 / 60
137*e6d6c189SCody Peter Mello
138*e6d6c189SCody Peter Mello
139*e6d6c189SCody Peter Mello	if( polar == 0 ) {
140*e6d6c189SCody Peter Mello		add2output( 0 )
141*e6d6c189SCody Peter Mello		add2output( 0 )
142*e6d6c189SCody Peter Mello	} else {
143*e6d6c189SCody Peter Mello		if(az == 0 || az == 180)
144*e6d6c189SCody Peter Mello			add2output( 0 )
145*e6d6c189SCody Peter Mello		else
146*e6d6c189SCody Peter Mello			add2output( - polar * sin(theta) )
147*e6d6c189SCody Peter Mello
148*e6d6c189SCody Peter Mello
149*e6d6c189SCody Peter Mello#			x = cos (phi)
150*e6d6c189SCody Peter Mello#			y = sin (phi) * cos (theta)
151*e6d6c189SCody Peter Mello#			add2output( atan2(y,x)*180 / pi * 60 )
152*e6d6c189SCody Peter Mello
153*e6d6c189SCody Peter Mello		if(az == 90 || az ==270 )
154*e6d6c189SCody Peter Mello			add2output( 0 )
155*e6d6c189SCody Peter Mello		else
156*e6d6c189SCody Peter Mello			add2output( - polar * cos(theta) )
157*e6d6c189SCody Peter Mello
158*e6d6c189SCody Peter Mello	}
159*e6d6c189SCody Peter Mello#			x = cos (phi)
160*e6d6c189SCody Peter Mello#			z= sin (phi) * sin (theta)
161*e6d6c189SCody Peter Mello#			add2output( atan2(z,x)*180 / pi * 60 )
162*e6d6c189SCody Peter Mello
163*e6d6c189SCody Peter Mello	if(config !~ /HXDA/) {
164*e6d6c189SCody Peter Mello# negative values of defocus move us farther from the source thus
165*e6d6c189SCody Peter Mello# increasing radius
166*e6d6c189SCody Peter Mello		radius = fullradius - defocus
167*e6d6c189SCody Peter Mello
168*e6d6c189SCody Peter Mello# FAM_x; FAM_y;  FAM_z
169*e6d6c189SCody Peter Mello	   	if((offset_y == 0) && (offset_z == 0)){
170*e6d6c189SCody Peter Mello			add2output( fullradius - radius * cos (phi) )
171*e6d6c189SCody Peter Mello
172*e6d6c189SCody Peter Mello			if (az == 90 || az ==270)
173*e6d6c189SCody Peter Mello				add2output( 0 )
174*e6d6c189SCody Peter Mello			else
175*e6d6c189SCody Peter Mello				add2output(  radius * sin (phi) * cos (theta) )
176*e6d6c189SCody Peter Mello
177*e6d6c189SCody Peter Mello			if (az == 0 || az == 180)
178*e6d6c189SCody Peter Mello				add2output( 0 )
179*e6d6c189SCody Peter Mello			else
180*e6d6c189SCody Peter Mello				add2output( - radius * sin (phi) * sin (theta) )
181*e6d6c189SCody Peter Mello	   	} else {
182*e6d6c189SCody Peter Mello# ******* THIS SEGMENT OF CODE IS NOT MATHEMATICALLY CORRECT FOR ****
183*e6d6c189SCody Peter Mello# OFF AXIS ANGLES AND IS SUPPLIED AS A WORKAROUND SINCE IT WILL
184*e6d6c189SCody Peter Mello# PROBABLY ONLY BE USED ON AXIS.
185*e6d6c189SCody Peter Mello			add2output( defocus )
186*e6d6c189SCody Peter Mello			add2output( offset_y )
187*e6d6c189SCody Peter Mello			add2output( offset_z )
188*e6d6c189SCody Peter Mello		}
189*e6d6c189SCody Peter Mello
190*e6d6c189SCody Peter Mello	} else {
191*e6d6c189SCody Peter Mello		add2output( "N/A" )
192*e6d6c189SCody Peter Mello		add2output( "N/A" )
193*e6d6c189SCody Peter Mello		add2output( "N/A" )
194*e6d6c189SCody Peter Mello	}
195*e6d6c189SCody Peter Mello}
196*e6d6c189SCody Peter Mello
197*e6d6c189SCody Peter Mello# set TIGHT/LOOSE to N/A if it is not one of the two allowed values
198*e6d6c189SCody Peter Mellofunction tight_na() {
199*e6d6c189SCody Peter Mello	if( $nf_tight !~ /TIGHT|LOOSE/ ) {
200*e6d6c189SCody Peter Mello		$nf_tight == "N/A"
201*e6d6c189SCody Peter Mello	}
202*e6d6c189SCody Peter Mello}
203*e6d6c189SCody Peter Mello
204*e6d6c189SCody Peter Mello# this entry is used to give certain entries names
205*e6d6c189SCody Peter Mello{
206*e6d6c189SCody Peter Mello	type = $nf_type
207*e6d6c189SCody Peter Mello	item = $nf_item
208*e6d6c189SCody Peter Mello	suite = $nf_suite
209*e6d6c189SCody Peter Mello	order = $nf_order
210*e6d6c189SCody Peter Mello	detector = $nf_detector
211*e6d6c189SCody Peter Mello	grating = $nf_grating
212*e6d6c189SCody Peter Mello	offset_y= $nf_offset_y
213*e6d6c189SCody Peter Mello	offset_z= $nf_offset_z
214*e6d6c189SCody Peter Mello	bnd = $nf_bnd
215*e6d6c189SCody Peter Mello	defocus = $nf_defocus
216*e6d6c189SCody Peter Mello}
217*e6d6c189SCody Peter Mello
218*e6d6c189SCody Peter Mello{
219*e6d6c189SCody Peter Mello	# make configuration parameter
220*e6d6c189SCody Peter Mello	# as well as setting configuration-dependent N/A values
221*e6d6c189SCody Peter Mello
222*e6d6c189SCody Peter Mello	if( $nf_bnd ~ "SCAN" ) {
223*e6d6c189SCody Peter Mello		# BND is scanning beam
224*e6d6c189SCody Peter Mello		config = "BND"
225*e6d6c189SCody Peter Mello		hxda_na()
226*e6d6c189SCody Peter Mello		fpsi_na()
227*e6d6c189SCody Peter Mello	} else {
228*e6d6c189SCody Peter Mello		if( grating == "NONE" ) {
229*e6d6c189SCody Peter Mello			config = "HRMA"
230*e6d6c189SCody Peter Mello		} else {
231*e6d6c189SCody Peter Mello			if( grating == "HETG" ) {
232*e6d6c189SCody Peter Mello				if( order != "Both" ) {
233*e6d6c189SCody Peter Mello				    $nf_shutter = order substr($nf_shutter, \
234*e6d6c189SCody Peter Mello					index($nf_shutter, ",") )
235*e6d6c189SCody Peter Mello				}
236*e6d6c189SCody Peter Mello			} else {
237*e6d6c189SCody Peter Mello				order = "N/A"
238*e6d6c189SCody Peter Mello			}
239*e6d6c189SCody Peter Mello			config = "HRMA/" grating
240*e6d6c189SCody Peter Mello		}
241*e6d6c189SCody Peter Mello
242*e6d6c189SCody Peter Mello		if( detector ~ /ACIS|HRC/ ) {
243*e6d6c189SCody Peter Mello			detsys = detector
244*e6d6c189SCody Peter Mello			nsub = sub("-", ",", detsys)
245*e6d6c189SCody Peter Mello			config = config "/" detsys
246*e6d6c189SCody Peter Mello			hxda_na()
247*e6d6c189SCody Peter Mello		} else {
248*e6d6c189SCody Peter Mello			config = config "/HXDA"
249*e6d6c189SCody Peter Mello			fpsi_na()
250*e6d6c189SCody Peter Mello			if( detector == "HSI" ) {
251*e6d6c189SCody Peter Mello				hxda_na()
252*e6d6c189SCody Peter Mello			}
253*e6d6c189SCody Peter Mello		}
254*e6d6c189SCody Peter Mello	}
255*e6d6c189SCody Peter Mello
256*e6d6c189SCody Peter Mello	add2output( config )
257*e6d6c189SCody Peter Mello
258*e6d6c189SCody Peter Mello	if( $nf_src ~ /EIPS|Penning/ ) mono_na()
259*e6d6c189SCody Peter Mello
260*e6d6c189SCody Peter Mello	if( $nf_src == "Penning" ) $nf_voltage = "N/A"
261*e6d6c189SCody Peter Mello
262*e6d6c189SCody Peter Mello	itm = sprintf("%03d", item)
263*e6d6c189SCody Peter Mello
264*e6d6c189SCody Peter Mello	if(config in mnemonic) {
265*e6d6c189SCody Peter Mello		if( type in mnemonic ) {
266*e6d6c189SCody Peter Mello		    ID = mnemonic[config] "-" mnemonic[type] "-" suite "." itm
267*e6d6c189SCody Peter Mello		    add2output( ID )
268*e6d6c189SCody Peter Mello		} else {
269*e6d6c189SCody Peter Mello			error(type, "measurement type not in list")
270*e6d6c189SCody Peter Mello		}
271*e6d6c189SCody Peter Mello	} else {
272*e6d6c189SCody Peter Mello		error(config, "measurement configuration not in list")
273*e6d6c189SCody Peter Mello	}
274*e6d6c189SCody Peter Mello
275*e6d6c189SCody Peter Mello	# add date to output line
276*e6d6c189SCody Peter Mello	add2output( out_date )
277*e6d6c189SCody Peter Mello
278*e6d6c189SCody Peter Mello	# Convert HRMA polar and azimuthal angles to yaw and pitch
279*e6d6c189SCody Peter Mello	polaz2yawpitch($nf_hrma_polar, $nf_hrma_az)
280*e6d6c189SCody Peter Mello
281*e6d6c189SCody Peter Mello	# set TIGHT/LOOSE to N/A if it is not one of the two allowed values
282*e6d6c189SCody Peter Mello	tight_na()
283*e6d6c189SCody Peter Mello
284*e6d6c189SCody Peter Mello	# compute number of HXDA apertures
285*e6d6c189SCody Peter Mello	if( config ~ /HXDA/ && $nf_hxda_aplist != "N/A")
286*e6d6c189SCody Peter Mello		add2output( split( $nf_hxda_aplist, dummy, "," ) )
287*e6d6c189SCody Peter Mello	else
288*e6d6c189SCody Peter Mello		add2output( "N/A" )
289*e6d6c189SCody Peter Mello
290*e6d6c189SCody Peter Mello	# make sure the BND value is properly set
291*e6d6c189SCody Peter Mello	if($nf_bnd == "FIXED" && detector ~ /ACIS/)
292*e6d6c189SCody Peter Mello		$nf_bnd =bnd"-SYNC"
293*e6d6c189SCody Peter Mello	else
294*e6d6c189SCody Peter Mello		$nf_bnd = bnd"-FREE"
295*e6d6c189SCody Peter Mello	print
296*e6d6c189SCody Peter Mello}
297