forked from scottransom/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc_utils.c
842 lines (730 loc) · 24 KB
/
misc_utils.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#include <ctype.h>
#include "misc_utils.h"
#include "slamac.h"
#ifndef ARCSEC2RAD
/* arcseconds to radians */
#define ARCSEC2RAD 4.8481368110953599358991410235794797595635330237270e-6
#endif
#ifndef SEC2RAD
/* seconds of time to radians */
#define SEC2RAD 7.2722052166430399038487115353692196393452995355905e-5
#endif
/* Speef of light in m/s */
#ifndef SOL
#define SOL 299792458.0
#endif
/* Radians to degrees */
#ifndef RADTODEG
#define RADTODEG 57.29577951308232087679815481410517033240547246656
#endif
/* return the max of two double values */
#define DMAX(a,b) ((a)>(b)?(a):(b))
/* like strcpy, except guaranteed to work with overlapping strings */
#define strMove(d,s) memmove(d,s,strlen(s)+1)
void slaDjcl(double djm, int *iy, int *im, int *id, double *fd, int *j);
char *rmtrail(char *str)
/* Removes trailing space from a string */
{
int i;
if (str && 0 != (i = strlen(str))) {
while (--i >= 0) {
if (!isspace(str[i]))
break;
}
str[++i] = '\0';
}
return str;
}
char *rmlead(char *str)
/* Removes leading space from a string */
{
char *obuf;
if (str) {
for (obuf = str; *obuf && isspace(*obuf); ++obuf);
if (str != obuf)
strMove(str, obuf);
}
return str;
}
char *remove_whitespace(char *str)
/* Remove leading and trailing space from a string */
{
return rmlead(rmtrail(str));
}
char *strlower(char *str)
/* Convert a string to lower case */
{
char *ss;
if (str) {
for (ss = str; *ss; ++ss)
*ss = tolower(*ss);
}
return str;
}
void split_path_file(char *input, char **path, char **file)
/* This routine splits an input string into a path and */
/* a filename. Since it allocates the memory for the */
/* path and filename dynamically, the calling program */
/* must free both "path" and "file". */
{
char *sptr = NULL;
unsigned int len, pathlen = 0, filelen = 0;
len = strlen(input);
if (len > 200) {
perror
("Error: input string length > 200, in split_path_file(). Likely an issue.\n");
exit(-1);
}
sptr = strrchr(input, '/');
// The 2nd part of the following handles relative paths,
// but in a strange way, in that the file will have the
// relative part in it. The path will be the absolute
// path of the current working directory. That's usually
// euivalent to what we want, though.
if ((sptr == NULL) || (input[0] == '.')) {
sptr = getcwd(NULL, 0);
if (sptr == NULL) {
perror
("Error: could not get current directory name (too long?) in split_path_file()\n");
exit(-1);
}
pathlen = strlen(sptr);
*path = (char *) calloc(pathlen + 1, sizeof(char));
*file = (char *) calloc(len + 1, sizeof(char));
strncpy(*path, sptr, pathlen + 1);
strncpy(*file, input, len + 1);
// ensure null-terminated
(*path)[pathlen] = '\0';
(*file)[len] = '\0';
free(sptr);
} else {
pathlen = sptr - input;
filelen = len - pathlen - 1;
*path = (char *) calloc(pathlen + 1, sizeof(char));
*file = (char *) calloc(filelen + 1, sizeof(char));
strncpy(*path, input, pathlen + 1);
strncpy(*file, sptr + 1, filelen + 1);
// ensure null-terminated
(*path)[pathlen] = '\0';
(*file)[filelen] = '\0';
}
}
int split_root_suffix(char *input, char **root, char **suffix)
/* This routine splits an input string into a root name */
/* + suffix. Since it allocates the memory for the */
/* root and suffix dynamically, the calling program */
/* must free both "root" and "suffix". */
/* If the routine finds a suffix, it returns 1, else 0. */
{
char *sptr = NULL;
unsigned int len, rootlen = 0, suffixlen = 0;
len = strlen(input);
if (len > 200) {
perror
("Error: input string length > 200, in split_root_suffix(). Likely an issue.\n");
exit(-1);
}
sptr = strrchr(input, '.');
if (sptr == NULL) {
*root = (char *) calloc(len + 1, sizeof(char));
strncpy(*root, input, len);
// ensure null-terminated
(*root)[len] = '\0';
return 0;
} else {
rootlen = sptr - input;
*root = (char *) calloc(rootlen + 1, sizeof(char));
strncpy(*root, input, rootlen);
// ensure null-terminated
(*root)[rootlen] = '\0';
suffixlen = len - rootlen - 1;
*suffix = (char *) calloc(suffixlen + 1, sizeof(char));
strncpy(*suffix, sptr + 1, suffixlen);
// ensure null-terminated
(*suffix)[suffixlen] = '\0';
return 1;
}
}
void strtofilename(char *string)
/* Trim spaces off the end of *input and convert */
/* all other spaces into underscores. */
{
int ii;
ii = strlen(string) - 1;
do {
if (string[ii] == ' ')
string[ii] = '\0';
else
break;
} while (ii--);
do {
if (string[ii] == ' ')
string[ii] = '_';
} while (ii--);
}
void mjd_to_datestr(double mjd, char *datestr)
// Convert an MJD to a PSRFITS-style DATEOBS
{
int year, month, day, hour, min, err;
double fracday, dh, dm, sec;
slaDjcl(mjd, &year, &month, &day, &fracday, &err);
if (err == -1) {
printf("Error in mjd_to_datestr: Bad MJD '%.12f'.\n", mjd);
exit(1);
}
dh = fracday * 24.0;
hour = (int) dh;
dm = (dh - hour) * 60.0;
min = (int) dm;
sec = (dm - min) * 60.0;
if (sec < 10.0) {
sprintf(datestr, "%4d-%02d-%02dT%02d:%02d:0%.6g",
year, month, day, hour, min, sec);
} else {
sprintf(datestr, "%4d-%02d-%02dT%02d:%02d:%.6g",
year, month, day, hour, min, sec);
}
}
void telescope_to_tempocode(char *inname, char *outname, char *obscode)
// Return the 2 character TEMPO string for an observatory
// whose name is in the string "inname". Return a nice
// name in "outname".
{
char scope[40];
strncpy(scope, inname, 40);
// ensure null-terminated
scope[39] = '\0';
strlower(scope);
if (strcmp(scope, "gbt") == 0) {
strcpy(obscode, "GB");
strcpy(outname, "GBT");
} else if (strcmp(scope, "arecibo") == 0) {
strcpy(obscode, "AO");
strcpy(outname, "Arecibo");
} else if (strcmp(scope, "vla") == 0) {
strcpy(obscode, "VL");
strcpy(outname, "VLA");
} else if (strcmp(scope, "parkes") == 0) {
strcpy(obscode, "PK");
strcpy(outname, "Parkes");
} else if (strcmp(scope, "jodrell") == 0) {
strcpy(obscode, "JB");
strcpy(outname, "Jodrell Bank");
} else if ((strcmp(scope, "gb43m") == 0) ||
(strcmp(scope, "gb 140ft") == 0) || (strcmp(scope, "nrao20") == 0)) {
strcpy(obscode, "G1");
strcpy(outname, "GB43m");
} else if (strcmp(scope, "nancay") == 0) {
strcpy(obscode, "NC");
strcpy(outname, "Nancay");
} else if (strcmp(scope, "effelsberg") == 0) {
strcpy(obscode, "EF");
strcpy(outname, "Effelsberg");
} else if (strcmp(scope, "srt") == 0) {
strcpy(obscode, "SR");
strcpy(outname, "Sardinia Radio Telescope");
} else if (strcmp(scope, "fast") == 0) {
strcpy(obscode, "FA");
strcpy(outname, "FAST");
} else if (strcmp(scope, "wsrt") == 0) {
strcpy(obscode, "WT");
strcpy(outname, "WSRT");
} else if (strcmp(scope, "gmrt") == 0) {
strcpy(obscode, "GM");
strcpy(outname, "GMRT");
} else if (strcmp(scope, "chime") == 0) {
strcpy(obscode, "CH");
strcpy(outname, "CHIME");
} else if (strcmp(scope, "lofar") == 0) {
strcpy(obscode, "LF");
strcpy(outname, "LOFAR");
} else if (strcmp(scope, "lwa") == 0) {
strcpy(obscode, "LW");
strcpy(outname, "LWA1");
} else if (strcmp(scope, "mwa") == 0 ) {
strcpy(obscode, "MW");
strcpy(outname, "MWA");
} else if (strcmp(scope, "meerkat") == 0 ) {
strcpy(obscode, "MK");
strcpy(outname, "MeerKAT");
} else if (strcmp(scope, "ata") == 0) {
strcpy(obscode, "AT");
strcpy(outname, "ATA");
} else if (strcmp(scope, "k7") == 0 ) {
strcpy(obscode, "K7");
strcpy(outname, "KAT-7");
} else if (strcmp(scope, "geocenter") == 0) {
strcpy(obscode, "0 ");
strcpy(outname, "Geocenter");
} else {
printf("\nWARNING!!!: I don't recognize the observatory (%s)!\n", inname);
printf(" Defaulting to the Geocenter for TEMPO.\n");
strcpy(obscode, "0 ");
strcpy(outname, "Unknown");
}
}
float invsqrtf(float x)
// See http://en.wikipedia.org/wiki/Fast_inverse_square_root
{
union {
float f;
int i;
} tmp;
tmp.f = x;
tmp.i = 0x5f3759df - (tmp.i >> 1);
float y = tmp.f;
return y * (1.5f - 0.5f * x * y * y);
}
long long next2_to_n(long long x)
/* Return the first value of 2^n >= x */
{
long long i = 1;
while (i < x)
i <<= 1;
return i;
}
int is_power_of_10(long long n)
/* Check whether n is a power of 10 or not. Return 0 or 1 */
{
while (n > 9L && n % 10L == 0)
n /= 10L;
return n == 1;
}
long long choose_good_N(long long orig_N)
// Choose a time series length that is larger than the input value but
// that is highly factorable.
{
char ctmp20[20], ctmp5[5];
long long two_N = 2, small_N;
int ii, first4;
int goodfactors[114] = {1000, 1008, 1024, 1056, 1120, 1152, 1200, 1232,
1280, 1296, 1344, 1408, 1440, 1536, 1568, 1584,
1600, 1680, 1728, 1760, 1792, 1920, 1936, 2000,
2016, 2048, 2112, 2160, 2240, 2304, 2352, 2400,
2464, 2560, 2592, 2640, 2688, 2800, 2816, 2880,
3024, 3072, 3136, 3168, 3200, 3360, 3456, 3520,
3584, 3600, 3696, 3840, 3872, 3888, 3920, 4000,
4032, 4096, 4224, 4320, 4400, 4480, 4608, 4704,
4752, 4800, 4928, 5040, 5120, 5184, 5280, 5376,
5488, 5600, 5632, 5760, 5808, 6000, 6048, 6144,
6160, 6272, 6336, 6400, 6480, 6720, 6912, 7040,
7056, 7168, 7200, 7392, 7680, 7744, 7776, 7840,
7920, 8000, 8064, 8192, 8400, 8448, 8624, 8640,
8800, 8960, 9072, 9216, 9408, 9504, 9600, 9680,
9856, 10000};
if (orig_N <= 0) return 0;
// Get the number represented by the first 4 digits of orig_N
sprintf(ctmp20, "%lld", orig_N);
strncpy(ctmp5, ctmp20, 4);
ctmp5[4] = '\0';
first4 = atoi(ctmp5);
// Now get the number that is just bigger than orig_N
// that has its first 4 digits equal to "factor"
for (ii = 0; ii < 114; ii++) {
small_N = goodfactors[ii];
// check to see if orig_N is a goodfactor times a power of 10
if (small_N == first4 &&
orig_N % small_N == 0 &&
is_power_of_10(orig_N/small_N)) break;
if (small_N > first4) break;
}
while (small_N < orig_N) small_N *= 10;
// Finally, compare new_N to the closest power_of_two
// greater than orig_N. Take the closest.
while (two_N < orig_N) two_N *= 2;
if (two_N < small_N) return two_N;
else return small_N;
}
#if 0
int gcd(int a, int b)
/* Return the greatest common divisor of a and b */
{
int aa, bb, tmpa;
aa = abs(a);
bb = abs(b);
while (a) {
tmpa = a;
a = b % a;
b = tmpa;
}
return b;
}
#endif
float beam_halfwidth(float freq, float dish_diam)
// Return the beam halfwidth in arcsec when freq
// is in MHz and dish_diam is in meters
{
return 0.5 * 1.2 * SOL / (freq * 1e6) / dish_diam * RADTODEG * 3600.0;
}
float *gen_freqs(long numfreqs, double lof, double df)
/* This routine generates a float vector of length numfreqs */
/* with values set to lof, lof+df, lof+2df, ... */
/* It is normally used when generating a list of freqs */
/* for an x-y plot of spectral data. */
{
long i;
float *freqs;
freqs = gen_fvect(numfreqs);
for (i = 0; i < numfreqs; i++) {
freqs[i] = lof + i * df;
}
return freqs;
}
double *gen_dfreqs(long numfreqs, double lof, double df)
/* This routine generates a double vector of length numfreqs */
/* with values set to lof, lof+df, lof+2df, ... */
/* It is normally used when generating a list of freqs */
/* for an x-y plot of spectral data. */
{
long i;
double *freqs;
freqs = gen_dvect(numfreqs);
for (i = 0; i < numfreqs; i++) {
freqs[i] = lof + i * df;
}
return freqs;
}
void i_to_n(int n, double *rl, double *im)
/* Return the real and imaginary portions of i^n */
{
n %= 4;
if (n < 0)
n += 4;
if (n == 0) {
*rl = 1.0;
*im = 0.0;
} else if (n == 1) {
*rl = 0.0;
*im = 1.0;
} else if (n == 2) {
*rl = -1.0;
*im = 0.0;
} else {
*rl = 0.0;
*im = -1.0;
}
}
void rotate_1d(float *data, long numbins, long bins_to_left)
/* Rotates a vector by bins_to_left places to the left. */
/* numbins is the number of FLOATING points to move. */
/* frotate is better. Use it. */
{
float *tmp;
if (bins_to_left < 0 || bins_to_left >= numbins) {
printf("\nNumber of bins to rotate array in rotate_1d is\n");
printf("\nout of bounds. Tried to rotate %ld bins. Exiting.\n",
bins_to_left);
exit(1);
}
tmp = gen_fvect(bins_to_left);
memcpy(tmp, data, sizeof(float) * bins_to_left);
memmove(data, data + bins_to_left, sizeof(float) * (numbins - bins_to_left));
memcpy(data + bins_to_left, tmp, sizeof(float) * bins_to_left);
vect_free(tmp);
}
void drotate_1d(double *data, long numbins, long bins_to_left)
/* Rotates a vector by bins_to_left places to the left. */
/* numbins is the number of DOUBLE points to move. */
/* drotate is better. Use it. */
{
double *tmp;
if (bins_to_left < 0 || bins_to_left >= numbins) {
printf("\nNumber of bins to rotate array in rotate_1d is\n");
printf("\nout of bounds. Tried to rotate %ld bins. Exiting.\n",
bins_to_left);
exit(1);
}
tmp = gen_dvect(bins_to_left);
memcpy(tmp, data, sizeof(double) * bins_to_left);
memmove(data, data + bins_to_left, sizeof(double) * (numbins - bins_to_left));
memcpy(data + bins_to_left, tmp, sizeof(double) * bins_to_left);
vect_free(tmp);
}
void frotate(float *data, long numbins, float bins_to_left)
/* Rotates a vector by bins_to_left places to the left. */
/* numbins is the number of FLOATING points to move. */
{
float *tmp;
double lopart, hipart, intpart;
long i, index;
bins_to_left = fmod(bins_to_left, (double) numbins);
if (bins_to_left < 0.0)
bins_to_left += numbins;
tmp = gen_fvect(numbins);
lopart = modf(bins_to_left, &intpart);
hipart = 1.0 - lopart;
index = (long) floor(intpart + 1.0E-20);
for (i = 0; i < numbins; i++)
tmp[i] = hipart * data[(index + i) % numbins] +
lopart * data[(index + i + 1) % numbins];
memcpy(data, tmp, sizeof(float) * numbins);
vect_free(tmp);
}
void drotate(double *data, long numbins, double bins_to_left)
/* Rotates a vector by bins_to_left places to the left. */
/* numbins is the number of DOUBLE points to move. */
{
double *tmp, lopart, hipart, intpart;
long i, index;
bins_to_left = fmod(bins_to_left, (double) numbins);
if (bins_to_left < 0.0)
bins_to_left += numbins;
tmp = gen_dvect(numbins);
lopart = modf(bins_to_left, &intpart);
hipart = 1.0 - lopart;
index = (long) floor(intpart + 1.0E-20);
for (i = 0; i < numbins; i++)
tmp[i] = hipart * data[(index + i) % numbins] +
lopart * data[(index + i + 1) % numbins];
memcpy(data, tmp, sizeof(double) * numbins);
vect_free(tmp);
}
void stats(float *x, int n, double *mean, double *var, double *skew, double *kurt)
/* For a floating point vector, *x, of length n, this routine */
/* returns the mean, variance, skewness, and kurtosis of *x. */
{
long i;
double an = 0.0, an1 = 0.0, dx, t1, t2, stdevmle;
/* Modified (29 June 98) C version of the following: */
/* ALGORITHM AS 52 APPL. STATIST. (1972) VOL.21, P.226 */
/* Returned values were checked with Mathematica 3.01 */
if (n < 1) {
printf("\vVector length must be > 0 in stats(). Exiting\n");
exit(1);
} else {
*mean = (double) x[0];
*var = 0.0;
*skew = 0.0;
*kurt = 0.0;
}
for (i = 1; i < n; i++) {
an = (double) (i + 1);
an1 = (double) (i);
dx = ((double) x[i] - *mean) / an;
t1 = an1 * dx * dx;
t2 = an * t1;
*kurt -=
dx * (4.0 * *skew - dx * (6.0 * *var + t1 * (1.0 + an1 * an1 * an1)));
*skew -= dx * (3.0 * *var - t2 * (an - 2.0));
*var += t2;
*mean += dx;
}
if (n > 1) {
stdevmle = sqrt(*var / an);
t1 = stdevmle * stdevmle * stdevmle;
*skew /= t1 * an;
*kurt = *kurt / (t1 * stdevmle * an) - 3.0;
*var /= an1;
}
return;
}
void dstats(double *x, int n, double *mean, double *var, double *skew, double *kurt)
/* For a double precision vector, *x, of length n, this routine */
/* returns the mean, variance, skewness, and kurtosis of *x. */
{
long i;
double an = 0.0, an1 = 0.0, dx, t1, t2, stdevmle;
/* Modified (29 June 98) C version of the following: */
/* ALGORITHM AS 52 APPL. STATIST. (1972) VOL.21, P.226 */
/* Returned values were checked with Mathematica 3.01 */
if (n < 1) {
printf("\vVector length must be > 0 in dstats(). Exiting\n");
exit(1);
} else {
*mean = (double) x[0];
*var = 0.0;
*skew = 0.0;
*kurt = 0.0;
}
for (i = 1; i < n; i++) {
an = (double) (i + 1);
an1 = (double) (i);
dx = (x[i] - *mean) / an;
t1 = an1 * dx * dx;
t2 = an * t1;
*kurt -=
dx * (4.0 * *skew - dx * (6.0 * *var + t1 * (1.0 + an1 * an1 * an1)));
*skew -= dx * (3.0 * *var - t2 * (an - 2.0));
*var += t2;
*mean += dx;
}
if (n > 1) {
stdevmle = sqrt(*var / an);
t1 = stdevmle * stdevmle * stdevmle;
*skew /= t1 * an;
*kurt = *kurt / (t1 * stdevmle * an) - 3.0;
*var /= an1;
}
return;
}
void avg_var(float *x, int n, double *mean, double *var)
/* For a float vector, *x, of length n, this routine */
/* returns the mean and variance of *x. */
{
long i;
double an = 0.0, an1 = 0.0, dx;
/* Modified (29 June 98) C version of the following: */
/* ALGORITHM AS 52 APPL. STATIST. (1972) VOL.21, P.226 */
/* Returned values were checked with Mathematica 3.01 */
if (n < 1) {
printf("\vVector length must be > 0 in avg_var(). Exiting\n");
exit(1);
} else {
*mean = (double) x[0];
*var = 0.0;
}
for (i = 1; i < n; i++) {
an = (double) (i + 1);
an1 = (double) (i);
dx = (x[i] - *mean) / an;
*var += an * an1 * dx * dx;
*mean += dx;
}
if (n > 1)
*var /= an1;
return;
}
void davg_dvar(double *x, int n, double *mean, double *var)
/* For a double vector, *x, of length n, this routine */
/* returns the mean and variance of *x. */
{
long i;
double an = 0.0, an1 = 0.0, dx;
/* Modified (29 June 98) C version of the following: */
/* ALGORITHM AS 52 APPL. STATIST. (1972) VOL.21, P.226 */
/* Returned values were checked with Mathematica 3.01 */
if (n < 1) {
printf("\vVector length must be > 0 in avg_var(). Exiting\n");
exit(1);
} else {
*mean = (double) x[0];
*var = 0.0;
}
for (i = 1; i < n; i++) {
an = (double) (i + 1);
an1 = (double) (i);
dx = (x[i] - *mean) / an;
*var += an * an1 * dx * dx;
*mean += dx;
}
if (n > 1)
*var /= an1;
return;
}
void ra_dec_to_string(char *radec, int h_or_d, int m, double s)
/* Return a properly formatted string containing RA or DEC values */
/* radec is a string with J2000 RA in the format 'hh:mm:ss.ssss' */
/* or a string with J2000 DEC in the format 'dd:mm:ss.ssss' */
{
int offset = 0;
if (h_or_d == 0 && (m < 0 || s < 0.0)) {
radec[0] = '-';
offset = 1;
}
sprintf(radec + offset, "%.2d:%.2d:%07.4f", h_or_d, abs(m), fabs(s));
}
void ra_dec_from_string(char *radec, int *h_or_d, int *m, double *s)
/* Return a values for hours or degrees, minutes and seconds */
/* given a properly formatted RA or DEC string. */
/* radec is a string with J2000 RA in the format 'hh:mm:ss.ssss' */
/* or a string with J2000 DEC in the format 'dd:mm:ss.ssss' */
{
int retval;
radec = remove_whitespace(radec);
retval = sscanf(radec, "%d:%d:%lf\n", h_or_d, m, s);
if (retval != 3) {
char tmp[100];
sprintf(tmp,
"Error: can not convert '%s' to RA or DEC in ra_dec_from_string()\n",
radec);
perror(tmp);
exit(1);
}
if (radec[0] == '-' && *h_or_d == 0) {
*m = -*m;
*s = -*s;
}
}
double hms2hours(int hours, int min, double sec)
/* Convert hours, minutes, and seconds of time to hours */
{
return (double) hours + (((double) min + sec / 60.0) / 60.0);
}
void hours2hms(double hours, int *h, int *m, double *s)
/* Convert decimal hours to hours, minutes, and seconds */
{
double tmp;
*h = (int) floor(hours);
tmp = (hours - *h) * 60.0;
*m = (int) floor(tmp);
*s = (tmp - *m) * 60.0;
}
void deg2dms(double degrees, int *d, int *m, double *s)
/* Convert decimal degrees to degrees, minutes, and seconds */
{
int sign = 1;
double tmp;
if (degrees < 0.0)
sign = -1;
*d = (int) floor(fabs(degrees));
tmp = (fabs(degrees) - *d) * 60.0;
*m = (int) floor(tmp);
*s = (tmp - *m) * 60.0;
*d *= sign;
if (*d == 0) {
*m *= sign;
*s *= sign;
}
}
double dms2rad(int deg, int min, double sec)
/* Convert degrees, minutes, and seconds of arc to radians */
{
double sign = 1.0;
if (deg < 0)
sign = -1.0;
if (deg == 0 && (min < 0 || sec < 0.0))
sign = -1.0;
return sign * ARCSEC2RAD * (60.0 * (60.0 * (double) abs(deg)
+ (double) abs(min)) + fabs(sec));
}
double hms2rad(int hour, int min, double sec)
/* Convert hours, minutes, and seconds of arc to radians */
{
return SEC2RAD * (60.0 * (60.0 * (double) hour + (double) min) + sec);
}
double mjd_sec_diff(int int1, double frac1, int int2, double frac2)
/* Return the difference in seconds between two MJDs (1 - 2) */
{
int idiff;
double fdiff;
idiff = (int1 - int2) * 86400;
fdiff = (frac1 - frac2) * 86400.0;
return (double) (idiff + fdiff);
}
double sphere_ang_diff(double ra1, double dec1, double ra2, double dec2)
/* Returns the angular difference in radians between two sets */
/* of RA and DEC (in radians). */
{
int i;
double d, vec1[3], vec2[3], s2, c2, cosb;
/* Convert coordinates from spherical to Cartesian */
cosb = cos(dec1);
vec1[0] = cos(ra1) * cosb;
vec1[1] = sin(ra1) * cosb;
vec1[2] = sin(dec1);
cosb = cos(dec2);
vec2[0] = cos(ra2) * cosb;
vec2[1] = sin(ra2) * cosb;
vec2[2] = sin(dec2);
/* Modulus squared of half the difference vector */
s2 = 0.0;
for (i = 0; i < 3; i++) {
d = vec1[i] - vec2[i];
s2 += d * d;
}
s2 /= 4.0;
/* Angle between the vectors */
c2 = 1.0 - s2;
return 2.0 * atan2(sqrt(s2), sqrt(DMAX(0.0, c2)));
}
#undef DMAX