Changeset 1616 in lmdz_wrf


Ignore:
Timestamp:
Sep 3, 2017, 12:29:48 AM (8 years ago)
Author:
lfita
Message:

Adding:

  • `clean_polygons': Subroutine to clean polygons from non-used paths, polygons only left as path since they are inner path of a hole
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/module_scientific.f90

    r1614 r1616  
    44!!!!!!! Functions & subroutines
    55! borders_matrixL: Subroutine to provide the borders of a logical array (interested in .TRUE.)
     6! clean_polygons: Subroutine to clean polygons from non-used paths, polygons only left as path since they are inner path of a hole
    67! FindMinimumR_K*: Function returns the location of the minimum in the section between Start and End.
    78! gridpoints_InsidePolygon: Subroutine to determine if a series of grid points are inside a polygon
     
    107108
    108109  Nppt = dx*dy/10
    109   Npts = dx*dy
    110110
    111111  IF (ALLOCATED(borders)) DEALLOCATE(borders)
     
    124124  CALL ErrMsg(msg, fname, ierr)
    125125
    126   ! Filling with the points of all the space
     126  ! Filling with the points of all the space with .TRUE.
     127  Npts = COUNT(boolmat)
     128
    127129  IF (ALLOCATED(points)) DEALLOCATE(points)
    128130  ALLOCATE(points(Npts,2), STAT=ierr)
     
    130132  CALL ErrMsg(msg, fname, ierr)
    131133
     134  ! We only want to localize that points 'inside'
    132135  ip = 1
    133136  DO i=1, dx
    134137    DO j=1, dy
    135       points(ip,1) = i
    136       points(ip,2) = j
    137       ip = ip + 1
     138      IF (boolmat(i,j)) THEN
     139        points(ip,1) = i
     140        points(ip,2) = j
     141        ip = ip + 1
     142      END IF
    138143    END DO
    139144  END DO
     
    177182
    178183    ! Filling polygons
    179     ipp = 1
    180     DO i=1, dx
    181       DO j=1, dy
    182         IF (isin(ipp)) polys(i,j) = ip
    183         ipp = ipp + 1
    184       END DO
     184    DO ipp=1, Npts
     185      IF (isin(ipp)) polys(points(ipp,1),points(ipp,2)) = ip
    185186    END DO
    186187
     
    196197  END DO
    197198
     199  ! Cleaning polygons matrix of not-used and paths around holes
     200  CALL clean_polygons(dx, dy, boolmat, polys, Npoly, dbg)
     201
    198202  DEALLOCATE (borders) 
    199203  DEALLOCATE (Nptpaths)
     
    206210
    207211END SUBROUTINE polygons
     212
     213SUBROUTINE clean_polygons(dx, dy, Lmat, pols, Npols, dbg)
     214! Subroutine to clean polygons from non-used paths, polygons only left as path since they are inner path of a hole
     215
     216  IMPLICIT NONE
     217
     218  INTEGER, INTENT(in)                                    :: dx, dy
     219  LOGICAL, DIMENSION(dx,dy), INTENT(in)                  :: Lmat
     220  INTEGER, INTENT(inout)                                 :: Npols
     221  INTEGER, DIMENSION(dx,dy), INTENT(inout)               :: pols
     222  LOGICAL, INTENT(in)                                    :: dbg
     223
     224! Local
     225  INTEGER                                                :: i,j,ip,iprm
     226  INTEGER, DIMENSION(Npols)                              :: origPol, NotPol, neigPol
     227  INTEGER                                                :: ispol, NnotPol
     228  CHARACTER(len=4)                                       :: ISa
     229
     230!!!!!!! Variables
     231! dx, dy: size of the space
     232! Lmat: original bolean matrix from which the polygons come from
     233! Npols: original number of polygons
     234! pols: polygons space
     235
     236  fname = 'clean_polygons'
     237  IF (dbg) PRINT *,"  At '" // TRIM(fname) // "' ..."
     238
     239  origPol = -1
     240
     241  ! Looking for polygons already in space
     242  NnotPol = 0
     243  DO ip=1, Npols
     244    ispol = COUNT(pols-ip == 0)
     245    IF (ispol > 0) THEN
     246      origPol(ip) = ip
     247    ELSE
     248      NnotPol = NnotPol + 1
     249      NotPol(NnotPol) = ip
     250      neigPol(NnotPol) = -1
     251    END IF
     252  END DO
     253
     254  IF (dbg) THEN
     255    PRINT *,'  It should be:', Npols, ' polygons, but already there are:', Npols - NnotPol
     256    PRINT *,'  Polygons to remove:', NotPol(1:NnotPol)
     257  END IF
     258 
     259  ! Looking for the hole border of a polygon. This is identify as such polygon point which along
     260  !   y-axis has NpolygonA, Npolygon, .FALSE.
     261  DO i=1,dx
     262    DO j=2,dy-1
     263      IF  ( (pols(i,j-1) /= pols(i,j) .AND. pols(i,j+1) == -1) .AND. (COUNT(NotPol-pols(i,j)==0)==0)  &
     264        .AND. (pols(i,j) /= -1) .AND. (pols(i,j-1) /= -1)) THEN
     265        IF (dbg) PRINT *,'  Polygon:', pols(i,j), ' to be removed at point (',i,',',j,'); j-1:',      &
     266          pols(i,j-1), ' j:', pols(i,j), ' j+1:', pols(i,j+1)
     267        NnotPol = NnotPol + 1
     268        NotPol(NnotPol) = pols(i,j)
     269        neigPol(NnotPol) = pols(i,j-1)
     270      END IF
     271    END DO
     272  END DO
     273
     274  IF (dbg) THEN
     275    PRINT *,'  It should be:', Npols, ' polygons, but already there are:', Npols - NnotPol
     276    PRINT *,'  Polygons to remove after looking for fake border-of-hole polygons _______'
     277    DO i=1, NnotPol
     278      PRINT *, '      Polygon:', NotPol(i), ' to be replaced by:', neigPol(i)
     279    END DO
     280  END IF
     281
     282  ! Removing polygons
     283  DO iprm=1, NnotPol
     284    IF (neigPol(iprm) == -1) THEN
     285      WHERE (pols == NotPol(iprm))
     286        pols = -1
     287      END WHERE
     288      IF (dbg) THEN
     289        PRINT *,'    removing polygon:', NotPol(iprm)
     290      END IF
     291    ELSE
     292      WHERE (pols == NotPol(iprm))
     293        pols = neigPol(iprm)
     294      END WHERE
     295      IF (dbg) THEN
     296        PRINT *,'       replacing polygon:', NotPol(iprm), ' by:', neigPol(iprm)
     297      END IF
     298    END IF
     299  END DO
     300
     301  ! Re-numbering (descending values)
     302  DO i = 1, NnotPol
     303    iprm = MAXVAL(NotPol(1:NnotPol))
     304    WHERE(pols > iprm)
     305      pols = pols - 1
     306    END WHERE
     307    j = Index1DArrayI(NotPol, NnotPol, iprm)
     308    NotPol(j) = -9
     309  END DO
     310
     311  Npols = Npols - NnotPol
     312
     313  RETURN
     314
     315END SUBROUTINE clean_polygons
    208316
    209317  SUBROUTINE path_properties(dx, dy, Lmat, Nptspth, pth, xxtrm, yxtrm, meanctr, axs, Nvrtx, vrtxs)
Note: See TracChangeset for help on using the changeset viewer.