1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195 |
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !-----------------------------------------------------------------------
- ! CVS m_MergeSorts.F90,v 1.3 2004-04-21 22:54:45 jacob Exp
- ! CVS MCT_2_8_0
- !BOP -------------------------------------------------------------------
- !
- ! !MODULE: m_MergeSorts - Tools for incremental indexed-sorting
- !
- ! !DESCRIPTION:
- !
- ! This tool module contains basic sorting procedures, that in
- ! addition to a couple of standard Fortran 90 statements in the
- ! array syntex, allow a full range sort or unsort operations.
- ! The main characteristics of the sorting algorithm used in this
- ! module are, a) stable, and b) index sorting.
- !
- ! !INTERFACE:
- module m_MergeSorts
- implicit none
- private ! except
- public :: IndexSet
- public :: IndexSort
- interface IndexSet
- module procedure setn_
- module procedure set_
- end interface
- interface IndexSort
- module procedure iSortn_
- module procedure rSortn_
- module procedure dSortn_
- module procedure cSortn_
- module procedure iSort_
- module procedure rSort_
- module procedure dSort_
- module procedure cSort_
- module procedure iSort1_
- module procedure rSort1_
- module procedure dSort1_
- module procedure cSort1_
- end interface
- ! !EXAMPLES:
- !
- ! ...
- ! integer, intent(in) :: No
- ! type(Observations), dimension(No), intent(inout) :: obs
- !
- ! integer, dimension(No) :: indx ! automatic array
- !
- ! call IndexSet(No,indx)
- ! call IndexSort(No,indx,obs(1:No)%lev,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%lon,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%lat,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%kt,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%ks,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%kx,descend=.false.)
- ! call IndexSort(No,indx,obs(1:No)%kr,descend=.false.)
- !
- ! ! Sorting
- ! obs(1:No) = obs( (/ (indx(i),i=1,No) /) )
- ! ...
- ! ! Unsorting
- ! obs( (/ (indx(i),i=1,No) /) ) = obs(1:No)
- !
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Added interfaces without the explicit size
- ! . Added interfaces for two dimensional arrays
- ! 02Feb99 - Jing Guo <guo@thunder> - Added if(present(stat)) ...
- ! 04Jan99 - Jing Guo <guo@thunder> - revised
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- !EOP ___________________________________________________________________
- character(len=*), parameter :: myname='MCT(MPEU)::m_MergeSorts'
- contains
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: setn_ - Initialize an array of data location indices
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine setn_(n,indx)
- implicit none
- integer, intent(in) :: n ! size of indx(:)
- integer, dimension(n), intent(out) :: indx ! indices
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . redefined for the original interface
- !EOP ___________________________________________________________________
- call set_(indx(1:n))
- end subroutine setn_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: set_ - Initialize an array of data location indices
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine set_(indx)
- implicit none
- integer, dimension(:), intent(out) :: indx ! indices
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Modified the interface, by removing the explicit size
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- ! 04Jan99 - Jing Guo <guo@thunder> - revised prolog format
- !EOP ___________________________________________________________________
- integer :: i
- do i=1,size(indx)
- indx(i)=i
- end do
- end subroutine set_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: iSortn_ - A stable merge index sorting of INTs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine iSortn_(n,indx,keys,descend,stat)
- implicit none
- integer,intent(in) :: n
- integer, dimension(n), intent(inout) :: indx
- integer, dimension(n), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . redefined for the original interface
- !EOP ___________________________________________________________________
- character(len=*),parameter :: myname_=myname//'::iSortn_'
- call iSort_(indx(1:n),keys(1:n),descend,stat)
- end subroutine iSortn_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: rSortn_ - A stable merge index sorting REALs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine rSortn_(n,indx,keys,descend,stat)
- use m_realkinds,only : SP
- implicit none
- integer,intent(in) :: n
- integer, dimension(n), intent(inout) :: indx
- real(SP),dimension(n), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . redefined for the original interface
- !EOP ___________________________________________________________________
- character(len=*),parameter :: myname_=myname//'::rSortn_'
- call rSort_(indx(1:n),keys(1:n),descend,stat)
- end subroutine rSortn_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: dSortn_ - A stable merge index sorting DOUBLEs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine dSortn_(n,indx,keys,descend,stat)
- use m_realkinds,only : DP
- implicit none
- integer,intent(in) :: n
- integer, dimension(n), intent(inout) :: indx
- real(DP), dimension(n), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . redefined for the original interface
- !EOP ___________________________________________________________________
- character(len=*),parameter :: myname_=myname//'::dSortn_'
- call dSort_(indx(1:n),keys(1:n),descend,stat)
- end subroutine dSortn_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: cSortn_ - A stable merge index sorting of CHAR(*)s.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine cSortn_(n,indx,keys,descend,stat)
- implicit none
- integer,intent(in) :: n
- integer, dimension(n), intent(inout) :: indx
- character(len=*), dimension(n), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . redefined for the original interface
- !EOP ___________________________________________________________________
- character(len=*),parameter :: myname_=myname//'::cSortn_'
- call cSort_(indx(1:n),keys(1:n),descend,stat)
- end subroutine cSortn_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: iSort_ - A stable merge index sorting of INTs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine iSort_(indx,keys,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- implicit none
- integer, dimension(:), intent(inout) :: indx
- integer, dimension(:), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Modified the interface, by removing the explicit size
- ! 02Feb99 - Jing Guo <guo@thunder> - Added if(present(stat)) ...
- ! 04Jan99 - Jing Guo <guo@thunder> - revised the prolog
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::iSort_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(indx(l1)) .ge. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(indx(l1)) .le. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine iSort_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: rSort_ - A stable merge index sorting REALs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine rSort_(indx,keys,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- use m_realkinds,only : SP
- implicit none
- integer, dimension(:), intent(inout) :: indx
- real(SP),dimension(:), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Modified the interface, by removing the explicit size
- ! 02Feb99 - Jing Guo <guo@thunder> - Added if(present(stat)) ...
- ! 04Jan99 - Jing Guo <guo@thunder> - revised the prolog
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::rSort_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(indx(l1)) .ge. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(indx(l1)) .le. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine rSort_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: dSort_ - A stable merge index sorting DOUBLEs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine dSort_(indx,keys,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- use m_realkinds,only : DP
- implicit none
- integer, dimension(:), intent(inout) :: indx
- real(DP), dimension(:), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Modified the interface, by removing the explicit size
- ! 02Feb99 - Jing Guo <guo@thunder> - Added if(present(stat)) ...
- ! 04Jan99 - Jing Guo <guo@thunder> - revised the prolog
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::dSort_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(indx(l1)) .ge. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(indx(l1)) .le. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine dSort_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: cSort_ - A stable merge index sorting of CHAR(*)s.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine cSort_(indx,keys,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- implicit none
- integer, dimension(:), intent(inout) :: indx
- character(len=*), dimension(:), intent(in) :: keys
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . Modified the interface, by removing the explicit size
- ! 02Feb99 - Jing Guo <guo@thunder> - Added if(present(stat)) ...
- ! 04Jan99 - Jing Guo <guo@thunder> - revised the prolog
- ! 09Sep97 - Jing Guo <guo@thunder> - initial prototype/prolog/code
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::cSort_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(indx(l1)) .ge. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(indx(l1)) .le. keys(indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine cSort_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: iSort1_ - A stable merge index sorting of INTs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine iSort1_(indx,keys,ikey,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- implicit none
- integer, dimension(:), intent(inout) :: indx
- integer, dimension(:,:), intent(in) :: keys
- integer,intent(in) :: ikey
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . Copied code from iSort_
- ! . Extended the interface and the algorithm to handle
- ! 2-d arrays with an index.
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::iSort1_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(ikey,indx(l1)) .ge. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(ikey,indx(l1)) .le. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine iSort1_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: rSort1_ - A stable merge index sorting REALs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine rSort1_(indx,keys,ikey,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- use m_realkinds,only : SP
- implicit none
- integer, dimension(:), intent(inout) :: indx
- real(SP),dimension(:,:), intent(in) :: keys
- integer,intent(in) :: ikey
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . Copied code from rSort_
- ! . Extended the interface and the algorithm to handle
- ! 2-d arrays with an index.
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::rSort1_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(ikey,indx(l1)) .ge. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(ikey,indx(l1)) .le. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine rSort1_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: dSort1_ - A stable merge index sorting DOUBLEs.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine dSort1_(indx,keys,ikey,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- use m_realkinds,only : DP
- implicit none
- integer, dimension(:), intent(inout) :: indx
- real(DP), dimension(:,:), intent(in) :: keys
- integer,intent(in) :: ikey
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . Copied code from dSort_
- ! . Extended the interface and the algorithm to handle
- ! 2-d arrays with an index.
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::dSort1_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(ikey,indx(l1)) .ge. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(ikey,indx(l1)) .le. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine dSort1_
- !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ! NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS !
- !BOP -------------------------------------------------------------------
- !
- ! !IROUTINE: cSort1_ - A stable merge index sorting of CHAR(*)s.
- !
- ! !DESCRIPTION:
- !
- ! !INTERFACE:
- subroutine cSort1_(indx,keys,ikey,descend,stat)
- use m_stdio, only : stderr
- use m_die, only : die
- implicit none
- integer, dimension(:), intent(inout) :: indx
- character(len=*), dimension(:,:), intent(in) :: keys
- integer,intent(in) :: ikey
- logical, optional, intent(in) :: descend
- integer, optional, intent(out) :: stat
- ! !REVISION HISTORY:
- ! 15Mar00 - Jing Guo
- ! . initial prototype/prolog/code
- ! . Copied code from cSort_
- ! . Extended the interface and the algorithm to handle
- ! 2-d arrays with an index.
- !EOP ___________________________________________________________________
- logical :: dsnd
- integer :: ierr
- integer, dimension(:),allocatable :: mtmp
- integer :: n
- character(len=*),parameter :: myname_=myname//'::cSort1_'
- if(present(stat)) stat=0
- n=size(indx)
- allocate(mtmp(n),stat=ierr)
- if(ierr /= 0) then
- write(stderr,'(2a,i4)') myname_, &
- ': allocate(mtmp(:)) error, stat =',ierr
- if(.not.present(stat)) call die(myname_)
- stat=ierr
- return
- endif
- dsnd=.false.
- if(present(descend)) dsnd=descend
- call MergeSort_()
- deallocate(mtmp)
- contains
- subroutine MergeSort_()
- implicit none
- integer :: mstep,lstep
- integer :: lb,lm,le
- mstep=1
- do while(mstep < n)
- lstep=mstep*2
- lb=1
- do while(lb < n)
- lm=lb+mstep
- le=min(lm-1+mstep,n)
- call merge_(lb,lm,le)
- indx(lb:le)=mtmp(lb:le)
- lb=le+1
- end do
- mstep=lstep
- end do
- end subroutine MergeSort_
- subroutine merge_(lb,lm,le)
- integer,intent(in) :: lb,lm,le
- integer :: l1,l2,l
- l1=lb
- l2=lm
- do l=lb,le
- if(l2.gt.le) then
- mtmp(l)=indx(l1)
- l1=l1+1
- elseif(l1.ge.lm) then
- mtmp(l)=indx(l2)
- l2=l2+1
- else
- if(dsnd) then
- if(keys(ikey,indx(l1)) .ge. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- else
- if(keys(ikey,indx(l1)) .le. keys(ikey,indx(l2))) then
- mtmp(l)=indx(l1)
- l1=l1+1
- else
- mtmp(l)=indx(l2)
- l2=l2+1
- endif
- endif
- endif
- end do
- end subroutine merge_
- end subroutine cSort1_
- !-----------------------------------------------------------------------
- end module m_MergeSorts
- !.
|