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