Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
baseline.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 #define NUM_OF_BCHANNEL_FOR_POLY 10
62 
65 
66 
68 {
69  bzero((char *)&bl_averages[0], sizeof(bl_averages));
70 
72 
73  return(0);
74 }
75 
76 
78 {
79  if(env->mode == MODE_CONT)
80  {
81  mapBaseline();
82  }
83  else
84  {
85  env->dobaseline = 1;
86  env->bshow = 0;
87 
88  if(isSet(PF_G_FLAG))
89  {
90  doGraceFit();
91  }
92  else
93  {
95  }
96  }
97 
98  return(0);
99 }
100 
101 
103 struct LP_IMAGE_INFO *image;
104 {
105  int a, l, j, k, n, foundone=0;
106  float *p;
107 
109 
110  p = image->yplot1;
111 
112  n = getNumericFlag(); /* Does the user want to use just one of the regions for average? */
113 
114  if(n < 0) /* No, User hasn't asked for only one region; average them all */
115  {
116  a = 0;
117  env->rst = 0.0;
118 
119  for(l=0;l<MAX_BASELINES;l++)
120  {
121  if(env->baselines[l][0] <= 0 || env->baselines[l][1] <= 0)
122  {
123  continue; /* Skip it */
124  }
125 
126  if(env->baselines[l][0] > image->head.noint || env->baselines[l][1] > image->head.noint)
127  {
128  continue;
129  }
130 
131  k = 0;
132 
133  for(j=env->baselines[l][0];j<env->baselines[l][1];j++)
134  {
135  if(j > env->bdrop && j < image->head.noint - env->edrop)
136  {
137  bl_averages[l] += p[j];
138  k++;
139 
140  foundone++;
141  }
142  }
143 
144  if(k)
145  {
146  bl_averages[l] /= (double)k;
147 
148  env->rst += bl_averages[l];
149  a++;
150  }
151  }
152 
153  if(a)
154  {
155  env->rst /= (double)a;
156  }
157 
158  valid_bl_averages = 1;
159  }
160  else /* Yes */
161  {
162  k = 0;
163 
164  for(j=env->baselines[n][0];j<env->baselines[n][1];j++)
165  {
166  if(j > env->bdrop && j < image->head.noint - env->edrop)
167  {
168  bl_averages[n] += p[j];
169  k++;
170 
171  foundone++;
172  }
173  }
174 
175  if(k)
176  {
177  bl_averages[n] /= (double)k;
178 
179  env->rst = bl_averages[n];
180  }
181  }
182 
183  k = 0;
184  for(l=0;l<MAX_BASELINES;l++)
185  {
186  if(bl_averages[l] != 0.0)
187  {
188  st_report("computeEachBaseLine(): Average values for Baseline Region: %d, %f", l, bl_averages[l]);
189  }
190  }
191 
192  return(foundone);
193 }
194 
195 
196 
200 int computeBaseLine(image)
201 struct LP_IMAGE_INFO *image;
202 {
203  int i, j, l, k, m=0;
204  float x[MAX_CHANS],
205  sig[MAX_CHANS],
206  lsqArray[MAX_CHANS];
207  float a, b, siga1, sigb1, chi2_1, q1;
208  float *p, r;
209  double avg = 0.0;
210 
211  bzero((char *)&x, sizeof(x));
212  bzero((char *)&sig, sizeof(sig));
213  bzero((char *)&lsqArray, sizeof(lsqArray));
214 
215  p = image->yplot1;
216 
217  k=0;
218 
219  for(l=0;l<MAX_BASELINES;l++)
220  {
221  if(env->baselines[l][0] <= 0 || env->baselines[l][1] <= 0)
222  {
223  continue; /* Skip it */
224  }
225 
226  if(env->baselines[l][0] > image->head.noint || env->baselines[l][1] > image->head.noint)
227  {
228  continue;
229  }
230 
231  m++;
232  for(j=env->baselines[l][0];j<env->baselines[l][1];j++)
233  {
234  if(j > env->bdrop && j < image->head.noint - env->edrop)
235  {
236  sig[k] = 0.0;
237  x[k] = (float)j;
238  lsqArray[k] = p[j];
239  k++;
240  }
241  }
242  }
243 
244  if(m == 0)
245  {
246  st_fail("No baseline regions defined");
247  return(0);
248  }
249 
250  for(i=0;i<k;i++)
251  {
252  avg += lsqArray[i];
253  }
254 
255  avg /= (double)k;
256 
257  if(isSet(PF_N_FLAG))
258  {
259  env->rst = avg;
260 
261  return(k);
262  }
263 
264  if(k>=4)
265  {
266  sptip_lsqfit(x, lsqArray, 0, k, sig, 0, &b, &a, &sigb1, &siga1, &chi2_1, &q1);
267 
268  computeRms(lsqArray, k, &r);
269 
270  image->binfo.a = a;
271  image->binfo.b = b;
272  image->binfo.rms = r;
273  }
274  else
275  {
276  image->binfo.a = 0.0;
277  image->binfo.b = 0.0;
278  image->binfo.rms = 100.0;
279  }
280 
281  env->rms = image->binfo.rms;
282  env->slope = image->binfo.a;
283  env->intercept = image->binfo.b;
284 
285  return(k);
286 }
287 
288 
289 
290 /* Take the yplot1 array in image and remove a baseline */
291 int line_doBaseLine(image)
292 struct LP_IMAGE_INFO *image;
293 {
294  int i, ret, first = 1;
295  float *f;
296 
297  copyEnv2Image(image); /* Make sure the structs are up to date */
298 
299  /* Compute baseline */
300  ret = computeBaseLine(image);
301 
302  /* Remove any baseline */
303  if(image->binfo.doBaseLine)
304  {
305  if(first && ret)
306  {
307  st_report("Removing Baselines from %s", image->name);
308  first = 0;
309  }
310 
312  {
313  st_report("Slope %f, Intercept %f", image->binfo.a, image->binfo.b);
314  }
315 
316  f = &image->yplot1[0];
317  for(i=0;i<image->head.noint;i++,f++)
318  {
319  /* Do not remove if it's already zero; otherwise
320  you get bogus results from shifted data which
321  zeros out the unshifted portion */
322  if(*f != 0.0)
323  {
324  *f = *f - ((float)i * image->binfo.a + image->binfo.b);
325  }
326  }
327  }
328 
329  return(0);
330 }
331 
332 
337 int chan;
338 {
339  int i;
340 
341  for(i=0;i<MAX_BASELINES;i++)
342  {
343  if(env->baselines[i][0] <= 0 || env->baselines[i][1] <= 0)
344  {
345  continue; /* Skip it */
346  }
347 
348  if(chan >= env->baselines[i][0] && chan <= env->baselines[i][1])
349  {
350  return(1);
351  }
352  }
353 
354  return(0);
355 }
356 
357 
359 {
360  int i;
361 
362 // st_report("Remove Baselines: %s", env->dobaseline == 1 ? "Yes" : " No");
363 
364  for(i=0;i<MAX_BASELINES;i++)
365  {
366  if(i==0)
367  {
368  st_report("Baseline Regions: [%2d] %5d %5d", i, env->baselines[i][0], env->baselines[i][1]);
369  }
370  else
371  {
372  if(env->baselines[i][0]+env->baselines[i][1] > 0)
373  {
374  st_report(" [%2d] %5d %5d", i, env->baselines[i][0], env->baselines[i][1]);
375  }
376  }
377  }
378 
379  return(0);
380 }
381 
382 
384 {
385  int i, n, c1 = 0, c2 = 0;
386  int base, noint, old_bmark;
387  int j=0, k=0;
388  int vals[2][2];
389  double arg1, arg2;
390  char hint[256];
391  struct LP_IMAGE_INFO *image = h0;
392 
393  noint = image->head.noint;
394 
396  {
397  st_report("Got %d tokens", args->ntokens);
398  }
399 
400  if(args->ntokens > 2) /* Did user pass args */
401  {
402  arg1 = atof(args->secondArg);
403  arg2 = atof(args->thirdArg);
404 
405  if(verboseLevel & VERBOSE_BASELINES)
406  {
407  st_report("Setting NBase to %.3f %.3f", arg1, arg2);
408  }
409 
410  if(image->x1data[0] < image->x1data[noint-1]) /* X-Axis Increases */
411  {
412  if(arg1 > arg2) /* Args in reverse order */
413  {
414  if(verboseLevel & VERBOSE_BASELINES)
415  {
416  st_report("X increasing, args decreaing");
417  }
418 
419  parseMouseValues(arg2, 0.0, 0.0, 0.0);
420 
421  if(verboseLevel & VERBOSE_BASELINES)
422  {
423  st_report("Channel: %.0f", env->ccur);
424  }
425 
426  c1 = (int)env->ccur;
427 
428  parseMouseValues(arg1, 0.0, 0.0, 0.0);
429 
430  if(verboseLevel & VERBOSE_BASELINES)
431  {
432  st_report("Channel: %.0f", env->ccur);
433  }
434 
435  c2 = (int)env->ccur;
436  }
437  else /* Args in correct order */
438  {
439  if(verboseLevel & VERBOSE_BASELINES)
440  {
441  st_report("X increasing, args increasing");
442  }
443 
444  parseMouseValues(arg1, 0.0, 0.0, 0.0);
445 
446  if(verboseLevel & VERBOSE_BASELINES)
447  {
448  st_report("Channel: %.0f", env->ccur);
449  }
450 
451  c1 = (int)env->ccur;
452 
453  parseMouseValues(arg2, 0.0, 0.0, 0.0);
454 
455  if(verboseLevel & VERBOSE_BASELINES)
456  {
457  st_report("Channel: %.0f", env->ccur);
458  }
459 
460  c2 = (int)env->ccur;
461  }
462 
463  if(c1 > -1 && c2 < image->head.noint-2)
464  {
465  bzero((char *)&env->baselines[0], sizeof(env->baselines));
466 
467  env->baselines[0][0] = 1;
468  env->baselines[0][1] = c1;
469 
470  env->baselines[1][0] = c2;
471  env->baselines[1][1] = image->head.noint-1;
472 
473  tellBaselines();
474 
475  return(0);
476  }
477  else
478  {
479  st_fail("Error in Translating Channels: %d %d", c1, c2);
480 
481  return(1);
482  }
483  }
484  else /* X-Axis decreases */
485  {
486  if(arg1 > arg2) /* Args in correct order */
487  {
488  if(verboseLevel & VERBOSE_BASELINES)
489  {
490  st_report("X decreasing, args decreaing");
491  }
492 
493  parseMouseValues(arg1, 0.0, 0.0, 0.0);
494 
495  if(verboseLevel & VERBOSE_BASELINES)
496  {
497  st_report("Channel: %.0f", env->ccur);
498  }
499 
500  c1 = (int)env->ccur;
501 
502  parseMouseValues(arg2, 0.0, 0.0, 0.0);
503 
504  if(verboseLevel & VERBOSE_BASELINES)
505  {
506  st_report("Channel: %.0f", env->ccur);
507  }
508 
509  c2 = (int)env->ccur;
510  }
511  else /* Args in reverse order */
512  {
513  if(verboseLevel & VERBOSE_BASELINES)
514  {
515  st_report("X decreasing, args increasing");
516  }
517 
518  parseMouseValues(arg2, 0.0, 0.0, 0.0);
519 
520  if(verboseLevel & VERBOSE_BASELINES)
521  {
522  st_report("Channel: %.0f", env->ccur);
523  }
524  c1 = (int)env->ccur;
525 
526  parseMouseValues(arg1, 0.0, 0.0, 0.0);
527 
528  if(verboseLevel & VERBOSE_BASELINES)
529  {
530  st_report("Channel: %.0f", env->ccur);
531  }
532 
533  c2 = (int)env->ccur;
534  }
535 
536  if(c1 > -1 && c2 < image->head.noint-2)
537  {
538  bzero((char *)&env->baselines[0], sizeof(env->baselines));
539 
540  env->baselines[0][0] = 1;
541  env->baselines[0][1] = c1;
542 
543  env->baselines[1][0] = c2;
544  env->baselines[1][1] = image->head.noint-1;
545 
546  tellBaselines();
547 
548  return(0);
549  }
550  else
551  {
552  st_fail("Error in Translating Channels: %d %d", c1, c2);
553 
554  return(1);
555  }
556  }
557  }
558 
559  old_bmark = env->bmark;
560  env->bmark = 0;
561 
562  xx();
563 
564  n = 2;
565 
566  bzero((char *)&vals[0], sizeof(vals));
567 
568  for(i=0;i<n;i++)
569  {
570  sprintf(hint, "Click Left Mouse Button on %s Edge of Segment %d", !(i%2) ? "Left" : "Right", j+1);
571  _doCCur(hint);
572 
573  base = (int)round(env->ccur);
574 
575  if(base < 1)
576  {
577  base = 1;
578  }
579 
580  if(base >= noint)
581  {
582  base = noint-1;
583  }
584 
585  vals[j][k] = base;
586  k++;
587 
588  if(k == 2)
589  {
590  j++;
591  k = 0;
592  }
593  }
594 
595  bzero((char *)&env->baselines[0], sizeof(env->baselines));
596 
597  env->baselines[0][0] = 1;
598  env->baselines[0][1] = vals[0][0];
599 
600  env->baselines[1][0] = vals[0][1];
601  env->baselines[1][1] = image->head.noint-1;
602 
603  tellBaselines();
604 
605  env->bmark = old_bmark;
606 
607  return(0);
608 }
609 
610 
612 {
613  int i, k, j=0, tmp;
614  int bl[MAX_BASELINES][2];
615  int avgs[MAX_BASELINES];
616  struct LP_IMAGE_INFO *image = h0;
617 
618  bcopy((char *)&env->baselines[0][0], (char *)&bl[0][0], sizeof(bl));
619  bzero((char *)&env->baselines[0][0], sizeof(env->baselines));
620  bzero((char *)&avgs[0], sizeof(avgs));
621 
622  /* Find the average of each baseline segment */
623  for(i=0;i<MAX_BASELINES;i++)
624  {
625  /* should be in numeric order Left to Right */
626  if(bl[i][0] > bl[i][1])
627  {
628  st_report("Flipping baseline order for Segment %d", i+1);
629 
630  tmp = bl[i][0];
631 
632  bl[i][0] = bl[i][1];
633  bl[i][1] = tmp;
634  }
635 
636  avgs[i] = (bl[i][0] + bl[i][1]) / 2;
637  }
638 
639  /* Now, for all possible channel numbers, place the segments in order */
640  for(k=0;k<MAX_CHANS;k++)
641  {
642  if(k > image->head.noint) /* Can't be higher then this */
643  {
644  break;
645  }
646 
647  for(i=0;i<MAX_BASELINES;i++)
648  {
649  if(avgs[i] == 0) /* Not set */
650  {
651  continue;
652  }
653 
654  if(avgs[i] == k)
655  {
656  env->baselines[j][0] = bl[i][0];
657  env->baselines[j][1] = bl[i][1];
658  j++;
659  }
660  }
661  }
662 
663  return(0);
664 }
665 
666 
668 {
669  int i;
670 
671  for(i=0;i<MAX_BASELINES;i++)
672  {
673  if(env->baselines[i][0] + env->baselines[i][1] == 0)
674  {
675  return(i);
676  }
677  }
678 
679  return(-1);
680 }
681 
682 
684 {
685  int i, j, n, new, ret, base, k=0;
686  char hint[256];
687  struct LP_IMAGE_INFO *image = h0;
688 
689  if(args->ntokens > 1) /* Passed param? */
690  {
691  n = atoi(args->secondArg);
692  }
693  else
694  {
695  n = 1;
696  }
697 
698  for(j=0;j<n;j++)
699  {
700  new = findEmptyBaseline();
701 
702  if(new < 0)
703  {
704  st_fail("Baselines are full");
705 
706  sortBaselines();
707 
708  return(1);
709  }
710 
711  for(i=0;i<2;i++)
712  {
713  sprintf(hint, "Click Left Mouse Button on %s Edge of Segment %d", !(i%2) ? "Left" : "Right", new+1);
714  ret = _doCCur(hint);
715 
716  if(ret)
717  {
718  return(1);
719  }
720 
721  base = (int)round(env->ccur);
722 
723  if(base < 1)
724  {
725  base = 1;
726  }
727 
728  if(base >= image->head.noint)
729  {
730  base = image->head.noint-1;
731  }
732 
733  if(base > -1)
734  {
735  env->baselines[new][k] = base;
736  k++;
737  }
738  }
739  }
740 
741  sortBaselines();
742 
743  return(0);
744 }
745 
746 
747 
749 {
750  int i, ret, now;
751  char hint[256];
752 
753  sprintf(hint, "Click Left Mouse Button Somewhere in Segment to Remove");
754  ret = _doCCur(hint);
755 
756  if(ret)
757  {
758  return(1);
759  }
760 
761  now = (int)round(env->ccur);
762 
763  for(i=0;i<MAX_BASELINES;i++)
764  {
765  if(env->baselines[i][0] && env->baselines[i][1])
766  {
767  if(now >= env->baselines[i][0] && now <= env->baselines[i][1])
768  {
769  env->baselines[i][0] = 0;
770  env->baselines[i][1] = 0;
771 
772  sortBaselines();
773 
774  return(0);
775  }
776  }
777  }
778 
779  return(0);
780 }
781 
782 
784 {
785  int i, n, base, j=0, k=0, ret, old_bmark;
786  char hint[256];
787  struct LP_IMAGE_INFO *image = h0;
788 
789  if(isSet(PF_C_FLAG)) /* Clear all and return */
790  {
791  st_report("Clearing out baselines segments");
792 
793  /* Zero out the current baselines */
794  bzero((char *)&env->baselines[0], sizeof(env->baselines));
795 
796  return(0);
797  }
798 
799  if(isSet(PF_A_FLAG)) /* Add a baseline region */
800  {
802 
803  tellBaselines();
804 
805  return(0);
806  }
807 
808  if(isSet(PF_R_FLAG)) /* Remove a baseline region */
809  {
811 
812  tellBaselines();
813 
814  return(0);
815  }
816 
817  if(args->ntokens > 1) /* Passed param? */
818  {
819  n = atoi(args->secondArg);
820  }
821  else
822  {
823  sprintf(hint, "Enter Number of Baseline Regions [ Up to %d]: ", MAX_BASELINES);
824 
825  do
826  {
827  n = getNumericResponse(hint);
828  }
829  while(n < 0 && n > MAX_BASELINES);
830  }
831 
832  if(n == 0)
833  {
834  return(0);
835  }
836 
837  if(n > MAX_BASELINES)
838  {
839  st_fail("Too Many Baseline Regions: [0 - %d]", MAX_BASELINES);
840 
841  return(1);
842  }
843 
844  n *= 2;
845 
846  /* Zero out the current baselines */
847  bzero((char *)&env->baselines[0], sizeof(env->baselines));
848 
849  if(n == 0)
850  {
851  st_print("Clearing out defined baseline regions\n");
852  env->dobaseline = 0;
853 
854  return(0);
855  }
856 
857  old_bmark = env->bmark;
858  env->bmark = 0;
859  xx();
860 
861  for(i=0;i<n;i++)
862  {
863  sprintf(hint, "Click Left Mouse Button on %s Edge of Segment %d", !(i%2) ? "Left" : "Right", j+1);
864  ret = _doCCur(hint);
865 
866  if(ret)
867  {
868  sortBaselines();
869 
870  return(1);
871  }
872 
873  base = (int)round(env->ccur);
874 
875  if(base < 1)
876  {
877  base = 1;
878  }
879 
880  if(base >= image->head.noint)
881  {
882  base = image->head.noint-1;
883  }
884 
885  if(base > -1)
886  {
887  env->baselines[j][k] = base;
888  k++;
889 
890  if(k == 2)
891  {
892  j++;
893  k = 0;
894  }
895 
896  if(j == MAX_BASELINES)
897  {
898  if(old_bmark)
899  {
900  env->bmark = old_bmark;
901  }
902  else
903  {
904  env->bmark = 1;
905  }
906 
907  sortBaselines();
908 
909  tellBaselines();
910 
911  return(0);
912  }
913  }
914  else
915  {
916  if(old_bmark)
917  {
918  env->bmark = old_bmark;
919  }
920  else
921  {
922  env->bmark = 1;
923  }
924 
925  st_fail("Can't set baseline channel of < 0");
926 
927  return(1);
928  }
929  }
930 
931  if(old_bmark)
932  {
933  env->bmark = old_bmark;
934  }
935  else
936  {
937  env->bmark = 1;
938  }
939 
940  sortBaselines();
941 
942  tellBaselines();
943 
944  return(0);
945 }
946 
947 
949 struct LP_IMAGE_INFO *image;
950 {
951  int i;
952 
953  for(i=0;i<MAX_BASELINES;i++)
954  {
955  if(image->binfo.baselines[i][0] == 0 && image->binfo.baselines[i][1] == 0)
956  {
957  continue;
958  }
959  else
960  {
961  /* found at least one segment defined */
962  return(1);
963  }
964  }
965 
966  return(0);
967 }
968 
969 
970 int findLastGood(image, indx, lgn, lg)
971 struct LP_IMAGE_INFO *image;
972 int indx, *lgn;
973 float *lg;
974 {
975  int i, n;
976  float *f;
977 
978  n = 0;
979 
980  f = &image->bplot[indx];
981  for(i=indx;i>=n;i--,f--)
982  {
983  if(*f != 0.0)
984  {
985  *lg = *f;
986  *lgn = i;
987 
988  return(0);
989  }
990  }
991 
992  return(1);
993 }
994 
995 
996 int findNextGood(image, indx, ngn, ng)
997 struct LP_IMAGE_INFO *image;
998 int indx, *ngn;
999 float *ng;
1000 {
1001  int i, n;
1002  float *f;
1003 
1004 
1005  n = image->head.noint;
1006 
1007  f = &image->bplot[indx];
1008  for(i=indx;i<n;i++,f++)
1009  {
1010  if(*f != 0.0)
1011  {
1012  *ng = *f;
1013  *ngn = i;
1014 
1015  return(0);
1016  }
1017  }
1018 
1019  return(0);
1020 }
1021 
1022 
1024 {
1025  int b, n, b1, b2, n1, n2, i, j, k;
1026  int lgIndx, nxIndx, idiff, noint;
1027  int lastbl = 0, recording=0, start, end;
1028  char nstr[80], bname[256];
1029  float lg, ng, diff, unit;
1030  float lastGoodVal, firstGoodVal;
1031  double xd[MAX_CHANS], yd[MAX_CHANS], coeff[32];
1032  struct LP_IMAGE_INFO *image = h0; /* h0 is a pointer to the current Hx */
1033  struct LP_IMAGE_INFO *b8image = &H8; /* H8 the real thing */
1034  struct LP_IMAGE_INFO *b9image = &H9; /* H9 the real thing */
1035  FILE *fp;
1036 
1037  bzero((char *)xd, sizeof(xd));
1038  bzero((char *)yd, sizeof(yd));
1039  bzero((char *)coeff, sizeof(coeff));
1040  bzero((char *)image->bplot, sizeof(image->bplot));
1041 
1042  if(!baselinesDefined(image))
1043  {
1044  st_fail("No baseline regions defined");
1045  return(1);
1046  }
1047 
1048  if(env->nbshape < 2)
1049  {
1050  st_fail("Error: nbshape must be greater than 1");
1051  return(1);
1052  }
1053 
1054  if(isSet(PF_R_FLAG))
1055  {
1056  recording = 1;
1057  }
1058 
1059  noint = (int)image->head.noint;
1060 
1061  st_report("Computing baseline using nbshape = %d", env->nbshape);
1062 
1063 /* First perform a boxcar smoothing on the data withing each baselines regions */
1064  for(i=0;i<MAX_BASELINES;i++)
1065  {
1066  b = image->binfo.baselines[i][0];
1067  n = image->binfo.baselines[i][1] - image->binfo.baselines[i][0];
1068 
1069  if(n)
1070  {
1071  boxcar(&image->yplot1[b], &image->bplot[b], n, env->nbshape);
1072 
1073  lastbl = i;
1074  }
1075  }
1076 
1077  image->pinfo.bplot = 1;
1078 
1079  /* Now bplot array contains a baseline profile with areas not
1080  defined as baseline areas zeroed out. Think areas with lines.
1081  Now fill in the areas between baseline areas */
1082 
1084  {
1085  st_report("Performing Linear Smoothing between Baseline Regions");
1086 
1087  for(i=0;i<noint;i++)
1088  {
1089  if(i == 0)
1090  {
1091  if(image->bplot[i] == 0.0)
1092  {
1093  findNextGood(image, i, &nxIndx, &ng);
1094  image->bplot[i] = ng;
1095  }
1096  }
1097  else
1098  if(i == noint-1)
1099  {
1100  if(image->bplot[i] == 0.0)
1101  {
1102  findLastGood(image, i, &lgIndx, &lg);
1103  image->bplot[i] = lg;
1104  }
1105  }
1106  else
1107  if(image->bplot[i] == 0.0)
1108  {
1109  findLastGood(image, i, &lgIndx, &lg);
1110  findNextGood(image, i, &nxIndx, &ng);
1111 
1112  diff = lg - ng;
1113  idiff = lgIndx - nxIndx;
1114 
1115  if(idiff)
1116  {
1117  unit = diff / (float)idiff;
1118 
1120  {
1121  st_report("Missing Chan = %4d", i);
1122  st_report("Last good = %4d, %f", lgIndx, lg);
1123  st_report("Next good = %4d, %f", nxIndx, ng);
1124  st_report("Total Diff = %f", diff);
1125  st_report("Unit Diff = %f", unit);
1126  }
1127 
1128  image->bplot[i] = lg + unit * (double)(i-lgIndx);
1129  }
1130  }
1131  }
1132  }
1133  else
1135  {
1136  if(!(env->nfit > 0 && env->nfit < MAX_NFIT))
1137  {
1138  st_fail("nfit must be > 0 & < %d; not %d", MAX_NFIT, env->nfit);
1139  return(1);
1140  }
1141 
1142  switch(env->nfit)
1143  {
1144  case 1: strcpy(nstr, "st"); break;
1145  case 2: strcpy(nstr, "nd"); break;
1146  case 3: strcpy(nstr, "rd"); break;
1147 
1148  default:
1149  strcpy(nstr, "th");
1150  break;
1151  }
1152 
1153  st_report("Performing %d%s Order Polynomial Smoothing between Baseline Regions", env->nfit, nstr);
1154 
1155  /* Now fill in each region between baselines with a polynomial interp */
1156  for(i=0;i<(MAX_BASELINES);i++)
1157  {
1158  b1 = image->binfo.baselines[i][0];
1159  n1 = image->binfo.baselines[i][1] - image->binfo.baselines[i][0];
1160 
1161  b2 = image->binfo.baselines[i+1][0];
1162  n2 = image->binfo.baselines[i+1][1] - image->binfo.baselines[i+1][0];
1163 
1164  if(n1>4 && n2>4)
1165  {
1166  if(recording)
1167  {
1168  sprintf(bname, "bline-poly-%02d.dat", i);
1169 
1170  if((fp = fopen(bname, "w")) == NULL)
1171  {
1172  st_fail("Unable to open %s", bname);
1173  recording = 0;
1174  }
1175  }
1176 
1177  bzero((char *)xd, sizeof(xd));
1178  bzero((char *)yd, sizeof(yd));
1179  bzero((char *)coeff, sizeof(coeff));
1180 
1182  {
1183  st_report("Interp between region %2d & %2d; %4d[%4d] to %4d[%4d]", i, i+1, b1, n1, b2, n2);
1184  }
1185 
1186  /* I only want NUM_OF_BCHANNEL_FOR_POLY channels in either side of the gap; if possible */
1187 
1188  j = 0;
1189  if(n1 > (NUM_OF_BCHANNEL_FOR_POLY-1) && n2 > (NUM_OF_BCHANNEL_FOR_POLY-1)) /* Both have plenty of channels */
1190  {
1191  start = b1 + n1 - NUM_OF_BCHANNEL_FOR_POLY;
1192  end = b1 + n1;
1193 
1194  for(k=start;k<end;k++)
1195  {
1196  xd[j] = image->x1data[k];
1197  yd[j] = image->bplot[k];
1198  j++;
1199  }
1200 
1201  start = b2;
1202  end = b2 + NUM_OF_BCHANNEL_FOR_POLY;
1203 
1204  for(k=start;k<end;k++)
1205  {
1206  xd[j] = image->x1data[k];
1207  yd[j] = image->bplot[k];
1208  j++;
1209  }
1210  }
1211  else
1212  if(n1 > (NUM_OF_BCHANNEL_FOR_POLY-1)) /* n2 somewhere between 2 - 9 channels */
1213  {
1214  start = b1 + n1 - NUM_OF_BCHANNEL_FOR_POLY;
1215  end = b1 + n1;
1216 
1217  for(k=start;k<end;k++)
1218  {
1219  xd[j] = image->x1data[k];
1220  yd[j] = image->bplot[k];
1221  j++;
1222  }
1223 
1224  start = b2;
1225  end = b2 + n2;
1226 
1227  for(k=start;k<end;k++)
1228  {
1229  xd[j] = image->x1data[k];
1230  yd[j] = image->bplot[k];
1231  j++;
1232  }
1233  }
1234  else
1235  if(n2 > (NUM_OF_BCHANNEL_FOR_POLY-1)) /* n1 somewhere between 2 - 9 channels */
1236  {
1237  start = b1;
1238  end = b1 + n2;
1239 
1240  for(k=start;k<end;k++)
1241  {
1242  xd[j] = image->x1data[k];
1243  yd[j] = image->bplot[k];
1244  j++;
1245  }
1246 
1247  start = b2;
1248  end = b2 + NUM_OF_BCHANNEL_FOR_POLY;
1249 
1250  for(k=start;k<end;k++)
1251  {
1252  xd[j] = image->x1data[k];
1253  yd[j] = image->bplot[k];
1254  j++;
1255  }
1256  }
1257  else /* both n1 & n2 somewhere between 2 - 9 channels */
1258  {
1259  for(k=b1;k<(b1+n1);k++)
1260  {
1261  xd[j] = image->x1data[k];
1262  yd[j] = image->bplot[k];
1263  j++;
1264  }
1265 
1266  for(k=b2;k<(b2+n2);k++)
1267  {
1268  xd[j] = image->x1data[k];
1269  yd[j] = image->bplot[k];
1270  j++;
1271  }
1272  }
1273 
1274  if(j < 10)
1275  {
1276  st_fail("Not enought Baseline channels set for this region");
1277  continue;
1278  }
1279 
1280  fitcurve(xd, yd, j, env->nfit, coeff);
1281 
1282  /* Now fill in bplot with results */
1283 
1284  if(verboseLevel & VERBOSE_BASELINES)
1285  {
1286  st_report("%02d: Filling in channels %d to %d", i, b1+n1, b2);
1287  }
1288 
1289  for(k=(b1+n1-1);k<(b2+1);k++)
1290  {
1291  image->bplot[k] = leasev(coeff, env->nfit, image->x1data[k]);
1292  }
1293 
1294  if(recording)
1295  {
1296  for(k=b1;k<(b2+n2);k++)
1297  {
1298  fprintf(fp, "%f %f\n", image->x1data[k], leasev(coeff, env->nfit, image->x1data[k]));
1299  }
1300 
1301  fclose(fp);
1302  }
1303 
1304  /* Now if this is the first region, does it cover the first channels */
1305  if(i == 0 && b1 > 0)
1306  {
1307  if(env->bleadtrail) /* Fill in leading channels with Polynomial from first good baseline ??? */
1308  {
1309  st_report("Poly-Filling in leading channels %d to %d", 0, b1);
1310 
1311  /* Recomute using entire 1st baseline region */
1312  j = 0;
1313  for(k=b1;k<b1+n1;k++)
1314  {
1315  xd[j] = image->x1data[k];
1316  yd[j] = image->bplot[k];
1317  j++;
1318  }
1319 
1320  fitcurve(xd, yd, j, env->nfit, coeff);
1321 
1322  j = 0;
1323  for(k=0;k<b1;k++)
1324  {
1325  image->bplot[k] = leasev(coeff, env->nfit, image->x1data[k]);
1326  j++;
1327  }
1328  }
1329  else /* Just take first good value and propagate to starting channel */
1330  {
1331  st_report("Linear-Filling in leading channels %d to %d", 0, b1);
1332 
1333  firstGoodVal = image->bplot[b1];
1334 
1335  j = 0;
1336  for(k=0;k<b1;k++)
1337  {
1338  image->bplot[k] = firstGoodVal;
1339  j++;
1340  }
1341  }
1342 
1343  if(verboseLevel & VERBOSE_BASELINES)
1344  {
1345  st_report("%02d: Filled in %d leading channels", i, j);
1346  }
1347  }
1348 
1349  /* Now, fill in trailing channels */
1350 
1351  if((i+1) == lastbl && (b2+n2) < noint)
1352  {
1353  lastGoodVal = image->bplot[b2+n2-1];
1354 
1355  if(env->bleadtrail) /* Continue Polynomial fitting out to the last channel ??? */
1356  {
1357  st_report("Poly-Filling in trailing channels %d to %d", b2+n2, noint);
1358 
1359  /* Recomute using entire 1st baseline region */
1360  j = 0;
1361  for(k=b2;k<b2+n2;k++)
1362  {
1363  xd[j] = image->x1data[k];
1364  yd[j] = image->bplot[k];
1365  j++;
1366  }
1367 
1368  fitcurve(xd, yd, j, env->nfit, coeff);
1369 
1370  j=0;
1371  for(k=b2+n2;k<noint;k++)
1372  {
1373  image->bplot[k] = leasev(coeff, env->nfit, image->x1data[k]);
1374  j++;
1375  }
1376 
1377  if(verboseLevel & VERBOSE_BASELINES)
1378  {
1379  st_report("Filled in %d trailing channels", j);
1380  }
1381  }
1382  else /* Take last good value and run it out to end */
1383  {
1384  st_report("Linear-Filling in trailing channels %d to %d", b2+n2, noint);
1385 
1386  j=0;
1387  for(k=b2+n2;k<noint;k++)
1388  {
1389  image->bplot[k] = lastGoodVal;
1390  j++;
1391  }
1392  }
1393 
1394  if(verboseLevel & VERBOSE_BASELINES)
1395  {
1396  st_report("Filled in %d trailing channels", j);
1397  }
1398 
1399  }
1400  }
1401  else
1402  {
1404  {
1405  if(n1 || n2)
1406  {
1407  st_report("Skipping short baseline region %d: n1 = %d, n2 = %d", i, n1, n2);
1408  }
1409  }
1410  }
1411  }
1412  }
1413  else
1414  {
1415  st_fail("bsmoothing can only be 0 or 1; not %d", env->bsmoothing);
1416  return(1);
1417  }
1418 
1419  if(isSet(PF_D_FLAG)) /* Duplicate to H8 image ?? */
1420  {
1421  st_report("Saving baseline model in H8");
1422  bcopy((char *)image, (char *)b8image, sizeof(struct LP_IMAGE_INFO));
1423  bcopy((char *)&image->bplot, (char *)b8image->yplot1, sizeof(b8image->yplot1));
1424  strcpy(b8image->name, "H8"); /* Opps, fix the name */
1425  }
1426 
1427  if(isSet(PF_S_FLAG))
1428  {
1429  args->pflags[PF_S_FLAG].set = 1;
1430 
1431  doBspline();
1432 
1433  if(isSet(PF_D_FLAG)) /* Duplicate to H9 image ?? */
1434  {
1435  st_report("Saving smoothed baseline model in H9");
1436  bcopy((char *)image, (char *)b9image, sizeof(struct LP_IMAGE_INFO));
1437  bcopy((char *)&image->bplot, (char *)b9image->yplot1, sizeof(b9image->yplot1));
1438  strcpy(b9image->name, "H9"); /* Opps, fix the name */
1439  }
1440  }
1441 
1442  return(0);
1443 }
1444 
1445 
1447 {
1448  int i, j, k, n, foundone = 0;
1449  float *f;
1450  double xd[MAX_CHANS], yd[MAX_CHANS], coeff[32], avg;
1451  struct ACCUM_SCAN_RESULT *p;
1452  struct ACCUM_RESULTS_SEND *s;
1453 
1454  if(isSet(PF_O_FLAG)) /* Perform this on the OBJECT arrays ?? */
1455  {
1456  return(do_objectGraceFit());
1457  }
1458 
1459  bzero((char *)xd, sizeof(xd));
1460  bzero((char *)yd, sizeof(yd));
1461  bzero((char *)coeff, sizeof(coeff));
1462 
1463  if(env->nfit == 0)
1464  {
1465  st_report("do_contGraceFit(): Using nfit = %d", env->nfit);
1466 
1467  for(j=0;j<MAX_IFS;j++)
1468  {
1469  n = accum_results_send.ar[j].num;
1470 
1471  if(n)
1472  {
1473  st_report("do_contGraceFit(): IF: %d contains %d records", j, n);
1474 
1475  p = &accum_results_send.ar[j].results[0];
1476 
1477  avg = 0.0;
1478  for(i=0;i<n;i++,p++)
1479  {
1480  switch(env->acc_plot_select_y)
1481  {
1482  case ACCUM_PLOT_Y_SIG: avg += p->sig; break;
1483  case ACCUM_PLOT_Y_SKY: avg += p->sky; break;
1484  case ACCUM_PLOT_Y_SMROR: avg += p->smror; break;
1485  case ACCUM_PLOT_Y_TSYS: avg += p->tsys; break;
1486  case ACCUM_PLOT_Y_TASTAR: avg += p->tastar; break;
1487  case ACCUM_PLOT_Y_TR: avg += p->tr; break;
1488  case ACCUM_PLOT_Y_JANSKY: avg += p->janskys; break;
1489  case ACCUM_PLOT_Y_NORMAL: avg += p->normalized; break;
1490  }
1491  }
1492 
1493  avg /= (double)n;
1494 
1495  st_report("do_contGraceFit(): Average for IF: (%d):, %f", j, avg);
1496 
1497  f = &accum_results_send.fitcurve[j][0];
1498  for(i=0;i<n;i++,f++)
1499  {
1500  *f = (float)avg;
1501  }
1502 
1503  foundone++;
1504  }
1505  }
1506 
1507  if(foundone)
1508  {
1511 
1512  sendAccumResults();
1513  }
1514  else
1515  {
1516  st_fail("do_contGraceFit(): No data to fit");
1517  return(1);
1518  }
1519  }
1520  else
1521  if(env->nfit > 0 && env->nfit < MAX_NFIT)
1522  {
1523  st_report("do_contGraceFit(): Using nfit = %d", env->nfit);
1524 
1525  /* Convert floats to doubles */
1526 
1527  for(j=0;j<MAX_IFS;j++)
1528  {
1529  n = accum_results_send.ar[j].num;
1530 
1531  if(n)
1532  {
1533  st_report("do_contGraceFit(): IF: %d contains %d records", j, n);
1534 
1535  p = &accum_results_send.ar[j].results[0];
1536 
1537  for(i=0;i<n;i++,p++)
1538  {
1539  switch(env->acc_plot_select_x)
1540  {
1541  case ACCUM_PLOT_X_EL: xd[i] = p->el; break;
1542  case ACCUM_PLOT_X_UT: xd[i] = p->ut; break;
1543  case ACCUM_PLOT_X_SCAN: xd[i] = p->scan; break;
1544  default: xd[i] = (double)i; break;
1545  }
1546 
1547  switch(env->acc_plot_select_y)
1548  {
1549  case ACCUM_PLOT_Y_SIG: yd[i] = p->sig; break;
1550  case ACCUM_PLOT_Y_SKY: yd[i] = p->sky; break;
1551  case ACCUM_PLOT_Y_SMROR: yd[i] = p->smror; break;
1552  case ACCUM_PLOT_Y_TSYS: yd[i] = p->tsys; break;
1553  case ACCUM_PLOT_Y_TASTAR: yd[i] = p->tastar; break;
1554  case ACCUM_PLOT_Y_TR: yd[i] = p->tr; break;
1555  case ACCUM_PLOT_Y_JANSKY: yd[i] = p->janskys; break;
1556  case ACCUM_PLOT_Y_NORMAL: yd[i] = p->normalized; break;
1557  }
1558  }
1559 
1560  fitcurve(xd, yd, n, env->nfit, coeff);
1561 
1562  if(env->issmt)
1563  {
1564  st_print("Linuxpops(C) >> # do_contGraceFit(): Results for IF: (%s): ", mg_sideband[j]);
1565  }
1566  else
1567  {
1568  st_print("Linuxpops(C) >> # do_contGraceFit(): Results for IF: (%s): ", kp_sideband[j]);
1569  }
1570 
1571  for(k=0;k<env->nfit+1;k++)
1572  {
1573  st_print("a%d = %.4e ", k, coeff[k]);
1574 
1575  accum_results_send.coeff[j][k] = coeff[k];
1576  }
1577 
1578  st_print("\n");
1579 
1580  /* Now fill in fitcurve with results */
1581  p = &accum_results_send.ar[j].results[0];
1582  f = &accum_results_send.fitcurve[j][0];
1583 
1584  for(i=0;i<n;i++,f++,p++)
1585  {
1586  *f = (float)leasev(coeff, env->nfit, p->el);
1587  }
1588 
1589  foundone++;
1590  }
1591  }
1592 
1593  if(foundone)
1594  {
1595  s = &accum_results_send;
1596 
1597  s->bplot = 1;
1598  s->ncoeff = env->nfit;
1599 
1600  sendAccumResults();
1601 
1602  if(isSet(PF_S_FLAG)) /* Save coeff to elgain terms?? */
1603  {
1604  st_report("Saving %d Elevation Gain Terms", env->nfit+1);
1605 
1606  env->elgain_term1 = 0.0; /* Zero old ones out first */
1607  env->elgain_term2 = 0.0;
1608  env->elgain_term2 = 0.0;
1609  env->elgain_term4 = 0.0;
1610  env->elgain_term5 = 0.0;
1611 
1612  for(k=0;k<env->nfit+1;k++)
1613  {
1614  switch(k)
1615  {
1616  case 0: env->elgain_term1 = (s->coeff[0][k] + s->coeff[1][k] + s->coeff[2][k] + s->coeff[3][k]) / (float)foundone; break;
1617  case 1: env->elgain_term2 = (s->coeff[0][k] + s->coeff[1][k] + s->coeff[2][k] + s->coeff[3][k]) / (float)foundone; break;
1618  case 2: env->elgain_term3 = (s->coeff[0][k] + s->coeff[1][k] + s->coeff[2][k] + s->coeff[3][k]) / (float)foundone; break;
1619  case 3: env->elgain_term4 = (s->coeff[0][k] + s->coeff[1][k] + s->coeff[2][k] + s->coeff[3][k]) / (float)foundone; break;
1620  case 4: env->elgain_term5 = (s->coeff[0][k] + s->coeff[1][k] + s->coeff[2][k] + s->coeff[3][k]) / (float)foundone; break;
1621  }
1622  }
1623  }
1624  }
1625  else
1626  {
1627  st_fail("do_contGraceFit(): No data to fit");
1628  return(1);
1629  }
1630  }
1631  else
1632  {
1633  st_fail("Error: nfit out of range: %d, [0 - %d]", env->nfit, MAX_NFIT-1);
1634  return(1);
1635  }
1636 
1637  return(0);
1638 }
1639 
1640 
1642 {
1643  int i, j, n, ret;
1644  struct LP_IMAGE_INFO *image = h0;
1645  double xd[MAX_CHANS], yd[MAX_CHANS], coeff[32], avg;
1646 
1647  if(env->mode == MODE_CONT)
1648  {
1649  ret = do_contGraceFit();
1650 
1651  return(ret);
1652  }
1653 
1654  bzero((char *)xd, sizeof(xd));
1655  bzero((char *)yd, sizeof(yd));
1656  bzero((char *)coeff, sizeof(coeff));
1657 
1658  n = image->head.noint;
1659 
1660  if(env->nfit == 0)
1661  {
1662  st_report("graceFit(): Using nfit = %d", env->nfit);
1663 
1664  image->pinfo.bplot = 1;
1665 
1666  j = 0;
1667  avg = 0.0;
1668  for(i=0;i<n;i++)
1669  {
1670  if(isInBaseLineRegion(i))
1671  {
1672  avg += image->yplot1[i];
1673  j++;
1674  }
1675  }
1676 
1677  if(j)
1678  {
1679  avg /= (double)j;
1680 
1681  /* Now fill in bplot with results */
1682  for(i=0;i<image->head.noint;i++)
1683  {
1684  image->bplot[i] = avg;
1685  }
1686  }
1687  else
1688  {
1689  st_fail("No baseline regions set");
1690  return(1);
1691  }
1692  }
1693  else
1694  if(env->nfit > 0 && env->nfit < MAX_NFIT)
1695  {
1696  st_report("graceFit(): Using nfit = %d", env->nfit);
1697 
1698  image->pinfo.bplot = 1;
1699 
1700  /* Convert floats to doubles */
1701  j = 0;
1702  for(i=0;i<n;i++)
1703  {
1704  if(isInBaseLineRegion(i))
1705  {
1706  xd[j] = image->x1data[i];
1707  yd[j] = image->yplot1[i];
1708  j++;
1709  }
1710  }
1711 
1712  if(j < 10)
1713  {
1714  st_fail("Not enought Baseline channels set");
1715  return(1);
1716  }
1717 
1718  fitcurve(xd, yd, j, env->nfit, coeff);
1719 
1720  /* Now fill in bplot with results */
1721  for(i=0;i<image->head.noint;i++)
1722  {
1723  image->bplot[i] = leasev(coeff, env->nfit, image->x1data[i]);
1724  }
1725  }
1726  else
1727  {
1728  st_fail("Error: nfit out of range: %d, [0 - 19]", env->nfit);
1729  }
1730 
1731  xx();
1732 
1733  return(0);
1734 }
1735 
1736 
1738 {
1739  double diff;
1740  struct LP_IMAGE_INFO *image = h0;
1741 
1742  st_report("Removing Computed Baseline from: %s", image->name);
1743 
1744  env->rm_fit_data = 1;
1745 
1746  removeFittedData(image);
1747 
1748  env->rm_fit_data = 0;
1749 
1750  if(isSet(PF_A_FLAG))
1751  {
1752  doAutoScale();
1753  }
1754 
1755  if(isSet(PF_F_FLAG))
1756  {
1757  diff = env->yupper - env->ylower;
1758 
1759  env->yupper = (diff * 0.5);
1760  env->ylower = -(diff * 0.5);
1761  }
1762 
1763  return(0);
1764 }
1765 
1766 
1767 int doBmove()
1768 {
1769  int i, ret, seg=0, now, then, min = 999999, mindx = -1;
1770  char hint[256];
1771  struct LP_IMAGE_INFO *image = h0;
1772 
1773  sprintf(hint, "Click Left Mouse Button on Edge of Segment to move");
1774  ret = _doCCur(hint);
1775 
1776  if(ret)
1777  {
1778  return(1);
1779  }
1780 
1781  now = (int)round(env->ccur);
1782 
1783  sprintf(hint, "Click Left Mouse Button in New Location");
1784  ret = _doCCur(hint);
1785 
1786  if(ret)
1787  {
1788  return(1);
1789  }
1790 
1791  then = (int)round(env->ccur);
1792 
1793  if(then < 1)
1794  {
1795  then = 1;
1796  }
1797  else
1798  if(then > image->head.noint)
1799  {
1800  then = image->head.noint-1;
1801  }
1802 
1803  for(i=0;i<MAX_BASELINES;i++)
1804  {
1805  if(env->baselines[i][0] != 0 && env->baselines[i][1] != 0) /* definded ?? */
1806  {
1807  if(abs(env->baselines[i][0]-now) < min)
1808  {
1809  seg = i;
1810  mindx = 0;
1811  min = abs(env->baselines[i][0]-now);
1812  }
1813 
1814  if(abs(env->baselines[i][1]-now) < min)
1815  {
1816  seg = i;
1817  mindx = 1;
1818  min = abs(env->baselines[i][1]-now);
1819  }
1820  }
1821  }
1822 
1823  if(seg >= 0 && seg < MAX_BASELINES)
1824  {
1825  if(mindx == 0 || mindx == 1)
1826  {
1827  st_report("Closest segment: %d, part: %d; Channel: %d", seg+1, mindx, env->baselines[seg][mindx]);
1828  st_report("Moving Segment %d edge from channel %d to %d", seg+1, now, then);
1829 
1830  env->baselines[seg][mindx] = then;
1831 
1832  if(isSet(PF_B_FLAG))
1833  {
1834  doBshape();
1835  }
1836  else
1837  if(isSet(PF_G_FLAG))
1838  {
1839  doGraceFit();
1840  }
1841 
1842  return(0);
1843  }
1844  }
1845 
1846  st_fail("Seg: %d or index: %d; out of range", seg, mindx);
1847 
1848  return(1);
1849 }
1850 
1851 
1852 
1853 int doBloop()
1854 {
1855  while(1)
1856  {
1857  if(doBmove() == 1)
1858  {
1859  return(0);
1860  }
1861 
1862  if(isSet(PF_B_FLAG))
1863  {
1864  doBshape();
1865  }
1866  else
1867  if(isSet(PF_G_FLAG))
1868  {
1869  doGraceFit();
1870  }
1871 
1872  xx();
1873  }
1874 
1875  return(0);
1876 }
1877 
1878 
1880 {
1881  int seg, start, end;
1882 
1883  /* /c flag clears the whole thing */
1884  if(isSet(PF_C_FLAG))
1885  {
1886  bzero((char *)&env->baselines[0], sizeof(env->baselines));
1887  }
1888 
1889  if(args->ntokens > 3)
1890  {
1891  seg = atoi(args->secondArg);
1892 
1893  if(seg >=0 && seg < MAX_BASELINES)
1894  {
1895  start = atoi(args->thirdArg);
1896  end = atoi(args->fourthArg);
1897 
1898  st_report("Setting Seg %2d: %4d %4d", seg, start, end);
1899 
1900  env->baselines[seg][0] = start;
1901  env->baselines[seg][1] = end;
1902  }
1903  else
1904  {
1905  st_fail("Baseline Segment %d, out of range", seg);
1906  return(1);
1907  }
1908  }
1909 
1910  if(isSet(PF_L_FLAG))
1911  {
1912  tellBaselines();
1913  }
1914 
1915  return(0);
1916 }
1917 
1918 
1920 {
1921  int i, n=0;
1922  FILE *fp;
1923 
1924  if(args->ntokens < 2)
1925  {
1926  st_fail("Usage: writebase filename");
1927  return(0);
1928  }
1929  else
1930  {
1931  if((fp = fopen(args->secondArg, "w")) == NULL)
1932  {
1933  st_fail("Unable to open: %s", args->secondArg);
1934  return(0);
1935  }
1936 
1937  fprintf(fp, "nbaseset /c\n"); /* Clear them out first */
1938 
1939  for(i=0;i<MAX_BASELINES;i++)
1940  {
1941  if(env->baselines[i][0] || env->baselines[i][1])
1942  {
1943  fprintf(fp, "nbaseset %2d %4d %4d\n", i, env->baselines[i][0], env->baselines[i][1]);
1944  n++;
1945  }
1946  }
1947 
1948  st_report("Wrote %d baseline segments to: '%s'", n, args->secondArg);
1949 
1950  fclose(fp);
1951  }
1952 
1953  return(0);
1954 }
1955 
1956 
1958 {
1959  int i, n, j;
1960  double avg;
1961 
1962  if(isSet(PF_A_FLAG)) /* take average and remove */
1963  {
1964  avg = 0.0;
1965  n = h0->head.noint;
1966 
1967  j = 0;
1968  for(i=0;i<n;i++)
1969  {
1970  if(isInBaseLineRegion(i))
1971  {
1972  avg += h0->yplot1[i];
1973  j++;
1974  }
1975  }
1976 
1977  if(j)
1978  {
1979  avg /= (double)j;
1980  }
1981  else
1982  {
1983  st_fail("No baseline regions defined");
1984  return(1);
1985  }
1986 
1987  for(i=0;i<n;i++)
1988  {
1989  h0->yplot1[i] -= avg;
1990  }
1991 
1992  return(0);
1993  }
1994 
1995  if(args->ntokens > 1)
1996  {
1997  avg = atof(args->secondArg);
1998 
1999  if(isnormal(avg))
2000  {
2001  n = h0->head.noint;
2002 
2003  for(i=0;i<n;i++)
2004  {
2005  h0->yplot1[i] -= avg;
2006  }
2007  }
2008  else
2009  {
2010  printH_FlagHelp("dcoffset");
2011 
2012  return(1);
2013  }
2014  }
2015  else
2016  {
2017  printH_FlagHelp("dcoffset");
2018 
2019  return(1);
2020  }
2021 
2022  return(0);
2023 }
2024 
int rm_fit_data
Definition: env.h:133
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int xx()
Definition: linuxpops.c:605
int bsmoothing
Definition: env.h:36
double rms
Definition: env.h:262
char * mg_sideband[]
Definition: strings.c:78
int num
Definition: accum.h:47
#define ACCUM_PLOT_X_EL
Definition: accum.h:10
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
int edrop
Definition: env.h:52
struct ACCUM_RESULTS_SEND accum_results_send
Definition: cont.c:62
double noint
Definition: header.h:211
int computeEachBaseLine(struct LP_IMAGE_INFO *image)
Definition: baseline.c:102
Definition: parse.h:8
int writeBaseLines()
Definition: baseline.c:1919
char * kp_sideband[]
Definition: strings.c:72
double elgain_term1
Definition: env.h:216
struct BASELINE_INFO binfo
Definition: image.h:101
int line_doBaseLine(struct LP_IMAGE_INFO *image)
Definition: baseline.c:291
#define MAX_CHANS
Definition: defines.h:38
int baselinesDefined(struct LP_IMAGE_INFO *image)
Definition: baseline.c:948
#define VERBOSE_BASELINES
Definition: defines.h:71
int zero_bl_averages()
Definition: baseline.c:67
double ccur
Definition: env.h:176
float tsys
Definition: accum.h:38
char name[32]
Definition: image.h:73
int getNumericFlag()
Definition: parse.c:196
#define MODE_CONT
Definition: defines.h:86
FILE * fp
Definition: linuxpops.c:70
int acc_plot_select_x
Definition: env.h:19
int printH_FlagHelp(char *cmd)
Definition: help.c:430
double slope
Definition: env.h:266
#define min(a, b)
Definition: cactus.h:114
float scan
Definition: accum.h:32
#define ACCUM_PLOT_Y_TSYS
Definition: accum.h:19
struct LP_IMAGE_INFO H9
Definition: image.c:72
int nbshape
Definition: env.h:80
int removeOneSegment()
Definition: baseline.c:748
void boxcar(float *input, float *output, int len, int order)
Definition: box.c:92
int computeBaseLine(struct LP_IMAGE_INFO *image)
Definition: baseline.c:200
int issmt
Definition: env.h:63
struct PARSE_FLAGS pflags[MAX_PARSE_FLAGS]
Definition: parse.h:178
#define LINEAR_BASELINE_SMOOTHING
Definition: defines.h:46
struct ACCUM_RESULTS ar[MAX_IFS]
Definition: accum.h:88
struct PLOT_INFO pinfo
Definition: image.h:102
float normalized
Definition: accum.h:43
#define ACCUM_PLOT_Y_SMROR
Definition: accum.h:18
int computeRms(float *y, int n, float *rms)
Definition: line.c:82
#define MAX_BASELINES
Definition: defines.h:42
int do_contGraceFit()
Definition: baseline.c:1446
#define ACCUM_PLOT_Y_SKY
Definition: accum.h:17
#define NUM_OF_BCHANNEL_FOR_POLY
Definition: baseline.c:61
int bleadtrail
Definition: env.h:33
int findNextGood(struct LP_IMAGE_INFO *image, int indx, int *ngn, float *ng)
Definition: baseline.c:996
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int _doCCur(char *msg)
Definition: linuxpops.c:2411
int nfit
Definition: env.h:81
int dobaseline
Definition: env.h:48
float x1data[MAX_CHANS]
Definition: image.h:117
char * thirdArg
Definition: parse.h:182
int setNegBaselines()
Definition: baseline.c:383
int doNBaseset()
Definition: baseline.c:1879
int copyEnv2Image(struct LP_IMAGE_INFO *image)
Definition: image.c:784
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
int doBshape()
Definition: baseline.c:1023
#define ACCUM_PLOT_Y_SIG
Definition: accum.h:16
int doBmove()
Definition: baseline.c:1767
int fitcurve(double *x, double *y, int n, int ideg, double *coeff)
Definition: grace_fit.c:239
#define MAX_NFIT
Definition: accum.h:6
int sptip_lsqfit(float x[], float y[], int bdata, int edata, float sig[], int mwt, float *a, float *b, float *siga, float *sigb, float *chi2, float *q)
#define ACCUM_PLOT_Y_TR
Definition: accum.h:21
float tastar
Definition: accum.h:39
double elgain_term5
Definition: env.h:220
double leasev(double *c, int degree, double x)
Definition: grace_fit.c:210
void st_fail()
int bshow
Definition: env.h:35
int addOneOrMoreSegment()
Definition: baseline.c:683
struct ACCUM_SCAN_RESULT results[MAX_ACCUM_SCANS]
Definition: accum.h:49
double ylower
Definition: env.h:287
double bl_averages[MAX_BASELINES]
Definition: baseline.c:64
int getNumericResponse()
int doBspline()
Definition: bspline_fit.c:71
#define ACCUM_PLOT_X_SCAN
Definition: accum.h:12
float janskys
Definition: accum.h:42
int doRmBase()
Definition: baseline.c:1737
int tellBaselines()
Definition: baseline.c:358
int isInBaseLineRegion(int chan)
Definition: baseline.c:336
int mapBaseline()
Definition: gridmap.c:2255
int bplot
Definition: image.h:35
int doGraceFit()
Definition: baseline.c:1641
#define ACCUM_PLOT_Y_TASTAR
Definition: accum.h:20
int mode
Definition: env.h:75
int parseMouseValues(double x1, double x2, double y1, double y2)
Definition: linuxpops.c:2154
void st_report()
Definition: parse.h:9
int valid_bl_averages
Definition: baseline.c:63
double rst
Definition: env.h:263
int recording
Definition: parse.c:83
int set
Definition: parse.h:88
char * fourthArg
Definition: parse.h:183
int bdrop
Definition: env.h:31
double elgain_term3
Definition: env.h:218
#define POLY_BASELINE_SMOOTHING
Definition: defines.h:47
int baselines[MAX_BASELINES][2]
Definition: image.h:9
float fitcurve[MAX_IFS][MAX_ACCUM_SCANS]
Definition: accum.h:90
double yupper
Definition: env.h:288
int setBaselines()
Definition: baseline.c:783
int acc_plot_select_y
Definition: env.h:20
double intercept
Definition: env.h:210
int sendAccumResults()
Definition: cont.c:452
#define ACCUM_PLOT_Y_NORMAL
Definition: accum.h:23
int doAutoScale()
Definition: linuxpops.c:2788
int sortBaselines()
Definition: baseline.c:611
double avg
Definition: beam.h:5
double elgain_term2
Definition: env.h:217
char * secondArg
Definition: parse.h:181
float bplot[MAX_CHANS]
Definition: image.h:116
void st_print()
int baselines[MAX_BASELINES][2]
Definition: env.h:30
double coeff[MAX_IFS][MAX_NFIT]
Definition: accum.h:84
int removeFittedData(struct LP_IMAGE_INFO *image)
Definition: line.c:773
#define m(i)
int ntokens
Definition: parse.h:173
int doBloop()
Definition: baseline.c:1853
float smror
Definition: accum.h:37
#define MAX_IFS
Definition: accum.h:5
int dcOffset()
Definition: baseline.c:1957
int bmark
Definition: env.h:34
int doBaseline()
Definition: baseline.c:77
int findLastGood(struct LP_IMAGE_INFO *image, int indx, int *lgn, float *lg)
Definition: baseline.c:970
int do_objectGraceFit()
Definition: object.c:942
double elgain_term4
Definition: env.h:219
struct LP_IMAGE_INFO H8
Definition: image.c:71
int findEmptyBaseline()
Definition: baseline.c:667
#define ACCUM_PLOT_X_UT
Definition: accum.h:11
#define ACCUM_PLOT_Y_JANSKY
Definition: accum.h:22
int verboseLevel
Definition: linuxpops.c:80