NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/FEM/loadgrid.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 %LOADGRID load principle gridfiles for a given FEM domain
2 % LOADGRID loads grid files for a given FEM domain and returns
3 % a fem_grid_struct to the local workspace.  The returned
4 % structure contains atleast the minimum grid components
5 % for boundary plotting, contouring, etc.  Additionally,
6 % if land description files exist (.lnd, .lbe), the land
7 % description arrays are attached to the structure as well.
8 %
9 %  Input: gridname - name of domain to load grid files
10 %
11 % Output: LOADGRID returns a fem_grid_struct containing (atleast)
12 %         the following fields:
13 %         1)  .name   name of FEM domain
14 %         2)  .e      node connectivity array (cols 2-4 of .ele file )
15 %         3)  .x      x-horizontal node list  (col 2 of .nod file )
16 %         4)  .y      y-horizontal node list  (col 3 of .nod file )
17 %         5)  .z      bathymetry list         (col 2 of .bat) file
18 %         6)  .bnd    boundary node/pair list (.bnd file )
19 %
20 % LOADGRID with no input gridname checks to see if the global
21 % variable DOMAIN has been set.  If so, LOADGRID uses this
22 % name to load files for.  If DOMAIN does NOT exist or is
23 % empty, the input gridname must be specified.
24 %
25 % If the boundary list (.bnd) exists locally, LOADGRID
26 % attempts to load the remaining files locally.  If the .bnd
27 % file does NOT exist locally, LOADGRID searches the
28 % GRIDS and GRIDDIRS global variables for the grid data.
29 %
30 % If any of the principle grids files does not exist, LOADGRID
31 % displays the appropriate message and exits, returning a
32 % partially filled fem_grid_struct WHICH IS INVALID.  The
33 % returned structure CANNOT be passed to any subsequent
34 % OPNML/MATLAB function expecting a valid fem_grid_struct.
35 % NOTE THIS EXCEPTION: the function GRIDINFO will take any
36 % fem_grid_struct and describe the field contents.
37 %
38 % EXAMPLE CALL:
39 % >> mcbo=loadgrid('mcbo')
40 %
41 % This returns a fem_grid_struct into the local workspace
42 % named "mcbo" which contains the following minimum fields:
43 %
44 %    mcbo =
45 %        name: 'mcbo'
46 %           e: [727x3 double]
47 %           x: [444x1 double]
48 %           y: [444x1 double]
49 %           z: [444x1 double]
50 %         bnd: [161x2 double]
51 %
52 % Written  by : Brian O. Blanton
53 % Summer 1997
54 %
55
56 function fem_grid_struct=loadgrid(gridname)
57
58 % make sure this is atleast MATLAB version5.0.0
59 %
60 vers=version;
61 if vers(1)<5
62    disp('??? Error using ==>> LOADGRID ');
63    disp('This version of LOADGRID REQUIRES!! MATLAB version 5.0.0 or later.');
64    disp('Sorry, but this is terminal.');
65    return
66 end
67    
68 % DEFINE ERROR STRINGS
69 err1=str2mat('You must specify 1 output argument; call LOADGRID',...
70       'as grid_struct=loadgrid(gridname)');
71 err2=['gridname not supplied and global DOMAIN is empty'];
72
73 global DOMAIN GRIDS GRIDDIRS
74
75 if nargout ~=1
76    disp('??? Error using ==> loadgrid');
77    disp(err1);
78    return
79 end
80
81 if ~exist('gridname')
82    if isempty(DOMAIN)
83       error(err2);
84    else
85       gridname=DOMAIN;
86    end
87 else
88    slashes=findstr(gridname,'/');
89    if length(slashes)==0
90       fpath=[];
91    else
92       lastslash=slashes(length(slashes));
93       fpath=gridname(1:lastslash);
94       gridname=gridname(lastslash+1:length(gridname));
95    end
96    DOMAIN=gridname;
97 end
98
99 % Is there a dot in the gridname?
100 %
101 dots=findstr(gridname,'.');
102 if length(dots)~=0
103    lastdot=dots(length(dots));
104    dotname=gridname(1:lastdot-1);
105 end     
106    
107 % check current working directory or fpath/cwd for principle gridfiles
108 %
109 if isempty(fpath)
110    disp('Searching locally ...');
111 else
112    disp([ 'Searching ' fpath]);
113 end
114
115 bndname=[deblank(gridname) '.bnd'];
116 nodname=[deblank(gridname) '.nod'];
117 elename=[deblank(gridname) '.ele'];
118 batname=[deblank(gridname) '.bat'];
119
120 gsum=0;
121 if exist([fpath bndname])==2
122    loadfn=['load ' fpath bndname];
123    eval(loadfn);
124    if ~isempty(dots)
125       bnd=eval(dotname);
126    else
127       bnd=eval(gridname);
128    end
129    bndfound=1;
130    gsum=gsum+1;
131    disp(['Got ' bndname])
132 else
133    bndfound=0;
134    bnd=[];
135 end
136
137 if exist([fpath nodname])==2
138    loadfn=['load ' fpath nodname];
139    eval(loadfn);
140    if ~isempty(dots)
141       nodes=eval(dotname);
142    else
143       nodes=eval(gridname);
144    end
145    x=nodes(:,2);
146    y=nodes(:,3);
147    nodfound=1;
148    gsum=gsum+1;
149    disp(['Got ' nodname])
150 else   
151    nodfound=0;
152    x=[];
153    y=[];
154 end
155    
156 if exist([fpath elename])==2
157    loadfn=['load ' fpath elename];
158    eval(loadfn);
159    if ~isempty(dots)
160       ele=eval(dotname);
161    else
162       ele=eval(gridname);
163    end
164    ele=ele(:,2:4);
165    elefound=1;
166    gsum=gsum+1;
167    disp(['Got ' elename])
168 else
169    elefound=0;
170    ele=[];
171 end
172
173 if exist([fpath batname])==2
174    loadfn=['load ' fpath batname];
175    eval(loadfn);
176    if ~isempty(dots)
177       z=eval(dotname);
178    else
179       z=eval(gridname);
180    end
181    z=z(:,2);
182    batfound=1;
183    gsum=gsum+1;
184    disp(['Got ' batname])
185 else
186    batfound=0;
187    z=[];
188 end
189
190 % If all gridfiles found locally, return
191 if gsum==4
192    lnod=length(x);
193    lbat=length(z);
194    maxe=max(max(ele));
195    if lnod~=lbat,disp(['WARNING!! Lengths of node list and depth list'...
196    ' are NOT equal']),end
197    if lnod~=maxe,disp(['WARNING!! Max node number in element list does NOT'...
198    ' equal length of node list']),end
199    % Load up return structure
200    fem_grid_struct.name=gridname;
201    fem_grid_struct.e=ele;
202    fem_grid_struct.x=x;
203    fem_grid_struct.y=y;
204    fem_grid_struct.z=z;
205    fem_grid_struct.bnd=bnd;
206    %
207    % Attach areas
208    temp=el_areas(fem_grid_struct);
209    fem_grid_struct.ar=temp.ar;
210    % Check for land description files locally.
211    lndname=[deblank(gridname) '.lnd'];
212    lbename=[deblank(gridname) '.lbe'];
213    if exist([fpath lndname])==2 & exist([fpath lbename])==2
214       loadfn=['load ' fpath lndname];
215       eval(loadfn);
216       if ~isempty(dots)
217          lnd=eval(dotname);
218       else
219          lnd=eval(gridname);
220       end
221       disp(['Got ' lndname])
222       loadfn=['load ' fpath lbename];
223       eval(loadfn);
224       if ~isempty(dots)
225          lbe=eval(dotname);
226       else
227          lbe=eval(gridname);
228       end
229       disp(['Got ' lbename])
230       fem_grid_struct.lbe=lbe;
231       fem_grid_struct.lnd=lnd(:,2:3);
232    else
233       disp('No land description files found locally')
234    end   
235    return
236 elseif bndfound==0&elefound==1&gsum==3
237    disp(['   ' bndname ' not found; computing from ' elename '.'])
238    bnd=detbndy(ele);
239    % Load up return structure
240    fem_grid_struct.name=gridname;
241    fem_grid_struct.e=ele;
242    fem_grid_struct.x=x;
243    fem_grid_struct.y=y;
244    fem_grid_struct.z=z;
245    fem_grid_struct.bnd=bnd;
246    %
247    % Check for land description files locally.
248    lndname=[deblank(gridname) '.lnd'];
249    lbename=[deblank(gridname) '.lbe'];
250    if exist([fpath lndname])==2 & exist([fpath lbename])==2
251       loadfn=['load ' fpath lndname];
252       eval(loadfn);
253       if ~isempty(dots)
254          lnd=eval(dotname);
255       else
256          lnd=eval(gridname);
257       end
258       disp(['Got ' lndname])
259       loadfn=['load ' fpath lbename];
260       eval(loadfn);
261       if ~isempty(dots)
262          lbe=eval(dotname);
263       else
264          lbe=eval(gridname);
265       end
266       disp(['Got ' lbename])
267       fem_grid_struct.lbe=lbe;
268       fem_grid_struct.lnd=lnd(:,2:3);
269    else
270       disp('No land description files found locally')
271    end   
272    % Attach areas
273    temp=el_areas(fem_grid_struct);
274    fem_grid_struct.ar=temp.ar;
275    return
276 elseif gsum~=0
277    disp(' ')
278    disp('   NOT ALL FILES FOUND LOCALLY.')
279    if ~nodfound,disp(['   ' nodname ' not found locally.']),end
280    if ~elefound,disp(['   ' elename ' not found locally.']),end
281    if ~batfound,disp(['   ' batname ' not found locally.']),end
282    str=str2mat(' ','   This is a problem.  The files ',...
283                ['   ' nodname ' ' elename ' & ' batname],...
284                '   must all exist locally or all in one of',...
285                '   the following directories (as set in ',...
286                '   the global GRIDDIRS):',...
287                GRIDDIRS,...
288                ' ');
289    disp(str);
290 %   DOMAIN=[];
291    return
292 end
293
294 if isempty(fpath)
295    disp(['Gridfiles not found in ' pwd])
296 else
297    disp(['Gridfiles not found in ' fpath])
298 end
299
300 if isempty(GRIDDIRS)
301    disp('No places in GRIDDIRS to search.');
302    return
303 else
304    disp('Searching GRIDS for gridname match.');
305 end
306
307 % Check GRIDS list for gridname
308 %
309 if ~isempty(GRIDS)
310    igrid=0;
311    [m,n]=size(GRIDS);
312    for i=1:m
313       if strcmp(deblank(GRIDS(i,:)),gridname)==1
314          igrid=i;
315       end
316    end
317 end
318 if ~igrid
319    disp([gridname ' not in GRIDS list']);
320    DOMAIN=[];
321    return
322 end
323 disp('Got it.')
324
325 % gridname found in GRIDS.  Now, check GRIDDIRS for gridfiles
326 %
327 fpath=deblank(GRIDDIRS(igrid,:)); 
328      
329 bn=[fpath '/' gridname '.bnd'];
330 nn=[fpath '/' gridname '.nod'];
331 en=[fpath '/' gridname '.ele'];
332 zn=[fpath '/' gridname '.bat'];
333
334 if ~exist(nn)
335     disp([nn ' does not exist.']);
336    return
337 end
338 disp(['Got ' nodname])
339 loadfn=['load ' nn];
340 eval(loadfn);
341 if ~isempty(dots)
342    nodes=eval(dotname);
343 else
344    nodes=eval(gridname);
345 end
346 x=nodes(:,2);
347 y=nodes(:,3);
348
349 if ~exist(en)
350    disp([en ' does not exist.']);
351    return
352 end
353 disp(['Got ' elename])
354 loadfn=['load ' en];
355 eval(loadfn);
356 if ~isempty(dots)
357    ele=eval(dotname);
358 else
359    ele=eval(gridname);
360 end
361 ele=ele(:,2:4);
362
363 if ~exist(zn)
364    disp([zn ' does not exist.']);
365    return
366 end
367 disp(['Got ' batname])
368 loadfn=['load ' zn];
369 eval(loadfn);
370 if ~isempty(dots)
371    z=eval(dotname);
372 else
373    z=eval(gridname);
374 end
375 z=z(:,2);
376
377 if exist(bn)
378    disp(['Got ' bndname])
379    loadfn=['load ' bn];
380    eval(loadfn);
381    if ~isempty(dots)
382       bnd=eval(dotname);
383    else
384       bnd=eval(gridname);
385    end
386 else
387    disp([bn ' does not exist.  Computing from ' elename]);
388    bnd=detbndy(ele);
389 end
390
391 % Load up return structure
392 fem_grid_struct.name=gridname;
393 fem_grid_struct.e=ele;
394 fem_grid_struct.x=x;
395 fem_grid_struct.y=y;
396 fem_grid_struct.z=z;
397 fem_grid_struct.bnd=bnd;
398
399 %
400 % Attach areas
401 temp=el_areas(fem_grid_struct);
402 fem_grid_struct.ar=temp.ar;
403
404 % Check for land description files locally.
405 lndname=[fpath '/' gridname '.lnd'];
406 lbename=[fpath '/' gridname '.lbe'];
407 if exist(lndname)==2 & exist(lbename)==2
408    loadfn=['load ' lndname];
409    eval(loadfn);
410    if ~isempty(dots)
411       lnd=eval(dotname);
412    else
413       lnd=eval(gridname);
414    end
415    disp(['Got ' lndname])
416    loadfn=['load ' lbename];
417    eval(loadfn);
418    if ~isempty(dots)
419       lbe=eval(dotname);
420    else
421       lbe=eval(gridname);
422    end
423    disp(['Got ' lbename])
424    fem_grid_struct.lbe=lbe;
425    fem_grid_struct.lnd=lnd;
426 else
427    disp('No land description files found locally')
428 end   
429
430
431 return;
432 %
433 %        Brian O. Blanton
434 %        Department of Marine Sciences
435 %        15-1A Venable Hall
436 %        CB# 3300
437 %        University of North Carolina
438 %        Chapel Hill, NC
439 %                 27599-3300
440 %
441 %        919-962-4466
442 %        blanton@marine.unc.edu
443 %
444 %        Summer 1997
445 %
446
Note: See TracBrowser for help on using the browser.