-
Notifications
You must be signed in to change notification settings - Fork 1
/
getfile.m
178 lines (155 loc) · 3.36 KB
/
getfile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function f=getfile(fin,f)
%
% try to find data files given parts of directory file name
% fin='file*' part of a data file or directory
%
if nargin < 1, fin = pwd; end
if length(fin)<1, fin=pwd; end
disp([' GETFILE: search near file: ',fin])
fdo=0;
fup=0;
% directory up to this point
id=find(fin=='/' | fin=='\');
if length(id)>0
fin1=fin(1:id(end));
else
fin1='';
end
% look deeper
a=dir([fin,'*']);
ia=1:size(a,1);
if length(ia)<1
ii=id(end):length(fin);
fin(ii)=lower(fin(ii));
a=dir([fin,'*']);
ia=1:size(a,1);
disp([' try lower case ',fin])
end
if length(ia)<1
ii=id(end):length(fin);
fin(ii)=upper(fin(ii));
disp([' try upper case ',fin])
a=dir([fin,'*']);
ia=1:size(a,1);
end
if length(ia)<1
f.ladcpdo='';
f.ladcpup='';
disp(' no ADCP files found ')
return
end
% check if directories are listed
if getfield(a,{1},'isdir')==1
disp(' GETFILE: found directory/ies ')
% parse out up/down looking directory
ii=ia;
for j=1:length(ii)
i=ii(j);
fin2=getfield(a,{i},'name');
if length(fin2)>1
if fin2(end)=='d' | fin2(end)=='D' | fin2(1:2)=='dn' | fin2(1:2)=='DN'
f.ladcpdo=[fin1,fin2,filesep];
fdo=i; ia(j)=NaN;
elseif fin2(end)=='u' | fin2(end)=='U' | fin2(1:2)=='up' | fin2(1:2)=='UP'
f.ladcpup=[fin1,fin2,filesep];
fup=i; ia(j)=NaN;
end
end
end
ii=ia(isfinite(ia));
for j=1:length(ii)
i=ii(j);
fin2=getfield(a,{i},'name');
if fup==0
if fin2(end)=='s' | fin2(end)=='S'
f.ladcpup=[fin1,fin2,filesep];
fup=i; ia(j)=NaN;
end
end
end
ia=ia(isfinite(ia));
if fdo==0 & ~isempty(ia)
fin2=getfield(a,{ia(1)},'name');
f.ladcpdo=[fin1,fin2,filesep];
fdo=99;
end
else
% we are in directory
% try to find down and up files
f.ladcpdo=largefile(fin,'*d');
if length(f.ladcpdo)<2
f.ladcpdo=largefile(fin,'*D');
end
if length(f.ladcpdo)<2
f.ladcpdo=largefile(fin,'*d0');
end
if length(f.ladcpdo)<2
f.ladcpdo=largefile(fin,'*D0');
end
f.ladcpup=largefile(fin,'*u');
if length(f.ladcpup)<2
f.ladcpup=largefile(fin,'*U');
end
if length(f.ladcpup)<2
f.ladcpup=largefile(fin,'*u0');
end
if length(f.ladcpup)<2
f.ladcpup=largefile(fin,'*U0');
end
if length(f.ladcpdo)<2
f.ladcpdo=largefile(fin);
end
return
end
if fdo>0
f.ladcpdo=largefile(f.ladcpdo);
end
if fup>0
f.ladcpup=largefile(f.ladcpup);
end
%------------------------------------------------------------------------------
function fout=largefile(fin,fin0)
% search for largest file in directory
if nargin<2, fin0=''; end
fout=' ';
% directory up to this point
ii=find(fin=='/' | fin=='\');
if length(ii)>0
fin1=fin(1:ii(end));
else
fin1='';
end
a=dir([fin,fin0,'*']);
ia=1:size(a,1);
% loop to get file size
if length(ia)>1
ib = [];
for i=ia
fsize(i)=getfield(a,{i},'bytes');
if fsize(i) > 10
if isbb(fopen([fin1, getfield(a,{i},'name')]));
ib = [ib i];
end
end
end
if ~isempty(ib)
[dum,ii]=max(fsize(ib));
fout=[fin1,getfield(a,{ib(ii)},'name')];
end
elseif length(ia)==1
fout=[fin1,getfield(a,{ia},'name')];
end
%------------------------------------------------------------------------------
function i = isbb(fid)
%ISBB True if broad-band ADCP.
% check header and data source identification bytes
hid = 127;
sid = 127;
id = fread(fid,2,'uint8');
if length(id)<2
err('ISBB: ****** can not read file id *****')
else
i = id(1) == hid & id(2) == sid;
end
% rewind file
fseek(fid,0,'bof');