Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stats.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 #include "channels.h"
62 
63 #include "proto.h"
64 #include "caclib_proto.h"
65 
66 int feed;
67 
69 
70 int mySort(a, b)
71 struct CHANNELS *a;
72 struct CHANNELS *b;
73 {
74  return( (*a).x < (*b).x ? 1 : (*a).x > (*b).x ? -1 : 0 );
75 }
76 
77 
78 /* Prepare & flag bad channel data */
79 int prepareData(p1, p2)
80 struct SCAN_INFO *p1;
81 struct SCAN_INFO *p2;
82 {
83  int i;
84 
85  st_report("PD: Preparing Data");
86 
87  p1->len = p1->head.datalen / sizeof(float);
88  p2->len = p2->head.datalen / sizeof(float);
89 
90 /* Assume all data good for the first pass */
91  for(i=0;i<p1->len;i++)
92  {
93  if(fabs(p1->data[i]) > 1.0e-20)
94  {
95  p1->good[i] = 1;
96  }
97  else
98  {
99  p1->good[i] = 0;
100  st_report("PD: Flagging X-Chan %3d: Value: %10.6f", i, p1->data[i]);
101  }
102 
103  if(fabs(p2->data[i]) > 1.0e-20)
104  {
105  p2->good[i] = 1;
106  }
107  else
108  {
109  p2->good[i] = 0;
110  st_report("PD: Flagging Y-Chan %3d: Value: %10.6f", i, p2->data[i]);
111  }
112  }
113 
114  return(0);
115 }
116 
117 
118 /* Find mean of datasets */
119 int findMean(p1, p2, pass)
120 struct SCAN_INFO *p1;
121 struct SCAN_INFO *p2;
122 int pass;
123 {
124  int i, j;
125 
126  st_report(" ");
127 
128  p1->mean = p2->mean = 0.0;
129 
130  st_report("CM: Computing Pass: %d Mean", pass);
131 
132  for(i=0,j=0;i<p1->len;i++)
133  {
134  if(p1->good[i] && p2->good[i])
135  {
136  p1->mean += p1->data[i];
137  p2->mean += p2->data[i];
138  j++;
139  }
140  }
141 
142  p1->mean /= (double)j;
143  p2->mean /= (double)j;
144 
145  st_report("CM: First-Pass: Mean-X = %10.6f, Mean-Y = %10.6f, %4d Good Channels",
146  p1->mean, p2->mean, j);
147 
148  return(j);
149 }
150 
151 
152 int remove10X(p1, p2, pass)
153 struct SCAN_INFO *p1;
154 struct SCAN_INFO *p2;
155 int pass;
156 {
157  int i;
158 
159  st_report("Removing any Channels 10X Mean: Pass %d", pass);
160  /* First cut at removing wild bad channels */
161  for(i=0;i<p1->len;i++)
162  {
163  if(p1->good[i])
164  {
165  /* If any channel is 10 time worst then mean */
166  if((fabs(p1->data[i]) / fabs(p1->mean)) > 10.0)
167  {
168  st_report("Flagging X-Chan %3d: Value: %10.6f: Factor: %.2f",
169  i, p1->data[i], fabs(p1->data[i]) / fabs(p1->mean));
170  p1->good[i] = 0; /* Flag as bad */
171  }
172  }
173 
174  if(p2->good[i])
175  {
176  if((fabs(p2->data[i]) / fabs(p2->mean)) > 10.0)
177  {
178  st_report("Flagging Y-Chan %3d: Value: %10.6f: Factor: %.2f",
179  i, p2->data[i], fabs(p2->data[i]) / fabs(p2->mean));
180  p2->good[i] = 0; /* Flag as bad */
181  }
182  }
183  }
184 
185  return(0);
186 }
187 
188 
190 {
191  int i, j, n, m, good, write_good, g;
192  double linear;
193  struct SCAN_INFO *p1, *p2;
194  struct CHANNELS *chans;
195 
196 
197  st_report("Entering correlateScans(): %.2f", h0->head.scan);
198 
199  bzero((char *)&dataset, sizeof(dataset));
200 
201  p1 = &dataset.info[0];
202  p2 = &dataset.info[1];
203  chans = &dataset.chans[0];
204 
205  /* Load the data into the struct */
206 
207  /* Channel #1 */
208  bcopy((char *)&h0->head, (char *)&p1->head, sizeof(struct HEADER));
209  bcopy((char *)&h0->head, (char *)&p2->head, sizeof(struct HEADER));
210 
211  p1->len = h0->head.noint;
212  for(i=0;i<p1->len;i++)
213  {
214  p1->data[i] = h0->yplot1[i];
215  }
216  p1->rms = (2.0 * p1->head.stsys) / sqrt(p1->head.inttime * fabs(p1->head.freqres * 1e6));
217 
218  /* Channel #2 */
219  p2->len = h0->head.noint;
220  for(i=0;i<p2->len;i++)
221  {
222  p2->data[i] = h0->yplot2[i];
223  }
224  p2->rms = (2.0 * p2->head.stsys) / sqrt(p2->head.inttime * fabs(p2->head.freqres * 1e6));
225 
226  prepareData(p1, p2);
227 
228  good = findMean(p1, p2, 1);
229 
230  /* Now loop until no change in mean's or 5 times */
231  for(m=0;m<5;m++)
232  {
233  remove10X(p1, p2, 2+m);
234 
235  g = findMean(p1, p2, 2+m);
236 
237  if(g == good) /* No change */
238  {
239  st_report("Converged on %d tries", m+2);
240  break;
241  }
242  good = g;
243  }
244 
245  st_report(" ");
246  st_report("Now Remove any Channels 5X the rms");
247 
248  /* If still good, copy to sort array */
249 
250  n = p1->len;
251  for(i=0,j=0;i<n;i++)
252  {
253  if(p1->good[i] && p2->good[i]) /* Already Flagged bad? */
254  {
255  if((p1->data[i] > (p1->mean - p1->rms * 5.0)) && (p1->data[i] < (p1->mean + p1->rms * 5.0)))
256  {
257  if((p2->data[i] > (p2->mean - p2->rms * 5.0)) && (p2->data[i] < (p2->mean + p2->rms * 5.0)))
258  {
259  chans[j].x = (double)p1->data[i];
260  chans[j].y = (double)p2->data[i];
261  chans[j].c = (double)i;
262  chans[j].good = 1;
263  j++;
264  }
265  else
266  {
267  st_report("Y-Channel %4d: %10.6f > Mean-Y: %10.6f", i, p2->data[i], p2->mean*5.0);
268  }
269  }
270  else
271  {
272  st_report("X-Channel %4d: %10.6f > Mean-X: %10.6f", i, p1->data[i], p1->mean*5.0);
273  }
274  }
275  else
276  {
277  st_report("Skipping Already Flagged Channel: %4d, %14.6f %14.6f",
278  i, p1->data[i], p2->data[i]);
279  }
280  }
281 
282  /* From now on only use j channels */
283 
284  st_report(" ");
285  st_report("Sorting %d channels", j);
286 
287  qsort(chans, j, sizeof(struct CHANNELS), (__compar_fn_t)mySort);
288 
289  for(i=0;i<j;i++)
290  {
291  dataset.xd[i] = chans[i].x;
292  dataset.yd[i] = chans[i].y;
293  dataset.sig[i] = 1.0;
294  }
295 
296  st_report(" ");
297  st_report("Fitting linear regression using grace_fit()");
298 
300 
301 // linear_regression(j, dataset.xd, dataset.yd, dataset.coeff);
302 
303  st_report("Ignoring Results with vaules 1.5 times rms: %10.6f", p2->rms);
304 
305  p2->rms *= 1.5;
306 
307  /* Copy results into H3 */
308 
309  hzero(&H3); /* Clear it out */
310  _copyImage(h0, &H3);
311 
312  write_good = 0;
313  for(i=0;i<j;i++)
314  {
315  if(!chans[i].good)
316  {
317  st_report("Found a Bad one: %d:", chans[i].c);
318 
319  continue;
320  }
321 
322  linear = dataset.coeff[1] * chans[i].x + dataset.coeff[0];
323 
324  if(chans[i].y < linear-p2->rms)
325  {
326  st_report("Ignoring channel[%4d]: data %10.6f < %10.6f",
327  (int)chans[i].c, chans[i].y, linear-p2->rms);
328  continue;
329  }
330 
331  if(chans[i].y > linear+p2->rms)
332  {
333  st_report("Ignoring channel[%4d]: data %10.6f > %10.6f",
334  (int)chans[i].c, chans[i].y, linear+p2->rms);
335  continue;
336  }
337 
339  {
340  st_print("# Channel %4d\n", (int)chans[i].c);
341  st_print("%10.6f %10.6f %10.6f %10.6f %10.6f\n",
342  chans[i].x,
343  chans[i].y,
344  linear,
345  linear + p2->rms,
346  linear - p2->rms);
347  }
348 
349  H3.bplot[i] = chans[i].x; /* Put the x-axis in the bplot array */
350  H3.yplot1[i] = chans[i].y;
351  H3.yplot2[i] = linear;
352  H3.yplot3[i] = linear + p2->rms;
353  H3.yplot4[i] = linear - p2->rms;
354 
355  write_good++;
356  }
357 
358  H3.mode = MODE_XCOR;
359  H3.plotwin = 1;
360  H3.narray = 4;
361  H3.nsize = j * sizeof(float);
362  H3.pinfo.bplot = j;
363 
364  _copyImage(&H3, &H0); /* Copy it int the default holder */
365 
366  _h0(); /* Point to H0 */
367 
369 
370  st_report("Wrote %d data points", write_good);
371 
372  return(0);
373 }
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int plotwin
Definition: image.h:78
double scan
Definition: header.h:35
int hzero(struct LP_IMAGE_INFO *image)
Definition: image.c:616
int _copyImage(struct LP_IMAGE_INFO *i1, struct LP_IMAGE_INFO *i2)
Definition: image.c:329
int _h0()
Definition: image.c:93
double mean
Definition: channels.h:6
int findMean(struct SCAN_INFO *p1, struct SCAN_INFO *p2, int pass)
Definition: stats.c:119
double noint
Definition: header.h:211
double datalen
Definition: header.h:34
#define MODE_XCOR
Definition: defines.h:92
struct CHANNELS chans[MAX_CHANS]
Definition: channels.h:33
struct LP_IMAGE_INFO H0
Definition: image.c:63
int good
Definition: channels.h:20
int nsize
Definition: image.h:80
#define VERBOSE_STATS
Definition: defines.h:72
Definition: header.h:13
int prepareData(struct SCAN_INFO *p1, struct SCAN_INFO *p2)
Definition: stats.c:79
struct PLOT_INFO pinfo
Definition: image.h:102
float yplot2[MAX_CHANS]
Definition: image.h:111
double xd[MAX_CHANS]
Definition: channels.h:28
int correlateScans(void)
Definition: stats.c:189
struct HEADER head
Definition: image.h:104
double y
Definition: channels.h:18
int fitcurve(double *x, double *y, int n, int ideg, double *coeff)
Definition: grace_fit.c:239
double c
Definition: channels.h:19
int len
Definition: channels.h:8
float data[MAX_CHANS]
Definition: channels.h:9
int mode
Definition: image.h:76
struct DATASET dataset
Definition: stats.c:68
int feed
Definition: stats.c:66
double stsys
Definition: header.h:203
int bplot
Definition: image.h:35
void st_report()
double sig[MAX_CHANS]
Definition: channels.h:30
double rms
Definition: beam.h:5
double x
Definition: channels.h:17
double yd[MAX_CHANS]
Definition: channels.h:29
double inttime
Definition: header.h:210
double coeff[16]
Definition: channels.h:27
struct LP_IMAGE_INFO H3
Definition: image.c:66
int remove10X(struct SCAN_INFO *p1, struct SCAN_INFO *p2, int pass)
Definition: stats.c:152
int narray
Definition: image.h:79
int sendImageStruct(struct LP_IMAGE_INFO *image, int mode)
Definition: image.c:820
int mySort(struct CHANNELS *a, struct CHANNELS *b)
Definition: stats.c:70
float bplot[MAX_CHANS]
Definition: image.h:116
void st_print()
#define m(i)
int good[MAX_CHANS]
Definition: channels.h:10
double rms
Definition: channels.h:7
double freqres
Definition: header.h:199
struct HEADER head
Definition: channels.h:11
struct SCAN_INFO info[2]
Definition: channels.h:32
float yplot3[MAX_CHANS]
Definition: image.h:112
int verboseLevel
Definition: linuxpops.c:80
float yplot4[MAX_CHANS]
Definition: image.h:113