forked from CJSaxton/patchSF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
164 lines (147 loc) · 4.48 KB
/
parse.c
File metadata and controls
164 lines (147 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*****************************************************************************
* Parse the command-line arguments as flag options, numerical parameters
* or equations to overwrite named variables.
*****************************************************************************/
/*
-mute suppress some screen messages
-tmp put temporary files in /tmp instead of /unsafe
-NOW soften the imposition of curfew
-rel minimise max relative error rather than max absolute error
-logx take logarithms of X data, and adjust uncertainties
-logy take logarithms of Y data, and adjust uncertainties
-pds generate power spectrum with Leahy normalisation, not a SF.
n = order of structure function
start = omit data before this starting time
end = omit data after this ending time
tmin = minimum timescale for SF or PDS calculation
tmax = maximum timescale for SF or PDS calculation
nx = number of coefficients in X independent variable
ny = number of coefficients in Y independent variable
guess = maximum initial guess for free parameters
try = number of fitting attempts, before saving the best
*/
double numin[42]; /* Numerical command-line options. */
long double eqnrhs[42]; /* Equation command-line options, values. */
char eqnlhs[42][80]; /* Equation command-line options, variable labels. */
int numpars;
int numequations=0;
int is_number(char *test)
{
/* Test whether a string represents a valid number. */
int c,bad,point,e;
bad=point=e=0;
for(c=0;test[c]!='\0';c++) {
if( (!isdigit(test[c])) && (test[c]!='.')
&& (test[c]!='e') &&(test[c]!='E')
&& (test[c]!='+') &&(test[c]!='-') ) bad++;
if(test[c]=='.') point++;
if((test[c]=='e')||(test[c]=='E')) e++;
}
if((bad>0)||(point>1)||(e>1)) return(FALSE);
else return(TRUE);
}
/***************************************************************************/
void read_flags(argc,argv)
int argc;
char *argv[];
{
char par[80],*val,*wo,*end,*endp;
long double nval;
int p,nn,nf,neqn,l;
numpars=0;
neqn=nn=nf=0;
for(p=1;p<argc;p++) {
val=strchr(argv[p],'=');
if(val!=NULL) {
/* This option is an equation. */
printf("equation:\t");
eqnrhs[neqn]=nval=atof(val+1);
l=val-argv[p];
strcpy(par,argv[p]);
val=strchr(par,'=');
*val='\0';
strcpy(eqnlhs[neqn],par);
printf("%s = %Lf\n",par,nval);
neqn++;
continue;
} else {
if (is_number(argv[p])) {
/* This option is a number. */
numin[nn]=atof(argv[p]);
printf("number : %e\n",numin[nn]);
nn++;
} else {
/* This option is a string or flag. */
l=strlen(argv[p]);
/*
*/
if(argv[p][0]=='^') { // file name
int i;
for(i=1;i<l;i++) profname[i-1]=argv[p][i];
profname[i-1]='\0';
printf("file : \"%s\"\n",profname);
continue;
} else
printf("string : \"%s\"\t%i long\n",argv[p],l);
if(!strcmp(argv[p],"-rel")) flag_rel=TRUE;
if(!strcmp(argv[p],"-mute")) mute_setup=TRUE;
if(!strcmp(argv[p],"-pds")) flag_pds=TRUE;
if(!strcmp(argv[p],"-logx")) flag_logx=TRUE;
if(!strcmp(argv[p],"-logy")) flag_logy=TRUE;
nf++;
}
}
}
numpars=nn;
numequations=neqn;
if(argc>1) {
printf("===== program options =====\n");
printf("\tequations : %i\n",numequations);
printf("\tnumerical : %i\n",numpars);
printf("\tcharacter : %i\n",argc-numpars-1);
}
return;
}
/***************************************************************************/
void read_indat()
{
/* Append equations stated in the "indat" setup file. */
FILE *indat;
int i;
char s[40],olds[40];
long double x;
sprintf(olds,"ZYXWV");
indat=fopen("indat","r");
do {
strcpy(olds,s);
i=fscanf(indat,"%s = %Lf",s,&x);
if(s[0]==EOF) break;
if(!strcmp(s,".")) break;
if(!mute_setup) printf("\t\"%s\"\t= % Le\n",s,x);
strcpy(eqnlhs[numequations],s);
eqnrhs[numequations]=x;
numequations++;
} while(strcmp(s,olds));
fclose(indat);
}
/****************************************************************************/
void setup()
{
void show(char *s, long double x) { printf("\t\"%s\"\t= % Lg\n",s,x); }
long double x;
char s[40];
int i,p=0;
for(i=numequations-1;i>=0;i--) {
sprintf(s,"%s",eqnlhs[i]);
x=eqnrhs[i];
if(!strcmp(s,"nx" )) { nx=x; show(s,x); }
if(!strcmp(s,"ny" )) { ny=x; show(s,x); }
if(!strcmp(s,"guess" )) { aguess=x; show(s,x); }
if(!strcmp(s,"try" )) { ntries=x; show(s,x); }
if(!strcmp(s,"start" )) { tstart=x; show(s,x); }
if(!strcmp(s,"end" )) { tend=x; show(s,x); }
if(!strcmp(s,"tmin" )) { tmin=x; show(s,x); }
if(!strcmp(s,"tmax" )) { tmax=x; show(s,x); }
if(!strcmp(s,"n" )) { sfn=x; show(s,x); }
}
}