1 |
C $Header: /u/gcmpack/MITgcm/pkg/mnc/mnc_grid.F,v 1.19 2008/05/22 12:21:19 mlosch Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "MNC_OPTIONS.h" |
5 |
|
6 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
7 |
CBOP 1 |
8 |
C !ROUTINE: MNC_GRID_INIT |
9 |
|
10 |
C !INTERFACE: |
11 |
SUBROUTINE MNC_GRID_INIT( |
12 |
I fname, |
13 |
I gname, |
14 |
I ndim, |
15 |
I dnames, |
16 |
I myThid ) |
17 |
|
18 |
C !DESCRIPTION: |
19 |
C Create an MNC grid within a NetCDF file context. |
20 |
|
21 |
C !USES: |
22 |
implicit none |
23 |
|
24 |
C !INPUT PARAMETERS: |
25 |
integer myThid, ndim |
26 |
character*(*) fname,gname |
27 |
character*(*) dnames(ndim) |
28 |
CEOP |
29 |
|
30 |
C !LOCAL VARIABLES: |
31 |
integer ind |
32 |
|
33 |
CALL MNC_GRID_INIT_ALL(fname, gname, ndim, dnames, ind, myThid) |
34 |
|
35 |
RETURN |
36 |
END |
37 |
|
38 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
39 |
CBOP 1 |
40 |
C !ROUTINE: MNC_GRID_INIT_ALL |
41 |
|
42 |
C !INTERFACE: |
43 |
SUBROUTINE MNC_GRID_INIT_ALL( |
44 |
I fname, |
45 |
I gname, |
46 |
I ndim, |
47 |
I dnames, |
48 |
O ind, |
49 |
I myThid ) |
50 |
|
51 |
C !DESCRIPTION: |
52 |
C Initialize a new conceptual (MNC inner layer) grid within a NetCDF |
53 |
C file context. If the requested grid name already exists, then |
54 |
C verify that it has exactly the same number of dimensions, each |
55 |
C with exactly the same size and report a fatal error if not. This |
56 |
C is a necessary check since the MNC inner layer does not support |
57 |
C grid name re--definition. |
58 |
|
59 |
C !USES: |
60 |
implicit none |
61 |
#include "MNC_COMMON.h" |
62 |
#include "EEPARAMS.h" |
63 |
#include "netcdf.inc" |
64 |
|
65 |
C !INPUT PARAMETERS: |
66 |
integer myThid, ndim, ind |
67 |
character*(*) fname,gname |
68 |
character*(*) dnames(ndim) |
69 |
CEOP |
70 |
|
71 |
C !LOCAL VARIABLES: |
72 |
integer i,j,k,ii,jj,kk, n,nf, indf,indg,indd, fid, ngrid |
73 |
integer ng_ind,lg_ind, ds_last, ndim_file, igr,ig1,ig2 |
74 |
integer ngt, ngn |
75 |
character*(MAX_LEN_MBUF) msgbuf |
76 |
character*(MNC_MAX_PATH) file_name |
77 |
|
78 |
C Functions |
79 |
integer ILNBLNK |
80 |
|
81 |
C Get the file ID and indicies |
82 |
DO i =1,MNC_MAX_PATH |
83 |
file_name(i:i) = ' ' |
84 |
ENDDO |
85 |
nf = ILNBLNK(fname) |
86 |
IF (nf .GT. MNC_MAX_PATH) nf = MNC_MAX_PATH |
87 |
file_name(1:nf) = fname(1:nf) |
88 |
CALL MNC_GET_IND(MNC_MAX_FID,file_name,mnc_f_names,indf,myThid) |
89 |
IF (indf .LT. 1) THEN |
90 |
write(msgbuf,'(3a)') 'MNC ERROR: file ''', file_name(1:nf), |
91 |
& ''' does not exist' |
92 |
CALL print_error( msgbuf, mythid ) |
93 |
stop 'ABNORMAL END: S/R MNC_GRID_INIT' |
94 |
ENDIF |
95 |
fid = mnc_f_info(indf,2) |
96 |
ngrid = mnc_f_info(indf,3) |
97 |
ng_ind = 4 + 3*ngrid |
98 |
IF (ngrid .EQ. 0) THEN |
99 |
ds_last = 0 |
100 |
ELSE |
101 |
lg_ind = 4 + 3*(ngrid - 1) |
102 |
ds_last = mnc_f_info(indf,(lg_ind+2)) |
103 |
ENDIF |
104 |
|
105 |
C Check for sufficient space in memory |
106 |
i = ds_last + ndim |
107 |
j = 3 + 3*(ngrid + 1) |
108 |
IF ((i .GE. MNC_MAX_INFO) .OR. (j .GE. MNC_MAX_INFO)) THEN |
109 |
write(msgbuf,'(2a)') 'MNC_GRID_INIT_ALL ERROR: insufficient', |
110 |
& ' space--please increase MNC_MAX_INFO' |
111 |
CALL print_error( msgbuf, mythid ) |
112 |
stop 'ABNORMAL END: S/R MNC_GRID_INIT_ALL' |
113 |
ENDIF |
114 |
|
115 |
C Enter DEFINE mode |
116 |
CALL MNC_FILE_REDEF(fname, myThid) |
117 |
|
118 |
ngn = ILNBLNK(gname) |
119 |
|
120 |
C Check for grid re-definition |
121 |
DO igr = 1,mnc_f_info(indf,3) |
122 |
ii = 4 + 3*(igr - 1) |
123 |
ngt = ILNBLNK(mnc_g_names(mnc_f_info(indf,ii))) |
124 |
IF ( (ngt .EQ. ngn) |
125 |
& .AND. (mnc_g_names(mnc_f_info(indf,ii))(1:ngt) |
126 |
& .EQ. gname(1:ngn)) ) THEN |
127 |
|
128 |
ig1 = mnc_f_info(indf,ii+1) |
129 |
ig2 = mnc_f_info(indf,ii+2) |
130 |
|
131 |
C Check if different number of dims |
132 |
IF (ndim .NE. (ig2-ig1+1)) THEN |
133 |
kk = ILNBLNK( mnc_f_names(indf) ) |
134 |
write(msgbuf,'(6a)') 'MNC ERROR: grid ''', gname(1:ngn), |
135 |
& ''' was previously defined for file ''', |
136 |
& mnc_f_names(indf)(1:kk), ''' with a different ', |
137 |
& 'number of dimensions' |
138 |
CALL print_error(msgbuf, mythid) |
139 |
stop 'ABNORMAL END: S/R MNC_GRID_INIT' |
140 |
ENDIF |
141 |
|
142 |
C Check if same number of dims but different dim names |
143 |
k = 0 |
144 |
DO jj = ig1,ig2 |
145 |
k = k + 1 |
146 |
IF (mnc_d_names(mnc_fd_ind(indf,jj)) .NE. dnames(k)) THEN |
147 |
kk = ILNBLNK( mnc_f_names(indf) ) |
148 |
write(msgbuf,'(6a)') 'MNC ERROR: grid ''', gname(1:ngn), |
149 |
& ''' was previously defined for file ''', |
150 |
& mnc_f_names(indf)(1:kk), ''' with a different ', |
151 |
& 'combination of dimensions' |
152 |
CALL print_error(msgbuf, mythid) |
153 |
stop 'ABNORMAL END: S/R MNC_GRID_INIT' |
154 |
ENDIF |
155 |
ENDDO |
156 |
|
157 |
C Reaching this point means that the grid name WAS previously |
158 |
C defined and the number and sizes of the associated dimensions |
159 |
C exactly match so everything is OK and we do not need to create |
160 |
C a new definition for this grid. |
161 |
RETURN |
162 |
|
163 |
ENDIF |
164 |
ENDDO |
165 |
|
166 |
C Reaching this point means the grid was NOT previously defined and |
167 |
C we must therefore create a new definition. |
168 |
CALL MNC_GET_NEXT_EMPTY_IND(MNC_MAX_ID, mnc_g_names, |
169 |
& 'mnc_g_names', indg, myThid) |
170 |
mnc_g_names(indg)(1:MNC_MAX_CHAR) = |
171 |
& mnc_blank_name(1:MNC_MAX_CHAR) |
172 |
n = ILNBLNK(gname) |
173 |
mnc_g_names(indg)(1:n) = gname(1:n) |
174 |
|
175 |
C Add the dimensions |
176 |
DO i = 1,ndim |
177 |
|
178 |
j = ds_last + i |
179 |
n = ILNBLNK(dnames(i)) |
180 |
|
181 |
C Search for the dimension ID within the list of dimensions |
182 |
C defined for this file |
183 |
ndim_file = mnc_f_alld(indf,1) |
184 |
indd = 0 |
185 |
DO ii = 1,ndim_file |
186 |
jj = mnc_f_alld(indf,ii+1) |
187 |
kk = ILNBLNK(mnc_d_names(jj)) |
188 |
IF ((n .EQ. kk) |
189 |
& .AND. (dnames(i)(1:n) .EQ. mnc_d_names(jj)(1:kk))) THEN |
190 |
indd = jj |
191 |
GOTO 20 |
192 |
ENDIF |
193 |
ENDDO |
194 |
20 CONTINUE |
195 |
IF (indd .LT. 1) THEN |
196 |
write(msgbuf,'(5a)') 'MNC ERROR: dimension ''', |
197 |
& dnames(i)(1:n), ''' does not exist for file ''', |
198 |
& fname(1:nf), '''' |
199 |
CALL print_error( msgbuf, mythid ) |
200 |
stop 'ABNORMAL END: S/R MNC_GRID_INIT' |
201 |
ENDIF |
202 |
|
203 |
mnc_fd_ind(indf,j) = indd |
204 |
|
205 |
ENDDO |
206 |
|
207 |
C Grid successfully added, so update file table |
208 |
mnc_f_info(indf,ng_ind) = indg |
209 |
mnc_f_info(indf,ng_ind+1) = ds_last + 1 |
210 |
mnc_f_info(indf,ng_ind+2) = ds_last + ndim |
211 |
mnc_f_info(indf,3) = ngrid + 1 |
212 |
ind = indg |
213 |
|
214 |
RETURN |
215 |
END |
216 |
|
217 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |
218 |
CBOP 1 |
219 |
C !ROUTINE: MNC_GRID_GET_DIMIND |
220 |
|
221 |
C !INTERFACE: |
222 |
SUBROUTINE MNC_GRID_GET_DIMIND( |
223 |
I indf, |
224 |
I dname, |
225 |
O ind_fg_ids, |
226 |
I myThid ) |
227 |
|
228 |
C !DESCRIPTION: |
229 |
C Get the dimension ID (index) for the named dimension. |
230 |
|
231 |
C !USES: |
232 |
implicit none |
233 |
#include "MNC_COMMON.h" |
234 |
|
235 |
C !INPUT PARAMETERS: |
236 |
integer indf, ind_fg_ids, myThid |
237 |
character*(*) dname |
238 |
CEOP |
239 |
|
240 |
C !LOCAL VARIABLES: |
241 |
integer i,j,k,l, n,n1, ngrid, ds,de |
242 |
|
243 |
C Functions |
244 |
integer ILNBLNK |
245 |
|
246 |
|
247 |
ind_fg_ids = -1 |
248 |
n = ILNBLNK(dname) |
249 |
ngrid = mnc_f_info(indf,3) |
250 |
DO i = 1,ngrid |
251 |
j = 4 + 3*(i - 1) |
252 |
ds = mnc_f_info(indf,j+1) |
253 |
de = mnc_f_info(indf,j+2) |
254 |
DO k = ds,de |
255 |
l = mnc_fd_ind(indf,k) |
256 |
n1 = ILNBLNK(mnc_d_names(l)) |
257 |
IF ((n .EQ. n1) |
258 |
& .AND. (mnc_d_names(l)(1:n1) .EQ. dname(1:n))) THEN |
259 |
ind_fg_ids = k |
260 |
RETURN |
261 |
ENDIF |
262 |
ENDDO |
263 |
ENDDO |
264 |
RETURN |
265 |
END |
266 |
|
267 |
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----| |