forked from lesgourg/class_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_CLASS_output.m
186 lines (165 loc) · 5.43 KB
/
plot_CLASS_output.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
179
180
181
182
183
184
185
186
function plot_CLASS_output(datafiles,varargin)
% plot_CLASS_output(datafiles,selection,options)
% plot_CLASS_output(datafiles,selection)
% plot_CLASS_output(datafiles,options)
% plot_CLASS_output(datafiles)
% Thomas Tram, 12th of March 2014. thomas.tram@epfl.ch
% Small plot utility for plotting background, thermodynamics and
% perturbations files. (Compatibility with other files may be added later.)
% Examples:
%
% plot_CLASS_output('c:\class\test_background.dat')
% plots every column of data in the background file
% 'c:\class\test_background.dat' and saves the plot in myplot.eps.
%
%
% plot_CLASS_output('c:\class\test_background.dat',{'cdm','ur','crit'})
% plots every mention of either 'cdm', 'ur' or 'crit' in the column titles.
%
%
% plot_CLASS_output('c:\class\test_perturbations_k0_s.dat',{'delta'})
% plots every mention of 'delta' in the column titles. Convenient for
% plotting the density perturbations of all species.
%
%
% plot_CLASS_output({'c:\class\model1_background.dat','c:\class\model2_background.dat'},{'rho'})
% plots all mentions of rho in the files listed in the first cell array.
%
%
% plot_CLASS_output('c:\class\test_perturbations_k0_s.dat',{'delta'},options)
% options follows the usual MATLAB convention of
% ...,paramname1,paramval1, paramname2, paramval2,...
%
% Names: Values:
% ----------------------------------------------------------------------
% EpsFilename Filename for output
% xvariable 'a' or 'z' (will convert one from the other)
% xscale 'log' or 'linear'
% yscale 'log' or 'linear'
% xlim [xmin xmax]
if mod(nargin,2)==0
opt = cell2struct(varargin(3:2:end),lower(varargin(2:2:end)),2);
if ischar(varargin{1})
opt.selection = varargin(1);
else
opt.selection = varargin{1};
end
else
opt = cell2struct(varargin(2:2:end),lower(varargin(1:2:end)),2);
opt.selection = 'all';
end
%Filename for saving plot:
if isfield(opt,'epsfilename');
epsfilename = opt.epsfilename;
else
epsfilename = 'myplot';
end
%=================================================================
close all
if ischar(datafiles)
datafiles = {datafiles};
end
linestyles = {'-','--',':','-.'};
legendcell = {};
xmin = inf;
xmax = -inf;
for fileidx = 1:length(datafiles)
linestyle = linestyles{mod(fileidx-1,length(linestyles))+1};
datafile = datafiles{fileidx};
%Find column titles:
fid = fopen(datafile);
titleline = 0;
tline = fgetl(fid);
while tline(1)=='#'
titleline = titleline+1;
tline_old = tline;
tline = fgetl(fid);
end
fclose(fid);
% S=importdata(datafile);
% data = S.data;
% titlestring = S.textdata{titleline};
titlestring = tline_old;
S = importdata(datafile,' ',titleline);
data = S.data;
%remove leading #
titlestring = titlestring(2:end);
colonidx = [find(titlestring==':'),length(titlestring)+1];
for j=1:(length(colonidx)-1)
cellnames{j} = strtrim(titlestring(colonidx(j)+1:colonidx(j+1)-3));
end
%Determine independent variable:
x = data(:,1);
xlab = cellnames{1};
xscale = 'linear';
if (~isempty(strfind(cellnames{1},'z')))
xscale = 'log';
%First column is z
if (~isfield(opt,'xvariable')) || (~strcmp(opt.xvariable,'z'))
%if the field is empty or the field is not 'z' change to scale factor a
x = 1./(x+1);
xlab = 'a';
end
end
if (~isempty(strfind(cellnames{1},'a')))
xscale = 'log';
%First column is a
if (isfield(opt,'xvariable')) && (strcmp(opt.xvariable,'z'))
%Change to z
x = 1./x-1;
xlab = 'z';
end
end
if isfield(opt,'xscale')
xscale = opt.xscale;
end
if strcmp(opt.selection,'all')
indices = 2:length(cellnames);
else
tmp = [];
for i=1:length(opt.selection)
tmp2=strfind(cellnames,opt.selection{i});
for j=1:length(tmp2)
if ~isempty(tmp2{j})
tmp = [tmp,j];
end
end
end
indices = unique(tmp);
end
if isempty(indices)
thenames = '';
for i=1:length(opt.selection)
thenames = [thenames,opt.selection{i},', '];
end
error(['No indices corresponding to the name(s) {',thenames,'} were found!'])
end
xmin = min(xmin,min(x));
xmax = max(xmax,max(x));
if isfield(opt,'xlim')
%We need to restrict plotting:
xl = opt.xlim;
mask = (x>=xl(1))&(x<=xl(2));
else
mask = true(size(x));
end
%semilogy(tau,data(:,indices))
loglog(x(mask),abs(data(mask,indices)),'LineWidth',2,'LineStyle',linestyle)
hold on
legendcell = [legendcell,cellnames(indices)];
end
legend(legendcell,'Interpreter','none','Location','best')
yl = ylim;
%Fixes x label when y values go to 0 in log plot:
ylim([max(1e-100,yl(1)),yl(2)])
xlabel(xlab)
set(gca,'xscale',xscale);
if isfield(opt,'yscale')
set(gca,'yscale',opt.yscale);
end
if isfield(opt,'xlim')
xlim(opt.xlim)
else
xlim([xmin,xmax])
end
saveas(gcf,[epsfilename,'.eps'],'epsc2')