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

root/gliderproc/trunk/MATLAB/opnml/IO_Functions/read_fem_data.m

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

Initial import of Stark code.

Line 
1 function fem_data_struct=read_fem_data(fname)
2 %READ_FEM_DATA read a FEM output file of standard filetype.
3 %   READ_FEM_DATA is part of a suite of OPNML I/O functions 
4 %   to read specific filetypes pertaining to FEM model 
5 %   input and output.   These functions allow the user to
6 %   get these data files into MATLAB without copying the
7 %   files and removing the header info by hand.
8 %
9 %   READ_FEM_DATA reads all FEM filetypes (.s2r,.v2c,...),
10 %   as detailed in "Data File Standards for the Gulf of Maine
11 %   Project" from the Numerical Methods Laboratory at
12 %   Dartmouth College. 
13 %
14 %   READ_FEM_DATA returns a FEM_DATA_STRUCT to the local
15 %   workspace that contains all into needed by subsequent
16 %   routines.
17 %
18 %   If FNAME is omitted, READ_FEM_DATA enables a file
19 %   browser with which the user can specify the  file.
20 %
21 %   Otherwise, FNAME is the name of a standard filetype,
22 %   relative or absolute (fullpath), including the filetype
23 %   suffix.  This input is a char array so it must be enclosed
24 %   in single quotes.
25 %
26 %   See FEM_DATA_STRUCT for a description of the struct array
27 %   returned.
28 % Call as: >> data_struct=read_fem_data(fname);
29 %
30 % Written by : Brian O. Blanton
31
32 err1=['READ_FEM_DATA requires 0 or 1 argument.'];
33 err2=['Argument to READ_FEM_DATA must be a string (filename)'];
34 if ~exist('fname')
35    [fname,fpath]=uigetfile('*','Which file');
36    if fname==0,return,end
37 else
38    if ~isstr(fname),error(err2),end
39    % parse into filename and pathname
40    slash_place=findstr(fname,'/');
41    slash_place=slash_place(length(slash_place));
42    fpath=fname(1:slash_place-1);
43    fname=fname(slash_place+1:length(fname));
44 end
45
46 % Find dot delimiters in filename
47 dot_place=findstr(fname,'.');
48 % use only the last dot as the extension delimiter;
49 % this allows for gridnames with dots, like grid.new.ele, etc...
50 dot_place=dot_place(length(dot_place));
51 flength=length(fname);
52
53 % get filetype from tail of fname
54 ftype=fname(dot_place+1:flength);
55 fname2=fname(1:dot_place-1);
56
57 % make sure this is an allowed filetype
58 % The second set with trailing "e" allows for element-based
59 % data.
60 possible_types={'s2r','s2c','v2r','v2c','s3r','s3c','v3r','v3c',...
61                 's2re','s2ce','v2re','v2ce','s3re','s3ce','v3re','v3ce'};
62 if ~strcmp(ftype,possible_types)
63       error(['READ_FEM_DATA cannot read ' ftype ' filetype'])
64 end
65
66 [pfid,message]=fopen([fpath '/' fname]);
67 if pfid==-1
68    error([fpath fname,' not found. ',message]);
69 end
70
71 fem_data_struct.fname=fname2;
72 fem_data_struct.fext =ftype;
73 fem_data_struct.fpath=fpath;
74
75 % If we're this far...
76
77 %   the 2nd character of ftype is the dimension of the data
78 ndim=eval(ftype(2));
79 fem_data_struct.ndim=ndim;
80
81 %   the 1st character of ftype is the type of the data
82 type1=ftype(1);
83 if strcmp(type1,'s')
84    type1='scalar';
85 else
86    type1='vector';
87 end
88 fem_data_struct.type1=type1;
89
90 %   the 3rd character of ftype is the type real/complex
91 type2=ftype(3);
92 if strcmp(type2,'r')
93    type2='real';
94 else
95    type2='complex';
96 end
97 fem_data_struct.type2=type2;
98
99 %   if there is a 4th character, it is necessarily 'e' for 'element'
100 if length(ftype)==3
101    type3='nodal';
102 else
103    type3='elemental';
104 end
105 fem_data_struct.type3=type3;
106
107 % In all filetypes there is always a gridname
108 % and description line as lines #1 and #2 of the file.
109 gridname=fgets(pfid);
110 iding=findstr(gridname,' ');
111 gridname(iding)=[];
112 descline=fgets(pfid);
113 fem_data_struct.gname=gridname;
114 fem_data_struct.h1=descline;
115
116 % If field is complex, frequency is next
117 %
118 fem_data_struct.freq=1e-12;
119 if strcmp(type2,'complex')
120    freq=eval(fgets(pfid));
121    fem_data_struct.freq=freq;
122 end
123
124 % Depending on filetype, read remainder of file
125 if ndim==3
126    % nnv is the next entry
127    nnv=eval(fgets(pfid));
128    fem_data_struct.nnv=nnv;
129 else
130    fem_data_struct.nnv=1;
131 end
132  
133 if ndim==2
134    if strcmp(type1,'scalar')
135       if strcmp(type2,'real')
136          ncol=1;
137       else
138          ncol=2;
139       end
140    else   %  vector
141       if strcmp(type2,'real')
142          ncol=2;
143       else
144          ncol=4;
145       end
146    end
147 else
148    if strcmp(type1,'scalar')
149       if strcmp(type2,'real')
150          ncol=1;
151       else
152          ncol=2;
153       end
154    else   %  vector
155       if strcmp(type2,'real')
156          ncol=3;
157       else
158          ncol=6;
159       end
160    end
161 end
162
163 % Build format string
164 format_string=['%d '];
165 for i=1:ncol
166    format_string=[format_string '%f '];
167 end
168
169 % Add column for node counter
170 ncol=ncol+1;
171
172 % Extra column for depth in 3-D file
173 if ndim==3
174    format_string=[format_string '%f '];
175    ncol=ncol+1;
176 end
177
178 % Read block of data
179 data=fscanf(pfid,format_string,[ncol inf])';
180
181 if ndim==3
182    fem_data_struct.zgrid=data(:,2);
183    fem_data_struct.data1=data(:,3:ncol+1);
184 else
185    fem_data_struct.zgrid='none';
186    fem_data_struct.data1=data(:,2:ncol);
187 end
188
189 fclose(pfid);
190
191 %
192 %        Brian O. Blanton
193 %        Department of Marine Sciences
194 %        15-1A Venable Hall
195 %        CB# 3300
196 %        Uni. of North Carolina
197 %        Chapel Hill, NC
198 %                 27599-3300
199 %
200 %        919-962-4466
201 %        blanton@marine.unc.edu
202 %
203 %        Fall 1998
Note: See TracBrowser for help on using the browser.