Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sptip.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 static float sinEl[] = {2.6, 2.3, 2.0, 1.7, 1.4, 1.1, 1.4, 1.7, 2.0, 2.3, 2.6};
62 static float sqrarg;
63 
65 float *ydata;
66 int dl;
67 float *data;
68 struct HEADER *h;
69 {
70  int i,j;
71 
72  if(env->issmt)
73  {
74  for(i=0,j=0;i<dl;i+=2)
75  {
76  ydata[j] = (data[i] + data[i+1]) / 2.0;
77  j++;
78  }
79  }
80  else
81  {
82  if(h->nophase == 4)
83  {
84  for(i=0,j=0;i<dl;i+=4)
85  {
86  ydata[j] = (data[i] + data[i+1] + data[i+2] + data[i+3]) / 4.0;
87  j++;
88  }
89  }
90  else
91  {
92  for(i=0,j=0;i<dl;i+=2)
93  {
94  ydata[j] = (data[i] + data[i+1]) / 2.0;
95  j++;
96  }
97  }
98  }
99 
100  return(j);
101 }
102 
103 
104 int sptip_display_sptip(tau0, siga, sigb, data, h, plot, chan)
105 float *tau0, *siga, *sigb;
106 float *data;
107 struct HEADER *h;
108 int plot, chan;
109 {
110  float xdata[MAX_CONT_SAMPLES];
111  float ydata[MAX_CONT_SAMPLES];
112  float sig[MAX_CONT_SAMPLES];
113 
114  float a1,b1,siga1,sigb1,chi2_1,q1,
115  a2,b2,siga2,sigb2,chi2_2,q2;
116 
117  int i, j, k, dlen;
118  float sinel[11], delta_airmass, upper_airmass, lower_airmass;
119 
120  dlen = (int)h->noint;
121 
122  zero_check("Sptip", dlen, data);
123 
124  dlen = sptip_bin_data(&ydata[0], dlen, data, h);
125 
126  j = 0;
127  for(i=0;i<dlen;i+=2) /* subtract vane-out from vane-in's */
128  {
129  ydata[j] = ydata[i] - ydata[i+1];
130  j++;
131  }
132 
133  dlen = j;
134 
135  if(dlen != 11)
136  {
137  printf("Problems: datalen != 11: %d\n", dlen);
138 
139  *tau0 = 0.0;
140  *siga = 0.0;
141  *sigb = 0.0;
142 
143  return(0);
144  }
145 
146  for(i=0;i<dlen;i++)
147  {
148  xdata[i] = (float)(i) + 1.0;
149  ydata[i] = log(ydata[i]);
150  sig[i] = 1.0;
151  }
152 
153 
154  if(sptip_min_max(&ydata[0], dlen, h->scan))
155  {
156  *tau0 = 0.0;
157  *siga = 0.0;
158  *sigb = 0.0;
159 
160  return(0);
161  }
162  /* does the header contain the stop and start airmasses ??? */
163  /* if so, use them */
164  if(h->sptip_start > 0.0 && h->sptip_stop > 0.0)
165  {
166  lower_airmass = 1.0 / sin(h->sptip_start * DEG_TO_RAD);
167  upper_airmass = 1.0 / sin(h->sptip_stop * DEG_TO_RAD);
168 
169  delta_airmass = (upper_airmass - lower_airmass) / 5.0;
170 
171  sinel[0] = sinel[10] = lower_airmass;
172  sinel[1] = sinel[ 9] = lower_airmass + delta_airmass * 1.0;
173  sinel[2] = sinel[ 8] = lower_airmass + delta_airmass * 2.0;
174  sinel[3] = sinel[ 7] = lower_airmass + delta_airmass * 3.0;
175  sinel[4] = sinel[ 6] = lower_airmass + delta_airmass * 4.0;
176  sinel[5] = upper_airmass;
177 
178  }
179  else /* if not, it's an old sptip and use the default */
180  {
181  for(k=0;k<11;k++)
182  {
183  sinel[k] = sinEl[k];
184  }
185  }
186 
187  sptip_lsqfit( sinel, &ydata[0], 0, 5, &sig[0], 0, &a1, &b1, &siga1, &sigb1, &chi2_1, &q1);
188  sptip_lsqfit( sinel, &ydata[0], 5, 10, &sig[0], 0, &a2, &b2, &siga2, &sigb2, &chi2_2, &q2);
189 
190  a1 = (a1 + a2) / 2.0;
191  b1 = (b1 + b2) / -2.0;
192 
193  *tau0 = b1;
194  *siga = (siga1 + siga2) / 2.0;
195  *sigb = (sigb1 + sigb2) / 2.0;
196 
197  if(!env->autotau)
198  {
199  st_report("%.2f: %5.4f %f %f", h->scan, *tau0, *siga, *sigb);
200  }
201  else
202  {
203  return(0);
204  }
205 
206  if(chan == 0)
207  {
208  bzero((char *)&cont_sptip_send, sizeof(cont_sptip_send));
209  bcopy((char *)h, (char *)&cont_sptip_send.head1, sizeof(struct HEADER));
210 
214  cont_sptip_send.dlen = dlen;
215 
216  for(i=0;i<dlen;i++)
217  {
218  cont_sptip_send.xdata[i] = xdata[i];
219  cont_sptip_send.ydata1[i] = ydata[i];
220  }
221  }
222  else
223  {
224  bcopy((char *)h, (char *)&cont_sptip_send.head2, sizeof(struct HEADER));
225 
226  for(i=0;i<dlen;i++)
227  {
228  cont_sptip_send.ydata2[i] = ydata[i];
229  }
230  }
231 
232  cont_sptip_send.results[chan].tau0 = *tau0;
233  cont_sptip_send.results[chan].sigmaA = *siga;
234  cont_sptip_send.results[chan].sigmaB = *sigb;
235 
236  cont_sptip_send.results[chan].a = a1;
237  cont_sptip_send.results[chan].b = b1;
238 
239  if(xgraphic && chan == 1)
240  {
242  validate(SP_TIP);
243 
244  return(0);
245  }
246 
247  validate(SP_TIP);
248 
249  return(1);
250 }
251 
252 
254 {
255  int save;
256 
257  save = env->autotau;
258 
259  env->autotau = 0;
260 
261  doSpTip();
262 
263  env->autotau = save;
264 
265  return(0);
266 }
267 
268 
269 int doSpTip()
270 {
271  int ret, i, j, n = 0;
272  float tau0, siga, sigb, fscan;
273  double t_total = 0.0;
274 
275  fscan = atof(args->secondArg);
276 
277  if(fscan == 0.0)
278  {
279  st_warn("No arg for SPTIP");
280  return(1);
281  }
282 
283  j = -1;
284 
285  fscan = round(fscan);
286 
287  st_report("Looking for all SPTIP scans starting with %.2f", fscan);
288 
289  for(i=0;i<4;i++)
290  {
291  fscan += 0.01;
292 
293  if(!env->autotau)
294  {
295  st_print("%s Looking for: %.4f ", prompt, fscan);
296  fflush(stdout);
297  }
298 
299  ret = read_scan(fscan, env->filename, h0->yplot1, &h0->head, 0);
300  env->curScan = fscan;
301 
302  if(ret > 0)
303  {
304  st_print("Found\n");
305 
306  j++; /* Found one */
307  if(!strncmp(h0->head.obsmode, "CONTSTIP", 8))
308  {
309  if(j == 0)
310  {
311  if(!env->autotau)
312  {
313  st_report(" Scan Tau0 Siga Sigb");
314  }
315  }
316 
317  sptip_display_sptip(&tau0, &siga, &sigb, h0->yplot1, &h0->head, PSPLOT, j);
318 
319  t_total += tau0;
320  n++;
321  }
322  else
323  {
324  st_warn("Scan %.2f is NOT an SPTIP", h0->head.scan);
325  return(1);
326  }
327  }
328  }
329 
330  if(n)
331  {
332  t_total /= (double)n;
333 
334  if(!env->autotau)
335  {
336  st_report("Average: %.4f", t_total);
337  }
338 
339  env->fittedtau = t_total;
340 
341  if(!env->autotau)
342  {
343  if(getYesNoQuit("Use tau0 value of %.3f: y/N ?", env->fittedtau))
344  {
345  useTau();
346  }
347  }
348  else
349  {
350  useTau();
351  }
352  }
353 
354  return(0);
355 }
356 
357 
358 int showTips()
359 {
360 
361  return(0);
362 }
363 
364 
365 int useTau()
366 {
367  if(env->fittedtau != 0.0)
368  {
369  if(!env->autotau)
370  {
371  st_report("Using %.3f Maunal Tau", env->fittedtau);
372  }
373  else
375  {
376  st_report("DEBUG: Using Automatic tau0 = %.3f from scan %.0f",
377  env->fittedtau, env->curScan);
378  }
379 
380  env->mantau0 = env->fittedtau;
381  }
382  else
383  {
384  st_warn("No Current Tau to Use");
385  }
386 
387  return(0);
388 }
389 
390 
391 int cont_display_sptip(image, plot)
392 struct LP_IMAGE_INFO *image;
393 int plot;
394 {
395  printf("\nSPTIP Display Not Functional Yet\n\n");
396 
397  return(0);
398 }
399 
400 
401 #define SQR(a) (sqrarg=(a),sqrarg*sqrarg)
402 
403 int sptip_lsqfit(x, y, bdata, edata, sig, mwt, a, b, siga, sigb, chi2, q)
404 float x[], y[], sig[], *a, *b, *siga, *sigb, *chi2, *q;
405 int bdata, edata, mwt;
406 {
407  int i;
408  float wt, t, sxoss, sx=0.0, sy=0.0,
409  st2 = 0.0, ss, sigdat;
410 
411  *b = 0.0;
412 
413  if(mwt)
414  {
415  ss=0.0;
416 
417  for(i=bdata;i<edata;i++)
418  {
419  wt = 1.0 / SQR(sig[i]);
420  ss += wt;
421  sx += x[i] * wt;
422  sy += y[i] * wt;
423  }
424  }
425  else
426  {
427  for(i=bdata;i<edata;i++)
428  {
429  sx += x[i];
430  sy += y[i];
431  }
432 
433  ss=edata-bdata;
434  }
435 
436  sxoss=sx/ss;
437 
438  if(mwt)
439  {
440  for(i=bdata;i<edata;i++)
441  {
442  t=(x[i]-sxoss)/sig[i];
443  st2 += t*t;
444  *b += t*y[i]/sig[i];
445  }
446  }
447  else
448  {
449  for(i=bdata;i<edata;i++)
450  {
451  t = x[i] - sxoss;
452  st2 += t*t;
453  *b += t *y[i];
454  }
455  }
456 
457  *b /= st2;
458  *a = (sy - sx * (*b)) / ss;
459 
460  *siga = sqrt((1.0 + sx*sx / (ss * st2)) / ss);
461  *sigb = sqrt( 1.0 / st2);
462 
463  *chi2 = 0.0;
464 
465  if(mwt == 0)
466  {
467  for(i=bdata;i<edata;i++)
468  {
469  *chi2 += SQR(y[i]-(*a)-(*b)*x[i]);
470  }
471 
472  *q=1.0;
473 
474  sigdat=sqrt((*chi2)/(edata-bdata-2));
475 
476  *siga *= sigdat;
477  *sigb *= sigdat;
478  }
479 
480  return(0);
481 }
482 
483 #undef SQR
484 
486 float *ydata;
487 int dl;
488 float scan;
489 {
490  int i;
491 
492  if(ydata == NULL)
493  {
494  return(1);
495  }
496  /* redetermine min and max */
497  env->datamax = -9999999999.9;
498  env->datamin = 9999999999.0;
499 
500  for(i=0;i<dl;i++)
501  {
502  if(ydata[i] > env->datamax)
503  {
504  env->datamax = ydata[i];
505  }
506 
507  if(ydata[i] < env->datamin)
508  {
509  env->datamin = ydata[i];
510  }
511  }
512 
513  if(fabs(env->datamax - env->datamin) < DATASPREAD)
514  {
515  st_fail("Scan #%7.2f is all %.4f", scan, env->datamin);
516  env->datamin -= 1.0;
517  env->datamax += 1.0;
518 
519  return(1);
520  }
521 
522  return(0);
523 }
struct SOCK * xgraphic
Definition: linuxpops.c:75
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
struct SCAN_SUM_SEND ss
Definition: scansum.c:61
int autotau
Definition: env.h:25
double scan
Definition: header.h:35
int ons
Definition: env.h:87
double curScan
Definition: env.h:178
#define PSPLOT
Definition: defines.h:30
int doSpTip(void)
Definition: sptip.c:269
char * prompt
Definition: makemem.c:101
struct PARSE_STACK * args
Definition: parse.h:196
double noint
Definition: header.h:211
int showTips(void)
Definition: sptip.c:358
int scan
double sqrarg
Definition: gridmap.c:65
double nophase
Definition: header.h:66
int read_scan()
Definition: header.h:13
float ydata1[64]
Definition: accum.h:145
#define SP_TIP
Definition: observing_par.h:49
int issmt
Definition: env.h:63
float tau0
Definition: accum.h:124
int zero_check(char *who, int dl, float *data)
Definition: cont.c:2498
double datamin
Definition: env.h:181
int sptip_min_max(float *ydata, int dl, double scan)
double sptip_stop
Definition: header.h:164
struct CONTINUUM_SPTIP cont_sptip_send
Definition: cont.c:64
int getYesNoQuit()
struct ENVIRONMENT * env
Definition: linuxpops.c:71
#define SEND_SPTIP
Definition: defines.h:135
struct HEADER head
Definition: image.h:104
#define DEG_TO_RAD
Definition: cactus.h:48
double datamax
Definition: env.h:180
#define VERBOSE_CALIBRATE
Definition: defines.h:67
void st_warn()
int sptip_lsqfit(float x[], float y[], int bdata, int edata, float sig[], int mwt, float *a, float *b, float *siga, float *sigb, float *chi2, float *q)
char obsmode[8]
Definition: header.h:41
struct TIP_RESULTS results[4]
Definition: accum.h:139
void st_fail()
int sendCont(int mode)
Definition: cont.c:1827
#define DATASPREAD
Definition: defines.h:23
float b
Definition: accum.h:128
int sptip_display_sptip(float *tau0, float *siga, float *sigb, float *data, struct HEADER *h, int plot, int chan)
Definition: sptip.c:104
double sptip_start
Definition: header.h:163
int plotwin
Definition: env.h:129
void st_report()
float sigmaA
Definition: accum.h:125
float bdata[MAX_ATM_ELEMENTS]
Definition: atm.c:94
float xdata[MAX_ATM_ELEMENTS]
Definition: atm.c:90
struct HEADER head2
Definition: accum.h:142
struct HEADER head1
Definition: accum.h:141
int cont_display_sptip(struct LP_IMAGE_INFO *image, int plot)
Definition: sptip.c:391
float ydata2[64]
Definition: accum.h:146
double mantau0
Definition: env.h:224
int validate(int which)
Definition: linuxpops.c:164
char * secondArg
Definition: parse.h:181
void st_print()
#define MAX_CONT_SAMPLES
Definition: accum.h:26
float ydata[MAX_ATM_ELEMENTS]
Definition: atm.c:91
float xdata[64]
Definition: accum.h:144
int useTau(void)
Definition: sptip.c:365
float a
Definition: accum.h:127
int cmd_doSpTip(void)
Definition: sptip.c:253
#define SQR(a)
Definition: sptip.c:401
float sigmaB
Definition: accum.h:126
char filename[256]
Definition: env.h:9
double fittedtau
Definition: env.h:194
int sptip_bin_data(float *ydata, int dl, float *data, struct HEADER *h)
Definition: sptip.c:64
int verboseLevel
Definition: linuxpops.c:80