@@ -18,7 +18,7 @@ module mk_restarts_getidsMod
1818
1919contains
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
603588end module mk_restarts_getidsMod
0 commit comments