1 |
C $Header: /u/gcmpack/MITgcm/pkg/mdsio/mdsio_readtile.F,v 1.5 2009/08/05 23:17:54 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MDSIO_OPTIONS.h" |
5 |
|
6 |
SUBROUTINE MDS_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 MDS_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 |
|
48 |
C Routine arguments |
49 |
character*(*) fName |
50 |
integer filePrec |
51 |
character*(2) arrType |
52 |
integer nNz |
53 |
_RL arr(*) |
54 |
integer bi, bj |
55 |
integer irecord |
56 |
integer myThid |
57 |
C Functions |
58 |
integer ILNBLNK |
59 |
integer MDS_RECLEN |
60 |
C Local variables |
61 |
character*(MAX_LEN_FNAM) dataFName |
62 |
integer iG,jG,irec,j,k,dUnit,IL |
63 |
logical exst |
64 |
Real*4 r4seg(sNx) |
65 |
Real*8 r8seg(sNx) |
66 |
logical globalFile,fileIsOpen |
67 |
integer length_of_rec |
68 |
character*(max_len_mbuf) msgbuf |
69 |
C ------------------------------------------------------------------ |
70 |
|
71 |
STOP 'S/R MDS_READTILE: not maintained' |
72 |
|
73 |
C Only do I/O if I am the master thread |
74 |
_BEGIN_MASTER( myThid ) |
75 |
|
76 |
C Record number must be >= 1 |
77 |
if (irecord .LT. 1) then |
78 |
write(msgbuf,'(a,i9.8)') |
79 |
& ' MDS_READTILE: argument irecord = ',irecord |
80 |
call print_message( msgbuf, standardmessageunit, |
81 |
& SQUEEZE_RIGHT , mythid) |
82 |
write(msgbuf,'(a)') |
83 |
& ' MDS_READTILE: Invalid value for irecord' |
84 |
call print_error( msgbuf, mythid ) |
85 |
stop 'ABNORMAL END: S/R MDS_READTILE' |
86 |
endif |
87 |
|
88 |
C Assume nothing |
89 |
globalFile = .FALSE. |
90 |
fileIsOpen = .FALSE. |
91 |
IL=ILNBLNK( fName ) |
92 |
|
93 |
C Assign a free unit number as the I/O channel for this routine |
94 |
call MDSFINDUNIT( dUnit, mythid ) |
95 |
|
96 |
C Check first for global file with simple name (ie. fName) |
97 |
dataFName = fName |
98 |
inquire( file=dataFname, exist=exst ) |
99 |
if (exst) then |
100 |
write(msgbuf,'(a,a)') |
101 |
& ' MDS_READTILE: opening global file: ',dataFName(1:IL) |
102 |
call print_message( msgbuf, standardmessageunit, |
103 |
& SQUEEZE_RIGHT , mythid) |
104 |
globalFile = .TRUE. |
105 |
endif |
106 |
|
107 |
C If negative check for global file with MDS name (ie. fName.data) |
108 |
if (.NOT. globalFile) then |
109 |
write(dataFname,'(2a)') fName(1:IL),'.data' |
110 |
inquire( file=dataFname, exist=exst ) |
111 |
if (exst) then |
112 |
write(msgbuf,'(a,a)') |
113 |
& ' MDS_READTILE: opening global file: ',dataFName(1:IL+5) |
114 |
call print_message( msgbuf, standardmessageunit, |
115 |
& SQUEEZE_RIGHT , mythid) |
116 |
globalFile = .TRUE. |
117 |
endif |
118 |
endif |
119 |
|
120 |
C If we are reading from a global file then we open it here |
121 |
if (globalFile) then |
122 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
123 |
open( dUnit, file=dataFName, status='old', |
124 |
& access='direct', recl=length_of_rec ) |
125 |
fileIsOpen=.TRUE. |
126 |
endif |
127 |
|
128 |
C Loop over all tiles |
129 |
c do bj=1,nSy |
130 |
c do bi=1,nSx |
131 |
|
132 |
C If we are reading from a tiled MDS file then we open each one here |
133 |
if (.NOT. globalFile) then |
134 |
iG=bi+(myXGlobalLo-1)/sNx ! Kludge until unstructered tiles |
135 |
jG=bj+(myYGlobalLo-1)/sNy ! Kludge until unstructered tiles |
136 |
write(dataFname,'(2a,i3.3,a,i3.3,a)') |
137 |
& fName(1:IL),'.',iG,'.',jG,'.data' |
138 |
inquire( file=dataFname, exist=exst ) |
139 |
C Of course, we only open the file if the tile is "active" |
140 |
C (This is a place-holder for the active/passive mechanism |
141 |
if (exst) then |
142 |
write(msgbuf,'(a,a)') |
143 |
& ' MDS_READTILE: opening file: ',dataFName(1:IL+13) |
144 |
call print_message( msgbuf, standardmessageunit, |
145 |
& SQUEEZE_RIGHT , mythid) |
146 |
length_of_rec=MDS_RECLEN( filePrec, sNx, mythid ) |
147 |
open( dUnit, file=dataFName, status='old', |
148 |
& access='direct', recl=length_of_rec ) |
149 |
fileIsOpen=.TRUE. |
150 |
else |
151 |
fileIsOpen=.FALSE. |
152 |
write(msgbuf,'(a,a)') |
153 |
& ' MDS_READTILE: filename: ',dataFName(1:IL+13) |
154 |
call print_message( msgbuf, standardmessageunit, |
155 |
& SQUEEZE_RIGHT , mythid) |
156 |
write(msgbuf,'(a)') |
157 |
& ' MDS_READTILE: File does not exist' |
158 |
call print_error( msgbuf, mythid ) |
159 |
stop 'ABNORMAL END: S/R MDS_READTILE' |
160 |
endif |
161 |
endif |
162 |
|
163 |
if (fileIsOpen) then |
164 |
do k=1,nNz |
165 |
do j=1,sNy |
166 |
if (globalFile) then |
167 |
iG = myXGlobalLo-1 + (bi-1)*sNx |
168 |
jG = myYGlobalLo-1 + (bj-1)*sNy |
169 |
irec=1 + INT(iG/sNx) + nSx*nPx*(jG+j-1) + nSx*nPx*Ny*(k-1) |
170 |
& + nSx*nPx*Ny*nNz*(irecord-1) |
171 |
else |
172 |
iG = 0 |
173 |
jG = 0 |
174 |
irec=j + sNy*(k-1) + sNy*nNz*(irecord-1) |
175 |
endif |
176 |
if (filePrec .eq. precFloat32) then |
177 |
read(dUnit,rec=irec) r4seg |
178 |
#ifdef _BYTESWAPIO |
179 |
call MDS_BYTESWAPR4( sNx, r4seg ) |
180 |
#endif |
181 |
if (arrType .eq. 'RS') then |
182 |
call MDS_SEG4toRS( j,bi,bj,k,nNz, r4seg, .TRUE., arr ) |
183 |
elseif (arrType .eq. 'RL') then |
184 |
call MDS_SEG4toRL( j,bi,bj,k,nNz, r4seg, .TRUE., arr ) |
185 |
else |
186 |
write(msgbuf,'(a)') |
187 |
& ' MDS_READTILE: illegal value for arrType' |
188 |
call print_error( msgbuf, mythid ) |
189 |
stop 'ABNORMAL END: S/R MDS_READTILE' |
190 |
endif |
191 |
elseif (filePrec .eq. precFloat64) then |
192 |
read(dUnit,rec=irec) r8seg |
193 |
#ifdef _BYTESWAPIO |
194 |
call MDS_BYTESWAPR8( sNx, r8seg ) |
195 |
#endif |
196 |
if (arrType .eq. 'RS') then |
197 |
call MDS_SEG8toRS( j,bi,bj,k,nNz, r8seg, .TRUE., arr ) |
198 |
elseif (arrType .eq. 'RL') then |
199 |
call MDS_SEG8toRL( j,bi,bj,k,nNz, r8seg, .TRUE., arr ) |
200 |
else |
201 |
write(msgbuf,'(a)') |
202 |
& ' MDS_READTILE: illegal value for arrType' |
203 |
call print_error( msgbuf, mythid ) |
204 |
stop 'ABNORMAL END: S/R MDS_READTILE' |
205 |
endif |
206 |
else |
207 |
write(msgbuf,'(a)') |
208 |
& ' MDS_READTILE: illegal value for filePrec' |
209 |
call print_error( msgbuf, mythid ) |
210 |
stop 'ABNORMAL END: S/R MDS_READTILE' |
211 |
endif |
212 |
C End of j loop |
213 |
enddo |
214 |
C End of k loop |
215 |
enddo |
216 |
if (.NOT. globalFile) then |
217 |
close( dUnit ) |
218 |
fileIsOpen = .FALSE. |
219 |
endif |
220 |
endif |
221 |
|
222 |
C End of bi,bj loops |
223 |
c enddo |
224 |
c enddo |
225 |
|
226 |
C If global file was opened then close it |
227 |
if (fileIsOpen .AND. globalFile) then |
228 |
close( dUnit ) |
229 |
fileIsOpen = .FALSE. |
230 |
endif |
231 |
|
232 |
_END_MASTER( myThid ) |
233 |
|
234 |
C ------------------------------------------------------------------ |
235 |
return |
236 |
end |