1 |
function [alldiag]=alldiag_load(dirMat,nameMat,varargin); |
2 |
%object: load single record files off basic_diags_ecco, and assemble time records |
3 |
%inputs: dirMat is the model run sub-directory where the matlab files are (e.g. 'mat/') |
4 |
% nameMat is the mat files name (e.g. 'basic_diags_ecco_A_*.mat') |
5 |
%optional: nameDiag is the name of the diagnostic of interest |
6 |
% subListTimes is a vector to reduce listTimes to listTimes(subListTimes) |
7 |
|
8 |
if nargin>2; nameDiag=varargin{1}; else; nameDiag=''; end; |
9 |
if nargin>3; subListTimes=varargin{2}; else; subListTimes=[]; end; |
10 |
|
11 |
%model run paramters (from basic_diags_ecco_mygrid.mat) |
12 |
global myparms; |
13 |
|
14 |
%get list of files |
15 |
listFiles=dir([dirMat '/' nameMat]); |
16 |
%get time steps and sort listFiles |
17 |
listSteps=[]; for tt=1:length(listFiles); |
18 |
nn=listFiles(tt).name; ii=strfind(nn,'_'); ii=ii(end); listSteps=[listSteps;str2num(nn(ii+1:end-4))]; end; |
19 |
[listSteps,ii]=sort(listSteps); |
20 |
listFiles=listFiles(ii); |
21 |
%compute approximate times (in years -- using 365.25 as year length) |
22 |
listTimes=myparms.yearFirst(1)+listSteps*myparms.timeStep/86400/365.25; |
23 |
%initialize alldiag |
24 |
alldiag=load([dirMat listFiles(1).name]); |
25 |
listDiags=fieldnames(alldiag); |
26 |
%restrict list of diags to load |
27 |
if ~isempty(nameDiag); |
28 |
if iscell(nameDiag); listDiags=nameDiag; |
29 |
else; listDiags={nameDiag}; |
30 |
end; |
31 |
end; |
32 |
%restrict list of times |
33 |
if ~isempty(subListTimes); |
34 |
listSteps=listSteps(subListTimes); |
35 |
listTimes=listTimes(subListTimes); |
36 |
listFiles=listFiles(subListTimes); |
37 |
alldiag=load([dirMat listFiles(1).name]); |
38 |
end; |
39 |
%extend time dimension |
40 |
listNdim=ones(1,length(listDiags)); |
41 |
listTT={}; |
42 |
listDD=''; |
43 |
for ii=1:length(listDiags); |
44 |
%list diags in text string |
45 |
listDD=[listDD listDiags{ii} ''',''']; |
46 |
%get data: |
47 |
eval(['tmp1=alldiag.' listDiags{ii} ';']); |
48 |
%determine the time dimension: |
49 |
if strcmp(class(tmp1),'gcmfaces'); nDim=size(tmp1{1}); else; nDim=size(tmp1); end; |
50 |
if ~isempty(find(nDim==0)); nDim=0; |
51 |
elseif nDim(end)==1; nDim=length(nDim)-1; |
52 |
else; nDim=length(nDim); |
53 |
end; |
54 |
%store nDim for use below |
55 |
listNdim(ii)=nDim; |
56 |
tt=''; for jj=1:nDim; tt=[tt ':,']; end; |
57 |
listTT{ii}=['(' tt 'tt)']; |
58 |
%extend time dimension: |
59 |
if nDim>0; |
60 |
tmp2=ones(1,nDim+1); tmp2(end)=length(listSteps); |
61 |
tmp2=repmat(tmp1,tmp2); |
62 |
eval(['alldiag.' listDiags{ii} '=tmp2;']); |
63 |
end; |
64 |
end; |
65 |
%finalize text-list of variables to load |
66 |
listDD=['''' listDD(1:end-2)]; |
67 |
%loop and concatenate |
68 |
for tt=2:length(listSteps); |
69 |
eval(['tmpdiag=load([dirMat listFiles(tt).name],' listDD ');']); |
70 |
for ii=1:length(listDiags); |
71 |
nDim=listNdim(ii); |
72 |
if nDim>0; |
73 |
eval(['alldiag.' listDiags{ii} listTT{ii} '=tmpdiag.' listDiags{ii} ';']); |
74 |
end; |
75 |
end; |
76 |
end; |
77 |
%clean empty diags up |
78 |
for ii=1:length(listDiags); |
79 |
eval(['tmp1=isempty(alldiag.' listDiags{ii} ');']); |
80 |
if tmp1; alldiag=rmfield(alldiag,listDiags{ii}); end; |
81 |
end; |
82 |
%remove diags that were not selected (and therefore not concatenated) |
83 |
tmpDiags=fieldnames(alldiag); |
84 |
for ii=1:length(tmpDiags); |
85 |
if ~sum(sum(strcmp(tmpDiags{ii},listDiags))); alldiag=rmfield(alldiag,tmpDiags{ii}); end; |
86 |
end; |
87 |
%complement alldiag |
88 |
alldiag.listSteps=listSteps; |
89 |
alldiag.listTimes=listTimes; |
90 |
alldiag.listDiags=listDiags; |
91 |
%squeeze |
92 |
for ii=1:length(alldiag.listDiags); |
93 |
eval(['alldiag.' listDiags{ii} '=squeeze(alldiag.' listDiags{ii} ');']); |
94 |
end; |
95 |
|