lagsht_healpixhelper.h 10.3 KB
Newer Older
1 2
#ifndef LAGSHTHEALPIXHELPER_SEEN
#define LAGSHTHEALPIXHELPER_SEEN
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  This file is part of LagSHT.
 *
 *  LagSHT is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  LagSHT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libsharp; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 *  LagSHT is being developed at the Linear Accelerateur Laboratory (LAL)
 *  91898 ORSAY CEDEX - FRANCE
 *  main author: J.E Campagne
 */
26 27 28

//extracted from Healpix 3.20 src/C/subs/chealpix.c

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
/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  Copyright (C) 1997-2014  HEALPix collaboration


  HEALPix is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  HEALPix is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with HEALPix; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  For more information about HEALPix see http://healpix.sourceforge.net

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

HISTORY
 
 This is a software package for HEALPix, the Hierarchical, Equal Area, and 
 iso-Latitude Pixelation of the sphere, which was  originally devised 
 in early 1997 by Krzysztof M. Gorski at the Theoretical Astrophysics 
 Center in Copenhagen. Early development of the HEALPix concept and initial 
 implementation was made in the spring of 1997 in  collaborative work 
 of KMG, currently at JPL with Eric Hivon, currently at IAP. Benjamin D. Wandelt,
 currently at IAP, has contributed critically to the 
 further development of HEALPix and related mathematical methods.

 The  HEALPix-based  methods  contained  in  the  present  package,  their 
 implementation in Fortran90, IDL, C, C++ and java routines and codes, and documentation 
 were developed jointly by: 
 Anthony J. Banday (banday@mpa-garching.mpg.de),
 Matthias Bartelmann (mbartelmann@ita.uni-heidelberg.de),
 Hans Kristian Eriksen (h.k.k.eriksen@astro.uio.no),
 Krzysztof M. Gorski (krzysztof.m.gorski@jpl.nasa.gov),
 Frode K. Hansen (fhansen@mpa-garching.mpg.de), 
 Eric Hivon (hivon@iap.fr),
 William O'Mullane (womullan@skysrv.pha.jhu.edu),
 Martin Reinecke (martin@mpa-garching.mpg.de), and
 Benjamin D. Wandelt (bwandelt@iap.fr).

 The HEALPix package is available at this address: 
 http://healpix.sourceforge.net

COPYRIGHTS

 Copyright 1997 by Krzysztof M. Gorski
 for the HEALPix design and logo.
  All rights reserved.

 Copyright 1997 by Eric Hivon and Krzysztof M. Gorski
 for the HEALPix package version 0.80
  All rights reserved.

 Copyright 1998 by Benjamin Wandelt, Eric Hivon, and Krzysztof M. Gorski
 for the HotSpot package
  All rights reserved.

 Copyright 1998 by K.M. Gorski, Eric Hivon, and B.D. Wandelt
 for the HEALPix package version 0.90
  All rights reserved.

 Copyright 1998 by A.J. Banday and M.S. Bartelmann
 for the healgif package
  All rights reserved.

 Copyright 1999 by K.M. Gorski, B.D. Wandelt, E. Hivon, F. Hansen, A.J. Banday,
 and M. Bartelmann
 for the HEALPix package version 1.00
  All rights reserved.

 Copyright 2000 by K.M. Gorski, B.D. Wandelt, E. Hivon, F. Hansen, A.J. Banday,
 and M. Bartelmann
 for the HEALPix package version 1.10
  All rights reserved.

 Copyright 2003 by K.M. Gorski, E. Hivon, M. Reinecke, A.J. Banday,
 B.D. Wandelt, and M. Bartelmann
 for the HEALPix package version 1.20
  All rights reserved.

 Copyright 2003 by E. Hivon, Kenneth M. Ganga (ganga@cdf.in2p3.fr), Reza Ansari (ansari@lal.in2p3.fr)
 for the HEALPix C package
  All rights reserved.

 Copyright 2005 by E. Hivon, M. Reinecke, W. O'Mullane, H.K. Eriksen, K.M. Gorski, A.J. Banday
 for the HEALPix package version 2.00
  All rights reserved.

 Copyright 2005 by  H.K. Eriksen, Snorre Boasson (snorre.boasson@ntnu.no), M. Reinecke, E. Hivon
 for the MPI implementation of HEALPix F90 alm routines
  All rights reserved.

 Copyright 2008 by E. Hivon, M. Reinecke, W. O'Mullane, H.K. Eriksen, K.M. Gorski, A.J. Banday, E. Joliet
 for the HEALPix package version 2.10
  All rights reserved.

 Copyright 2008 by G. Rocha
 for the ngsims package
  All rights reserved.

 Copyright 2008 by J. P. Leahy
 for the ximview package
  All rights reserved.

 Copyright 2008 by D. Larson
 for the alice package
  All rights reserved.

 Copyright 2010 by E. Hivon, M. Reinecke, W. O'Mullane, E. Joliet, K.M. Gorski, A.J. Banday
 for the HEALPix package version 2.20
  All rights reserved.

 Copyright 2012 by E. Hivon, M. Reinecke, W. O'Mullane, E. Joliet, K.M. Gorski, A.J. Banday
 for the HEALPix package version 3.00
  All rights reserved.

 Copyright 2013 by E. Hivon, M. Reinecke, W. O'Mullane, E. Joliet, K.M. Gorski, A.J. Banday
 for the HEALPix package version 3.10
  All rights reserved.

 Copyright 2014 by E. Hivon, M. Reinecke, W. O'Mullane, E. Joliet, K.M. Gorski, A.J. Banday
 for the HEALPix package version 3.20
  All rights reserved.

 Copyright 2013 by A. Zonca, C. Rosset
 for the healpy package version 1.5.0
  All rights reserved.

 Copyright 2014 by A. Zonca, L. Singer, C. Rosset
 for the healpy package version 1.8.1
  All rights reserved.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

ACKNOWLEDGMENTS

We thank the following people for their contributions and/or suggestions, which
significantly helped us to improve HEALPix throughout its several releases

 Reza Ansari,
 Marc Ashdown,
 Carlo Baccigalupi,
 Tristan Beau,
 Snorre Boasson,
 Michela Botti,
 Stephane Colombi,
 Giancarlo de Gasperis,
 Herve Dole,
 Olivier Dore,
 Hans Kristian Eriksen,
 Ken Ganga,
 Giovanna Giardino,
 Jean-Christophe Hamilton,
 Andrew Jaffe
 Alex Kim,
 Ted Kisner,
 Guy Le Meur,
 Anthony Lewis,
 Davide Maino,
 Serge Monkewitz,
 Paolo Natoli,
 Clive Page,
 William Pence,
 Nicolas Ponthieu,
 Torsti Poutanen,
 Simon Prunet,
 Benoit Revenu,
 Rosa Ruiloba,
 David N. Spergel,
 Radek Stompor,
 Giovanna Tinetti.
*/

210 211
namespace LagSHT {

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  struct HEALPIX {
    public:
    //coonstantes defined at the end of the file
    static const double pi;
    static const double twopi;
    static const double inv_halfpi;
    static const double halfpi;
    static const double twothird;
    
    static int isqrt(int v)
    { return (int)(sqrt(v+0.5)); }
    
    /*! Returns the remainder of the division \a v1/v2.
      The result is non-negative.
      \a v1 can be positive or negative; \a v2 must be positive. */
    static int imodulo (int v1, int v2)
    { int v=v1%v2; return (v>=0) ? v : v+v2; }
    
    
    /*! Returns the remainder of the division \a v1/v2.
      The result is non-negative.
      \a v1 can be positive or negative; \a v2 must be positive. */
    static double fmodulo (double v1, double v2)
235
    {
236 237 238 239
      if (v1>=0)
	return (v1<v2) ? v1 : fmod(v1,v2);
      double tmp=fmod(v1,v2)+v2;
      return (tmp==v2) ? 0. : tmp;
240
    }
241 242 243
    
    //From Healpix 3.20
    static int ang2pix_ring_z_phi (long nside_, double z, double phi)
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
      double za = fabs(z);
      double tt = fmodulo(phi,twopi) * inv_halfpi; /* in [0,4) */
      
      if (za<=twothird) /* Equatorial region */
	{
	  double temp1 = nside_*(0.5+tt);
	  double temp2 = nside_*z*0.75;
	  int jp = (int)(temp1-temp2); /* index of  ascending edge line */
	  int jm = (int)(temp1+temp2); /* index of descending edge line */
	  
	  /* ring number counted from z=2/3 */
	  int ir = nside_ + 1 + jp - jm; /* in {1,2n+1} */
	  int kshift = 1-(ir&1); /* kshift=1 if ir even, 0 otherwise */
	  
	  int ip = (jp+jm-nside_+kshift+1)/2; /* in {0,4n-1} */
	  ip = imodulo(ip,4*nside_);

	  return nside_*(nside_-1)*2 + (ir-1)*4*nside_ + ip;
	}
      else  /* North & South polar caps */
	{
	  double tp = tt-(int)(tt);
	  double tmp = nside_*sqrt(3*(1-za));

	  int jp = (int)(tp*tmp); /* increasing edge line index */
	  int jm = (int)((1.0-tp)*tmp); /* decreasing edge line index */

	  int ir = jp+jm+1; /* ring number counted from the closest pole */
	  int ip = (int)(tt*ir); /* in {0,4*ir-1} */
	  ip = imodulo(ip,4*ir);

	  if (z>0)
	    return 2*ir*(ir-1) + ip;
	  else
	    return 12*nside_*nside_ - 2*ir*(ir+1) + ip;
	}
281 282
    }

283
    static void pix2ang_ring_z_phi (int nside_, int pix, double *z, double *phi)
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
      long ncap_=nside_*(nside_-1)*2;
      long npix_=12*nside_*nside_;
      double fact2_  = 4./npix_;
      if (pix<ncap_) /* North Polar cap */
	{
	  int iring = (int)(0.5*(1+isqrt(1+2*pix))); /* 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) * pi/nl2;
	}
      else /* South Polar cap */
	{
	  int ip = npix_ - pix;
	  int iring = (int)(0.5*(1+isqrt(2*ip-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;
	}
318 319
    }

320 321 322 323 324 325 326 327 328
  };

  
  const double HEALPIX::pi = 3.141592653589793238462643383279502884197;
  const double HEALPIX::twopi = 6.283185307179586476925286766559005768394;
  const double HEALPIX::inv_halfpi = 0.6366197723675813430755350534900574;
  const double HEALPIX::halfpi = 1.570796326794896619231321691639751442099;
  const double HEALPIX::twothird = 2.0/3.0;
  
329 330 331

} //end namespace
#endif // LAGSHTHEALPIXHELPER_SEEN