Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gauss.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include "extern.h"
60 
61 /* LINE Gaussian Routines */
62 //struct GAUSSIAN_FIT {
63 // int xmode;
64 // double peak;
65 // double center;
66 // double hp;
67 // double sum;
68 //};
69 
71 
72 
73 int printGauss(image)
74 struct LP_IMAGE_INFO *image;
75 {
76  int i, n;
77 
78  bzero((char *)&sat.gauss, sizeof(sat.gauss));
79 
80  sprintf(sat.gauss.results[0].string, "Results Of Last Gaussian Fit");
81 
82  if(env->jfact == 0.0)
83  {
84  if(env->issmt)
85  {
87  }
88  else
89  {
91  }
92  }
93 
94  switch(gfit.xmode)
95  {
96  case XMODE_VEL:
97  sprintf(sat.gauss.results[1].string, "Peak (K): %9.3f", gfit.peak);
98  sprintf(sat.gauss.results[2].string, "Peak Velocity (KM/s): %9.3f", gfit.center);
99  sprintf(sat.gauss.results[3].string, "Peak HP Width (KM/s): %9.3f", gfit.hp);
100  sprintf(sat.gauss.results[4].string, "Sum Under Gauss (K): %9.3f", gfit.sumK);
101  sprintf(sat.gauss.results[5].string, "Sum Under Gauss (Jk km/s): %9.3f", gfit.sumKm * env->jfact);
102  n = 6;
103  break;
104 
105  case XMODE_CHAN:
106  sprintf(sat.gauss.results[1].string, "Peak (K): %9.3f", gfit.peak);
107  sprintf(sat.gauss.results[2].string, "Peak Channel : %9.3f", gfit.center);
108  sprintf(sat.gauss.results[3].string, "Peak HP Width (in chans): %9.3f", gfit.hp);
109  sprintf(sat.gauss.results[4].string, "Sum Under Gauss (K): %9.3f", gfit.sumK);
110  sprintf(sat.gauss.results[5].string, "Sum Under Gauss (Jk km/s): %9.3f", gfit.sumKm * env->jfact);
111  n = 6;
112  break;
113 
114  case XMODE_FREQ:
115  sprintf(sat.gauss.results[1].string, "Peak (K): %9.3f", gfit.peak);
116  sprintf(sat.gauss.results[2].string, "Peak Frequency (MHz): %9.3f", gfit.center);
117  sprintf(sat.gauss.results[3].string, "Rest Frequency (MHz): %12.6f", (image->head.restfreq+gfit.center) / 1000.0);
118  sprintf(sat.gauss.results[4].string, "Peak HP Width (MHz): %9.3f", gfit.hp);
119  sprintf(sat.gauss.results[5].string, "Sum Under Gauss (K): %9.3f", gfit.sumK);
120  sprintf(sat.gauss.results[6].string, "Sum Under Gauss (Jk km/s): %9.3f", gfit.sumKm * env->jfact);
121  n = 7;
122  break;
123  }
124 
125  if(image->xaxisotfoff)
126  {
127  sprintf(sat.gauss.results[1].string, "Peak (K): %9.3f", gfit.peak);
128  sprintf(sat.gauss.results[2].string, "Peak Offset (arcsecs): %9.3f", gfit.center);
129  sprintf(sat.gauss.results[3].string, "Peak HP Width (arcsecs): %9.3f", gfit.hp);
130  sprintf(sat.gauss.results[4].string, "Sum Under Gauss (K): %9.3f", gfit.sumK);
131 
132  n = 6;
133  }
134 
135  for(i=0;i<n;i++)
136  {
138  sat.gauss.results[i].used = 1;
139  }
140 
141  return(0);
142 }
143 
144 
145 int clearGauss(image, clear)
146 struct LP_IMAGE_INFO *image;
147 int clear;
148 {
149  if(clear)
150  {
151  bzero((char *)&image->ginfo, sizeof(struct GAUSSIAN)); /* Clear the gaussian struct */
152  }
153 
154  bzero((char *)&gfit, sizeof(gfit));
155 
156  bzero((char *)&sat.gauss, sizeof(sat.gauss));
157 
158  sendSat();
159 
160  return(0);
161 }
162 
163 
164 int copyGoodChars(out, in)
165 char *out, *in;
166 {
167  char *cp, *cp2;
168 
169  cp2 = out;
170  for(cp=in;*cp;cp++)
171  {
172  if(isalnum(*cp))
173  {
174  *cp2 = *cp;
175 
176  cp2++;
177  }
178  }
179 
180  *cp2 = '\0';
181 
182  return(0);
183 }
184 
185 
186 
187 int findLineName(f, d, buf)
188 double f, d;
189 char *buf;
190 {
191  int try, j;
192  char tryBuf[256], line[256][256], *string;
193  double fr;
194  FILE *fp;
195 
196  fr = d;
197 
198  for(try=0;try<100;try++,fr *= 1.5)
199  {
200  sprintf(tryBuf, "slovas %f %f", f, fr);
201 
202  if(isSet(PF_V_FLAG))
203  {
204  st_print("TRY(%d): %s\n", try+1, tryBuf);
205  }
206 
207  if((fp = popen(tryBuf, "r")) <= (FILE *)0)
208  {
209  st_fail("Unable to open: %s", tryBuf);
210  return(1);
211  }
212 
213  bzero(line, sizeof(line));
214 
215  j = 0;
216  while((string = fgets(line[j], sizeof(line[j]), fp)) != NULL)
217  {
218  strcmprs(line[j]);
219 
220  if(isSet(PF_V_FLAG))
221  {
222  st_print("READ(%d) GOT: %s\n", j+1, line[j]);
223  }
224 
225  j++;
226  }
227 
228  pclose(fp);
229 
230  if(j)
231  {
232  if(j == 1)
233  {
234  copyGoodChars(buf, line[j-1]);
235  st_report("findLineName(): Try: %d, Got: %s", try+1, buf);
236  }
237 
238  return(0);
239  }
240  }
241 
242  return(0);
243 }
244 
245 int gaussList(header)
246 int header;
247 {
248  int i;
249  struct LP_IMAGE_INFO *p;
250 
251  p = h0;
252 
253  if(p->ginfo.nfits)
254  {
255  if(header)
256  {
257  st_print( " Fit# Peak Offset Rest-Freq FWHM Height-Error Center-Error FWHM-Error Line-Name (Guess)\n\n");
258  }
259 
260  for(i=0;i<p->ginfo.nfits;i++)
261  {
262  st_print("%2d %9.6f %12.6f %12.8f %9.6f %9.6f %9.6f %9.6f %s\n",
263  i,
264  p->ginfo.fits[i].peak,
265  p->ginfo.fits[i].center,
266  (p->ginfo.fits[i].center+p->head.restfreq) / 1000.0,
267  p->ginfo.fits[i].hp,
268  p->ginfo.fits[i].hghterr,
269  p->ginfo.fits[i].cnterr,
270  p->ginfo.fits[i].hwerr,
271  p->ginfo.fits[i].linename);
272  }
273 
274  if(header)
275  {
276  st_print("\n");
277  }
278  }
279 
280  return(0);
281 }
282 
283 
285 {
286  char fname[256];
287  FILE *fp;
288 
289  strcpy(fname, "gaussian.results");
290 
291  fp = fopen(fname, "a");
292 
293  if(!fp)
294  {
295  st_fail("gaussRecord();Unable to open %s for appending", fname);
296  return(1);
297  }
298 
299  fprintf(fp, "%8.2f - %8.2f %10.6f %9.3f %9.3f %9.3f %9.3f %9.6f %9.6f %9.6f\n",
300  h0->ainfo.bscan,
301  h0->ainfo.escan,
302  (env->gaussCenter + h0->head.restfreq) / 1000.0,
303  env->gaussPeak,
304  env->gaussCenter,
305  env->gaussHP,
306  env->gaussSum,
307  env->hghterr,
308  env->cnterr,
309  env->hwerr);
310 
311  fclose(fp);
312 
313  st_report("gaussRecord(): Recorded Gaussian fit to %s", fname);
314 
315  return(0);
316 }
317 
318 
320 int n;
321 {
322  int i, j, ret, len;
323  int pgauss = 0, h1gauss = 0, h2gauss = 0;
324  double aa[4], y;
325  double xd[MAX_CHANS], yd[MAX_CHANS], sig[MAX_CHANS];
326  char cmdBuf[256];
327  float ydata[MAX_CHANS];
328  struct LP_IMAGE_INFO *image = h0;
329 
330  st_report("Auto Computing %d Gaussian Curves", n);
331 
332  /* First save a copy the plot data to a local array
333  because I'm going to destroy the yplot1 array in the
334  process of fitting all the peaks;
335  copy it back when down */
336 
337  bcopy((char *)&image->yplot1[0], (char *)&ydata[0], sizeof(image->yplot1));
338  len = image->head.noint;
339 
340  for(i=0;i<n;i++)
341  {
342  line_determine_min_max(image);
343 
344  st_report("Peak %d: Chan: %4d, Temp: %f", i, image->peakChan, image->datamax);
345 
346  pgauss = env->peakChan;
347 
348  /* Find the channel to the left of the line that's < 1/2 peak value */
349  for(j=0;j<100;j++)
350  {
351  if(image->yplot1[pgauss-j] < (image->yplot1[env->peakChan] / 2.0)) /* Is it 1/2 the power? */
352  {
353  break;
354  }
355  }
356 
357  if(j > 99)
358  {
359  st_fail("Autofind Gauss fit Lower HP; failed");
360  bcopy((char *)&ydata[0], (char *)&image->yplot1[0], sizeof(image->yplot1));
361  line_determine_min_max(image);
362 
363  return(1);
364  }
365 
366  h1gauss = pgauss-j;
367 
368  if(h1gauss < 0)
369  {
370  h1gauss = 0;
371  }
372  if(h2gauss >= len)
373  {
374  h1gauss = len-1;
375  }
376 
377  /* Find the channel to the right of the line that's < 1/2 peak value */
378  for(j=0;j<100;j++)
379  {
380  if(image->yplot1[pgauss+j] < (image->yplot1[env->peakChan] / 2.0)) /* Is it 1/2 the power? */
381  {
382  break;
383  }
384  }
385  if(j > 99)
386  {
387  st_fail("Autofind Gauss fit Upper HP; failed");
388  bcopy((char *)&ydata[0], (char *)&image->yplot1[0], sizeof(image->yplot1));
389  line_determine_min_max(image);
390 
391  return(1);
392  }
393  h2gauss = pgauss+j;
394 
395  st_report("With a initial guess of: Peak %f @ Chan %d", image->yplot1[pgauss], pgauss);
396  st_report("And Half-Power Points of: %d & %d", h1gauss, h2gauss);
397 
398  clearGauss(image, 0); /* clear out the local gfit */
399 
400  /* Copy data into double arrays for fitting */
401  for(j=0;j<len;j++)
402  {
403  xd[j] = image->x1data[j];
404  yd[j] = image->yplot1[j];
405  sig[j] = 1.0;
406  }
407 
408  aa[0] = image->yplot1[pgauss];
409  aa[1] = image->x1data[pgauss];
410  aa[2] = fabs(image->x1data[h1gauss]-image->x1data[h2gauss]);
411 
412  st_report("Gauss Guess: %f %f %f", aa[0], aa[1], aa[2]);
413 
414  /* Use older Unipops version of gauss_fit */
415  if(!env->usegslgauss)
416  {
417  st_report("Using Unipops Gaussian Routines");
418  /* shift up one element */
419  aa[3] = aa[2];
420  aa[2] = aa[1];
421  aa[1] = aa[0];
422  aa[0] = 0.0;
423 
424  ret = gauss_fit(&xd[0], &yd[0], &sig[0], len-1, &aa[0], isSet(PF_V_FLAG));
425 
426  if(ret) /* return value is oppisite of gsl version */
427  {
428  ret = 0;
429  }
430  else
431  {
432  ret = 1;
433  }
434 
435  aa[0] = aa[1];
436  aa[1] = aa[2];
437  aa[2] = aa[3];
438  aa[3] = 0.0;
439  }
440  else
441  {
442  st_report("Using GSL Gaussian Routines");
443  ret = gaussian_fit_gsl(&xd[0], &yd[0], len, &aa[0]);
444  aa[2] *= 2.0; /* gsl functions return 1/2 the proper value; double it here */
445  }
446 
447  if(!ret)
448  {
449  st_report("Gauss Results: %f %f %f", aa[0], aa[1], aa[2]);
450 
451  gfit.xmode = env->x1mode;
452  gfit.peak = aa[0];
453  gfit.center = aa[1];
454  gfit.hp = aa[2];
455  gfit.sumK = 0.0;
456  gfit.hghterr = hghterr;
457  gfit.cnterr = cnterr;
458  gfit.hwerr = hwerr;
459 
460  for(j=0;j<len;j++)
461  {
462  y = gaussian(aa[0], aa[1], aa[2], xd[j]);
463  gfit.sumK += y;
464  }
465 
466  env->gaussPeak = gfit.peak;
468  env->gaussHP = gfit.hp;
469  env->hghterr = gfit.hghterr;
470  env->cnterr = gfit.cnterr;
471  env->hwerr = gfit.hwerr;
472 
473  findLineName((gfit.center+image->head.restfreq) / 1000.0, fabs(image->head.freqres) / 1000.0, gfit.linename);
474 
475  st_report("%4d Channels @ deltax = %f", len, image->head.deltax);
476 
477  gfit.sumKm = gfit.sumK * fabs(image->head.deltax);
478  env->gaussSum = gfit.sumK;
479 
480  printGauss(image);
481 
482  bcopy((char *)&gfit, (char *)&image->ginfo.fits[image->ginfo.nfits], sizeof(struct GAUSSIAN_FIT));
483  image->ginfo.nfits++;
484 
485  xx();
486 
487  if(isSet(PF_S_FLAG))
488  {
489  sprintf(cmdBuf, "splat /l");
490  parseCmd(cmdBuf);
491  }
492  }
493  else
494  {
495  st_fail("Gaussian Fit Failed");
496  bcopy((char *)&ydata[0], (char *)&image->yplot1[0], sizeof(image->yplot1));
497  line_determine_min_max(image);
498 
499  return(1);
500  }
501 
502  /* Zero out these channels so next round doesn't see them */
503  h1gauss -= 5;
504  h2gauss += 5;
505 
506  for(j=h1gauss;j<h2gauss;j++)
507  {
508  image->yplot1[j] = 0.0;
509  }
510 
511  }
512 
513  bcopy((char *)&ydata[0], (char *)&image->yplot1[0], sizeof(image->yplot1));
514  line_determine_min_max(image);
515 
516  gaussList(1);
517 
518  return(0);
519 }
520 
521 
523 {
524  int i, k, ret, len, n, rn;
525  int pgauss = 0, h1gauss = 0, h2gauss = 0;
526  double aa[4], y;
527  double xd[MAX_CHANS], yd[MAX_CHANS], sig[MAX_CHANS];
528  char cmdBuf[256];
529  struct LP_IMAGE_INFO *image = h0;
530 
531  if(isSet(PF_C_FLAG))
532  {
533  st_report("Clearing out all previous gaussian fits");
534 
535  clearGauss(image, 1);
536 
537 
538  return(0);
539  }
540 
541  if(isSet(PF_L_FLAG))
542  {
543  gaussList(1);
544 
545  return(0);
546  }
547 
548  xx(); /* Repaint with any previous gaussian curves cleared */
549 
550  st_report("Gaussian Fitting Routine");
551 
552  rn = getNset();
553 
554  if(isSet(PF_A_FLAG) && rn)
555  {
556  return(autoNGauss(rn));
557  }
558  else
559  if(isSet(PF_A_FLAG) && !rn)
560  {
561  return(autoNGauss(1));
562  }
563  else
564  {
565  do
566  {
567  n = getNumericResponse("Enter Number of Gaussians to fit [ Up to %d]: ", MAX_GAUSSIAN_FITS);
568  }
569  while(n < 0);
570 
571  if(n == 0)
572  {
573  return(0);
574  }
575 
576  if(n > MAX_GAUSSIAN_FITS)
577  {
578  st_fail("Too many fit requests: max = %d", MAX_GAUSSIAN_FITS);
579  return(1);
580  }
581 
582  st_report("You will need to make 3 Mouse Clicks per Gaussian");
583  }
584 
585  for(k=0;k<n;k++)
586  {
587  if(n > 1)
588  {
589  st_report(" ");
590  st_report("Fit #%d", k);
591  st_report(" ");
592  }
593 
594  ret = _doCCur("Mouse-Click plot on PEAK of Gaussian Area");
595  if(ret == 1)
596  {
597  st_fail("Gauss fit Cancelled");
598  return(1);
599  }
600 
601  pgauss = (int)round(env->ccur);
602 
603  if(pgauss == 0)
604  {
605  st_fail("Gauss fit Cancelled");
606  return(1);
607  }
608 
609  st_report("gauss center channel: %d, temp = %f", pgauss, image->yplot1[pgauss]);
610 
611  ret = _doCCur("Mouse-Click plot on LEFT edge of Gaussian Half-Power Point");
612 
613  if(ret == 1)
614  {
615  st_fail("Gauss fit Cancelled");
616  return(1);
617  }
618 
619  h1gauss = (int)round(env->ccur);
620 
621  st_report("gauss left channel: %d, temp = %f", h1gauss, image->yplot1[h1gauss]);
622 
623  if(h1gauss == 0)
624  {
625  st_fail("Gauss fit Cancelled");
626  return(1);
627  }
628 
629  ret = _doCCur("Mouse-Click plot on RIGHT edge of Gaussian Half-Power Point");
630 
631  if(ret == 1)
632  {
633  st_fail("Gauss fit Cancelled");
634  return(1);
635  }
636 
637  h2gauss = (int)round(env->ccur);
638 
639  st_report("gauss right channel: %d, temp = %f", h2gauss, image->yplot1[h2gauss]);
640 
641  if(h2gauss == 0)
642  {
643  st_fail("Gauss fit Cancelled");
644  return(1);
645  }
646 
647  st_report("With a initial guess of: Peak %f @ Chan %d", image->yplot1[pgauss], pgauss);
648  st_report("And Half-Power Points of: %d & %d", h1gauss, h2gauss);
649 
650  clearGauss(image, 0); /* clear out the local gfit */
651 
652  len = image->head.noint;
653 
654  for(i=0;i<len;i++)
655  {
656  xd[i] = image->x1data[i];
657  yd[i] = image->yplot1[i];
658  sig[i] = 1.0;
659  }
660 
661  aa[0] = yd[pgauss];
662  aa[1] = xd[pgauss];
663  aa[2] = fabs(xd[h1gauss]-xd[h2gauss]);
664 
665  st_report("Gauss Guess: %f %f %f", aa[0], aa[1], aa[2]);
666 
667  /* computer gaussian using Temp / Channel */
668  /* Use older Unipops version of gauss_fit */
669  if(!env->usegslgauss)
670  {
671  st_report("Using Unipops Gaussian Routines");
672  /* shift up one element for Fortran */
673  aa[3] = aa[2];
674  aa[2] = aa[1];
675  aa[1] = aa[0];
676  aa[0] = 0.0;
677 
678  ret = gauss_fit(&xd[0], &yd[0], &sig[0], len-1, &aa[0], isSet(PF_V_FLAG));
679 
680  if(ret) /* return value is oppisite of gsl version */
681  {
682  ret = 0;
683  }
684  else
685  {
686  ret = 1;
687  }
688 
689  /* shift back down one element for Unix */
690  aa[0] = aa[1];
691  aa[1] = aa[2];
692  aa[2] = aa[3];
693  aa[3] = 0.0;
694  }
695  else
696  {
697  st_report("Using GSL Gaussian Routines");
698  ret = gaussian_fit_gsl(&xd[0], &yd[0], len, &aa[0]);
699  aa[2] *= 2.0; /* gsl functions return 1/2 the proper value; double it here */
700  }
701 
702  if(!ret)
703  {
704  st_report("Gauss Results: %f %f %f", aa[0], aa[1], aa[2]);
705 
706  gfit.xmode = env->x1mode;
707  gfit.peak = aa[0];
708  gfit.center = aa[1];
709  gfit.hp = aa[2];
710  gfit.sumK = 0.0;
711  gfit.hghterr = hghterr;
712  gfit.cnterr = cnterr;
713  gfit.hwerr = hwerr;
714 
715  for(i=0;i<len;i++)
716  {
717  y = gaussian(aa[0], aa[1], aa[2], xd[i]);
718  gfit.sumK += y;
719  }
720 
721  env->gaussPeak = gfit.peak;
723  env->gaussHP = gfit.hp;
724  env->hghterr = gfit.hghterr;
725  env->cnterr = gfit.cnterr;
726  env->hwerr = gfit.hwerr;
727 
728  findLineName((gfit.center+image->head.restfreq) / 1000.0, fabs(image->head.freqres) / 1000.0, gfit.linename);
729 
730  st_report("%4d Channels @ deltax = %f", len, image->head.deltax);
731 
732  gfit.sumKm = gfit.sumK * fabs(image->head.deltax);
733  env->gaussSum = gfit.sumK;
734 
735  printGauss(image);
736 
737  bcopy((char *)&gfit, (char *)&image->ginfo.fits[image->ginfo.nfits], sizeof(struct GAUSSIAN_FIT));
738  image->ginfo.nfits++;
739 
740  xx();
741 
742  if(isSet(PF_S_FLAG))
743  {
744  sprintf(cmdBuf, "splat /l");
745  parseCmd(cmdBuf);
746  }
747  }
748  else
749  {
750  st_fail("Gaussian Fit Failed");
751 
752  return(1);
753  }
754 
755  }
756 
757  gaussList(1);
758 
759  return(0);
760 }
761 
762 
763 /* Continuum Gaussian Routines */
764 
765 
766 int cont_gauss(y1, y2, y3, x1, x2, x3, a, b, c)
767 float y1, y2, y3; /* Y coordinates */
768 double x1, x2, x3; /* X coordinates */
769 float *a, *b, *c; /* curve constants */
770 {
771  float l2, l3, t;
772 
773  if( (t = y2/y1) > 0.0)
774  {
775  l2 = log(t);
776  }
777  else
778  {
779  return(0);
780  }
781 
782  if( (t = y3/y1) > 0.0)
783  {
784  l3 = log(t);
785  }
786  else
787  {
788  return(0);
789  }
790 
791  *c = (-l2 * x1 * x1 + l3 * x1 * x1 - l3 * x2 * x2 + l2 * x3 * x3)/
792  (2 * l2 * x1 - 2 * l3 * x1 + 2 * l3 * x2 - 2 * l2 * x3);
793 
794  *b = (l2*x1 - l3*x1 + l3*x2 - l2*x3)/
795  (x1*x1*x2 - x1*x2*x2 - x1*x1*x3 + x2*x2*x3 + x1*x3*x3 - x2*x3*x3);
796 
797  *a = y1 *exp(*b * (*c + x1) * (*c + x1));
798 
799  if(*b < 0.0)
800  {
801  return(0);
802  }
803 
804  return(1);
805 }
806 
807 
808 int doGauss()
809 {
810  int i, n;
811  char buf[256], bbuf[512];
812 
813  if(h0->head.scan < 1.0)
814  {
815  st_warn("No Scan loaded; Use 'get scan#'");
816  return(0);
817  }
818 
819 // if(isSet(PF_R_FLAG)) /* Record the results ?? */
820 // {
821 // gaussRecord(0);
822 //
823 // return(0);
824 // }
825 
826  if(isSet(PF_N_FLAG))
827  {
828  if(args->ntokens > 2)
829  {
830  n = atoi(args->secondArg);
831 
832  if(n < 0 || n >= MAX_GAUSSIAN_FITS)
833  {
834  st_print("gauss /n: arg out of range: 0 to %d\n", MAX_GAUSSIAN_FITS-1);
835 
837  return(1);
838  }
839 
840  bzero(bbuf, sizeof(bbuf));
841 
842  for(i=2;i<args->ntokens;i++)
843  {
844  sprintf(buf, args->tok[i]);
845 
846  strcat(bbuf, buf);
847  }
848 
849  st_report("Setting Gaussian fit[%d] name to %s", n, bbuf);
850 
851  strcpy(h0->ginfo.fits[n].linename, bbuf);
852 
853  return(0);
854  }
855  else
856  {
857  st_print("Missing arg for gauss /n\n");
858 
860 
861  return(1);
862  }
863  }
864 
865  if(env->mode == MODE_CONT)
866  {
868  }
869  else /* Works for both OTF and SPEC */
870  {
871  do_line_gauss();
872  xx();
873  }
874 
875  return(0);
876 }
877 
878 
879 int do_cont_gauss(d, h)
880 float *d;
881 struct HEADER *h;
882 {
883  float ydata[MAX_CONT_SAMPLES];
884  int i, dlen, gstart;
885  double aa1[4];
886  double xd[256], yd[256], s[256], deltax;
887  char sysBuf[256];
888 
889  st_report("do_cont_gauss(): Enter");
890 
891  if(processContinuum(h, d, &ydata[0], &dlen, "do_cont_gauss"))
892  {
893  return(1);
894  }
895 
896  deltax = findDeltaX(h);
897 
898  /* Copy into (double)gauss array */
899  for(i=0;i<dlen;i++)
900  {
901  xd[i] = (double)i * deltax - ((double)dlen * deltax / 2.0) + deltax / 2.0;
902  yd[i] = ydata[i];
903  s[i] = 1.0;
904  }
905 
906  aa1[0] = 0.0;
907  aa1[1] = env->datamax;
908  aa1[2] = xd[env->peakChan];
909  aa1[3] = h->bfwhm;
910 
911  st_report("do_cont_gauss(): Guess: Center = %f @ %f: width = %f", aa1[1], aa1[2], aa1[3]);
912 
913  if(env->glength <= 0)
914  {
915  env->glength = 15;
916  }
917  else
918  if(env->glength > dlen)
919  {
920  env->glength = dlen - 2;
921  }
922 
923  gstart = dlen / 2 - 5;
924 
925  gstart = env->peakChan - env->glength / 2;
926 
927  gauss_fit(&xd[gstart], &yd[gstart], &s[gstart], env->glength, &aa1[0], isSet(PF_V_FLAG));
928 
929  st_report("do_cont_gauss(): %d-Point Gaussian Fit: Y = %.2f, X = %.2f, BW = %.2f",
930  env->glength, aa1[1], aa1[2], aa1[3]);
931  beam_map.gmap_peak1 = aa1[1];
932  beam_map.gmap_bw1 = aa1[3];
933 
934  beam_map.gmap_peak2 = 0.0;
935  beam_map.gmap_bw2 = 0.0;
936 
937  doDeConvol(); /* Deconvol out the planet */
938 
939  if(isSet(PF_P_FLAG))
940  {
941  st_report("do_cont_gauss(): Displaying Plot");
942  plotGauss(&xd[0], dlen, &aa1[0], h, 0);
943  }
944 
945  if(isSet(PF_S_FLAG))
946  {
947  setFlag(PF_C_FLAG, 1); /* Set for capture */
948 
949  printPlot();
950 
951  usleep(1000000);
952 
953  st_report("do_cont_gauss(): Saving Plot");
954  sprintf(sysBuf, "cp /tmp/%s/ccal_plot.ps Scan-%.2f.ps", session, h->scan);
955  system(sysBuf);
956  }
957 
958  return(0);
959 }
960 
961 
962 int plotGauss(x, len, a, h, mode)
963 double *x, *a;
964 int len, mode;
965 struct HEADER *h;
966 {
967  int i;
968  char tbuf[512];
969  double yd, dyda[4], max = -9999999.9;
970 
971  st_report("plotGauss(): Enter");
972 
973  if(cont_plain_send.dlen == 0)
974  {
975  st_fail("plotGauss(): Must load a scan and do a 'show' first");
976  return(1);
977  }
978  /* Copy gaussian data to Struct*/
979 
980  for(i=0;i<len;i++)
981  {
982  fgauss(x[i], a, &yd, &dyda[0]);
983 
984  cont_plain_send.gdata[i] = yd;
985  }
986 
987  if(env->yaxis_scale == LOG_PLOT)
988  {
989  for(i=0;i<len;i++)
990  {
991  if(cont_plain_send.gdata[i] > max)
992  {
993  max = cont_plain_send.gdata[i];
994  }
995  }
996 
997  if(max != 0.0)
998  {
999  for(i=0;i<len;i++)
1000  {
1001  cont_plain_send.gdata[i] = 10.0 * log10(cont_plain_send.gdata[i] / max);
1002  }
1003  }
1004  }
1005 
1006 
1007  cont_plain_send.ngauss = len;
1008 
1009  sprintf(tbuf, "%d Point Gaussian Fit", env->glength);
1010  strcpy(cont_plain_send.glabels[0], tbuf);
1011 
1012  sprintf(tbuf, "Gaussian Peak: %10.2f K", a[1]);
1013  strcpy(cont_plain_send.glabels[1], tbuf);
1014 
1015  if(mode == 2)
1016  {
1017  sprintf(tbuf, "Gaussian BW: %6.2f mm", a[3]);
1018  }
1019  else
1020  {
1021  sprintf(tbuf, "Gaussian BW: %6.2f sec", a[3]);
1022  }
1023 
1024  strcpy(cont_plain_send.glabels[2], tbuf);
1025 
1026  if(mode != 2)
1027  {
1028  sprintf(tbuf, "De-Convolved BW: %6.2f sec", decon_beam_size);
1029  strcpy(cont_plain_send.glabels[3], tbuf);
1030  }
1031 
1032  if(mode == 2)
1033  {
1034  sprintf(tbuf, "AX Focus Input: %6.2f mm, Results: %6.2f mm", h->focusr, a[2]);
1035  }
1036  else
1037  if(mode == 1)
1038  {
1039  sprintf(tbuf, "AX Focus Input: %6.2f mm, Results: %6.2f mm", h->focusr, a[2]);
1040  }
1041  else
1042  {
1043  sprintf(tbuf, "AX Focus Setting: %6.2f mm", h->focusr);
1044  }
1045  strcpy(cont_plain_send.glabels[4], tbuf);
1046 
1047  sprintf(tbuf, "NS Focus Setting: %6.2f mm", h->focusv);
1048  strcpy(cont_plain_send.glabels[5], tbuf);
1049 
1050  sprintf(tbuf, "EW Focus Setting: %6.2f mm", h->focush);
1051  strcpy(cont_plain_send.glabels[6], tbuf);
1052 
1053  if(mode != 2)
1054  {
1055  sprintf(tbuf, "Map Angle: %6d Degs", env->mapangle);
1056  strcpy(cont_plain_send.glabels[7], tbuf);
1057  }
1058 
1059  /* Save latest in global */
1060  env->gaussPeak = a[1];
1061  env->gaussCenter = a[2];
1062  env->gaussHP = a[3];
1063 
1064  if(xgraphic && notSet(PF_X_FLAG))
1065  {
1067  }
1068 
1069  return(0);
1070 }
struct SOCK * xgraphic
Definition: linuxpops.c:75
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
double focush
Definition: header.h:56
struct STRINGS_AND_THINGS sat
int xx()
Definition: linuxpops.c:605
double hp
Definition: gaussian.h:13
double scan
Definition: header.h:35
#define LOG_PLOT
Definition: defines.h:111
char * strcmprs(char *str)
struct PARSE_STACK * args
Definition: parse.h:196
struct GAUSSIAN_FIT gfit
Definition: gauss.c:70
double sumK
Definition: gaussian.h:14
int x1mode
Definition: env.h:145
int gaussian_fit_gsl(double *xd, double *yd, const size_t n, double *aa)
Definition: gaussian_fit.c:239
double noint
Definition: header.h:211
Definition: parse.h:8
char linename[80]
Definition: gaussian.h:20
int plotGauss(double *x, int len, double *a, struct HEADER *h, int mode)
Definition: gauss.c:962
int glength
Definition: env.h:57
#define MAX_CHANS
Definition: defines.h:38
float escan
Definition: image.h:53
int usegslgauss
Definition: env.h:142
double ccur
Definition: env.h:176
int copyGoodChars(char *out, char *in)
Definition: gauss.c:164
int do_line_gauss()
Definition: gauss.c:522
int sendSat(void)
double bfwhm
Definition: header.h:114
#define MODE_CONT
Definition: defines.h:86
int cont_gauss(float y1, float y2, float y3, double x1, double x2, double x3, float *a, float *b, float *c)
Definition: gauss.c:766
FILE * fp
Definition: linuxpops.c:70
#define max(a, b)
Definition: cactus.h:115
Definition: header.h:13
int printH_FlagHelp(char *cmd)
Definition: help.c:430
int autoNGauss(int n)
Definition: gauss.c:319
double jfact
Definition: env.h:212
void fgauss()
int gauss_fit()
int setFlag(int flag, int state)
Definition: parse.c:685
char glabels[MAX_GLABELS][80]
Definition: accum.h:110
int issmt
Definition: env.h:63
int do_cont_gauss(float *d, struct HEADER *h)
Definition: gauss.c:879
float bscan
Definition: image.h:52
double hghterr
Definition: gaussian.h:16
int printGauss(struct LP_IMAGE_INFO *image)
Definition: gauss.c:73
double gaussian(const double a, const double b, const double c, const double t)
Definition: gaussian_fit.c:70
int peakChan
Definition: env.h:126
char tok[MAX_TOKENS][MAX_TOKENS]
Definition: parse.h:176
int gaussList(int header)
Definition: gauss.c:245
int processContinuum(struct HEADER *head, float *phases, float *data, int *dl, char *mode)
Definition: cont.c:2035
double findDeltaX(struct HEADER *h)
Definition: cont.c:1901
struct BEAM_MAP beam_map
Definition: gridmap.c:61
int notSet(int flag)
Definition: parse.c:669
#define XMODE_CHAN
Definition: defines.h:97
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int _doCCur(char *msg)
Definition: linuxpops.c:2411
#define MG_J_FACTOR
Definition: defines.h:11
double cnterr
Definition: env.h:177
double hwerr
Definition: env.h:206
double decon_beam_size
Definition: convol.c:76
float x1data[MAX_CHANS]
Definition: image.h:117
#define MAX_GAUSSIAN_FITS
Definition: gaussian.h:5
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
double cnterr
Definition: gaussian.h:17
double center
Definition: gaussian.h:12
double datamax
Definition: env.h:180
void st_warn()
void st_fail()
double gaussPeak
Definition: env.h:199
float gmap_peak1
Definition: gridmap.h:58
float gmap_bw1
Definition: gridmap.h:56
float datamax
Definition: image.h:98
double gaussCenter
Definition: env.h:200
int mode
Definition: image.h:76
struct ACCUM_INFO ainfo
Definition: image.h:100
int getNumericResponse()
int sendCont(int mode)
Definition: cont.c:1827
#define KP_J_FACTOR
Definition: defines.h:19
char session[80]
Definition: strings.c:67
struct GAUSSIAN_FIT fits[MAX_GAUSSIAN_FITS]
Definition: gaussian.h:26
struct CONTINUUM_PLAIN cont_plain_send
Definition: cont.c:63
double focusr
Definition: header.h:54
int mode
Definition: env.h:75
#define XMODE_FREQ
Definition: defines.h:95
double hwerr
Definition: gaussian.h:18
int gaussRecord()
Definition: gauss.c:284
double cnterr
Definition: gaussfit.c:16
void st_report()
int findLineName(double f, double d, char *buf)
Definition: gauss.c:187
double hghterr
Definition: gaussfit.c:16
double deltax
Definition: header.h:209
double gaussHP
Definition: env.h:201
struct IMAGE_GAUSS gauss
int peakChan
Definition: image.h:77
double peak
Definition: gaussian.h:11
double sumKm
Definition: gaussian.h:15
struct GAUSS_INFO_RESULTS results[MAX_IMAGE_GAUSS_RESULTS]
float gdata[MAX_CONT_SAMPLES]
Definition: accum.h:119
int doGauss()
Definition: gauss.c:808
int getNset()
Definition: parse.c:214
float gmap_peak2
Definition: gridmap.h:59
double gaussSum
Definition: env.h:202
int yaxis_scale
Definition: env.h:148
double hghterr
Definition: env.h:204
#define SEND_PLAIN
Definition: defines.h:134
int clearGauss(struct LP_IMAGE_INFO *image, int clear)
Definition: gauss.c:145
char * secondArg
Definition: parse.h:181
int doDeConvol()
Definition: convol.c:504
void st_print()
int ntokens
Definition: parse.h:173
#define MAX_CONT_SAMPLES
Definition: accum.h:26
float ydata[MAX_ATM_ELEMENTS]
Definition: atm.c:91
int line_determine_min_max(struct LP_IMAGE_INFO *image)
Definition: line.c:694
float gmap_bw2
Definition: gridmap.h:57
int mapangle
Definition: env.h:70
double hwerr
Definition: gaussfit.c:16
double focusv
Definition: header.h:55
char * firstArg
Definition: parse.h:180
double restfreq
Definition: header.h:198
int parseCmd(char *buf)
Definition: parse.c:2090
#define XMODE_VEL
Definition: defines.h:96
double freqres
Definition: header.h:199
int xmode
Definition: gaussian.h:8
int nfits
Definition: gaussian.h:24
int printPlot()
Definition: linuxpops.c:385
struct GAUSSIAN ginfo
Definition: image.h:103