1!-----------------------------------------------------------------------------
2!
3!  Copyright (C) 1997-2013 Krzysztof M. Gorski, Eric Hivon,
4!                          Benjamin D. Wandelt, Anthony J. Banday,
5!                          Matthias Bartelmann, Hans K. Eriksen,
6!                          Frode K. Hansen, Martin Reinecke
7!
8!
9!  This file is part of HEALPix.
10!
11!  HEALPix is free software; you can redistribute it and/or modify
12!  it under the terms of the GNU General Public License as published by
13!  the Free Software Foundation; either version 2 of the License, or
14!  (at your option) any later version.
15!
16!  HEALPix is distributed in the hope that it will be useful,
17!  but WITHOUT ANY WARRANTY; without even the implied warranty of
18!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19!  GNU General Public License for more details.
20!
21!  You should have received a copy of the GNU General Public License
22!  along with HEALPix; if not, write to the Free Software
23!  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24!
25!  For more information about HEALPix see http://healpix.sourceforge.net
26!
27!-----------------------------------------------------------------------------
28!--------------------------------------------------------------
29!
30! generic body of the subroutines convert_ring2nest_*_nd
31! to be inserted as is in pix_tools.f90
32!    edited 2009-04-03 to accept Nside>8192
33!--------------------------------------------------------------
34    !=======================================================================
35    !     makes the conversion RING to NEST
36    !=======================================================================
37    integer(kind=I4B), intent(in) :: nside
38    integer(kind=I4B) :: nd, j
39    integer(kind=I4B) :: ipn4, ipr4
40    integer(kind=I8B) :: ipn8, ipr8, npix
41    integer(kind=I4B),  dimension(:), allocatable :: mapping4
42    integer(kind=I8B),  dimension(:), allocatable :: mapping8
43    !=======================================================================
44    npix = nside2npix(nside)
45    call assert (npix>0,       code//": invalid Nside")
46
47    nd = size(map,2)
48
49    ! case where 2nd dimension=1
50    if (nd == 1) then
51       map1 => map(0:npix-1,1)
52       call convert_ring2nest(nside, map1)
53       return
54    endif
55
56    allocate(map_tmp(0:npix-1))
57
58    if (nside <= ns_max4) then
59       allocate(mapping4(0:npix-1))
60
61    ! do N->R mapping only once
62!$OMP parallel default(none) &
63!$OMP   shared(mapping4, npix, nside) private(ipr4, ipn4)
64!$OMP do schedule(dynamic,64)
65       do ipr4 = 0_i4b, npix-1
66          call ring2nest(nside, ipr4, ipn4)
67          mapping4(ipr4) = ipn4
68       enddo
69!$OMP end do
70!$OMP end parallel
71
72       ! convert maps one by one
73       do j = 1, nd
74          do ipr4 = 0_i4b, npix-1
75             map_tmp(mapping4(ipr4)) = map(ipr4,j)
76          enddo
77          map(0:npix-1, j) = map_tmp(0:npix-1)
78       enddo
79
80       deallocate(mapping4)
81
82    else
83
84       allocate(mapping8(0:npix-1))
85
86    ! do N->R mapping only once
87!$OMP parallel default(none) &
88!$OMP   shared(mapping8, npix, nside) private(ipr8, ipn8)
89!$OMP do schedule(dynamic,64)
90       do ipr8 = 0_i8b, npix-1
91          call ring2nest(nside, ipr8, ipn8)
92          mapping8(ipr8) = ipn8
93       enddo
94!$OMP end do
95!$OMP end parallel
96
97       ! convert maps one by one
98       do j = 1, nd
99          do ipr8 = 0_i8b, npix-1
100             map_tmp(mapping8(ipr8)) = map(ipr8,j)
101          enddo
102          map(0:npix-1, j) = map_tmp(0:npix-1)
103       enddo
104
105       deallocate(mapping8)
106
107    endif
108
109    deallocate(map_tmp)
110
111    return
112