c     ------------------------------------------------------------
c
c     Computation of the extinction: SWG-RD-03 (Drimmel, 2002)
c
c     Fortran version developed in Barcelona Univ. (January 2003)
c     This version includes the COBE Fortran routines for
c     computing the pixel number
c
c     The last grid of the Orion Arm has been rebuit at a higher
c     resolution to get rid of the artifacts that were present at
c     around l=75
c
c     ------------------------------------------------------------

      implicit double precision (a-h,o-z)

      integer npix
      real*8 l,ll
      parameter(zsun=0.015d0, xsun=-8.d0, pi=3.141592654d0,
     *          npix=393216, deg_rad=pi/180.d0)
      character afact*1
      dimension a(2,2,2),delta(3),n(3),fac(3)

c     3D Grids from SWG-RD-02
      real*4 avgrid(151,151,51), avloc(101,201,51)

c     3D Grids from SWG-RD-03

c     Large scale 
      real*4 avdisk(151,151,51), avspir(151,151,51),
     *avori(76,151,51)

c     Local scale
      real*4 avori2(101,201,51), avdloc(31,31,51)

c     Rescaling factors
      real*4 glonold(npix), glatold(npix), rf(npix)
      integer ncompold(npix),pixnum(npix)
      real*4 glon(npix), glat(npix), rfac(npix)
      integer ncomp(npix)

c     ncomp=1 ---> disk
c     ncomp=2 ---> spiral
c     ncomp=3 ---> orion


c     Reading the 3D grids:

      write(*,*) 'Do you want to use rescaling factors (SWG-RD-03)? (Y/N
     *)'
      read(*,*) afact

      if((afact.eq.'n').or.(afact.eq.'N')) then

	open(1,file='avgrid.dat',status='old',form='unformatted')
	open(2,file='avloc.dat',status='old',form='unformatted')
        read(1) avgrid
        read(2) avloc
	close(1)
	close(2)

      else

        open(11,file='avdisk.dat',status='old',form='unformatted')
        open(12,file='avspir.dat',status='old',form='unformatted')
        open(13,file='avori.dat',status='old',form='unformatted')
        open(14,file='avori2.dat',status='old',form='unformatted')
        open(15,file='avdloc.dat',status='old',form='unformatted')
        read(11) avdisk
        read(12) avspir
        read(13) avori
        read(14) avori2
        read(15) avdloc
	close(11)
	close(12)
	close(13)
	close(14)
	close(15)

        
	open(16,file='rf_allsky.dat',status='old',form='unformatted')
        read(16) pixnum,ncompold,glonold,glatold,rf
        close(16)

c
c	Sort rescaling factors in COBE system
c
	do i=1,npix
      	   ncomp(1+pixnum(i))=ncompold(i)
	   glon(1+pixnum(i))=glonold(i)
	   glat(1+pixnum(i))=glatold(i)
           rfac(1+pixnum(i))=rf(i)
	end do

      endif


c     ------------------------------------------------------------
c
c     Coordinates input and conversion to galactic coordinates 
c
c     ------------------------------------------------------------

      write(*,*) ''
      write(*,*) "Coordinates of the object:"
      write(*,*) '-------------------------'
      write(*,'(A,$)') 'Enter a distance from the Sun, d (in Kpc): '
      read (*,*) d

95    write (*,*) ' Kind of coordinates to be introduced:'
      write (*,*) '    1 = Ecliptic (lecl,becl)'
      write (*,*) '    2 = Equatorial (alpha,beta)'
      write (*,*) '    3 = Galactic (l,b)'
      write(*,'(A,$)') ''
      read(*,*) itype
      write(*,*)''

      if((itype.ne.1).and.(itype.ne.2).and.(itype.ne.3)) goto 95

        if(itype.eq.1) then
40      write(*,'(A,$)')'Enter ecliptic longitude (-180 < l_ecl < 180):
     * '
        read(*,*) xx
          if(xx.gt.180.d0.or.xx.lt.-180.d0) then
          write(*,*) 'ecliptic longitude out of range'
          goto 40
          endif

41      write(*,'(A,$)') 'Enter ecliptic latitude (-90 < b_ecl < 90): '
        read(*,*) yy
          if(yy.gt.90.d0.or.yy.lt.-90.d0) then
          write(*,*) 'ecliptic latitude out of range'
          goto 41
          endif

        endif


        if(itype.eq.2) then
30      write(*,'(A,$)') 'Enter alpha (0h < alpha < 24h): '
        read(*,*) xx
          if(xx.gt.24.d0.or.xx.lt.0.d0) then
          write(*,*) 'alpha out of range'
          goto 30
          endif
        xx=xx*360.d0/24.d0   

71      write(*,'(A,$)') 'Enter delta (-90 < delta < 90): '
        read(*,*) yy
          if(yy.gt.90.d0.or.yy.lt.-90.d0) then
          write(*,*) 'delta out of range'
          goto 71
          endif

        endif

        if(itype.eq.3) then
60      write(*,'(A,$)') 'Enter galactic longitude (0 < l < 360): '
        read(*,*) xx
          if(xx.gt.360.d0.or.xx.lt.0.d0) then
          write(*,*) 'l out of range'
          goto 60
          endif

61      write(*,'(A,$)') 'Enter b (-90 < b < 90): '
        read(*,*) yy
          if(yy.gt.90.d0.or.yy.lt.-90.d0) then
          write(*,*) 'b out of range'
          goto 61
          endif

	endif



c     Conversion to galactic coordinates (l,b)

      call coord(itype,xx,yy,l,b) 



c     To avoid sin/cos equal zero in the denominator

      if(b.eq.0.d0.or.b.eq.90.d0.or.b.eq.-90.d0)b=b+1.d-10

      if(l.eq.0.d0.or.l.eq.180.d0.or.
     *   l.eq.90.d0.or.l.eq.270.d0)l=l+1.d-10


c     ------------------------------------------------------------
c
c     If the star is located out of the large-scale grid, its 
c     distance is reduced to the maximum size of the grid 
c
c     ------------------------------------------------------------


      ll=l*deg_rad
      bb=b*deg_rad

c     Greatest distance of the grid in z 
         d1=0.5d0/dabs(dsin(bb))-zsun/dsin(bb)

C     Greatest distance of the grid in y
         d2=15.d0/dabs(dcos(ll))-xsun/dcos(ll)
	 d2=d2/dabs(dcos(bb))


c     Greatest distance of the grid in x
         d3=15.d0/dabs(dsin(ll))
	 d3=d3/dabs(dcos(bb))


c     We choose the lower maximum distance
         dmax=min(d1,d2)
         dmax=min(dmax,d3)

c     If the star distance is greater than dmax
c     we decrease this distance to d=dmax

         if(d.gt.dmax) then
             d=dmax
         endif

c     ------------------------------------------------------------
c
c     Conversion from (l,b,d) to (x,y,z)   
c
c     ------------------------------------------------------------


       x=d*dcos(bb)*dcos(ll)
       y=d*dcos(bb)*dsin(ll)
       z=d*dsin(bb)+zsun

       x_gc = x + xsun
       x_he = x


c      Defined only to consider the non-centered origin of the 
c      large scale grid for the Orion Arm

       n_cen = 0

	
c     ------------------------------------------------------------
c
c     Computation of the Av without considering rescaling factors
c     (SWG-RD-02)
c
c     ------------------------------------------------------------

      if((afact.eq.'y').or.(afact.eq.'Y')) go to 998 

c     large scale grid:

      if ((dabs(x).gt.1.d0).or.(dabs(y).gt.2.d0)) then 

	  delta(1)= 0.2d0
	  delta(2)= 0.2d0
	  delta(3)=0.02d0

          n(1)=151
          n(2)=151
          n(3)= 51

          call point (x_gc,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 

          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avgrid(i+ip-1,j+jp-1,k+kp-1)

          end do
          end do
          end do

      else 

c     local scale grid:

	  delta(1)=0.02d0
	  delta(2)=0.02d0
	  delta(3)=0.02d0

          n(1)=101
          n(2)=201
          n(3)= 51

          call point (x_he,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 

          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avloc(i+ip-1,j+jp-1,k+kp-1)

          end do
          end do
          end do

      endif 

      call interp (a,f_i,f_j,f_k,abs)

      go to 999

998   continue 

c     ------------------------------------------------------------
c
c     Computation of the rescaling factors  (SWG-RD-03)
c
c     ------------------------------------------------------------

c          fac(1)    !  disk component rescaling factor
c          fac(2)    !  spiral component rescaling factor
c          fac(3)    !  orion component rescaling factor

      do i=1,3
      fac(i)=1.d0
      enddo

      call ll2pix (l,b,ipix)

c     print *, glon(ipix+1),glat(ipix+1),ipix+1
      icomponent=ncomp(ipix+1)
      fac(icomponent)=rfac(ipix+1)
c     ------------------------------------------------------------
c
c     Computation of the Av considering rescaling factors(SWG-RD-03)
c
c     ------------------------------------------------------------

c     Disk component:

c       Large scale disk component:

        if ((dabs(x).gt.0.75d0).or.(dabs(y).gt.0.75d0)) then 

	  delta(1)= 0.2d0
	  delta(2)= 0.2d0
	  delta(3)=0.02d0

          n(1)=151
          n(2)=151
          n(3)= 51

          call point (x_gc,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 

          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avdisk(i+ip-1,j+jp-1,k+kp-1)

          end do
          end do
          end do

        else 

c       Local scale disk component:

	  delta(1)=0.05d0
	  delta(2)=0.05d0
	  delta(3)=0.02d0

          n(1)=31
          n(2)=31
          n(3)=51

          call point (x_he,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 

          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avdloc(i+ip-1,j+jp-1,k+kp-1)

          end do
          end do
          end do

        endif 

        call interp (a,f_i,f_j,f_k,abs_d)

c     Spiral component:

	  delta(1)= 0.2d0
	  delta(2)= 0.2d0
	  delta(3)=0.02d0

          n(1)=151
          n(2)=151
          n(3)= 51

          call point (x_gc,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 

          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avspir(i+ip-1,j+jp-1,k+kp-1)

          end do
          end do
          end do 

        call interp (a,f_i,f_j,f_k,abs_s)

c     Orion arm component:

c       Large scale orion arm component:

        if ((dabs(x).gt.1.d0).or.(dabs(y).gt.2.d0)) then 

c     Greatest distance of the grid in z 
         dori1=0.5d0/dabs(dsin(bb))-zsun/dsin(bb)

c     Greatest distance of the grid in y
	 dori2=3.75d0/dabs(dsin(ll))
	 dori2=dori2/dabs(dcos(bb))

c     Greatest distance of the grid in x
         if(cos(ll).gt.0.d0)dori3=2.375/dabs(dcos(ll))
         if(cos(ll).lt.0.d0)dori3=1.375/dabs(dcos(ll))
	 dori3=dori3/dabs(dcos(bb))

c     We choose the lower maximum distance
         dmax_ori=min(dori1,dori2)
         dmax_ori=min(dmax_ori,dori3)

c        print*, 'dmax_ori=', dmax_ori

c     If the star distance is greater than dmax_ori
c     we decrease this distance to d=dmax_ori

         if(d.gt.dmax_ori) then
             d_ori=dmax_ori
         else
         d_ori=d
         endif

c     Conversion from (l,b,d) to (x,y,z)   

       x_ori=d_ori*dcos(bb)*dcos(ll)
       y_ori=d_ori*dcos(bb)*dsin(ll)
       z_ori=d_ori*dsin(bb)+zsun

       x_gc_ori = x_ori + xsun

	  delta(1)= 0.05d0
	  delta(2)= 0.05d0
	  delta(3)=0.02d0

          n(1)= 76 
          n(2)=151
          n(3)= 51

          n_cen=1

          call point (x_gc_ori,y_ori,z_ori,n,delta,i,j,k,
     *                n_cen,f_i,f_j,f_k) 


          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

	  if(x_gc_ori.gt.0.d0) then
	  a(ip,jp,kp)=0.d0
	  else
          a(ip,jp,kp)=avori(i+ip-1,j+jp-1,k+kp-1)
	  endif

          end do
          end do
          end do

        else 

c       Local scale orion arm component:

	  delta(1)=0.02d0
	  delta(2)=0.02d0
	  delta(3)=0.02d0

          n(1)=101
          n(2)=201
          n(3)=51

          call point (x_he,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 


          do ip = 1,2
          do jp = 1,2
          do kp = 1,2

          a(ip,jp,kp)=avori2(i+ip-1,j+jp-1,k+kp-1)
          end do
          end do
          end do

        endif 

        call interp (a,f_i,f_j,f_k,abs_o)

c       Computation of the total absorption 

        abs = fac(1)*abs_d + fac(2)*abs_s + fac(3)*abs_o

c       write(*,*) ''
c       write(*,*) 'Disk=',fac(1),' X ', abs_d,'=',fac(1)*abs_d
c       write(*,*) 'Spiral=',fac(2),' X ', abs_s,'=',fac(2)*abs_s
c       write(*,*) 'Orion=',fac(3),' X ', abs_o,'=',fac(3)*abs_o


999   continue

        write(*,*) ''
        write(*,*) 'Av=',abs
        write(*,*) ''

      stop
      end 


c     ..........................................................
c     
c     Subroutine for the transformation to (l,b)  coordinates  
c     ..........................................................

      subroutine coord (itype, xx, yy, l, b) 
      implicit double precision (a-h,o-z)
      real*8 l, l_gp
      parameter(ep=23.389291111d0, pi=3.141592654d0,
     *          alphag=192.859458333d0, deltag=27.12825d0, 
     *          l_gp=122.9285d0, deg_rad=pi/180.d0)

c     itype = 1        !Ecliptic Coordinates
c     itype = 2        !Equatorial Coordinates
c     itype = 3        !Galactic Coordinates  

      if(itype.eq.3) then
        l=xx
	b=yy
      else 

c       Conversion from ecliptic to equatorial

        if(itype.eq.1) then

          xx=xx*deg_rad
          yy=yy*deg_rad
          epp=ep*deg_rad

	  delta=(dasin(dsin(yy)*dcos(epp)+
     *           dcos(yy)*dsin(epp)*dsin(xx)))

	  alpha=dacos(dcos(yy)*dcos(xx)/dcos(delta))

        else
          alpha=xx*deg_rad
          delta=yy*deg_rad

        endif

c	Conversion from equatorial to galactic

          a_g=alphag*deg_rad
          d_g=deltag*deg_rad

	b=dasin(dsin(delta)*dsin(d_g)+
     *    dcos(delta)*dcos(d_g)*dcos((a_g-alpha)))/deg_rad
	l=l_gp-dasin(-dcos(delta)*dsin((a_g-alpha))
     *    /dcos(b))/deg_rad

      endif

      return 
      end 

c     ..........................................................

c     Computation of the grid point & interpolation factors 
c     ..........................................................

      subroutine point (x,y,z,n,delta,i,j,k,n_cen,f_i,f_j,f_k) 
      implicit double precision (a-h,o-z)
      dimension delta(3),n(3)


        if(n_cen.ne.1) then 
            exacti=x/delta(1)+(n(1)-1.d0)/2.d0+1.d0  
        else
            exacti=x/delta(1)+2.5d0*(n(1)-1.d0)+1.d0   
        endif 

        exactj=y/delta(2)+(n(2)-1.d0)/2.d0+1.d0   
        exactk=z/delta(3)+(n(3)-1.d0)/2.d0+1.d0

	i=int(exacti)
	j=int(exactj)
	k=int(exactk)

        if(i.eq.n(1))i=i-1
        if(j.eq.n(2))j=j-1
        if(k.eq.n(3))k=k-1

        if(i.eq.0)i=i+1
        if(j.eq.0)j=j+1
        if(k.eq.0)k=k+1

	f_i=exacti-i
	f_j=exactj-j
	f_k=exactk-k

      return 
      end 
c     ..........................................................

c     Linear interpolation inside the grids
c     ..........................................................

      subroutine  interp (a,f_i,f_j,f_k,abs)
      implicit double precision (a-h,o-z)
      dimension a(2,2,2)

        
        p1=a(1,1,1)+(a(2,1,1)-a(1,1,1))*f_i

        p2=a(1,2,1)+(a(2,2,1)-a(1,2,1))*f_i
        
        p3=a(1,1,2)+(a(2,1,2)-a(1,1,2))*f_i

        p4=a(1,2,2)+(a(2,2,2)-a(1,2,2))*f_i

        p12=p1+(p2-p1)*f_j

        p34=p3+(p4-p3)*f_j

        abs=p12+(p34-p12)*f_k


      return 
      end 
c     ..........................................................

c	Computation of the pixel number
c	(Using some implemented COBE routines)

c     ..........................................................

       subroutine ll2pix (longitude,latitude,pixel)
!                                                                  
!  NAME:
!    lonlat_to_pixel
!
!  PURPOSE:                                   
!        Routine returns pixel number given the longitude and latitude.
!        Additional inputs are the coordinate system and output SKYCUBE
!        resolution. Note, all coordinates are at epoch=J2000.
!
!  CALLING SEQUENCE:
!    ll2pix (longitude, latitude, resolution) > pixel number
!
!  INPUT:
!    longitude, latitude
!    resolution - Quad-cube resolution
!    coord - output coordinate system
!
!  OUTPUT:
!    pixel number
!
!  SUBROUTINES CALLED:
!    PIXEL_NUMBER, INCUBE, AXISXY, BIT_SET
!    CONV_G2E,CONV_Q2E
!
!  REVISION HISTORY
!
c      Implicit NONE
C
C  Arguments:
C
c      character*1    COORD         ! Coordinate System
      integer*4      RESOLUTION    ! Quad-cube resolution
      integer*4      ISTAT         ! Status
      integer*8      ll2uv         ! Routine convert lon/lat to unit vector
      integer*4      PIXEL         ! Pixel number
      real*8         VECTOR(3)     ! Temp. unit vector to center of pixel
      real*8         OVECTOR(3)    ! Unit vector to center of pixel
c      real*4         PIXEL_NUMBER  ! Routine computes pixel_numbers
c      real*4         CONV_Q2E, CONV_G2E !Convert from galactic to ecliptic
c                                        !and equatorial to ecliptic
      real*8         longitude,latitude  !output longitude and latitude

C
      istat=ll2uv(longitude,latitude,vector)

      call CONV_G2E(vector,ovector)
      
      resolution=9
      call PIXEL_NUMBER(ovector,resolution,pixel)
      return	
      end
c      
      Subroutine PIXEL_NUMBER(ovector,resolution,pixel)
C
C    Routine to return the pixel number corresponding to the input unit
C  vector in the given resolution.  Adapted from the SUPER_PIXNO routine
C  by E. Wright, this routine determines the pixel number in the maximum
C  resolution (15), then divides by the appropriate power of 4 to determine
C  the pixel number in the desired resolution.
c*
c*     PROLOGUE:
c*
ch     Date    Version   SPR#   Programmer    Comments
ch     ----    -------   ----   ----------    --------
ch
C
      Implicit NONE
C
C  Arguments:
C
      Real*8        OVECTOR(3)     ! Unit vector of position of interest
      Integer*4     RESOLUTION    ! Resolution of quad cube
      Integer*4     PIXEL         ! Pixel number (0 -> 6*((2**(res-1))**2)
C
C  Variables:
C
      Integer*4     FACE2             ! I*2 Cube face number (0-5)
      Integer*4     FACE4             ! I*4 Cube face number
      Real*4        X,Y               ! Coordinates on cube face
      Integer*4     IX(128),IY(128)   ! Bit tables for calculating I,J
      Integer*4     I,J               ! Integer pixel coordinates
      Integer*4     IP,ID       
      Integer*4     FOUR             ! I*4 '4' - reuired to avoid integer
                                      !  overflow for large pixel numbers
      Integer*4     IL,IH,JL,JH       ! High and low 2-bytes of I & J
      Integer*2     jh1,ih1
c      Integer*4     jishft
c      Integer*2     iishft
      Real*4 TWO14,TWO28
C
      Parameter  (TWO14 = 2**14)
      Parameter  (TWO28 = 2**28)
      Parameter  (FOUR = 4)
C
      Data IX(128) /0/
      if (ix(128) .eq. 0) call BIT_SET(ix,iy,128)
C
      call AXISXY(ovector,face2,x,y)
      face4 = face2
      i = 2.**14 * x
      j = 2.**14 * y
      if (i .gt. 16383) i = 16383
      if (j .gt. 16383) j = 16383
      ih = ishft(i,-7)
      ih1 = ih	
      il = i - ishft(ih1,7)
      jh = ishft(j,-7)
      jh1= jh	
      jl = j - ishft(jh1,7)
      pixel = ishft(face4,28) + ix(il+1) + iy(jl+1) +
     +             ishft((ix(ih+1) + iy(jh+1)),14)
C
C     'Pixel' now contains the pixel number for a resolution of 15.  To
C     convert to the desired resolution, (integer) divide by 4 to the power
C     of the difference between 15 and the given resolution:
C
      pixel = ishft(pixel,-(30-2*resolution))  !  = pixel / 4**(15-resolution)
C
      return
      end

	SUBROUTINE INCUBE(ALPHA,BETA,X,Y)
C
c*     PROLOGUE:
c*
ch     Date    Version   SPR#   Programmer    Comments
ch     ----    -------   ----   ----------    --------
ch
C
	REAL*4 ALPHA,BETA
        Real*4 GSTAR_1,M_G,C_COMB,R0
	PARAMETER (GSTAR=1.37484847732) 
	PARAMETER (G=-0.13161671474)
	M=0.004869491981
	W1=-0.159596235474
	C00=0.141189631152
	C10=0.0809701286525
	C01=-0.281528535557
	C11=0.15384112876
	C20=-0.178251207466
	C02=0.106959469314
	D0=0.0759196200467
	D1=-0.0217762490699
	PARAMETER R0=0.577350269
	AA=ALPHA**2
	BB=BETA**2
	A4=AA**2
	B4=BB**2
	ONMAA=1.-AA
	ONMBB=1.-BB

        gstar_1 = 1. - gstar
        m_g     = m - g
        c_comb  = c00 + c11*aa*bb

	X = ALPHA * 
     +       (GSTAR + 
     +        AA * gstar_1 + 
     +        ONMAA * (BB * (G + (m_g)*AA + 
     +	                     ONMBB * (c_comb + C10*AA + C01*BB +
     +                                C20*A4 + C02*B4)) +
     +	               AA * (W1 - ONMAA*(D0 + D1*AA))))

	Y = BETA * 
     +       (GSTAR + 
     +        BB * gstar_1 + 
     +        ONMBB * (AA * (G + (m_g)*BB +
     +                       ONMAA * (c_comb + C10*BB + C01*AA +
     +                                C20*B4+C02*A4)) + 
     +                 BB * (W1 - ONMBB*(D0 + D1*BB))))
	RETURN
	END

	SUBROUTINE AXISXY(C,NFACE,X,Y)
C
C	CONVERTS UNIT VECTOR C INTO NFACE NUMBER (0-5) AND X,Y
C	IN RANGE 0-1
C
	REAL*8 C(3)
	AC3=ABS(C(3))
	AC2=ABS(C(2))
	AC1=ABS(C(1))
	IF(AC3.GT.AC2) THEN
	   IF(AC3.GT.AC1) THEN
	      IF(C(3).GT.0.) GO TO 100
		GO TO 150
	   ELSE
	      IF(C(1).GT.0.) GO TO 110
		GO TO 130
	   ENDIF
	ELSE
	   IF(AC2.GT.AC1) THEN
	      IF(C(2).GT.0.) GO TO 120
		GO TO 140
	   ELSE
	      IF(C(1).GT.0.) GO TO 110
		GO TO 130
	   ENDIF
	ENDIF
100	NFACE=0
	ETA=-C(1)/C(3)
	XI=C(2)/C(3)
	GO TO 200
110	NFACE=1
	XI=C(2)/C(1)
	ETA=C(3)/C(1)
	GO TO 200
120	NFACE=2
	ETA=C(3)/C(2)
	XI=-C(1)/C(2)
	GO TO 200
130	NFACE=3
	ETA=-C(3)/C(1)
	XI=C(2)/C(1)
	GO TO 200
140	NFACE=4
	ETA=-C(3)/C(2)
	XI=-C(1)/C(2)
	GO TO 200
150	NFACE=5
	ETA=-C(1)/C(3)
	XI=-C(2)/C(3)
	GO TO 200
C
200	CALL INCUBE(XI,ETA,X,Y)
	X=(X+1.)/2.
	Y=(Y+1.)/2.
	RETURN
	END

      Subroutine BIT_SET(IX,IY,LENGTH)
C
C    Routine to set up the bit tables for use in the pixelization subroutines 
C  (extracted and generalized from existing routines).
C
C  Arguments:
C
      Integer*4   LENGTH       ! Number of elements in IX, IY
      Integer*4   IX(length)   ! X bits
      Integer*4   IY(length)   ! Y bits
C
C  Variables:
C
      Integer*4   i,j,k,ip,id  ! Loop variables
C
      Do 30 I = 1,length
          j = i - 1
          k = 0
          ip = 1
   10     if (j .eq. 0) then
              ix(i) = k
              iy(i) = 2*k
            else
              id = mod(j,2)
              j = j/2
              k = ip * id + k
              ip = ip * 4
              go to 10
            endif
   30   continue
C
      return
      end

      Subroutine CONV_G2E(ivector,ovector)
C
C    Routine to convert galactic coordinates to ecliptic (celestial) 
C  coordinates at the epoch=2000.  
C
C                          --------------
C
C  Galactic Coordinate System Definition (from Zombeck, "Handbook of
C  Space Astronomy and Astrophysics", page 71):
C
C         North Pole:  12:49       hours right ascension (1950.0)
C                     +27.4        degrees declination   (1950.0)
C
C    Reference Point:  17:42.6     hours right ascension (1950.0)
C                     -28 55'      degrees declination   (1950.0)
C
C                          --------------
C
c*     PROLOGUE:
c*
ch     Date    Version   SPR#   Programmer    Comments
ch     ----    -------   ----   ----------    --------
ch
ch                                            Pole:  12:49      
ch                                                  +27.4
ch                                          0 long:  17:42:26.603 
ch                                                  -28 55 00.45
ch
C
      Implicit NONE
C
C  Arguments:
C
      Real*8     IVECTOR(3)      ! Input coordinate vector
      Real*8     OVECTOR(3)      ! Output coordinate vector
C
C  Program variables:
C
      Real*8     T(3,3)          ! Galactic coordinate transformation matrix
      Integer*4  I,J             ! Loop variables
C
      Data T / -0.054882486d0, -0.993821033d0, -0.096476249d0, ! 1st column
     +          0.494116468d0, -0.110993846d0,  0.862281440d0, ! 2nd column
     +         -0.867661702d0, -0.000346354d0,  0.497154957d0/ ! 3rd column
C
C     Multiply by transformation matrix:
C
      Do 200 I = 1,3
          ovector(i) = 0.0
          Do 100 J = 1,3
  100        ovector(i) = ovector(i) + ivector(j)*T(i,j)
  200   end do
C
      return
      end

C*******************************************************************************
c*     ll2uv
c*
c*     Function: Convert from decimal latitude/longitude to unit three-vector
c*
c*     PROLOGUE:
c*
ch     Date    Version   SPR#   Programmer    Comments
ch     ----    -------   ----   ----------    --------
ch
c*
c*     INPUT DATA:  Decimal latitude and longitude in degrees
c*
c*     OUTPUT DATA: Unit three-vector equivalent
c*
c*     INVOCATION:  status=ll2uv(longitude,latitude,vector)
c*
c*     INPUT PARAMETERS:
c*
c*     Name                Type          Description
c*     ----                ----          -----------
c*
c*     longitude          real*4        decimal longitude
c*     latitude           real*4        decimal latitude
c*
c*     OUTPUT PARAMETERS:
c*
c*     Name                Type          Description
c*     ----                ----          -----------
c*
c*     vector             real*4(3)     output vector equivalent
c*
c*     COMMON VARIABLES USED: none
c*
c*     SUBROUTINES CALLED:    none
c*
c*     FUNCTIONS CALLED:      none
c*
c*     PROCESSING METHOD:
c*
c*     BEGIN
c*
c*       Apply standard conversion to input coordinates
c*       Return
c*
c*     END
c*******************************************************************************
c*
      Integer*8 Function ll2uv(longitude,latitude,vector)

c
c  Arguments:
c
      Real*8      LONGITUDE  ! longitude in degrees
      Real*8      LATITUDE   ! latitude in degrees
      Real*8      VECTOR(3)  ! output coordinate
c
c     Convert:
c
      deg_rad=3.141592/180.
      vector(1) = dcos(latitude*deg_rad) * dcos(longitude*deg_rad)
      vector(2) = dcos(latitude*deg_rad) * dsin(longitude*deg_rad)
      vector(3) = dsin(latitude*deg_rad)
c     print *, vector
c
c     Done:
c
      ll2uv=1
      return
      end
