1 |
C $Header: $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MDSIO_OPTIONS.h" |
5 |
|
6 |
SUBROUTINE MDSWRITEFIELD( |
7 |
I fName, |
8 |
I filePrec, |
9 |
I globalFile, |
10 |
I arrType, |
11 |
I nNz, |
12 |
I arr, |
13 |
I irecord, |
14 |
I myIter, |
15 |
I myThid ) |
16 |
C |
17 |
C Arguments: |
18 |
C |
19 |
C fName string base name for file to written |
20 |
C filePrec integer number of bits per word in file (32 or 64) |
21 |
C globalFile logical selects between writing a global or tiled file |
22 |
C arrType char(2) declaration of "arr": either "RS" or "RL" |
23 |
C nNz integer size of third dimension: normally either 1 or Nr |
24 |
C arr RS/RL array to write, arr(:,:,nNz,:,:) |
25 |
C irecord integer record number to read |
26 |
C myIter integer time step number |
27 |
C myThid integer thread identifier |
28 |
C |
29 |
C MDSWRITEFIELD creates either a file of the form "fName.data" and |
30 |
C "fName.meta" if the logical flag "globalFile" is set true. Otherwise |
31 |
C it creates MDS tiled files of the form "fName.xxx.yyy.data" and |
32 |
C "fName.xxx.yyy.meta". A meta-file is always created. |
33 |
C Currently, the meta-files are not read because it is difficult |
34 |
C to parse files in fortran. We should read meta information before |
35 |
C adding records to an existing multi-record file. |
36 |
C The precision of the file is decsribed by filePrec, set either |
37 |
C to floatPrec32 or floatPrec64. The precision or declaration of |
38 |
C the array argument must be consistently described by the char*(2) |
39 |
C string arrType, either "RS" or "RL". nNz allows for both 2-D and |
40 |
C 3-D arrays to be handled. nNz=1 implies a 2-D model field and |
41 |
C nNz=Nr implies a 3-D model field. irecord is the record number |
42 |
C to be read and must be >= 1. NOTE: It is currently assumed that |
43 |
C the highest record number in the file was the last record written. |
44 |
C Nor is there a consistency check between the routine arguments and file. |
45 |
C ie. if your write record 2 after record 4 the meta information |
46 |
C will record the number of records to be 2. This, again, is because |
47 |
C we have read the meta information. To be fixed. |
48 |
C |
49 |
C Created: 03/16/99 adcroft@mit.edu |
50 |
C |
51 |
C Changed: 05/31/00 heimbach@mit.edu |
52 |
C open(dUnit, ..., status='old', ... -> status='unknown' |
53 |
|
54 |
implicit none |
55 |
C Global variables / common blocks |
56 |
#include "SIZE.h" |
57 |
#include "EEPARAMS.h" |
58 |
#include "PARAMS.h" |
59 |
|
60 |
C Routine arguments |
61 |
character*(*) fName |
62 |
integer filePrec |
63 |
logical globalFile |
64 |
character*(2) arrType |
65 |
integer nNz |
66 |
cph( |
67 |
cph Real arr(*) |
68 |
_RL arr(1-oLx:sNx+oLx,1-oLy:sNy+oLy,nNz,nSx,nSy) |
69 |
cph) |
70 |
integer irecord |
71 |
integer myIter |
72 |
integer myThid |
73 |
C Functions |
74 |
integer ILNBLNK |
75 |
integer MDS_RECLEN |
76 |
C Local variables |
77 |
character*(80) dataFName,metaFName |
78 |
integer iG,jG,irec,bi,bj,j,k,dUnit,IL |
79 |
Real*4 r4seg(sNx) |
80 |
Real*8 r8seg(sNx) |
81 |
integer dimList(3,3),ndims |
82 |
integer length_of_rec |
83 |
logical fileIsOpen |
84 |
character*(max_len_mbuf) msgbuf |
85 |
C ------------------------------------------------------------------ |
86 |
|
87 |
C Only do I/O if I am the master thread |
88 |
_BEGIN_MASTER( myThid ) |
89 |
|
90 |
C Record number must be >= 1 |
91 |
if (irecord .LT. 1) then |
92 |
write(msgbuf,'(a,i9.8)') |
93 |
& ' MDSWRITEFIELD: argument irecord = ',irecord |
94 |
call print_message( msgbuf, standardmessageunit, |
95 |
& SQUEEZE_RIGHT , mythid) |
96 |
write(msgbuf,'(a)') |
97 |
& ' MDSWRITEFIELD: invalid value for irecord' |
98 |
call print_error( msgbuf, mythid ) |
99 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
100 |
endif |
101 |
|
102 |
C Assume nothing |
103 |
fileIsOpen=.FALSE. |
104 |
IL=ILNBLNK( fName ) |
105 |
|
106 |
C Assign a free unit number as the I/O channel for this routine |
107 |
call MDSFINDUNIT( dUnit, mythid ) |
108 |
|
109 |
C If we are writing to a global file then we open it here |
110 |
if (globalFile) then |
111 |
write(dataFname(1:80),'(2a)') fName(1:IL),'.data' |
112 |
if (irecord .EQ. 1) then |
113 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
114 |
open( dUnit, file=dataFName, status=_NEW_STATUS, |
115 |
& access='direct', recl=length_of_rec ) |
116 |
fileIsOpen=.TRUE. |
117 |
else |
118 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
119 |
open( dUnit, file=dataFName, status=_OLD_STATUS, |
120 |
& access='direct', recl=length_of_rec ) |
121 |
fileIsOpen=.TRUE. |
122 |
endif |
123 |
endif |
124 |
|
125 |
C Loop over all tiles |
126 |
do bj=1,nSy |
127 |
do bi=1,nSx |
128 |
C If we are writing to a tiled MDS file then we open each one here |
129 |
if (.NOT. globalFile) then |
130 |
iG=bi+(myXGlobalLo-1)/sNx ! Kludge until unstructered tiles |
131 |
jG=bj+(myYGlobalLo-1)/sNy ! Kludge until unstructered tiles |
132 |
write(dataFname(1:80),'(2a,i3.3,a,i3.3,a)') |
133 |
& fName(1:IL),'.',iG,'.',jG,'.data' |
134 |
if (irecord .EQ. 1) then |
135 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
136 |
open( dUnit, file=dataFName, status=_NEW_STATUS, |
137 |
& access='direct', recl=length_of_rec ) |
138 |
fileIsOpen=.TRUE. |
139 |
else |
140 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
141 |
open( dUnit, file=dataFName, status=_OLD_STATUS, |
142 |
& access='direct', recl=length_of_rec ) |
143 |
fileIsOpen=.TRUE. |
144 |
endif |
145 |
endif |
146 |
if (fileIsOpen) then |
147 |
do k=1,nNz |
148 |
do j=1,sNy |
149 |
if (globalFile) then |
150 |
iG = myXGlobalLo-1+(bi-1)*sNx |
151 |
jG = myYGlobalLo-1+(bj-1)*sNy |
152 |
irec=1+INT(iG/sNx)+nSx*nPx*(jG+j-1)+nSx*nPx*Ny*(k-1) |
153 |
& +nSx*nPx*Ny*nNz*(irecord-1) |
154 |
else |
155 |
iG = 0 |
156 |
jG = 0 |
157 |
irec=j + sNy*(k-1) + sNy*nNz*(irecord-1) |
158 |
endif |
159 |
if (filePrec .eq. precFloat32) then |
160 |
if (arrType .eq. 'RS') then |
161 |
call MDS_SEG4toRS( j,bi,bj,k,nNz, r4seg, .FALSE., arr ) |
162 |
elseif (arrType .eq. 'RL') then |
163 |
call MDS_SEG4toRL( j,bi,bj,k,nNz, r4seg, .FALSE., arr ) |
164 |
else |
165 |
write(msgbuf,'(a)') |
166 |
& ' MDSWRITEFIELD: illegal value for arrType' |
167 |
call print_error( msgbuf, mythid ) |
168 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
169 |
endif |
170 |
#ifdef _BYTESWAPIO |
171 |
call MDS_BYTESWAPR4( sNx, r4seg ) |
172 |
#endif |
173 |
write(dUnit,rec=irec) r4seg |
174 |
elseif (filePrec .eq. precFloat64) then |
175 |
if (arrType .eq. 'RS') then |
176 |
call MDS_SEG8toRS( j,bi,bj,k,nNz, r8seg, .FALSE., arr ) |
177 |
elseif (arrType .eq. 'RL') then |
178 |
call MDS_SEG8toRL( j,bi,bj,k,nNz, r8seg, .FALSE., arr ) |
179 |
else |
180 |
write(msgbuf,'(a)') |
181 |
& ' MDSWRITEFIELD: illegal value for arrType' |
182 |
call print_error( msgbuf, mythid ) |
183 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
184 |
endif |
185 |
#ifdef _BYTESWAPIO |
186 |
call MDS_BYTESWAPR8( sNx, r8seg ) |
187 |
#endif |
188 |
write(dUnit,rec=irec) r8seg |
189 |
else |
190 |
write(msgbuf,'(a)') |
191 |
& ' MDSWRITEFIELD: illegal value for filePrec' |
192 |
call print_error( msgbuf, mythid ) |
193 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
194 |
endif |
195 |
C End of j loop |
196 |
enddo |
197 |
C End of k loop |
198 |
enddo |
199 |
else |
200 |
write(msgbuf,'(a)') |
201 |
& ' MDSWRITEFIELD: I should never get to this point' |
202 |
call print_error( msgbuf, mythid ) |
203 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
204 |
endif |
205 |
C If we were writing to a tiled MDS file then we close it here |
206 |
if (fileIsOpen .AND. (.NOT. globalFile)) then |
207 |
close( dUnit ) |
208 |
fileIsOpen = .FALSE. |
209 |
endif |
210 |
C Create meta-file for each tile if we are tiling |
211 |
if (.NOT. globalFile) then |
212 |
iG=bi+(myXGlobalLo-1)/sNx ! Kludge until unstructered tiles |
213 |
jG=bj+(myYGlobalLo-1)/sNy ! Kludge until unstructered tiles |
214 |
write(metaFname(1:80),'(2a,i3.3,a,i3.3,a)') |
215 |
& fName(1:IL),'.',iG,'.',jG,'.meta' |
216 |
dimList(1,1)=Nx |
217 |
dimList(2,1)=myXGlobalLo+(bi-1)*sNx |
218 |
dimList(3,1)=myXGlobalLo+bi*sNx-1 |
219 |
dimList(1,2)=Ny |
220 |
dimList(2,2)=myYGlobalLo+(bj-1)*sNy |
221 |
dimList(3,2)=myYGlobalLo+bj*sNy-1 |
222 |
dimList(1,3)=Nr |
223 |
dimList(2,3)=1 |
224 |
dimList(3,3)=Nr |
225 |
ndims=3 |
226 |
if (nNz .EQ. 1) ndims=2 |
227 |
call MDSWRITEMETA( metaFName, dataFName, |
228 |
& filePrec, ndims, dimList, irecord, myIter, mythid ) |
229 |
endif |
230 |
C End of bi,bj loops |
231 |
enddo |
232 |
enddo |
233 |
|
234 |
C If global file was opened then close it |
235 |
if (fileIsOpen .AND. globalFile) then |
236 |
close( dUnit ) |
237 |
fileIsOpen = .FALSE. |
238 |
endif |
239 |
|
240 |
C Create meta-file for the global-file |
241 |
if (globalFile) then |
242 |
C We can not do this operation using threads (yet) because of the |
243 |
C "barrier" at the next step. The barrier could be removed but |
244 |
C at the cost of "safe" distributed I/O. |
245 |
if (nThreads.NE.1) then |
246 |
write(msgbuf,'(a,a)') |
247 |
& ' MDSWRITEFIELD: A threads version of this routine', |
248 |
& ' does not exist.' |
249 |
call print_message( msgbuf, standardmessageunit, |
250 |
& SQUEEZE_RIGHT , mythid) |
251 |
write(msgbuf,'(a)') |
252 |
& ' MDSWRITEFIELD: This needs to be fixed...' |
253 |
call print_message( msgbuf, standardmessageunit, |
254 |
& SQUEEZE_RIGHT , mythid) |
255 |
write(msgbuf,'(a,i3.2)') |
256 |
& ' MDSWRITEFIELD: nThreads = ',nThreads |
257 |
call print_message( msgbuf, standardmessageunit, |
258 |
& SQUEEZE_RIGHT , mythid) |
259 |
write(msgbuf,'(a)') |
260 |
& ' MDSWRITEFIELD: Stopping because you are using threads' |
261 |
call print_error( msgbuf, mythid ) |
262 |
stop 'ABNORMAL END: S/R MDSWRITEFIELD' |
263 |
endif |
264 |
C We put a barrier here to ensure that all processes have finished |
265 |
C writing their data before we update the meta-file |
266 |
_BARRIER |
267 |
write(metaFName(1:80),'(2a)') fName(1:IL),'.meta' |
268 |
dimList(1,1)=Nx |
269 |
dimList(2,1)=1 |
270 |
dimList(3,1)=Nx |
271 |
dimList(1,2)=Ny |
272 |
dimList(2,2)=1 |
273 |
dimList(3,2)=Ny |
274 |
dimList(1,3)=Nr |
275 |
dimList(2,3)=1 |
276 |
dimList(3,3)=Nr |
277 |
ndims=3 |
278 |
if (nNz .EQ. 1) ndims=2 |
279 |
call MDSWRITEMETA( metaFName, dataFName, |
280 |
& filePrec, ndims, dimList, irecord, myIter, mythid ) |
281 |
fileIsOpen=.TRUE. |
282 |
endif |
283 |
|
284 |
_END_MASTER( myThid ) |
285 |
|
286 |
C ------------------------------------------------------------------ |
287 |
return |
288 |
end |