m_SortingTools.F90 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
  3. !-----------------------------------------------------------------------
  4. ! CVS m_SortingTools.F90,v 1.4 2004-04-21 22:54:46 jacob Exp
  5. ! CVS MCT_2_8_0
  6. !BOP -------------------------------------------------------------------
  7. !
  8. ! !MODULE: m_SortingTools - A collection of different sorting tools
  9. !
  10. ! !DESCRIPTION:
  11. !
  12. ! This module contains a collection of sorting utilities. The
  13. ! utilities are accessed through three generic interfaces, IndexSet(),
  14. ! IndexSort(), and IndexBin().
  15. !
  16. ! Note that, a version of IndexBin() for real arguments is not
  17. ! implemented due to the difficulty of comparing two real values as
  18. ! being equal. For example, a bin for real values may be specified
  19. ! as a single number, a range of two numbers, a number with an
  20. ! absolute error-bar, or a number with a relative error-bar.
  21. !
  22. ! In general, one may have to map both keys(:) and bins(:) to
  23. ! integer indices by the a given rule, then use the integer version
  24. ! of IndexBin() with the two integer index arrays to do the sorting.
  25. ! This mapping rule, however, is application dependent.
  26. !
  27. ! Also note that, in principle, it is possible to use both
  28. ! IndexSort() and IndexBin() in the same sorting task.
  29. !
  30. ! !INTERFACE:
  31. module m_SortingTools
  32. use m_MergeSorts !only : IndexSet,IndexSort
  33. use m_IndexBin_integer !only : IndexBin
  34. use m_IndexBin_char !only : IndexBin
  35. use m_IndexBin_logical !only : IndexBin
  36. use m_rankMerge !only : RankSet,RankMerge,IndexedRankMerge
  37. use m_Permuter !only : Permute, Unpermute
  38. implicit none
  39. private ! except
  40. public :: IndexSet ! define an initial list of indices
  41. public :: IndexSort ! index for a new rank out of the old
  42. public :: IndexBin ! index for sorting bins
  43. public :: RankSet ! define an initial list of ranks
  44. public :: RankMerge ! merge two arrays by re-ranking
  45. public :: IndexedRankMerge ! index-merge two array segments
  46. public :: Permute ! permute array entries
  47. public :: Unpermute ! invert permutation
  48. ! !EXAMPLES:
  49. !
  50. ! - An example of using IndexSet()/IndexSort() in combination with
  51. ! the convenience of the Fortran 90 array syntex can be found in the
  52. ! prolog of m_MergeSorts.
  53. !
  54. ! - An example of using IndexSet()/IndexBin(): Copying all "good"
  55. ! data to another array.
  56. !
  57. ! integer :: indx(n)
  58. ! call IndexSet(n,indx)
  59. ! call IndexBin(n,indx,allObs(:)%qcflag,GOOD,ln0=ln_GOOD)
  60. !
  61. ! ! Copy all "good" data to another array
  62. ! goodObs(1:ln_GOOD)=allObs( indx(1:ln_GOOD) )
  63. !
  64. ! ! Refill all "good" data back to their original places
  65. ! allObs( indx(1:ln_GOOD) ) = goodObs(1:ln_GOOD)
  66. !
  67. ! - Similarily, multiple keys may be used in an IndexBin() call
  68. ! to selectively sort the data. The following code will move data
  69. ! with kt = kt_Us,kt_U,kt_Vs,kt_V up to the front:
  70. !
  71. ! call IndexBin(n,indx,allObs(:)%kt,(/kt_Us,kt_U,kt_Vs,kt_V/))
  72. ! allObs(1:n) = allObs( indx(1:n) )
  73. !
  74. ! - Additional applications can also be implemented with other
  75. ! argument combinations.
  76. !
  77. ! !REVISION HISTORY:
  78. ! 15Mar00 - Jing Guo
  79. ! . Added m_rankMerge module interface
  80. ! 20Apr99 - Jing Guo
  81. ! - Commented "only" in use m_IndexBin_xxx to avoid an
  82. ! apperent compiler bug on DEC/OSF1
  83. ! 17Feb99 - Jing Guo <guo@thunder> - initial prototype/prolog/code
  84. ! 19Oct00 - J.W. Larson <larson@mcs.anl.gov> - added Permuter and
  85. ! Unpermuter to list of public functions.
  86. !EOP ___________________________________________________________________
  87. character(len=*),parameter :: myname='MCT(MPEU)::m_SortingTools'
  88. end module m_SortingTools