Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sum.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include "extern.h"
60 
61 int doSum()
62 {
63  int f, s, i, alreadyH0 = 0;
64  float *f1, *f2, wt1, wt2;
65  struct LP_IMAGE_INFO *first, *second;
66  struct LP_IMAGE_INFO sfirst, ssecond;
67 
68  if(strlen(args->secondArg) && strlen(args->thirdArg))
69  {
70  f = atoi(args->secondArg+1);
71  s = atoi(args->thirdArg+1);
72 
73  if((f >= 0 && f < MAX_IMAGES) && (s >=0 && s < MAX_IMAGES))
74  {
75  if(f == s)
76  {
77  st_fail("doSum(): First and Second Holders must be different");
78  return(1);
79  }
80 
81  first = findImage(f);
82  second = findImage(s);
83 
84  if(first->head.inttime && second->head.inttime)
85  {
86  bcopy((char *)first, &sfirst, sizeof(sfirst));
87  bcopy((char *)second, &ssecond, sizeof(ssecond));
88 
89  if(!strcmp(first->name, "H0"))
90  {
91  alreadyH0 = 1;
92  }
93 
94  if(!strcmp(second->name, "H0"))
95  {
96  alreadyH0 = 2;
97 
98  /* Swap them */
99  first = &ssecond;
100  second = &sfirst;
101  }
102 
103  /* Allow up to 60% of channel spacing */
104  if(fabs(first->head.restfreq - second->head.restfreq) > fabs(first->head.freqres * 0.6))
105  {
106  st_fail("Rest Freq's diff: %f > %f", fabs(first->head.restfreq-second->head.restfreq),
107  fabs(first->head.freqres * 0.6));
108  return(1);
109  }
110 
111  if(fabs(first->head.freqres - second->head.freqres) > 0.01)
112  {
113  st_fail("Backend Resolutions differ: %.3f %.3f", first->head.freqres, second->head.freqres);
114  return(1);
115  }
116 
117  if(first->head.noint != second->head.noint)
118  {
119  st_fail("Number of Channels differ: %.0f %.0f", first->head.noint, second->head.noint);
120  return(1);
121  }
122 
123  if(strncmp(first->head.object, second->head.object, 16))
124  {
125  st_fail("Source names differ: %.16s %.16s", first->head.object, second->head.object);
126  return(1);
127  }
128 
129  st_report("Summing %s & %s", first->name, second->name);
130 
131  /* Copy 'first' into H0; maybe */
132 
133  if(alreadyH0 == 0) /* first & second args a NOT H0 */
134  {
135  _copyImage(first, &H0);
136  }
137 
138  /* From now on the H0 contains the 'first' image */
139  wt1 = H0.head.inttime / ( H0.head.stsys * H0.head.stsys );
140  wt2 = second->head.inttime / ( second->head.stsys * second->head.stsys );
141 
142  f1 = H0.yplot1;
143  f2 = second->yplot1;
144 
145  for(i=0;i<H0.head.noint;i++, f1++, f2++)
146  {
147  *f1 = (*f1 * wt1 + *f2 * wt2) / (wt1 + wt2);
148  }
149 
150  H0.ainfo.escan = second->ainfo.escan;
151  H0.ainfo.scanct += second->ainfo.scanct;
152 
153  wt1 = first->head.inttime;
154  wt2 = second->head.inttime;
155 
156  H0.head.stsys = (first->head.stsys * wt1 + second->head.stsys * wt2) / (wt1 + wt2);
157  H0.head.tcal = (first->head.tcal * wt1 + second->head.tcal * wt2) / (wt1 + wt2);
158 
159  H0.head.inttime += second->head.inttime;
160  H0.head.effint += second->head.effint;
161 
162  _h0(); /* Point to H0 for replot */
163  }
164  else
165  if(!first->head.inttime && second->head.inttime) /* This an the equivalent of averaging nothing with something; just copy s to f */
166  {
167  subArgs(2, "H1");
168  subArgs(3, "H0");
169 
170  copyImage();
171 
172  return(0);
173  }
174  else
175  {
176  st_fail("doSum(): Unable to locate First or Second headers");
177 
178  return(1);
179  }
180  }
181  }
182 
183  return(0);
184 }
185 
186 
187 /* Subtract one array from another */
189 {
190  int f, s, i, alreadyH0 = 0;
191  float *f1, *f2;
192  struct LP_IMAGE_INFO *first, *second;
193  struct LP_IMAGE_INFO sfirst, ssecond;
194 
195  if(strlen(args->secondArg) && strlen(args->thirdArg))
196  {
197  f = atoi(args->secondArg+1);
198  s = atoi(args->thirdArg+1);
199 
200  if((f >= 0 && f < MAX_IMAGES) && (s >=0 && s < MAX_IMAGES))
201  {
202  if(f == s)
203  {
204  st_fail("doImageDiff(): First and Second Holders must be different");
205  return(1);
206  }
207 
208  first = findImage(f);
209  second = findImage(s);
210 
211  if(first && second)
212  {
213  bcopy((char *)first, &sfirst, sizeof(sfirst));
214  bcopy((char *)second, &ssecond, sizeof(ssecond));
215 
216  if(!strcmp(first->name, "H0"))
217  {
218  alreadyH0 = 1;
219  }
220 
221  if(!strcmp(second->name, "H0"))
222  {
223  alreadyH0 = 2;
224 
225  /* Swap them */
226  first = &ssecond;
227  second = &sfirst;
228  }
229 
230  /* Allow up to 60% of channel spacing */
231  if(fabs(first->head.restfreq - second->head.restfreq) > fabs(first->head.freqres * 0.6))
232  {
233  st_fail("doImageDiff(): Rest Freq's diff: %f > %f", fabs(first->head.restfreq-second->head.restfreq),
234  fabs(first->head.freqres * 0.6));
235  return(1);
236  }
237 
238  if(fabs(first->head.freqres - second->head.freqres) > 0.01)
239  {
240  st_fail("doImageDiff(): Backend Resolutions differ: %.3f %.3f", first->head.freqres, second->head.freqres);
241  return(1);
242  }
243 
244  if(first->head.noint != second->head.noint)
245  {
246  st_fail("doImageDiff(): Number of Channels differ: %.0f %.0f", first->head.noint, second->head.noint);
247  return(1);
248  }
249 
250  if(strncmp(first->head.object, second->head.object, 16))
251  {
252  st_fail("doImageDiff(): Source names differ: %.16s %.16s", first->head.object, second->head.object);
253  return(1);
254  }
255 
256  st_report("doImageDiff(): Diffing %s & %s", first->name, second->name);
257 
258  /* Copy 'first' into H0; maybe */
259 
260  if(alreadyH0 == 0) /* first & second args a NOT H0 */
261  {
262  _copyImage(first, &H0);
263  }
264 
265  /* From now on the H0 contains the 'first' image */
266 
267  f1 = H0.yplot1;
268  f2 = second->yplot1;
269 
270  for(i=0;i<H0.head.noint;i++, f1++, f2++)
271  {
272  *f1 = *f1 - *f2;
273  }
274 
275  H0.ainfo.bscan = H0.head.scan;
276  H0.ainfo.escan = second->head.scan;
277  H0.ainfo.scanct = 1;
278 
279  _h0(); /* Point to H0 for replot */
280  }
281  else
282  {
283  st_fail("doImageDiff(): Unable to locate First or Second headers");
284  }
285  }
286  }
287 
288  return(0);
289 }
290 
291 
292 /* Divide one array into another */
294 {
295  int f, s, i, alreadyH0 = 0;
296  float *f1, *f2;
297  struct LP_IMAGE_INFO *first, *second;
298  struct LP_IMAGE_INFO sfirst, ssecond;
299 
300  if(strlen(args->secondArg) && strlen(args->thirdArg))
301  {
302  f = atoi(args->secondArg+1);
303  s = atoi(args->thirdArg+1);
304 
305  if((f >= 0 && f < MAX_IMAGES) && (s >=0 && s < MAX_IMAGES))
306  {
307  if(f == s)
308  {
309  st_fail("doImageDiv(): First and Second Holders must be different");
310  return(1);
311  }
312 
313  first = findImage(f);
314  second = findImage(s);
315 
316  if(first && second)
317  {
318  bcopy((char *)first, &sfirst, sizeof(sfirst));
319  bcopy((char *)second, &ssecond, sizeof(ssecond));
320 
321  if(!strcmp(first->name, "H0"))
322  {
323  alreadyH0 = 1;
324  }
325 
326  if(!strcmp(second->name, "H0"))
327  {
328  alreadyH0 = 2;
329 
330  /* Swap them */
331  first = &ssecond;
332  second = &sfirst;
333  }
334 
335  /* Allow up to 60% of channel spacing */
336  if(fabs(first->head.restfreq - second->head.restfreq) > fabs(first->head.freqres * 0.6))
337  {
338  st_fail("doImageDiv(): Rest Freq's diff: %f > %f", fabs(first->head.restfreq-second->head.restfreq),
339  fabs(first->head.freqres * 0.6));
340  return(1);
341  }
342 
343  if(fabs(first->head.freqres - second->head.freqres) > 0.01)
344  {
345  st_fail("doImageDiv(): Backend Resolutions differ: %.3f %.3f", first->head.freqres, second->head.freqres);
346  return(1);
347  }
348 
349  if(first->head.noint != second->head.noint)
350  {
351  st_fail("doImageDiv(): Number of Channels differ: %.0f %.0f", first->head.noint, second->head.noint);
352  return(1);
353  }
354 
355  if(strncmp(first->head.object, second->head.object, 16))
356  {
357  st_fail("doImageDiv(): Source names differ: %.16s %.16s", first->head.object, second->head.object);
358  return(1);
359  }
360 
361  st_report("doImageDiv(): Dividing %s & %s", first->name, second->name);
362 
363  /* Copy 'first' into H0; maybe */
364 
365  if(alreadyH0 == 0) /* first & second args a NOT H0 */
366  {
367  _copyImage(first, &H0);
368  }
369 
370  /* From now on the H0 contains the 'first' image */
371 
372  f1 = H0.yplot1;
373  f2 = second->yplot1;
374 
375  for(i=0;i<H0.head.noint;i++, f1++, f2++)
376  {
377  if(*f2)
378  {
379  *f1 = *f1 / *f2;
380  }
381  else
382  {
383  st_print("doImageDiv(): Channel %d has zero in the divisor\n", i);
384  *f2 = H0.head.tcal;
385  }
386  }
387 
388  H0.ainfo.bscan = H0.head.scan;
389  H0.ainfo.escan = second->head.scan;
390  H0.ainfo.scanct = 1;
391 
392  _h0(); /* Point to H0 for replot */
393  }
394  else
395  {
396  st_fail("doImageDiv(): Unable to locate First or Second headers");
397  }
398  }
399  }
400 
401  return(0);
402 }
403 
404 
405 int doHalves()
406 {
407  subArgs(2, "H1");
408  subArgs(3, "H2");
409 
410  doSum();
411 
412  doAutoScale();
413 
414  return(0);
415 }
float yplot1[MAX_CHANS]
Definition: image.h:110
double scan
Definition: header.h:35
int _copyImage(struct LP_IMAGE_INFO *i1, struct LP_IMAGE_INFO *i2)
Definition: image.c:329
int first
Definition: dbase.c:106
int _h0()
Definition: image.c:93
struct PARSE_STACK * args
Definition: parse.h:196
double noint
Definition: header.h:211
struct LP_IMAGE_INFO H0
Definition: image.c:63
float escan
Definition: image.h:53
char name[32]
Definition: image.h:73
double tcal
Definition: header.h:202
int doHalves(void)
Definition: sum.c:405
int doImageDiff(void)
Definition: sum.c:188
float bscan
Definition: image.h:52
int doImageDiv(void)
Definition: sum.c:293
char * thirdArg
Definition: parse.h:182
struct HEADER head
Definition: image.h:104
void st_fail()
int strcmp()
struct ACCUM_INFO ainfo
Definition: image.h:100
double stsys
Definition: header.h:203
int doSum(void)
Definition: sum.c:61
int copyImage()
Definition: image.c:275
void st_report()
#define MAX_IMAGES
Definition: defines.h:34
int doAutoScale()
Definition: linuxpops.c:2788
void subArgs(int wt, const char *fmt,...)
Definition: parse.c:430
double inttime
Definition: header.h:210
int scanct
Definition: image.h:51
char * secondArg
Definition: parse.h:181
void st_print()
double effint
Definition: header.h:218
char object[16]
Definition: header.h:40
double restfreq
Definition: header.h:198
double freqres
Definition: header.h:199
struct LP_IMAGE_INFO * findImage(int i)
Definition: image.c:233