#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 $ */ /* double vector allocation */ void hc_dvecalloc(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(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(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(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(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(float **x,int n,char *message) { *x = (float *)realloc(*x,sizeof(float)*(size_t)n); if(!(*x)) HC_MEMERROR(message); } /* double vector reallocation */ void hc_dvecrealloc(double **x,int n,char *message) { *x = (double *)realloc(*x,sizeof(double)*(size_t)n); if(!(*x)) HC_MEMERROR(message); } /* general version */ void hc_vecrealloc(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_svec_rms_diff(float *a,float *b,int n) { int i; float sum=0.0,tmp; for(i=0;ilmax,g1->lmax); lmaxg = MIN(20,lmaxg); switch(mode){ case 0: if(verbose) fprintf(stderr,"hc_compute_correlation: computing 1...%i\n",lmaxg); c[0] = sh_correlation_per_degree(g1,g2,1,lmaxg); case 1: /* 1...20 and 4..9 correlations */ 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; 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(float lon, float lat, float *polar_vec,float *cart_vec) { double polar_base[9]; float theta,phi; theta = LAT2THETA((double)lat); phi = LON2PHI((double)lon); calc_polar_base_at_theta_phi(theta,phi,polar_base); lonlatpv2cv_with_base(polar_vec,polar_base,cart_vec); } void lonlatpv2cv_with_base(float *polar_vec, double *polar_base, float *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(float theta, float phi, double *polar_base) { double cp,sp,ct,st; // base vecs ct=cos((double)theta);cp=cos((double)phi); st=sin((double)theta);sp=sin((double)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