1 |
C $Header: /u/gcmpack/MITgcm/pkg/mdsio/mdsio_readvector.F,v 1.14 2009/02/03 23:10:28 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MDSIO_OPTIONS.h" |
5 |
|
6 |
SUBROUTINE MDSREADVECTOR( |
7 |
I fName, |
8 |
I filePrec, |
9 |
I arrType, |
10 |
I narr, |
11 |
O arr, |
12 |
I bi, |
13 |
I bj, |
14 |
I irecord, |
15 |
I myThid ) |
16 |
|
17 |
C Arguments: |
18 |
C |
19 |
C fName string :: base name for file to read |
20 |
C filePrec integer :: number of bits per word in file (32 or 64) |
21 |
C arrType char(2) :: declaration of "arr": either "RS" or "RL" |
22 |
C narr integer :: size of third dimension: normally either 1 or Nr |
23 |
C arr RS/RL :: array to read into, arr(narr) |
24 |
c bi integer :: x tile index |
25 |
c bj integer :: y tile index |
26 |
C irecord integer :: record number to read |
27 |
C myThid integer :: thread identifier |
28 |
C |
29 |
C Created: 03/26/99 eckert@mit.edu |
30 |
C Modified: 03/29/99 adcroft@mit.edu + eckert@mit.edu |
31 |
C Fixed to work work with _RS and _RL declarations |
32 |
C Modified: 07/27/99 eckert@mit.edu |
33 |
C Customized for state estimation (--> active_file_control.F) |
34 |
|
35 |
IMPLICIT NONE |
36 |
C Global variables / COMMON blocks |
37 |
#include "SIZE.h" |
38 |
#include "EEPARAMS.h" |
39 |
#include "PARAMS.h" |
40 |
#include "EESUPPORT.h" |
41 |
|
42 |
C Routine arguments |
43 |
CHARACTER*(*) fName |
44 |
INTEGER filePrec |
45 |
CHARACTER*(2) arrType |
46 |
INTEGER narr |
47 |
_RL arr(narr) |
48 |
INTEGER bi,bj |
49 |
INTEGER irecord |
50 |
INTEGER myThid |
51 |
|
52 |
#ifdef ALLOW_AUTODIFF |
53 |
|
54 |
C Functions |
55 |
INTEGER ILNBLNK |
56 |
INTEGER MDS_RECLEN |
57 |
C Local variables |
58 |
CHARACTER*(MAX_LEN_FNAM) dataFName,pfName |
59 |
INTEGER iG,jG,irec,dUnit,IL,pIL |
60 |
LOGICAL exst |
61 |
LOGICAL globalFile,fileIsOpen |
62 |
INTEGER length_of_rec |
63 |
CHARACTER*(MAX_LEN_MBUF) msgBuf |
64 |
|
65 |
cph( |
66 |
cph Deal with useSingleCpuIO |
67 |
cph Not extended here for EXCH2 |
68 |
INTEGER k,l |
69 |
INTEGER vec_size |
70 |
Real*4 xy_buffer_r4(narr*nPx*nPy) |
71 |
Real*8 xy_buffer_r8(narr*nPx*nPy) |
72 |
Real*8 global (narr*nPx*nPy) |
73 |
_RL local(narr) |
74 |
cph) |
75 |
C ------------------------------------------------------------------ |
76 |
|
77 |
vec_size = narr*nPx*nPy |
78 |
|
79 |
C Only DO I/O IF I am the master thread |
80 |
_BEGIN_MASTER( myThid ) |
81 |
|
82 |
C Record number must be >= 1 |
83 |
IF (irecord .LT. 1) THEN |
84 |
WRITE(msgBuf,'(A,I9.8)') |
85 |
& ' MDSREADVECTOR: argument irecord = ',irecord |
86 |
CALL PRINT_ERROR( msgBuf, myThid ) |
87 |
WRITE(msgBuf,'(A)') |
88 |
& ' MDSREADVECTOR: invalid value for irecord' |
89 |
CALL PRINT_ERROR( msgBuf, myThid ) |
90 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
91 |
ENDIF |
92 |
|
93 |
C Assume nothing |
94 |
globalFile = .FALSE. |
95 |
fileIsOpen = .FALSE. |
96 |
IL = ILNBLNK( fName ) |
97 |
pIL = ILNBLNK( mdsioLocalDir ) |
98 |
|
99 |
C Assign special directory |
100 |
IF ( mdsioLocalDir .NE. ' ' ) THEN |
101 |
WRITE(pfName,'(2A)') |
102 |
& mdsioLocalDir(1:pIL), fName(1:IL) |
103 |
ELSE |
104 |
pfName= fName |
105 |
ENDIF |
106 |
pIL=ILNBLNK( pfName ) |
107 |
|
108 |
C Assign a free unit number as the I/O channel for this routine |
109 |
CALL MDSFINDUNIT( dUnit, myThid ) |
110 |
|
111 |
IF ( .not. useSingleCPUIO ) THEN |
112 |
|
113 |
C Check first for global file with simple name (ie. fName) |
114 |
dataFName = fName |
115 |
INQUIRE( file=dataFName, exist=exst ) |
116 |
IF (exst) THEN |
117 |
IF ( debugLevel .GE. debLevB ) THEN |
118 |
WRITE(msgBuf,'(A,A)') |
119 |
& ' MDSREADVECTOR: opening global file: ',dataFName(1:IL) |
120 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
121 |
& SQUEEZE_RIGHT , myThid ) |
122 |
ENDIF |
123 |
globalFile = .TRUE. |
124 |
ENDIF |
125 |
|
126 |
C If negative check for global file with MDS name (ie. fName.data) |
127 |
IF (.NOT. globalFile) THEN |
128 |
WRITE(dataFName,'(2A)') fName(1:IL),'.data' |
129 |
INQUIRE( file=dataFName, exist=exst ) |
130 |
IF (exst) THEN |
131 |
IF ( debugLevel .GE. debLevB ) THEN |
132 |
WRITE(msgBuf,'(A,A)') |
133 |
& ' MDSREADVECTOR: opening global file: ',dataFName(1:IL+5) |
134 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
135 |
& SQUEEZE_RIGHT , myThid ) |
136 |
ENDIF |
137 |
globalFile = .TRUE. |
138 |
ENDIF |
139 |
ENDIF |
140 |
|
141 |
C If we are reading from a global file then we open it here |
142 |
IF (globalFile) THEN |
143 |
length_of_rec=MDS_RECLEN( filePrec, narr, myThid ) |
144 |
OPEN( dUnit, file=dataFName, status='old', |
145 |
& access='direct', recl=length_of_rec ) |
146 |
fileIsOpen=.TRUE. |
147 |
ENDIF |
148 |
|
149 |
C Loop over all tiles |
150 |
c DO bj=1,nSy |
151 |
c DO bi=1,nSx |
152 |
C If we are reading from a tiled MDS file then we open each one here |
153 |
IF (.NOT. globalFile) THEN |
154 |
iG=bi+(myXGlobalLo-1)/sNx ! Kludge until unstructered tiles |
155 |
jG=bj+(myYGlobalLo-1)/sNy ! Kludge until unstructered tiles |
156 |
WRITE(dataFName,'(2A,I3.3,A,I3.3,A)') |
157 |
& pfName(1:pIL),'.',iG,'.',jG,'.data' |
158 |
INQUIRE( file=dataFName, exist=exst ) |
159 |
C Of course, we only open the file IF the tile is "active" |
160 |
C (This is a place-holder for the active/passive mechanism) |
161 |
IF (exst) THEN |
162 |
IF ( debugLevel .GE. debLevB ) THEN |
163 |
WRITE(msgBuf,'(A,A)') |
164 |
& ' MDSREADVECTOR: opening file: ',dataFName(1:pIL+13) |
165 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
166 |
& SQUEEZE_RIGHT , myThid ) |
167 |
ENDIF |
168 |
length_of_rec=MDS_RECLEN( filePrec, narr, myThid ) |
169 |
OPEN( dUnit, file=dataFName, status='old', |
170 |
& access='direct', recl=length_of_rec ) |
171 |
fileIsOpen=.TRUE. |
172 |
ELSE |
173 |
fileIsOpen=.FALSE. |
174 |
WRITE(msgBuf,'(4A)') |
175 |
& ' MDSREADVECTOR: opening file: ',fName(1:IL), |
176 |
& ' , ',dataFName(1:pIL+13) |
177 |
CALL PRINT_ERROR( msgBuf, myThid ) |
178 |
WRITE(msgBuf,'(A)') |
179 |
& ' MDSREADVECTOR: un-active tiles not implemented yet' |
180 |
CALL PRINT_ERROR( msgBuf, myThid ) |
181 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
182 |
ENDIF |
183 |
ENDIF |
184 |
IF (fileIsOpen) THEN |
185 |
IF (globalFile) THEN |
186 |
iG = myXGlobalLo-1+(bi-1)*sNx |
187 |
jG = myYGlobalLo-1+(bj-1)*sNy |
188 |
irec = 1 + int(iG/sNx) + (jG/sNy)*nSx*nPx + |
189 |
& (irecord-1)*nSx*nPx*nSy*nPy |
190 |
ELSE |
191 |
iG = 0 |
192 |
jG = 0 |
193 |
irec = irecord |
194 |
ENDIF |
195 |
IF ( arrType.EQ.'RS' ) THEN |
196 |
CALL MDS_RD_REC_RS( arr, xy_buffer_r4, xy_buffer_r8, |
197 |
I filePrec, dUnit, irec, narr, myThid ) |
198 |
ELSEIF ( arrType.EQ.'RL' ) THEN |
199 |
CALL MDS_RD_REC_RL( arr, xy_buffer_r4, xy_buffer_r8, |
200 |
I filePrec, dUnit, irec, narr, myThid ) |
201 |
ELSE |
202 |
WRITE(msgBuf,'(A)') |
203 |
& ' MDSREADVECTOR: illegal value for arrType' |
204 |
CALL PRINT_ERROR( msgBuf, myThid ) |
205 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
206 |
ENDIF |
207 |
IF (.NOT. globalFile) THEN |
208 |
CLOSE( dUnit ) |
209 |
fileIsOpen = .FALSE. |
210 |
ENDIF |
211 |
ENDIF |
212 |
C End of bi,bj loops |
213 |
c ENDDO |
214 |
c ENDDO |
215 |
|
216 |
C If global file was opened then close it |
217 |
IF (fileIsOpen .AND. globalFile) THEN |
218 |
CLOSE( dUnit ) |
219 |
fileIsOpen = .FALSE. |
220 |
ENDIF |
221 |
|
222 |
_END_MASTER( myThid ) |
223 |
|
224 |
ENDIF |
225 |
C end-if ( .not. useSingleCPUIO ) |
226 |
|
227 |
|
228 |
C ------------------------------------------------------------------ |
229 |
|
230 |
|
231 |
IF ( useSingleCPUIO ) THEN |
232 |
|
233 |
C master thread of process 0, only, opens a global file |
234 |
_BEGIN_MASTER( myThid ) |
235 |
#ifdef ALLOW_USE_MPI |
236 |
IF( mpiMyId .EQ. 0 ) THEN |
237 |
#else |
238 |
IF ( .TRUE. ) THEN |
239 |
#endif /* ALLOW_USE_MPI */ |
240 |
|
241 |
C Check first for global file with simple name (ie. fName) |
242 |
dataFName = fName |
243 |
INQUIRE( file=dataFName, exist=exst ) |
244 |
IF (exst) globalFile = .TRUE. |
245 |
|
246 |
C If negative check for global file with MDS name (ie. fName.data) |
247 |
IF (.NOT. globalFile) THEN |
248 |
WRITE(dataFName,'(2a)') fName(1:IL),'.data' |
249 |
INQUIRE( file=dataFName, exist=exst ) |
250 |
IF (exst) globalFile = .TRUE. |
251 |
ENDIF |
252 |
|
253 |
C If global file is visible to process 0, then open it here. |
254 |
C Otherwise stop program. |
255 |
IF ( globalFile) THEN |
256 |
length_of_rec=MDS_RECLEN( filePrec, vec_size, myThid ) |
257 |
OPEN( dUnit, file=dataFName, status='old', |
258 |
& access='direct', recl=length_of_rec ) |
259 |
ELSE |
260 |
WRITE(msgBuf,'(2A)') |
261 |
& ' MDSREADVECTOR: filename: ',dataFName(1:IL) |
262 |
C-jmc: why double print (stdout + stderr) ? |
263 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
264 |
& SQUEEZE_RIGHT , myThid ) |
265 |
CALL PRINT_ERROR( msgBuf, myThid ) |
266 |
WRITE(msgBuf,'(A)') |
267 |
& ' MDSREADVECTOR: File does not exist' |
268 |
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit, |
269 |
& SQUEEZE_RIGHT , myThid ) |
270 |
CALL PRINT_ERROR( msgBuf, myThid ) |
271 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
272 |
ENDIF |
273 |
|
274 |
ENDIF |
275 |
_END_MASTER( myThid ) |
276 |
|
277 |
DO k=1,1 |
278 |
|
279 |
_BEGIN_MASTER( myThid ) |
280 |
#ifdef ALLOW_USE_MPI |
281 |
IF( mpiMyId .EQ. 0 ) THEN |
282 |
#else |
283 |
IF ( .TRUE. ) THEN |
284 |
#endif /* ALLOW_USE_MPI */ |
285 |
irec = irecord |
286 |
IF (filePrec .EQ. precFloat32) THEN |
287 |
READ(dUnit,rec=irec) xy_buffer_r4 |
288 |
#ifdef _BYTESWAPIO |
289 |
CALL MDS_BYTESWAPR4( vec_size, xy_buffer_r4 ) |
290 |
#endif |
291 |
cph#if defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) |
292 |
c |
293 |
cph#else /* defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) */ |
294 |
DO L=1,narr*nPx*nPy |
295 |
global(L) = xy_buffer_r4(L) |
296 |
ENDDO |
297 |
cph#endif /* defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) */ |
298 |
ELSEIF (filePrec .EQ. precFloat64) THEN |
299 |
READ(dUnit,rec=irec) xy_buffer_r8 |
300 |
#ifdef _BYTESWAPIO |
301 |
CALL MDS_BYTESWAPR8( vec_size, xy_buffer_r8 ) |
302 |
#endif |
303 |
cph#if defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) |
304 |
c |
305 |
cph#else /* defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) */ |
306 |
DO L=1,narr*nPx*nPy |
307 |
global(L) = xy_buffer_r8(L) |
308 |
ENDDO |
309 |
cph#endif /* defined(ALLOW_EXCH2) && !defined(MISSING_TILE_IO) */ |
310 |
ELSE |
311 |
WRITE(msgBuf,'(A)') |
312 |
& ' MDSREADVECTOR: illegal value for filePrec' |
313 |
CALL PRINT_ERROR( msgBuf, myThid ) |
314 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
315 |
ENDIF |
316 |
ENDIF |
317 |
_END_MASTER( myThid ) |
318 |
CALL SCATTER_VECTOR( narr,global,local,myThid ) |
319 |
IF ( arrType.EQ.'RS' ) THEN |
320 |
CALL MDS_BUFFERtoRS( local, arr, narr, .TRUE., myThid ) |
321 |
ELSEIF ( arrType.EQ.'RL' ) THEN |
322 |
CALL MDS_BUFFERtoRL( local, arr, narr, .TRUE., myThid ) |
323 |
ELSE |
324 |
WRITE(msgBuf,'(A)') |
325 |
& ' MDSREADVECTOR: illegal value for arrType' |
326 |
CALL PRINT_ERROR( msgBuf, myThid ) |
327 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR' |
328 |
ENDIF |
329 |
|
330 |
ENDDO |
331 |
C end-do k=1,1 |
332 |
|
333 |
_BEGIN_MASTER( myThid ) |
334 |
CLOSE( dUnit ) |
335 |
_END_MASTER( myThid ) |
336 |
|
337 |
ENDIF |
338 |
C end-if ( useSingleCPUIO ) |
339 |
|
340 |
#else /* ALLOW_AUTODIFF */ |
341 |
STOP 'ABNORMAL END: S/R MDSREADVECTOR is empty' |
342 |
#endif /* ALLOW_AUTODIFF */ |
343 |
|
344 |
RETURN |
345 |
END |