libcov.c 16.1 KB
Newer Older
Matthieu Tristram's avatar
Matthieu Tristram committed
1 2
#include "libcov.h"

Matthieu Tristram's avatar
Matthieu Tristram committed
3

Matthieu Tristram's avatar
Matthieu Tristram committed
4 5 6 7 8 9
//problem of precision wrt python...
void build_Wll( int nl, int npix, double* El, double* Pl, double* Wll)
{
  int64_t npixtot = npix*npix;
  
  memset( Wll, 0., (nl*nl) * sizeof(double));
Matthieu Tristram's avatar
Matthieu Tristram committed
10

Matthieu Tristram's avatar
Matthieu Tristram committed
11 12 13 14 15 16 17 18
#pragma omp parallel default(none) shared(nl, npixtot, El, Pl, Wll)
  {
#pragma omp for schedule(dynamic)
    for( int l1=0; l1<nl; l1++) {
      for( int l2=0; l2<nl; l2++) {
	
	for( int p=0; p<npixtot; p++)
	  Wll[l1*nl+l2] += El[l1*npixtot+p]*Pl[l2*npixtot+ p];
Matthieu Tristram's avatar
Matthieu Tristram committed
19
	
Matthieu Tristram's avatar
Matthieu Tristram committed
20 21 22 23
      } //loop l2
    } //loop l1
    
  } //end omp parallel
Matthieu Tristram's avatar
Matthieu Tristram committed
24

Matthieu Tristram's avatar
Matthieu Tristram committed
25 26 27 28 29 30 31 32 33 34
//   fprintf( stdout, "E[13] = ");
//   for( int p=0; p<10; p++) fprintf( stdout, "%f\t", El[13*npixtot+p]);
//   fprintf( stdout, "\n");
//   fprintf( stdout, "P[4] = ");
//   for( int p=0; p<10; p++) fprintf( stdout, "%f\t", Pl[4*npixtot+p]);
//   fprintf( stdout, "\n");
//   fprintf( stdout, "E[13]*P[4] = ");
//   for( int p=0; p<10; p++) fprintf( stdout, "%f\t", El[13*npixtot+p]*Pl[4*npixtot+p]);
//   fprintf( stdout, "\n");
//   fprintf( stdout, "Wll[%d,%d]=%f\n", 13, 4, Wll[13*nl+4]);
Matthieu Tristram's avatar
Matthieu Tristram committed
35
  
Matthieu Tristram's avatar
Matthieu Tristram committed
36
}
Matthieu Tristram's avatar
Matthieu Tristram committed
37 38


39
void build_dSdC( int nside, int nstokes, int npix, int nbin, long *ispec, long *ellbins, long *ipix, double *bl, double* dSdC)
Matthieu Tristram's avatar
Matthieu Tristram committed
40
{
41
  const int lmax=ellbins[nbin]-1;
Matthieu Tristram's avatar
Matthieu Tristram committed
42
  const int lmax1 = lmax+1;
43 44 45
  const int ns=3, nspecall = 6;
  const int nspec = ispec2nspec(ispec);
//   const int nspec = nstokes2nspec(nstokes);
46
  const int64_t npixtot = npix*nstokes;
Matthieu Tristram's avatar
Matthieu Tristram committed
47

Matthieu Tristram's avatar
Matthieu Tristram committed
48 49 50 51
//   fprintf( stdout, "lmax=%d\n", lmax);
//   fprintf( stdout, "npix=%d\n", npix);
//   fprintf( stdout, "nstokes=%d\n", nstokes);
//   fprintf( stdout, "nspec=%d\n", nspec);
Matthieu Tristram's avatar
Matthieu Tristram committed
52

53
  int64_t ntot = nspec*nbin*npixtot*npixtot;
Matthieu Tristram's avatar
Matthieu Tristram committed
54
//   fprintf( stdout, "memset (%d,%d,%d,%d = %lld)...\n", nspec,nbin,nstokes,npix,ntot);
55
  memset( dSdC, 0., ntot * sizeof(double));
Matthieu Tristram's avatar
Matthieu Tristram committed
56
//   fprintf( stdout, "ispec = (%d,%d,%d,%d,%d,%d)\n", ispec[0],ispec[1],ispec[2],ispec[3],ispec[4],ispec[5]);
57

58
#pragma omp parallel default(none) shared(stdout,nbin,nside,npix,nstokes,dSdC,ipix,bl,ellbins,ispec)
Matthieu Tristram's avatar
Matthieu Tristram committed
59 60 61 62 63 64 65 66 67 68
  {
    int s=0;
    double vr[3], vc[3];
    double **dSdCpix=NULL;
    dSdCpix = (double **) malloc( nspecall*lmax1*sizeof(double));
    for( int il=0; il<nspecall*lmax1; il++) {
      dSdCpix[il] = NULL;
      dSdCpix[il] = (double *) calloc( ns*ns, sizeof(double));
      if( dSdCpix[il] == NULL) EXIT_INFO( -1, "Problem allocation dSdCpix (l=%d)...\n", il);
    }
69
    
70 71
    int sI = 0, sQ = 1, sU = 2;
    if( nstokes == 2) { sQ = 0; sU = 1; }
Matthieu Tristram's avatar
Matthieu Tristram committed
72 73 74 75 76 77 78 79 80
    
#pragma omp for schedule(dynamic)
    /* loop on local pix to build (3x3) blocks */
    for( int cpix=0; cpix<npix; cpix++) {
      pix2vec_ring(nside, ipix[cpix], vc);

      for( int rpix=0; rpix<npix; rpix++) {
	pix2vec_ring(nside, ipix[rpix], vr);

81
	QML_compute_dSdC( vr, vc, lmax, ispec, dSdCpix);
Matthieu Tristram's avatar
Matthieu Tristram committed
82

83 84 85 86
	for( int ib=0; ib<nbin; ib++) {
// 	  int l=ellbins[ib];
 	  for( int l=ellbins[ib]; l<=ellbins[ib+1]-1; l++) {
	    s=0;
Matthieu Tristram's avatar
Matthieu Tristram committed
87

88 89
	    if( ispec[0] == 1) {
	      dSdC(s*nbin+ib,sI*npix+cpix,sI*npix+rpix) += dSdCpix[0*lmax1+l][0*ns+0] * bl[0*lmax1+l]*bl[0*lmax1+l];  //TT on II
90 91
	      s++;
	    }
Matthieu Tristram's avatar
Matthieu Tristram committed
92
	    
93
	    //EE-BB
94 95 96 97 98
	    if( ispec[1] == 1 || ispec[2] == 1) {
	      dSdC(s*nbin+ib,sQ*npix+cpix,sQ*npix+rpix) += dSdCpix[1*lmax1+l][1*ns+1] * bl[1*lmax1+l]*bl[1*lmax1+l];  //EE on QQ
	      dSdC(s*nbin+ib,sU*npix+cpix,sQ*npix+rpix) += dSdCpix[1*lmax1+l][2*ns+1] * bl[1*lmax1+l]*bl[1*lmax1+l];  //EE on QU
	      dSdC(s*nbin+ib,sQ*npix+cpix,sU*npix+rpix) += dSdCpix[1*lmax1+l][1*ns+2] * bl[1*lmax1+l]*bl[1*lmax1+l];  //EE on UQ
	      dSdC(s*nbin+ib,sU*npix+cpix,sU*npix+rpix) += dSdCpix[1*lmax1+l][2*ns+2] * bl[1*lmax1+l]*bl[1*lmax1+l];  //EE on UU
99 100
	      s++;
	      
101 102 103 104
	      dSdC(s*nbin+ib,sQ*npix+cpix,sQ*npix+rpix) += dSdCpix[2*lmax1+l][1*ns+1] * bl[2*lmax1+l]*bl[2*lmax1+l];  //BB on QQ
	      dSdC(s*nbin+ib,sU*npix+cpix,sQ*npix+rpix) += dSdCpix[2*lmax1+l][2*ns+1] * bl[2*lmax1+l]*bl[2*lmax1+l];  //BB on QU
	      dSdC(s*nbin+ib,sQ*npix+cpix,sU*npix+rpix) += dSdCpix[2*lmax1+l][1*ns+2] * bl[2*lmax1+l]*bl[2*lmax1+l];  //BB on UQ
	      dSdC(s*nbin+ib,sU*npix+cpix,sU*npix+rpix) += dSdCpix[2*lmax1+l][2*ns+2] * bl[2*lmax1+l]*bl[2*lmax1+l];  //BB on UU
105 106 107 108
	      s++;
	    }
	    
	    //TE
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	    if( ispec[3] == 1) {
	      dSdC(s*nbin+ib,sI*npix+cpix,sQ*npix+rpix) += dSdCpix[3*lmax1+l][0*ns+1] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TE on IQ
	      dSdC(s*nbin+ib,sI*npix+cpix,sU*npix+rpix) += dSdCpix[3*lmax1+l][0*ns+2] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TE on IU
	      dSdC(s*nbin+ib,sQ*npix+cpix,sI*npix+rpix) += dSdCpix[3*lmax1+l][1*ns+0] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TE on QI
	      dSdC(s*nbin+ib,sU*npix+cpix,sI*npix+rpix) += dSdCpix[3*lmax1+l][2*ns+0] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TE on UI
	      s++;
	    }

	    //TB
	    if( ispec[4] == 1) {
	      dSdC(s*nbin+ib,sI*npix+cpix,sQ*npix+rpix) += dSdCpix[4*lmax1+l][0*ns+1] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TB on IQ
	      dSdC(s*nbin+ib,sI*npix+cpix,sU*npix+rpix) += dSdCpix[4*lmax1+l][0*ns+2] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TB on IU
	      dSdC(s*nbin+ib,sQ*npix+cpix,sI*npix+rpix) += dSdCpix[4*lmax1+l][1*ns+0] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TB on QI
	      dSdC(s*nbin+ib,sU*npix+cpix,sI*npix+rpix) += dSdCpix[4*lmax1+l][2*ns+0] * bl[3*lmax1+l]*bl[3*lmax1+l];  //TB on UI
	      s++;
	    }

	    //EB
	    if( ispec[5] == 1) {
	      dSdC(s*nbin+ib,sQ*npix+cpix,sQ*npix+rpix) += dSdCpix[5*lmax1+l][1*ns+1] * bl[1*lmax1+l]*bl[2*lmax1+l];  //EB on QQ
	      dSdC(s*nbin+ib,sQ*npix+cpix,sU*npix+rpix) += dSdCpix[5*lmax1+l][1*ns+2] * bl[1*lmax1+l]*bl[2*lmax1+l];  //EB on QU
	      dSdC(s*nbin+ib,sU*npix+cpix,sQ*npix+rpix) += dSdCpix[5*lmax1+l][2*ns+1] * bl[1*lmax1+l]*bl[2*lmax1+l];  //EB on UQ
	      dSdC(s*nbin+ib,sU*npix+cpix,sU*npix+rpix) += dSdCpix[5*lmax1+l][2*ns+2] * bl[1*lmax1+l]*bl[2*lmax1+l];  //EB on UU
	      s++;
133
	    }
134

135
 	  } /* end loop l */
Matthieu Tristram's avatar
Matthieu Tristram committed
136
	  
137
	} /* end loop bins */
Matthieu Tristram's avatar
Matthieu Tristram committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	
      } /* end loop rpix */
    } /* end loop cpix */

    /* free */
    for( int l=0; l<nspecall*lmax1; l++) free( dSdCpix[l]);
    free( dSdCpix);
  } //end omp parallel


//   return( dSdC);
}






/* compute 3x3 matrix correlation matrix for couple (ipix,jpix) */
157
void QML_compute_dSdC( double *vr, double *vc, int lmax, long *ispec, double **dSdCpix)
Matthieu Tristram's avatar
Matthieu Tristram committed
158 159 160 161 162 163
{
  double *pl=NULL, *d20=NULL, *d2p2=NULL, *d2m2=NULL;
  double cos2aij, sin2aij, cos2aji, sin2aji;
  double norm, P02, Q22, R22;
  double cos_chi;
  int l, ns=3;
164
  int spin0=0, spin2=0, spin02=0;
Matthieu Tristram's avatar
Matthieu Tristram committed
165 166

  /* alloc vectors */
167 168 169 170 171 172 173 174
  if( ispec[0] == 1) spin0 = 1;
  if( ispec[1] == 1 || ispec[2] == 1 || ispec[5] == 1) spin2 = 1;
  if( ispec[3] == 1 || ispec[4] == 1) spin02 = 1;
  
  if( spin0 ) pl   = (double *) calloc( (lmax+1), sizeof(double));
  if( spin2 ) d2p2 = (double *) calloc( (lmax+1), sizeof(double));
  if( spin2 ) d2m2 = (double *) calloc( (lmax+1), sizeof(double));
  if( spin02) d20  = (double *) calloc( (lmax+1), sizeof(double));
Matthieu Tristram's avatar
Matthieu Tristram committed
175 176 177 178 179 180 181 182
  
  /* generate d_ss'(l) */
  cos_chi = QML_scal_prod( vc, vr);
/*   if( cos_chi > 1 || cos_chi < -1) INFO( "%i angle[%d(%d),%d(%d)] = %e\n", rank, rpix_global, rpix_global/nstokes, cpix_global, cpix_global/nstokes, fabs(cos_chi)-1); */
  if( cos_chi >  1) cos_chi =  1.;
  if( cos_chi < -1) cos_chi = -1.;

/* legendre( cos_chi, 0, lmax, pl); */
183 184 185 186
  if( spin0 ) dlss( cos_chi, 0,  0, lmax,   pl);
  if( spin2 ) dlss( cos_chi, 2,  2, lmax, d2p2);
  if( spin2 ) dlss( cos_chi, 2, -2, lmax, d2m2);
  if( spin02) dlss( cos_chi, 2,  0, lmax,  d20);
Matthieu Tristram's avatar
Matthieu Tristram committed
187 188

  /* generate rotation angles */
189 190
  if( spin2 || spin02) polrotangle( vr, vc, &cos2aij, &sin2aij);
  if( spin2 || spin02) polrotangle( vc, vr, &cos2aji, &sin2aji);
Matthieu Tristram's avatar
Matthieu Tristram committed
191 192 193 194 195
  
  /* loop on l */
  for( l=2; l<=lmax; l++) {
    norm = (double)(2*l+1) / (4.*M_PI);

196
    if( ispec[0] == 1)
Matthieu Tristram's avatar
Matthieu Tristram committed
197 198
      dSdCpix[0*(lmax+1)+l][0*ns + 0] = norm * pl[l] ;  //TT on II

199
    if( ispec[1] == 1 || ispec[2] == 1) {
200 201
      Q22 = norm * ( d2p2[l] + d2m2[l] )/2.;
      R22 = norm * ( d2p2[l] - d2m2[l] )/2.;
Matthieu Tristram's avatar
Matthieu Tristram committed
202
      
203 204 205 206
      dSdCpix[1*(lmax+1)+l][1*ns + 1] = ( cos2aij*cos2aji*Q22 + sin2aij*sin2aji*R22);  //EE on QQ
      dSdCpix[1*(lmax+1)+l][2*ns + 1] = (-cos2aij*sin2aji*Q22 + sin2aij*cos2aji*R22);  //EE on QU
      dSdCpix[1*(lmax+1)+l][1*ns + 2] = (-sin2aij*cos2aji*Q22 + cos2aij*sin2aji*R22);  //EE on UQ
      dSdCpix[1*(lmax+1)+l][2*ns + 2] = ( sin2aij*sin2aji*Q22 + cos2aij*cos2aji*R22);  //EE on UU
Matthieu Tristram's avatar
Matthieu Tristram committed
207
      
208 209 210 211
      dSdCpix[2*(lmax+1)+l][1*ns + 1] = ( cos2aij*cos2aji*R22 + sin2aij*sin2aji*Q22);  //BB on QQ
      dSdCpix[2*(lmax+1)+l][2*ns + 1] = (-cos2aij*sin2aji*R22 + sin2aij*cos2aji*Q22);  //BB on QU
      dSdCpix[2*(lmax+1)+l][1*ns + 2] = (-sin2aij*cos2aji*R22 + cos2aij*sin2aji*Q22);  //BB on UQ
      dSdCpix[2*(lmax+1)+l][2*ns + 2] = ( sin2aij*sin2aji*R22 + cos2aij*cos2aji*Q22);  //BB on UU
Matthieu Tristram's avatar
Matthieu Tristram committed
212 213
    }
    
214
    if( ispec[3] == 1) {
Matthieu Tristram's avatar
Matthieu Tristram committed
215
      P02 = -d20[l];
216 217 218 219
      dSdCpix[3*(lmax+1)+l][1*ns + 0] =   P02*cos2aji ;  //TE on IQ
      dSdCpix[3*(lmax+1)+l][2*ns + 0] = - P02*sin2aji ;  //TE on IU
      dSdCpix[3*(lmax+1)+l][0*ns + 1] =   P02*cos2aij ;  //TE on QI
      dSdCpix[3*(lmax+1)+l][0*ns + 2] = - P02*sin2aij ;  //TE on UI
Matthieu Tristram's avatar
Matthieu Tristram committed
220 221
    }

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    if( ispec[4] == 1) {
      P02 = -d20[l];
      dSdCpix[4*(lmax+1)+l][1*ns + 0] = P02*sin2aji ;  //TB on IQ
      dSdCpix[4*(lmax+1)+l][2*ns + 0] = P02*cos2aji ;  //TB on IU
      dSdCpix[4*(lmax+1)+l][0*ns + 1] = P02*sin2aij ;  //TB on QI
      dSdCpix[4*(lmax+1)+l][0*ns + 2] = P02*cos2aij ;  //TB on UI
    }

    if( ispec[5] == 1) {
      Q22 = norm * ( d2p2[l] + d2m2[l] )/2.;
      R22 = norm * ( d2p2[l] - d2m2[l] )/2.;      
      dSdCpix[5*(lmax+1)+l][1*ns + 1] = ( cos2aij*sin2aji*Q22 - sin2aij*cos2aji*R22 + sin2aij*cos2aji*Q22 - cos2aij*sin2aji*R22);  //EB on QQ
      dSdCpix[5*(lmax+1)+l][1*ns + 2] = ( cos2aij*cos2aji*Q22 + sin2aij*sin2aji*R22 - sin2aij*sin2aji*Q22 - cos2aij*cos2aji*R22);  //EB on QU
      dSdCpix[5*(lmax+1)+l][2*ns + 1] = ( cos2aij*cos2aji*Q22 + sin2aij*sin2aji*R22 - sin2aij*sin2aji*Q22 - cos2aij*cos2aji*R22);  //EB on UQ
      dSdCpix[5*(lmax+1)+l][2*ns + 2] = (-cos2aij*sin2aji*Q22 + sin2aij*cos2aji*R22 - sin2aij*cos2aji*Q22 + cos2aij*sin2aji*R22);  //EB on UU
    }

Matthieu Tristram's avatar
Matthieu Tristram committed
239 240
  }

241 242 243 244
  if( spin0 ) free( pl  );
  if( spin2 ) free( d2p2);
  if( spin2 ) free( d2m2);
  if( spin02) free( d20 );
Matthieu Tristram's avatar
Matthieu Tristram committed
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
}






/*************************************************************************/
/* Polar Rotation Angle                                                  */
/*************************************************************************/
int polrotangle( double *ri, double *rj, double *cos2a, double *sin2a)
{
  int i;
  double norm2, cosa, sina;
  double rij[3], ris[3], rijris[3];
  double z[3] = {0.,0.,1.};

  /* compute ri x rj */
  QML_cross_prod( ri, rj, rij);
  norm2 = QML_scal_prod(rij, rij);
  /* pixels are either identical or on diametrically opposite sides of the sky */
  if( norm2 <= 1e-15) {
    *cos2a = 1.;
    *sin2a = 0.;  
    return(OK);
  }
  for( i=0; i<3; i++) rij[i] /= sqrt(norm2);

  /* compute z x ri */
  QML_cross_prod( z, ri, ris);
  norm2 = QML_scal_prod( ris, ris);
  if( norm2 <= 1e-15) {
    /* pixel i is at the pole */
    *cos2a = 1.;
    *sin2a = 0.;  
    return(OK);
  }
  for( i=0; i<3; i++) ris[i] /= sqrt(norm2);

  /* compute cosa, sina */
  QML_cross_prod( rij, ris, rijris);

  cosa = QML_scal_prod( rij, ris);
  sina = QML_scal_prod( rijris, ri);

  /* compute cos2a, sin2a */
  *cos2a = 2.*cosa*cosa - 1.;
  *sin2a = 2.*cosa*sina;
  
  return(OK);
}

void QML_cross_prod( double *v, double *w, double *z)
{
  z[0] = v[1]*w[2] - v[2]*w[1];
  z[1] = v[2]*w[0] - v[0]*w[2];
  z[2] = v[0]*w[1] - v[1]*w[0];
}

double QML_scal_prod( double *v, double *w)
{
  double s;

  s = v[0]*w[0] + v[1]*w[1] + v[2]*w[2];

  return( s);
  
}
/*************************************************************************/




/*************************************************************************/
/* Compute Legendre                                                      */
/*************************************************************************/
void legendre( double X, int m, int lmax, double *P)
{
  int l, i;
  
  if( (m > lmax) || (m < 0) )
    EXIT_INFO( -1, "Wrong m value : %d\n", m);
  
  /* compute Pmm(X) */
  P[m] = 1.0;
  for( i=1; i<=m; i++) {
    P[m] = -(2.*i+1) * sqrt(1.-X*X) * P[m];
  }

  /* compute Pmm1(X) */
  P[m+1] = X*(double)(2*m+1)*P[m];
  
  /* Compute Plm(X) */
  for( l=m+2; l<=lmax; l++)
    P[l] = ( (double)(2*l-1)*X*P[l-1] - (double)(l+m-1)*P[l-2] ) / (double)(l-m);

}
/*************************************************************************/






/*************************************************************************/
/* Compute d_l^ss'                                                       */
/*************************************************************************/
void dlss( double X, int s1, int s2, int lmax, double *d)
{
  int l, l1, sign;
  double rhoSSL, rhoSSL1;
  
  if( s1 < abs(s2) )
    EXIT_INFO( -1, "Wrong s values (requested s<abs(s')) : s=%d s'=%d\n", s1, s2);

  /* init l=s1 */
  sign = ( (s1+s2) & 1 ) ? -1 : 1;
  d[s1] = sign / pow(2.,s1) * sqrt( fact(2*s1) / fact(s1+s2) / fact(s1-s2) ) * pow( (1.+X), (s1+s2)/2.) * pow( (1.-X), (s1-s2)/2.);  

  /* init l=s1+1 */
  l1 = s1+1;
  rhoSSL1 = sqrt( ((double)l1*l1 - (double)s1*s1) * ((double)l1*l1 - (double)s2*s2) ) / (double)l1;
  d[s1+1] = (double)(2*s1+1)*(X-(double)s2/(double)(s1+1)) * d[s1] / rhoSSL1;

  /* Compute dss'_l(X) */
  for( l=s1+1; l<lmax; l++) {
    l1 = l+1;
    rhoSSL  = sqrt( ((double)l*l   - (double)s1*s1) * ((double)l*l   - (double)s2*s2) ) / (double)l;
    rhoSSL1 = sqrt( ((double)l1*l1 - (double)s1*s1) * ((double)l1*l1 - (double)s2*s2) ) / (double)l1;
    d[l+1] = ( (double)(2*l+1)*(X-(double)s1*s2/(double)l/(double)l1)*d[l] - rhoSSL*d[l-1] ) / rhoSSL1;    
  }

}

double fact( int n)
{
     double f = 1.;
     while( n > 1) f *= (double)(n--);
     return f;
}
/*************************************************************************/










/*************************************************************************/
397 398 399 400
int ispec2nspec( long *ispec)
{
  int nspec=0;

401 402 403 404 405 406 407 408 409 410 411 412 413
  //force TT & EE for TE
  if( ispec[3] == 1) ispec[0] = ispec[1] = 1;

  //force TT & BB for TB
  if( ispec[4] == 1) ispec[0] = ispec[2] = 1;

  //force EE & BB
  if( ispec[1] == 1) ispec[1] = ispec[2] = 1;
  if( ispec[2] == 1) ispec[1] = ispec[2] = 1;  

  //force EE & BB for EB
  if( ispec[5] == 1) ispec[1] = ispec[2] = 1;

414 415 416 417 418 419
  for( int i=0; i<6; i++)
    if( ispec[i]) nspec++;

  return( nspec);
}

Matthieu Tristram's avatar
Matthieu Tristram committed
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
int nstokes2nspec( int nstokes)
{
  int nspec=0;
  switch( nstokes) {
  case 1:
    nspec = 1;
    break;
  case 2:
    nspec = 2;
    break;
  case 3:
    nspec = 4;
    break;
  }
  
  return( nspec);
}
/*************************************************************************/



/*************************************************************************/
/* Extract from Healpix                                                  */
/*************************************************************************/

void pix2ang_ring(long nside, long ipix, double *theta, double *phi)
  {
  double z;
  pix2ang_ring_z_phi (nside,ipix,&z,phi);
  *theta=acos(z);
  }


void pix2vec_ring(long nside, long ipix, double *vec)
  {
  double z, phi;
  pix2ang_ring_z_phi (nside,ipix,&z,&phi);
  double stheta=sqrt((1.-z)*(1.+z));
  vec[0]=stheta*cos(phi);
  vec[1]=stheta*sin(phi);
  vec[2]=z;
  }

void pix2ang_ring_z_phi(long nside_, long pix, double *z, double *phi)
  {
  long ncap_=nside_*(nside_-1)*2;
  long npix_=12*nside_*nside_;
  double fact2_ = 4./npix_;
  if (pix<ncap_) /* North Polar cap */
    {
    int iring = (1+isqrt(1+2*pix))>>1; /* counted from North pole */
    int iphi  = (pix+1) - 2*iring*(iring-1);

    *z = 1.0 - (iring*iring)*fact2_;
    *phi = (iphi-0.5) * halfpi/iring;
    }
  else if (pix<(npix_-ncap_)) /* Equatorial region */
    {
    double fact1_  = (nside_<<1)*fact2_;
    int ip  = pix - ncap_;
    int iring = ip/(4*nside_) + nside_; /* counted from North pole */
    int iphi  = ip%(4*nside_) + 1;
    /* 1 if iring+nside is odd, 1/2 otherwise */
    double fodd = ((iring+nside_)&1) ? 1 : 0.5;

    int nl2 = 2*nside_;
    *z = (nl2-iring)*fact1_;
    *phi = (iphi-fodd) * M_PI/nl2;
    }
  else /* South Polar cap */
    {
    int ip = npix_ - pix;
    int iring = (1+isqrt(2*ip-1))>>1; /* counted from South pole */
    int iphi  = 4*iring + 1 - (ip - 2*iring*(iring-1));

    *z = -1.0 + (iring*iring)*fact2_;
    *phi = (iphi-0.5) * halfpi/iring;
    }
  }

int isqrt(int v)
  { return (int)(sqrt(v+0.5)); }
/*************************************************************************/