1 |
function err=writegrid(fem_grid_struct,fnamebase); |
---|
2 |
%WRITEGRID write a fem_grid_struct to separate gridfiles (.ele, .nod,...) |
---|
3 |
% WRITEGRID writes the standard gridfiles .ele, .bat, .and nod to |
---|
4 |
% disk. It also writes the neighbor list to a file with extension |
---|
5 |
% '.icm' if the .icm field of fem_grid_struct is filled. |
---|
6 |
% |
---|
7 |
% WRITEGRID(FEM_GRID_STRUCT) prompts the user for a gridname, and |
---|
8 |
% writes the files <gridname>.ele, <gridname>.bat, and <gridname>.nod |
---|
9 |
% (and possibly <gridname>.icm). |
---|
10 |
% |
---|
11 |
% WRITEGRID(FEM_GRID_STRUCT,FNAMEBASE) writes the files |
---|
12 |
% <fnamebase>.ele, <fnamebase>.bat, and <fnamebase>.nod |
---|
13 |
% (and possibly <gridname>.icm). |
---|
14 |
% |
---|
15 |
% WRITEGRID returns a 0 if successful and a -1 if not. |
---|
16 |
% |
---|
17 |
|
---|
18 |
if nargin==0 & nargout==0 |
---|
19 |
disp('Call as: writegrid(fem_grid_struct,fnamebase)') |
---|
20 |
return |
---|
21 |
end |
---|
22 |
|
---|
23 |
|
---|
24 |
if nargin~=1 & nargin~=2 |
---|
25 |
error('WRITEGRID needs 1 or 2 input arguments.') |
---|
26 |
else |
---|
27 |
if nargin==1 |
---|
28 |
fnamebase=input('Enter a domain name:','s'); |
---|
29 |
else |
---|
30 |
% make sure input fnamebase is a string |
---|
31 |
if ~isa(fnamebase,'char') |
---|
32 |
disp('FNAMEBASE to WRITEGRID must be a string.'); |
---|
33 |
err=-1; |
---|
34 |
return |
---|
35 |
end |
---|
36 |
end |
---|
37 |
end |
---|
38 |
|
---|
39 |
% Is fem_grid_struct valid? |
---|
40 |
if ~is_valid_struct(fem_grid_struct) |
---|
41 |
error('fem_grid_struct to WRITEGRID not valid.') |
---|
42 |
end |
---|
43 |
|
---|
44 |
% Call write functions |
---|
45 |
disp(['Writing element file ' fnamebase '.ele.']) |
---|
46 |
if write_ele(fem_grid_struct.e,[fnamebase '.ele'])==-1 |
---|
47 |
disp(['Error writing element file ' fnamebase '.ele.']) |
---|
48 |
err=-1; |
---|
49 |
return |
---|
50 |
end |
---|
51 |
|
---|
52 |
disp(['Writing node file ' fnamebase '.nod.']) |
---|
53 |
if write_nod(fem_grid_struct.x,fem_grid_struct.y,[fnamebase '.nod'])==-1 |
---|
54 |
disp(['Error writing node file ' fnamebase '.nod.']) |
---|
55 |
err=-1; |
---|
56 |
return |
---|
57 |
end |
---|
58 |
|
---|
59 |
disp(['Writing depth file ' fnamebase '.bat.']) |
---|
60 |
if write_bat(fem_grid_struct.z,[fnamebase '.bat'])==-1 |
---|
61 |
disp(['Error writing depth file ' fnamebase '.bat.']) |
---|
62 |
err=-1; |
---|
63 |
return |
---|
64 |
end |
---|
65 |
|
---|
66 |
% if there is a neighbor list (.icm) attached to fem_grid_struct, |
---|
67 |
% write it to <fnamebase>.icm |
---|
68 |
if isfield(fem_grid_struct,'icm') |
---|
69 |
disp(['Writing neighbor list file ' fnamebase '.icm.']) |
---|
70 |
[m,n]=size(fem_grid_struct.icm); |
---|
71 |
fmtstr=[]; |
---|
72 |
mm=max(fem_grid_struct.icm(:)); |
---|
73 |
mm=length(int2str(mm*10)); |
---|
74 |
mm=int2str(mm); |
---|
75 |
fmtstp=['%' mm 'd']; |
---|
76 |
for i=1:n |
---|
77 |
fmtstr=[fmtstr fmtstp]; |
---|
78 |
end |
---|
79 |
fmtstr=[fmtstr '\n']; |
---|
80 |
fname=[fnamebase '.icm']; |
---|
81 |
[fid,mesg]=fopen(fname,'w'); |
---|
82 |
% Check for open error |
---|
83 |
if(fid<0) |
---|
84 |
errstr=sprintf('Filename %s could not be opened;\n%s\n',fname,mesg); |
---|
85 |
disp(errstr); |
---|
86 |
err=-1; |
---|
87 |
return |
---|
88 |
else |
---|
89 |
fprintf(fid,fmtstr,fem_grid_struct.icm'); |
---|
90 |
fclose(fid); |
---|
91 |
end |
---|
92 |
end |
---|
93 |
|
---|
94 |
% Successful |
---|
95 |
err=0; |
---|
96 |
|
---|
97 |
return |
---|
98 |
|
---|
99 |
% |
---|
100 |
% Brian O. Blanton |
---|
101 |
% Department of Marine Sciences |
---|
102 |
% Ocean Processes Numerical Modeling Laboratory |
---|
103 |
% 12-7 Venable Hall |
---|
104 |
% CB# 3300 |
---|
105 |
% University of North Carolina |
---|
106 |
% Chapel Hill, NC |
---|
107 |
% 27599-3300 |
---|
108 |
% |
---|
109 |
% 919-962-4466 |
---|
110 |
% blanton@marine.unc.edu |
---|
111 |
% |
---|
112 |
% Summer 1998 |
---|
113 |
% |
---|
114 |
|
---|