Changeset 1620 in lmdz_wrf


Ignore:
Timestamp:
Sep 7, 2017, 2:52:14 PM (8 years ago)
Author:
lfita
Message:

Adding in `polygon_properties' calculation of:

  • area-weighted center
  • coordinates of the minimum and maximum values
  • coordinates of the area-weighted and distance to minium/maximum center of the polygon
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/module_scientific.f90

    r1618 r1620  
    3232  CONTAINS
    3333
    34   SUBROUTINE all_polygons_properties(dx, dy, Npoly, polys, lon, lat, values, xres, yres, projN,       &
    35     Npolyptss, xxtrms, yxtrms, meanctrs, areas, nvals, xvals, mvals, m2vals, stdvals, Nquant, quants)
     34  SUBROUTINE all_polygons_properties(dbg, dx, dy, Npoly, polys, lon, lat, values, xres, yres, projN,  &
     35    Npolyptss, xxtrms, yxtrms, meanctrs, meanwctrs, areas, nvals, xvals, mvals, m2vals, stdvals,      &
     36    Nquant, quants, nvcoords, xvcoords, meanvnctrs, meanvxctrs)
    3637! Subroutine to determine the properties of all polygons in a 2D field:
    3738!   Number of grid points
    3839!   grid-point coordinates of the minimum and maximum of the path along x,y axes
    39 !   center from the mean of the coordinates of the paths locations
     40!   grid coordinates of center from the mean of the coordinates of the poygon locations
     41!   lon, lat center from the area weighted mean of the coordinates of the polygon locations
    4042!   area of the polygon (km2)
    4143!   minimum and maximum of the values within the polygon
     
    4547!   number of quantiles
    4648!   quantiles of the values within the polygon
     49!   grid coordinates of the minimum, maximum value within the polygon
     50!   lon, lat coordinates of the area center weighted and also by distance to the lowest or highest values of the polygon
    4751
    4852  IMPLICIT NONE
    4953
     54  LOGICAL, INTENT(in)                                    :: dbg
    5055  INTEGER, INTENT(in)                                    :: dx, dy, Npoly, Nquant
    5156  INTEGER, DIMENSION(dx,dy), INTENT(in)                  :: polys
    5257  REAL(r_k), DIMENSION(dx,dy), INTENT(in)                :: lon, lat, values
    5358  REAL(r_k), INTENT(in)                                  :: xres, yres
    54   CHARACTER(len=50), INTENT(in)                          :: projN
     59  CHARACTER(len=1000), INTENT(in)                        :: projN
    5560  INTEGER, DIMENSION(Npoly), INTENT(out)                 :: Npolyptss
    56   INTEGER, DIMENSION(Npoly, 2), INTENT(out)              :: xxtrms, yxtrms, meanctrs
     61  INTEGER, DIMENSION(Npoly,2), INTENT(out)               :: xxtrms, yxtrms, meanctrs
    5762  REAL(r_k), DIMENSION(Npoly), INTENT(out)               :: areas
    5863  REAL(r_k), DIMENSION(Npoly), INTENT(out)               :: nvals, xvals, mvals, m2vals, stdvals
    5964  REAL(r_k), DIMENSION(Npoly, Nquant), INTENT(out)       :: quants
     65  INTEGER, DIMENSION(Npoly,2), INTENT(out)               :: nvcoords, xvcoords
     66  REAL(r_k), DIMENSION(Npoly,2), INTENT(out)             :: meanwctrs, meanvnctrs, meanvxctrs
    6067
    6168! Local
     
    7279! projN: name of the projection
    7380!   'lon/lat': for regular longitude-latitude
     81!   'nadir-sat,[lonNADIR],[latNADIR]': for satellite data with the resolution given at nadir (lonNADIR, latNADIR)
    7482! Npolyptss: number of points of the polygons
    7583! [x/y]xtrms: grid-point coordinates of minimum and maximum coordinates of the polygons
    7684! meanctrs: center from the mean of the coordinates of the polygons
     85! meanwctrs: lon, lat coordinates of the center from the spatial-weighted mean of the polygons
    7786! areas: area of the polygons [km]
    7887! [n/x]vals: minimum and maximum of the values within the polygons
     
    8291! Nquant: number of quantiles
    8392! quants: quantiles of the values within the polygons
     93! [n/x]vcoords: grid coordinates of the minimum/maximum of the values within the polygons
     94! meanv[n/x]ctrs: lon, lat coordinates of the area center weighted and also by distance to the lowest or highest values of the polygons
    8495
    8596  fname = 'all_polygons_properties'
     
    90101  yxtrms = fillval64
    91102  meanctrs = fillval64
     103  meanwctrs = fillval64
    92104  areas = fillval64
    93105  nvals = -1
     
    97109  stdvals = fillval64
    98110  quants = fillval64
     111  nvcoords = -1
     112  xvcoords = -1
     113  meanvnctrs = fillval64
     114  meanvxctrs = fillval64
    99115
    100116  DO ip=1, Npoly
    101117    boolpoly = polys == ip
    102     CALL polygon_properties(dx, dy, boolpoly, lon, lat, values, xres, yres, projN, Npolyptss(ip),     &
    103       xxtrms(ip,:), yxtrms(ip,:), meanctrs(ip,:), areas(ip), nvals(ip), xvals(ip), mvals(ip),         &
    104       m2vals(ip), stdvals(ip), Nquant, quants(ip,:))
     118    CALL polygon_properties(dbg, dx, dy, boolpoly, lon, lat, values, xres, yres, projN, Npolyptss(ip),&
     119      xxtrms(ip,:), yxtrms(ip,:), meanctrs(ip,:), meanwctrs(ip,:), areas(ip), nvals(ip), xvals(ip),   &
     120      mvals(ip), m2vals(ip), stdvals(ip), Nquant, quants(ip,:), nvcoords(ip,:), xvcoords(ip,:),       &
     121      meanvnctrs(ip,:), meanvxctrs(ip,:))
    105122  END DO
    106123
     
    109126  END SUBROUTINE all_polygons_properties
    110127
    111   SUBROUTINE polygon_properties(dx, dy, poly, lon, lat, values, xres, yres, projN, Npolypts, xxtrm,   &
    112     yxtrm, meanctr, area, nval, xval, mval, m2val, stdval, Nquant, quant)
     128  SUBROUTINE polygon_properties(dbg, dx, dy, poly, lon, lat, values, xres, yres, projN, Npolypts,     &
     129    xxtrm, yxtrm, meanctr, meanwctr, area, nval, xval, mval, m2val, stdval, Nquant, quant, nvcoord,   &
     130    xvcoord, meanvnctr, meanvxctr)
    113131! Subroutine to determine the properties of a polygon (as .TRUE. matrix)
    114132!   Number of grid points
    115133!   grid-point coordinates of the minimum and maximum of the path along x,y axes
    116 !   center from the mean of the coordinates of the paths locations
     134!   grid coordinates of center from the mean of the coordinates of the poygon locations
     135!   lon, lat center from the area weighted mean of the coordinates of the polygon locations
    117136!   area of the polygon (km2)
    118137!   minimum and maximum of the values within the polygon
     
    122141!   number of quantiles
    123142!   quantiles of the values within the polygon
     143!   grid coordinates of the minimum, maximum value within the polygon
     144!   lon, lat coordinates of the area center weighted and also by distance to the lowest or highest values of the polygon
    124145
    125146  IMPLICIT NONE
    126147
     148  LOGICAL, INTENT(in)                                    :: dbg
    127149  INTEGER, INTENT(in)                                    :: dx, dy, Nquant
    128150  LOGICAL, DIMENSION(dx,dy), INTENT(in)                  :: poly
    129151  REAL(r_k), DIMENSION(dx,dy), INTENT(in)                :: lon, lat, values
    130152  REAL(r_k), INTENT(in)                                  :: xres, yres
    131   CHARACTER(len=50), INTENT(in)                          :: projN
     153  CHARACTER(len=1000), INTENT(in)                        :: projN
    132154  INTEGER, INTENT(out)                                   :: Npolypts
    133155  INTEGER, DIMENSION(2), INTENT(out)                     :: xxtrm, yxtrm, meanctr
     156  INTEGER, DIMENSION(2), INTENT(out)                     :: nvcoord, xvcoord
     157  REAL(r_k), DIMENSION(2), INTENT(out)                   :: meanwctr, meanvnctr, meanvxctr
    134158  REAL(r_k), INTENT(out)                                 :: area
    135159  REAL(r_k), INTENT(out)                                 :: nval, xval, mval, m2val, stdval
     
    140164  INTEGER                                                :: ierr
    141165  INTEGER, DIMENSION(:,:), ALLOCATABLE                   :: polypts
    142   REAL(r_k), DIMENSION(:), ALLOCATABLE                   :: polyvals
     166  REAL(r_k), DIMENSION(:), ALLOCATABLE                   :: polyvals, distvn, distvx
     167  REAL(r_k)                                              :: lonNADIR, latNADIR
     168  REAL(r_k)                                              :: sumRESx, sumRESy
     169  REAL(r_k), DIMENSION(dx,dy)                            :: xcorr, ycorr
     170  CHARACTER(len=200), DIMENSION(3)                       :: satSvals
     171  CHARACTER(len=50)                                      :: projS
     172  REAL(r_k)                                              :: sumDISTnlon, sumDISTnlat, sumDISTxlon,   &
     173    sumDISTxlat
    143174
    144175!!!!!!! Variables
     
    149180! [x/y]res resolution along the x and y axis
    150181! projN: name of the projection
     182!   'ctsarea': Constant Area
    151183!   'lon/lat': for regular longitude-latitude
     184!   'nadir-sat,[lonNADIR],[latNADIR]': for satellite data with the resolution given at nadir (lonNADIR, latNADIR)
    152185! Npolypts: number of points of the polygon
    153186! [x/y]xtrm: grid-point coordinates of minimum and maximum coordinates of the polygon
    154187! meanctr: center from the mean of the coordinates of the polygon
     188! meanwctr: lon, lat coordinates of the center from the spatial-weighted mean of the polygon
    155189! area: area of the polygon [km]
    156190! [n/x]val: minimum and maximum of the values within the polygon
     
    159193! stdval: standard deviation of the values within the polygon
    160194! Nquant: number of quantiles
    161 ! quant: quantiles of the values within the polygon
     195! quant: quantiles of the values within the polygon
     196! [n/x]vcoord: grid coordinates of the minimum/maximum of the values within the polygon
     197! meanv[n/x]ctr: lon, lat coordinates of the area center weighted and also by distance to the lowest or highest values of the polygon
    162198
    163199  fname = 'polygon_properties'
     200
     201  IF (dbg) PRINT *,"  '" // TRIM(fname) // "' ..."
    164202
    165203  ! Getting grid-point coordinates of the polygon
     
    176214  CALL ErrMsg(msg, fname, ierr)
    177215
     216  IF (ALLOCATED(distvn)) DEALLOCATE(distvn)
     217  ALLOCATE(distvn(Npolypts), STAT=ierr)
     218  msg = "Problems allocating 'distvn'"
     219  CALL ErrMsg(msg, fname, ierr)
     220
     221  IF (ALLOCATED(distvx)) DEALLOCATE(distvx)
     222  ALLOCATE(distvx(Npolypts), STAT=ierr)
     223  msg = "Problems allocating 'distvx'"
     224  CALL ErrMsg(msg, fname, ierr)
     225
     226  IF (projN(1:7) == 'lon/lat') THEN
     227    projS = projN
     228  ELSE IF (projN(1:7) == 'ctsarea') THEN
     229    projS = projN
     230  ELSE IF (projN(1:9) == 'nadir-sat') THEN
     231    projS = 'nadir-sat'
     232    CALL split(projN, ',', 3, satSvals)
     233    READ(satSvals(2),'(F200.100)')lonNadir
     234    READ(satSvals(3),'(F200.100)')latNadir
     235    IF (dbg) PRINT *,"  'nadir-geostationary-satellite' based projection of data with nadir " //      &
     236      "location at:", lonNadir, latNadir
     237  ELSE
     238    msg = "Projection '" // TRIM(projN) // "' not ready" // CHAR(10) // " available ones: " //        &
     239       "'ctsarea', 'lon/lat', 'nadir-sat'"
     240    CALL ErrMsg(msg,fname,-1)
     241  END IF
     242
     243  area = 0.
     244  sumRESx = 0.
     245  sumRESy = 0.
     246  meanwctr = 0.
     247  meanvnctr = 0.
     248  meanvxctr = 0.
     249
     250  nval = fillval64
     251  xval = -fillval64
     252
    178253  ip = 1
    179   area = 0.
    180254  DO i=1,dx
    181255    DO j=1,dy
     
    183257        polypts(ip,:) = (/ i,j /)
    184258        polyvals(ip) = values(i,j)
    185         SELECT CASE (TRIM(projN))
     259        SELECT CASE (TRIM(projS))
     260          CASE ('ctsarea')
     261            ! Constant Area
     262            xcorr(i,j) = 1.
     263            ycorr(i,j) = 1.
    186264          CASE ('lon/lat')
    187265            ! Area as fixed yres and sinus-lat varying for xres
    188             area = area + xres*DABS(DSIN(lon(i,j)*DegRad))*yres
    189           CASE DEFAULT
    190             msg = "Projection '" // TRIM(projN) // "' not ready" // CHAR(10) //                       &
    191               " available ones: 'lon/lat'"
     266            xcorr(i,j) = DABS(DSIN(lon(i,j)*DegRad))
     267            ycorr(i,j) = 1.
     268          CASE ('nadir-sat')
     269            ! Area from nadir resolution and degrading as we get far from satellite's nadir
     270            ! GOES-E: 0 N, 75 W
     271            xcorr(i,j) = DABS(DSIN(lon(i,j)*DegRad))
     272            ycorr(i,j) = 1.
    192273        END SELECT
     274        area = area + xres*xcorr(i,j)*yres*ycorr(i,j)
     275        meanwctr(1) = meanwctr(1) + lon(i,j)*xres*xcorr(i,j)
     276        meanwctr(2) = meanwctr(2) + lat(i,j)*yres*ycorr(i,j)
     277        IF (nval > values(i,j)) THEN
     278          nvcoord(1) = i
     279          nvcoord(2) = j
     280          nval = values(i,j)
     281        END IF
     282        IF (xval < values(i,j)) THEN
     283          xvcoord(1) = i
     284          xvcoord(2) = j
     285          xval = values(i,j)
     286        END IF
    193287        ip = ip + 1
    194288      END IF
    195289    END DO
    196290  END DO
     291
     292  IF (dbg) THEN
     293    PRINT *,'  Values: ', polyvals
     294  END IF
     295
     296  sumRESx = xres*SUM(xcorr)
     297  sumRESy = yres*SUM(ycorr)
    197298
    198299  xxtrm = (/ MINVAL(polypts(:,1)), MAXVAL(polypts(:,1)) /)
    199300  yxtrm = (/ MINVAL(polypts(:,2)), MAXVAL(polypts(:,2)) /)
    200301  meanctr = (/ SUM(polypts(:,1))/Npolypts, SUM(polypts(:,2))/Npolypts /)
    201 
     302  meanwctr = (/ meanwctr(1)/sumRESx, meanwctr(2)/sumRESy /)
     303
     304  IF (dbg) THEN
     305    PRINT *,'  mean grid center: ', meanctr, ' weighted mean center: ', meanwctr
     306  END IF
     307 
    202308  ! Statistics of the values within the polygon
    203309  CALL StatsR_K(Npolypts, polyvals, nval, xval, mval, m2val, stdval)
     310
     311  IF (dbg) THEN
     312    PRINT *,'    minimum value: ', nval, ' maximum:', xval, ' mean:', mval
     313    PRINT *,'    coor. minimum: ', nvcoord
     314    PRINT *,'    coor. maximum: ', xvcoord
     315  END IF
     316
     317  ! Mean center weighted to minimum and maximum values
     318  distvn = DABS(polyvals - nval)
     319  distvx = DABS(polyvals - xval)
     320
     321  meanvnctr = 0.
     322  meanvxctr = 0.
     323  sumDISTnlon = 0.
     324  sumDISTnlat = 0.
     325  sumDISTxlon = 0.
     326  sumDISTxlat = 0.
     327
     328  ip = 1
     329  DO i=1,dx
     330    DO j=1,dy
     331      IF (poly(i,j)) THEN
     332        meanvnctr(1) = meanvnctr(1)+lon(i,j)*distvn(ip)*xres*xcorr(i,j)
     333        meanvnctr(2) = meanvnctr(2)+lat(i,j)*distvn(ip)*yres*ycorr(i,j)
     334
     335        meanvxctr(1) = meanvxctr(1)+lon(i,j)*distvx(ip)*xres*xcorr(i,j)     
     336        meanvxctr(2) = meanvxctr(2)+lat(i,j)*distvx(ip)*yres*ycorr(i,j)
     337
     338        sumDISTnlon = sumDISTnlon + lon(i,j)*distvn(ip)*xres*xcorr(i,j)
     339        sumDISTnlat = sumDISTnlat + lat(i,j)*distvn(ip)*yres*ycorr(i,j)
     340        sumDISTxlon = sumDISTxlon + lon(i,j)*distvx(ip)*xres*xcorr(i,j)
     341        sumDISTxlat = sumDISTxlat + lat(i,j)*distvx(ip)*yres*ycorr(i,j)
     342
     343        ip = ip + 1
     344      END IF
     345    END DO
     346  END DO
     347
     348  meanvnctr = (/ meanvnctr(1)/sumDISTnlon, meanvnctr(2)/sumDISTnlat /)
     349  meanvxctr = (/ meanvxctr(1)/sumDISTxlon, meanvxctr(2)/sumDISTxlat /)
     350
     351  IF (dbg) THEN
     352    PRINT *,'  mean center to minimum: ', meanvnctr, ' to maximum: ', meanvxctr
     353  END IF
    204354
    205355  ! Quantiles of the values within the polygon
Note: See TracChangeset for help on using the changeset viewer.