4 |
|
|
5 |
contains |
contains |
6 |
|
|
7 |
SUBROUTINE histwrite_real(pfileid, varid, pitau, nbdpt, buff_tmp, nbindex, & |
SUBROUTINE histwrite_real(fileid, varid, itau, nbdpt, buff_tmp, nbindex, & |
8 |
nindex, do_oper, do_write) |
nindex, do_oper, do_write) |
9 |
|
|
10 |
! This subroutine is internal and does the calculations and writing |
! This subroutine is internal and does the calculations and writing |
11 |
! 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 |
12 |
! and writing subroutines. |
! and writing subroutines. |
|
!-------------------------------------------------------------------- |
|
13 |
|
|
14 |
USE mathop_m, ONLY : mathop |
USE histcom_var, ONLY: buffer, buff_pos, datasz_max, deltat, & |
15 |
USE mathelp, ONLY : trans_buff, moycum |
last_opp, last_wrt, missing_val, nbopp, nb_opp, nb_wrt, ncdf_ids, & |
16 |
|
ncvar_ids, point, regular, scal, scsize, sopps, tax_last, tdimid, & |
17 |
|
topp, var_axid, zorig, zsize |
18 |
|
USE mathelp, ONLY: trans_buff, moycum |
19 |
|
USE mathop_m, ONLY: mathop |
20 |
use netcdf, only: NF90_PUT_VAR |
use netcdf, only: NF90_PUT_VAR |
|
use histcom_var |
|
21 |
|
|
22 |
INTEGER, INTENT(IN) :: pfileid, pitau, varid, & |
INTEGER, INTENT(IN):: fileid, varid, itau, nbdpt |
23 |
& nbindex, nindex(nbindex), nbdpt |
REAL buff_tmp(:) |
24 |
REAL, DIMENSION(:) :: buff_tmp |
|
25 |
LOGICAL, INTENT(IN) :: do_oper, do_write |
INTEGER, INTENT(IN):: nbindex |
26 |
|
! number of indices provided |
27 |
INTEGER :: tsz, ncid, ncvarid |
! If it is equal to the size of the full field as provided in histdef |
28 |
INTEGER :: i, iret, ipt, itax |
! then nothing is done. |
29 |
INTEGER :: io, nbin, nbout |
|
30 |
INTEGER, DIMENSION(4) :: corner, edges |
INTEGER, INTENT(IN):: nindex(nbindex) |
31 |
INTEGER :: itime |
! The indices used to expand the variable (pdata) onto the full field |
32 |
|
|
33 |
REAL :: rtime |
LOGICAL, INTENT(IN):: do_oper, do_write |
34 |
CHARACTER(LEN=7) :: tmp_opp |
|
35 |
|
! Local: |
36 |
REAL, ALLOCATABLE, SAVE :: buff_tmp2(:) |
|
37 |
INTEGER, SAVE :: buff_tmp2_sz |
INTEGER:: tsz, ncid, ncvarid |
38 |
REAL, ALLOCATABLE, SAVE :: buffer_used(:) |
INTEGER:: i, iret, ipt, itax |
39 |
INTEGER, SAVE :: buffer_sz |
INTEGER:: io, nbin, nbout |
40 |
|
INTEGER, DIMENSION(4):: corner, edges |
41 |
|
INTEGER:: itime |
42 |
|
|
43 |
|
REAL:: rtime |
44 |
|
CHARACTER(LEN=7):: tmp_opp |
45 |
|
|
46 |
|
REAL, ALLOCATABLE, SAVE:: buff_tmp2(:) |
47 |
|
INTEGER, SAVE:: buff_tmp2_sz |
48 |
|
REAL, ALLOCATABLE, SAVE:: buffer_used(:) |
49 |
|
INTEGER, SAVE:: buffer_sz |
50 |
|
|
51 |
!-------------------------------------------------------------------- |
!-------------------------------------------------------------------- |
52 |
|
|
53 |
! The sizes which can be encoutered |
! The sizes which can be encoutered |
54 |
|
|
55 |
tsz = zsize(pfileid, varid, 1)*zsize(pfileid, varid, 2)*zsize(pfileid, varid, 3) |
tsz = zsize(fileid, varid, 1) * zsize(fileid, varid, 2) & |
56 |
|
* zsize(fileid, varid, 3) |
57 |
|
|
58 |
! 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 |
59 |
! and the temporary space needed for operations. |
! and the temporary space needed for operations. |
60 |
! We have to keep precedent buffer if needed |
! We have to keep precedent buffer if needed |
61 |
|
|
62 |
IF (.NOT. ALLOCATED(buffer)) THEN |
IF (.NOT. ALLOCATED(buffer)) THEN |
63 |
ALLOCATE(buffer(buff_pos)) |
ALLOCATE(buffer(buff_pos)) |
65 |
buffer(:)=0.0 |
buffer(:)=0.0 |
66 |
ELSE IF (buffer_sz < buff_pos) THEN |
ELSE IF (buffer_sz < buff_pos) THEN |
67 |
IF (SUM(buffer)/=0.0) THEN |
IF (SUM(buffer)/=0.0) THEN |
68 |
ALLOCATE (buffer_used(buffer_sz)) |
ALLOCATE(buffer_used(buffer_sz)) |
69 |
buffer_used(:)=buffer(:) |
buffer_used(:)=buffer(:) |
70 |
DEALLOCATE (buffer) |
DEALLOCATE(buffer) |
71 |
ALLOCATE (buffer(buff_pos)) |
ALLOCATE(buffer(buff_pos)) |
72 |
buffer_sz = buff_pos |
buffer_sz = buff_pos |
73 |
buffer(:SIZE(buffer_used))=buffer_used |
buffer(:SIZE(buffer_used))=buffer_used |
74 |
DEALLOCATE (buffer_used) |
DEALLOCATE(buffer_used) |
75 |
ELSE |
ELSE |
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(:)=0.0 |
buffer(:)=0.0 |
80 |
ENDIF |
ENDIF |
84 |
! reduces the umber of allocates but increases memory needs. |
! reduces the umber of allocates but increases memory needs. |
85 |
|
|
86 |
IF (.NOT.ALLOCATED(buff_tmp2)) THEN |
IF (.NOT.ALLOCATED(buff_tmp2)) THEN |
87 |
ALLOCATE (buff_tmp2(datasz_max(pfileid, varid))) |
ALLOCATE(buff_tmp2(datasz_max(fileid, varid))) |
88 |
buff_tmp2_sz = datasz_max(pfileid, varid) |
buff_tmp2_sz = datasz_max(fileid, varid) |
89 |
ELSE IF ( datasz_max(pfileid, varid) > buff_tmp2_sz) THEN |
ELSE IF (datasz_max(fileid, varid) > buff_tmp2_sz) THEN |
90 |
DEALLOCATE (buff_tmp2) |
DEALLOCATE(buff_tmp2) |
91 |
ALLOCATE (buff_tmp2(datasz_max(pfileid, varid))) |
ALLOCATE(buff_tmp2(datasz_max(fileid, varid))) |
92 |
buff_tmp2_sz = datasz_max(pfileid, varid) |
buff_tmp2_sz = datasz_max(fileid, varid) |
93 |
ENDIF |
ENDIF |
94 |
|
|
95 |
rtime = pitau * deltat(pfileid) |
rtime = itau * deltat(fileid) |
96 |
tmp_opp = topp(pfileid, varid) |
tmp_opp = topp(fileid, varid) |
97 |
|
|
98 |
! 3.0 Do the operations or transfer the slab of data into buff_tmp |
! 3.0 Do the operations or transfer the slab of data into buff_tmp |
99 |
|
|
100 |
! 3.1 DO the Operations only if needed |
! 3.1 DO the Operations only if needed |
101 |
|
|
102 |
IF ( do_oper ) THEN |
IF (do_oper) THEN |
103 |
i = pfileid |
i = fileid |
104 |
nbout = nbdpt |
nbout = nbdpt |
105 |
|
|
106 |
!- 3.4 We continue the sequence of operations |
! 3.4 We continue the sequence of operations |
107 |
!- we started in the interface routine |
! we started in the interface routine |
108 |
|
|
109 |
DO io = 2, nbopp(i, varid), 2 |
DO io = 2, nbopp(i, varid), 2 |
110 |
nbin = nbout |
nbin = nbout |
111 |
nbout = datasz_max(i, varid) |
nbout = datasz_max(i, varid) |
112 |
CALL mathop(sopps(i, varid, io), nbin, buff_tmp, missing_val, & |
CALL mathop(sopps(i, varid, io), nbin, buff_tmp, missing_val, & |
113 |
& nbindex, nindex, scal(i, varid, io), nbout, buff_tmp2) |
nbindex, nindex, scal(i, varid, io), nbout, buff_tmp2) |
114 |
|
|
115 |
nbin = nbout |
nbin = nbout |
116 |
nbout = datasz_max(i, varid) |
nbout = datasz_max(i, varid) |
117 |
CALL mathop(sopps(i, varid, io+1), nbin, buff_tmp2, missing_val, & |
CALL mathop(sopps(i, varid, io+1), nbin, buff_tmp2, missing_val, & |
118 |
& nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp) |
nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp) |
119 |
ENDDO |
ENDDO |
120 |
|
|
121 |
! 3.5 Zoom into the data |
! 3.5 Zoom into the data |
122 |
|
|
123 |
CALL trans_buff & |
CALL trans_buff(zorig(i, varid, 1), zsize(i, varid, 1), & |
124 |
& (zorig(i, varid, 1), zsize(i, varid, 1), & |
zorig(i, varid, 2), zsize(i, varid, 2), zorig(i, varid, 3), & |
125 |
& zorig(i, varid, 2), zsize(i, varid, 2), & |
zsize(i, varid, 3), scsize(i, varid, 1), scsize(i, varid, 2), & |
126 |
& 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) |
|
127 |
|
|
128 |
!- 5.0 Do the operations if needed. In the case of instantaneous |
! 5.0 Do the operations if needed. In the case of instantaneous |
129 |
!- output we do not transfer to the buffer. |
! output we do not transfer to the buffer. |
130 |
|
|
131 |
ipt = point(pfileid, varid) |
ipt = point(fileid, varid) |
132 |
|
|
133 |
IF ( (TRIM(tmp_opp) /= "inst") & |
IF ((TRIM(tmp_opp) /= "inst") & |
134 |
& .AND.(TRIM(tmp_opp) /= "once") ) THEN |
.AND.(TRIM(tmp_opp) /= "once")) THEN |
135 |
CALL moycum(tmp_opp, tsz, buffer(ipt:), & |
CALL moycum(tmp_opp, tsz, buffer(ipt:), & |
136 |
& buff_tmp2, nb_opp(pfileid, varid)) |
buff_tmp2, nb_opp(fileid, varid)) |
137 |
ENDIF |
ENDIF |
138 |
|
|
139 |
last_opp(pfileid, varid) = pitau |
last_opp(fileid, varid) = itau |
140 |
nb_opp(pfileid, varid) = nb_opp(pfileid, varid)+1 |
nb_opp(fileid, varid) = nb_opp(fileid, varid)+1 |
141 |
|
|
142 |
ENDIF |
ENDIF |
143 |
|
|
144 |
! 6.0 Write to file if needed |
! 6.0 Write to file if needed |
145 |
|
|
146 |
IF ( do_write ) THEN |
IF (do_write) THEN |
147 |
|
ncvarid = ncvar_ids(fileid, varid) |
148 |
ncvarid = ncvar_ids(pfileid, varid) |
ncid = ncdf_ids(fileid) |
149 |
ncid = ncdf_ids(pfileid) |
|
150 |
|
! 6.1 Do the operations that are needed before writting |
151 |
!- 6.1 Do the operations that are needed before writting |
IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN |
152 |
|
rtime = (rtime + last_wrt(fileid, varid)*deltat(fileid)) / 2. |
153 |
IF ( (TRIM(tmp_opp) /= "inst") & |
ENDIF |
154 |
& .AND.(TRIM(tmp_opp) /= "once") ) THEN |
|
155 |
rtime = (rtime+last_wrt(pfileid, varid)*deltat(pfileid))/2.0 |
! 6.2 Add a value to the time axis of this variable if needed |
156 |
ENDIF |
IF (TRIM(tmp_opp) /= "l_max" .AND. TRIM(tmp_opp) /= "l_min" & |
157 |
|
.AND. TRIM(tmp_opp) /= "once") THEN |
158 |
!- 6.2 Add a value to the time axis of this variable if needed |
itax = var_axid(fileid, varid) |
159 |
|
itime = nb_wrt(fileid, varid) + 1 |
160 |
IF ( (TRIM(tmp_opp) /= "l_max") & |
|
161 |
& .AND.(TRIM(tmp_opp) /= "l_min") & |
IF (tax_last(fileid, itax) < itime) THEN |
162 |
& .AND.(TRIM(tmp_opp) /= "once") ) THEN |
iret = NF90_PUT_VAR(ncid, tdimid(fileid, itax), (/rtime/), & |
163 |
|
start=(/itime/)) |
164 |
itax = var_axid(pfileid, varid) |
tax_last(fileid, itax) = itime |
|
itime = nb_wrt(pfileid, varid)+1 |
|
|
|
|
|
IF (tax_last(pfileid, itax) < itime) THEN |
|
|
iret = NF90_PUT_VAR (ncid, tdimid(pfileid, itax), (/ rtime /), & |
|
|
& start=(/ itime /), count=(/ 1 /)) |
|
|
tax_last(pfileid, itax) = itime |
|
165 |
ENDIF |
ENDIF |
166 |
ELSE |
ELSE |
167 |
itime=1 |
itime=1 |
168 |
ENDIF |
ENDIF |
169 |
|
|
170 |
!- 6.3 Write the data. Only in the case of instantaneous output |
! 6.3 Write the data. Only in the case of instantaneous output |
171 |
! we do not write the buffer. |
! we do not write the buffer. |
172 |
|
|
173 |
IF (scsize(pfileid, varid, 3) == 1) THEN |
IF (scsize(fileid, varid, 3) == 1) THEN |
174 |
IF (regular(pfileid)) THEN |
IF (regular(fileid)) THEN |
175 |
corner(1:4) = (/ 1, 1, itime, 0 /) |
corner(1:4) = (/1, 1, itime, 0/) |
176 |
edges(1:4) = (/ zsize(pfileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
177 |
& zsize(pfileid, varid, 2), & |
zsize(fileid, varid, 2), & |
178 |
& 1, 0 /) |
1, 0/) |
179 |
ELSE |
ELSE |
180 |
corner(1:4) = (/ 1, itime, 0, 0 /) |
corner(1:4) = (/1, itime, 0, 0/) |
181 |
edges(1:4) = (/ zsize(pfileid, varid, 1), 1, 0, 0 /) |
edges(1:4) = (/zsize(fileid, varid, 1), 1, 0, 0/) |
182 |
ENDIF |
ENDIF |
183 |
ELSE |
ELSE |
184 |
IF ( regular(pfileid) ) THEN |
IF (regular(fileid)) THEN |
185 |
corner(1:4) = (/ 1, 1, 1, itime /) |
corner(1:4) = (/1, 1, 1, itime/) |
186 |
edges(1:4) = (/ zsize(pfileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
187 |
& zsize(pfileid, varid, 2), & |
zsize(fileid, varid, 2), & |
188 |
& zsize(pfileid, varid, 3), 1 /) |
zsize(fileid, varid, 3), 1/) |
189 |
ELSE |
ELSE |
190 |
corner(1:4) = (/ 1, 1, itime, 0 /) |
corner(1:4) = (/1, 1, itime, 0/) |
191 |
edges(1:4) = (/ zsize(pfileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
192 |
& zsize(pfileid, varid, 3), 1, 0 /) |
zsize(fileid, varid, 3), 1, 0/) |
193 |
ENDIF |
ENDIF |
194 |
ENDIF |
ENDIF |
195 |
|
|
196 |
ipt = point(pfileid, varid) |
ipt = point(fileid, varid) |
197 |
|
|
198 |
IF ( (TRIM(tmp_opp) /= "inst") & |
IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN |
199 |
& .AND.(TRIM(tmp_opp) /= "once") ) THEN |
iret = NF90_PUT_VAR(ncid, ncvarid, buffer(ipt:), & |
200 |
iret = NF90_PUT_VAR (ncid, ncvarid, buffer(ipt:), & |
start=corner(1:4), count=edges(1:4)) |
|
& start=corner(1:4), count=edges(1:4)) |
|
201 |
ELSE |
ELSE |
202 |
iret = NF90_PUT_VAR (ncid, ncvarid, buff_tmp2, & |
iret = NF90_PUT_VAR(ncid, ncvarid, buff_tmp2, & |
203 |
& start=corner(1:4), count=edges(1:4)) |
start=corner(1:4), count=edges(1:4)) |
204 |
ENDIF |
ENDIF |
205 |
|
|
206 |
last_wrt(pfileid, varid) = pitau |
last_wrt(fileid, varid) = itau |
207 |
nb_wrt(pfileid, varid) = nb_wrt(pfileid, varid)+1 |
nb_wrt(fileid, varid) = nb_wrt(fileid, varid)+1 |
208 |
nb_opp(pfileid, 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) |
|
|
|
|
209 |
ENDIF |
ENDIF |
210 |
!--------------------------- |
|
211 |
END SUBROUTINE histwrite_real |
END SUBROUTINE histwrite_real |
212 |
|
|
213 |
end module histwrite_real_m |
end module histwrite_real_m |