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