-
Notifications
You must be signed in to change notification settings - Fork 19
/
netvarread.m
69 lines (54 loc) · 1.69 KB
/
netvarread.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
function varargout=netvarread(myfile)
% [varlist,allsame]=NETVARREAD(myfile)
%
% Read a netcef file and get the variable list in the file.
%
%
% INPUT:
%
% myfile The name of file you want to examine. OR
% A list of filenames. OR
% A directory containing netcdf files.
% If all have the same variables you get one list.
%
% OUTPUT:
%
% varlist The variable name list in a cell array
% allsame If you gave a cell array of files, did they all have the
% same variables?
%
%
% Last modified by charig-at-princeton.edu, 09/02/2014
defval('varlist',[]);
defval('allsame',1);
if iscell(myfile)
% We have a list of files, e.g. from ls2cell
Finfo = ncinfo(myfile{1});
filevarlist{1} = {Finfo.Variables.Name};
for i = 2:length(myfile)
Finfo = ncinfo(myfile{i});
filevarlist{i} = {Finfo.Variables.Name};
if ~isequal(filevarlist{1},filevarlist{i})
allsame=0;
end
end
if allsame
%disp(['All of your files have the same variable names in them. Only'...
%' one list returned.'])
varlist = filevarlist{1};
else
varlist = filevarlist;
end
elseif exist(myfile)==2
% It is one file, so just read it
%ischar(myfile)
Finfo = ncinfo(myfile);
varlist = {Finfo.Variables.Name};
elseif exist(myfile)==7
% It is a directory, so we run ls2cell on the contents and then netvarread
cls=ls2cell([myfile '*nc'],1); % We want only the netcdf files in there.
[varlist,allsame] = netvarread(cls);
end
% Provide output where requested
varns={varlist,allsame};
varargout=varns(1:nargout);