Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
fboffset.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #include "extern.h"
60 
62 
63 
65 {
66  int seg;
67 
68  for(seg=0;seg<MAX_FILTER_SEGS;seg++)
69  {
70  st_print("Segment %3d: Offset = %12.6f\n", seg, fboffsets[seg]);
71 
72  if(((seg+1) * env->markers) >= h0->head.noint)
73  {
74  return(0);
75  }
76  }
77 
78  return(0);
79 }
80 
81 
82 int autoLevel()
83 {
84  int i, seg, m, k, j=0, noint;
85  double sum, gsum;
86  struct LP_IMAGE_INFO *image = h0;
87 
88  if(env->markers > 0)
89  {
90  noint = (int)image->head.noint;
91  k = 0;
92 
93  for(i=0;i<noint;i++)
94  {
95  if(isInBaseLineRegion(i))
96  {
97  gsum += image->yplot1[i];
98  j++;
99  }
100  }
101 
102  if(j > 10)
103  {
104  gsum /= (double)j;
105 
106  st_report("autoLevel(): Grand Avg of %d Baseline Channels: %f", j, gsum);
107 
108  for(seg=0;seg<MAX_FILTER_SEGS;seg++)
109  {
110  sum = 0.0;
111 
112  if(k >= noint) /* Stop then */
113  {
114  return(0);
115  }
116 
117  for(m=0;m<env->markers;m++)
118  {
119  sum += image->yplot1[k];
120  k++;
121  }
122 
123  sum /= (double)env->markers;
124 
125  st_report("autoLevel(): Segment %3d Avg = %10.6f, diff = %10.6f", seg, sum, sum-gsum);
126 
127  fboffsets[seg] = sum-gsum;
128  }
129  }
130  else
131  {
132  st_fail("autoLevel(): Little or no baseline channels defined");
133  return(1);
134  }
135  }
136  else
137  {
138  st_fail("autoLevel(): Can NOT perform Auto Level without variable: 'markers', being set");
139  return(1);
140  }
141 
142  return(0);
143 }
144 
145 
146 int autoMatch(seg)
147 int seg;
148 {
149  int noint, m;
150  int g1, b1, g2, b2; /* Indx into array */
151  double db1, db2, dg1, dg2; /* Array values */
152  double diff1, diff2, avg;
153  struct LP_IMAGE_INFO *image = h0;
154 
155  noint = image->head.noint;
156  m = env->markers;
157 
158  if(seg == 0) /* Can only use right edge for matching */
159  {
160  b2 = (seg+1) * m -1;
161  g2 = b2+1;
162 
163  db2 = image->yplot1[b2];
164  dg2 = image->yplot1[g2];
165 
166  diff2 = db2 - dg2;
167 
168  if(isSet(PF_V_FLAG))
169  {
170  st_report("Bad Channel Right Edge %3d", b2);
171  st_report("Good Channel Left Edge %3d", g2);
172  st_report("Bad Channel Value is %5.2f", db2);
173  st_report("Good Channel Value is %5.2f", dg2);
174  st_report("Diff2 = %f", diff2);
175  }
176 
177  st_report("Applying a %.3f offset to segment %d", diff2, seg);
178 
179  fboffsets[seg] = diff2;
180  }
181  else
182  if((seg * m + m) >= noint) /* Can only use left edge for matching */
183  {
184  b1 = seg * m;
185  g1 = b1-1;
186 
187  db1 = image->yplot1[b1];
188  dg1 = image->yplot1[g1];
189 
190  diff1 = db1 - dg1;
191 
192  if(isSet(PF_V_FLAG))
193  {
194  st_report("Bad Channel Left Edge %3d", b1);
195  st_report("Good Channel Right Edge %3d", g1);
196  st_report("Bad Channel Value is %5.2f", db1);
197  st_report("Good Channel Value is %5.2f", dg1);
198  st_report("Diff1 = %f", diff1);
199  }
200 
201  st_report("Applying a %.3f offset to segment %d", diff1, seg);
202 
203  fboffsets[seg] = diff1;
204  }
205  else
206  {
207  b1 = seg * m;
208  b2 = (seg+1) * m -1;
209 
210  g1 = b1-1;
211  g2 = b2+1;
212 
213 
214  db1 = image->yplot1[b1];
215  db2 = image->yplot1[b2];
216  dg1 = image->yplot1[g1];
217  dg2 = image->yplot1[g2];
218 
219 
220  diff1 = db1 - dg1;
221  diff2 = db2 - dg2;
222 
223 
224  avg = (diff1 + diff2) / 2.0;
225 
226  if(isSet(PF_V_FLAG))
227  {
228  st_report("Bad Channels are %3d - %3d", b1, b2);
229  st_report("Good Channels are %3d & %3d", g1, g2);
230  st_report("Bad Channels Values are %5.2f - %5.2f", db1, db2);
231  st_report("Good Channels Values are %5.2f & %5.2f", dg1, dg2);
232  st_report("Diff1 = %f", diff1);
233  st_report("Diff2 = %f", diff2);
234  st_report("Avg = %f", avg);
235  }
236 
237  st_report("Applying a %.3f offset to segment %d", avg, seg);
238 
239  fboffsets[seg] = avg;
240  }
241 
242 
243  return(0);
244 }
245 
246 
247 
249 {
250  int seg, seg2, old_mshow;
251  double from, to, diff;
252 
253  if(isSet(PF_C_FLAG))
254  {
255  clearFBOffets();
256 
257  return(0);
258  }
259 
260  if(!env->markers)
261  {
262  st_fail("'markers' must be set to a valid value");
263  return(1);
264  }
265 
266  if(isSet(PF_L_FLAG))
267  {
268  printFBOffsets();
269 
270  return(0);
271  }
272 
273  if(isSet(PF_A_FLAG))
274  {
275  /* Do Auto level segments */
276  autoLevel();
277  applyOffsets(h0);
278 
279  return(0);
280  }
281 
282  old_mshow = env->mshow;
283  env->mshow=1;
284  xx();
285 
286  if(strlen(args->secondArg))
287  {
288  if(!strncmp(args->secondArg, "match", strlen(args->secondArg)))
289  {
290  if(strlen(args->thirdArg))
291  {
292  seg = atoi(args->thirdArg);
293 
294  autoMatch(seg);
295 
296  applyOffsets(h0);
297 
298  env->mshow = old_mshow;
299 
300  return(0);
301  }
302  else
303  {
304  st_fail("Third arg missing for 'fboffset match seq'");
305  env->mshow = old_mshow;
306 
307  return(1);
308  }
309 
310  return(0);
311  }
312 
313  if(!strncmp(args->secondArg, "bmatch", strlen(args->secondArg)))
314  {
315  if(!valid_bl_averages)
316  {
317  st_fail("Must do a 'avg /b /s' before using 'bmatch' function");
318  return(1);
319  }
320 
321  if(strlen(args->thirdArg) && strlen(args->fourthArg))
322  {
323  seg = atoi(args->thirdArg);
324 
325  from = bl_averages[seg];
326 
327  seg2 = atof(args->fourthArg);
328 
329  to = bl_averages[seg2];
330 
331  diff = from - to;
332 
333  st_report("Applying a %.3f offset to segment %d", diff, seg);
334 
335  fboffsets[seg] = diff;
336 
337  applyOffsets(h0);
338 
339  env->mshow = old_mshow;
340 
341  return(0);
342  }
343  else
344  {
346  env->mshow = old_mshow;
347  }
348 
349  return(1);
350  }
351 
352  seg = atoi(args->secondArg);
353 
354  if(seg >= 0 && seg < MAX_FILTER_SEGS)
355  {
356  if(args->ntokens == 4) /* User supplied offsets */
357  {
358  if(strlen(args->thirdArg) && strlen(args->fourthArg))
359  {
360  from = atof(args->thirdArg);
361  to = atof(args->fourthArg);
362 
363  diff = from - to;
364 
365  st_report("Applying a %.3f offset to segment %d", diff, seg);
366 
367  fboffsets[seg] = diff;
368 
369  applyOffsets(h0);
370 
371  env->mshow = old_mshow;
372 
373  return(0);
374  }
375  }
376  else
377  if(args->ntokens == 2) /* User will set offsets with mouse */
378  {
379  _doTCur("Click the Left Mouse Button on Current Value");
380  from = env->tcur;
381 
382  _doTCur("Click the Left Mouse Button on Desired Value");
383  to = env->tcur;
384 
385  diff = from - to;
386 
387  st_report("Applying a %.3f offset to segment %d", diff, seg);
388 
389  fboffsets[seg] = diff;
390 
391  applyOffsets(h0);
392 
393  env->mshow = old_mshow;
394 
395  return(0);
396  }
397  }
398  }
399  else
400  {
402  env->mshow = old_mshow;
403 
404  return(1);
405  }
406 
407  env->mshow = old_mshow;
408 
409  return(0);
410 }
411 
412 
413 int applyOffsets(image)
414 struct LP_IMAGE_INFO *image;
415 {
416  int m, seg, k;
417 
418  st_report("Removing FB offset in %s", image->name);
419 
420  if(env->markers > 0)
421  {
422  k = 0;
423 
424  for(seg=0;seg<MAX_FILTER_SEGS;seg++)
425  {
426  for(m=0;m<env->markers;m++)
427  {
428  image->yplot1[k] -= fboffsets[seg];
429  k++;
430 
431  if(k >= image->head.noint)
432  {
433 // fboffsets[seg] = 0.0; /* Don't apply again */
434 
435  return(0);
436  }
437  }
438 
439 // fboffsets[seg] = 0.0; /* Don't apply again */
440  }
441  }
442  else
443  {
444  st_fail("Markers must be non-zero if using filter offsets");
445  }
446 
447  return(1);
448 }
449 
450 
452 {
453  int i;
454  double total = 0.0;
455 
456  for(i=0;i<MAX_FILTER_SEGS; i++)
457  {
458  total += fabs(fboffsets[i]);
459  }
460 
461  if(total > 0.0) /* Was there any offset before ?? */
462  {
463  st_report("Clear out all FB offsets");
464  }
465 
466  bzero((char *)&fboffsets, sizeof(fboffsets));
467 
468  return(0);
469 }
470 
471 
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int xx()
Definition: linuxpops.c:605
double tcur
Definition: env.h:271
int autoLevel()
Definition: fboffset.c:82
struct PARSE_STACK * args
Definition: parse.h:196
double noint
Definition: header.h:211
Definition: parse.h:8
int printH_FlagHelp(char *cmd)
Definition: help.c:430
int applyOffsets(struct LP_IMAGE_INFO *image)
Definition: fboffset.c:413
int markers
Definition: env.h:72
struct ENVIRONMENT * env
Definition: linuxpops.c:71
double sum
Definition: beam.h:5
char * thirdArg
Definition: parse.h:182
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
#define MAX_FILTER_SEGS
Definition: defines.h:119
void st_fail()
double bl_averages[MAX_BASELINES]
Definition: baseline.c:64
int isInBaseLineRegion(int chan)
Definition: baseline.c:336
int printFBOffsets()
Definition: fboffset.c:64
void st_report()
int clearFBOffets()
Definition: fboffset.c:451
int valid_bl_averages
Definition: baseline.c:63
char * fourthArg
Definition: parse.h:183
double avg
Definition: beam.h:5
char * secondArg
Definition: parse.h:181
int _doTCur(char *msg)
Definition: linuxpops.c:2580
void st_print()
#define m(i)
int ntokens
Definition: parse.h:173
char * firstArg
Definition: parse.h:180
float fboffsets[MAX_FILTER_SEGS]
Definition: fboffset.c:61
int mshow
Definition: env.h:76
int autoMatch(int seg)
Definition: fboffset.c:146
int doFBOffset()
Definition: fboffset.c:248