m_IndexBin_logical.F90 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  3. !-----------------------------------------------------------------------
  4. ! CVS m_IndexBin_logical.F90,v 1.3 2004-04-21 22:54:45 jacob Exp
  5. ! CVS MCT_2_8_0
  6. !BOP -------------------------------------------------------------------
  7. !
  8. ! !MODULE: m_IndexBin_logical - Template of indexed bin-sorting module
  9. !
  10. ! !DESCRIPTION:
  11. !
  12. ! !INTERFACE:
  13. module m_IndexBin_logical
  14. implicit none
  15. private ! except
  16. public :: IndexBin
  17. interface IndexBin; module procedure &
  18. IndexBin0_
  19. end interface
  20. ! !REVISION HISTORY:
  21. ! 17Feb99 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  22. !EOP ___________________________________________________________________
  23. character(len=*),parameter :: myname='MCT(MPEU)::m_IndexBin_logical'
  24. contains
  25. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  27. !BOP -------------------------------------------------------------------
  28. !
  29. ! !IROUTINE: IndexBin0_ - Indexed sorting for a single value
  30. !
  31. ! !DESCRIPTION:
  32. !
  33. ! !INTERFACE:
  34. subroutine IndexBin0_(n,indx,keys,key0,ln0)
  35. use m_stdio, only : stderr
  36. use m_die, only : die
  37. implicit none
  38. integer, intent(in) :: n
  39. integer, dimension(n), intent(inout) :: indx
  40. logical, dimension(n), intent(in) :: keys
  41. logical, intent(in) :: key0 ! The key value to be moved to front
  42. integer,optional,intent(out) :: ln0
  43. ! !REVISION HISTORY:
  44. ! 16Feb99 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  45. ! 27Sep99 - Jing Guo <guo@thunder> - Fixed a bug pointed out by
  46. ! Chris Redder
  47. !EOP ___________________________________________________________________
  48. character(len=*),parameter :: myname_=myname//'::IndexBin0_'
  49. integer,allocatable,dimension(:) :: inew
  50. integer :: ni,ix,i,ier
  51. integer :: ln(0:1),lc(0:1)
  52. !________________________________________
  53. allocate(inew(n),stat=ier)
  54. if(ier /= 0) then
  55. write(stderr,'(2a,i4)') myname_, &
  56. ': allocate() error, stat =',ier
  57. call die(myname_)
  58. endif
  59. !________________________________________
  60. ! Count numbers entries for the given key0
  61. lc(0)=1 ! the location of values the same as key0
  62. ln(0)=0
  63. do i=1,n
  64. if(keys(i) .eqv. key0) ln(0)=ln(0)+1
  65. end do
  66. lc(1)=ln(0)+1 ! the location of values not the same as key0
  67. !________________________________________
  68. ! Reset the counters
  69. ln(0:1)=0
  70. do i=1,n
  71. ix=indx(i)
  72. if(keys(ix) .eqv. key0) then
  73. ni=lc(0)+ln(0)
  74. ln(0)=ln(0)+1
  75. else
  76. ni=lc(1)+ln(1)
  77. ln(1)=ln(1)+1
  78. endif
  79. inew(ni)=ix
  80. end do
  81. !________________________________________
  82. ! Sort out the old pointers according to the new order
  83. indx(:)=inew(:)
  84. if(present(ln0)) ln0=ln(0)
  85. !________________________________________
  86. deallocate(inew)
  87. end subroutine IndexBin0_
  88. end module m_IndexBin_logical