Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tsys.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 /* Elev Min & Max */
62 static double elmin = 10.0;
63 static double elmax = 90.0;
64 
65 /* Freq Min & Max */
66 static double Fmin = 68.0;
67 static double Fmax = 900.0;
68 
69 /* Tsys Max */
70 static double tmax = 2600.0;
71 
72 static char tsys_tok[40][40];
73 static int tsys_num = 0;
74 
75 
76 static struct TSYS_SEND tsys_send;
77 static struct TSYS_ELEM tsys_freqs[MAX_TSYS_BINS];
78 
79 /* Sort by freq first; then el */
80 int tsysSort(p1, p2)
81 struct TSYS_ELEM *p1, *p2;
82 {
83 
84  if(p1->freq < p2->freq)
85  {
86  return(-1);
87  }
88  else
89  if(p1->freq > p2->freq)
90  {
91  return(1);
92  }
93  else
94  {
95  return(0);
96  }
97 }
98 
99 
101 {
102  st_report("Sorting %d elements", tsys_num);
103 
104  qsort(&tsys_freqs, tsys_num, sizeof(struct TSYS_ELEM), (__compar_fn_t)tsysSort);
105 
106  return(0);
107 }
108 
109 
111 double f;
112 int first;
113 {
114  int i, indx = -1;
115  int freq;
116 
117  freq = (int)round(f);
118 
119  for(i=0;i<MAX_TSYS_BINS;i++)
120  {
121  if(tsys_freqs[i].used)
122  {
123  if(tsys_freqs[i].freq == freq) /* Found a match */
124  {
125  return(i);
126  }
127  }
128  }
129 
130  /* Mark and Return first unused bin */
131  for(i=0;i<MAX_TSYS_BINS;i++)
132  {
133  if(!tsys_freqs[i].used)
134  {
135  tsys_freqs[i].used = 1;
136  tsys_freqs[i].freq = freq;
137 
138  return(i);
139  }
140  }
141 
142  return(indx);
143 }
144 
145 
146 
150 int tsys_get_tok(edit)
151 char *edit;
152 {
153  int c = 0;
154  char *token;
155 
156  bzero((char *)tsys_tok, sizeof(tsys_tok));
157 
158  token = strtok(edit, "\n\t ");
159  while(token != (char *)NULL)
160  {
161  sprintf(tsys_tok[c++], "%s", token);
162  token = strtok((char *)NULL, "\n\t ");
163  }
164 
165  return(c);
166 }
167 
168 
170 struct TSYS_SEND *tsys;
171 {
172  int i, j, n, freq;
173  char tbuf[256], input[1024], results[256];
174 
175  if(!tsys_send.num)
176  {
177  st_fail("No Tsys data to Select From");
178  return(1);
179  }
180 
181  sprintf(input, "--list --width=300 --height=400 --text=\"Select Frequencies to Display\" --title=\"Select Frequencies\" --checklist --multiple --column=\"Select\" --column=\"Frequency\" --separator=\" \" ");
182 
183  for(i=0;i<tsys->num;i++)
184  {
185  sprintf(tbuf, "%s %03d ", tsys->freqs[i].used ? "TRUE" : "FALSE", tsys->freqs[i].freq);
186 
187  strcat(input, tbuf);
188  }
189 
190  bzero(results, sizeof(results));
191 
192  doZenity(input, results);
193 
194  st_report("You Selected: %s");
195 
196  n = tsys_get_tok(results);
197 
198  if(n > 0)
199  {
200  for(j=0;j<tsys->num;j++)
201  {
202  tsys->freqs[j].used = 0;
203  }
204 
205  for(i=0;i<n;i++)
206  {
207  freq = atoi(tsys_tok[i]);
208 
209  for(j=0;j<tsys->num;j++)
210  {
211  if(freq == tsys->freqs[j].freq)
212  {
213  tsys->freqs[j].used = 1;
214  }
215  }
216  }
217  }
218  else
219  {
220  return(1);
221  }
222 
223  return(0);
224 }
225 
227 {
228  int i, j, indx, el, first = 1;
229  struct HEADER *p;
230  double freq;
231 
232  bzero((char *)&tsys_freqs, sizeof(tsys_freqs));
233 
234  j = 0;
235  p = &sa[0];
236  for(i=0;i<env->numofscans;i++,p++)
237  {
238  if(!meetsCritiria(p, CHECK_BKEND)) /* Only use matching backends */
239  {
240  continue;
241  }
242 
243  freq = p->restfreq / 1000.0;
244 
245  if(freq < Fmin || freq > Fmax)
246  {
247  continue;
248  }
249 
250  if(p->el < elmin || p->el > elmax)
251  {
252  continue;
253  }
254 
255  if(fabs(p->stsys) > tmax)
256  {
257  continue;
258  }
259 
260  indx = findIndex(freq, first);
261  first = 0;
262 
263  if(indx >=0 && indx < MAX_TSYS_BINS)
264  {
265 // st_print("Using freq bin %d, %d\n", indx, tsys_freqs[indx].freq);
266 
267  el = (int)(round(p->el));
268 
269  if(el > 0 && el < 90)
270  {
271  tsys_freqs[indx].tsys[el] += (int)round(fabs(p->stsys));
272  tsys_freqs[indx].cnts[el]++;
273  }
274 
275  j++;
276  }
277  else
278  {
279  st_print("Unable to find freq index for scan %.2f\n", p->scan);
280  }
281  }
282 
283  st_report("Processed %d scans", j);
284 
285  tsys_num = 0;
286  for(i=0;i<MAX_TSYS_BINS;i++)
287  {
288  if(tsys_freqs[i].used)
289  {
290  for(el=0;el<90;el++)
291  {
292  if(tsys_freqs[i].cnts[el])
293  {
294  tsys_freqs[i].tsys[el] /= tsys_freqs[i].cnts[el];
295  }
296  }
297 
298  tsys_num++;
299  }
300  }
301 
302  doTsysSort();
303  doTsysPrepare();
304 
305  return(0);
306 }
307 
308 
309 int doTsys3D()
310 {
311  int i, j, el, first=1, fm=68, fx=900;
312  char fname[256], sysBuf[300];
313  FILE *fp;
314 
315  sprintf(fname, "/tmp/%s/tsys.dat", session);
316 
317  if((fp = fopen(fname, "w")) == NULL)
318  {
319  st_fail("Unable to open %s file", fname);
320 
321  return(0);
322  }
323 
324  j = 0;
325  for(i=0;i<tsys_send.num;i++)
326  {
327  if(tsys_send.freqs[i].used)
328  {
329  fprintf(fp, "\n");
330 
331  if(first)
332  {
333  fm = tsys_send.freqs[i].freq;
334  first = 0;
335  }
336 
337  fx = tsys_send.freqs[i].freq;
338 
339  for(el=0;el<90;el++)
340  {
341  if(tsys_send.freqs[i].elev[el])
342  {
343  fprintf(fp, "%d %d %d\n", tsys_send.freqs[i].freq, el, tsys_send.freqs[i].elev[el]);
344  }
345  }
346  j++;
347  }
348  }
349 
350  fclose(fp);
351 
352  if(j < 2)
353  {
354  st_fail("No Tsys data to plot3d");
355  return(1);
356  }
357 
358  /* Now write out the dem file */
359  sprintf(fname, "/tmp/%s/tsys.dem", session);
360 
361  if((fp = fopen(fname, "w")) == NULL)
362  {
363  st_fail("Unable to open %s file", fname);
364 
365  return(0);
366  }
367 
368  fprintf(fp, " \n");
369  fprintf(fp, "set title \"Tsys vs. Freq\"\n");
370  fprintf(fp, "set xlabel \"Freq\" \n");
371  fprintf(fp, "set ylabel \"Elevation\" \n");
372  fprintf(fp, "set zlabel \"Tsys\" \n");
373  fprintf(fp, "#\n");
374  fprintf(fp, "set key off\n");
375  fprintf(fp, "set border 4095\n");
376 // fprintf(fp, "set hidden3d\n");
377  fprintf(fp, "# \n");
378  fprintf(fp, "set xrange [%d:%d]\n", fm, fx);
379  fprintf(fp, "set yrange [%f:%f]\n", elmin, elmax);
380  fprintf(fp, "set zrange [0:]\n");
381  fprintf(fp, "set xyplane at 0\n");
382  fprintf(fp, "# \n");
383  fprintf(fp, "set samples %d*%d \n", fx-fm, (int)round(elmax-elmin));
384 // fprintf(fp, "set isosamples 60 \n");
385 // fprintf(fp, "# \n");
386  fprintf(fp, "# \n");
387 // fprintf(fp, "set palette model RGB\n");
388 // fprintf(fp, "set palette color\n");
389 // fprintf(fp, "set palette defined ( 0 \"green\", 1 \"blue\", 2 \"red\", 3 \"orange\" )\n");
390 // fprintf(fp, "set palette gamma 1.0\n");
391 // fprintf(fp, "#\n");
392  fprintf(fp, "set terminal x11 enhanced size 690, 733 position 2338, 0\n");
393  fprintf(fp, "splot \"/tmp/%s/tsys.dat\" every 1::0::1024 with lines\n", session);
394  fprintf(fp, "pause -1 \"Hit Return When Finished\"\n");
395 
396  fclose(fp);
397 
398  sprintf(sysBuf, "gnuplot %s", fname);
399 
400  system(sysBuf);
401 
402  return(0);
403 }
404 
405 
406 /* Prepare data for plotting */
408 {
409  int i, el;
410 
411  bzero((char *)&tsys_send, sizeof(tsys_send));
412 
413  strcpy(tsys_send.magic, "TSYS_SEND");
414 
415  tsys_send.plotwin = env->plotwin;
416  tsys_send.xdelay = env->xdelay;
417 
418  for(i=0;i<MAX_TSYS_BINS;i++)
419  {
420  if(tsys_freqs[i].used)
421  {
422  tsys_send.freqs[tsys_send.num].freq = tsys_freqs[i].freq;
423  tsys_send.freqs[tsys_send.num].used = 1;
424 
425  for(el=0;el<90;el++)
426  {
427  if(tsys_freqs[i].tsys[el])
428  {
429  tsys_send.freqs[tsys_send.num].elev[el] = tsys_freqs[i].tsys[el];
430  }
431  }
432 
433  tsys_send.num++;
434  }
435  }
436 
437  if(!tsys_send.num)
438  {
439  st_fail("No Tsys data to Plot");
440  return(1);
441  }
442 
443  return(0);
444 }
445 
446 
448 {
449  if(notSet(PF_A_FLAG))
450  {
451  doTsysGUI(&tsys_send);
452  }
453 
454  sock_write(xgraphic, (char *)&tsys_send, sizeof(tsys_send));
455  st_report("Sent TSYS struct to xgraphics");
456 
457  return(0);
458 }
459 
460 
462 {
463  int i, el, total;
464 
465  if(!tsys_send.num)
466  {
467  st_fail("No Tsys data to List");
468  return(1);
469  }
470 
471  st_report("Freq Used Total");
472 
473  for(i=0;i<tsys_send.num;i++)
474  {
475  total = 0;
476  for(el=0;el<90;el++)
477  {
478  if(tsys_send.freqs[i].elev[el] > 0)
479  {
480  total++;
481  }
482  }
483  st_report(" %3d %3d %3d", tsys_send.freqs[i].freq, tsys_send.freqs[i].used, total);
484  }
485 
486  return(0);
487 }
488 
489 
490 int doTsys()
491 {
492  int len, nt;
493 
494  nt = args->ntokens;
495 
496  /* Short cuts */
497 
498  if(isSet(PF_I_FLAG))
499  {
500  doTsysIngest();
501  }
502 
503  if(isSet(PF_P_FLAG))
504  {
505  doTsysPlot();
506  }
507 
508  if(isSet(PF_S_FLAG))
509  {
510  if(!doTsysGUI(&tsys_send))
511  {
512  sock_write(xgraphic, (char *)&tsys_send, sizeof(tsys_send));
513  }
514  }
515 
516  if(isSet(PF_L_FLAG))
517  {
518  doTsysList();
519  }
520 
521  if(nt > 1) /* If using the short cuts, nt should = 0 */
522  {
523  len = strlen(args->secondArg);
524 
525  if(!strncmp(args->secondArg, "ingest", len))
526  {
527  doTsysIngest();
528  }
529  else
530  if(!strncmp(args->secondArg, "plot", len))
531  {
532  doTsysPlot();
533  }
534  else
535  if(!strncmp(args->secondArg, "3d", len))
536  {
537  doTsys3D();
538  }
539  else
540  if(!strncmp(args->secondArg, "list", len))
541  {
542  doTsysList();
543  }
544  else
545  if(!strncmp(args->secondArg, "select", len))
546  {
547  if(!doTsysGUI(&tsys_send))
548  {
549  sock_write(xgraphic, (char *)&tsys_send, sizeof(tsys_send));
550  }
551  }
552  else
553  if(!strncmp(args->secondArg, "elmin", len))
554  {
555  if(nt > 2)
556  {
557  elmin = atof(args->thirdArg);
558  }
559  else
560  {
561  st_fail("Missing arg for 'tsys elmin'");
562  }
563  }
564  else
565  if(!strncmp(args->secondArg, "elmax", len))
566  {
567  if(nt > 2)
568  {
569  elmax = atof(args->thirdArg);
570  }
571  else
572  {
573  st_fail("Missing arg for 'tsys elmax'");
574  }
575  }
576  else
577  if(!strncmp(args->secondArg, "fmin", len))
578  {
579  if(nt > 2)
580  {
581  Fmin = atof(args->thirdArg);
582  }
583  else
584  {
585  st_fail("Missing arg for 'tsys fmin'");
586  }
587  }
588  else
589  if(!strncmp(args->secondArg, "fmax", len))
590  {
591  if(nt > 2)
592  {
593  Fmax = atof(args->thirdArg);
594  }
595  else
596  {
597  st_fail("Missing arg for 'tsys fmax'");
598  }
599  }
600  else
601  if(!strncmp(args->secondArg, "tmax", len))
602  {
603  if(nt > 2)
604  {
605  tmax = atof(args->thirdArg);
606  }
607  else
608  {
609  st_fail("Missing arg for 'tsys tmax'");
610  }
611  }
612  else
613  if(!strncmp(args->secondArg, "query", len))
614  {
615  st_report("TSYS: El Min: %9.0f", elmin);
616  st_report("TSYS: El Max: %9.0f", elmax);
617  st_report("TSYS: Freq Min: %9.0f", Fmin);
618  st_report("TSYS: Freq Max: %9.0f", Fmax);
619  st_report("TSYS: Tsys Max: %9.0f", tmax);
620  st_report("TSYS: Num Freqs:%9d", tsys_num);
621  }
622  }
623 
624  return(0);
625 }
struct SOCK * xgraphic
Definition: linuxpops.c:75
double scan
Definition: header.h:35
int freq
Definition: tsys.h:34
int sock_write(struct SOCK *s, char *message, int l)
Definition: tsys.h:32
int first
Definition: dbase.c:106
struct RESULTS results[MAX_BKNDS][MAX_BKNDS]
Definition: beam.c:81
struct PARSE_STACK * args
Definition: parse.h:196
char magic[32]
Definition: tsys.h:49
Definition: parse.h:8
int doTsysPrepare(void)
Definition: tsys.c:407
int findIndex(double f, int first)
Definition: tsys.c:110
FILE * fp
Definition: linuxpops.c:70
Definition: header.h:13
int elev[90]
Definition: tsys.h:44
int xdelay
Definition: env.h:144
int doTsysGUI(struct TSYS_SEND *tsys)
Definition: tsys.c:169
struct HEADER * sa
Definition: linuxpops.c:72
int tsys_get_tok(char *edit)
Definition: tsys.c:150
int notSet(int flag)
Definition: parse.c:669
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int doTsys3D(void)
Definition: tsys.c:309
char * thirdArg
Definition: parse.h:182
int doTsys(void)
Definition: tsys.c:490
int freq
Definition: tsys.h:41
int cnts[90]
Definition: tsys.h:36
int doTsysSort(void)
Definition: tsys.c:100
int tsys[90]
Definition: tsys.h:35
int isSet(int flag)
Definition: parse.c:646
int doZenity(char *cmd, char *rst)
Definition: io.c:86
int num
Definition: tsys.h:51
int xdelay
Definition: tsys.h:52
int doTsysIngest(void)
Definition: tsys.c:226
double indx
Definition: header.h:84
void st_fail()
#define MAX_TSYS_BINS
Definition: tsys.h:7
int meetsCritiria(struct HEADER *p, int mask)
Definition: observe.c:90
double stsys
Definition: header.h:203
int doTsysPlot(void)
Definition: tsys.c:447
char session[80]
Definition: strings.c:67
Definition: tsys.h:48
int plotwin
Definition: env.h:129
void st_report()
double el
Definition: header.h:83
int used
Definition: tsys.h:42
int numofscans
Definition: env.h:85
#define CHECK_BKEND
Definition: defines.h:62
int tsysSort(struct TSYS_ELEM *p1, struct TSYS_ELEM *p2)
Definition: tsys.c:80
struct TSYS_FREQ freqs[MAX_TSYS_FREQS]
Definition: tsys.h:54
int doTsysList(void)
Definition: tsys.c:461
char * secondArg
Definition: parse.h:181
void st_print()
int ntokens
Definition: parse.h:173
double restfreq
Definition: header.h:198
int plotwin
Definition: tsys.h:50
int used
Definition: tsys.h:33