Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gridmap.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 
62 
63 char middleRowFname[256];
64 
65 double sqrarg;
66 
69 
70 int doingDive = 0;
71 
72 #define SQR(a) (sqrarg=(a),sqrarg*sqrarg)
73 
74 #define MAX_NUM_BEAM_SEP 2048
75 
76 struct BEAM_MAPS_SEP {
77  int used;
78  int pad;
79  double scan;
80  double seperation;
81  double bw1;
82  double bw2;
83  double peak1;
84  double peak2;
85  double ratio;
86 };
87 
89 
91 
92 struct SCAN_HEADERS {
93  int ver;
94  float scan;
95 
96  struct HEADER head;
97 
98  struct SCAN_HEADERS *next;
99 };
100 
102 
103 
104 struct HEADER *do_addHeader(ver, scan, head)
105 int ver;
106 float scan;
107 struct HEADER *head;
108 {
109  struct HEADER *h;
110 
111  if((h = checkScanHeader(ver, scan)))
112  {
113  return(h);
114  }
115  else
116  {
117  if((h = addScanHeader(ver, scan, head)))
118  {
119  return(h);
120  }
121  else
122  {
123  log_msg("do_addHeader(): Error adding scan %.2f, in ver %d", scan, ver);
124  }
125  }
126 
127  return((struct HEADER *)NULL);
128 }
129 
130 
131 struct HEADER *checkScanHeader(v, s)
132 int v;
133 float s;
134 {
135  struct SCAN_HEADERS *m;
136  float ss;
137 
138  if(scan_headers == NULL)
139  {
140  return((struct HEADER *)NULL);
141  }
142 
143  /* users can pass a scan number like 1234.0, which will ignore all
144  scans with that prefix */
145  ss = (float)(int)s;
146 
147  for(m=scan_headers;m->next; m=m->next)
148  {
149  if(m)
150  {
151  if((m->scan == s || m->scan == ss) && m->ver == v)
152  {
153  return(&m->head);
154  }
155  }
156  }
157 
158  /* Check the last one, if valid */
159  if(m)
160  {
161  if((m->scan == s || m->scan == ss) && m->ver == v)
162  {
163  return(&m->head);
164  }
165  }
166 
167  return((struct HEADER *)NULL);
168 }
169 
170 
171 struct HEADER *addScanHeader(v, s, head)
172 int v;
173 float s;
174 struct HEADER *head;
175 {
176  struct SCAN_HEADERS *m;
177 
178  /* Create a fresh list */
179  if(scan_headers == NULL)
180  {
181  if((scan_headers=(struct SCAN_HEADERS *)malloc((unsigned)(sizeof(struct SCAN_HEADERS))))==NULL)
182  {
183  log_msg("Error in addIgnoreScans malloc(%d)", sizeof(struct SCAN_HEADERS));
184 
185  return((struct HEADER *)NULL);
186  }
187 
188  scan_headers->ver = v;
189  scan_headers->scan = s;
190 
191  bcopy((char *)head, (char *)&scan_headers->head, sizeof(struct HEADER));
192 
193  scan_headers->next = NULL;
194 
195  return(&scan_headers->head);
196  }
197 
198  /* Find next available slot */
199  for(m=scan_headers;m->next; m=m->next)
200  {
201  ;
202  }
203 
204  /* Append new scan */
205  if((m->next=(struct SCAN_HEADERS *)malloc((unsigned)(sizeof(struct SCAN_HEADERS))))==NULL)
206  {
207  log_msg("Error in addIgnoreScans malloc(%d)", sizeof(struct SCAN_HEADERS));
208 
209  return((struct HEADER *)NULL);
210  }
211 
212  m->next->ver = v;
213  m->next->scan = s;
214 
215  bcopy((char *)head, (char *)&m->next->head, sizeof(struct HEADER));
216 
217  m->next->next = NULL;
218 
219  return(&m->next->head);
220 }
221 
222 
223 
225 {
226  struct SCAN_HEADERS *p;
227  struct SCAN_HEADERS *q;
228 
229  for(p=scan_headers; p; p=q)
230  {
231  q = p->next;
232 
233  if(p)
234  {
235  free((char *)p);
236  }
237  }
238 
239  scan_headers = NULL;
240 
241  return(0);
242 }
243 
244 
245 
247 {
248  fprintf(stdout, "%c[0m" , 0x1b);
249  fflush(stdout);
250  fprintf(stdout, "%c[2J", 0x1b);
251  fflush(stdout);
252 
253  return(0);
254 }
255 
256 
257 int gotoxy(x, y)
258 int x, y;
259 {
260  fprintf(stdout, "%c[%d;%dH" , 0x1b, y, x);
261  fflush(stdout);
262 
263  return(0);
264 }
265 
266 
267 int gridmap_goWriteCenterRow(xdata, ydata, aa1, aa2, n, scanno)
268 double *xdata, *ydata, *aa1, *aa2, scanno;
269 int n;
270 {
271  int i;
272  FILE *fp;
273  double g1, g2, y, dyda[4];
274 
275  sprintf(middleRowFname, "/tmp/%s/middle_row.%04.2f.dat", session, scanno);
276 
277  if((fp = fopen(middleRowFname, "w")) == NULL)
278  {
279  fprintf(stderr, "Unable to open output file %s\n", middleRowFname);
280 
281  exit(1);
282  }
283 
284  for(i=0;i<n;i++)
285  {
286  fgauss(xdata[i], aa1, &y, dyda);
287  g1 = y;
288 
289  fgauss(xdata[i], aa2, &y, dyda);
290  g2 = y;
291 
292  fprintf(fp, "%f %f %f %f\n", xdata[i], ydata[i], g1, g2);
293 
294  global_gdata[i] = g1 + g2;
295  }
296 
297  fclose(fp);
298 
299 
300  return(0);
301 }
302 
303 
305 {
306  char tbuf[512];
307 
308  if(strlen(middleRowFname) > 0)
309  {
310  sprintf(tbuf, "show_mid.csh %s %s &", middleRowFname, session);
311  system(tbuf);
312  }
313  else
314  {
315  st_print("No Middle Row to Plot\n");
316  st_print("Have you done a 'gridmap' and a 'mapgauss'?\n");
317  }
318 
319  return(0);
320 }
321 
322 
324 {
325  int x, y;
326  FILE *fp;
327  char outfile[256];
328  struct BEAM_MAP_ELEM *p;
329 
330  if(beam_map.map_h == 0 || beam_map.map_w == 0)
331  {
332  st_fail("No map loaded");
333  return(0);
334  }
335 
336  sprintf(outfile, "/tmp/%s/beam_map.dat", session);
337 
338  if((fp = fopen(outfile, "w")) == NULL)
339  {
340  st_print("Unable to open output file %s\n", outfile);
341 
342  return(1);
343  }
344 
345  for(y=beam_map.plot_yb;y<beam_map.plot_ye;y++)
346  {
347  p = &beam_map.bmes[y][beam_map.plot_xb];
348 
349  if(!beam_map.bmes[y][0].n) /* Row not populated yet? */
350  {
351  continue;
352  }
353 
354  for(x=beam_map.plot_xb;x<beam_map.plot_xe;x++,p++)
355  {
356  fprintf(fp, "%5.2f %5.2f %10.6f\n", p->eloff, p->azoff, p->pval);
357  }
358 
359  fprintf(fp, "\n");
360  }
361 
362  fclose(fp);
363 
364  return(0);
365 }
366 
367 
369 {
370  int x, y;
371  FILE *fp;
372  char outfile[256], source[80];
373  double pvalue;
374  struct BEAM_MAP_ELEM *p;
375  struct HEADER *h;
376 
377  if(beam_map.map_h == 0 || beam_map.map_w == 0)
378  {
379  st_fail("No map loaded");
380  return(0);
381  }
382 
383  p = &beam_map.bmes[beam_map.map_h /2][0];
384  h = p->headers[0]; /* Grab the center row header */
385 
386  if(isSet(PF_A_FLAG)) /* Look for Auto-Name flag */
387  {
388  strncpy(source, h->object, 16);
389  source[16] = '\0';
390  strcmprs(source);
391  sprintf(outfile, "%s-BeamMap-%.1f-IF%1d.csv", source, h->restfreq / 1e3, (int)round(env->if_feed * 100.0));
392  strcmprs(outfile);
393  }
394  else
395  if(args->ntokens > 1) /* Look for a passed filename */
396  {
397  strcpy(outfile, args->secondArg);
398 
399  if(!strstr(outfile, ".csv")) /* check for file-extension */
400  {
401  strcat(outfile, ".csv");
402  }
403  }
404  else /* If not, Use a default */
405  {
406  sprintf(outfile, "beam_map.csv");
407  }
408 
409  if((fp = fopen(outfile, "w")) == NULL)
410  {
411  st_print("Unable to open output file %s\n", outfile);
412 
413  return(1);
414  }
415 
416 
417  fprintf(fp, "# MAP Source (Name): %16.16s\n", h->object);
418  fprintf(fp, "# MAP X-Axis (Cols): %.0f\n", h->noxpts);
419  fprintf(fp, "# MAP Y-Axis (Rows): %.0f\n", h->noypts);
420  fprintf(fp, "# MAP Delta-X (Secs): %.0f\n", h->deltaxr);
421  fprintf(fp, "# MAP Delta-Y (Secs): %.0f\n", h->deltayr);
422  fprintf(fp, "# MAP Freq (GHz ): %.6f\n", h->restfreq / 1e3);
423  fprintf(fp, "# MAP Bk-End : %8.8s\n", h->backend);
424 
425  for(y=0;y<beam_map.map_h;y++)
426  {
427  fprintf(fp, "%.1f, ", (float)y - (float)beam_map.map_h / 2.0 + 0.5);
428 
429  for(x=0;x<beam_map.map_w;x++)
430  {
431  pvalue = beam_map.bmes[y][x].pval;
432 
433  fprintf(fp, "%f", pvalue);
434 
435  if(x < (beam_map.map_w-1))
436  {
437  fprintf(fp, ", ");
438  }
439  }
440 
441  fprintf(fp, "\n");
442  }
443 
445  {
446  st_report("Wrote %d x %d map to %s", beam_map.map_w, beam_map.map_h, outfile);
447  }
448 
449  fclose(fp);
450 
451  return(0);
452 }
453 
454 
456 {
457  int i, x, y, j, n, mid;
458  int plotval = 0, scanct = 0, scans = 0, tsys = 0,
459  rval = 0, itime = 0, offsets = 0, writeit = 0;
460  int xstart, xend, ystart, yend;
461  char fname[256], blanks[80];
462  struct BEAM_MAP_ELEM *p;
463  struct BEAM_MAP *bm;
464  FILE *fp, *saveFp;
465 
466  if(isSet(PF_H_FLAG))
467  {
469 
470  return(0);
471  }
472 
473  if(beam_map.map_h == 0 || beam_map.map_w == 0)
474  {
475  st_fail("No map loaded");
476  return(0);
477  }
478 
479  bm = &beam_map;
480 
481  if(isSet(PF_C_FLAG)) /* Display Scan Counts; default */
482  {
483  scanct = 1;
484  }
485  else
486  if(isSet(PF_S_FLAG)) /* Display Scan Counts; default */
487  {
488  scans = 1;
489  }
490  else
491  if(isSet(PF_R_FLAG)) /* Display Raw Value */
492  {
493  rval = 1;
494  }
495  else
496  if(isSet(PF_P_FLAG)) /* Display Plot Value */
497  {
498  plotval = 1;
499  }
500  else
501  if(isSet(PF_T_FLAG)) /* Display Tsys Value */
502  {
503  tsys = 1;
504  }
505  else
506  if(isSet(PF_I_FLAG)) /* Display Intigration Time */
507  {
508  itime = 1;
509  }
510  else
511  if(isSet(PF_O_FLAG)) /* Display Map Offsets */
512  {
513  offsets = 1;
514  }
515 
516  /* The WRITE flag indicates if the user wants
517  to write out the results too;
518 
519  Hijack the library printing function to log the
520  output for the duration of this function */
521  if(isSet(PF_W_FLAG))
522  {
523  if(args->ntokens > 1 && strlen(args->secondArg) > 1)
524  {
525  strcpy(fname, args->secondArg);
526 
527  st_report("gridMapStats(): Writing outputfile: %s", fname);
528 
529  if((fp = fopen(fname, "w") ) <= (FILE *)0)
530  {
531  st_fail("Unable to open %s", fname);
532  writeit = 0;
533  }
534  else
535  {
536  saveFp = errPrint_fp;
537  errPrint_fp = fp;
538  writeit = 1;
539  }
540  }
541  else
542  {
543  st_fail("Missing filename arg for '/w' flag");
544 
546 
547  return(1);
548  }
549  }
550 
551  strcpy(blanks, " ");
552 
553  n = 8 - strlen(bm->source);
554  if(n > 0)
555  {
556  blanks[n] = '\0';
557  st_print("# MAP Source (Name): %s%.16s\n", blanks, bm->source);
558  }
559  else
560  {
561  st_print("# MAP Source (Name): %.16s\n", bm->source);
562  }
563 
564  st_print("# MAP X-Axis (Cols): %8d\n", bm->map_w);
565  st_print("# MAP Y-Axis (Rows): %8d\n", bm->map_h);
566  st_print("# MAP Delta-X (Secs): %8.0f\n", bm->map_step_x);
567  st_print("# MAP Delta-Y (Secs): %8.0f\n", bm->map_step_y);
568  st_print("# MAP Size-X (Secs): %8.0f\n", bm->map_size_x);
569  st_print("# MAP Size-Y (Secs): %8.0f\n", bm->map_size_y);
570 
571  /* Slice through the map horizonatally in the middle and look for
572  values to determine the beginning and ending rows; only works if
573  map is somewhat populated */
574  xstart = -1;
575  xend = 0;
576 
577  mid = bm->map_h / 2;
578  for(i=0;i<bm->plot_xe;i++)
579  {
580  if((bm->bmes[mid][i].rval != 0.0) && (xstart == -1))
581  {
582  xstart = i;
583  }
584 
585  if(bm->bmes[mid][i].rval != 0.0)
586  {
587  xend = i;
588  }
589  }
590 
591  st_print("# MAP Start-X: %8d\n", xstart +1);
592  st_print("# MAP End-X: %8d\n", xend +1);
593 
594  /* Now do the same vertically through center */
595  ystart = -1;
596  yend = 0;
597 
598  mid = bm->map_w / 2;
599  for(i=0;i<bm->plot_ye;i++)
600  {
601  if((bm->bmes[i][mid].rval != 0.0) && (ystart == -1))
602  {
603  ystart = i;
604  }
605 
606  if(bm->bmes[i][mid].rval != 0.0)
607  {
608  yend = i;
609  }
610  }
611 
612  st_print("# MAP Start-Y: %8d\n", ystart +1);
613  st_print("# MAP End-Y: %8d\n", yend +1);
614 
615  st_print("# MAP Freq (GHz ): %8.1f\n", bm->freq / 1e3);
616  st_print("# MAP Num of Scans: %8d\n", bm->nscans);
617  st_print("# MAP Blines Samples: %8d\n", bm->nbase);
618  st_print("# MAP Blines Removed: %8d\n", bm->base_removed);
619  st_print("# MAP First Scan: %8.2f\n", bm->fscan);
620  st_print("# MAP Last Scan: %8.2f\n", bm->lscan);
621  st_print("# MAP Y-Label: %8s\n", bm->yaxis_label);
622 
623  st_print("#\n");
624 
625  /* No use going any farther */
626  if((plotval + scanct + scans + tsys + rval + itime + offsets) == 0)
627  {
628  if(writeit)
629  {
630  fclose(fp);
631  errPrint_fp = saveFp;
632  }
633 
634  return(0);
635  }
636 
637  if(scans)
638  {
639  for(y=0;y<bm->map_h;y++)
640  {
641  st_print("%2d ", y);
642 
643  p = &bm->bmes[y][0];
644 
645  st_print("%7.1f ", p->eloff);
646 
647  for(j=0;j<p->n;j++)
648  {
649  if(p && p->headers[j]->scan)
650  {
651  st_print("%4.2f ", p->headers[j]->scan); /* Stored as (int)(scan * 100.0) */
652  }
653  else
654  {
655  st_print("%4.2f ", 0.0);
656  }
657  }
658 
659  st_print("\n");
660  }
661 
662  if(writeit)
663  {
664  fclose(fp);
665  errPrint_fp = saveFp;
666  }
667 
668  return(0);
669  }
670 
671 
672  /* don't place the 'Col' header in the output file */
673  if(!writeit)
674  {
675  st_print("#\n# Col");
676 
677  for(x=0;x<bm->map_w;x++)
678  {
679  if(x == 0)
680  {
681  st_print("%4d,", x);
682  }
683  else
684  {
685  if(offsets)
686  {
687  st_print("%9d,", x);
688  }
689  else
690  {
691  st_print("%8d,", x);
692  }
693  }
694  }
695 
696  st_print("\n#\n");
697  }
698 
699  if(offsets)
700  {
701  for(y=0;y<bm->map_h;y++)
702  {
703  /* Don't write leading 'Row' values to output file */
704  if(!writeit)
705  {
706  st_print("%2d ", y);
707  st_print("%8.2f", bm->bmes[y][0].eloff);
708  }
709 
710  for(x=1;x<bm->map_w;x++)
711  {
712  st_print("%8.2f", bm->bmes[y][x].azoff);
713 
714  if(x < (bm->map_w-1))
715  {
716  st_print(", ");
717  }
718  }
719 
720  st_print("\n");
721  }
722 
723  if(writeit)
724  {
725  fclose(fp);
726  errPrint_fp = saveFp;
727  }
728 
729  return(0);
730  }
731 
732  for(y=0;y<bm->map_h;y++)
733  {
734  if(!writeit)
735  {
736  st_print("%2d: ", y);
737  }
738 
739  for(x=0;x<bm->map_w;x++)
740  {
741 
742  if(scanct)
743  {
744  st_print("%2d", bm->bmes[y][x].n);
745  }
746  else
747  if(rval)
748  {
749  st_print("%7.3f", bm->bmes[y][x].rval);
750  }
751  else
752  if(plotval)
753  {
754  st_print("%7.3f", bm->bmes[y][x].pval);
755  }
756  else
757  if(tsys)
758  {
759  st_print("%3.0f", bm->bmes[y][x].tsys);
760  }
761  else
762  if(itime)
763  {
764  st_print("%2.0f", bm->bmes[y][x].itime);
765  }
766 
767  if(x < (bm->map_w-1))
768  {
769  st_print(", ");
770  }
771  }
772 
773  st_print("\n");
774  }
775 
776  if(writeit)
777  {
778  fclose(fp);
779  errPrint_fp = saveFp;
780  }
781 
782  return(0);
783 }
784 
785 
786 
787 int gridmap_writeDemFile(off, how)
788 int off, how;
789 {
790  int i, interp=0, max;
791  FILE *fp;
792  char source[80], fname[256], psfilename[256];
793  double clevel, cval;
794 // struct BEAM_MAP_ELEM *p;
795 
796  sprintf(fname, "/tmp/%s/bmap.dem", session);
797  if((fp = fopen(fname, "w")) == NULL)
798  {
799  fprintf(stderr, "Unable to open output file \"%s\"\n", fname);
800 
801  return(1);
802  }
803 
804  interp = isSet(PF_I_FLAG); /* Interpolate level ?? */
805 
806 // p = &beam_map.bmes[off][0];
807 
808 // strncpy(source, p->headers[0]->object, 16);
809 // source[16] = '\0';
810 
811  strcpy(source, env->source);
812 
813  fprintf(fp, "reset\n");
814  fprintf(fp, "set title \"Beam Map of %s\"\n", source);
815  fprintf(fp, "set xlabel \"Map El Offset\"\n");
816  fprintf(fp, "set ylabel \"Map Az Offset\"\n");
817  fprintf(fp, "set zlabel \"%s\"\n", beam_map.yaxis_label);
818 
819  fprintf(fp, "#\n");
820  fprintf(fp, "set border 4095\n");
821  fprintf(fp, "set hidden3d\n");
822  fprintf(fp, "#\n");
823 
824  if(isSet(PF_A_FLAG))
825  {
827 
828  fprintf(fp, "set xrange [%.2f:%.2f]\n", max / 2.0 - max, max / 2.0);
829  fprintf(fp, "set yrange [%.2f:%.2f]\n", max / 2.0 - max, max / 2.0);
830  }
831  else
832  {
833  fprintf(fp, "set xrange [%.2f:%.2f]\n", beam_map.map_size_y / 2.0 - beam_map.map_size_y, beam_map.map_size_y / 2.0);
834  fprintf(fp, "set yrange [%.2f:%.2f]\n", beam_map.map_size_x / 2.0 - beam_map.map_size_x, beam_map.map_size_x / 2.0);
835  }
836 
837  if(env->gmap_zmin != 0.0 || env->gmap_zmax != 0.0)
838  {
839  if(isSet(PF_V_FLAG))
840  {
841  st_report("Using User supplied Z-Range of: [%.2f %.2f]", env->gmap_zmin, env->gmap_zmax);
842  }
843 
844  fprintf(fp, "set zrange [%.2f:%.2f]\n", env->gmap_zmin, env->gmap_zmax);
845  }
846  else
847  {
848  fprintf(fp, "set zrange [%.2f:%.2f]\n", beam_map.map_min, beam_map.map_max);
849  }
850 
851  fprintf(fp, "set cbrange [%.2f:%.2f]\n", beam_map.map_min, beam_map.map_max);
852  fprintf(fp, "#\n");
853  fprintf(fp, "set samples %d*%d\n", beam_map.map_w, beam_map.map_h);
854  fprintf(fp, "set isosamples 60\n");
855 
856  fprintf(fp, "set ticslevel 0.1\n");
857 
858  fprintf(fp, "#\n");
859 
860  if(isSet(PF_B_FLAG))
861  {
862  if(isSet(PF_B_FLAG) > 1) /* Reverse plot order */
863  {
864  fprintf(fp, "set pm3d at bs ");
865  }
866  else
867  {
868  fprintf(fp, "set pm3d at sb ");
869  }
870 
871  if(interp)
872  {
873  fprintf(fp, "interpolate %d,%d\n", interp+1, interp+1);
874  }
875  else
876  {
877  fprintf(fp, "\n");
878  }
879  }
880  else
881  if(isSet(PF_W_FLAG))
882  {
883  fprintf(fp, "unset pm3d\n");
884  }
885  else
886  {
887  fprintf(fp, "set pm3d at s ");
888 
889  if(interp)
890  {
891  fprintf(fp, "interpolate %d,%d\n", interp+1, interp+1);
892  }
893  else
894  {
895  fprintf(fp, "\n");
896  }
897  }
898 
899  fprintf(fp, "#\n");
900 
901  fprintf(fp, "set palette color\n");
902 
903  if(isSet(PF_R_FLAG) == 1)
904  {
905  fprintf(fp, "set palette defined\n");
906  }
907  else
908  if(isSet(PF_R_FLAG) == 2)
909  {
910  fprintf(fp, "set palette model RGB\n");
911  fprintf(fp, "set palette defined ( 0 0 0 0, 1 0 0 1, 3 0 1 0, 4 1 0 0, 6 1 1 1 )\n");
912  }
913  else
914  if(isSet(PF_R_FLAG) >= 3)
915  {
916  fprintf(fp, "set palette model HSV\n");
917  fprintf(fp, "set palette defined ( 0 0 1 1, 1 1 1 1 )\n");
918  }
919  else
920  if(isSet(PF_G_FLAG))
921  {
922  fprintf(fp, "set palette model RGB\n");
923  fprintf(fp, "set palette defined ( 0 0 0 0, 1 1 1 1 )\n");
924  }
925  else
926  {
927  fprintf(fp, "set palette model RGB\n");
928  fprintf(fp, "set palette defined ( 0 \"green\", 1 \"blue\", 2 \"red\", 3 \"orange\" )\n");
929  }
930 
931  if(env->gmap_gamma != 0.0)
932  {
933  fprintf(fp, "set palette gamma %.2f\n", env->gmap_gamma);
934  }
935  else
936  {
937  fprintf(fp, "set palette gamma 1.0\n");
938  }
939 
940  fprintf(fp, "#\n");
941 
942  fprintf(fp, "set view %.1f, %.1f, 1.0, 1.0\n", env->map_angle[0], env->map_angle[1]);
943 
944  if(how == GNUPLOT)
945  {
946  if(doingDive)
947  {
948  fprintf(fp, "set terminal x11 enhanced %d size %d, %d position 500, 0\n", 21, 1024, 1080 );
949  }
950  else
951  {
952  fprintf(fp, "set terminal x11 enhanced %d size %d, %d position 930, 0\n", 20, 1100, 1100 );
953  }
954  }
955  else
956  {
957  fprintf(fp, "set terminal postscript enhanced size 17, 9.5625\n");
958  }
959 
960  if(isSet(PF_L_FLAG)) /* want contours */
961  {
962  if(isSet(PF_S_FLAG) == 1)
963  {
964  fprintf(fp, "set contour surface\n");
965  }
966  else
967  if(isSet(PF_S_FLAG) == 2)
968  {
969  fprintf(fp, "set contour both\n");
970  }
971  else
972  {
973  fprintf(fp, "set contour base\n");
974  }
975 
976  clevel = fabs(beam_map.map_min-beam_map.map_max) / 21.0;
977 
978  if(isSet(PF_V_FLAG))
979  {
980  st_print("Contour levels set to:\n");
981 
982  cval = beam_map.map_min;
983 
984  for(i=0;i<21;i++)
985  {
986  st_print("%6.1f ", cval);
987 
988  if(i == 6 || i == 13)
989  {
990  st_print("\n");
991  }
992 
993  cval += clevel;
994  }
995 
996  st_print("\n");
997  }
998 
999  fprintf(fp, "set cntrparam levels incr %.2f,%.2f,%.2f\n", beam_map.map_min, clevel, beam_map.map_max);
1000  }
1001 
1002  if(how == GNUPLOT)
1003  {
1004  fprintf(fp, "splot \"/tmp/%s/beam_map.dat\" every 1::0::1024 with lines\n", session);
1005 // fprintf(fp, "pause -1 \"Hit Return When Finished\"\n");
1006  }
1007  else
1008  {
1009  sprintf(psfilename, "/tmp/%s/ccal_plot.ps", session);
1010 
1011  fprintf(fp, "set out \"%s\"\n", psfilename);
1012  fprintf(fp, "splot \"/tmp/%s/beam_map.dat\" every 1::0::1024 with lines\n", session);
1013  fprintf(fp, "\n");
1014 
1015  st_print("\nMade PS file: %s\n\n", psfilename);
1016  }
1017 
1018  fclose(fp);
1019 
1020  return(0);
1021 }
1022 
1023 
1024 
1026 {
1027  int xb, xe, yb, ye;
1028 
1029  if(isSet(PF_H_FLAG))
1030  {
1032 
1033  return(0);
1034  }
1035 
1036  if(isSet(PF_R_FLAG)) /* Reset */
1037  {
1038  beam_map.plot_xb = 0;
1040  beam_map.plot_yb = 0;
1042 
1043  return(0);
1044  }
1045 
1046  if(isSet(PF_S_FLAG)) /* Show */
1047  {
1048  st_report("Gridmap Plot Limits: Az: %d - %d; El: %d - %d",
1049  beam_map.plot_xb,
1050  beam_map.plot_xe,
1051  beam_map.plot_yb,
1052  beam_map.plot_ye);
1053 
1054  return(0);
1055  }
1056 
1057  if(args->ntokens < 5)
1058  {
1060 
1061  return(1);
1062  }
1063 
1064  xb = atoi(args->secondArg);
1065  xe = atoi(args->thirdArg);
1066  yb = atoi(args->fourthArg);
1067  ye = atoi(args->fifthArg);
1068 
1069  if(xb < 0 || xb > beam_map.map_w)
1070  {
1071  st_fail("xb val: %d, out of range of %d %d", xb, 0, beam_map.map_w);
1072 
1073  return(0);
1074  }
1075 
1076  if(xe == xb)
1077  {
1078  st_fail("xe val: %d, can not be same as xb: %d", xe, xb);
1079 
1080  return(0);
1081  }
1082 
1083  if(xe > beam_map.map_w || xe < xb)
1084  {
1085  st_fail("xe val: %d, out of range of %d %d", xe, xb, beam_map.map_w);
1086 
1087  return(0);
1088  }
1089 
1090  if(yb < 0 || yb > beam_map.map_h)
1091  {
1092  st_fail("yb val: %d, out of range of %d %d", yb, 0, beam_map.map_h);
1093 
1094  return(0);
1095  }
1096 
1097  if(ye == yb)
1098  {
1099  st_fail("ye val: %d, can not be same as yb: %d", ye, yb);
1100 
1101  return(0);
1102  }
1103 
1104  if(ye > beam_map.map_h || ye <= yb)
1105  {
1106  st_fail("ye val: %d, out of range of %d %d", ye, yb, beam_map.map_h);
1107 
1108  return(0);
1109  }
1110 
1111  beam_map.plot_xb = xb;
1112  beam_map.plot_xe = xe;
1113  beam_map.plot_yb = yb;
1114  beam_map.plot_ye = ye;
1115 
1116  return(0);
1117 }
1118 
1119 
1121 {
1122  int i, j, x, y, indx1, indx2, ret, hm, glen, request;
1123  double xdata[512], ydata[512];
1124  double arcsec, max1=0.0, max2=0.0;
1125  double xd[512], yd[512], sig[512];
1126  double aa1[4], aa2[4], peak1, peak2, beam1, beam2;
1127  char tbuf[256], ttbuf[256];
1128  struct BEAM_MAP_ELEM *p;
1129 
1130  if(beam_map.map_w == 0 || beam_map.map_h == 0)
1131  {
1132  st_print("\n No Map to fit a gaussian to\n");
1133  st_print("Have you done a 'gridmap' yet?\n\n");
1134 
1135  return(1);
1136  }
1137 
1138  glen = 5;
1139 
1140  bzero((char *)&xd[0], sizeof(xd));
1141  bzero((char *)&yd[0], sizeof(yd));
1142 
1143  y = beam_map.map_h / 2;
1144  hm = beam_map.map_w / 2; /* Half map */
1145 
1146  if(args->ntokens > 1)
1147  {
1148  request = atoi(args->secondArg);
1149 
1150  request--; /* change it to zero based */
1151 
1152  if(request >= 0 && request < beam_map.map_h)
1153  {
1154  y = request;
1155  }
1156  }
1157 
1158  p = &beam_map.bmes[y][0];
1159 
1160  if(!p->itime)
1161  {
1162  st_print("\n No Map in row %d to fit a gaussian to\n", y);
1163 
1164  return(1);
1165  }
1166 
1167  arcsec = p->headers[0]->deltaxr; /* Grab the first header in this cell */
1168 
1169  tbuf[0] = '\0';
1170  for(j=0;j<p->n;j++)
1171  {
1172  sprintf(ttbuf, "%.2f ", p->headers[j]->scan);
1173 
1174  strcat(tbuf, ttbuf);
1175  }
1176 
1177  st_report( "Fitting Gaussian Using Scan(s) %s; Row %d; Arcsec = %.1f", tbuf, (int)p->headers[0]->ycell0-1, arcsec);
1178 
1179  for(x=0;x<beam_map.map_w;x++) /* Create the x,y-axis data; Find Min/Max too */
1180  {
1181  xdata[x] = (float)x * arcsec;
1182 
1183  if(isSet(PF_P_FLAG))
1184  {
1185  ydata[x] = beam_map.bmes[y][x].pval;
1186  }
1187  else
1188  {
1189  ydata[x] = beam_map.bmes[y][x].rval;
1190  }
1191 
1192  sig[x] = 1.0;
1193 
1194  if(ydata[x] > max1)
1195  {
1196  max1 = ydata[x];
1197  indx1 = x;
1198  }
1199  if(ydata[x] < max2)
1200  {
1201  max2 = ydata[x];
1202  indx2 = x;
1203  }
1204  }
1205 
1206  // Set up for gausian fitting
1207 
1208  if(max1 == 0.0 || max2 == 0.0)
1209  {
1210  st_fail("No data to do gausfit");
1211  return(1);
1212  }
1213 
1214  st_report("Guess, Plus Peak: %7.2f @ Sample %d or %.2f\"",
1215  max1, indx1, xdata[indx1]);
1216  st_report("Guess, Minus Peak: %7.2f @ Sample %d or %.2f\"",
1217  max2, indx2, xdata[indx2]);
1218  st_report("Guess, Beam Sep: %7.2f\"",
1219  fabs(indx1-indx2) * arcsec);
1220 
1221  /* Compute ideal gaussian 1/2 power width in samples */
1222  glen = (int)(p->headers[0]->bfwhm / arcsec) + 1;
1223 
1224 // Load first peak
1225  for(i=indx1-glen,j=0;i<indx1+glen;i++,j++)
1226  {
1227  xd[j] = xdata[i];
1228  yd[j] = ydata[i];
1229  }
1230 
1231  st_report("Fitting a %d Point Gauss to Plus Peak", j+1);
1232  aa1[1] = max1;
1233  aa1[2] = xdata[indx1];
1234  aa1[3] = (double)j * arcsec; /* Beam width */
1235 
1236  ret = gauss_fit(&xd[0], &yd[0], sig, j, aa1, isSet(PF_V_FLAG));
1237 
1238  if(!ret)
1239  {
1240  st_fail("1st Half Gauss Fit Failed");
1241 
1242  for(i=0;i<j;i++)
1243  {
1244  st_print("%2d %4.1f %4.1f\n", i, xd[i], yd[i]);
1245  }
1246 
1247  return(1);
1248  }
1249 
1250  st_report("Found Plus Beam @ %7.2f\", Peak = %7.2f, Beam Width = %7.2f",
1251  aa1[2], aa1[1], fabs(aa1[3]));
1252 
1253  peak1 = aa1[2];
1254  beam1 = fabs((aa1[2] - xdata[hm]));
1255 
1256 // Load second peak
1257  for(i=indx2-glen,j=0;i<indx2+glen;i++,j++)
1258  {
1259  xd[j] = xdata[i];
1260  yd[j] = ydata[i];
1261  }
1262 
1263  st_report("Fitting a %d Point Gauss to Minus Peak", j+1);
1264  aa2[1] = max2;
1265  aa2[2] = xdata[indx2];
1266  aa2[3] = (double)j * arcsec;
1267 
1268  ret = gauss_fit(&xd[0], &yd[0], sig, j, aa2, isSet(PF_V_FLAG));
1269 
1270  if(!ret)
1271  {
1272  st_fail("2nd Half Gauss Fit Failed");
1273 
1274  for(i=0;i<j;i++)
1275  {
1276  st_print("%2d %4.1f %4.1f\n", i, xd[i], yd[i]);
1277  }
1278 
1279  return(1);
1280  }
1281 
1282  st_report("Found Minus Beam @ %7.2f\", Peak = %7.2f, Beam Width = %7.2f",
1283  aa2[2], aa2[1], fabs(aa2[3]));
1284 
1285  peak2 = aa2[2];
1286  beam2 = fabs((aa2[2] - xdata[hm]));
1287 
1288  st_report("Plus Beam +%7.2f\" From Center", beam2);
1289  st_report("Minus Beam -%7.2f\" From Center", beam1);
1290  st_report("Beam Sep: %7.2f\"", fabs(peak2-peak1));
1291 
1292  beam_map.gmap_results = fabs(peak2-peak1);
1293  beam_map.gmap_bw1 = fabs(aa1[3]);
1294  beam_map.gmap_bw2 = fabs(aa2[3]);
1295  beam_map.gmap_peak1 = fabs(aa1[1]);
1296  beam_map.gmap_peak2 = fabs(aa2[1]);
1297 
1298  st_print("Beam Ratio: %7.2f ", beam_map.gmap_peak1 / beam_map.gmap_peak2);
1299 
1301  {
1302  st_print(" <- Neg Beam Stronger ");
1303  }
1304  else
1305  {
1306  st_print(" Pos Beam Stronger -> ");
1307  }
1308 
1309  st_print("\n\n");
1310 
1311  p = &beam_map.bmes[y][0];
1312 
1313  use_global_gdata = 1;
1314  gridmap_goWriteCenterRow(xdata, ydata, aa1, aa2, beam_map.map_w, p->headers[0]->scan);
1315 
1316  return(0);
1317 }
1318 
1319 
1320 
1321 int gridmap_lsqfit(x, y, bdata, ndata, sig, mwt, a, b, siga, sigb, chi2, q)
1322 double x[], y[], sig[], *a, *b, *siga, *sigb, *chi2, *q;
1323 int bdata, ndata, mwt;
1324 {
1325  int i;
1326  double wt, t, sxoss, sx=0.0, sy=0.0, st2=0.0, ss, sigdat;
1327 
1328  *b = 0.0;
1329 
1330  if(mwt)
1331  {
1332  ss = 0.0;
1333 
1334  for(i=bdata;i<ndata;i++)
1335  {
1336  wt = 1.0 / SQR(sig[i]);
1337  ss += wt;
1338  sx += x[i] * wt;
1339  sy += y[i] * wt;
1340  }
1341  }
1342  else
1343  {
1344  for(i=bdata;i<ndata;i++)
1345  {
1346  sx += x[i];
1347  sy += y[i];
1348  }
1349 
1350  ss = ndata - bdata;
1351  }
1352 
1353  sxoss = sx / ss;
1354 
1355  if(mwt)
1356  {
1357  for(i=bdata;i<ndata;i++)
1358  {
1359  t = (x[i] - sxoss) / sig[i];
1360  st2 += t * t;
1361  *b += t * y[i] / sig[i];
1362  }
1363  }
1364  else
1365  {
1366  for(i=bdata;i<ndata;i++)
1367  {
1368  t = x[i] - sxoss;
1369  st2 += t * t;
1370  *b += t * y[i];
1371  }
1372  }
1373 
1374  *b /= st2;
1375  *a = (sy - sx * (*b)) / ss;
1376  *siga = sqrt((1.0 + sx*sx / (ss * st2)) / ss);
1377  *sigb = sqrt(1.0 / st2);
1378  *chi2 = 0.0;
1379 
1380  if(mwt == 0)
1381  {
1382  for(i=bdata;i<ndata;i++)
1383  {
1384  *chi2 += SQR(y[i] - (*a) - (*b) * x[i]);
1385  }
1386 
1387  *q=1.0;
1388  sigdat = sqrt((*chi2) / (ndata - bdata -2));
1389 
1390  *siga *= sigdat;
1391  *sigb *= sigdat;
1392  }
1393 
1394  return(0);
1395 }
1396 
1397 
1399 {
1400  int x, y;
1401  double val;
1402 
1403  beam_map.map_max = -1e9;
1404  beam_map.map_min = 1e9;
1405 
1406  for(y=0;y<beam_map.map_h;y++)
1407  {
1408  for(x=0;x<beam_map.map_w;x++)
1409  {
1410  val = beam_map.bmes[y][x].pval;
1411 
1412  if(val > beam_map.map_max)
1413  {
1414  beam_map.map_max = val;
1415  }
1416  if(val < beam_map.map_min)
1417  {
1418  beam_map.map_min = val;
1419  }
1420  }
1421  }
1422 
1423  beam_map.map_min -= 0.5;
1424  beam_map.map_max += 0.5;
1425 
1426  if(beam_map.map_min < env->ylower)
1427  {
1428  st_report("Truncating Z-Scale upper to %f", env->yupper);
1430  }
1431 
1432  if(beam_map.map_max > env->yupper)
1433  {
1434  st_report("Truncating Z-Scale lower to %f", env->ylower);
1436  }
1437 
1439  {
1440  st_report("Plot: Min/Max Map Values: %f/%f", beam_map.map_min, beam_map.map_max);
1441  }
1442 
1443  return(0);
1444 }
1445 
1446 
1447 /* Remove a baseline from the Raw Array */
1449 int nb;
1450 {
1451  int x, y, j, start, end;
1452  double xd[32], yd[32], sig[512];
1453  double a, b, siga, sigb, chi2, q, xdata[512];
1454 
1456  {
1457  st_report("Baselines already removed from Map");
1458 
1459  return(0);
1460  }
1461 
1462  if(nb == 0)
1463  {
1464  st_report("mapbaselines set to 0; skipping removal");
1465 
1466  return(0);
1467  }
1468 
1469  if(nb < 0)
1470  {
1471  st_report("gridmap_removeBaselines(): Removing Linear Baseline Using Center %d samples", abs(nb));
1472  }
1473  else
1474  {
1475  st_report("gridmap_removeBaselines(): Removing Linear Baseline Using %d samples on both ends", nb);
1476  }
1477 
1478  bzero((char *)&xd[0], sizeof(xd));
1479  bzero((char *)&yd[0], sizeof(yd));
1480 
1481  for(x=0;x<beam_map.map_w;x++) /* Create the x-axis data */
1482  {
1483  xdata[x] = (float)x - (float)beam_map.map_w / 2.0;
1484  }
1485 
1486  for(y=0;y<beam_map.map_h;y++) /* For each row */
1487  {
1488  j = 0;
1489 
1490  if(nb < 0)
1491  {
1492  start = (beam_map.map_w / 2) - (abs(nb) / 2);
1493  end = (beam_map.map_w / 2) + (abs(nb) / 2);
1494 
1495  for(x=start;x<end;x++)
1496  {
1497  xd[j] = xdata[x];
1498  yd[j] = beam_map.bmes[y][x].rval;
1499 
1500  j++;
1501  }
1502  }
1503  else
1504  {
1505  for(x=0;x<nb;x++)
1506  {
1507  xd[j] = xdata[x];
1508  yd[j] = beam_map.bmes[y][x].rval;
1509 
1510  j++;
1511  }
1512 
1513  for(x=beam_map.map_w-nb;x<beam_map.map_w;x++)
1514  {
1515  xd[j] = xdata[x];
1516  yd[j] = beam_map.bmes[y][x].rval;
1517 
1518  j++;
1519  }
1520  }
1521 
1522  gridmap_lsqfit(xd, yd, 0, j-1, sig, 0, &a, &b, &siga, &sigb, &chi2, &q);
1523 
1524  if(a != 0.0 && b != 0.0) /* Remove a baseline where there is data */
1525  {
1526  for(x=0;x<beam_map.map_w;x++)
1527  {
1528  beam_map.bmes[y][x].rval = beam_map.bmes[y][x].rval -(a+b*xdata[x]);
1529  }
1530  }
1531  }
1532 
1533  beam_map.base_removed = 1;
1534  beam_map.nbase = abs(nb);
1535 
1536  return(0);
1537 }
1538 
1539 
1541 struct BEAM_MAP_ELEM *p;
1542 double scan;
1543 {
1544  int i;
1545 
1546  for(i=0;i<p->n;i++)
1547  {
1548  if(fabs(p->headers[i]->scan-scan) < 0.005)
1549  {
1550  return(1);
1551  }
1552  }
1553 
1554 // p->scans[p->n] = scan;
1555 
1556  return(0);
1557 }
1558 
1559 
1560 
1562 struct HEADER *h;
1563 {
1564  int x, y;
1565  struct BEAM_MAP_ELEM *p;
1566 
1567  for(y=0;y<beam_map.map_h;y++)
1568  {
1569  p = &beam_map.bmes[y][0];
1570 
1571  for(x=0;x<beam_map.map_w;x++,p++)
1572  {
1573  p->azoff = ((float)x - (float)beam_map.map_w / 2.0) * h->deltaxr;
1574  p->eloff = ((float)y - (float)beam_map.map_h / 2.0 + 0.5) * h->deltayr;
1575  }
1576  }
1577 
1578  return(0);
1579 }
1580 
1581 
1582 /* Grid the data into the Raw Array */
1584 float *data;
1585 struct HEADER *h;
1586 {
1587  int x, y, dlen, skip = 0;
1588  float ydata[MAX_CONT_SAMPLES];
1589  double iwt, owt, beamthrow;
1590  struct BEAM_MAP_ELEM *p;
1591  struct BEAM_MAP *bm;
1592 
1593  if(processContinuum(h, data, &ydata[0], &dlen, "Az-El Grid Map"))
1594  {
1595  return(1);
1596  }
1597 
1598  bm = &beam_map;
1599 
1600  if(!bm->offsets_filled) /* the first time through ?? */
1601  {
1602  bzero(bm->source, sizeof(beam_map.source));
1603  strncpy(bm->source, h->object, 16);
1604  strcmprs(bm->source);
1605 
1606  bm->freq = h->restfreq;
1607  bm->beamthrow = fabs(h->mbeam[0]) + fabs(h->pbeam[0]);
1608 
1609  gmapFillOffsets(h);
1610 
1611  bm->offsets_filled = 1;
1612 
1613  st_report("Setting Beam Map to: Source: %s, Freq %f, Beam Throw: %f", bm->source, bm->freq, bm->beamthrow);
1614  }
1615 
1616  /* Check to make sure this scan matches the map cube */
1617 
1618  if(strncmp(h->object, bm->source, strlen(bm->source)))
1619  {
1620  st_fail("Scan %.2f, not same Source: %.16s as map: %f", h->scan, h->object, bm->freq);
1621  return(1);
1622  }
1623 
1624  if(fabs(h->restfreq-bm->freq) > 1.0)
1625  {
1626  st_fail("Scan %.2f, not same freq: %f as map: %f", h->scan, h->restfreq, bm->freq);
1627  return(1);
1628  }
1629 
1630  beamthrow = fabs(h->mbeam[0]) + fabs(h->pbeam[0]);
1631 
1632  if(fabs(beamthrow-bm->beamthrow) > 2.0)
1633  {
1634  st_fail("Scan %.2f, not same BeamThrow: %f as map: %f", h->scan, beamthrow, bm->beamthrow);
1635  return(1);
1636  }
1637 
1638  y = (int)h->ycell0 - 1;
1639  st_report("Gridding %.2f; Grid-Row %2d", h->scan, y);
1640 
1641  if(y < 0 || y >= bm->map_h)
1642  {
1643  st_fail("Offset %d out of range of map %d", y, bm->map_h);
1644 
1645  return(1);
1646  }
1647 
1648  iwt = h->samprat / (h->stsys * h->stsys); /* New data */
1649 
1650  p = &bm->bmes[y][0];
1651 
1652 
1653  st_report("gridmap_grid_data(): Gridding %d samples", dlen);
1654 
1655  for(x=0;x<dlen;x++,p++)
1656  {
1657  if(p->n < MAX_GMAP_SCANS)
1658  {
1659  p->headers[p->n] = do_addHeader(0, h->scan, h);
1660 
1661  if(!scanAlreadyLoaded(p, h->scan))
1662  {
1663  p->x = x;
1664  p->y = y;
1665  p->tsys *= p->itime; /* Un-roll previous tys */
1666 
1667  p->itime += h->samprat;
1668  p->tsys += (h->stsys * h->samprat);
1669  p->tsys /= p->itime;
1670 
1671 // p->azoff = ((float)x - (float)bm->map_w / 2.0) * h->deltaxr;
1672  p->eloff = ((float)y - (float)bm->map_h / 2.0 + 0.5) * h->deltayr;
1673 
1674  /* Compute weights and weight-average data */
1675  owt = p->wt; /* Data already in array */
1676 
1677  p->rval = (p->rval * owt + ydata[x] * iwt) / (owt + iwt); /* Average them using a weighted scale */
1678 
1679  p->wt += iwt;
1680 
1681  p->n++;
1682  }
1683  else
1684  {
1685  skip++;
1686  }
1687  }
1688  else
1689  {
1690  st_print("Too many scans for array: limit: %d", MAX_GMAP_SCANS);
1691  }
1692  }
1693 
1694  if(skip)
1695  {
1696  st_report("Skipping scan %.2f; already loaded", h->scan);
1697  return(1);
1698  }
1699 
1700  return(0);
1701 }
1702 
1703 
1704 /* Limit value to upper and lower limits */
1705 double limitValue(l, v, u)
1706 double l, v, u;
1707 {
1708  double val;
1709 
1710  if(v > u)
1711  {
1712  val = u;
1713  }
1714  else
1715  if(v < l)
1716  {
1717  val = l;
1718  }
1719  else
1720  {
1721  val = v;
1722  }
1723 
1724  return(val);
1725 }
1726 
1727 
1728 
1729 /* Copy the Raw Array to the Plot Array appling any limits, checking for NAN's and
1730  * conversion to dB's if requested
1731  */
1733 {
1734  int x, y, dBmap = 0, isnanCount=0;
1735  double pvalue, max_value = -1e9, min_value = 1e9, ylower, yupper;
1736 
1737  if(isSet(PF_D_FLAG))
1738  {
1739  freeY(); /* Can't use upper and lower limits and decibel scale */
1740  dBmap = 1;
1741  }
1742 
1743  yupper = env->yupper;
1744  ylower = env->ylower;
1745 
1746  /* Find map Max, Min, check for NAN values */
1747  for(y=0;y<beam_map.map_h;y++)
1748  {
1749  for(x=0;x<beam_map.map_w;x++)
1750  {
1751  if(gsl_isnan(beam_map.bmes[y][x].rval))
1752  {
1753  beam_map.bmes[y][x].rval = 0.0; /* Correct it */
1754  isnanCount++;
1755  }
1756 
1757  pvalue = beam_map.bmes[y][x].rval;
1758 
1759  max_value = GSL_MAX(pvalue, max_value);
1760  min_value = GSL_MIN(pvalue, min_value);
1761  }
1762 
1763  if(isnanCount)
1764  {
1765  st_report("Corrected %d bad cells in Map", isnanCount);
1766  }
1767  }
1768 
1769  if(isSet(PF_V_FLAG))
1770  {
1771  st_report("Raw: Min/Max Map Values: %f/%f", min_value, max_value);
1772  }
1773 
1774  if(dBmap)
1775  {
1776  strcpy(beam_map.yaxis_label, "dB's");
1777  }
1778  else
1779  {
1780  strcpy(beam_map.yaxis_label, "Intensity (K)");
1781  }
1782 
1783  for(y=0;y<beam_map.map_h;y++)
1784  {
1785  for(x=0;x<beam_map.map_w;x++)
1786  {
1787  pvalue = limitValue(ylower, beam_map.bmes[y][x].rval, yupper);
1788 
1789  if(dBmap)
1790  {
1791  if(env->lowerdb > 0.0 && fabs(pvalue) < env->lowerdb)
1792  {
1793  pvalue = env->lowerdb;
1794  }
1795 
1796  if(pvalue != 0.0)
1797  {
1798  pvalue = log10(fabs(pvalue) / max_value) * 10.0;
1799  }
1800  else
1801  {
1802  pvalue = env->lowerdb;
1803  }
1804 
1805  if(env->lowerdb < 0.0 && pvalue < env->lowerdb)
1806  {
1807  pvalue = env->lowerdb;
1808  }
1809 
1810  }
1811 
1812  beam_map.bmes[y][x].pval = pvalue;
1813  }
1814  }
1815 
1816  return(0);
1817 }
1818 
1819 
1820 int findMapColIndx(x1, y1)
1821 double x1;
1822 int y1;
1823 {
1824  int x, n, indx = 0;
1825  double diff, closest = 1e9;
1826 
1828  {
1829  st_report("findMapColIndx(%f, %d)", x1, y1);
1830  }
1831 
1832  n = beam_map.map_w;
1833 
1834  for(x=0;x<n;x++)
1835  {
1836  diff = fabs(beam_map.bmes[y1][x].azoff - x1);
1837 
1838  if(verboseLevel & VERBOSE_GRIDMAP)
1839  {
1840  st_report("Checking fabs(%f - %f): %f", beam_map.bmes[y1][x].azoff, x1, diff);
1841  }
1842 
1843  if(diff < closest)
1844  {
1845  closest = diff;
1846  indx = x;
1847  }
1848  }
1849 
1850  if(verboseLevel & VERBOSE_GRIDMAP)
1851  {
1852  st_report("findMapColIndx(): Closest = %f, indx = %d", closest, indx);
1853  }
1854 
1855  return(indx);
1856 }
1857 
1858 
1859 int findMapRowIndx(x1, y1)
1860 int x1;
1861 double y1;
1862 {
1863  int y, n, indx = 0;
1864  double diff, closest = 1e9;
1865 
1867  {
1868  st_report("findMapRowIndx(%d, %f)", x1, y1);
1869  }
1870 
1871  n = beam_map.map_h;
1872 
1873  for(y=0;y<n;y++)
1874  {
1875  diff = fabs(beam_map.bmes[y][x1].eloff - y1);
1876 
1877  if(verboseLevel & VERBOSE_GRIDMAP)
1878  {
1879  st_report("Checking fabs(%f - %f): %f", beam_map.bmes[y][x1].eloff, y1, diff);
1880  }
1881 
1882  if(diff < closest)
1883  {
1884  closest = diff;
1885  indx = y;
1886  }
1887  }
1888 
1889  if(verboseLevel & VERBOSE_GRIDMAP)
1890  {
1891  st_report("findMapRowIndx(): Closest = %f, indx = %d", closest, indx);
1892  }
1893 
1894  return(indx);
1895 }
1896 
1897 
1899 {
1900  int x, y, cr, start, end, dlen, old_plotstyle;
1901  double xb, xe, yb, ye;
1902  struct BEAM_MAP_ELEM *p;
1903 
1904  if(isSet(PF_H_FLAG))
1905  {
1907 
1908  return(0);
1909  }
1910 
1911  if(args->ntokens < 2)
1912  {
1914 
1915  return(1);
1916  }
1917 
1918  if(beam_map.map_w == 0)
1919  {
1920  st_fail("No Grid Map Loaded");
1921 
1922  return(1);
1923  }
1924 
1925  cr = atoi(args->secondArg); /* Col or row number */
1926 
1927  cr--; /* Make it 0 based */
1928 
1929  if(isSet(PF_Y_FLAG)) /* DO columns ?? */
1930  {
1931  if(cr < 0 || cr > beam_map.map_w)
1932  {
1933  st_fail("Col number %d, out of range: %d to %d", cr, 0, beam_map.map_w);
1934  return(1);
1935  }
1936  }
1937  else
1938  {
1939  if(cr < 0 || cr > beam_map.map_h)
1940  {
1941  st_fail("Row number %d, out of range: %d to %d", cr, 0, beam_map.map_h);
1942  return(1);
1943  }
1944  }
1945 
1946  if(args->ntokens > 3) /* User selected a sub range */
1947  {
1948  if(isSet(PF_Y_FLAG)) /* DO columns ?? */
1949  {
1950  yb = atof(args->thirdArg);
1951  ye = atof(args->fourthArg);
1952 
1953  start = findMapRowIndx(cr, yb);
1954  end = findMapRowIndx(cr, ye);
1955 
1956  dlen = (end - start);
1957  }
1958  else
1959  {
1960  xb = atof(args->thirdArg);
1961  xe = atof(args->fourthArg);
1962 
1963  start = findMapColIndx(xb, cr);
1964  end = findMapColIndx(xe, cr);
1965 
1966  dlen = (end - start);
1967  }
1968  }
1969  else
1970  {
1971  start = 0;
1972 
1973  if(isSet(PF_Y_FLAG)) /* DO columns ?? */
1974  {
1975  end = beam_map.map_h;
1976  }
1977  else
1978  {
1979  end = beam_map.map_w;
1980  }
1981 
1982  dlen = end - start;
1983  }
1984 
1986  {
1987  st_report("Start = %d, End = %d, Dlen = %d", start, end, dlen);
1988  }
1989 
1990  if(isSet(PF_Y_FLAG)) /* DO columns ?? */
1991  {
1992  /* Pick center row for header */
1993 
1994  p = &beam_map.bmes[beam_map.map_h/2][0];
1995 
1996  if(p->headers[0])
1997  {
1998  clearFillContStruct(p->headers[0], dlen, "Map El Offset", beam_map.yaxis_label);
1999  }
2000  else
2001  {
2002  st_fail("gmapSlice(): No header found");
2003  return(1);
2004  }
2005 
2006  for(y=start;y<end;y++)
2007  {
2008  cont_plain_send.xdata[y-start] = beam_map.bmes[y][cr].eloff;
2009  cont_plain_send.ydata[y-start] = beam_map.bmes[y][cr].pval;
2010  }
2011 
2012  if(xgraphic)
2013  {
2015  }
2016  }
2017  else
2018  {
2019  p = &beam_map.bmes[cr][0];
2020 
2021  if(p->headers[0])
2022  {
2023  clearFillContStruct(p->headers[0], dlen, "Map Az Offset", beam_map.yaxis_label);
2024  }
2025  else
2026  {
2027  st_fail("gmapSlice(): No header found");
2028  return(1);
2029  }
2030 
2031  for(x=start;x<end;x++)
2032  {
2033  cont_plain_send.xdata[x-start] = beam_map.bmes[cr][x].azoff;
2034  cont_plain_send.ydata[x-start] = beam_map.bmes[cr][x].pval;
2035  }
2036 
2037  if(use_global_gdata)
2038  {
2039  for(x=start;x<end;x++)
2040  {
2042  }
2043 
2045  }
2046 
2047  if(xgraphic)
2048  {
2049  old_plotstyle = env->plotstyle;
2050 
2052 
2054 
2055  env->plotstyle = old_plotstyle;
2056  }
2057  }
2058 
2059  return(0);
2060 }
2061 
2062 
2063 
2065 {
2066  int win;
2067  char name[256], sysBuf[256];
2068 
2069  if(isSet(PF_H_FLAG))
2070  {
2072 
2073  return(0);
2074  }
2075 
2076  if(notSet(PF_F_FLAG)) /* Auto set map angle or leave it fixed */
2077  {
2078  doGetMapAngle();
2079  }
2080 
2081  if(isSet(PF_0_FLAG))
2082  {
2083  env->map_angle[0] = 60.0;
2084  env->map_angle[1] = 30.0;
2085  }
2086  else
2087  if(isSet(PF_1_FLAG))
2088  {
2089  env->map_angle[0] = 180.0;
2090  env->map_angle[1] = 90.0;
2091  }
2092  else
2093  if(isSet(PF_2_FLAG))
2094  {
2095  env->map_angle[0] = 90.0;
2096  env->map_angle[1] = 0.0;
2097  }
2098  else
2099  if(isSet(PF_3_FLAG))
2100  {
2101  env->map_angle[0] = 0.0;
2102  env->map_angle[1] = 0.0;
2103  }
2104  else
2105  if(isSet(PF_4_FLAG))
2106  {
2107  env->map_angle[0] = 0.0;
2108  env->map_angle[1] = 90.0;
2109  }
2110  else
2111  if(isSet(PF_5_FLAG))
2112  {
2113  env->map_angle[0] = 90.0;
2114  env->map_angle[1] = 270.0;
2115  }
2116  else
2117  if(isSet(PF_6_FLAG))
2118  {
2119  env->map_angle[0] = 90.0;
2120  env->map_angle[1] = 180.0;
2121  }
2122  else
2123  if(isSet(PF_7_FLAG))
2124  {
2125  env->map_angle[0] = 60.0;
2126  env->map_angle[1] = 220.0;
2127  }
2128  else
2129  if(isSet(PF_8_FLAG))
2130  {
2131  env->map_angle[0] = 90.0;
2132  env->map_angle[1] = 90.0;
2133  }
2134  else
2135  if(isSet(PF_9_FLAG))
2136  {
2137  env->map_angle[0] = 270.0;
2138  env->map_angle[1] = 270.0;
2139  }
2140 
2141 
2142  /* Remember the flags for any move that's played later */
2143  bcopy(args->pflags, (char *)&gmap_pflags, sizeof(gmap_pflags));
2144 
2145  prepareMap(); /* Go prepare the map for plotting */
2146  gridmap_findMinMax(); /* Determine it's bounds */
2147 
2148  if(isSet(PF_C_FLAG))
2149  {
2151  validate(CONTMAP);
2152 
2153  return(0);
2154  }
2155 
2156  if(isSet(PF_P_FLAG) == 1) /* Make a PS file */
2157  {
2158  doMapPlot(PSPLOT);
2159  }
2160  else
2161  {
2162  doMapPlot(GNUPLOT); /* Display a GUI plot */
2163  }
2164 
2165  if(isSet(PF_P_FLAG) == 2) /* Make a PNG file */
2166  {
2167  if(doingDive)
2168  {
2169  win = getPlotWinId(21);
2170  }
2171  else
2172  {
2173  win = getPlotWinId(20);
2174  }
2175 
2176  if(!win)
2177  {
2178  st_fail("gridMapPlot(): Unable to determine GNUPLOT windo ID");
2179 
2180  validate(CONTMAP);
2181 
2182  return(1);
2183  }
2184 
2185  sprintf(name, "%s.%d.png", env->source, (int)(env->if_feed*100.0));
2186 
2187  sprintf(sysBuf, "import -window 0x%x -silent %s", win, name);
2188  system(sysBuf);
2189 
2190  st_report("Created file: %s", name);
2191  }
2192 
2193  validate(CONTMAP);
2194 
2195  return(0);
2196 }
2197 
2203 int doMapPlot(how)
2204 int how;
2205 {
2206  char sysBuf[256];
2207 
2208  if(beam_map.map_h > 0 && beam_map.map_w > 0)
2209  {
2211  gridmap_writeDemFile(beam_map.map_h / 2.0, how);
2212 
2213  sprintf(sysBuf, "xgraphic load '/tmp/%s/bmap.dem'", session);
2214 
2216  {
2217  st_report("Parsing Command: %s", sysBuf);
2218  }
2219 
2220  parseCmd(sysBuf);
2221  }
2222  else
2223  {
2224  st_warn("No Map to Plot");
2225  st_warn("Have you done a 'gridmap'?");
2226  }
2227 
2228  return(0);
2229 }
2230 
2231 
2233 {
2235 
2236  return(0);
2237 }
2238 
2239 
2241 {
2242  if(isSet(PF_H_FLAG))
2243  {
2245 
2246  return(0);
2247  }
2248 
2249  gridmap_fitGauss();
2250 
2251  return(0);
2252 }
2253 
2254 
2256 {
2257  if(isSet(PF_H_FLAG))
2258  {
2260 
2261  return(0);
2262  }
2263 
2265 
2267 
2268  return(0);
2269 }
2270 
2271 
2273 {
2274  int i, n=0, ret, delay=0, first = 1, w, h, append=0;
2275  struct HEADER *p;
2276 
2277  if(isSet(PF_H_FLAG))
2278  {
2280 
2281  return(0);
2282  }
2283 
2284  if(!env->numofscans)
2285  {
2286  st_warn("No data file loaded");
2287 
2288  return(1);
2289  }
2290 
2291  if(notSet(PF_A_FLAG)) /* Do not zero out beam_map struct if we are appending */
2292  {
2293  bzero((char *)&beam_map, sizeof(beam_map));
2294 
2295  remScanHeaders(); /* Zero out scan Headers */
2296  }
2297  else
2298  {
2299  beam_map.base_removed = 0; /* Allow baselines to be removed again */
2300  append = 1;
2301  }
2302 
2303  if(isSet(PF_D_FLAG))
2304  {
2305  delay = 1;
2306  }
2307 
2308  p = &sa[0];
2309 
2310  for(i=0;i<env->numofscans;i++,p++)
2311  {
2312  if(!meetsCritiria(p, CHECK_ALL))
2313  {
2314  continue;
2315  }
2316 
2317  if(strncmp(p->obsmode, "CONTMAP", 7))
2318  {
2319  continue;
2320  }
2321 
2322  if(isScanIgnored(p->scan))
2323  {
2324  continue;
2325  }
2326 
2327  if((ret = read_scan( p->scan, env->filename, h0->yplot1, &h0->head, 0)) > 0.0)
2328  {
2329  env->curScan = p->scan;
2330 
2331  w = (int)h0->head.noxpts;
2332  h = (int)h0->head.noypts;
2333 
2334  if(w == 0 || h == 0 || w > MAX_MAP_W || h > MAX_MAP_H)
2335  {
2336  fprintf(stderr, "Error in grid map size: %d %d\n", w, h);
2337 
2338  return(1);
2339  }
2340 
2341  if(first)
2342  {
2343  beam_map.map_w = w;
2344  beam_map.map_h = h;
2349 
2350  st_report("Load Grid Map: %d X %d", beam_map.map_w, beam_map.map_h);
2351 
2352  /* Set plot extents */
2353  beam_map.plot_xb = 0;
2355  beam_map.plot_yb = 0;
2357 
2358  if(!append)
2359  {
2360  beam_map.fscan = h0->head.scan;
2361  }
2362 
2363  first = 0;
2364  }
2365 
2366  if(w != beam_map.map_w || h != beam_map.map_h)
2367  {
2368  st_fail("Scan %.2f, has incorrect gridmap size: %d x %d", h0->head.scan, w, h);
2369 
2370  return(1);
2371  }
2372 
2373  showScan();
2374 
2375  if(delay)
2376  {
2377  usleep(env->loopdelay1);
2378  }
2379 
2380  if(!gridmap_grid_data(h0->yplot1, &h0->head))
2381  {
2382  n++;
2383  beam_map.nscans++;
2384 
2385  beam_map.lscan = h0->head.scan;
2386  }
2387  }
2388  else
2389  {
2390  st_fail("Unable to read scan %.2f", p->scan);
2391  }
2392  }
2393 
2394  if(n)
2395  {
2396  st_report("Found %d Scan(s) out of %d Matching Criteria", n, env->numofscans);
2397 
2398  if(notSet(PF_N_FLAG))
2399  {
2400  mapBaseline();
2401  }
2402  else
2403  {
2404  st_report("Skipping baseline removal");
2405  }
2406 
2407  validate(CONT_MAP);
2408  }
2409  else
2410  {
2411  st_report("No Scans Matched Criteria or all already included");
2412  }
2413 
2414  return(0);
2415 }
2416 
2417 
2419 {
2420  char sysBuf[256];
2421 
2422  if(isSet(PF_H_FLAG))
2423  {
2425 
2426  return(0);
2427  }
2428 
2429  sprintf(sysBuf, "xgraphic load '/tmp/%s/bmap.dem'", session);
2430 
2431  parseCmd(sysBuf);
2432 
2433  return(0);
2434 }
2435 
2436 
2437 
2439 {
2440  int bl;
2441 
2442  if(strlen(args->secondArg))
2443  {
2444  bl = atoi(args->secondArg);
2445 
2446  if(bl > -10 && bl < 10)
2447  {
2449  {
2450  st_report("Setting Map Baseline Region to %d samples", bl);
2451  }
2452 
2453  env->mapbaselines = bl;
2454  }
2455  else
2456  {
2457  st_warn("Map Baselines of %d out of range: -10 to +10", bl);
2458  }
2459  }
2460 
2461  return(0);
2462 }
2463 
2464 
2466 {
2467  if(!args->secondArg || !args->thirdArg)
2468  {
2469  st_warn("Missing Args for mapangle");
2470  }
2471 
2472  env->map_angle[0] = atof(args->secondArg);
2473  env->map_angle[1] = atof(args->thirdArg);
2474 
2476  {
2477  st_report("Setting Map Angle to %.1f %.1f", env->map_angle[0], env->map_angle[1]);
2478  }
2479 
2480  return(0);
2481 
2482 }
2483 
2484 
2486 {
2487  double scan;
2488  double bscan, escan;
2489 
2490  if(!args->secondArg)
2491  {
2492  st_warn("Missing Args for center_row()");
2493 
2494  return(1);
2495  }
2496 
2498 
2499  bscan = env->bscan;
2500  escan = env->escan;
2501 
2502  scan = floor(atof(args->secondArg));
2503 
2505  {
2506  st_report("Analysing Center Map Row %.0f", scan);
2507  }
2508 
2509  env->bscan = scan;
2510  env->escan = scan;
2511 
2512  gridMapGrid();
2513 
2514  mapGaussFit();
2515 
2516  env->bscan = bscan;
2517  env->escan = escan;
2518 
2519  return(0);
2520 }
2521 
2522 
2524 {
2525  int i, n=0, pause = 0, ret, first = 1;
2526  struct HEADER *p;
2527  double bscan, escan, err, error;
2528  double bsep, bw1t, bw2t, peak1, peak2, ratio;
2529  struct BEAM_MAPS_SEP *bms;
2530  char *symbol;
2531 
2532  if(!env->numofscans)
2533  {
2534  st_warn("No data file loaded");
2535 
2536  return(1);
2537  }
2538 
2539  if(getYesNoQuit("Pause Between Scans: y/N ?"))
2540  {
2541  pause = 1;
2542  }
2543 
2544  bscan = env->bscan;
2545  escan = env->escan;
2546 
2547  p = &sa[0];
2548  bms = &beam_map_sep[0];
2549 
2550  for(i=0;i<env->numofscans;i++,p++)
2551  {
2552  if(!meetsCritiria(p, CHECK_ALL))
2553  {
2554  continue;
2555  }
2556 
2557  if(strncmp(p->obsmode, "CONTMAP", 7))
2558  {
2559  continue;
2560  }
2561 
2562  if(isScanIgnored(p->scan))
2563  {
2564  continue;
2565  }
2566 
2567  if(p->noypts > 1)
2568  {
2569  continue;
2570  }
2571 
2572  subArgs(2, "%.0f", p->scan);
2573 
2574  if(first)
2575  {
2576  clearScreen();
2577  first = 0;
2578  }
2579 
2580  gotoxy(1, 1);
2581 
2582  centerRow();
2583 
2584  if(pause)
2585  {
2586  mapGaussPlot();
2587 
2588  ret = getYesNoQuit("Any key to continue; 'q' to quit: [*,q]?");
2589 
2590  if(ret == 2)
2591  {
2592  st_report("Aborting Plots");
2593  pause = 0;
2594  }
2595  }
2596 
2597  bms->scan = p->scan;
2599  bms->bw1 = beam_map.gmap_bw1;
2600  bms->bw2 = beam_map.gmap_bw2;
2601  bms->peak1 = beam_map.gmap_peak1;
2602  bms->peak2 = beam_map.gmap_peak2;
2603  bms->ratio = bms->peak1 / bms->peak2;
2604 
2605  /* the above functions messes with the bscan and escan; reset them here */
2606  env->bscan = bscan;
2607  env->escan = escan;
2608 
2609  n++;
2610  bms++;
2611  }
2612 
2613  if(n)
2614  {
2615  st_report("Found %d Scan(s) out of %d Matching Criteria", n, env->numofscans);
2616 
2617  st_report("Results of %d map scans", n);
2618  st_report("");
2619 
2620  st_print(" Scan# Seperation Error BW1 BW2 PK1 PK2 Ratio\n");
2621 
2622  error = 0.0;
2623  bw1t = bw2t = 0.0;
2624  peak1 = peak2 = ratio = 0.0;
2625 
2626  bms = &beam_map_sep[0];
2627  for(i=0;i<n;i++,bms++)
2628  {
2629  bw1t += bms->bw1;
2630  bw2t += bms->bw2;
2631  peak1 += bms->peak1;
2632  peak2 += bms->peak2;
2633  ratio += bms->ratio;
2634  }
2635 
2636  bw1t /= (double)n;
2637  bw2t /= (double)n;
2638  peak1 /= (double)n;
2639  peak2 /= (double)n;
2640  ratio /= (double)n;
2641 
2642  bsep = fabs(h0->head.pbeam[0] - h0->head.mbeam[0]);
2643 
2644  bms = &beam_map_sep[0];
2645  for(i=0;i<n;i++,bms++)
2646  {
2647  err = bms->seperation - bsep;
2648 
2649  if(bms->ratio < 0.995)
2650  {
2651  symbol = "->";
2652  }
2653  else
2654  if(bms->ratio > 1.005)
2655  {
2656  symbol = "<-";
2657  }
2658  else
2659  {
2660  symbol = "==";
2661  }
2662 
2663  st_print("%.2f %.2f %6.2f %5.2f %5.2f %5.2f %5.2f %5.2f %s\n",
2664  bms->scan,
2665  bms->seperation,
2666  err,
2667  bms->bw1,
2668  bms->bw2,
2669  bms->peak1,
2670  bms->peak2,
2671  bms->ratio,
2672  symbol);
2673 
2674  error += err;
2675  }
2676 
2677  error /= (double)n;
2678 
2679  st_print("---------------------------\n");
2680 
2681  st_print(" %3d Scans: %.2f %7.3f %5.2f %5.2f %5.2f %5.2f %5.2f\n\n",
2682  n, bsep, error, bw1t, bw2t, peak1, peak2, ratio);
2683  }
2684  else
2685  {
2686  st_fail("No Scans Matched Criteria");
2687  }
2688 
2689  return(0);
2690 }
2691 
2692 
2694 {
2695  double scan;
2696 
2697  if(strlen(args->secondArg))
2698  {
2699  scan = atof(args->secondArg);
2700 
2701  sprintf(middleRowFname, "/tmp/%s/middle_row.%04.2f.dat", session, scan);
2702 
2704  }
2705 
2706  return(0);
2707 }
2708 
2709 
2711 float *d;
2712 int len;
2713 double scan;
2714 {
2715  int i, j=0, nb, start, end;
2716  double xd[32], yd[32], sig[32];
2717  double a, b, siga, sigb, chi2, q;
2718 
2719  bzero((char *)&xd[0], sizeof(xd));
2720  bzero((char *)&yd[0], sizeof(yd));
2721 
2722  nb = env->mapbaselines;
2723 
2724  if(abs(nb) < 2)
2725  {
2726  st_report("Not enough baseline cells definded: %d", nb);
2727  return(0);
2728  }
2729 
2730  st_report("cont_removeBaselines(): Removing Cont Baseline On %d datapoints", len);
2731 
2732  if(nb < 0)
2733  {
2734  st_report("cont_removeBaselines(): Removing Linear Baseline Using center %d samples", nb*-1);
2735  }
2736  else
2737  {
2738  st_report("cont_removeBaselines(): Removing Linear Baseline Using %d samples on both ends", nb);
2739  }
2740 
2741  if(nb < 0)
2742  {
2743  nb *= -1;
2744 
2745  start = beam_map.map_w / 2 - nb / 2;
2746  end = beam_map.map_w / 2 + nb / 2;
2747 
2748  for(i=start;i<end;i++)
2749  {
2750  xd[j] = (float)i;
2751  yd[j] = d[i];
2752  sig[j] = 0.0;
2753 
2754  j++;
2755  }
2756  }
2757  else
2758  {
2759  for(i=0;i<nb;i++)
2760  {
2761  xd[j] = (float)i;
2762  yd[j] = d[i];
2763  sig[j] = 0.0;
2764 
2765  j++;
2766  }
2767 
2768  for(i=len-nb;i<len;i++)
2769  {
2770  xd[j] = (float)i;
2771  yd[j] = d[i];
2772  sig[j] = 0.0;
2773 
2774  j++;
2775  }
2776  }
2777 
2778  gridmap_lsqfit(xd, yd, 0, j-1, sig, 0, &a, &b, &siga, &sigb, &chi2, &q);
2779 
2780  if(a != 0.0 && b != 0.0) /* Remove a baseline where there is data */
2781  {
2782  for(i=0;i<len;i++)
2783  {
2784  d[i] = d[i] -(a+b*(float)i);
2785  }
2786  }
2787 
2788  return(0);
2789 }
2790 
2791 
2792 
2794 int w;
2795 {
2796  int id = 0;
2797  char pbuf[256], inbuf[256], *cp;
2798  FILE *fp;
2799 
2800  sprintf(pbuf, "/usr/bin/xwininfo -name 'GNUPLOT_%s %d' | grep 'Window id' | awk '{print $4}'", session, w);
2801 
2802  if( (fp = popen(pbuf, "r") ) <= (FILE *)0 )
2803  {
2804  st_fail("getPlotWinId(): Unable to open %s", pbuf);
2805 
2806  return(0);
2807  }
2808 
2809  bzero(inbuf, sizeof(inbuf));
2810 
2811  while((cp = fgets(inbuf, sizeof(inbuf), fp)) != NULL)
2812  {
2813  deNewLineIt(inbuf);
2814 
2815  id = atox(inbuf);
2816  }
2817 
2818  pclose(fp);
2819 
2820  return(id);
2821 }
2822 
2823 
2825 {
2826  int i, win, record = 0, keep = 0;
2827  double angle;
2828  char name[256], sysBuf[256];
2829 
2830  if(isSet(PF_H_FLAG))
2831  {
2833 
2834  return(0);
2835  }
2836 
2837  if(isSet(PF_R_FLAG)) /* Record the rotation ?? */
2838  {
2839  record = 1;
2840 
2841  if(isSet(PF_K_FLAG)) /* Keep intermidiate frames ?? */
2842  {
2843  keep = 1;
2844  }
2845  }
2846 
2847  setFlag(PF_R_FLAG, 0); /* Unset so as not to mess with gridplot command */
2848  setFlag(PF_K_FLAG, 0); /* Unset so as not to mess with gridplot command */
2849 
2850  if(record)
2851  {
2852  sprintf(name, "%s.%d.seq", env->source, (int)(env->if_feed*100.0));
2853 
2854  win = getPlotWinId(20);
2855 
2856  if(!win)
2857  {
2858  st_fail("gmapRotate(): Unable to determine GNUPLOT windo ID");
2859 
2860  return(1);
2861  }
2862 
2863  st_report("gmapRotate(): Using Window ID: 0x%x, filenames: %s", win, name);
2864  }
2865 
2866  doGetMapAngle();
2867 
2868  angle = env->map_angle[1]; /* We will only rotate the second angle */
2869 
2870  /* Make the equal to the last time gmapplot was called */
2871  for(i=0;i<MAX_PARSE_FLAGS;i++)
2872  {
2873  if((gmap_pflags[i].set))
2874  {
2875  setFlag(i, gmap_pflags[i].set);
2876  }
2877  }
2878 
2879  if(record) /* Delete any old ones */
2880  {
2881  st_report("Deleting any Leftover Intermidiate Frames: /tmp/%s.???.dpx", name);
2882 
2883  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
2884  system(sysBuf);
2885  }
2886 
2887 
2888  setFlag(PF_F_FLAG, 1); /* I want gridMapPlot() to ignore the current map angle and use my new one */
2889 
2890  clearInterrupt();
2891 
2892  for(i=0;i<120;i++)
2893  {
2894  angle += 3.0;
2895 
2896  if(angle > 360.0)
2897  {
2898  angle -= 360.0;
2899  }
2900 
2901  env->map_angle[1] = angle;
2902 
2903  st_report("Using Angles: %2d: %f, %f", i, env->map_angle[0], env->map_angle[1]);
2904 
2905  gridMapPlot();
2906 
2907  usleep(200000);
2908 
2909  if(isInterrupt()) /* Check for Control-C input; any will cause reading to stop */
2910  {
2911  st_print("Control-C hit; Stoping\n");
2912 
2913  if(record && !keep) /* Delete intermidiate frames */
2914  {
2915  st_report("Deleting Intermidiate Frames: /tmp/%s.???.dpx", name);
2916 
2917  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
2918  system(sysBuf);
2919  }
2920 
2921  return(0);
2922  }
2923 
2924  doGetMapAngle();
2925 
2926  if(record)
2927  {
2928  sprintf(sysBuf, "import -window 0x%x -silent /tmp/%s.%03d.dpx", win, name, i);
2929  system(sysBuf);
2930  }
2931  }
2932 
2933  if(record)
2934  {
2935  st_report("Deleting any Old Movie File: %s.m4v", name);
2936 
2937  sprintf(sysBuf, "rm -f %s.m4v", name);
2938  system(sysBuf);
2939 
2940  st_report("Creating New Movie File: %s.m4v", name);
2941 
2942  sprintf(sysBuf, "xterm -bg black -fg white -fn 10x20 -e /usr/bin/ffmpeg -r 8 \
2943  -i \"/tmp/%s.%%3d.dpx\" -crf 10 -vcodec h264 %s.m4v; sleep 5 &", name, name);
2944 
2945  strcmprs(sysBuf);
2946 
2947  system(sysBuf);
2948 
2949  if(!keep) /* Delete intermidiate frames */
2950  {
2951  st_report("Deleting Intermidiate Frames: /tmp/%s.???.dpx", name);
2952 
2953  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
2954  system(sysBuf);
2955  }
2956  }
2957 
2958  return(0);
2959 }
2960 
2961 
2962 
2964 {
2965  int i, lowerdb, maxdb, win, record = 0, keep = 0, indx = 0;
2966  double old_lowerdb, old_mapangle[2], old_zmin, old_zmax;
2967  char name[256], sysBuf[256];
2968 
2969  if(isSet(PF_H_FLAG))
2970  {
2972 
2973  return(0);
2974  }
2975 
2976  if(isSet(PF_R_FLAG)) /* Record ?? */
2977  {
2978  record = 1;
2979 
2980  if(isSet(PF_K_FLAG)) /* Keep intermidiate frames ?? */
2981  {
2982  keep = 1;
2983  }
2984  }
2985 
2986  if(args->ntokens < 2)
2987  {
2989 
2990  return(1);
2991  }
2992 
2993  maxdb = abs(atoi(args->secondArg));
2994 
2995  old_zmin = env->gmap_zmin;
2996  old_zmax = env->gmap_zmax;
2997  old_mapangle[0] = env->map_angle[0];
2998  old_mapangle[1] = env->map_angle[1];
2999  env->map_angle[0] = 180.0;
3000  env->map_angle[1] = 90.0;
3001  env->gmap_zmin = 0.0;
3002  env->gmap_zmax = 0.0;
3003 
3004  setFlag(PF_R_FLAG, 0); /* Unset so as not to mess with gridplot command */
3005  setFlag(PF_K_FLAG, 0); /* Unset so as not to mess with gridplot command */
3006 
3007  doingDive = 1;
3008 
3009  sprintf(name, "%s.%d.seq", env->source, (int)(env->if_feed*100.0));
3010 
3011 
3012  old_lowerdb = env->lowerdb;
3013 
3014  /* Make the equal to the last time gmapplot was called */
3015  for(i=0;i<MAX_PARSE_FLAGS;i++)
3016  {
3017  if((gmap_pflags[i].set))
3018  {
3019  setFlag(i, gmap_pflags[i].set);
3020  }
3021  }
3022 
3023  if(record) /* Delete any old ones */
3024  {
3025  st_report("Deleting any Leftover Intermidiate Frames: /tmp/%s.???.dpx", name);
3026 
3027  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
3028  system(sysBuf);
3029  }
3030 
3031  setFlag(PF_F_FLAG, 1); /* I want gridMapPlot() to ignore the current map angle and use my new one */
3032  setFlag(PF_D_FLAG, 1); /* I want gridMapPlot() to plot in dBs */
3033 
3034  indx = 0;
3035 
3036  clearInterrupt();
3037 
3038  for(lowerdb=-1;lowerdb>-maxdb;lowerdb--)
3039  {
3040  st_report("Using LowerDB's: %2d", lowerdb);
3041 
3042  env->lowerdb = (double)lowerdb;
3043 
3044  gridMapPlot();
3045 
3046  if(record && !win) /* Can't find window before the first plot */
3047  {
3048  win = getPlotWinId(21);
3049 
3050  if(!win)
3051  {
3052  st_fail("gmapRotate(): Unable to determine GNUPLOT windo ID");
3053 
3054  doingDive = 0;
3055 
3056  env->map_angle[0] = old_mapangle[0];
3057  env->map_angle[1] = old_mapangle[1];
3058  env->gmap_zmin = old_zmin;
3059  env->gmap_zmax = old_zmax;
3060 
3061  return(1);
3062  }
3063 
3064  st_report("gmapRotate(): Using Window ID: 0x%x, filenames: %s", win, name);
3065  }
3066 
3067  usleep(200000);
3068 
3069  if(isInterrupt()) /* Check for Control-C input; any will cause reading to stop */
3070  {
3071  st_print("Control-C hit; Stoping\n");
3072 
3073  if(record && !keep) /* Delete intermidiate frames */
3074  {
3075  st_report("Deleting Intermidiate Frames: /tmp/%s.???.dpx", name);
3076 
3077  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
3078  system(sysBuf);
3079  }
3080 
3081  env->lowerdb = old_lowerdb;
3082  env->map_angle[0] = old_mapangle[0];
3083  env->map_angle[1] = old_mapangle[1];
3084  env->gmap_zmin = old_zmin;
3085  env->gmap_zmax = old_zmax;
3086  doingDive = 0;
3087 
3088  return(0);
3089  }
3090 
3091  doGetMapAngle();
3092 
3093  if(record)
3094  {
3095  sprintf(sysBuf, "import -window 0x%x -silent /tmp/%s.%03d.dpx", win, name, indx);
3096  system(sysBuf);
3097  }
3098 
3099  indx++;
3100  }
3101 
3102  clearInterrupt();
3103 
3104  for(lowerdb=-maxdb;lowerdb<0;lowerdb++)
3105  {
3106  st_report("Using LowerDB's: %2d", lowerdb);
3107 
3108  env->lowerdb = (double)lowerdb;
3109 
3110  gridMapPlot();
3111 
3112  usleep(200000);
3113  if(isInterrupt()) /* Check for Control-C input; any will cause reading to stop */
3114  {
3115  st_print("Control-C hit; Stoping\n");
3116 
3117  if(record && !keep) /* Delete intermidiate frames */
3118  {
3119  st_report("Deleting Intermidiate Frames: /tmp/%s.???.dpx", name);
3120 
3121  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
3122  system(sysBuf);
3123  }
3124 
3125  env->lowerdb = old_lowerdb;
3126  env->map_angle[0] = old_mapangle[0];
3127  env->map_angle[1] = old_mapangle[1];
3128  env->gmap_zmin = old_zmin;
3129  env->gmap_zmax = old_zmax;
3130  doingDive = 0;
3131 
3132  return(0);
3133  }
3134 
3135  doGetMapAngle();
3136 
3137  if(record)
3138  {
3139  sprintf(sysBuf, "import -window 0x%x -silent /tmp/%s.%03d.dpx", win, name, indx);
3140  system(sysBuf);
3141  }
3142 
3143  indx++;
3144  }
3145 
3146  if(record)
3147  {
3148  st_report("Deleting any Old Movie File: %s.m4v", name);
3149 
3150  sprintf(sysBuf, "rm -f %s.m4v", name);
3151  system(sysBuf);
3152 
3153  st_report("Creating New Movie File: %s.m4v", name);
3154 
3155  sprintf(sysBuf, "xterm -bg black -fg white -fn 10x20 -e /usr/bin/ffmpeg -r 8 \
3156  -i \"/tmp/%s.%%3d.dpx\" -crf 10 -vcodec h264 %s.m4v; sleep 5 &", name, name);
3157 
3158 
3159  strcmprs(sysBuf);
3160 
3161  st_report("Executing: %s", sysBuf);
3162 
3163  system(sysBuf);
3164 
3165  if(!keep) /* Delete intermidiate frames */
3166  {
3167  st_report("Deleting Intermidiate Frames: /tmp/%s.???.dpx", name);
3168 
3169  sprintf(sysBuf, "rm -f /tmp/%s.???.dpx", name);
3170  system(sysBuf);
3171  }
3172  }
3173 
3174  env->lowerdb = old_lowerdb;
3175  env->map_angle[0] = old_mapangle[0];
3176  env->map_angle[1] = old_mapangle[1];
3177  env->gmap_zmin = old_zmin;
3178  env->gmap_zmax = old_zmax;
3179  doingDive = 0;
3180 
3181  return(0);
3182 }
3183 
3184 
3185 
3187 {
3188  int i, j, x, n, indx1, indx2, ret, hm, glen;
3189  double xdata[512], ydata[512];
3190  double arcsec, max1=0.0, max2=0.0;
3191  double xd[512], yd[512], sig[512];
3192  double aa1[4], aa2[4], peak1, peak2, beam1, beam2;
3193  double g1, g2, y, dyda[4];
3194 
3195  glen = 5;
3196 
3197  bzero((char *)&xd[0], sizeof(xd));
3198  bzero((char *)&yd[0], sizeof(yd));
3199 
3200  hm = cont_plain_send.dlen / 2; /* Half row */
3201 
3202  arcsec = cont_plain_send.head.deltaxr;
3203 
3204  st_report( "Fitting Gaussian Using Scan(s) %.2f; Arcsec = %.1f", cont_plain_send.head.scan, arcsec);
3205 
3206  n = cont_plain_send.dlen;
3207 
3208  for(x=0;x<n;x++) /* Create the x,y-axis data; Find Min/Max too */
3209  {
3210  xdata[x] = cont_plain_send.xdata[x];
3211  ydata[x] = cont_plain_send.ydata[x];
3212 
3213  sig[x] = 1.0;
3214 
3215  if(ydata[x] > max1)
3216  {
3217  max1 = ydata[x];
3218  indx1 = x;
3219  }
3220  if(ydata[x] < max2)
3221  {
3222  max2 = ydata[x];
3223  indx2 = x;
3224  }
3225  }
3226 
3227  // Set up for gausian fitting
3228 
3229  if(max1 == 0.0 || max2 == 0.0)
3230  {
3231  st_fail("No data to do gausfit");
3232  return(1);
3233  }
3234 
3235  st_report("Guess, Plus Peak: %7.2f @ Sample %d or %.2f\"", max1, indx1, xdata[indx1]);
3236  st_report("Guess, Minus Peak: %7.2f @ Sample %d or %.2f\"", max2, indx2, xdata[indx2]);
3237  st_report("Guess, Beam Sep: %7.2f\"", fabs(indx1-indx2) * arcsec);
3238 
3239  /* Compute ideal gaussian 1/2 power width in samples */
3240  glen = (int)(cont_plain_send.head.bfwhm / arcsec) + 1;
3241 
3242 // Load first peak
3243  for(i=indx1-glen,j=0;i<indx1+glen;i++,j++)
3244  {
3245  xd[j] = xdata[i];
3246  yd[j] = ydata[i];
3247  }
3248 
3249  st_report("Fitting a %d Point Gauss to Plus Peak", j+1);
3250  aa1[1] = max1;
3251  aa1[2] = xdata[indx1];
3252  aa1[3] = (double)j * arcsec; /* Beam width */
3253 
3254  ret = gauss_fit(&xd[0], &yd[0], sig, j, aa1, isSet(PF_V_FLAG));
3255 
3256  if(!ret)
3257  {
3258  st_fail("1st Half Gauss Fit Failed");
3259 
3260  for(i=0;i<j;i++)
3261  {
3262  st_print("%2d %4.1f %4.1f\n", i, xd[i], yd[i]);
3263  }
3264 
3265  return(1);
3266  }
3267 
3268  st_report("Found Plus Beam @ %7.2f\", Peak = %7.2f, Beam Width = %7.2f", aa1[2], aa1[1], fabs(aa1[3]));
3269 
3270  peak1 = aa1[2];
3271  beam1 = fabs((aa1[2] - xdata[hm]));
3272 
3273 // Load second peak
3274  for(i=indx2-glen,j=0;i<indx2+glen;i++,j++)
3275  {
3276  xd[j] = xdata[i];
3277  yd[j] = ydata[i];
3278  }
3279 
3280  st_report("Fitting a %d Point Gauss to Minus Peak", j+1);
3281  aa2[1] = max2;
3282  aa2[2] = xdata[indx2];
3283  aa2[3] = (double)j * arcsec;
3284 
3285  ret = gauss_fit(&xd[0], &yd[0], sig, j, aa2, isSet(PF_V_FLAG));
3286 
3287  if(!ret)
3288  {
3289  st_fail("2nd Half Gauss Fit Failed");
3290 
3291  for(i=0;i<j;i++)
3292  {
3293  st_print("%2d %4.1f %4.1f\n", i, xd[i], yd[i]);
3294  }
3295 
3296  return(1);
3297  }
3298 
3299  st_report("Found Minus Beam @ %7.2f\", Peak = %7.2f, Beam Width = %7.2f", aa2[2], aa2[1], fabs(aa2[3]));
3300 
3301  peak2 = aa2[2];
3302  beam2 = fabs((aa2[2] - xdata[hm]));
3303 
3304  st_report("Plus Beam +%7.2f\" From Center", beam2);
3305  st_report("Minus Beam -%7.2f\" From Center", beam1);
3306  st_report("Beam Sep: %7.2f\"", fabs(peak2-peak1));
3307 
3308  sprintf(cont_plain_send.glabels[0], "Plus Beam +%7.2f Arc-Seconds From Center", beam2);
3309  sprintf(cont_plain_send.glabels[1], "Minus Beam -%7.2f Arc-Seconds From Center", beam1);
3310  sprintf(cont_plain_send.glabels[2], "Beam Sep: %7.2f Arc-Seconds", fabs(peak2-peak1));
3311 
3312  /* Populate the gdata array */
3313  for(x=0;x<n;x++)
3314  {
3315  fgauss(cont_plain_send.xdata[x], aa1, &y, dyda);
3316  g1 = y;
3317 
3318  fgauss(cont_plain_send.xdata[x], aa2, &y, dyda);
3319  g2 = y;
3320 
3321  cont_plain_send.gdata[x] = g1 + g2;
3322  }
3323 
3324  cont_plain_send.ngauss = x;
3325 
3326  if(xgraphic)
3327  {
3329  }
3330 
3331  return(0);
3332 }
3333 
3334 
3335 /* Remove a baseline from the loaded CONTMAP scane*/
3337 int pf, nb;
3338 {
3339  int x, j, n, start, end;
3340  double xd[MAX_CONT_SAMPLES], yd[MAX_CONT_SAMPLES], sig[MAX_CONT_SAMPLES];
3341  double a, b, siga, sigb, chi2, q;
3342 
3343  if(!pf)
3344  {
3345  return(0);
3346  }
3347 
3348  if(nb == 0)
3349  {
3350  st_report("mapbaselines set to 0; skipping removal");
3351 
3352  return(0);
3353  }
3354 
3355  if(nb < 0)
3356  {
3357  st_report("removeContMapBaselines(): Removing Linear Baseline Using Center %d samples", abs(nb));
3358  }
3359  if(pf == 2)
3360  {
3361  st_report("removeContMapBaselines(): Removing Linear Baseline Using %d samples on both ends + middle", nb);
3362  }
3363  else
3364  {
3365  st_report("removeContMapBaselines(): Removing Linear Baseline Using %d samples on both ends", nb);
3366  }
3367 
3368  bzero((char *)&xd[0], sizeof(xd));
3369  bzero((char *)&yd[0], sizeof(yd));
3370 
3371  n = cont_plain_send.dlen;
3372 
3373  j = 0;
3374 
3375  if(nb < 0) /* Take them from the middle */
3376  {
3377  start = (n / 2) - (abs(nb) / 2);
3378  end = (n / 2) + (abs(nb) / 2);
3379 
3380  for(x=start;x<end;x++)
3381  {
3382  xd[j] = cont_plain_send.xdata[x];
3383  yd[j] = cont_plain_send.ydata[x];
3384 
3385  j++;
3386  }
3387  }
3388  else
3389  if(pf == 2) /* Take them from the beginning middle and end */
3390  {
3391  for(x=0;x<abs(nb);x++)
3392  {
3393  xd[j] = cont_plain_send.xdata[x];
3394  yd[j] = cont_plain_send.ydata[x];
3395 
3396  j++;
3397  }
3398 
3399  start = (n / 2) - (abs(nb) / 2);
3400  end = (n / 2) + (abs(nb) / 2);
3401 
3402  for(x=start;x<end;x++)
3403  {
3404  xd[j] = cont_plain_send.xdata[x];
3405  yd[j] = cont_plain_send.ydata[x];
3406 
3407  j++;
3408  }
3409 
3410  for(x=n-abs(nb);x<n;x++)
3411  {
3412  xd[j] = cont_plain_send.xdata[x];
3413  yd[j] = cont_plain_send.ydata[x];
3414 
3415  j++;
3416  }
3417  }
3418  else /* Take them from the beginning and end */
3419  {
3420  for(x=0;x<nb;x++)
3421  {
3422  xd[j] = cont_plain_send.xdata[x];
3423  yd[j] = cont_plain_send.ydata[x];
3424 
3425  j++;
3426  }
3427 
3428  for(x=n-nb;x<n;x++)
3429  {
3430  xd[j] = cont_plain_send.xdata[x];
3431  yd[j] = cont_plain_send.ydata[x];
3432 
3433  j++;
3434  }
3435  }
3436 
3437  gridmap_lsqfit(xd, yd, 0, j-1, sig, 0, &a, &b, &siga, &sigb, &chi2, &q);
3438 
3439  if(a != 0.0 && b != 0.0) /* Remove a baseline where there is data */
3440  {
3441  for(x=0;x<n;x++)
3442  {
3444  }
3445  }
3446 
3447  if(xgraphic)
3448  {
3450  }
3451 
3452  return(0);
3453 }
3454 
3455 
3457 {
3458  char tbuf[256];
3459  double scanno;
3460 
3461  if(args->ntokens < 2)
3462  {
3464 
3465  return(1);
3466  }
3467 
3468  /* Set these to '0' so x-axis is set properly */
3469  env->map_w = 0;
3470  env->map_h = 0;
3471 
3472  scanno = atof(args->secondArg);
3473 
3474  sprintf(tbuf, "get %.2f", scanno);
3475  parseCmd(tbuf);
3476 
3477  strcpy(tbuf, "show");
3478  parseCmd(tbuf);
3479 
3481 
3482  if(isSet(PF_G_FLAG))
3483  {
3484  contMapFitGauss();
3485  }
3486 
3487  return(0);
3488 }
3489 
3490 
3492 {
3493  int ret;
3494 
3495  if(isSet(PF_H_FLAG))
3496  {
3498 
3499  return(0);
3500  }
3501 
3502  ret = isSet(PF_Y_FLAG);
3503  if(!ret) /* Ask */
3504  {
3505  ret = getYesNoQuit("Delete Internal Grid Map: y/N ?");
3506  }
3507 
3508  if(ret)
3509  {
3510  st_report("Deleting Internal Grid Map");
3511 
3512  bzero((char *)&beam_map, sizeof(beam_map));
3513 
3514  remScanHeaders(); /* Zero out scan Headers */
3515 
3516  env->map_w = 0;
3517  env->map_h = 0;
3518  }
3519  else
3520  {
3521  st_report("Skipping Grid Map Deleting");
3522  }
3523 
3524  return(0);
3525 }
3526 
3527 
3528 /* Just a frontend to gmap functions */
3529 /* CAUTION: Flags are overloaded here */
3530 int doGmap()
3531 {
3532  int indx;
3533  struct PARSE_FLAGS *p;
3534 
3535  p = findFirstFlag();
3536 
3537  if(p)
3538  {
3539  indx = p->index; /* Save value */
3540 
3541  shiftFlags(); /* Remove this first flag and decriment is 'set' count */
3542 
3543  switch(indx)
3544  {
3545  case PF_B_FLAG: strcpy(args->firstArg, "gmapmapbase"); mapBaseline(); break; /* Remove Baseline from Gridded Map */
3546  case PF_C_FLAG: strcpy(args->firstArg, "gmapslice"); gridMapSlice(); break; /* Cut a slice of map */
3547  case PF_D_FLAG: strcpy(args->firstArg, "gmapdive"); gridMapDive(); break; /* Plot Grid Map with varing dB's */
3548  case PF_E_FLAG: strcpy(args->firstArg, "gmapclear"); doGmapClear(); break; /* Clear out internal Gridmap data */
3549  case PF_F_FLAG: strcpy(args->firstArg, "gmapgauss"); mapGaussFit(); break; /* Fit Gaussian to a Gridded Map Center*/
3550  case PF_G_FLAG: strcpy(args->firstArg, "gmapgrid"); gridMapGrid(); break; /* Accumulate Selected CONTMAP Scans */
3551  case PF_L_FLAG: strcpy(args->firstArg, "gmaplimits"); gridMapLimits(); break; /* Set / Reset Plotting Limits */
3552  case PF_P_FLAG: strcpy(args->firstArg, "gmapplot"); gridMapPlot(); break; /* Plot 3D Gaussian Gridded Map */
3553  case PF_R_FLAG: strcpy(args->firstArg, "gmaprotate"); gmapRotate(); break; /* Rotate Gridmap and Snap images */
3554  case PF_S_FLAG: strcpy(args->firstArg, "gmapstats"); gridMapStats(); break; /* Display Stats of Grid Map */
3555  case PF_X_FLAG: strcpy(args->firstArg, "gmapstats"); gridMapGrab(); break; /* Force Gridmap Window to be Active */
3556 
3557  default: printH_FlagHelp(args->firstArg); break;
3558  }
3559 
3560  return(0);
3561  }
3562 
3564 
3565  return(0);
3566 }
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
#define SQR(a)
Definition: gridmap.c:72
int shiftFlags()
Definition: parse.c:622
double scan
Definition: header.h:35
int clearFillContStruct(struct HEADER *h, int dlen, char *xl, char *yl)
Definition: cont.c:1726
int getPlotWinId(int w)
Definition: gridmap.c:2793
double gmap_zmin
Definition: env.h:295
double curScan
Definition: env.h:178
#define PSPLOT
Definition: defines.h:30
double pbeam[2]
Definition: header.h:146
float rval
Definition: gridmap.h:21
float map_step_y
Definition: gridmap.h:48
char * strcmprs(char *str)
int plot_yb
Definition: gridmap.h:36
double beamthrow
Definition: gridmap.h:65
int centerRow()
Definition: gridmap.c:2485
int removeContMapBaselines(int pf, int nb)
Definition: gridmap.c:3336
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
int interp(double *array, int nx, int ny, double *parray, int npx, int npy)
Definition: interp.c:102
#define MAX_PARSE_FLAGS
Definition: parse.h:80
int deNewLineIt(char *buf)
Definition: parse.h:8
int loopdelay1
Definition: env.h:67
int doingDive
Definition: gridmap.c:70
double scan
Definition: gridmap.c:79
int scan
double noxpts
Definition: header.h:106
double sqrarg
Definition: gridmap.c:65
double samprat
Definition: header.h:68
char source[SRCLEN+1]
Definition: dataserv.c:52
#define PLOT_STYLE_LINES
Definition: defines.h:101
int read_scan()
int gotoxy(int x, int y)
Definition: gridmap.c:257
int plot_ye
Definition: gridmap.h:37
int gridMapDive()
Definition: gridmap.c:2963
double bfwhm
Definition: header.h:114
float scan
Definition: gridmap.c:94
float map_size_y
Definition: gridmap.h:46
int plot_xb
Definition: gridmap.h:34
float tsys
Definition: gridmap.h:18
FILE * fp
Definition: linuxpops.c:70
#define max(a, b)
Definition: cactus.h:115
float azoff
Definition: gridmap.h:13
int gridmap_showMiddleRow()
Definition: gridmap.c:304
Definition: header.h:13
int isInterrupt()
Definition: interrupt.c:152
char * fifthArg
Definition: parse.h:184
int printH_FlagHelp(char *cmd)
Definition: help.c:430
double seperation
Definition: gridmap.c:80
int gridmap_grid_data(float *data, struct HEADER *h)
Definition: gridmap.c:1583
void fgauss()
int gauss_fit()
int map_h
Definition: gridmap.h:32
#define MAX_NUM_BEAM_SEP
Definition: gridmap.c:74
float xdata[MAX_CONT_SAMPLES]
Definition: accum.h:117
float itime
Definition: gridmap.h:19
char yaxis_label[80]
Definition: gridmap.h:28
struct HEADER * sa
Definition: linuxpops.c:72
int setFlag(int flag, int state)
Definition: parse.c:685
char glabels[MAX_GLABELS][80]
Definition: accum.h:110
int setMapAngle()
Definition: gridmap.c:2465
struct PARSE_FLAGS pflags[MAX_PARSE_FLAGS]
Definition: parse.h:178
float ydata[MAX_CONT_SAMPLES]
Definition: accum.h:118
struct BEAM_MAPS_SEP beam_map_sep[MAX_NUM_BEAM_SEP]
Definition: gridmap.c:88
int index
Definition: parse.h:87
int doGmapClear()
Definition: gridmap.c:3491
int base_removed
Definition: gridmap.h:40
float global_gdata[MAX_CONT_SAMPLES]
Definition: gridmap.c:67
#define MAX_GMAP_SCANS
Definition: gridmap.h:5
struct SCAN_HEADERS * scan_headers
Definition: gridmap.c:101
int offsets_filled
Definition: gridmap.h:42
struct PARSE_FLAGS gmap_pflags[MAX_PARSE_FLAGS]
Definition: gridmap.c:90
int mBaselines()
Definition: gridmap.c:2438
int processContinuum(struct HEADER *head, float *phases, float *data, int *dl, char *mode)
Definition: cont.c:2035
int gridmap_findMinMax()
Definition: gridmap.c:1398
struct BEAM_MAP beam_map
Definition: gridmap.c:61
int clearInterrupt()
Definition: interrupt.c:144
int notSet(int flag)
Definition: parse.c:669
struct HEADER head
Definition: accum.h:115
int gridMapPlot()
Definition: gridmap.c:2064
int getYesNoQuit()
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int gridMapLimits()
Definition: gridmap.c:1025
struct HEADER * checkScanHeader(int v, float s)
Definition: gridmap.c:131
int log_msg(const char *fmt,...)
int mapGaussFit()
Definition: gridmap.c:2240
char * thirdArg
Definition: parse.h:182
char source[64]
Definition: env.h:11
int gridMapGrid()
Definition: gridmap.c:2272
int gridmap_goWriteCenterRow(double *xdata, double *ydata, double *aa1, double *aa2, int n, double scanno)
Definition: gridmap.c:267
int isSet(int flag)
Definition: parse.c:646
int centerAll()
Definition: gridmap.c:2523
struct HEADER head
Definition: image.h:104
float gmap_results
Definition: gridmap.h:55
int gridmap_write_datafile()
Definition: gridmap.c:323
double ratio
Definition: gridmap.c:85
double limitValue(double l, double v, double u)
Definition: gridmap.c:1705
double lowerdb
Definition: env.h:222
float map_max
Definition: gridmap.h:51
#define MAX_MAP_W
Definition: gridmap.h:1
float pval
Definition: gridmap.h:22
int findMapColIndx(double x1, int y1)
Definition: gridmap.c:1820
float map_size_x
Definition: gridmap.h:45
double peak1
Definition: gridmap.c:83
void st_warn()
int nscans
Definition: gridmap.h:41
int clearScreen()
Definition: gridmap.c:246
char obsmode[8]
Definition: header.h:41
double mbeam[2]
Definition: header.h:147
char backend[8]
Definition: header.h:43
double gmap_gamma
Definition: env.h:297
void st_fail()
struct PARSE_FLAGS * findFirstFlag()
Definition: parse.c:602
float gmap_peak1
Definition: gridmap.h:58
float gmap_bw1
Definition: gridmap.h:56
int meetsCritiria(struct HEADER *p, int mask)
Definition: observe.c:90
double ylower
Definition: env.h:287
#define MAX_MAP_H
Definition: gridmap.h:2
int map_h
Definition: env.h:74
float fscan
Definition: gridmap.h:61
int nbase
Definition: gridmap.h:39
int sendCont(int mode)
Definition: cont.c:1827
struct HEADER head
Definition: gridmap.c:96
int prepareMap()
Definition: gridmap.c:1732
int use_global_gdata
Definition: gridmap.c:68
int mapBaseline()
Definition: gridmap.c:2255
int showMidRow()
Definition: gridmap.c:2693
int gridMapStats()
Definition: gridmap.c:455
double peak2
Definition: gridmap.c:84
float lscan
Definition: gridmap.h:62
double stsys
Definition: header.h:203
int isScanIgnored(double scan)
Definition: ignore.c:181
#define VERBOSE_GRIDMAP
Definition: defines.h:77
char session[80]
Definition: strings.c:67
struct CONTINUUM_PLAIN cont_plain_send
Definition: cont.c:63
struct HEADER * headers[MAX_GMAP_SCANS]
Definition: gridmap.h:24
struct BEAM_MAP_ELEM bmes[MAX_MAP_W][MAX_MAP_H]
Definition: gridmap.h:67
float eloff
Definition: gridmap.h:14
int cont_removeBaselines(float *d, int len, double scan)
Definition: gridmap.c:2710
float map_min
Definition: gridmap.h:50
void st_report()
double gmap_zmax
Definition: env.h:296
float bdata[MAX_ATM_ELEMENTS]
Definition: atm.c:94
Definition: parse.h:9
float xdata[MAX_ATM_ELEMENTS]
Definition: atm.c:90
float map_step_x
Definition: gridmap.h:47
double if_feed
Definition: env.h:208
int showScan()
Definition: observe.c:1205
int gridmap_lsqfit(x, y, int bdata, int ndata, sig, int mwt,*a,*b,*siga,*sigb,*chi2,*q)
Definition: gridmap.c:1321
int gridmap_fitGauss()
Definition: gridmap.c:1120
double freq
Definition: gridmap.h:64
int numofscans
Definition: env.h:85
char * fourthArg
Definition: parse.h:183
int mapbaselines
Definition: env.h:71
int mapGaussPlot()
Definition: gridmap.c:2232
#define CONTMAP
Definition: defines.h:32
char middleRowFname[256]
Definition: gridmap.c:63
int plot_xe
Definition: gridmap.h:35
int doNutator()
Definition: gridmap.c:3456
float gdata[MAX_CONT_SAMPLES]
Definition: accum.h:119
double yupper
Definition: env.h:288
double bscan
Definition: env.h:173
int map_w
Definition: env.h:73
void subArgs(int wt, const char *fmt,...)
Definition: parse.c:430
float gmap_peak2
Definition: gridmap.h:59
struct HEADER * addScanHeader(int v, float s, struct HEADER *head)
Definition: gridmap.c:171
#define CHECK_ALL
Definition: defines.h:64
int plotstyle
Definition: env.h:127
char source[32]
Definition: gridmap.h:29
double ycell0
Definition: header.h:109
#define SEND_PLAIN
Definition: defines.h:134
double deltaxr
Definition: header.h:103
double deltayr
Definition: header.h:104
int map_w
Definition: gridmap.h:31
int gmapFillOffsets(struct HEADER *h)
Definition: gridmap.c:1561
int findMapRowIndx(int x1, double y1)
Definition: gridmap.c:1859
int scanAlreadyLoaded(struct BEAM_MAP_ELEM *p, double scan)
Definition: gridmap.c:1540
int validate(int which)
Definition: linuxpops.c:164
char * secondArg
Definition: parse.h:181
float wt
Definition: gridmap.h:20
void st_print()
#define m(i)
int ntokens
Definition: parse.h:173
double map_angle[2]
Definition: env.h:225
double bw1
Definition: gridmap.c:81
#define MAX_CONT_SAMPLES
Definition: accum.h:26
double escan
Definition: env.h:186
int gmapRotate()
Definition: gridmap.c:2824
int gridMapGrab()
Definition: gridmap.c:2418
int contMapFitGauss()
Definition: gridmap.c:3186
float ydata[MAX_ATM_ELEMENTS]
Definition: atm.c:91
struct HEADER * do_addHeader(int ver, float scan, struct HEADER *head)
Definition: gridmap.c:104
int doGetMapAngle()
Definition: linuxpops.c:2448
int doMapPlot(int how)
Definition: gridmap.c:2203
float gmap_bw2
Definition: gridmap.h:57
int doGmap()
Definition: gridmap.c:3530
int gridMapSlice()
Definition: gridmap.c:1898
double bw2
Definition: gridmap.c:82
int remScanHeaders()
Definition: gridmap.c:224
char object[16]
Definition: header.h:40
char * firstArg
Definition: parse.h:180
int gridmap_writeDemFile(int off, int how)
Definition: gridmap.c:787
double noypts
Definition: header.h:107
double restfreq
Definition: header.h:198
int parseCmd(char *buf)
Definition: parse.c:2090
#define CONT_MAP
Definition: observing_par.h:45
int delay(double dc)
int gridmap_removeBaselines(int nb)
Definition: gridmap.c:1448
int gridmap_write_csvfile()
Definition: gridmap.c:368
char filename[256]
Definition: env.h:9
#define GNUPLOT
Definition: defines.h:29
FILE * errPrint_fp
Definition: linuxpops.c:70
struct SCAN_HEADERS * next
Definition: gridmap.c:98
int freeY()
Definition: paxis.c:488
int verboseLevel
Definition: linuxpops.c:80
int atox(char *s)