1 |
C $Header: $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MDSIO_OPTIONS.h" |
5 |
|
6 |
SUBROUTINE MDSIO_READTILE( |
7 |
I fName, |
8 |
I filePrec, |
9 |
I arrType, |
10 |
I nNz, |
11 |
O arr, |
12 |
I bi, bj, |
13 |
I irecord, |
14 |
I myThid ) |
15 |
C |
16 |
C Arguments: |
17 |
C |
18 |
C fName string base name for file to read |
19 |
C filePrec integer number of bits per word in file (32 or 64) |
20 |
C arrType char(2) declaration of "arr": either "RS" or "RL" |
21 |
C nNz integer size of third dimension: normally either 1 or Nr |
22 |
C arr RS/RL array to read into, arr(:,:,nNz,:,:) |
23 |
C irecord integer record number to read |
24 |
C myThid integer thread identifier |
25 |
C |
26 |
C MDSIO_READTILE first checks to see if the file "fName" exists, then |
27 |
C if the file "fName.data" exists and finally the tiled files of the |
28 |
C form "fName.xxx.yyy.data" exist. Currently, the meta-files are not |
29 |
C read because it is difficult to parse files in fortran. |
30 |
C The precision of the file is decsribed by filePrec, set either |
31 |
C to floatPrec32 or floatPrec64. The precision or declaration of |
32 |
C the array argument must be consistently described by the char*(2) |
33 |
C string arrType, either "RS" or "RL". nNz allows for both 2-D and |
34 |
C 3-D arrays to be handled. nNz=1 implies a 2-D model field and |
35 |
C nNz=Nr implies a 3-D model field. irecord is the record number |
36 |
C to be read and must be >= 1. The file data is stored in |
37 |
C arr *but* the overlaps are *not* updated. ie. An exchange must |
38 |
C be called. This is because the routine is sometimes called from |
39 |
C within a MASTER_THID region. |
40 |
C |
41 |
C Created: 03/16/99 adcroft@mit.edu |
42 |
|
43 |
implicit none |
44 |
C Global variables / common blocks |
45 |
#include "SIZE.h" |
46 |
#include "EEPARAMS.h" |
47 |
#include "PARAMS.h" |
48 |
|
49 |
C Routine arguments |
50 |
character*(*) fName |
51 |
integer filePrec |
52 |
character*(2) arrType |
53 |
integer nNz |
54 |
Real arr(*) |
55 |
integer bi, bj |
56 |
integer irecord |
57 |
integer myThid |
58 |
C Functions |
59 |
integer ILNBLNK |
60 |
integer MDS_RECLEN |
61 |
C Local variables |
62 |
character*(80) dataFName |
63 |
integer iG,jG,irec,j,k,dUnit,IL |
64 |
logical exst |
65 |
Real*4 r4seg(sNx) |
66 |
Real*8 r8seg(sNx) |
67 |
logical globalFile,fileIsOpen |
68 |
integer length_of_rec |
69 |
character*(max_len_mbuf) msgbuf |
70 |
C ------------------------------------------------------------------ |
71 |
|
72 |
C Only do I/O if I am the master thread |
73 |
_BEGIN_MASTER( myThid ) |
74 |
|
75 |
C Record number must be >= 1 |
76 |
if (irecord .LT. 1) then |
77 |
write(msgbuf,'(a,i9.8)') |
78 |
& ' MDSIO_READTILE: argument irecord = ',irecord |
79 |
call print_message( msgbuf, standardmessageunit, |
80 |
& SQUEEZE_RIGHT , mythid) |
81 |
write(msgbuf,'(a)') |
82 |
& ' MDSIO_READTILE: Invalid value for irecord' |
83 |
call print_error( msgbuf, mythid ) |
84 |
stop 'ABNORMAL END: S/R MDSIO_READTILE' |
85 |
endif |
86 |
|
87 |
C Assume nothing |
88 |
globalFile = .FALSE. |
89 |
fileIsOpen = .FALSE. |
90 |
IL=ILNBLNK( fName ) |
91 |
|
92 |
C Assign a free unit number as the I/O channel for this routine |
93 |
call MDSFINDUNIT( dUnit, mythid ) |
94 |
|
95 |
C Check first for global file with simple name (ie. fName) |
96 |
dataFName = fName |
97 |
inquire( file=dataFname, exist=exst ) |
98 |
if (exst) then |
99 |
write(msgbuf,'(a,a)') |
100 |
& ' MDSIO_READTILE: opening global file: ',dataFName |
101 |
call print_message( msgbuf, standardmessageunit, |
102 |
& SQUEEZE_RIGHT , mythid) |
103 |
globalFile = .TRUE. |
104 |
endif |
105 |
|
106 |
C If negative check for global file with MDS name (ie. fName.data) |
107 |
if (.NOT. globalFile) then |
108 |
write(dataFname(1:80),'(2a)') fName(1:IL),'.data' |
109 |
inquire( file=dataFname, exist=exst ) |
110 |
if (exst) then |
111 |
write(msgbuf,'(a,a)') |
112 |
& ' MDSIO_READTILE: opening global file: ',dataFName |
113 |
call print_message( msgbuf, standardmessageunit, |
114 |
& SQUEEZE_RIGHT , mythid) |
115 |
globalFile = .TRUE. |
116 |
endif |
117 |
endif |
118 |
|
119 |
C If we are reading from a global file then we open it here |
120 |
if (globalFile) then |
121 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
122 |
open( dUnit, file=dataFName, status='old', |
123 |
& access='direct', recl=length_of_rec ) |
124 |
fileIsOpen=.TRUE. |
125 |
endif |
126 |
|
127 |
C Loop over all tiles |
128 |
c do bj=1,nSy |
129 |
c do bi=1,nSx |
130 |
|
131 |
C If we are reading from a tiled MDS file then we open each one here |
132 |
if (.NOT. globalFile) then |
133 |
iG=bi+(myXGlobalLo-1)/sNx ! Kludge until unstructered tiles |
134 |
jG=bj+(myYGlobalLo-1)/sNy ! Kludge until unstructered tiles |
135 |
write(dataFname(1:80),'(2a,i3.3,a,i3.3,a)') |
136 |
& fName(1:IL),'.',iG,'.',jG,'.data' |
137 |
inquire( file=dataFname, exist=exst ) |
138 |
C Of course, we only open the file if the tile is "active" |
139 |
C (This is a place-holder for the active/passive mechanism |
140 |
if (exst) then |
141 |
write(msgbuf,'(a,a)') |
142 |
& ' MDSIO_READTILE: opening file: ',dataFName |
143 |
call print_message( msgbuf, standardmessageunit, |
144 |
& SQUEEZE_RIGHT , mythid) |
145 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
146 |
open( dUnit, file=dataFName, status='old', |
147 |
& access='direct', recl=length_of_rec ) |
148 |
fileIsOpen=.TRUE. |
149 |
else |
150 |
fileIsOpen=.FALSE. |
151 |
write(msgbuf,'(a,a)') |
152 |
& ' MDSIO_READTILE: filename: ',dataFName |
153 |
call print_message( msgbuf, standardmessageunit, |
154 |
& SQUEEZE_RIGHT , mythid) |
155 |
write(msgbuf,'(a)') |
156 |
& ' MDSIO_READTILE: File does not exist' |
157 |
call print_error( msgbuf, mythid ) |
158 |
stop 'ABNORMAL END: S/R MDSIO_READTILE' |
159 |
endif |
160 |
endif |
161 |
|
162 |
if (fileIsOpen) then |
163 |
do k=1,nNz |
164 |
do j=1,sNy |
165 |
if (globalFile) then |
166 |
iG = myXGlobalLo-1 + (bi-1)*sNx |
167 |
jG = myYGlobalLo-1 + (bj-1)*sNy |
168 |
irec=1 + INT(iG/sNx) + nSx*nPx*(jG+j-1) + nSx*nPx*Ny*(k-1) |
169 |
& + nSx*nPx*Ny*nNz*(irecord-1) |
170 |
else |
171 |
iG = 0 |
172 |
jG = 0 |
173 |
irec=j + sNy*(k-1) + sNy*nNz*(irecord-1) |
174 |
endif |
175 |
if (filePrec .eq. precFloat32) then |
176 |
read(dUnit,rec=irec) r4seg |
177 |
#ifdef _BYTESWAPIO |
178 |
call MDS_BYTESWAPR4( sNx, r4seg ) |
179 |
#endif |
180 |
if (arrType .eq. 'RS') then |
181 |
call MDS_SEG4toRS( j,bi,bj,k,nNz, r4seg, .TRUE., arr ) |
182 |
elseif (arrType .eq. 'RL') then |
183 |
call MDS_SEG4toRL( j,bi,bj,k,nNz, r4seg, .TRUE., arr ) |
184 |
else |
185 |
write(msgbuf,'(a)') |
186 |
& ' MDSIO_READTILE: illegal value for arrType' |
187 |
call print_error( msgbuf, mythid ) |
188 |
stop 'ABNORMAL END: S/R MDSIO_READTILE' |
189 |
endif |
190 |
elseif (filePrec .eq. precFloat64) then |
191 |
read(dUnit,rec=irec) r8seg |
192 |
#ifdef _BYTESWAPIO |
193 |
call MDS_BYTESWAPR8( sNx, r8seg ) |
194 |
#endif |
195 |
if (arrType .eq. 'RS') then |
196 |
call MDS_SEG8toRS( j,bi,bj,k,nNz, r8seg, .TRUE., arr ) |
197 |
elseif (arrType .eq. 'RL') then |
198 |
call MDS_SEG8toRL( j,bi,bj,k,nNz, r8seg, .TRUE., arr ) |
199 |
else |
200 |
write(msgbuf,'(a)') |
201 |
& ' MDSIO_READTILE: illegal value for arrType' |
202 |
call print_error( msgbuf, mythid ) |
203 |
stop 'ABNORMAL END: S/R MDSIO_READTILE' |
204 |
endif |
205 |
else |
206 |
write(msgbuf,'(a)') |
207 |
& ' MDSIO_READTILE: illegal value for filePrec' |
208 |
call print_error( msgbuf, mythid ) |
209 |
stop 'ABNORMAL END: S/R MDSIO_READTILE' |
210 |
endif |
211 |
C End of j loop |
212 |
enddo |
213 |
C End of k loop |
214 |
enddo |
215 |
if (.NOT. globalFile) then |
216 |
close( dUnit ) |
217 |
fileIsOpen = .FALSE. |
218 |
endif |
219 |
endif |
220 |
|
221 |
C End of bi,bj loops |
222 |
c enddo |
223 |
c enddo |
224 |
|
225 |
C If global file was opened then close it |
226 |
if (fileIsOpen .AND. globalFile) then |
227 |
close( dUnit ) |
228 |
fileIsOpen = .FALSE. |
229 |
endif |
230 |
|
231 |
_END_MASTER( myThid ) |
232 |
|
233 |
C ------------------------------------------------------------------ |
234 |
return |
235 |
end |