1 |
gforget |
1.1 |
function [cost]=cost_read(dir0,file0,list0); |
2 |
|
|
%object: read a cost function file such as costfunction0000 |
3 |
|
|
%input: dir0 is the model run directory name |
4 |
|
|
% file0 (optional) is the cost function file name (costfunction0000 or |
5 |
|
|
% or costfunction0001 etc ). If file0 is not specified, |
6 |
|
|
% of specified as '' then it will be automatically be set |
7 |
|
|
% by inspecting dir0 (assuming there is only one in dir0) |
8 |
|
|
% list0 (optional) is a cell array of cost term names |
9 |
|
|
%output : if list0 is NOT specified, then cost is a structure vector |
10 |
|
|
% of cost(kk).fc, cost(kk).no, cost(kk).fc |
11 |
|
|
% if list0 is specified, then cost is the vector of cost terms |
12 |
|
|
|
13 |
|
|
if isempty(who('file0')); file0=''; end; |
14 |
|
|
if isempty(who('list0')); list0=''; end; |
15 |
|
|
|
16 |
|
|
%take care of file name: |
17 |
|
|
dir0=[dir0 '/']; |
18 |
|
|
if isempty(file0); |
19 |
|
|
file0=dir([dir0 'costfunction0*']); |
20 |
|
|
if length(file0)>1&nargin>1; |
21 |
|
|
fprintf('several costfunction0??? files were found:\n'); |
22 |
|
|
{file0(:).name}' |
23 |
|
|
error('please be more specific'); |
24 |
|
|
end; |
25 |
|
|
file0=file0.name; |
26 |
|
|
end; |
27 |
|
|
file0=[dir0 file0]; |
28 |
|
|
|
29 |
|
|
%read costfunction0??? file: |
30 |
|
|
fid=fopen(file0); tmp2=''; |
31 |
|
|
while 1; |
32 |
|
|
tline = fgetl(fid); |
33 |
|
|
if ~ischar(tline), break, end |
34 |
|
|
if isempty(tmp2); tmp2=[tline ' ; ']; else; tmp2=[tmp2 ' ' tline ' ; ']; end; |
35 |
|
|
end |
36 |
|
|
fclose(fid); |
37 |
|
|
|
38 |
|
|
%get cost from file (already in text form in memory): |
39 |
|
|
cost=[]; cost2=[]; kk=0; |
40 |
|
|
tmp1=[-3 strfind(tmp2,' ; ')]; |
41 |
|
|
for ii=1:length(tmp1)-1; |
42 |
|
|
tmp3=tmp2(tmp1(ii)+4:tmp1(ii+1)); |
43 |
|
|
jj=strfind(tmp3,'='); jj=jj(end)+1; |
44 |
|
|
tmp_name=tmp3(1:jj-2); tmp_val=tmp3(jj:end-1); |
45 |
|
|
% |
46 |
|
|
tmp_val(strfind(tmp_val,'D'))='e'; tmp_val(strfind(tmp_val,'+'))=''; |
47 |
|
|
eval(['tmp_val=[ ' tmp_val ' ];']); tmp_val(tmp_val==0)=NaN; |
48 |
|
|
% |
49 |
|
|
if ~isnan(tmp_val(1)); |
50 |
|
|
kk=kk+1; |
51 |
|
|
if kk==1; |
52 |
|
|
cost.name=strtrim(tmp_name); cost.fc=tmp_val(1); cost.no=tmp_val(2); |
53 |
|
|
else; |
54 |
|
|
cost(kk).name=strtrim(tmp_name); cost(kk).fc=tmp_val(1); cost(kk).no=tmp_val(2); |
55 |
|
|
end; |
56 |
|
|
end; |
57 |
|
|
end; |
58 |
|
|
|
59 |
|
|
%output vector of cost? |
60 |
|
|
if ~isempty(list0); |
61 |
|
|
cost_bak=cost; |
62 |
|
|
nn=length(list0); |
63 |
|
|
cost=NaN*zeros(nn,1); |
64 |
|
|
list1={cost_bak(:).name}; |
65 |
|
|
for kk=1:nn; |
66 |
|
|
jj=find(strcmp(list1,list0{kk})); |
67 |
|
|
if length(jj)>1; |
68 |
|
|
error([list0{kk} ' is not specific enough']); |
69 |
|
|
elseif length(jj)==1; |
70 |
|
|
%cost(kk,:)=[cost_bak(jj).fc cost_bak(jj).no]; |
71 |
|
|
cost(kk)=cost_bak(jj).fc; |
72 |
|
|
end; |
73 |
|
|
end; |
74 |
|
|
end; |
75 |
|
|
|