source: flex_extract.git/Source/Fortran/rwgrib2.f90 @ dfa7dbd

ctbtodev
Last change on this file since dfa7dbd was dfa7dbd, checked in by pesei <petra seibert -@…>, 5 years ago

changes in the Fortran part and associated regression test

2019-08-21 PS
introduce the "new" versions of source files:

all .f90 free format
code beautification
regression test is OK

make new local gfortran makefiles, remove parameters not needed
anymore

change filenames rwgrib.f90 (all lower), preconvert to calc_etadot,
adapt messages and comments in calc_etadot.f90
adapt all makefiles to new filenames
adapt success message of logfiles in regression test references
redo regression test OK

provide softlinks for standards:

calc_etadot.out -> calc_etadot_fast.out
makefile_local_gfortran -> makefile_fast

provide changelog.txt in Fortran
provide readme.txt in Regression/FortranEtadot?

  • Property mode set to 100644
File size: 7.0 KB
Line 
1 MODULE RWGRIB2
2
3 CONTAINS
4
5 SUBROUTINE READLATLON(FILENAME,FELD,MAXL,MAXB,MLEVEL,MPAR)
6
7!! Read a field from GRIB file on lat-lon grid
8
9 USE GRIB_API
10
11 IMPLICIT NONE
12
13   integer                            ::  ifile
14   integer                            ::  iret
15   integer                            ::  n,mk,parid,nm
16   integer                            ::  i,k
17   integer,dimension(:),allocatable   ::  igrib
18   integer                            ::  numberOfPointsAlongAParallel
19   integer                            ::  numberOfPointsAlongAMeridian
20   real, dimension(:), allocatable    ::  values
21   integer                            ::  numberOfValues
22   real,dimension(maxl,maxb,mlevel)   ::  feld 
23   integer:: maxl,maxb,mlevel,mstride,mpar(:),irest,div,level
24   integer :: l(size(mpar))
25   character*(*):: filename   
26   
27   feld=0.                         
28
29   call grib_open_file(ifile, TRIM(FILENAME),'r')
30 
31   ! count the messages in the file
32   call grib_count_in_file(ifile,n)
33   allocate(igrib(n))
34   igrib=-1
35 
36   ! Load the messages from the file.
37   DO i=1,n
38      call grib_new_from_file(ifile,igrib(i), iret)
39   END DO
40 
41   ! we can close the file
42   call grib_close_file(ifile)
43 
44      nm=size(mpar)
45      div=mlevel/nm
46      l=0
47     
48   ! Loop on all the messages in memory
49  iloop:  DO i=1,n
50!      write(*,*) 'processing message number ',i
51      !     get as a integer
52      call grib_get(igrib(i),'numberOfPointsAlongAParallel', &
53           numberOfPointsAlongAParallel)
54 
55      !     get as a integer
56      call grib_get(igrib(i),'numberOfPointsAlongAMeridian', &
57           numberOfPointsAlongAMeridian)
58
59      call grib_get(igrib(i),'numberOfVerticalCoordinateValues',mk)
60
61      call grib_get_size(igrib(i),'values',numberOfValues)
62!      write(*,*) 'numberOfValues=',numberOfValues
63 
64      allocate(values(numberOfValues), stat=iret)
65      !     get data values
66      call grib_get(igrib(i),'values',values)
67
68      call grib_get(igrib(i),'paramId',parid)
69      call grib_get(igrib(i),'level',level)
70
71 kloop:  do k=1,nm
72        if (parid .eq. mpar(k)) then
73!         l(k)=l(k)+1
74         feld(:,:,(k-1)*div+level)=reshape(values,(/maxl,maxb/))
75!         print*,(k-1)*div+l(k),parid
76         exit kloop
77        end if
78      end do kloop
79      if (k .gt. nm .and. parid .ne. mpar(nm)) then
80        write(*,*) k,nm,parid,mpar(nm)
81        write(*,*) 'ERROR readlatlon: parameter ',parid,'is not',mpar
82        stop
83      end if
84
85!      print*,i
86   END DO iloop
87!!   write(*,*) 'readlatlon: ',i-1,' records read'
88 
89   DO i=1,n
90     call grib_release(igrib(i))
91   END DO
92 
93   if (allocated(values)) deallocate(values)
94   deallocate(igrib)
95
96   END SUBROUTINE READLATLON
97
98   SUBROUTINE WRITELATLON(iunit,igrib,ogrib,FELD,MAXL,MAXB,MLEVEL,&
99   MLEVELIST,MSTRIDE,MPAR)
100
101!! write a field on lat-lon grid to GRIB file
102
103   USE GRIB_API
104
105   IMPLICIT NONE
106
107   INTEGER IFIELD,MLEVEL,MNAUF,I,J,K,L,MSTRIDE,IERR,JOUT
108   INTEGER MPAR(MSTRIDE),MAXL,MAXB,LEVMIN,LEVMAX
109   INTEGER IUNIT,igrib,ogrib
110   REAL ZSEC4(MAXL*MAXB)
111   REAL    FELD(MAXL,MAXB,MLEVEL)
112   CHARACTER*(*) MLEVELIST
113   INTEGER ILEVEL(MLEVEL),MLINDEX(MLEVEL+1),LLEN
114
115 ! parse MLEVELIST
116
117   LLEN=len(trim(MLEVELIST))
118   if (index(MLEVELIST,'to') .ne. 0 .or. index(MLEVELIST,'TO') .ne. 0) THEN
119     i=index(MLEVELIST,'/')
120     read(MLEVELIST(1:i-1),*) LEVMIN
121     i=index(MLEVELIST,'/',.true.)
122     read(MLEVELIST(i+1:LLEN),*) LEVMAX
123     l=0
124     do i=LEVMIN,LEVMAX
125       l=l+1
126       ILEVEL(l)=i
127     end do
128   else
129     l=1
130     MLINDEX(1)=0
131     do i=1,LLEN
132       if (MLEVELIST(i:i) .eq. '/') THEN
133                l=l+1
134                MLINDEX(l)=i
135              end if
136            end do
137            MLINDEX(l+1)=LLEN+1
138            do i=1,l
139              read(MLEVELIST(MLINDEX(i)+1:MLINDEX(i+1)-1),*) ILEVEL(i)
140            end do
141   end if
142
143   DO k=1,l
144     call grib_set(igrib,"level",ILEVEL(k))
145     DO j=1,MSTRIDE
146       call grib_set(igrib,"paramId",MPAR(j))
147!         if (MPAR(j) .eq. 87) then
148!           call grib_set(igrib,"shortName","etadot")
149!           call grib_set(igrib,"units","Pa,s**-1")
150!         end if
151!         if (MPAR(j) .eq. 77) then
152!           call grib_set(igrib,"shortName","etadot")
153!           call grib_set(igrib,"units","s**-1")
154!         end if
155              if (l .ne. mlevel) then
156         zsec4(1:maxl*maxb)=RESHAPE(FELD(:,:,ILEVEL(k)),(/maxl*maxb/))
157              else
158                zsec4(1:maxl*maxb)=RESHAPE(FELD(:,:,k),(/maxl*maxb/))
159              end if
160       call grib_set(igrib,"values",zsec4)
161
162       call grib_write(igrib,iunit)
163
164     END DO
165   END DO
166
167   END SUBROUTINE WRITELATLON
168
169   SUBROUTINE READSPECTRAL(FILENAME,CXMN,MNAUF,MLEVEL,MAXLEV,MPAR,A,B)
170
171!!  read a GRIB file in spherical harmonics
172
173   USE GRIB_API
174
175   IMPLICIT NONE
176
177   integer                            ::  ifile
178   integer                            ::  iret
179   integer                            ::  n,mk,div,nm,k
180   integer                            ::  i,j,parid
181   integer,dimension(:),allocatable   ::  igrib
182   real, dimension(:), allocatable    ::  values
183   integer                            ::  numberOfValues,maxlev
184   REAL :: A(MAXLEV+1),B(MAXLEV+1),pv(2*MAXLEV+2)
185   REAL:: CXMN(0:(MNAUF+1)*(MNAUF+2)-1,MLEVEL)
186  integer:: maxl,maxb,mlevel,mstride,mpar(:),mnauf,ioffset,ipar,ilev,l(size(mpar))
187   character*(*):: filename                             
188 
189   call grib_open_file(ifile, TRIM(FILENAME),'r')
190 
191   ! count the messages in the file
192   call grib_count_in_file(ifile,n)
193   allocate(igrib(n))
194   igrib=-1
195 
196   ! Load the messages from the file.
197   DO i=1,n
198      call grib_new_from_file(ifile,igrib(i), iret)
199   END DO
200 
201   ! we can close the file
202   call grib_close_file(ifile)
203 
204    l=0
205   ! Loop on all the messages in memory
206   iloop: DO i=1,n
207   ! write(*,*) 'processing message number ',i
208      !     get as a integer
209      call grib_get(igrib(i),'pentagonalResolutionParameterJ', j)
210
211      call grib_get_size(igrib(i),'values',numberOfValues)
212   !   write(*,*) 'numberOfValues=',numberOfValues
213 
214      call grib_get(igrib(i),'numberOfVerticalCoordinateValues',mk)
215
216      call grib_get(igrib(i),'level',ilev)
217
218     
219
220      call grib_get(igrib(i),'pv',pv)
221
222      allocate(values(numberOfValues), stat=iret)
223      !     get data values
224      call grib_get(igrib(i),'values',values)
225
226!      IOFFSET=mod(i-1,MSTRIDE)*(mk/2-1)
227!           CXMN(:,IOFFSET+ilev)=values(1:(MNAUF+1)*(MNAUF+2))
228
229      call grib_get(igrib(i),'paramId',parid)
230      nm=size(mpar)
231      div=mlevel/nm
232      kloop:  do k=1,nm
233        if (parid .eq. mpar(k)) then
234         l(k)=l(k)+1
235         cxmn(:,(k-1)*div+l(k))=values(1:(MNAUF+1)*(MNAUF+2))
236!         print*,(k-1)*div+l(k),parid
237         exit kloop
238        end if
239       
240      end do kloop
241      if (k .gt. nm .and. parid .ne. mpar(nm)) then
242        write(*,*) k,nm,parid,mpar(nm)
243        write(*,*) 'ERROR readspectral: parameter ',parid,'is not',mpar
244        stop
245      end if
246
247!      print*,i
248
249   END DO iloop
250
251!!   write(*,*) 'readspectral: ',i-1,' records read'
252 
253   DO i=1,n
254     call grib_release(igrib(i))
255   END DO
256 
257   deallocate(values)
258   deallocate(igrib)
259
260   A=pv(1:1+MAXLEV)
261   B=pv(2+MAXLEV:2*MAXLEV+2)
262
263   END SUBROUTINE READSPECTRAL
264
265END MODULE RWGRIB2
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG