HOPS
HOPS class reference
cohfit.h
Go to the documentation of this file.
1 /*
2  * Header for cohfit
3  *
4  * Extensively hacked up April-May 2026 by GBC
5  */
6 #define FALSE 0
7 #define TRUE 1
8 
9 #include <stdio.h>
10 
11 /* optind and optarg in unistd.h are used in main() and parse_cmdline() */
12 #include <unistd.h>
13 #include <gsl/gsl_errno.h>
14 #include "adata.h"
15 #include "msg.h"
16 
17 #ifndef HAVEGSL2P8
18 #define HAVEGSL2P8 0
19 #endif /* HAVEGSL2P8 */
20 #ifndef HAVEGSL2P7
21 #define HAVEGSL2P7 0
22 #endif /* HAVEGSL2P7 */
23 
24 /* note that in mk4_data.h #define MAXSTATIONS 32 */
25 #define MAX_BNO 120 /* 20 stations */
26 #define MAX_NSEGLEN 100 /* Fringex max -iarg MAXNSECS */
27 #define MAX_TXT 80 /* pgplot labels */
28 #define MAX_ITERS 100 /* max iterations */
29 #define NFITOPT 6 /* number FITOPT bits */
30 #define MAX_COTIME 392.0 /* historical limit */
31 #define INIT_AVSP 500 /* avg data storage, initially */
32 #define INCR_AVSP 100 /* increment for more */
33 
34 #ifdef requirefitolerances
35 /* some common constants needed for gsl_multifit_nlinear_driver() */
36 const double xtol = 1e-6;
37 const double gtol = 1e-6;
38 const double ftol = 0.0;
39 const size_t maxits = MAX_ITERS;
40 #endif /* requirefitolerances */
41 
42 /* this is used for sorting the incoming data into proper order */
43 typedef struct avdata
44  {
45  int order;
46  int lastorder;
47  int keyval;
50 typedef struct avgdatastore
51  {
52  int space; /* space (re)allocated for avg_data */
53  avg_data *data; /* pointer to avg_data so allocated */
55 
56 /* this holds a scan-boundary group data through its processing */
57 typedef struct cosummary
58  {
59  /* inputs */
60  fringesum *datum; /* Data structure ptr for this scan */
61  float orig_srch; /* copy of orig datum->src_cotime */
62  float ampl[MAX_NSEGLEN]; /* segment avg amplitudes */
63  float snr[MAX_NSEGLEN]; /* segment avg snr */
64  float seglen[MAX_NSEGLEN]; /* segment lengths (in seconds) */
65  int navg[MAX_NSEGLEN]; /* num avg'd segments per segment */
66  /* outputs or options [-? arg] */
67  float fitsnr[MAX_NSEGLEN]; /* 3pt snr fit */
68  float fit2p8[MAX_NSEGLEN]; /* cubic spline (GSL2.8) snr fit */
69  float fit2p7[MAX_NSEGLEN]; /* cubic spline (GSL2.7) snr fit */
70  float fitaps[MAX_NSEGLEN]; /* ps amp fit result */
71  float fitapo[MAX_NSEGLEN]; /* po amp fit result */
72  float fitaso[MAX_NSEGLEN]; /* so amp fit result */
73  float plateau; /* estimate of amplitude */
74  float breakpoint; /* start of loss region */
75  float slope; /* decay rate in time */
76  float cohereloss; /* [-s] 5% in the original */
77  double maxcotime; /* [-c] coh. limit option */
78  int nsegtime; /* how many segments == npt */
79  int fitmask; /* [-f] allowed fits, see below */
80  int bestamp; /* best amp fit, see below */
81  int bestsnr; /* same for snr */
82  int didfits; /* fit*[] &c. see below */
83  int iteratio[NFITOPT]; /* iterations on fits */
84  float redchisq[NFITOPT]; /* chisq/dof each fit */
85  int snr_peak[3]; /* seg indices for fit_snr() */
86  int cbs_p2p8[3]; /* seg indices for fit_cbs() */
87  int cbs_p2p7[3]; /* seg indices for fit_cbs() */
88  double pspar[3], pserr[3]; /* ps fit parameters, errors */
89  double popar[1], poerr[1]; /* po fit parameters, errors */
90  double sopar[2], soerr[2]; /* so fit parameters, errors */
91  double ampl_cotime; /* ampl noloss coherence time */
92  double snr_cotime; /* snr noloss coherence time */
93  /* external reporting */
94  char *exampat; /* [-e] filename base */
95  int examlen; /* [-e] length of the base */
96  int labels; /* [-l] show/skip labels */
98 
99 /* per-base-pol and accumulated knowledge for [-e] option */
100 typedef struct edatum {
101  char frlabel[MAX_TXT]; /* date/type2.stamp pol */
102  double lenmin, lenmax; /* extrema of segment lengths */
103  double ampmax, ampmin; /* extrema of amplitude */
104  double snrmax, snrmin; /* extrema of SNR */
107  double redchisq[NFITOPT];
108  double pspar[3], pserr[3];
109  double popar[1], poerr[1];
110  double sopar[2], soerr[2];
112  char *examfile; /* copy of output file */
113  int bno; /* ordinal of the file */
115 
116 /* this is the top-level holder of command-line options and it also holds
117  * the group of output data in the range [pbno..nbno) for the -e proceeing.
118  * all char* items are allocated */
119 typedef struct examdata {
120  /* processing options set via [-g] option, static for entire execution */
121  int gnuplot, montage; /* defaults 1,1 */
122  int customlimits; /* per plot, default 0 */
123  int density; /* for montage, default 150 */
124  int rnc[2]; /* row,col from [-g] option */
125  int labels; /* [-l] show/skip labels */
126  int fitmask; /* [-f] allowed fits, see below */
127  float closs; /* [-s] coherence loss */
128  double mxcotime; /* [-c] max coh time */
129  char *pdfile; /* [-d] pdf file if /pdf given */
130  char *devp; /* [-d] device name */
131  char *epat; /* [-e] file base without pattern */
132  int elen; /* and its length is computed */
133  char *edit; /* [-g] plot edit file */
134  int gslegacy; /* use legacy gsl_multifit_nlin.h */
135  int gpdashtype; /* gnuplot supports dashtype */
136  int gpboldface; /* gnuplot supports {:/Bold} */
137  /* scan-boundary group data, cleared each scan-boundary */
138  int nbno; /* number of base-pols */
139  int pbno; /* previous total of such */
140  edatum edata[MAX_BNO]; /* per-file allocations */
142 
143 /* values use in cosumary.fitmask and cosumary.bestamp above
144  * fitmask is what fits should be tried (i.e. code exercised)
145  * didfits is what fits were actually done (i.e output populated)
146  * bestamp is what fits was selected as best (i.e. final answer)
147  * bestsnr is what fits was selected as best (i.e. final answer)
148  * these values must be consistent with [-f] option. Various data
149  * about the fits is also captured: model paramters, iterations and
150  * reduced chisq.
151  */
152 #define FITOPT_NOFITS 0x00 /* do no fits */
153 #define FITOPT_AMP_PS 0x01 /* do plateau-slope fit */
154 #define FITOPT_NDX_PS 0 /* < NFITOPT */
155 #define FITOPT_AMP_PO 0x02 /* do plateau-only fit */
156 #define FITOPT_NDX_PO 1 /* < NFITOPT */
157 #define FITOPT_AMP_SO 0x04 /* do slope-only fit */
158 #define FITOPT_NDX_SO 2 /* < NFITOPT */
159 #define FITOPT_SNR_3PT 0x10 /* do 3-point SNR fit at max */
160 #define FITOPT_NDX_3PT 3 /* < NFITOPT */
161 #define FITOPT_SNR_2P8 0x20 /* do GSL 2.8 cubic spline fit */
162 #define FITOPT_NDX_2P8 4 /* < NFITOPT */
163 #define FITOPT_SNR_2P7 0x40 /* do GSL 2.7 cubic spline fit */
164 #define FITOPT_NDX_2P7 5 /* < NFITOPT */
165 #define FITOPT_AMP_ALL (FITOPT_AMP_PS|FITOPT_AMP_PO|FITOPT_AMP_SO)
166 #define FITOPT_SNR_ALL (FITOPT_SNR_3PT|FITOPT_SNR_2P8|FITOPT_SNR_2P7)
167 #define FITOPT_ALLFITS (FITOPT_AMP_ALL|FITOPT_SNR_ALL)
168 
169 /* Fit conclusions that propagate to cofit output (fit_codata.c):
170  * never fit amplitude => codatum->datum->noloss_cotime = -2.0;
171  * fit_ampl() != 0 => codatum->datum->noloss_cotime = -1.0;
172  * or noloss_cotime < 1.0 => codatum->datum->noloss_cotime = 1.0;
173  * never fit the SNR => codatum->datum->srch_cotime = -2.0;
174  * normalize_snr() != 0 => codatum->datum->srch_cotime = -1.0;
175  * fit_snr <= 0 => codatum->datum->srch_cotime = -1.0;
176  * Negatives result in removal from output, otherwise a line is generated.
177  * Note that if the cotime is the first or last segment time it means that
178  * we don't really have a robust result.
179  */
180 
181 /* routines that move data around */
182 extern int normalize_snr(cosumary *, int );
183 extern void sorter(avg_data *, int);
184 extern int parse_cmdline(int, char **, FILE **, examdata *);
185 extern int read_data(avgspace *, char *, int *, int *);
186 
187 extern int sort_data(avg_data *, int);
188 extern void plot_codata(cosumary *);
189 extern int write_codata(cosumary *, FILE *);
190 extern void clear_codata(cosumary *);
191 
192 /* routines associated with [-e] and [-g] handling */
193 extern void exam_file(cosumary *, int, examdata *);
194 extern void exam_gnuplot(examdata *);
195 extern int exam_edit(char *, examdata *);
196 extern int get_gslegacy_default(void);
197 extern void set_gnuplot_opts(examdata *);
198 
199 /* routines that are support fitting */
200 extern char *as_fit_nm_ndx(int, int*);
201 extern char *as_fit_ndx_nm(int);
202 extern int fit_ampl(cosumary *, int, int);
203 extern int fit_msnr(cosumary *, int);
204 extern void fit_codata(cosumary *, examdata *);
205 
206 /* for non-abort GSL error handling */
207 extern gsl_error_handler_t err_handler;
208 extern char *get_err_handler_report();
209 extern void clear_err_handler_report(void);
210 
211 /*
212  * eof vim:nospell
213  */
Definition: adata.h:70
double plateau
Definition: cohfit.h:105
struct edatum edatum
struct avgdatastore avgspace
int fitmask
Definition: cohfit.h:79
int order
Definition: cohfit.h:45
double snr_cotime
Definition: cohfit.h:92
#define MAX_ITERS
Definition: cohfit.h:28
float fitaso[MAX_NSEGLEN]
Definition: cohfit.h:72
double redchisq[NFITOPT]
Definition: cohfit.h:107
int nbno
Definition: cohfit.h:138
char * pdfile
Definition: cohfit.h:129
double sopar[2]
Definition: cohfit.h:110
float slope
Definition: cohfit.h:75
double pserr[3]
Definition: cohfit.h:108
edatum edata[MAX_BNO]
Definition: cohfit.h:140
int gslegacy
Definition: cohfit.h:134
double lenmin
Definition: cohfit.h:102
double soerr[2]
Definition: cohfit.h:110
double snrmax
Definition: cohfit.h:104
int gpdashtype
Definition: cohfit.h:135
double breakpoint
Definition: cohfit.h:105
double mxcotime
Definition: cohfit.h:128
int examlen
Definition: cohfit.h:95
struct avdata avg_data
double snr_cotime
Definition: cohfit.h:106
double soerr[2]
Definition: cohfit.h:90
double popar[1]
Definition: cohfit.h:109
char * devp
Definition: cohfit.h:130
char * as_fit_ndx_nm(int)
Definition: as_fit_nm_ndx.c:43
int elen
Definition: cohfit.h:132
int bestampndx
Definition: cohfit.h:111
int write_codata(cosumary *, FILE *)
Definition: write_codata.c:23
int nsegtime
Definition: cohfit.h:78
int gpboldface
Definition: cohfit.h:136
int snr_peak[3]
Definition: cohfit.h:85
int space
Definition: cohfit.h:52
fringesum fdata
Definition: cohfit.h:48
int labels
Definition: cohfit.h:125
void plot_codata(cosumary *)
int rnc[2]
Definition: cohfit.h:124
int montage
Definition: cohfit.h:121
char frlabel[MAX_TXT]
Definition: cohfit.h:101
double ampmin
Definition: cohfit.h:103
double sopar[2]
Definition: cohfit.h:90
float orig_srch
Definition: cohfit.h:61
#define MAX_TXT
Definition: cohfit.h:27
int exam_edit(char *, examdata *)
Definition: exam_edits.c:178
float closs
Definition: cohfit.h:127
void exam_gnuplot(examdata *)
Definition: exam_gnuplot.c:479
float ampl[MAX_NSEGLEN]
Definition: cohfit.h:62
fringesum * datum
Definition: cohfit.h:60
#define MAX_NSEGLEN
Definition: cohfit.h:26
int parse_cmdline(int, char **, FILE **, examdata *)
Definition: applications/cohfit/src/parse_cmdline.c:58
#define MAX_BNO
Definition: cohfit.h:25
void fit_codata(cosumary *, examdata *)
float fitapo[MAX_NSEGLEN]
Definition: cohfit.h:71
double pspar[3]
Definition: cohfit.h:108
int labels
Definition: cohfit.h:96
double maxcotime
Definition: cohfit.h:77
int fit_msnr(cosumary *, int)
Definition: fit_msrn.c:24
int fitmask
Definition: cohfit.h:126
int normalize_snr(cosumary *, int)
Definition: normalize_snr.c:27
int lastorder
Definition: cohfit.h:46
double pspar[3]
Definition: cohfit.h:88
void clear_codata(cosumary *)
Definition: clear_codata.c:16
int navg[MAX_NSEGLEN]
Definition: cohfit.h:65
char * edit
Definition: cohfit.h:133
double pserr[3]
Definition: cohfit.h:88
int fit_ampl(cosumary *, int, int)
Definition: fit_ampl.c:85
double slope
Definition: cohfit.h:105
void exam_file(cosumary *, int, examdata *)
Definition: exam_file.c:23
float cohereloss
Definition: cohfit.h:76
float redchisq[NFITOPT]
Definition: cohfit.h:84
double ampl_cotime
Definition: cohfit.h:106
float seglen[MAX_NSEGLEN]
Definition: cohfit.h:64
float fit2p8[MAX_NSEGLEN]
Definition: cohfit.h:68
char * examfile
Definition: cohfit.h:112
float fitaps[MAX_NSEGLEN]
Definition: cohfit.h:70
void set_gnuplot_opts(examdata *)
Definition: exam_gnuplot.c:44
float fitsnr[MAX_NSEGLEN]
Definition: cohfit.h:67
float plateau
Definition: cohfit.h:73
float breakpoint
Definition: cohfit.h:74
int cbs_p2p7[3]
Definition: cohfit.h:87
char * epat
Definition: cohfit.h:131
float snr[MAX_NSEGLEN]
Definition: cohfit.h:63
char * as_fit_nm_ndx(int, int *)
Definition: as_fit_nm_ndx.c:6
avg_data * data
Definition: cohfit.h:53
int sort_data(avg_data *, int)
Definition: cohfit/src/sort_data.c:15
double snrmin
Definition: cohfit.h:104
struct cosummary cosumary
int customlimits
Definition: cohfit.h:122
double poerr[1]
Definition: cohfit.h:109
double ampmax
Definition: cohfit.h:103
double lenmax
Definition: cohfit.h:102
double cohereloss
Definition: cohfit.h:105
char * exampat
Definition: cohfit.h:94
int didfits
Definition: cohfit.h:82
int iteratio[NFITOPT]
Definition: cohfit.h:83
double ampl_cotime
Definition: cohfit.h:91
int bno
Definition: cohfit.h:113
double popar[1]
Definition: cohfit.h:89
int gnuplot
Definition: cohfit.h:121
int read_data(avgspace *, char *, int *, int *)
Definition: cohfit/src/read_data.c:25
int cbs_p2p8[3]
Definition: cohfit.h:86
void clear_err_handler_report(void)
Definition: err_handler.c:15
int bestsnr
Definition: cohfit.h:81
double poerr[1]
Definition: cohfit.h:89
char * get_err_handler_report()
Definition: err_handler.c:37
int keyval
Definition: cohfit.h:47
int get_gslegacy_default(void)
Definition: fit_ampl.c:19
int density
Definition: cohfit.h:123
#define NFITOPT
Definition: cohfit.h:29
int bestamp
Definition: cohfit.h:80
float fit2p7[MAX_NSEGLEN]
Definition: cohfit.h:69
void sorter(avg_data *, int)
Definition: cohfit/src/sorter.c:38
struct examdata examdata
int pbno
Definition: cohfit.h:139
int bestsnrndx
Definition: cohfit.h:111
gsl_error_handler_t err_handler
Definition: cohfit.h:44
Definition: cohfit.h:51
Definition: cohfit.h:58
Definition: cohfit.h:100
Definition: cohfit.h:119
const double xtol
const double gtol
Definition: fit_gsl.h:63
const double ftol
Definition: fit_gsl.h:63
const size_t maxits
Definition: search.h:33