Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sfocus.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 double last_focus_fit = 0.0;
62 
63 /* Process a multi spectral line focus; assumes scan is already loaded */
64 int specFocusOne(noint, nfoc, feed)
65 int noint, nfoc, feed;
66 {
67  int i, j, n, ret, delta, nchan, expectedPos, maxi, save_ps;
68  int start, end, npts, bchans;
69  char cmdBuf[256];
70  double focusp[MAX_SPEC_FOCUS], peaks[MAX_SPEC_FOCUS];
71  double maxG, aa1[4], sig[MAX_SPEC_FOCUS];
72 
73  /* These routines assume the line is in the center; need to allow otherwise */
74  if(env->sf_fitcenter != 0)
75  {
76  expectedPos = env->sf_fitcenter;
77  st_report("Using User supplied Center Channel: %d", expectedPos);
78  }
79  else
80  {
81  expectedPos = noint / 2; /* halfway through bandpass??? */
82  }
83 
84  if(env->sf_fitmhz == 0.0)
85  {
86  env->sf_fitmhz = 20.0;
87  st_report("Using Line Width: %f", env->sf_fitmhz);
88  }
89  else
90  {
91  st_report("Using User supplied Line Width: %f", env->sf_fitmhz);
92  }
93 
94  if(env->sf_bchans)
95  {
96  bchans = env->sf_bchans;
97  }
98  else
99  {
100  bchans = 40;
101  }
102 
103  delta = (int)((env->sf_fitmhz / fabs(h0->head.freqres) ) / 4.0);
104 
105  st_report("specFocusOne(): noint = %d, nfoc = %d, expectedPos = %d, delta = %d",
106  noint, nfoc, expectedPos, delta);
107 
108  /* Automatically set baselines to 40 channels on the ends of each expected spectra */
109  /* Zero out the current baselines */
110  bzero((char *)&env->baselines[0], sizeof(env->baselines));
111 
112  env->baselines[0][0] = 1;
113  env->baselines[0][1] = bchans;
114  for(i=1;i<nfoc;i++)
115  {
116  env->baselines[i][0] = (i * noint) - bchans;
117  env->baselines[i][1] = (i * noint) + bchans;
118  }
119 
120  env->baselines[nfoc][0] = h0->head.noint - bchans;
121  env->baselines[nfoc][1] = h0->head.noint - 1;
122 
123  env->bmark = 1;
124 
125  tellBaselines();
126 
127  strcpy(cmdBuf, "show"); /* Replot with baselines visible */
128  parseCmd(cmdBuf);
129  xdelay(1000000); /* Give plot time to show */
130 
131  env->markers = noint; /* Show breaks between spectra */
132  env->mshow = 1; /* Markers show */
133  env->bmark = 1; /* Show Baseline regions */
134  env->zline = 1; /* Show Zero Line */
135  env->bsmoothing = 1; /* poly baseline shape */
136 
137  if(!env->nfit)
138  {
139  env->nfit = 1; /* linear baseline fitting */
140  }
141 
142  strcpy(cmdBuf, "gracefit");
143  parseCmd(cmdBuf);
144  xdelay(1000000); /* Give plot time to show */
145 
146  strcpy(cmdBuf, "rmbase");
147  parseCmd(cmdBuf);
148  xdelay(1000000); /* Give plot time to show */
149 
150  strcpy(cmdBuf, "show"); /* Replot with baselines visible */
151  parseCmd(cmdBuf);
152  xdelay(1000000); /* Give plot time to show */
153 
154  if(isSet(PF_P_FLAG))
155  {
156  sprintf(cmdBuf, "make /c /g /d sfocus_data.%.2f.png", h0->head.scan);
157  parseCmd(cmdBuf);
158  }
159 
160  maxG = 0.0;
161  maxi = 0;
162 
163  npts = 0;
164 
165  if(env->sf_start)
166  {
167  start = env->sf_start;
168  }
169  else
170  {
171  start = 0;
172  }
173 
174  if(env->sf_end)
175  {
176  end = env->sf_end;
177  }
178  else
179  {
180  end = nfoc;
181  }
182 
183  for(i=start;i<end;i++)
184  {
185  nchan = expectedPos + (noint * i);
186 
187  peaks[i] = 0.0;
188  n = 0;
189  for(j=(nchan-delta);j<(nchan+delta);j++) /* Average up the delta*2 channels about the center */
190  {
191  peaks[i] += h0->yplot1[j];
192  n++;
193  }
194 
195  focusp[i] = h0->head.focusr - (floor((double)nfoc / 2.0) * h0->head.wl) + (double)i * h0->head.wl;
196  peaks[i] /= (double)n;
197 
198  if(peaks[i] > maxG)
199  {
200  maxG = peaks[i];
201  maxi = i;
202  }
203 
204  st_report("[%2d]: %f %f", i, focusp[i], peaks[i]);
205 
206  sig[i] = 1.0;
207 
208  npts++;
209  }
210 
211  /* Steal the continuum focus structure so xgraphics will plot the results */
212  clearFillContStruct(&h0->head, npts, "Focus", "Ta* (K)");
213 
214  st_report("Fitting a %d Point Gauss to Peak: %f, @ %f, HP = %f", npts, maxG, focusp[maxi], h0->head.wl * 6.0);
215  aa1[0] = 0.0;
216  aa1[1] = maxG;
217  aa1[2] = h0->head.focusr; /* Center Focus */
218  aa1[3] = h0->head.wl * 6.0;
219 
220  ret = gauss_fit(&focusp[start], &peaks[start], &sig[start], npts-1, aa1, isSet(PF_V_FLAG)); /* Fit a gaussian curve to peaks */
221 
222  if(ret != 1)
223  {
224  st_report("gauss_fit() returned: %d", ret);
225  }
226 
227  st_report("Gaussian Peak: %7.2f K", aa1[1]);
228  st_report("Gaussian Center: %7.2f", aa1[2]);
229  st_report("Gaussian FWHM: %7.2f mm", aa1[3]);
230 
231  last_focus_fit = aa1[2];
232 
233  for(i=0;i<npts;i++)
234  {
235  cont_plain_send.xdata[i] = (float)focusp[start+i];
236  cont_plain_send.ydata[i] = (float)peaks[start+i];
237  }
238 
239  if(feed == 0)
240  {
241  env->plotwin = 15;
242  }
243  else
244  {
245  env->plotwin = 16;
246  }
247 
249 
250  save_ps = env->plotstyle;
252 
253  env->zline = 0;
254 
255  if(xgraphic)
256  {
258  xdelay(1000000); /* Give plot time to show */
259  }
260 
261  plotGauss(&focusp[start], npts, aa1, &h0->head, 1);
262  xdelay(1000000); /* Give plot time to show */
263 
264  env->plotstyle = save_ps;
265 
266  if(isSet(PF_P_FLAG))
267  {
268  sprintf(cmdBuf, "make /c /g /d sfocus_gauss.%.2f.png", h0->head.scan);
269  parseCmd(cmdBuf);
270  }
271 
272  return(0);
273 }
274 
275 /* SFocus analysis:
276 
277  The Spec Focus comes in as an array of bandpasses, each at a different
278  focus position.
279 
280  openpar[6] contains the number of focus positions. Maximum of 15;
281 
282  Procedure:
283 
284  1) Fetch the scan;
285  2) Copy it to h8 to save it.
286  3) Move the n'th / 2 array into h0->yplot[];
287  4) Show it and fit a gauss
288  5) Wait for xgraphics to reply on yaxis.
289  6) Fix the yaxis to min/max
290  7) for(i=0;i<n;i++)
291  {
292  bcopy(h8->yplot1[noint*i], h0->yplot1[0], noint * sizeof(float));
293 
294  show();
295 
296  peak();
297 
298  gauss(); /a
299 
300  save();
301  }
302 
303 */
304 
305 int doSfocus()
306 {
307  int i, ret, noint, nfoc;
308  int feed;
309  char monBuf[256], cmdBuf[256];
310  double scan;
311 
312  if(args->ntokens < 2)
313  {
315  }
316 
317  if(strlen(args->secondArg))
318  {
319  scan = atof(args->secondArg);
320  }
321  else
322  {
323  st_fail("Missing Scan Number");
325 
326  return(1);
327  }
328 
329  saveEnv();
330 
331  env->focus_fit_1 = env->focus_fit_2 = 0.0;
332 
333  for(i=0;i<2;i++)
334  {
335  ret = read_scan(scan, env->filename, h0->yplot1, &h0->head, 0);
336 
337  if(ret <= 0)
338  {
339  st_fail("doSfocus(): Scan %.2f, not found", scan);
340 
341  restoreEnv();
342 
343  return(1);
344  }
345 
346  noint = (int)h0->head.noint;
347  nfoc = (int)h0->head.openpar[6];
348 
349  if(i == 0)
350  {
351  env->plotwin = 0;
352  }
353  else
354  {
355  env->plotwin = 1;
356  }
357 
358  sprintf(cmdBuf, "show /f");
359  parseCmd(cmdBuf);
360 
361  xdelay(1000000); /* Give plot time to show */
362 
363  specFocusOne(noint, nfoc, i);
364 
365  feed = getFeed(h0->head.scan);
366 
367  if((feed%2)) /* odd channel */
368  {
370  }
371  else /* even channel */
372  {
374  }
375 
376  if(isSet(PF_2_FLAG))
377  {
378  scan += 0.02;
379  }
380  else
381  {
382  scan += 0.01;
383  }
384  }
385 
386  sprintf(monBuf, "DISPLAY FIT_AX01 %f FIT_AX02 %f FIT_AX0AVG %f", env->focus_fit_1, env->focus_fit_2, (env->focus_fit_1 + env->focus_fit_2) / 2.0);
387  st_report("Results: %s", monBuf);
388 
389  if(isSet(PF_T_FLAG))
390  {
391  if(monitorSock)
392  {
393  st_report("To MONITOR: %s", monBuf);
394 
395  sock_send(monitorSock, monBuf);
396  }
397  else
398  {
399  st_fail("monitor sockect not defined");
400  }
401  }
402 
403  restoreEnv();
404 
405  return(0);
406 }
407 
struct SOCK * xgraphic
Definition: linuxpops.c:75
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
int getFeed()
double scan
Definition: header.h:35
int clearFillContStruct(struct HEADER *h, int dlen, char *xl, char *yl)
Definition: cont.c:1726
int bsmoothing
Definition: env.h:36
#define MAX_SPEC_FOCUS
Definition: defines.h:44
struct PARSE_STACK * args
Definition: parse.h:196
double noint
Definition: header.h:211
int scan
int plotGauss(double *x, int len, double *a, struct HEADER *h, int mode)
Definition: gauss.c:962
int xdelay(int d)
Definition: linuxpops.c:3559
int read_scan()
double sf_fitmhz
Definition: env.h:193
int sf_start
Definition: env.h:158
int printH_FlagHelp(char *cmd)
Definition: help.c:430
int sf_end
Definition: env.h:159
int gauss_fit()
double last_focus_fit
Definition: sfocus.c:61
float xdata[MAX_CONT_SAMPLES]
Definition: accum.h:117
float ydata[MAX_CONT_SAMPLES]
Definition: accum.h:118
double openpar[10]
Definition: header.h:155
int markers
Definition: env.h:72
int doSfocus(void)
Definition: sfocus.c:305
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int nfit
Definition: env.h:81
int isSet(int flag)
Definition: parse.c:646
#define PLOT_STYLE_LINEPTS
Definition: defines.h:103
struct HEADER head
Definition: image.h:104
int sock_send(struct SOCK *s, char *message)
struct SOCK * monitorSock
Definition: linuxpops.c:75
int saveEnv()
Definition: env.c:709
int sf_fitcenter
Definition: env.h:157
void st_fail()
int restoreEnv()
Definition: env.c:722
int sendCont(int mode)
Definition: cont.c:1827
int feed
Definition: stats.c:66
int tellBaselines()
Definition: baseline.c:358
double focus_fit_2
Definition: env.h:196
double focus_fit_1
Definition: env.h:195
struct CONTINUUM_PLAIN cont_plain_send
Definition: cont.c:63
double focusr
Definition: header.h:54
int plotwin
Definition: env.h:129
int yscale
Definition: env.h:149
void st_report()
int specFocusOne(int noint, int nfoc, int feed)
Definition: sfocus.c:64
double wl
Definition: header.h:144
int plotstyle
Definition: env.h:127
#define SEND_PLAIN
Definition: defines.h:134
int sf_bchans
Definition: env.h:160
#define Y_AUTO_SCALE
Definition: defines.h:106
char * secondArg
Definition: parse.h:181
int baselines[MAX_BASELINES][2]
Definition: env.h:30
int ntokens
Definition: parse.h:173
int zline
Definition: env.h:151
char * firstArg
Definition: parse.h:180
int parseCmd(char *buf)
Definition: parse.c:2090
double freqres
Definition: header.h:199
int mshow
Definition: env.h:76
int bmark
Definition: env.h:34
char filename[256]
Definition: env.h:9