Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
alma.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 #include "extern.h"
59 
60 #define MAX_ALMA_CALIBRATORS 12480
61 #define SORT_BY_DATE 1
62 #define SORT_BY_FREQ 2
63 #define SORT_BY_NAME 3
64 #define SORT_BY_JANSKY 4
65 
67 
69 double almaFreq;
70 
72 
74 
75 int alma_get_tok(edit)
76 char *edit;
77 {
78  int c = 0;
79  char *token;
80 
81  bzero((char *)&alma_tok, sizeof(alma_tok));
82 
83  token = strtok(edit, ",\n");
84  while(token != (char *)NULL)
85  {
86  sprintf(alma_tok[c++], "%s", token);
87  token = strtok((char *)NULL, ",\n");
88  }
89 
90  return(c);
91 }
92 
93 
94 int almaSort(p1, p2)
95 struct CALIBRATOR *p1, *p2;
96 {
98  {
99  return(strcmp(p1->date, p2->date));
100  }
101  else
103  {
104  return(strcmp(p1->name, p2->name));
105  }
106  else
108  {
109  if(p1->freq < p2->freq)
110  {
111  return(-1);
112  }
113  else
114  if(p1->freq > p2->freq)
115  {
116  return(1);
117  }
118  else
119  {
120  return(0);
121  }
122  }
123  else
125  {
126  if(p1->jk < p2->jk)
127  {
128  return(-1);
129  }
130  else
131  if(p1->jk > p2->jk)
132  {
133  return(1);
134  }
135  else
136  {
137  return(0);
138  }
139  }
140 
141  return(0);
142 }
143 
144 
145 int sortCalibrators(field)
146 int field;
147 {
148  almaSortField = field;
149 
150  qsort(&calibrators[0], max_calibrators, sizeof(struct CALIBRATOR), (__compar_fn_t)almaSort);
151 
152  return(0);
153 }
154 
155 
156 char angle_tok[40][40];
157 
158 int angle_get_tok(edit)
159 char *edit;
160 {
161  int c = 0;
162  char *token;
163 
164  bzero((char *)&angle_tok, sizeof(angle_tok));
165 
166  token = strtok(edit, " :\n");
167  while(token != (char *)NULL)
168  {
169  sprintf(angle_tok[c++], "%s", token);
170  token = strtok((char *)NULL, " :\n");
171  }
172 
173  return(c);
174 }
175 
176 
177 double angleToDeg(inBuf)
178 char *inBuf;
179 {
180  int n, negflag=0;
181  char buf[256];
182  double decdeg, decmin, decsec, d;
183 
184  strcpy(buf, inBuf);
185 
186  n = angle_get_tok(buf);
187 
188  if(n == 3)
189  {
190  decdeg = atof(angle_tok[0]);
191  decmin = atof(angle_tok[1]);
192  decsec = atof(angle_tok[2]);
193 
194  if(decdeg < 0.0) /* Look for negitive num */
195  {
196  negflag = 1;
197  }
198 
199  decdeg = fabs(decdeg);
200 
201  d = decdeg + decmin / 60.0 + decsec / 3600.0;
202 
203  if(negflag)
204  {
205  d *= -1.0;
206  }
207  }
208  else
209  {
210  d = 0.0;
211  }
212 
213  return(d);
214 }
215 
216 
217 
219 char *name;
220 {
221  int n, j = 0, indx=0;
222  char fname[512], line[1024], *string;
223  FILE *fp;
224  struct CALIBRATOR *p;
225 
226  if(name && strlen(name))
227  {
228  strcpy(fname, name);
229  }
230  else /* Load defaults calibrators */
231  {
232  sprintf(fname, "%s/share/alma_calibrators.dat", pkg_home);
233  }
234 
235  if((fp = fopen(fname, "r") ) <= (FILE *)0)
236  {
237  st_fail("Unable to open file %s", fname);
238  return(0);
239  }
240 
241  bzero((char *)&calibrators[0], sizeof(calibrators));
242 
243  p = &calibrators[0];
244 
245  while((string = fgets(line, sizeof(line), fp)) != NULL)
246  {
247  indx++;
248 
249  if(line[0] == '#')
250  {
251  continue;
252  }
253 
254  n = alma_get_tok(line);
255 
256  if(n == 8)
257  {
258  p->band = atoi(alma_tok[0]);
259 
260  strncpy(p->date, alma_tok[1], strlen(alma_tok[1]));
261  strcmprs(p->date);
262 
263  p->ra = angleToDeg(alma_tok[2]) * 15.0;
264  p->dec = angleToDeg(alma_tok[3]);
265 
266  p->jk = atof(alma_tok[4]);
267  p->errBar = atof(alma_tok[5]);
268  p->freq = atof(alma_tok[6]) / 1e9;
269 
270  strncpy(p->name, alma_tok[7], strlen(alma_tok[7]));
271  strcmprs(p->name);
272 
273  p++;
274  j++;
275 
276  if(j >= MAX_ALMA_CALIBRATORS)
277  {
278  st_fail("Maximum number Calibrators Loaded");
279  max_calibrators = j;
280  fclose(fp);
281 
282  return(1);
283  }
284  }
285  else
286  {
287  st_print("Wrong number of tokens %d, in loadCalibrators(), line %d\n", n, indx);
288  }
289  }
290 
291  fclose(fp);
292 
293  st_report("Loaded %d ALMA Calibrators", j);
294 
295  max_calibrators = j;
296 
297  sortCalibrators(SORT_BY_DATE); /* Sort them by date, then freq */
298 
299  return(0);
300 }
301 
302 
303 
305 struct CALIBRATOR *p;
306 int first;
307 {
308  char rastr[80], decstr[80];
309 
310 
311  if(p->band != almaBand)
312  {
313  return(0);
314  }
315 
316  sexagesimal(p->ra / 15.0, rastr, DDD_MM_SS_S100);
317  sexagesimal(p->dec, decstr, HH_MM_SS);
318 
319 
320  if(first)
321  {
322  st_print("\n");
323  st_print(
324  " Source RA Dec Freq Jy Date-Obs AB\n\n");
325  }
326 
327  st_print("%64.64s %s %s %7.2f %5.2f +/- %4.2f %s %d\n",
328  p->name, rastr, decstr, p->freq, p->jk, p->errBar, p->date, p->band);
329 
330  return(0);
331 }
332 
333 
335 {
336  int i, j, y, m, d, first = 1;
337  char src[80], firstDate[80], lastDate[80], tbuf[256];
338  struct CALIBRATOR *p;
339 
340  bzero((char *)&flux_history, sizeof(flux_history));
341 
342  if(!strncasecmp(env->source, "APS-", 4)) /* Strip off APS- */
343  {
344  strcpy(src, env->source+4);
345  }
346  else
347  {
348  strcpy(src, env->source);
349  }
350 
351  st_report("Plotting ALMA Calibrator %s", src);
352 
353  p = &calibrators[0];
354  j = 0;
355  for(i=0;i<max_calibrators;i++,p++)
356  {
357  if(strcasestr(p->name, src) && /*p->band == almaBand &&*/ (fabs(p->freq-almaFreq) < 5.0))
358  {
359  y = atoi(p->date);
360  m = atoi(p->date+5);
361  d = atoi(p->date+8);
362 
363  makeJulDate(m, d, y-2000, 0.0);
364 
365  sprintf(tbuf, "%04d/%02d/%02d", y, m, d);
366 
367  strcpy(flux_history.history[j].date, tbuf);
368  flux_history.history[j].flux = p->jk;
369 
370  if(first)
371  {
372  strcpy(firstDate, p->date);
373  first = 0;
374  }
375  else
376  {
377  strcpy(lastDate, p->date);
378  }
379 
380  j++;
381  }
382  }
383 
384  if(j < 3)
385  {
386  st_fail("Little or No ALMA calibrators for %s @ %f GHz", src, almaFreq);
387  return(1);
388  }
389  else
390  {
391  st_report("Plotting %d ALMA calibrator measurements", j);
392  st_report("Dated from %s to %s", firstDate, lastDate);
393  }
394 
395  sprintf(flux_history.datestr, "Dated from %s to %s", firstDate, lastDate);
396 
397  if(xgraphic)
398  {
399  st_report("Sending Flux History to Xgraphic");
400 
401  strcpy(flux_history.magic, "FLUX_HISTORY");
402  strcpy(flux_history.name, src);
403 
404  flux_history.num = j;
405 
407 
408  sock_write(xgraphic, (char *)&flux_history, sizeof(flux_history));
409 
411  }
412 
413  return(0);
414 }
415 
416 
418 int band;
419 {
420  int i, first = 1;
421  char raStr[256], decStr[256];
422  struct CALIBRATOR *p;
423  char old_name[80], catname[80];
424  char outBuf[1024], previous[512];
425  FILE *fp;
426 
427  sprintf(catname, "alma_band_%d_cal.cat", band);
428 
429  if((fp = fopen(catname, "w") ) <= (FILE *)0)
430  {
431  st_fail("Unable to open file %s", catname);
432  return(0);
433  }
434 
435  p = &calibrators[0];
436  for(i=0;i<max_calibrators;i++,p++)
437  {
438  if(band == p->band)
439  {
440  if(first)
441  {
442  strncpy(old_name, p->name, 10);
443  first = 0;
444  }
445 
446  sexagesimal(p->ra / 15.0, raStr, DDD_MM_SS_S100);
447  sexagesimal(p->dec, decStr, DDD_MM_SS_S100);
448 
449  sprintf(outBuf, "%s %s J2000.0 %10.10s 0.0 LSR RAD # %d %4.1f %s",
450  raStr, decStr, p->name, p->band, p->jk, p->name);
451 
452  if(strncmp(old_name, p->name, 10))
453  {
454  fprintf(fp, "%s\n", previous);
455  strcpy(old_name, p->name);
456  }
457 
458  strcpy(previous, outBuf);
459  }
460  }
461 
462  st_report("Wrote ALMA Calibrator Source Catalog %s", catname);
463 
464  fclose(fp);
465 
466  return(0);
467 }
468 
469 
471 {
472  int i, j=0, b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0, b8=0, b9=0, b10=0;
473  struct CALIBRATOR *p;
474 
475  p = &calibrators[0];
476  for(i=0;i<max_calibrators;i++,p++)
477  {
478  if(p->band > 0)
479  {
480  switch(p->band)
481  {
482  case 1: b1++; break;
483  case 2: b2++; break;
484  case 3: b3++; break;
485  case 4: b4++; break;
486  case 5: b5++; break;
487  case 6: b6++; break;
488  case 7: b7++; break;
489  case 8: b8++; break;
490  case 9: b9++; break;
491  case 10: b10++; break;
492  }
493 
494  j++;
495  }
496  }
497 
498  st_report("Validate(): Found %d total calibrators out of %d", j, max_calibrators);
499 
500  st_report("Band: 1 2 3 4 5 6 7 8 9 10");
501  st_report("Num: %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d",
502  b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
503 
504  return(0);
505 }
506 
507 
509 {
510  int i, show_all = 0, specific_src = 0, first = 1;
511  double f;
512  char *src = " ", src2[80];
513  struct CALIBRATOR *p;
514 
515  f = env->freq;
516 
517  if(f > ALMA_BAND_2_LOWER && f <= ALMA_BAND_2_UPPER)
518  {
519  almaBand = 2;
520  }
521  else
522  if(f > ALMA_BAND_3_LOWER && f <= ALMA_BAND_3_UPPER)
523  {
524  almaBand = 3;
525  }
526  else
527  if(f > ALMA_BAND_4_LOWER && f <= ALMA_BAND_4_UPPER)
528  {
529  almaBand = 4;
530  }
531  else
532  if(f > ALMA_BAND_5_LOWER && f <= ALMA_BAND_5_UPPER)
533  {
534  almaBand = 5;
535  }
536  else
537  if(f > ALMA_BAND_6_LOWER && f <= ALMA_BAND_6_UPPER)
538  {
539  almaBand = 6;
540  }
541  else
542  if(f > ALMA_BAND_7_LOWER && f <= ALMA_BAND_7_UPPER)
543  {
544  almaBand = 7;
545  }
546  else
547  if(f > ALMA_BAND_8_LOWER && f <= ALMA_BAND_8_UPPER)
548  {
549  almaBand = 8;
550  }
551  else
552  if(f > ALMA_BAND_9_LOWER && f <= ALMA_BAND_9_UPPER)
553  {
554  almaBand = 9;
555  }
556  else
557  if(f > ALMA_BAND_10_LOWER && f <= ALMA_BAND_10_UPPER)
558  {
559  almaBand = 10;
560  }
561 
562  if(strlen(args->secondArg))
563  {
564  if(!strcmp(args->secondArg, "sort"))
565  {
566  if(strlen(args->thirdArg))
567  {
568  if(!strncmp(args->thirdArg, "name", strlen(args->thirdArg)))
569  {
571  }
572  else
573  if(!strncmp(args->thirdArg, "date", strlen(args->thirdArg)))
574  {
576  }
577  else
578  if(!strncmp(args->thirdArg, "freq", strlen(args->thirdArg)))
579  {
581  }
582  else
583  if(!strncmp(args->thirdArg, "jansky", strlen(args->thirdArg)))
584  {
586  }
587 
589  }
590  else
591  {
592  st_fail("Missing arg for 'alma sort'; [name|date|freq|jansky]");
593 
594  printH_FlagHelp("alma");
595  }
596 
597  return(0);
598  }
599  else
600  if(!strcmp(args->secondArg, "plot"))
601  {
602  if(strlen(args->thirdArg))
603  {
604  almaFreq = atof(args->thirdArg);
606  }
607  else
608  {
609  st_fail("Missing arg for 'alma plot freq");
610 
611  printH_FlagHelp("alma");
612  }
613  return(0);
614  }
615  else
616  if(!strcmp(args->secondArg, "validate"))
617  {
619  return(0);
620  }
621  else
622  if(!strncmp(args->secondArg, "catalog", strlen(args->secondArg)))
623  {
624  /* First sort the catalog by date */
625  /* This places the most recent entry for each soure last */
627 
628  /* Now sort by name which puts them in RA order */
630 
632 
633  /* Put the catalog back in date order */
635 
636  return(0);
637  }
638  else
639  if(!strcmp(args->secondArg, "all"))
640  {
641  show_all = 1;
642  }
643  else
644  if(!strcmp(args->secondArg, "reload"))
645  {
646  st_report("Reloading ALMA Calibrators");
647 
648  loadCalibrators(NULL);
649 
650  return(0);
651  }
652  else /* Assume any remaining arg is a source name */
653  {
654  src = args->secondArg;
655  specific_src = 1;
656  }
657  }
658  else
659  {
660  src = env->source;
661  }
662 
663 
664  st_print("Looking for Band-%d: Calibrator: %s\n", almaBand, src);
665 
666  if(!strncasecmp(env->source, "APS-", 4)) /* Strip off APS- */
667  {
668  strcpy(src2, env->source+4);
669  }
670  else
671  {
672  strcpy(src2, env->source);
673  }
674 
675  p = &calibrators[0];
676  for(i=0;i<max_calibrators;i++,p++)
677  {
678  if(show_all) /* Show them all */
679  {
680  showCalibrator(p, first);
681  first = 0;
682 
683  continue;
684  }
685 
686  if(specific_src) /* Show only the passed arg */
687  {
688 // st_print("Looking for Calibrator %s in %s\n", src, p->name);
689  if(strcasestr(p->name, src))
690  {
691  showCalibrator(p, first);
692  first = 0;
693 
694  continue;
695  }
696  }
697  else
698  {
699  if(strcasestr(p->name, src2))
700  {
701  showCalibrator(p, first);
702  first = 0;
703 
704  continue;
705  }
706  }
707  }
708 
709  if(first)
710  {
711  st_fail("No Band-%d Calibrator Found Matching %s or %s", almaBand, src, src2);
712  }
713 
714  return(0);
715 }
struct SOCK * xgraphic
Definition: linuxpops.c:75
int almaSort(struct CALIBRATOR *p1, struct CALIBRATOR *p2)
Definition: alma.c:94
int showCalibrator(struct CALIBRATOR *p, int first)
Definition: alma.c:304
#define ALMA_BAND_7_LOWER
Definition: alma.h:22
char pkg_home[256]
Definition: makemem.c:97
int sock_write(struct SOCK *s, char *message, int l)
char * strcmprs(char *str)
struct FLUX_HISTORY flux_history
Definition: alma.c:73
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
int almaSortField
Definition: alma.c:68
char alma_tok[MAX_TOKENS][MAX_TOKENS]
Definition: alma.c:71
#define ALMA_BAND_6_LOWER
Definition: alma.h:19
double almaFreq
Definition: alma.c:69
char name[80]
Definition: flux.h:31
#define MAX_TOKENS
Definition: defines.h:40
int plotAlmaCalibrator()
Definition: alma.c:334
struct CALIBRATOR calibrators[MAX_ALMA_CALIBRATORS]
Definition: alma.c:66
FILE * fp
Definition: linuxpops.c:70
int printH_FlagHelp(char *cmd)
Definition: help.c:430
#define ALMA_BAND_8_LOWER
Definition: alma.h:25
char name[128]
Definition: commands.h:24
#define ALMA_BAND_10_UPPER
Definition: alma.h:32
int alma_get_tok(char *edit)
Definition: alma.c:75
#define LAST_PLOT_FLUX_HISTORY
Definition: defines.h:166
#define ALMA_BAND_10_LOWER
Definition: alma.h:31
double makeJulDate(int im, int id, int iy, double ut)
#define ALMA_BAND_8_UPPER
Definition: alma.h:26
struct FLUX_HISTORY_ELEMENT history[MAX_FLUX_HISTORY]
Definition: flux.h:36
char magic[32]
Definition: flux.h:30
int createAlmaCat(int band)
Definition: alma.c:417
int sexagesimal(double dang, char *str, int prec)
struct ENVIRONMENT * env
Definition: linuxpops.c:71
#define SORT_BY_NAME
Definition: alma.c:63
double angleToDeg(char *inBuf)
Definition: alma.c:177
#define SORT_BY_JANSKY
Definition: alma.c:64
int almaCalibrator()
Definition: alma.c:508
char * thirdArg
Definition: parse.h:182
char source[64]
Definition: env.h:11
#define ALMA_BAND_5_LOWER
Definition: alma.h:16
char datestr[80]
Definition: flux.h:32
double freq
Definition: commands.h:23
#define SORT_BY_DATE
Definition: alma.c:61
#define MAX_ALMA_CALIBRATORS
Definition: alma.c:60
#define ALMA_BAND_6_UPPER
Definition: alma.h:20
#define SORT_BY_FREQ
Definition: alma.c:62
int plotwin
Definition: flux.h:34
int num
Definition: flux.h:33
#define ALMA_BAND_4_UPPER
Definition: alma.h:14
void st_fail()
#define ALMA_BAND_4_LOWER
Definition: alma.h:13
#define ALMA_BAND_3_UPPER
Definition: alma.h:11
double errBar
Definition: commands.h:22
int strcmp()
int almaBand
Definition: alma.c:68
char angle_tok[40][40]
Definition: alma.c:156
#define ALMA_BAND_2_UPPER
Definition: alma.h:8
#define ALMA_BAND_5_UPPER
Definition: alma.h:17
int max_calibrators
Definition: alma.c:68
int validateCalibrators()
Definition: alma.c:470
int plotwin
Definition: env.h:129
void st_report()
#define ALMA_BAND_2_LOWER
Definition: alma.h:7
int last_plot
Definition: env.c:93
#define DDD_MM_SS_S100
Definition: sex.h:18
#define HH_MM_SS
Definition: sex.h:14
double jk
Definition: commands.h:21
double freq
Definition: env.h:197
#define ALMA_BAND_7_UPPER
Definition: alma.h:23
char date[16]
Definition: flux.h:9
#define ALMA_BAND_9_LOWER
Definition: alma.h:28
char * secondArg
Definition: parse.h:181
int band
Definition: commands.h:18
void st_print()
#define m(i)
int angle_get_tok(char *edit)
Definition: alma.c:158
double ra
Definition: commands.h:19
#define ALMA_BAND_3_LOWER
Definition: alma.h:10
int loadCalibrators(char *name)
Definition: alma.c:218
int sortCalibrators(int field)
Definition: alma.c:145
#define ALMA_BAND_9_UPPER
Definition: alma.h:29
double dec
Definition: commands.h:20
char date[80]
Definition: commands.h:17