Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
histogram.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 <gsl/gsl_histogram.h>
60 
61 #include "extern.h"
62 
64 
65 int dummie_main (int argc, char **argv)
66 {
67  double a, b;
68  size_t n;
69 
70  if(argc != 4)
71  {
72  printf ("Usage: gsl-histogram xmin xmax n\n"
73  "Computes a histogram of the data "
74  "on stdin using n bins from xmin "
75  "to xmax\n");
76 
77  exit (0);
78  }
79 
80  a = atof(argv[1]);
81  b = atof(argv[2]);
82  n = atoi(argv[3]);
83 
84  {
85  double x;
86 
87  gsl_histogram * h = gsl_histogram_alloc (n);
88  gsl_histogram_set_ranges_uniform (h, a, b);
89 
90  while (fscanf (stdin, "%lg", &x) == 1)
91  {
92  gsl_histogram_increment (h, x);
93  }
94 
95  gsl_histogram_fprintf (stdout, h, "%g", "%g");
96  gsl_histogram_free (h);
97  }
98 
99  exit (0);
100 }
101 
102 
104 struct HISTO_INFO *h;
105 {
106  int i, k, n;
107  float f[MAX_HISTO_LENGTH], rms;
108  double total=0.0;
109 
110  n = h->num;
111 
112  for(i=0;i<n;i++)
113  {
114  f[i] = h->ydata[i] - h->gdata[i];
115  total += h->gdata[i];
116  }
117 
118  if(isSet(PF_V_FLAG))
119  {
120  k = 0;
121  for(i=0;i<n;i++,k++)
122  {
123  if(k > 9)
124  {
125  st_print("\n");
126  k = 0;
127  }
128 
129  st_print("%9.4f ", f[i]);
130  }
131 
132  st_print("\n");
133  }
134 
135  computeRms(f, n, &rms);
136 
137  st_report("Histogram rms: %11.6f", rms);
138  st_report("Histogram moment: %11.6f", total);
139  st_report("Histogram Percent: %11.6f", rms / total * 100.0);
140 
141  return(0);
142 }
143 
144 
146 {
147  int i, n;
148  double min, max;
149  struct HISTO_INFO *h;
150  gsl_histogram *histo;
151 
152  if(args->ntokens < 2)
153  {
154  printH_FlagHelp("histogram");
155 
156  return(1);
157  }
158 
159  n = atoi(args->secondArg);
160 
161  if( n <= 10 || n >= MAX_HISTO_LENGTH)
162  {
163  printH_FlagHelp("histogram");
164 
165  return(1);
166  }
167 
168  min = h0->datamin ;
169  max = h0->datamax ;
170 
171  if(min > 0.0 || max < 0.0)
172  {
173  st_fail("Please remove a DC offset first");
174  return(0);
175  }
176 
177  histo = gsl_histogram_alloc(n);
178  gsl_histogram_set_ranges_uniform(histo, min, max);
179 
180  for(i=0;i<h0->head.noint;i++)
181  {
182  gsl_histogram_increment(histo, (double)h0->yplot1[i]);
183  }
184 
185  if(isSet(PF_W_FLAG))
186  {
187  char fname[256];
188  FILE *fp;
189 
190  sprintf(fname, "histo.dat");
191 
192  if((fp = fopen(fname, "w") ) <= (FILE *)0)
193  {
194  st_fail("Unable to open %s", fname);
195 
196  return(1);
197  }
198 
199  gsl_histogram_fprintf(fp, histo, "%g", "%g");
200 
201  fclose(fp);
202 
203  st_report("Histogram Written to %s", fname);
204  }
205 
206  h = &last_histo;
207 
208  bzero((char *)h, sizeof(*h));
209 
210  strcpy(h->magic, "HISTOGRAM");
211 
212  bcopy((char *)&h0->head, (char *)&h->head, sizeof(struct HEADER));
213  h->plotwin = env->plotwin;
214 
215  for(i=0;i<n;i++)
216  {
217  h->xdata[i] = histo->range[i];
218  h->ydata[i] = histo->bin[i];
219  }
220 
221  h->num = n;
222 
223  st_report("Histogram Max: %11.6f", gsl_histogram_max_val(histo));
224  st_report("Histogram Min: %11.6f", gsl_histogram_min_val(histo));
225  st_report("Histogram Mean: %11.6f", gsl_histogram_mean(histo));
226  st_report("Histogram Sigma: %11.6f", gsl_histogram_sigma(histo));
227  st_report("Histogram Sum: %11.6f", gsl_histogram_sum(histo));
228 
229 
230  if(isSet(PF_G_FLAG))
231  {
232  int pgauss, h1gauss, h2gauss, ret;
233  double xd[MAX_HISTO_LENGTH], yd[MAX_HISTO_LENGTH], sig[MAX_HISTO_LENGTH];
234  double a[4], dyda[4], y;
235 
236  pgauss = gsl_histogram_max_bin(histo);
237 
238  h1gauss = pgauss - (n/8);
239  h2gauss = pgauss + (n/8);
240 
241  for(i=0;i<n;i++)
242  {
243  xd[i] = h->xdata[i];
244  yd[i] = h->ydata[i];
245  sig[i] = 1.0;
246  }
247 
248  a[0] = 0.0;
249  a[1] = yd[pgauss];
250  a[2] = xd[pgauss];
251  a[3] = fabs(xd[h1gauss]-xd[h2gauss]);
252 
253  st_report("Histogram Gauss Input: %11.6f %11.6f %11.6f", a[1], a[2], a[3]);
254 
255  /* computer gaussian using Temp / Channel */
256  ret = gauss_fit(&xd[0], &yd[0], &sig[0], n-1, &a[0], isSet(PF_V_FLAG));
257 
258  if(ret)
259  {
260  st_report("Histogram Gauss Results: %11.6f %11.6f %11.6f", a[1], a[2], a[3]);
261 
262  for(i=0;i<n;i++)
263  {
264  fgauss(xd[i], &a[0], &y, &dyda[0]);
265  h->gdata[i] = y;
266  }
267 
268  h->gset = 1;
269 
270  computeHistoRMS(h);
271  }
272  }
273 
274  if(xgraphic)
275  {
276  sock_write(xgraphic, (char *)h, sizeof(*h));
277  }
278 
280 
281  st_report("Sent Histogram to Xgraphics");
282 
283  gsl_histogram_free(histo);
284 
285  return(0);
286 }
struct SOCK * xgraphic
Definition: linuxpops.c:75
float yplot1[MAX_CHANS]
Definition: image.h:110
struct LP_IMAGE_INFO * h0
Definition: image.c:74
float ydata[MAX_HISTO_LENGTH]
Definition: histogram.h:22
int sock_write(struct SOCK *s, char *message, int l)
#define LAST_PLOT_SPEC_HISTO
Definition: defines.h:158
struct PARSE_STACK * args
Definition: parse.h:196
char magic[32]
Definition: histogram.h:8
double noint
Definition: header.h:211
FILE * fp
Definition: linuxpops.c:70
#define max(a, b)
Definition: cactus.h:115
Definition: header.h:13
int printH_FlagHelp(char *cmd)
Definition: help.c:430
void fgauss()
#define min(a, b)
Definition: cactus.h:114
int gauss_fit()
float datamin
Definition: image.h:95
int computeRms(float *y, int n, float *rms)
Definition: line.c:82
struct HISTO_INFO last_histo
Definition: histogram.c:63
float gdata[MAX_HISTO_LENGTH]
Definition: histogram.h:23
struct ENVIRONMENT * env
Definition: linuxpops.c:71
#define MAX_HISTO_LENGTH
Definition: histogram.h:4
int dummie_main(int argc, char **argv)
Definition: histogram.c:65
int plotwin
Definition: histogram.h:9
int isSet(int flag)
Definition: parse.c:646
struct HEADER head
Definition: image.h:104
int doHistogram()
Definition: histogram.c:145
void st_fail()
float datamax
Definition: image.h:98
float xdata[MAX_HISTO_LENGTH]
Definition: histogram.h:21
int plotwin
Definition: env.h:129
void st_report()
int last_plot
Definition: env.c:93
double rms
Definition: beam.h:5
struct HEADER head
Definition: histogram.h:19
char * secondArg
Definition: parse.h:181
void st_print()
int ntokens
Definition: parse.h:173
int computeHistoRMS(struct HISTO_INFO *h)
Definition: histogram.c:103
int gset
Definition: histogram.h:10