Changeset 2288 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jan 25, 2019, 3:30:00 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `grid_spacepercen': Subroutine to compute the space-percentages of a series of grid cells (B) which lay inside another series of grid-cells (A)
  • `grid_spacepercen_within_reg': Subroutine to compute the percentage of grid space of a series of grid cells which are encompassed by a polygon
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/module_scientific.f90

    r2286 r2288  
    2020! fill3DR_2Dvec: Subroutine to fill a 3D float matrix from a series of indices from a 2D matrix
    2121! grid_within_polygon: Subroutine to determine which grid cells from a matrix lay inside a polygon
     22! grid_spacepercen: Subroutine to compute the space-percentages of a series of grid cells (B) which lay inside another
     23!   series of grid-cells (A)
     24! grid_spacepercen_within_reg: Subroutine to compute the percentage of grid space of a series of grid cells which are encompassed
     25!   by a polygon
    2226! gridpoints_InsidePolygon: Subroutine to determine if a series of grid points are inside a polygon
    2327!   following ray casting algorithm
     
    49414945    END IF
    49424946
     4947    IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     4948    IF (ALLOCATED(icoinpoly)) DEALLOCATE(icoinpoly)
     4949    IF (ALLOCATED(coinpoly)) DEALLOCATE(coinpoly)
     4950    IF (ALLOCATED(sortpoly)) DEALLOCATE(sortpoly)
     4951    IF (ALLOCATED(poly)) DEALLOCATE(poly)
     4952
    49434953  END SUBROUTINE spacepercen_within_reg
    49444954
     4955  SUBROUTINE grid_spacepercen_within_reg(NvertexA, polygonA, dx, dy, Nvertexmax, xBvals, yBvals,      &
     4956    Ngridsin, gridsin, strict, gridspace, percentages)
     4957  ! Subroutine to compute the percentage of grid space of a series of grid cells which are encompassed
     4958  !   by a polygon
     4959  ! NOTE: Assuming coordinates on the plane with rectilinar, distance preserved and perpendicular x
     4960  !   and y axes.
     4961
     4962    IMPLICIT NONE
     4963
     4964    INTEGER, INTENT(in)                                  :: NvertexA, dx, dy, Nvertexmax
     4965    REAL(r_k), DIMENSION(NvertexA,2), INTENT(in)         :: polygonA
     4966    REAL(r_k), DIMENSION(dx,dy,Nvertexmax), INTENT(in)   :: xBvals, yBvals
     4967    INTEGER, INTENT(in)                                  :: Ngridsin
     4968    INTEGER, DIMENSION(Ngridsin,2), INTENT(in)           :: gridsin
     4969    LOGICAL, INTENT(in)                                  :: strict
     4970    REAL(r_k), DIMENSION(Ngridsin), INTENT(out)          :: gridspace, percentages
     4971
     4972! Local
     4973   INTEGER                                               :: ig, iv, ix, iy
     4974   INTEGER                                               :: Nvertex, NvertexAgrid, Ncoin, Nsort
     4975   CHARACTER(len=20)                                     :: DS
     4976   REAL(r_k)                                             :: areapoly, areagpoly
     4977   REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                :: vertexgrid, icoinpoly, coinpoly,          &
     4978     sortpoly, poly
     4979
     4980!!!!!!! Variables
     4981! NvertexA: Number of vertices of the polygon to find the grids
     4982! polygonA: ordered vertices of the polygon
     4983! dx, dy: shape of the matrix with the grid points
     4984! xCvals, yCvals: coordinates of the center of the grid cells
     4985! Nvertexmax: Maximum number of vertices of the grid cells
     4986! xBvals, yBvals: coordinates of th vertices of the grid cells (-99999 for no vertex)
     4987! Ngridsin: number of grids with some extension within the polygon
     4988! gridsin: grids within the polygon
     4989! strict: give an error if the area of the polygon is not fully covered
     4990! gridspace: area of each of the grids
     4991! percentages: percentages of grid area of each of the grids within the polygon
     4992
     4993    fname = 'grid_spacepercen_within_reg'
     4994
     4995    gridspace = zeroRK
     4996    percentages = zeroRK
     4997
     4998    DO ig = 1, Ngridsin
     4999      ix = gridsin(ig,1)
     5000      iy = gridsin(ig,2)
     5001
     5002     ! Getting grid vertices
     5003      Nvertex = 0
     5004      DO iv=1, Nvertexmax
     5005        IF (xBvals(ix,iy,iv) /= fillvalI) THEN
     5006          Nvertex = Nvertex + 1
     5007        END IF
     5008      END DO
     5009      IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5010      ALLOCATE(vertexgrid(Nvertex,2))
     5011      vertexgrid(:,1) = xBvals(ix,iy,1:Nvertex)
     5012      vertexgrid(:,2) = yBvals(ix,iy,1:Nvertex)
     5013      areapoly = shoelace_area_polygon(Nvertex, vertexgrid)
     5014
     5015      ! Getting common vertices
     5016      NvertexAgrid = NvertexA*Nvertex
     5017      IF (ALLOCATED(icoinpoly)) DEALLOCATE(icoinpoly)
     5018      ALLOCATE(icoinpoly(NvertexAgrid,2))
     5019      CALL coincident_polygon(NvertexA, Nvertex, NvertexAgrid, polygonA, vertexgrid, Ncoin, icoinpoly)
     5020
     5021      IF (ALLOCATED(coinpoly)) DEALLOCATE(coinpoly)
     5022      ALLOCATE(coinpoly(Ncoin,2))
     5023      DO iv=1, Ncoin
     5024        coinpoly(iv,:) = icoinpoly(iv,:)
     5025      END DO
     5026
     5027      IF (ALLOCATED(sortpoly)) DEALLOCATE(sortpoly)
     5028      ALLOCATE(sortpoly(Ncoin,2))
     5029      CALL sort_polygon(Ncoin, coinpoly, 1, Nsort, sortpoly)
     5030
     5031      IF (ALLOCATED(poly)) DEALLOCATE(poly)
     5032      ALLOCATE(poly(Nsort,2))
     5033      DO iv=1, Nsort
     5034        poly(iv,:) = sortpoly(iv,:)
     5035      END DO
     5036
     5037      areagpoly = shoelace_area_polygon(Nsort, poly)
     5038      gridspace(ig) = ABS(areapoly)
     5039      percentages(ig) = ABS(areagpoly / areapoly)
     5040    END DO
     5041
     5042    IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5043    IF (ALLOCATED(icoinpoly)) DEALLOCATE(icoinpoly)
     5044    IF (ALLOCATED(coinpoly)) DEALLOCATE(coinpoly)
     5045    IF (ALLOCATED(sortpoly)) DEALLOCATE(sortpoly)
     5046    IF (ALLOCATED(poly)) DEALLOCATE(poly)
     5047
     5048  END SUBROUTINE grid_spacepercen_within_reg
     5049
    49455050  SUBROUTINE spacepercen(xCAvals, yCAvals, xBAvals, yBAvals, xCBvals, yCBvals, xBBvals, yBBvals,      &
    4946     dxA, dyA, NAvertexmax, dxB, dyB, dxyB, NBvertexmax, strict, Ngridsin, gridsin, percentages)
     5051    dxA, dyA, NAvertexmax, dxB, dyB, dxyB, NBvertexmax, strict, Ngridsin, gridsin, areas, percentages)
    49475052  ! Subroutine to compute the space-percentages of a series of grid cells (B) into another series of
    49485053  !   grid-cells (A)
     
    49615066    INTEGER, DIMENSION(dxA,dyA), INTENT(out)             :: Ngridsin
    49625067    INTEGER, DIMENSION(dxA,dyA,dxyB,2), INTENT(out)      :: gridsin
     5068    REAL(r_k), DIMENSION(dxA,dyA), INTENT(out)           :: areas
    49635069    REAL(r_k), DIMENSION(dxA,dyA,dxyB), INTENT(out)      :: percentages
    49645070
     
    49675073   INTEGER                                               :: Nvertex
    49685074   INTEGER, ALLOCATABLE, DIMENSION(:,:)                  :: poinsin
    4969    CHARACTER(len=20)                                     :: DS
     5075   CHARACTER(len=20)                                     :: IS
    49705076   REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                :: vertexgrid
    49715077
     
    49825088! Ngridsin: number of grids from grid B with some extension within the grid cell A
    49835089! gridsin: indices of B grids within the grids of A
     5090! areas: areas of the polygons
    49845091! percentages: percentages of area of cells B of each of the grids within the grid cell A
    49855092
     
    49895096      DO iy = 1, dyA
    49905097
    4991       ! Getting grid vertices
    4992       Nvertex = 0
    4993       DO iv=1, NAvertexmax
    4994         IF (xBAvals(ix,iy,iv) /= fillval64) THEN
    4995           Nvertex = Nvertex + 1
    4996         END IF
     5098        ! Getting grid vertices
     5099        Nvertex = 0
     5100        DO iv=1, NAvertexmax
     5101          IF (xBAvals(ix,iy,iv) /= fillval64) THEN
     5102           Nvertex = Nvertex + 1
     5103          END IF
     5104        END DO
     5105        IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5106        ALLOCATE(vertexgrid(Nvertex,2))
     5107        vertexgrid(:,1) = xBAvals(ix,iy,1:Nvertex)
     5108        vertexgrid(:,2) = yBAvals(ix,iy,1:Nvertex)
     5109 
     5110        CALL grid_within_polygon(Nvertex, vertexgrid, dxB, dyB, dxB*dyB, xCBvals, yCBvals,            &
     5111          NBvertexmax, xBBvals, yBBvals, Ngridsin(ix,iy), gridsin(ix,iy,:,:))
     5112   
     5113        IF (ALLOCATED(poinsin)) DEALLOCATE(poinsin)
     5114        ALLOCATE(poinsin(Ngridsin(ix,iy),2))
     5115
     5116        DO iv=1, Ngridsin(ix,iy)
     5117          poinsin(iv,1) = gridsin(ix,iy,iv,1)
     5118          poinsin(iv,2) = gridsin(ix,iy,iv,2)
     5119        END DO
     5120
     5121        areas(ix,iy) = shoelace_area_polygon(Nvertex, vertexgrid)
     5122        CALL spacepercen_within_reg(Nvertex, vertexgrid, dxB, dyB, NBvertexmax, xBBvals, yBBvals,     &
     5123          Ngridsin(ix,iy), poinsin, strict, percentages(ix,iy,:))
     5124
    49975125      END DO
    4998       IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
    4999       ALLOCATE(vertexgrid(Nvertex,2))
    5000       vertexgrid(:,1) = xBAvals(ix,iy,1:Nvertex)
    5001       vertexgrid(:,2) = yBAvals(ix,iy,1:Nvertex)
     5126    END DO
     5127
     5128    IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5129    IF (ALLOCATED(poinsin)) DEALLOCATE(poinsin)
     5130
     5131  END SUBROUTINE spacepercen
     5132
     5133  SUBROUTINE grid_spacepercen(xCAvals, yCAvals, xBAvals, yBAvals, xCBvals, yCBvals, xBBvals, yBBvals, &
     5134    dxA, dyA, NAvertexmax, dxB, dyB, dxyB, NBvertexmax, strict, Ngridsin, gridsin, areas, percentages)
     5135  ! Subroutine to compute the space-percentages of a series of grid cells (B) which lay inside another
     5136  !   series of grid-cells (A)
     5137  ! NOTE: Assuming coordinates on the plane with rectilinar, distance preserved and perpendicular x
     5138  !   and y axes.
     5139
     5140    IMPLICIT NONE
     5141
     5142    INTEGER, INTENT(in)                                  :: dxA, dyA, NAvertexmax
     5143    INTEGER, INTENT(in)                                  :: dxB, dyB, NBvertexmax, dxyB
     5144    REAL(r_k), DIMENSION(dxA,dyA), INTENT(in)            :: xCAvals, yCAvals
     5145    REAL(r_k), DIMENSION(dxB,dyB), INTENT(in)            :: xCBvals, yCBvals
     5146    REAL(r_k), DIMENSION(dxA,dyA,NAvertexmax), INTENT(in):: xBAvals, yBAvals
     5147    REAL(r_k), DIMENSION(dxB,dyB,NBvertexmax), INTENT(in):: xBBvals, yBBvals
     5148    LOGICAL, INTENT(in)                                  :: strict
     5149    INTEGER, DIMENSION(dxA,dyA), INTENT(out)             :: Ngridsin
     5150    INTEGER, DIMENSION(dxA,dyA,dxyB,2), INTENT(out)      :: gridsin
     5151    REAL(r_k), DIMENSION(dxB,dyB), INTENT(out)           :: areas
     5152    REAL(r_k), DIMENSION(dxA,dyA,dxyB), INTENT(out)      :: percentages
     5153
     5154! Local
     5155   INTEGER                                               :: iv, ix, iy
     5156   INTEGER                                               :: Nvertex
     5157   INTEGER, ALLOCATABLE, DIMENSION(:,:)                  :: poinsin
     5158   CHARACTER(len=20)                                     :: IS
     5159   REAL(r_k), ALLOCATABLE, DIMENSION(:)                  :: pareas
     5160   REAL(r_k), ALLOCATABLE, DIMENSION(:,:)                :: vertexgrid
     5161
     5162!!!!!!! Variables
     5163! dxA, dyA: shape of the matrix with the grid points A
     5164! xCAvals, yCAvals: coordinates of the center of the grid cells A
     5165! NAvertexmax: Maximum number of vertices of the grid cells A
     5166! xBAvals, yBAvals: coordinates of th vertices of the grid cells A (-99999 for no vertex)
     5167! dxB, dyB: shape of the matrix with the grid points B
     5168! xCBvals, yCBvals: coordinates of the center of the grid cells B
     5169! NBvertexmax: Maximum number of vertices of the grid cells B
     5170! xBBvals, yBBvals: coordinates of th vertices of the grid cells B (-99999 for no vertex)
     5171! strict: give an error if the area of the polygon is not fully covered
     5172! Ngridsin: number of grids from grid B with some extension within the grid cell A
     5173! gridsin: indices of B grids within the grids of A
     5174! areas: areas of the grids
     5175! percentages: percentages of area of cells B of each of the grids inside the grid cell A
     5176
     5177    fname = 'grid_spacepercen'
     5178
     5179    areas = zeroRK
     5180
     5181    DO ix = 1, dxA
     5182      DO iy = 1, dyA
     5183
     5184        ! Getting grid vertices
     5185        Nvertex = 0
     5186        DO iv=1, NAvertexmax
     5187          IF (xBAvals(ix,iy,iv) /= fillval64) THEN
     5188           Nvertex = Nvertex + 1
     5189          END IF
     5190        END DO
     5191        IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5192        ALLOCATE(vertexgrid(Nvertex,2))
     5193        vertexgrid(:,1) = xBAvals(ix,iy,1:Nvertex)
     5194        vertexgrid(:,2) = yBAvals(ix,iy,1:Nvertex)
    50025195 
    5003       CALL grid_within_polygon(Nvertex, vertexgrid, dxB, dyB, dxB*dyB, xCBvals, yCBvals, NBvertexmax, &
    5004         xBBvals, yBBvals, Ngridsin(ix,iy), gridsin(ix,iy,:,:))
     5196        CALL grid_within_polygon(Nvertex, vertexgrid, dxB, dyB, dxB*dyB, xCBvals, yCBvals,            &
     5197          NBvertexmax, xBBvals, yBBvals, Ngridsin(ix,iy), gridsin(ix,iy,:,:))
    50055198   
    5006       IF (ALLOCATED(poinsin)) DEALLOCATE(poinsin)
    5007       ALLOCATE(poinsin(Ngridsin(ix,iy),2))
    5008 
    5009       DO iv=1, Ngridsin(ix,iy)
    5010         poinsin(iv,1) = gridsin(ix,iy,iv,1)
    5011         poinsin(iv,2) = gridsin(ix,iy,iv,2)
     5199        IF (ALLOCATED(poinsin)) DEALLOCATE(poinsin)
     5200        ALLOCATE(poinsin(Ngridsin(ix,iy),2))
     5201        IF (ALLOCATED(pareas)) DEALLOCATE(pareas)
     5202        ALLOCATE(pareas(Ngridsin(ix,iy)))
     5203
     5204        DO iv=1, Ngridsin(ix,iy)
     5205          poinsin(iv,1) = gridsin(ix,iy,iv,1)
     5206          poinsin(iv,2) = gridsin(ix,iy,iv,2)
     5207        END DO
     5208
     5209        CALL grid_spacepercen_within_reg(Nvertex, vertexgrid, dxB, dyB, NBvertexmax, xBBvals,       &
     5210          yBBvals, Ngridsin(ix,iy), poinsin, strict, pareas, percentages(ix,iy,:))
     5211
     5212        ! Filling areas
     5213        DO iv = 1, Ngridsin(ix,iy)
     5214          IF (areas(poinsin(iv,1), poinsin(iv,2)) == zeroRK) THEN
     5215            areas(poinsin(iv,1), poinsin(iv,2)) = pareas(iv)
     5216          END IF
     5217        END DO
     5218
    50125219      END DO
    5013 
    5014       CALL spacepercen_within_reg(Nvertex, vertexgrid, dxB, dyB, NBvertexmax, xBBvals, yBBvals,       &
    5015         Ngridsin(ix,iy), poinsin, strict, percentages(ix,iy,:))
    5016 
    50175220    END DO
    5018     END DO
    5019 
    5020   END SUBROUTINE spacepercen
     5221
     5222    IF (ALLOCATED(vertexgrid)) DEALLOCATE(vertexgrid)
     5223    IF (ALLOCATED(pareas)) DEALLOCATE(pareas)
     5224    IF (ALLOCATED(poinsin)) DEALLOCATE(poinsin)
     5225
     5226  END SUBROUTINE grid_spacepercen
    50215227
    50225228  SUBROUTINE unique_matrixRK2D(dx, dy, dxy, matrix2D, Nunique, unique)
     
    50405246! dxy: dx*dy, maximum possible amount of different values
    50415247! matrix2D: matgrix of values
    5042 ! Nunique: amount of unique values
     5248! Nunique: amount of unique values 
    50435249! unique: sorted from minimum to maximum vector with the unique values
    50445250
Note: See TracChangeset for help on using the changeset viewer.