test_pointlist_from_locStream_wmask Subroutine

subroutine test_pointlist_from_locStream_wmask(rc)

Arguments

Type IntentOptional Attributes Name
integer, intent(out) :: rc

Source Code

  subroutine test_pointlist_from_locStream_wmask(rc)
    integer, intent(out) :: rc

    integer :: localrc

    !LOCAL VARIABLES:
    type(ESMF_PointList) :: pointlist
    type(ESMF_VM) :: vm
    integer :: maxpts, mydims, mypts, myid
    type(ESMF_Mesh) :: myMesh
    integer :: numNodes
    integer :: numTriElems, numQuadElems, numTotElems
    integer, pointer :: elemIds(:),elemTypes(:),elemConn(:)
    integer, pointer :: nodeIds(:), nodeOwners(:)
    real(ESMF_KIND_R8), pointer :: nodeCoords(:)
    real(ESMF_KIND_R8), pointer :: elemCoords(:)
    integer :: petCount,localPet
    real(ESMF_KIND_R8), dimension(3) :: test_coords
    real(ESMF_KIND_R8) my_err1,my_err2,my_err3

    real(ESMF_KIND_R8), pointer :: lon(:),lat(:)
    real(ESMF_KIND_R8), pointer :: temperature(:)
    integer(ESMF_KIND_I4) :: maskValues(2)
    integer(ESMF_KIND_I4), pointer :: maskArray(:)
    integer :: local_pts,i
    type(ESMF_LocStream) :: myLocStream
    type(ESMF_Field) :: field_temperature

    ! get global VM
    call ESMF_VMGetGlobal(vm, rc=localrc)
    if (ESMF_LogFoundError(localrc, &
        ESMF_ERR_PASSTHRU, &
        ESMF_CONTEXT, rcToReturn=rc)) return

    call ESMF_VMGet(vm, localPet=localPet, petCount=petCount, rc=localrc)
    if (ESMF_LogFoundError(localrc, &
        ESMF_ERR_PASSTHRU, &
        ESMF_CONTEXT, rcToReturn=rc)) return

    ! If we don't have 1 or 4 PETS then exit successfully
    if ((petCount .ne. 1) .and. (petCount .ne. 4)) then
      print*,'ERROR:  test must be run using exactly 1 or 4 PETS - detected ',petCount
      rc=ESMF_FAILURE
      return
    endif

    ! setup source locStream
    local_pts=6

    !-------------------------------------------------------------------
    ! Allocate and set example Field data
    !-------------------------------------------------------------------
    allocate(temperature(local_pts))

    do i=1,local_pts
      temperature(i)=80.0+i
    enddo


    !-------------------------------------------------------------------
    ! Create the LocStream:  Allocate space for the LocStream object,
    ! define the number and distribution of the locations.
    !-------------------------------------------------------------------
    myLocStream=ESMF_LocStreamCreate(name="Equatorial Measurements", &
                                   localCount=local_pts, &
                                   coordSys=ESMF_COORDSYS_SPH_DEG, &
                                   rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble creating locStream'
      rc=ESMF_FAILURE
      return
    endif


    !-------------------------------------------------------------------
    ! Add key data (internally allocating memory).
    !-------------------------------------------------------------------
    call ESMF_LocStreamAddKey(myLocStream,                  &
                              keyName="ESMF:Lat",           &
                              KeyTypeKind=ESMF_TYPEKIND_R8, &
                              keyUnits="Degrees",           &
                              keyLongName="Latitude", rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble adding LocStream key for Lat'
      rc=ESMF_FAILURE
      return
    endif
    call ESMF_LocStreamAddKey(myLocStream,                  &
                              keyName="ESMF:Lon",           &
                              KeyTypeKind=ESMF_TYPEKIND_R8, &
                              keyUnits="Degrees",           &
                              keyLongName="Longitude", rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble adding LocStream key for Lon'
      rc=ESMF_FAILURE
      return
    endif
    call ESMF_LocStreamAddKey(myLocStream,                    &
                              keyName="ESMF:Mask",                &
                              KeyTypeKind=ESMF_TYPEKIND_I4, &
                              keyUnits="none",           &
                              keyLongName="mask values", rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble adding LocStream key for Mask'
      rc=ESMF_FAILURE
      return
    endif
    !-------------------------------------------------------------------
    ! Get key data.
    !-------------------------------------------------------------------
    call ESMF_LocStreamGetKey(myLocStream,                  &
                              localDE=0,                    &
                              keyName="ESMF:Lat",           &
                              farray=lat,                   &
                              rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble getting LocStream key for Lat'
      rc=ESMF_FAILURE
      return
    endif
    call ESMF_LocStreamGetKey(myLocStream,                  &
                              localDE=0,                    &
                              keyName="ESMF:Lon",           &
                              farray=lon,                   &
                              rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble getting LocStream key for Lon'
      rc=ESMF_FAILURE
      return
    endif
    call ESMF_LocStreamGetKey(myLocStream,                    &
                              localDE=0,                    &
                              keyName="ESMF:Mask",                &
                              farray=maskArray,                   &
                              rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble getting LocStream key for Lon'
      rc=ESMF_FAILURE
      return
    endif
    !-------------------------------------------------------------------
    ! Set key data.
    !-------------------------------------------------------------------
    do i=1,local_pts
       lon(i)=(i-1)*360.0/local_pts
       lat(i)=0.0
    enddo

    maskArray(1)=0
    maskArray(2)=1
    maskArray(3)=2
    maskArray(4)=2
    maskArray(5)=1
    maskArray(6)=0

    !-------------------------------------------------------------------
    ! Create a Field on the Location Stream. In this case the
    ! Field is created from a user array, but any of the other
    ! Field create methods (e.g. from ArraySpec) would also apply.
    !-------------------------------------------------------------------
    field_temperature=ESMF_FieldCreate(myLocStream,   &
                                       temperature, &
                                       name="temperature", &
                                       rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble creating field on locStream'
      rc=ESMF_FAILURE
      return
    endif

    maskValues=(/0,2/)
    pointlist=ESMF_PointListCreate(myLocStream, maskValues=maskValues, rc=localrc)
    if (localrc /= ESMF_SUCCESS) then
      print*,'ERROR:  trouble creating pointlist'
      rc=ESMF_FAILURE
      return
    endif       

    call ESMF_PointListGet(pointlist, dims=mydims, numpts=mypts, maxpts=maxpts, rc=localrc)
    if (localrc /= ESMF_SUCCESS) then
       print*,'ERROR: trouble accessing pointlist data with get routine'
       rc=ESMF_FAILURE
       return
    endif

    if (maxpts .ne. 2 .or. mypts .ne. 2 .or. mydims .ne. 3) then
       print*,'ERROR:  unexpected values for newly created pointlist:'
       print*,'maxpts should be: ',local_pts,' got: ',maxpts
       print*,'numpts should be: ',local_pts,' got: ',mypts
       print*,'dims should be: 2  got: ',mydims
       rc=ESMF_FAILURE
       return
    endif

!    call ESMF_PointListPrint(pointlist)
!    if (localrc /= ESMF_SUCCESS) then
!       rc=ESMF_FAILURE
!       return
!    endif      


    if (local_pts .gt. 0) then

      !locations values are zero based
      call ESMF_PointListGetForLoc(pointlist,1,loc_coords=test_coords,rc=localrc)
      if (localrc /= ESMF_SUCCESS) then
        print*,'ERROR:  trouble accessing pointlist data with get for location routine'
        rc=ESMF_FAILURE
        return
      endif

      my_err1 = abs(test_coords(1) - (-0.5))
      my_err2 = abs(test_coords(2) - (-0.866))
      my_err3 = abs(test_coords(3) - (-0.0))
      if (my_err1 .gt. .0001 .or. my_err2 .gt. .0001 .or. my_err3 .gt. .0001) then
        print*,'ERROR:  unexpected coordinates for queried pointlist location:'
        print*,'expected ( 0.5 , 0.866 , 0 )  got  (',test_coords(1),',',test_coords(2),',',test_coords(3),')'
        rc=ESMF_FAILURE
        return
      endif

    endif

    deallocate(temperature)

    call ESMF_PointListDestroy(pointlist,rc=localrc)
    if (localrc /= ESMF_SUCCESS) then
      print*,'ERROR:  trouble destroying pointlist'
      rc=ESMF_FAILURE
      return
    endif       


    call ESMF_LocStreamDestroy(myLocStream, rc=localrc)
    if (localrc /=ESMF_SUCCESS) then
      print*,'ERROR:  trouble destroying LocStream'
      rc=ESMF_FAILURE
      return
    endif

    rc=ESMF_SUCCESS
  end subroutine test_pointlist_from_locStream_wmask