-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhc_misc.c
430 lines (406 loc) · 8.38 KB
/
hc_misc.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "hc.h"
/*
miscellaneous functions for allocating arrays, copying arrays, opening
file streams, the like
$Id: hc_misc.c,v 1.5 2004/12/20 05:18:12 becker Exp becker $
*/
/* high precision vector allocation */
void hc_hvecalloc(HC_HIGH_PREC **x,int n,char *message)
{
*x = (HC_HIGH_PREC *)malloc(sizeof(HC_HIGH_PREC)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* double */
void
hc_dvecalloc (x, n, message)
double **x;
int n;
char *message;
{
*x = (double *)malloc(sizeof(double)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* single prec vector allocation */
void
hc_svecalloc (x, n, message)
float **x;
int n;
char *message;
{
*x = (float *)malloc(sizeof(float)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* integer vector allocation */
void
hc_ivecalloc (x, n, message)
int **x;
int n;
char *message;
{
*x = (int *)malloc(sizeof(int)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* general floating point vector allocation */
void
hc_vecalloc (x, n, message)
HC_PREC **x;
int n;
char *message;
{
*x = (HC_PREC *)malloc(sizeof(HC_PREC)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* single prec complex vector allocation */
void
hc_scmplx_vecalloc (x, n, message)
struct hc_scmplx **x;
int n;
char *message;
{
*x = (struct hc_scmplx *)malloc(sizeof(struct hc_scmplx)*(size_t)n);
if(! (*x))
HC_MEMERROR(message);
}
/* single vector reallocation */
void
hc_svecrealloc (x, n, message)
float **x;
int n;
char *message;
{
*x = (float *)realloc(*x,sizeof(float)*(size_t)n);
if(!(*x))
HC_MEMERROR(message);
}
/* HC_HIGH_PREC vector reallocation */
void
hc_dvecrealloc (x, n, message)
HC_HIGH_PREC **x;
int n;
char *message;
{
*x = (HC_HIGH_PREC *)realloc(*x,sizeof(HC_HIGH_PREC)*(size_t)n);
if(!(*x))
HC_MEMERROR(message);
}
/* general version */
void
hc_vecrealloc (x, n, message)
HC_PREC **x;
int n;
char *message;
{
*x = (HC_PREC *)realloc(*x,sizeof(HC_PREC)*(size_t)n);
if(!(*x))
HC_MEMERROR(message);
}
/*
sqrt(sum(squared diff)) between [n] vectors a and b
*/
float
hc_vec_rms_diff (a, b, n)
HC_PREC *a;
HC_PREC *b;
int n;
{
int i;
HC_PREC sum=0.0,tmp;
for(i=0;i<n;i++){
tmp = a[i] - b[i];
sum += tmp*tmp;
}
return sqrt(sum/n);
}
float
hc_vec_rms (a, n)
HC_PREC *a;
int n;
{
int i;
HC_PREC sum=0.0;
for(i=0;i<n;i++){
sum += a[i] * a[i];
}
return sqrt(sum/n);
}
/* a[n] = b[n], single precision version */
void
hc_a_equals_b_svector (a, b, n)
float *a;
float *b;
int n;
{
memcpy(a,b,sizeof(float)*n);
}
/* general version */
void
hc_a_equals_b_vector (a, b, n)
HC_PREC *a;
HC_PREC *b;
int n;
{
memcpy(a,b,sizeof(HC_PREC)*n);
}
/* compute the mean of a single precision vector */
float
hc_mean_svec (x, n)
float *x;
int n;
{
float sum=0.0;
int i;
for(i=0;i<n;i++){
sum += x[i];
}
sum /= (float)n;
return sum;
}
/* compute the mean of a vector */
HC_PREC
hc_mean_vec (x, n)
HC_PREC *x;
int n;
{
HC_PREC sum=0.0;
int i;
for(i=0;i<n;i++){
sum += x[i];
}
sum /= (HC_PREC)n;
return sum;
}
/* zero a HC_HIGH_PREC precision vector */
void hc_zero_dvector (x, n)
HC_HIGH_PREC *x;
int n;
{
int i;
for(i=0;i<n;i++)
x[i] = 0.0;
}
/* zero a vector of type logical */
void hc_zero_lvector (x, n)
hc_boolean *x;
int n;
{
int i;
for(i=0;i<n;i++)
x[i] = FALSE;
}
/*
assign floating point formats to a string as used by sscanf
of fscanf
if append is TRUE, will add a format string, else will create
anew
*/
void hc_get_flt_frmt_string (string, n, append)
char *string;
int n;
hc_boolean append;
{
static hc_boolean init=FALSE; /* that's OK, multiple instances calling are fine */
static char type_s[3];
char buffer[HC_CHAR_LENGTH+1];
int i;
if(!init){
if(sizeof(HC_PREC) == sizeof(float)){
sprintf(type_s,"f");
}else if (sizeof(HC_PREC) == sizeof(double)){
sprintf(type_s,"lf");
}else if (sizeof(HC_PREC) == sizeof(long double)){
sprintf(type_s,"lf");
}else{
fprintf(stderr,"hc_get_flt_frmt_string: assignment error\n");
exit(-1);
}
init=TRUE;
}
if(!append)
sprintf(string,"%%%s",type_s);
for(i=1;i<n;i++){
if((int)strlen(string)+4 < HC_CHAR_LENGTH){
strncpy(buffer,string,HC_CHAR_LENGTH);
sprintf(string,"%s %%%s",buffer,type_s);
}else{
fprintf(stderr,"hc_get_flt_frmt_string: error: out of string length at %i\n",
(int)strlen(string));
exit(-1);
}
}
}
//
// deal with boolean values/switches
char *hc_name_boolean (value)
hc_boolean value;
{
if(value)
return("ON");
else
return("OFF");
}
hc_boolean hc_toggle_boolean (variable)
hc_boolean *variable;
{
if(*variable){
*variable=FALSE;
return(FALSE);
}else{
*variable=TRUE;
return(TRUE);
}
}
//
// check, if we can read a value for the option flag in a chain of command line
// arguments
//
void
hc_advance_argument (i, argc, argv)
int *i;
int argc;
char **argv;
{
if(argc <= *i + 1){// no arguments left
fprintf(stderr,"%s: input parameters: error: option \"%s\" needs a value\n",
argv[0],argv[*i]);
exit(-1);
}
*i += 1;
}
/*
compute the correlation between two scalar fields
mode 0 : full correlation
1 : up to 20, and between 4...9
2 : up to 20, and between 4...9, and between 2...4
*/
void
hc_compute_correlation (g1, g2, c, mode, verbose)
struct sh_lms *g1;
struct sh_lms *g2;
HC_PREC *c;
int mode;
hc_boolean verbose;
{
int lmaxg;
lmaxg = HC_MIN(g1->lmax,g1->lmax);
switch(mode){
case 0: /* 1...LMAX */
if(verbose)
fprintf(stderr,"hc_compute_correlation: computing 1...%i\n",lmaxg);
c[0] = sh_correlation_per_degree(g1,g2,1,lmaxg);
break;
case 1: /* 1...20 and 4..9 correlations */
lmaxg = HC_MIN(20,lmaxg);
if(verbose)
fprintf(stderr,"hc_compute_correlation: computing 1...%i and 4..9 correlations\n",lmaxg);
c[0] = sh_correlation_per_degree(g1,g2,1,lmaxg);
c[1] = sh_correlation_per_degree(g1,g2,4,9);
break;
case 2: /* 1...20, 4..9, 2..4 correlations */
lmaxg = HC_MIN(20,lmaxg);
if(verbose)
fprintf(stderr,"hc_compute_correlation: computing 1...%i and 4..9 correlations\n",lmaxg);
c[0] = sh_correlation_per_degree(g1,g2,1,lmaxg);
c[1] = sh_correlation_per_degree(g1,g2,4,9);
c[2] = sh_correlation_per_degree(g1,g2,2,4);
break;
default:
fprintf(stderr,"sh_compute_correlation: mode %i undefined\n",mode);
exit(-1);
}
}
/*
convert polar vector in r,theta,phi format to cartesian
vector x
*/
void
lonlatpv2cv (lon, lat, polar_vec, cart_vec)
HC_PREC lon;
float lat;
HC_PREC *polar_vec;
HC_PREC *cart_vec;
{
HC_PREC theta,phi;
theta = LAT2THETA((HC_HIGH_PREC)lat);
phi = LON2PHI((HC_HIGH_PREC)lon);
thetaphipv2cv(theta,phi,polar_vec,cart_vec);
}
/* theta, phi version */
void
thetaphipv2cv (theta, phi, polar_vec, cart_vec)
HC_PREC theta;
float phi;
HC_PREC *polar_vec;
HC_PREC *cart_vec;
{
HC_HIGH_PREC polar_base[9];
calc_polar_base_at_theta_phi(theta,phi,polar_base);
lonlatpv2cv_with_base(polar_vec,polar_base,cart_vec);
}
void
lonlatpv2cv_with_base (polar_vec, polar_base, cart_vec)
HC_PREC *polar_vec;
HC_HIGH_PREC *polar_base;
HC_PREC *cart_vec;
{
int i;
// convert vector
for(i=0;i<3;i++){
cart_vec[i] = polar_base[i] * polar_vec[0]; /* r,theta,phi */
cart_vec[i] += polar_base[3+i] * polar_vec[1];
cart_vec[i] += polar_base[6+i] * polar_vec[2];
}
}
void
calc_polar_base_at_theta_phi (theta, phi, polar_base)
HC_PREC theta;
HC_PREC phi;
HC_HIGH_PREC *polar_base;
{
HC_HIGH_PREC cp,sp,ct,st;
// base vecs
ct=cos((HC_HIGH_PREC)theta);cp=cos((HC_HIGH_PREC)phi);
st=sin((HC_HIGH_PREC)theta);sp=sin((HC_HIGH_PREC)phi);
//
polar_base[0]= st * cp;
polar_base[1]= st * sp;
polar_base[2]= ct;
//
polar_base[3]= ct * cp;
polar_base[4]= ct * sp;
polar_base[5]= -st;
//
polar_base[6]= -sp;
polar_base[7]= cp;
polar_base[8]= 0.0;
}
/*
given a sorted vector y (y0<y1<...<yn-1) with n elements, return
the weights f1 for element i1 and weight f2 for element i2 such
that the interpolation is at y1
*/
void
hc_linear_interpolate (y, n, y1, i1, i2, f1, f2)
HC_PREC *y;
int n;
HC_PREC y1;
int *i1;
int *i2;
HC_PREC *f1;
HC_PREC *f2;
{
int n1;
n1 = n-1;
*i2 = 0;
while((*i2 < n1) && (y[*i2] < y1))
*i2 += 1;
if(*i2 == 0)
*i2 = 1;
*i1 = *i2 - 1;
*f2 = (y1 - y[*i1])/(y[*i2]-y[*i1]);
*f1 = 1.0 - *f2;
}