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 |
|
|
|
USE mathop_m, ONLY: mathop |
|
|
USE mathelp, ONLY: trans_buff, moycum |
|
|
use netcdf, only: NF90_PUT_VAR |
|
14 |
USE histcom_var, ONLY: buffer, buff_pos, datasz_max, deltat, & |
USE histcom_var, ONLY: buffer, buff_pos, datasz_max, deltat, & |
15 |
last_opp, last_wrt, missing_val, nbopp, nb_opp, nb_wrt, ncdf_ids, & |
last_opp, last_wrt, missing_val, nbopp, nb_opp, nb_wrt, ncdf_ids, & |
16 |
ncvar_ids, point, regular, scal, scsize, sopps, tax_last, tdimid, & |
ncvar_ids, point, regular, scal, scsize, sopps, tax_last, tdimid, & |
17 |
topp, var_axid, zorig, zsize |
topp, var_axid, zorig, zsize |
18 |
|
USE trans_buff_m, ONLY: trans_buff |
19 |
|
use moycum_m, only: moycum |
20 |
|
USE mathop_m, ONLY: mathop |
21 |
|
use netcdf, only: NF90_PUT_VAR |
22 |
|
|
23 |
INTEGER, INTENT(IN):: fileid, varid, itau, nbdpt |
INTEGER, INTENT(IN):: fileid, varid, itau, nbdpt |
24 |
REAL buff_tmp(:) |
REAL buff_tmp(:) |
53 |
|
|
54 |
! The sizes which can be encoutered |
! The sizes which can be encoutered |
55 |
|
|
56 |
tsz = zsize(fileid, varid, 1)*zsize(fileid, varid, 2)*zsize(fileid, varid, 3) |
tsz = zsize(fileid, varid, 1) * zsize(fileid, varid, 2) & |
57 |
|
* zsize(fileid, varid, 3) |
58 |
|
|
59 |
! 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 |
60 |
! and the temporary space needed for operations. |
! and the temporary space needed for operations. |
61 |
! We have to keep precedent buffer if needed |
! We have to keep precedent buffer if needed |
62 |
|
|
63 |
IF (.NOT. ALLOCATED(buffer)) THEN |
IF (.NOT. ALLOCATED(buffer)) THEN |
64 |
ALLOCATE(buffer(buff_pos)) |
ALLOCATE(buffer(buff_pos)) |
66 |
buffer(:)=0.0 |
buffer(:)=0.0 |
67 |
ELSE IF (buffer_sz < buff_pos) THEN |
ELSE IF (buffer_sz < buff_pos) THEN |
68 |
IF (SUM(buffer)/=0.0) THEN |
IF (SUM(buffer)/=0.0) THEN |
69 |
ALLOCATE (buffer_used(buffer_sz)) |
ALLOCATE(buffer_used(buffer_sz)) |
70 |
buffer_used(:)=buffer(:) |
buffer_used(:)=buffer(:) |
71 |
DEALLOCATE (buffer) |
DEALLOCATE(buffer) |
72 |
ALLOCATE (buffer(buff_pos)) |
ALLOCATE(buffer(buff_pos)) |
73 |
buffer_sz = buff_pos |
buffer_sz = buff_pos |
74 |
buffer(:SIZE(buffer_used))=buffer_used |
buffer(:SIZE(buffer_used))=buffer_used |
75 |
DEALLOCATE (buffer_used) |
DEALLOCATE(buffer_used) |
76 |
ELSE |
ELSE |
77 |
DEALLOCATE (buffer) |
DEALLOCATE(buffer) |
78 |
ALLOCATE (buffer(buff_pos)) |
ALLOCATE(buffer(buff_pos)) |
79 |
buffer_sz = buff_pos |
buffer_sz = buff_pos |
80 |
buffer(:)=0.0 |
buffer(:)=0.0 |
81 |
ENDIF |
ENDIF |
85 |
! reduces the umber of allocates but increases memory needs. |
! reduces the umber of allocates but increases memory needs. |
86 |
|
|
87 |
IF (.NOT.ALLOCATED(buff_tmp2)) THEN |
IF (.NOT.ALLOCATED(buff_tmp2)) THEN |
88 |
ALLOCATE (buff_tmp2(datasz_max(fileid, varid))) |
ALLOCATE(buff_tmp2(datasz_max(fileid, varid))) |
89 |
buff_tmp2_sz = datasz_max(fileid, varid) |
buff_tmp2_sz = datasz_max(fileid, varid) |
90 |
ELSE IF ( datasz_max(fileid, varid) > buff_tmp2_sz) THEN |
ELSE IF (datasz_max(fileid, varid) > buff_tmp2_sz) THEN |
91 |
DEALLOCATE (buff_tmp2) |
DEALLOCATE(buff_tmp2) |
92 |
ALLOCATE (buff_tmp2(datasz_max(fileid, varid))) |
ALLOCATE(buff_tmp2(datasz_max(fileid, varid))) |
93 |
buff_tmp2_sz = datasz_max(fileid, varid) |
buff_tmp2_sz = datasz_max(fileid, varid) |
94 |
ENDIF |
ENDIF |
95 |
|
|
100 |
|
|
101 |
! 3.1 DO the Operations only if needed |
! 3.1 DO the Operations only if needed |
102 |
|
|
103 |
IF ( do_oper ) THEN |
IF (do_oper) THEN |
104 |
i = fileid |
i = fileid |
105 |
nbout = nbdpt |
nbout = nbdpt |
106 |
|
|
107 |
!- 3.4 We continue the sequence of operations |
! 3.4 We continue the sequence of operations |
108 |
!- we started in the interface routine |
! we started in the interface routine |
109 |
|
|
110 |
DO io = 2, nbopp(i, varid), 2 |
DO io = 2, nbopp(i, varid), 2 |
111 |
nbin = nbout |
nbin = nbout |
119 |
nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp) |
nbindex, nindex, scal(i, varid, io+1), nbout, buff_tmp) |
120 |
ENDDO |
ENDDO |
121 |
|
|
122 |
! 3.5 Zoom into the data |
! 3.5 Zoom into the data |
123 |
|
|
124 |
CALL trans_buff & |
CALL trans_buff(zorig(i, varid, 1), zsize(i, varid, 1), & |
125 |
(zorig(i, varid, 1), zsize(i, varid, 1), & |
zorig(i, varid, 2), zsize(i, varid, 2), zorig(i, varid, 3), & |
126 |
zorig(i, varid, 2), zsize(i, varid, 2), & |
zsize(i, varid, 3), scsize(i, varid, 1), scsize(i, varid, 2), & |
127 |
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) |
|
128 |
|
|
129 |
!- 5.0 Do the operations if needed. In the case of instantaneous |
! 5.0 Do the operations if needed. In the case of instantaneous |
130 |
!- output we do not transfer to the buffer. |
! output we do not transfer to the buffer. |
131 |
|
|
132 |
ipt = point(fileid, varid) |
ipt = point(fileid, varid) |
133 |
|
|
134 |
IF ( (TRIM(tmp_opp) /= "inst") & |
IF ((TRIM(tmp_opp) /= "inst") & |
135 |
.AND.(TRIM(tmp_opp) /= "once") ) THEN |
.AND.(TRIM(tmp_opp) /= "once")) THEN |
136 |
CALL moycum(tmp_opp, tsz, buffer(ipt:), & |
CALL moycum(tmp_opp, tsz, buffer(ipt:), & |
137 |
buff_tmp2, nb_opp(fileid, varid)) |
buff_tmp2, nb_opp(fileid, varid)) |
138 |
ENDIF |
ENDIF |
144 |
|
|
145 |
! 6.0 Write to file if needed |
! 6.0 Write to file if needed |
146 |
|
|
147 |
IF ( do_write ) THEN |
IF (do_write) THEN |
|
|
|
148 |
ncvarid = ncvar_ids(fileid, varid) |
ncvarid = ncvar_ids(fileid, varid) |
149 |
ncid = ncdf_ids(fileid) |
ncid = ncdf_ids(fileid) |
150 |
|
|
151 |
!- 6.1 Do the operations that are needed before writting |
! 6.1 Do the operations that are needed before writting |
152 |
|
IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN |
153 |
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 |
|
154 |
ENDIF |
ENDIF |
155 |
|
|
156 |
!- 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 |
157 |
|
IF (TRIM(tmp_opp) /= "l_max" .AND. TRIM(tmp_opp) /= "l_min" & |
158 |
IF ( (TRIM(tmp_opp) /= "l_max") & |
.AND. TRIM(tmp_opp) /= "once") THEN |
|
.AND.(TRIM(tmp_opp) /= "l_min") & |
|
|
.AND.(TRIM(tmp_opp) /= "once") ) THEN |
|
|
|
|
159 |
itax = var_axid(fileid, varid) |
itax = var_axid(fileid, varid) |
160 |
itime = nb_wrt(fileid, varid)+1 |
itime = nb_wrt(fileid, varid) + 1 |
161 |
|
|
162 |
IF (tax_last(fileid, itax) < itime) THEN |
IF (tax_last(fileid, itax) < itime) THEN |
163 |
iret = NF90_PUT_VAR (ncid, tdimid(fileid, itax), (/ rtime /), & |
iret = NF90_PUT_VAR(ncid, tdimid(fileid, itax), (/rtime/), & |
164 |
start=(/ itime /), count=(/ 1 /)) |
start=(/itime/)) |
165 |
tax_last(fileid, itax) = itime |
tax_last(fileid, itax) = itime |
166 |
ENDIF |
ENDIF |
167 |
ELSE |
ELSE |
168 |
itime=1 |
itime=1 |
169 |
ENDIF |
ENDIF |
170 |
|
|
171 |
!- 6.3 Write the data. Only in the case of instantaneous output |
! 6.3 Write the data. Only in the case of instantaneous output |
172 |
! we do not write the buffer. |
! we do not write the buffer. |
173 |
|
|
174 |
IF (scsize(fileid, varid, 3) == 1) THEN |
IF (scsize(fileid, varid, 3) == 1) THEN |
175 |
IF (regular(fileid)) THEN |
IF (regular(fileid)) THEN |
176 |
corner(1:4) = (/ 1, 1, itime, 0 /) |
corner(1:4) = (/1, 1, itime, 0/) |
177 |
edges(1:4) = (/ zsize(fileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
178 |
zsize(fileid, varid, 2), & |
zsize(fileid, varid, 2), & |
179 |
1, 0 /) |
1, 0/) |
180 |
ELSE |
ELSE |
181 |
corner(1:4) = (/ 1, itime, 0, 0 /) |
corner(1:4) = (/1, itime, 0, 0/) |
182 |
edges(1:4) = (/ zsize(fileid, varid, 1), 1, 0, 0 /) |
edges(1:4) = (/zsize(fileid, varid, 1), 1, 0, 0/) |
183 |
ENDIF |
ENDIF |
184 |
ELSE |
ELSE |
185 |
IF ( regular(fileid) ) THEN |
IF (regular(fileid)) THEN |
186 |
corner(1:4) = (/ 1, 1, 1, itime /) |
corner(1:4) = (/1, 1, 1, itime/) |
187 |
edges(1:4) = (/ zsize(fileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
188 |
zsize(fileid, varid, 2), & |
zsize(fileid, varid, 2), & |
189 |
zsize(fileid, varid, 3), 1 /) |
zsize(fileid, varid, 3), 1/) |
190 |
ELSE |
ELSE |
191 |
corner(1:4) = (/ 1, 1, itime, 0 /) |
corner(1:4) = (/1, 1, itime, 0/) |
192 |
edges(1:4) = (/ zsize(fileid, varid, 1), & |
edges(1:4) = (/zsize(fileid, varid, 1), & |
193 |
zsize(fileid, varid, 3), 1, 0 /) |
zsize(fileid, varid, 3), 1, 0/) |
194 |
ENDIF |
ENDIF |
195 |
ENDIF |
ENDIF |
196 |
|
|
197 |
ipt = point(fileid, varid) |
ipt = point(fileid, varid) |
198 |
|
|
199 |
IF ( (TRIM(tmp_opp) /= "inst") & |
IF ((TRIM(tmp_opp) /= "inst") .AND. (TRIM(tmp_opp) /= "once")) THEN |
200 |
.AND.(TRIM(tmp_opp) /= "once") ) THEN |
iret = NF90_PUT_VAR(ncid, ncvarid, buffer(ipt:), & |
|
iret = NF90_PUT_VAR (ncid, ncvarid, buffer(ipt:), & |
|
201 |
start=corner(1:4), count=edges(1:4)) |
start=corner(1:4), count=edges(1:4)) |
202 |
ELSE |
ELSE |
203 |
iret = NF90_PUT_VAR (ncid, ncvarid, buff_tmp2, & |
iret = NF90_PUT_VAR(ncid, ncvarid, buff_tmp2, & |
204 |
start=corner(1:4), count=edges(1:4)) |
start=corner(1:4), count=edges(1:4)) |
205 |
ENDIF |
ENDIF |
206 |
|
|
207 |
last_wrt(fileid, varid) = itau |
last_wrt(fileid, varid) = itau |
208 |
nb_wrt(fileid, varid) = nb_wrt(fileid, varid)+1 |
nb_wrt(fileid, varid) = nb_wrt(fileid, varid)+1 |
209 |
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) |
|
|
|
|
210 |
ENDIF |
ENDIF |
211 |
|
|
212 |
END SUBROUTINE histwrite_real |
END SUBROUTINE histwrite_real |