convert_corner_arrays_to_1D Subroutine

private subroutine convert_corner_arrays_to_1D(isSphere, dim1, dim2, cornerX2D, cornerY2D, cornerX, cornerY, rc)

Arguments

Type IntentOptional Attributes Name
logical :: isSphere
integer :: dim1
integer :: dim2
real(kind=ESMF_KIND_R8) :: cornerX2D(:,:)
real(kind=ESMF_KIND_R8) :: cornerY2D(:,:)
real(kind=ESMF_KIND_R8) :: cornerX(:)
real(kind=ESMF_KIND_R8) :: cornerY(:)
integer :: rc

Source Code

subroutine convert_corner_arrays_to_1D(isSphere,dim1,dim2,cornerX2D,cornerY2D,cornerX,cornerY, rc)
 logical :: isSphere
 integer :: dim1,dim2
 real(ESMF_KIND_R8) :: cornerX2D(:,:),cornerY2D(:,:)
 real(ESMF_KIND_R8) :: cornerX(:),cornerY(:)
 integer :: rc

 integer :: localrc
 integer :: i,j
 real(ESMF_KIND_R8) :: tol=0.0000000001
 logical :: foundAlign
 integer :: topCorner
 integer :: topRightCorner
 integer :: BtmRightCorner
 integer :: btmCorner
 logical :: matches
 integer :: count,inPos,outPos
 integer :: ip1,im1

 ! make sure no dimensions are 0
 if ((dim1 < 1) .or. (dim2 <1)) then
     call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_WRONG,msg="- Currently can't handle a grid of width <1 in a dim.", &
          ESMF_CONTEXT, rcToReturn=rc)
     return
 endif

 ! Handle 1 width cases
 if ((dim1 == 1) .and. (dim2 == 1)) then
   ! Put corner array into Grid in an order
   ! so that it'll be recoverted to a
   ! Mesh element in the same order

    cornerX(1)=cornerX2D(1,1)
    cornerY(1)=cornerY2D(1,1)

    cornerX(2)=cornerX2D(2,1)
    cornerY(2)=cornerY2D(2,1)

    cornerX(3)=cornerX2D(4,1)
    cornerY(3)=cornerY2D(4,1)

    cornerX(4)=cornerX2D(3,1)
    cornerY(4)=cornerY2D(3,1)
    return
 endif

 ! Find the alignment of the corners
 call find_corner_align(1,dim1,dim2,cornerX2D,cornerY2D, &
      foundAlign,topCorner,topRightCorner,btmRightCorner,btmCorner,rc=localrc)
 if (ESMF_LogFoundError(localrc, &
      ESMF_ERR_PASSTHRU, &
      ESMF_CONTEXT, rcToReturn=rc)) return

 ! Try second row
 if (.not. foundAlign) then
    call find_corner_align(1+dim1,dim1,dim2,cornerX2D,cornerY2D, &
         foundAlign,topCorner,topRightCorner,btmRightCorner,btmCorner,rc=localrc)
    if (ESMF_LogFoundError(localrc, &
         ESMF_ERR_PASSTHRU, &
         ESMF_CONTEXT, rcToReturn=rc)) return
 endif

#if 0
 ! Debug output
 write(*,*) "topCorner=",topCorner
 write(*,*) "topRightCorner=",topRightCorner
 write(*,*) "btmRightCorner=",btmRightCorner
 write(*,*) "btmCorner=",btmCorner
#endif

 ! If we couldn't find an align then return error
 if (.not. foundAlign) then
    call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_WRONG, &
         msg=" Couldn't find a consistent ordering of corners around each cell in file"// & 
             " to be able to arrange them into a logically rectangular Grid.", &
         ESMF_CONTEXT, rcToReturn=rc)
    return
 endif

#if 0
! Error check corner info from file to make sure corners are consistent throughout file
if (isSphere) then
   write(*,*)
   write(*,*) "Error checking spherical Grid..."

   ! Init to starting pos
   inPos=0

   ! Check rows up to btm row
   do i=1,dim2-1
      do j=1,dim1-1
         inPos=inPos+1
         if ((cornerX2D(TopRightCorner,inPos) .ne. cornerX2D(TopCorner,inPos+1)) .and. &
              (cornerY2D(TopRightCorner,inPos) .ne. cornerY2D(TopCorner,inPos+1))) then
            write(*,*) "TopRightCorner of ",inPos," doesn't match TopCorner of ",inPos+1
            write(*,*) cornerX2D(TopRightCorner,inPos),cornerY2D(TopRightCorner,inPos)," .ne. ", &
                       cornerX2D(TopCorner,inPos+1),cornerY2D(TopCorner,inPos+1)
            !              stop
         endif

         if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(BtmCorner,inPos+1)) .and. &
              (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(BtmCorner,inPos+1))) then
            write(*,*) "BtmRightCorner of ",inPos," doesn't match BtmCorner of ",inPos+1
            !           stop
         endif

         if ((cornerX2D(BtmCorner,inPos) .ne. cornerX2D(TopCorner,inPos+dim1)) .and. &
              (cornerY2D(BtmCorner,inPos) .ne. cornerY2D(TopCorner,inPos+dim1))) then
            write(*,*) "BtmCorner of ",inPos," doesn't match TopCorner of ",inPos+dim1
            !           stop
         endif

         if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(TopRightCorner,inPos+dim1)) .and. &
              (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(TopRightCorner,inPos+dim1))) then
            write(*,*) "BtmRightCorner of ",inPos," doesn't match TopRightCorner of ",inPos+dim1
            !           stop
         endif
      enddo

      ! Check last point in row with beginning of row
      inPos=inPos+1
      if ((cornerX2D(TopRightCorner,inPos) .ne. cornerX2D(TopCorner,inPos-dim1+1)) .and. &
           (cornerY2D(TopRightCorner,inPos) .ne. cornerY2D(TopCorner,inPos-dim1+1))) then
         write(*,*) "TopRightCorner of ",inPos," doesn't match TopCorner of ",inPos-dim1+1
         !        stop
      endif

      if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(BtmCorner,inPos-dim1+1)) .and. &
           (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(BtmCorner,inPos-dim1+1))) then
         write(*,*) "BtmRightCorner of ",inPos," doesn't match BtmCorner of ",inPos-dim1+1
         stop
      endif


      if ((cornerX2D(BtmCorner,inPos) .ne. cornerX2D(TopCorner,inPos+dim1)) .and. &
           (cornerY2D(BtmCorner,inPos) .ne. cornerY2D(TopCorner,inPos+dim1))) then
         write(*,*) "BtmCorner of ",inPos," doesn't match TopCorner of ",inPos+dim1
         !        stop
      endif

      if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(TopRightCorner,inPos+dim1)) .and. &
           (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(TopRightCorner,inPos+dim1))) then
         write(*,*) "BtmRightCorner of ",inPos," doesn't match TopRightCorner of ",inPos+dim1
         !        stop
      endif
   enddo


   ! Check bottom row
   do j=1,dim1-1
      inPos=inPos+1
      if ((cornerX2D(TopRightCorner,inPos) .ne. cornerX2D(TopCorner,inPos+1)) .and. &
           (cornerY2D(TopRightCorner,inPos) .ne. cornerY2D(TopCorner,inPos+1))) then
         write(*,*) "TopRightCorner of ",inPos," doesn't match TopCorner of ",inPos+1
         !              stop
      endif

      if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(BtmCorner,inPos+1)) .and. &
           (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(BtmCorner,inPos+1))) then
         write(*,*) "BtmRightCorner of ",inPos," doesn't match BtmCorner of ",inPos+1
         !           stop
      endif
   enddo


   ! Check last point in row with beginning of row
   inPos=inPos+1
   if ((cornerX2D(TopRightCorner,inPos) .ne. cornerX2D(TopCorner,inPos-dim1+1)) .and. &
        (cornerY2D(TopRightCorner,inPos) .ne. cornerY2D(TopCorner,inPos-dim1+1))) then
      write(*,*) "TopRightCorner of ",inPos," doesn't match TopCorner of ",inPos-dim1+1
      !        stop
   endif

   if ((cornerX2D(BtmRightCorner,inPos) .ne. cornerX2D(BtmCorner,inPos-dim1+1)) .and. &
        (cornerY2D(BtmRightCorner,inPos) .ne. cornerY2D(BtmCorner,inPos-dim1+1))) then
      write(*,*) "BtmRightCorner of ",inPos," doesn't match BtmCorner of ",inPos-dim1+1
      !     stop
   endif
else
   ! TODO: Check regional grid


endif


#endif

  ! Set Corner info
  if (isSphere) then
     count=size(cornerX2D,2)
     do i=1,count
        cornerX(i)=cornerX2D(TopCorner,i)
        cornerY(i)=cornerY2D(TopCorner,i)
     enddo

     do i=1,dim1
        cornerX(i+count)=cornerX2D(BtmCorner,count-dim1+i)
        cornerY(i+count)=cornerY2D(BtmCorner,count-dim1+i)
     enddo
  else
     ! Set Corner info
     inPos=0
     outPos=0
     do i=1,dim2
        do j=1,dim1
           inPos=inPos+1
           outPos=outPos+1
           cornerX(outPos)=cornerX2D(TopCorner,inPos)
           cornerY(outPos)=cornerY2D(TopCorner,inPos)
        enddo
        outPos=outPos+1
        cornerX(outPos)=cornerX2D(TopRightCorner,inPos)
        cornerY(outPos)=cornerY2D(TopRightCorner,inPos)
     enddo

     inPos=inPos-dim1
     do i=1,dim1
        inPos=inPos+1
        outPos=outPos+1
        cornerX(outPos)=cornerX2D(BtmCorner,inPos)
        cornerY(outPos)=cornerY2D(BtmCorner,inPos)
     enddo

     outPos=outPos+1
     cornerX(outPos)=cornerX2D(BtmRightCorner,inPos)
     cornerY(outPos)=cornerY2D(BtmRightCorner,inPos)
  endif

 ! return success
 rc=ESMF_SUCCESS

end subroutine convert_corner_arrays_to_1D