Skip to content

Commit 2f5654d

Browse files
authored
Merge pull request #1094 from GEOS-ESM/feature/wjiang/fix_remap
use nc4 tile file when remapping restarts
2 parents 8185f71 + 2fabc5c commit 2f5654d

File tree

9 files changed

+132
-181
lines changed

9 files changed

+132
-181
lines changed

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/mk_restarts/CatchmentRst.F90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,10 @@ subroutine re_tile(this, InTileFile, OutBcsDir, OutTileFile, surflay, rc)
807807
if (root_proc) then
808808
allocate (long (out_ntiles))
809809
allocate (latg (out_ntiles))
810-
call ReadTileFile_RealLatLon ( OutTileFile, n, long, latg)
810+
call ReadTileFile_RealLatLon ( OutTileFile, n, xlon=long, xlat=latg)
811811
_ASSERT( n == out_ntiles, "Out tile number should match")
812812
this%latg = latg
813-
call ReadTileFile_RealLatLon ( InTileFile, n, lonc, latc)
813+
call ReadTileFile_RealLatLon ( InTileFile, n, xlon=lonc, xlat=latc)
814814
_ASSERT( n == in_ntiles, "In tile number should match")
815815
endif
816816

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/mk_restarts/SaltImpConverter.F90

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ program SaltImpConverter
66
use MAPL_ConstantsMod,only: MAPL_PI, MAPL_radius
77
use netcdf
88
use MAPL
9-
use mk_restarts_getidsMod, only: ReadTileFile_IntLatLon
9+
use mk_restarts_getidsMod, only: ReadTileFile_RealLatLon
1010
use gFTL_StringVector
1111
implicit none
1212

@@ -18,8 +18,6 @@ program SaltImpConverter
1818
character*256 :: arg
1919

2020
integer :: i, rc, jc, iostat, iargc, n, mask,j,k,otiles,nsubtiles,l,itiles,nwords
21-
integer, pointer :: Lono(:), Lato(:), Id(:), Pf(:)
22-
integer, pointer :: Loni(:), Lati(:)
2321
real, allocatable :: varIn(:),varOut(:)
2422
real, allocatable :: TW(:),SW(:)
2523
real*8, allocatable :: varInR8(:),varOutR8(:)
@@ -113,13 +111,7 @@ program SaltImpConverter
113111
! Read Output Tile File .til file
114112
! to get the index into the pfafsttater table
115113

116-
call ReadTileFile_IntLatLon(InTileFile ,Pf,Id,loni,lati,zoom, 0)
117-
deallocate(Pf,Id)
118-
119-
nullify(Pf)
120-
nullify(Id)
121-
122-
itiles = size(loni) ! Input Tile Size
114+
call ReadTileFile_RealLatLon(InTileFile , itiles, mask = 0)
123115

124116
allocate( varIn(itiles) )
125117
allocate( varOut(itiles) )

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/mk_restarts/SaltIntSplitter.F90

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ program SaltIntSplitter
55
use MAPL_ConstantsMod,only: MAPL_PI, MAPL_radius
66
use netcdf
77
use MAPL
8-
use mk_restarts_getidsMod, only: ReadTileFile_IntLatLon
8+
use mk_restarts_getidsMod, only: ReadTileFile_RealLatLon
99
use gFTL_StringVector
1010
use gFTL_StringIntegerMap
1111

@@ -17,8 +17,6 @@ program SaltIntSplitter
1717
character*256 :: arg
1818

1919
integer :: i, rc, jc, iostat, iargc, n, mask,j,k,otiles,nsubtiles,l,itiles,nwords
20-
integer, pointer :: Lono(:), Lato(:), Id(:), Pf(:)
21-
integer, pointer :: Loni(:), Lati(:)
2220
real, allocatable :: varIn(:),varOut(:)
2321
real*8, allocatable :: varInR8(:),varOutR8(:)
2422
real, allocatable :: var2(:,:)
@@ -66,16 +64,8 @@ program SaltIntSplitter
6664
call getarg(1,InTileFile)
6765
call getarg(2,InRestart)
6866

69-
! Read Output Tile File .til file
70-
! to get the index into the pfafsttater table
7167

72-
call ReadTileFile_IntLatLon(InTileFile ,Pf,Id,loni,lati,zoom,0)
73-
deallocate(Pf,Id)
74-
75-
nullify(Pf)
76-
nullify(Id)
77-
78-
itiles = size(loni) ! Input Tile Size
68+
call ReadTileFile_RealLatLon(InTileFile, itiles, mask=0)
7969

8070
allocate( varIn(itiles), source = 0. )
8171
allocate( varOut(itiles), source = 0. )

GEOSagcm_GridComp/GEOSphysics_GridComp/GEOSsurface_GridComp/Utils/mk_restarts/getids.F90

Lines changed: 100 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module mk_restarts_getidsMod
1818

1919
contains
2020

21-
subroutine ReadTileFile_IntLatLon(Tf,Pf,Id,lon,lat,zoom,mask)
21+
subroutine ReadTileFile_IntLatLon(Tf, ntiles, zoom, lon_int, lat_int, mask)
2222

2323
! Read *.til tile definition file, return integer lat/lon for fast but inaccurate processing.
2424
! Can handle "old" format of *.til files, but that is probably obsolete as of March 2020 and
@@ -27,77 +27,32 @@ subroutine ReadTileFile_IntLatLon(Tf,Pf,Id,lon,lat,zoom,mask)
2727
! that is read into "Pf" depends on whether the file is for EASE or cube-sphere grid tiles!
2828
! - reichle, 4 Mar 2020
2929

30-
character*(*), intent(IN) :: Tf
31-
integer, pointer :: Pf(:), Id(:), lon(:), lat(:)
32-
integer, intent(in) :: zoom
30+
character*(*), intent(IN) :: Tf
31+
integer, intent(out) :: ntiles
32+
integer, intent(in) :: zoom
33+
integer, pointer, optional :: lon_int(:), lat_int(:)
3334
integer, optional, intent(IN) :: mask
34-
35-
integer, allocatable :: Pf1(:), Id1(:), ln1(:), lt1(:)
36-
integer :: k, i, nt, pfs, ids,n,msk, umask
35+
36+
real, pointer :: xlon(:), xlat(:)
37+
3738
real :: dum(4),dum1,lnn,ltt
3839
integer :: de, ce, st
39-
logical :: old
4040

41-
de=180*zoom
42-
ce=360*zoom
43-
st=2*zoom
44-
if(present(mask)) then
45-
umask = mask
41+
42+
if (present(lon_int) .and. present(lat_int)) then
43+
de=180*zoom
44+
ce=360*zoom
45+
call ReadTileFile_RealLatLon(Tf, ntiles, xlon=xlon, xlat=xlat, mask=mask)
46+
allocate(lon_int(ntiles), lat_int(ntiles))
47+
lon_int = nint(xlon*zoom)
48+
lat_int = max(min(nint(xlat*zoom),90*zoom),-90*zoom)
49+
where(lon_int<-de) lon_int = lon_int + ce
50+
where(lon_int> de) lon_int = lon_int - ce
51+
deallocate(xlon, xlat)
4652
else
47-
umask = 100
53+
call ReadTileFile_RealLatLon(Tf, ntiles, mask=mask)
4854
endif
49-
50-
print *, "Reading tilefile ",trim(Tf)
51-
52-
open(unit=20,file=trim(Tf),form='formatted')
53-
54-
read(20,*,iostat=n) Nt,i,k
55-
old=n<0
56-
close(20)
57-
58-
open(unit=20,file=trim(Tf),form='formatted')
59-
60-
read(20,*) Nt
61-
62-
do i=1,7
63-
read(20,*)
64-
enddo
65-
66-
allocate(Pf1(Nt),Id1(Nt),ln1(Nt),lt1(Nt))
67-
68-
n=0
69-
do i=1,Nt
70-
if(old) then
71-
read(20,*,end=200) msk, Pfs, lnn, ltt
72-
ids = 0
73-
else
74-
read(20,*,end=200) msk, dum1, lnn, ltt, dum, Pfs, Ids
75-
end if
76-
if(msk/=umask) cycle
77-
n = n+1
78-
pf1(n) = pfs
79-
Id1(n) = ids
80-
ln1(n) = nint(lnn*zoom)
81-
Lt1(n)=max(min(nint(ltt*zoom),90*zoom),-90*zoom)
82-
if(ln1(n)<-de) ln1(n) = ln1(n) + ce
83-
if(ln1(n)> de) ln1(n) = ln1(n) - ce
84-
enddo
85-
86-
200 continue
87-
88-
close(20)
89-
90-
Nt=n
91-
print *, "Found ",nt," land tiles."
92-
93-
allocate(Pf(Nt),Id(Nt),lon(Nt),lat(Nt))
94-
Pf = Pf1(:Nt)
95-
Id = Id1(:Nt)
96-
lon = ln1(:Nt)
97-
lat = lt1(:Nt)
98-
deallocate(Pf1,Id1,ln1,lt1)
99-
100-
return
55+
10156
end subroutine ReadTileFile_IntLatLon
10257

10358
subroutine GetStencil(ii,jj,st)
@@ -535,69 +490,99 @@ real function haversine(deglat1,deglon1,deglat2,deglon2)
535490

536491
! *****************************************************************************
537492

538-
subroutine ReadTileFile_RealLatLon (InCNTileFile, ntiles, xlon, xlat,mask)
493+
subroutine ReadTileFile_RealLatLon (InCNTileFile, ntiles, xlon, xlat, mask)
539494

540495
! read *.til tile definition file, return *real* lat/lon for slow but accurate processing
541496

542497
implicit none
543498
character(*), intent (in) :: InCNTileFile
544-
integer , intent (inout) :: ntiles
545-
real, pointer, dimension (:) :: xlon, xlat
499+
integer , intent (out) :: ntiles
500+
real, pointer, optional, dimension (:) :: xlon, xlat
546501
integer, optional, intent(IN) :: mask
547502
integer :: n,icnt,ityp, nt, umask, i, header
548503
real :: xval,yval, pf
549-
real, allocatable :: ln1(:), lt1(:)
550-
551-
if(present(mask)) then
552-
umask = mask
553-
else
554-
umask = 100
555-
endif
556-
557-
open(11,file=InCNTileFile, &
558-
form='formatted',action='read',status='old')
504+
real, allocatable :: ln1(:), lt1(:)
505+
real, pointer :: AVR(:,:)
506+
integer :: filetype, k
507+
integer, allocatable :: indices(:), indices_tmp(:)
508+
logical :: isNC4
509+
510+
if(present(mask)) then
511+
umask = mask
512+
else
513+
umask = 100
514+
endif
515+
516+
call MAPL_NCIOGetFileType(InCNTileFile, filetype)
517+
isNC4 = (filetype == MAPL_FILETYPE_NC4)
518+
519+
if (isNC4) then
520+
call MAPL_ReadTilingNC4(InCNTileFile, AVR=AVR)
521+
allocate(indices_tmp(size(AVR,1)))
522+
k = 0
523+
do i = 1, size(AVR,1)
524+
if( int(AVR(i,1)) == umask) then
525+
k = k+1
526+
indices_tmp(k) = i
527+
endif
528+
enddo
529+
indices = indices_tmp(1:k)
530+
Ntiles = k
531+
if ( present(xlon) .and. present(xlat)) then
532+
if(.not.associated (xlon)) allocate(xlon(Ntiles))
533+
if(.not.associated (xlat)) allocate(xlat(Ntiles))
534+
xlon = AVR(indices, 3)
535+
xlat = AVR(indices, 4)
536+
endif
537+
deallocate(AVR)
538+
else
559539

560-
! first read number of lines in the til file header
561-
! -------------------------------------------------
562-
header = 5
563-
read (11,*, iostat=n) Nt
564-
do i = 1, header -1
565-
read (11,*)
566-
end do
567-
read (11,*,IOSTAT=n)ityp,pf,xval, yval
568-
if(n /= 0) header = 8
540+
open(11,file=InCNTileFile, form='formatted',action='read',status='old')
569541

570-
rewind (11)
542+
! first read number of lines in the til file header
543+
! -------------------------------------------------
544+
header = 5
545+
read (11,*, iostat=n) Nt
546+
do i = 1, header -1
547+
read (11,*)
548+
end do
549+
read (11,*,IOSTAT=n)ityp,pf,xval, yval
550+
if(n /= 0) header = 8
571551

572-
! read the tile file
573-
!-------------------
574-
read (11,*, iostat=n) Nt
552+
rewind (11)
553+
554+
! read the tile file
555+
!-------------------
556+
read (11,*, iostat=n) Nt
575557

576-
allocate(ln1(Nt),lt1(Nt))
558+
allocate(ln1(Nt),lt1(Nt))
577559

578-
do n = 1,header-1 ! skip header
579-
read(11,*)
580-
end do
560+
do n = 1,header-1 ! skip header
561+
read(11,*)
562+
end do
581563

582-
icnt = 0
583-
584-
do i=1,Nt
585-
read(11,*) ityp,pf,xval,yval
586-
if(ityp == umask) then
587-
icnt = icnt + 1
588-
ln1(icnt) = xval
589-
Lt1(icnt) = yval
590-
endif
591-
end do
592-
593-
close(11)
594-
595-
Ntiles = icnt
596-
if(.not.associated (xlon)) allocate(xlon(Ntiles))
597-
if(.not.associated (xlat)) allocate(xlat(Ntiles))
598-
xlon = ln1(:Ntiles)
599-
xlat = lt1(:Ntiles)
600-
564+
icnt = 0
565+
566+
do i=1,Nt
567+
read(11,*) ityp,pf,xval,yval
568+
if(ityp == umask) then
569+
icnt = icnt + 1
570+
ln1(icnt) = xval
571+
Lt1(icnt) = yval
572+
endif
573+
end do
574+
575+
close(11)
576+
577+
Ntiles = icnt
578+
if ( present(xlon) .and. present(xlat)) then
579+
if(.not.associated (xlon)) allocate(xlon(Ntiles))
580+
if(.not.associated (xlat)) allocate(xlat(Ntiles))
581+
xlon = ln1(:Ntiles)
582+
xlat = lt1(:Ntiles)
583+
endif
584+
endif !isNC4
585+
601586
end subroutine ReadTileFile_RealLatLon
602587

603588
end module mk_restarts_getidsMod

0 commit comments

Comments
 (0)