/[lmdze]/trunk/IOIPSL/histwrite_real.f
ViewVC logotype

Diff of /trunk/IOIPSL/histwrite_real.f

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/libf/IOIPSL/histwrite_real.f90 revision 62 by guez, Thu Jul 26 14:37:37 2012 UTC trunk/Sources/IOIPSL/histwrite_real.f revision 178 by guez, Fri Mar 11 18:47:26 2016 UTC
# Line 2  module histwrite_real_m Line 2  module histwrite_real_m
2    
3    implicit none    implicit none
4    
5      REAL, ALLOCATABLE, SAVE:: buffer(:)
6    
7  contains  contains
8    
9    SUBROUTINE histwrite_real(fileid, varid, itau, nbdpt, buff_tmp, nbindex, &    SUBROUTINE histwrite_real(datasz_max, fileid, varid, itau, nbdpt, buff_tmp, &
10         nindex, do_oper, do_write)         nbindex, nindex, do_oper, do_write)
11    
12      ! This subroutine is internal and does the calculations and writing      ! This subroutine is internal and does the calculations and writing
13      ! if needed. At a later stage it should be split into an operation      ! if needed. At a later stage it should be split into an operation
14      ! and writing subroutines.      ! and writing subroutines.
15    
16        use histbeg_totreg_m, only: deltat, regular
17        USE histcom_var, ONLY: last_opp, last_wrt, missing_val, nbopp, nb_opp, &
18             nb_wrt, ncdf_ids, scal, scsize, sopps, tax_last, tdimid, topp, &
19             var_axid, zorig, zsize
20        use histdef_m, only: buff_pos, point
21        use histend_m, only: ncvar_ids
22      USE mathop_m, ONLY: mathop      USE mathop_m, ONLY: mathop
23      USE mathelp, ONLY: trans_buff, moycum      use moycum_m, only: moycum
24      use netcdf, only: NF90_PUT_VAR      use netcdf, only: NF90_PUT_VAR
25      USE histcom_var, ONLY: buffer, buff_pos, datasz_max, deltat, &      USE trans_buff_m, ONLY: trans_buff
          last_opp, last_wrt, missing_val, nbopp, nb_opp, nb_wrt, ncdf_ids, &  
          ncvar_ids, point, regular, scal, scsize, sopps, tax_last, tdimid, &  
          topp, var_axid, zorig, zsize  
26    
27        INTEGER, INTENT(IN):: datasz_max(:, :) ! (nb_files_max, nb_var_max)
28      INTEGER, INTENT(IN):: fileid, varid, itau, nbdpt      INTEGER, INTENT(IN):: fileid, varid, itau, nbdpt
29      REAL buff_tmp(:)      REAL buff_tmp(:)
30    
# Line 52  contains Line 58  contains
58    
59      ! The sizes which can be encoutered      ! The sizes which can be encoutered
60    
61      tsz = zsize(fileid, varid, 1)*zsize(fileid, varid, 2)*zsize(fileid, varid, 3)      tsz = zsize(fileid, varid, 1) * zsize(fileid, varid, 2) &
62             * zsize(fileid, varid, 3)
63    
64      ! 1.0 We allocate the memory needed to store the data between write      ! 1.0 We allocate the memory needed to store the data between write
65      !     and the temporary space needed for operations.      ! and the temporary space needed for operations.
66      !     We have to keep precedent buffer if needed      ! We have to keep precedent buffer if needed
67    
68      IF (.NOT. ALLOCATED(buffer)) THEN      IF (.NOT. ALLOCATED(buffer)) THEN
69         ALLOCATE(buffer(buff_pos))         ALLOCATE(buffer(buff_pos))
# Line 64  contains Line 71  contains
71         buffer(:)=0.0         buffer(:)=0.0
72      ELSE IF (buffer_sz < buff_pos) THEN      ELSE IF (buffer_sz < buff_pos) THEN
73         IF (SUM(buffer)/=0.0) THEN         IF (SUM(buffer)/=0.0) THEN
74            ALLOCATE (buffer_used(buffer_sz))            ALLOCATE(buffer_used(buffer_sz))
75            buffer_used(:)=buffer(:)            buffer_used(:)=buffer(:)
76            DEALLOCATE (buffer)            DEALLOCATE(buffer)
77            ALLOCATE (buffer(buff_pos))            ALLOCATE(buffer(buff_pos))
78            buffer_sz = buff_pos            buffer_sz = buff_pos
79            buffer(:SIZE(buffer_used))=buffer_used            buffer(:SIZE(buffer_used))=buffer_used
80            DEALLOCATE (buffer_used)            DEALLOCATE(buffer_used)
81         ELSE         ELSE
82            DEALLOCATE (buffer)            DEALLOCATE(buffer)
83            ALLOCATE (buffer(buff_pos))            ALLOCATE(buffer(buff_pos))
84            buffer_sz = buff_pos            buffer_sz = buff_pos
85            buffer(:)=0.0            buffer(:)=0.0
86         ENDIF         ENDIF
# Line 83  contains Line 90  contains
90      ! reduces the umber of allocates but increases memory needs.      ! reduces the umber of allocates but increases memory needs.
91    
92      IF (.NOT.ALLOCATED(buff_tmp2)) THEN      IF (.NOT.ALLOCATED(buff_tmp2)) THEN
93         ALLOCATE (buff_tmp2(datasz_max(fileid, varid)))         ALLOCATE(buff_tmp2(datasz_max(fileid, varid)))
94         buff_tmp2_sz = datasz_max(fileid, varid)         buff_tmp2_sz = datasz_max(fileid, varid)
95      ELSE IF ( datasz_max(fileid, varid) > buff_tmp2_sz) THEN      ELSE IF (datasz_max(fileid, varid) > buff_tmp2_sz) THEN
96         DEALLOCATE (buff_tmp2)         DEALLOCATE(buff_tmp2)
97         ALLOCATE (buff_tmp2(datasz_max(fileid, varid)))         ALLOCATE(buff_tmp2(datasz_max(fileid, varid)))
98         buff_tmp2_sz = datasz_max(fileid, varid)         buff_tmp2_sz = datasz_max(fileid, varid)
99      ENDIF      ENDIF
100    
# Line 98  contains Line 105  contains
105    
106      ! 3.1 DO the Operations only if needed      ! 3.1 DO the Operations only if needed
107    
108      IF ( do_oper ) THEN      IF (do_oper) THEN
109         i = fileid         i = fileid
110         nbout = nbdpt         nbout = nbdpt
111    
112         !- 3.4 We continue the sequence of operations         ! 3.4 We continue the sequence of operations
113         !-     we started in the interface routine         ! we started in the interface routine
114    
115         DO io = 2, nbopp(i, varid), 2         DO io = 2, nbopp(i, varid), 2
116            nbin = nbout            nbin = nbout
# Line 117  contains Line 124  contains
124                 nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp)                 nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp)
125         ENDDO         ENDDO
126    
127         !   3.5 Zoom into the data         ! 3.5 Zoom into the data
128    
129         CALL trans_buff &         CALL trans_buff(zorig(i, varid, 1), zsize(i, varid, 1), &
130              (zorig(i, varid, 1), zsize(i, varid, 1), &              zorig(i, varid, 2), zsize(i, varid, 2), zorig(i, varid, 3), &
131              zorig(i, varid, 2), zsize(i, varid, 2), &              zsize(i, varid, 3), scsize(i, varid, 1), scsize(i, varid, 2), &
132              zorig(i, varid, 3), zsize(i, varid, 3), &              scsize(i, varid, 3), buff_tmp, buff_tmp2_sz, buff_tmp2)
             scsize(i, varid, 1), scsize(i, varid, 2), scsize(i, varid, 3), &  
             buff_tmp, buff_tmp2_sz, buff_tmp2)  
133    
134         !- 5.0 Do the operations if needed. In the case of instantaneous         ! 5.0 Do the operations if needed. In the case of instantaneous
135         !-     output we do not transfer to the buffer.         ! output we do not transfer to the buffer.
136    
137         ipt = point(fileid, varid)         ipt = point(fileid, varid)
138    
139         IF (     (TRIM(tmp_opp) /= "inst") &         IF ((TRIM(tmp_opp) /= "inst") &
140              .AND.(TRIM(tmp_opp) /= "once") ) THEN              .AND.(TRIM(tmp_opp) /= "once")) THEN
141            CALL moycum(tmp_opp, tsz, buffer(ipt:), &            CALL moycum(tmp_opp, tsz, buffer(ipt:), &
142                 buff_tmp2, nb_opp(fileid, varid))                 buff_tmp2, nb_opp(fileid, varid))
143         ENDIF         ENDIF
# Line 144  contains Line 149  contains
149    
150      ! 6.0 Write to file if needed      ! 6.0 Write to file if needed
151    
152      IF ( do_write ) THEN      IF (do_write) THEN
   
153         ncvarid = ncvar_ids(fileid, varid)         ncvarid = ncvar_ids(fileid, varid)
154         ncid = ncdf_ids(fileid)         ncid = ncdf_ids(fileid)
155    
156         !- 6.1 Do the operations that are needed before writting         ! 6.1 Do the operations that are needed before writting
157           IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN
158         IF (     (TRIM(tmp_opp) /= "inst") &            rtime = (rtime + last_wrt(fileid, varid)*deltat(fileid)) / 2.
             .AND.(TRIM(tmp_opp) /= "once") ) THEN  
           rtime = (rtime+last_wrt(fileid, varid)*deltat(fileid))/2.0  
159         ENDIF         ENDIF
160    
161         !- 6.2 Add a value to the time axis of this variable if needed         ! 6.2 Add a value to the time axis of this variable if needed
162           IF (TRIM(tmp_opp) /= "l_max" .AND. TRIM(tmp_opp) /= "l_min" &
163         IF (     (TRIM(tmp_opp) /= "l_max") &              .AND. TRIM(tmp_opp) /= "once") THEN
             .AND.(TRIM(tmp_opp) /= "l_min") &  
             .AND.(TRIM(tmp_opp) /= "once") ) THEN  
   
164            itax = var_axid(fileid, varid)            itax = var_axid(fileid, varid)
165            itime = nb_wrt(fileid, varid)+1            itime = nb_wrt(fileid, varid) + 1
166    
167            IF (tax_last(fileid, itax) < itime) THEN            IF (tax_last(fileid, itax) < itime) THEN
168               iret = NF90_PUT_VAR (ncid, tdimid(fileid, itax), (/ rtime /), &               iret = NF90_PUT_VAR(ncid, tdimid(fileid, itax), (/rtime/), &
169                    start=(/ itime /), count=(/ 1 /))                    start=(/itime/))
170               tax_last(fileid, itax) = itime               tax_last(fileid, itax) = itime
171            ENDIF            ENDIF
172         ELSE         ELSE
173            itime=1            itime=1
174         ENDIF         ENDIF
175    
176         !- 6.3 Write the data. Only in the case of instantaneous output         ! 6.3 Write the data. Only in the case of instantaneous output
177         !       we do not write the buffer.         ! we do not write the buffer.
178    
179         IF (scsize(fileid, varid, 3) == 1) THEN         IF (scsize(fileid, varid, 3) == 1) THEN
180            IF (regular(fileid)) THEN            IF (regular(fileid)) THEN
181               corner(1:4) = (/ 1, 1, itime, 0 /)               corner(1:4) = (/1, 1, itime, 0/)
182               edges(1:4) = (/ zsize(fileid, varid, 1), &               edges(1:4) = (/zsize(fileid, varid, 1), &
183                    zsize(fileid, varid, 2), &                    zsize(fileid, varid, 2), &
184                    1, 0 /)                    1, 0/)
185            ELSE            ELSE
186               corner(1:4) = (/ 1, itime, 0, 0 /)               corner(1:4) = (/1, itime, 0, 0/)
187               edges(1:4) = (/ zsize(fileid, varid, 1), 1, 0, 0 /)               edges(1:4) = (/zsize(fileid, varid, 1), 1, 0, 0/)
188            ENDIF            ENDIF
189         ELSE         ELSE
190            IF ( regular(fileid) ) THEN            IF (regular(fileid)) THEN
191               corner(1:4) = (/ 1, 1, 1, itime /)               corner(1:4) = (/1, 1, 1, itime/)
192               edges(1:4) = (/ zsize(fileid, varid, 1), &               edges(1:4) = (/zsize(fileid, varid, 1), &
193                    zsize(fileid, varid, 2), &                    zsize(fileid, varid, 2), &
194                    zsize(fileid, varid, 3), 1 /)                    zsize(fileid, varid, 3), 1/)
195            ELSE            ELSE
196               corner(1:4) = (/ 1, 1, itime, 0 /)               corner(1:4) = (/1, 1, itime, 0/)
197               edges(1:4) = (/ zsize(fileid, varid, 1), &               edges(1:4) = (/zsize(fileid, varid, 1), &
198                    zsize(fileid, varid, 3), 1, 0 /)                    zsize(fileid, varid, 3), 1, 0/)
199            ENDIF            ENDIF
200         ENDIF         ENDIF
201    
202         ipt = point(fileid, varid)         ipt = point(fileid, varid)
203    
204         IF (     (TRIM(tmp_opp) /= "inst") &         IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN
205              .AND.(TRIM(tmp_opp) /= "once") ) THEN            iret = NF90_PUT_VAR(ncid, ncvarid, buffer(ipt:), &
           iret = NF90_PUT_VAR (ncid, ncvarid, buffer(ipt:), &  
206                 start=corner(1:4), count=edges(1:4))                 start=corner(1:4), count=edges(1:4))
207         ELSE         ELSE
208            iret = NF90_PUT_VAR (ncid, ncvarid, buff_tmp2, &            iret = NF90_PUT_VAR(ncid, ncvarid, buff_tmp2, &
209                 start=corner(1:4), count=edges(1:4))                 start=corner(1:4), count=edges(1:4))
210         ENDIF         ENDIF
211    
212         last_wrt(fileid, varid) = itau         last_wrt(fileid, varid) = itau
213         nb_wrt(fileid, varid) = nb_wrt(fileid, varid)+1         nb_wrt(fileid, varid) = nb_wrt(fileid, varid)+1
214         nb_opp(fileid, varid) = 0         nb_opp(fileid, varid) = 0
        !--  
        !   After the write the file can be synchronized so that no data is  
        !   lost in case of a crash. This feature gives up on the benefits of  
        !   buffering and should only be used in debuging mode. A flag is  
        !   needed here to switch to this mode.  
        !--  
        !   iret = NF90_SYNC (ncid)  
   
215      ENDIF      ENDIF
216    
217    END SUBROUTINE histwrite_real    END SUBROUTINE histwrite_real

Legend:
Removed from v.62  
changed lines
  Added in v.178

  ViewVC Help
Powered by ViewVC 1.1.21