Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
badchan.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 ZERO_CHANNEL -1
62 
81 int addBadChan(bad)
82 int bad;
83 {
84  int i;
85 
86  /* Already set ? */
87  if(isBadChan(bad))
88  {
89  st_print("Channel %d, already set\n", bad);
90  return(0);
91  }
92 
93  /* Find a slot to put it in */
94  for(i=0;i<MAX_BAD_CHANS;i++)
95  {
96  if(env->badchans[i] == -1)
97  {
98  st_report("Added %d to BadChan Array", bad);
99 
100  env->badchans[i] = bad;
101 
102  return(0);
103  }
104  }
105 
106  st_fail("Bad Chan Array Full");
107 
108  return(1);
109 }
110 
111 
114 int bad;
115 {
116  int i;
117 
118  for(i=0;i<MAX_BAD_CHANS;i++)
119  {
120  if(env->badchans[i] == bad)
121  {
122  st_report("Removing %d from Bad Chan Array", bad);
123  env->badchans[i] = -1;
124 
125  return(0);
126  }
127  }
128 
129  st_fail("Bad Chan %d not found in Bad Chan Array()", bad);
130 
131  return(1);
132 }
133 
134 
135 int autoFind(flag)
136 int flag;
137 {
138  int i;
139  float *f;
140  double val;
141 
142  if(args->ntokens < 2)
143  {
144  st_fail("autoBad(): Missing args");
145  return(1);
146  }
147 
148  if(strlen(args->secondArg))
149  {
150  val = atof(args->secondArg);
151  }
152  else
153  {
154  return(1);
155  }
156 
157  f = &h0->yplot1[0];
158  if(flag > 0)
159  {
160  for(i=0;i<h0->head.noint;i++,f++)
161  {
162  if(*f > val)
163  {
164  addBadChan(i);
165  }
166  }
167  }
168  else
169  if(flag < 0)
170  {
171  for(i=0;i<h0->head.noint;i++,f++)
172  {
173  if(*f < val)
174  {
175  addBadChan(i);
176  }
177  }
178  }
179  else
180  {
181  for(i=0;i<h0->head.noint;i++,f++)
182  {
183  if(fabs(*f - val) < 0.001)
184  {
185  addBadChan(i);
186  }
187  }
188  }
189 
190  return(0);
191 }
192 
193 
195 {
196  int i, n, s, m;
197 
198  if(args->ntokens < 3)
199  {
200  printH_FlagHelp("badchan");
201 
202  return(1);
203  }
204 
205  s = atoi(args->secondArg);
206  m = atoi(args->thirdArg);
207 
208  n = (int)h0->head.noint;
209 
210  if(n == 0)
211  {
212  st_fail("Looks like no scan has been loaded");
213  return(1);
214  }
215 
216  if(s < 0 || s > n)
217  {
218  st_fail("Starting channel, %d, must be >= 0 & < %d", s, n);
219 
220  return(1);
221  }
222 
223  if(m < 0 || m > n)
224  {
225  st_fail("Modulo number, %d, must be >= 0 & < %d", m, n);
226 
227  return(1);
228  }
229 
230  i = s;
231  while(i<n)
232  {
233  addBadChan(i);
234 
235  i += m;
236  }
237 
238  return(0);
239 }
240 
241 
245 {
246  int i, n, bad;
247  char hint[256];
248 
249  /* Zero out the current ones if requested */
250  if(isSet(PF_C_FLAG))
251  {
252  clearBadChan();
253 
254  return(0);
255  }
256 
257  if(isSet(PF_I_FLAG))
258  {
259  return(setBadInterval());
260  }
261 
262  if(isSet(PF_A_FLAG))
263  {
264  if(isSet(PF_E_FLAG))
265  {
266  return(autoFind(0));
267  }
268  else
269  if(isSet(PF_L_FLAG))
270  {
271  return(autoFind(-1));
272  }
273  else
274  if(isSet(PF_G_FLAG))
275  {
276  return(autoFind(1));
277  }
278 
279  return(0);
280  }
281 
282  if(strlen(args->secondArg))
283  {
284  bad = atoi(args->secondArg);
285 
286  if(bad >= 0)
287  {
288  addBadChan(bad);
289 
290  return(0);
291  }
292  else
293  if(bad < 0)
294  {
295  removeBadChanFromArray(bad * -1);
296 
297  return(0);
298  }
299  else
300  {
301  st_fail("Bad Chan must be >= 0");
302 
303  return(1);
304  }
305  }
306 
307  do
308  {
309  n = getNumericResponse("Enter Number of Bad Channels [ Up to 9]: ");
310  }
311  while(n < 0);
312 
313  if( n == 0)
314  {
315  return(0);
316  }
317 
318  for(i=0;i<n;i++)
319  {
320  sprintf(hint, "Click Left Mouse Button to pick Bad Channel %d", i+1);
321 
322  _doCCur(hint);
323 
324  bad = (int)round(env->ccur);
325 
326  if(bad >= 0)
327  {
328  addBadChan(bad);
329  }
330  else
331  {
332  st_fail("Can't Add/Remove BadChan %d", bad);
333  }
334  }
335 
336  /* Show the user which channels are flagged */
337  tellBadChan();
338 
339  /* Tell the system to remove bad channels when encountered */
340  env->dobadchan = 1;
341 
342  /* Remove them now */
344 
345  return(0);
346 }
347 
348 
352 {
353  int i;
354 
355  st_print("%s # BadChan: \t", prompt);
356 
357  for(i=0;i<MAX_BAD_CHANS;i++)
358  {
359  if(env->badchans[i] >= 0)
360  {
361  st_print("%4d ", env->badchans[i]);
362 
363  if(i > 0 && !(i%8))
364  {
365  st_print("\n\t\t\t\t");
366  }
367  }
368  }
369 
370  st_print("\n");
371 
372  return(0);
373 }
374 
375 
376 /* Is candidate channel, 'bad', flagged as bad */
377 int isBadChan(bad)
378 int bad;
379 {
380  int i;
381 
382  for(i=0;i<MAX_BAD_CHANS;i++)
383  {
384  if(env->badchans[i] == bad)
385  {
386  return(1);
387  }
388  }
389 
390  return(0);
391 }
392 
393 
397 {
398  int i;
399 
400  st_report("Clearing out all Bad Chans");
401 
402  for(i=0;i<MAX_BAD_CHANS;i++)
403  {
404  env->badchans[i] = -1;
405  }
406 
407  return(0);
408 }
409 
410 
412 {
413  FILE *fp;
414  char *string, line[256], fname[256];
415  int bad;
416 
417  if(strlen(args->secondArg) < 2)
418  {
419  st_warn("Missing arg for readIgnoreFile()");
420  return(1);
421  }
422 
423  strcpy(fname, args->secondArg);
424 
425  if((fp = fopen(fname, "r") ) <= (FILE *)0)
426  {
427  fprintf(stderr, "Unable to open file %s\n", fname);
428  return(0);
429  }
430 
431  while((string = fgets(line, sizeof(line), fp)) != NULL)
432  {
433  if(line[0] == '#')
434  {
435  continue;
436  }
437 
438  deNewLineIt(line); /* Clean it up */
439 
440  st_report("Parsing: %s", line);
441 
442  bad = atoi(line);
443 
444  if(bad >= 0)
445  {
446  addBadChan(bad);
447  }
448  else
449  if(bad < 0)
450  {
451  removeBadChanFromArray(bad * -1);
452  }
453  else
454  {
455  st_fail("Can't Add/Remove BadChan %d", bad);
456  }
457  }
458 
459  tellBadChan();
460 
461  fclose(fp);
462 
463  return(0);
464 }
465 
466 
468 {
469  env->badchanshow = 1;
470 
471  xx();
472 
473  env->badchanshow = 0;
474 
475  return(0);
476 }
477 
478 
479 /* Return the data value of the next available good channel */
480 float findNextGoodChan(image, j)
481 struct LP_IMAGE_INFO *image;
482 int j;
483 {
484  int i;
485  float f = 0.0;
486 
487  for(i=j;i<image->head.noint;i++)
488  {
489  if(!isBadChan(i))
490  {
491  return(image->yplot1[i]);
492  }
493  }
494 
495  return(f);
496 }
497 
498 
499 /* Return the data value of the previous available good channel */
500 float findPreGoodChan(image, j)
501 struct LP_IMAGE_INFO *image;
502 int j;
503 {
504  int i;
505  float f = 0.0;
506 
507  for(i=j;i>0;i--)
508  {
509  if(!isBadChan(i))
510  {
511  return(image->yplot1[i]);
512  }
513  }
514 
515  return(f);
516 }
517 
518 
519 /* First remove any bad channels marked & copy to local yplot array
520  */
521 int line_removeBadChans(image, print)
522 struct LP_IMAGE_INFO *image;
523 int print;
524 {
525  int i, first=1, j=0;
526 
527  for(i=0;i<image->head.noint;i++)
528  {
529  if(env->dobadchan && isBadChan(i))
530  {
531  if(i > 0 && i < image->head.noint-1)
532  {
533  if(print && first)
534  {
535  st_print("%s # Smoothing Bad Chan: ", prompt);
536  first = 0;
537  }
538 
539  if(print)
540  {
541  st_print("%4d ", i);
542  }
543 
544  j++;
545 
546  if(print && !(j%8))
547  {
548  st_print("\n\t\t\t\t\t ");
549  }
550 
551  image->yplot1[i] = (findPreGoodChan(image, i) + findNextGoodChan(image, i)) / 2.0;
552  }
553  else
554  if(i == 0) /* Nothing to average; set to next good value */
555  {
556  if(print && first)
557  {
558  st_print("%s # Smoothing Bad Chan: ", prompt);
559  first = 0;
560  }
561 
562  if(print)
563  {
564  st_print("%4d ", i);
565  }
566 
567  j++;
568 
569  if(print && !(j%8))
570  {
571  st_print("\n\t\t\t\t\t ");
572  }
573 
574  image->yplot1[i] = findNextGoodChan(image, i);
575  }
576  else
577  if(i == image->head.noint-1) /* Nothing to average; set to previous good value */
578  {
579  if(print && first)
580  {
581  st_print("%s # Smoothing Bad Chan: ", prompt);
582  first = 0;
583  }
584 
585  if(print)
586  {
587  st_print("%4d ", i);
588  }
589 
590  j++;
591 
592  if(print && !(j%8))
593  {
594  st_print("\n\t\t\t\t\t ");
595  }
596 
597  image->yplot1[i] = findPreGoodChan(image, i);
598  }
599  else /* No ajacent channels to smooth to, so forget it */
600  {
601  image->yplot1[i] = image->yplot1[i];
602  }
603  }
604  else
605  {
606  image->yplot1[i] = image->yplot1[i];
607  }
608  }
609 
610  /* Found at least one; finish the print staement */
611  if(print && !first)
612  {
613  st_print("\n");
614  }
615 
616  return(0);
617 }
618 
619 
620 
621 int doSpike()
622 {
623  int i, j, guesses[MAX_CHANS], remove=0;
624  float cval, aval, ratio;
625  float boxout[MAX_CHANS];
626  struct LP_IMAGE_INFO *image = h0;
627 
628  if(strlen(args->secondArg))
629  {
630  if(!strncmp(args->secondArg, "/r", 2))
631  {
632  remove = 1;
633  }
634  }
635 
636  boxcar(&image->yplot1[0], &boxout[0], image->head.noint, 8);
637 
638  bzero((char *)&guesses, sizeof(guesses));
639  j=0;
640 
641  for(i=0;i<image->head.noint;i++)
642  {
643  if(i > 0 && i < image->head.noint-1)
644  {
645  cval = image->yplot1[i];
646  aval = (image->yplot1[i-1] + image->yplot1[i+1]) / 2.0;
647  ratio = cval / boxout[i];
648  st_print("C[%3d] = %f, Avg = %f, ratio = %f", i, cval, aval, ratio);
649 
650  if(ratio < 0.85 || ratio > 1.25)
651  {
652  st_print(" *\n");
653  guesses[j] = i;
654  j++;
655  }
656  else
657  {
658  st_print("\n");
659  }
660  }
661  }
662 
663  if(j)
664  {
665  st_print("Possible Bad Channels: ");
666 
667  for(i=0;i<j;i++)
668  {
669  st_print("%d ", guesses[i]);
670  if(remove)
671  {
672  subArgs(2, "%d", guesses[i]);
673  ignoreBadChan();
674  }
675  }
676  st_print("\n");
677  }
678 
679  return(0);
680 }
681 
682 
689 {
690  int e, i;
691 
692  for(e=0;e<MAX_ENV;e++)
693  {
694  if(CGM->envrs[e].badchanepoch < 1482161359)
695  {
696  st_report("Upgrading Bad Channel Array[%d]", e);
697 
698  for(i=0;i<MAX_BAD_CHANS;i++)
699  {
700  if(CGM->envrs[e].badchans[i] == 0)
701  {
702  CGM->envrs[e].badchans[i] = -1;
703  }
704  }
705 
706  CGM->envrs[e].badchanepoch = time(NULL);
707  }
708  }
709 
710  return(0);
711 }
712 
713 
715 {
716  int i, half;
717 
718  if(args->ntokens > 1)
719  {
720  half = atoi(args->secondArg);
721  }
722  else
723  {
724  printH_FlagHelp("showbad");
725 
726  return(0);
727  }
728 
729  st_print("%s # BadChan for Rambo: \t", prompt);
730 
731  for(i=0;i<MAX_BAD_CHANS;i++)
732  {
733  if(env->badchans[i] >= 0)
734  {
735  st_print("%4d ", half + env->badchans[i] + 1);
736 
737  if(i > 0 && !(i%8))
738  {
739  st_print("\n\t\t\t\t");
740  }
741  }
742  }
743 
744  st_print("\n");
745 
746  return(0);
747 }
748 
749 
751 {
752  int i;
753  FILE *fp;
754  char fname[256];
755 
756  if(strlen(args->secondArg) < 2)
757  {
758  st_warn("Missing arg for writeBadChans()");
759  return(1);
760  }
761 
762  strcpy(fname, args->secondArg);
763 
764  if((fp = fopen(fname, "w") ) <= (FILE *)0)
765  {
766  fprintf(stderr, "Unable to open file %s\n", fname);
767  return(0);
768  }
769 
770  fprintf(fp, "badch /c /i\n");
771 
772  for(i=0;i<MAX_BAD_CHANS;i++)
773  {
774  if(env->badchans[i] >= 0)
775  {
776  fprintf(fp, "badch %4d /i\n", env->badchans[i]);
777  }
778  }
779 
780  fclose(fp);
781 
782  return(0);
783 }
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int xx()
Definition: linuxpops.c:605
#define MAX_BAD_CHANS
Definition: defines.h:43
int badChanShow()
Definition: badchan.c:467
char * prompt
Definition: makemem.c:101
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
int deNewLineIt(char *buf)
double noint
Definition: header.h:211
int do_showBad()
Definition: badchan.c:714
Definition: parse.h:8
int correctBadChans()
Definition: badchan.c:688
#define MAX_CHANS
Definition: defines.h:38
struct ENVIRONMENT envrs[MAX_ENV]
Definition: global.h:62
double ccur
Definition: env.h:176
int setBadInterval()
Definition: badchan.c:194
FILE * fp
Definition: linuxpops.c:70
int addBadChan(int bad)
Definition: badchan.c:81
int printH_FlagHelp(char *cmd)
Definition: help.c:430
int writeBadChans()
Definition: badchan.c:750
void boxcar(float *input, float *output, int len, int order)
Definition: box.c:92
int ignoreBadChan()
Definition: badchan.c:244
int badchanshow
Definition: env.h:28
int doSpike()
Definition: badchan.c:621
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int _doCCur(char *msg)
Definition: linuxpops.c:2411
char * thirdArg
Definition: parse.h:182
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
int tellBadChan()
Definition: badchan.c:351
void st_warn()
void st_fail()
float findPreGoodChan(struct LP_IMAGE_INFO *image, int j)
Definition: badchan.c:500
int badchans[MAX_BAD_CHANS]
Definition: env.h:29
int removeBadChanFromArray(int bad)
Definition: badchan.c:113
int getNumericResponse()
struct GLOBAL_MEMORY * CGM
Definition: global.h:66
int dobadchan
Definition: env.h:47
void st_report()
int readBadChanFile()
Definition: badchan.c:411
int isBadChan(int bad)
Definition: badchan.c:377
int line_removeBadChans(struct LP_IMAGE_INFO *image, int print)
Definition: badchan.c:521
void subArgs(int wt, const char *fmt,...)
Definition: parse.c:430
int clearBadChan()
Definition: badchan.c:396
char * secondArg
Definition: parse.h:181
void st_print()
#define m(i)
int ntokens
Definition: parse.h:173
#define MAX_ENV
Definition: defines.h:35
int badchanepoch
Definition: env.h:27
int autoFind(int flag)
Definition: badchan.c:135
float findNextGoodChan(struct LP_IMAGE_INFO *image, int j)
Definition: badchan.c:480