1 function [ score,diff_var ] = time_score(perim_path,wrfout_path,wrfout_time_step)
2 %function [ score ] = time_score(perim_kml,wrfout)
3 % Function computes a score for a fire simulation based on fire arrival
4 % times of the model compared with satellite observations
5 % needs the kml2struct function to work
7 % perim - string, path to kml file with perim(s) or to a directory
8 % with kmls files or shape file, shape files should be downloaded
9 % from geomac as zip, then put into a common directory
10 % wrfout - string, path to a wrfout file for a simulation
11 % wrfout_time_step - optional, string with the timestep to be read
13 % score - average of the differences in fire arrival times for each
14 % pixel in the perimeter
16 %%%%% read the wrfout file and create interpolant
18 ts = wrfout_time_step;
19 fprintf('Will read wrfout at %s \n',ts)
20 w = read_wrfout_tign(wrfout_path,ts);
22 w = read_wrfout_tign(wrfout_path);
25 %%% compute score using data likelihood
26 like_score = input_num('Use likelihood approach? No = [0] Yes = [1]',0,1);
29 red = subset_domain(w,1);
30 %% set cone_mask < to use only the cone, no flat part at top
31 cone_mask = red.tign_g <= max(red.tign_g(:));
32 flat_top = red.tign_g == max(red.tign_g(:));
33 %Fw = scatteredInterpolant(w.fxlong(:),w.fxlat(:),w.tign_g(:));
34 Fr = scatteredInterpolant(red.fxlong(cone_mask),red.fxlat(cone_mask),red.tign(cone_mask));
36 %%%% testing the interpolation object
37 %zq = Fr(red.fxlong,red.fxlat);
38 %figure,mesh(red.fxlong,red.fxlat,red.tign_g),title('org cone')
39 %figure,mesh(red.fxlong,red.fxlat,zq),title('interpolated new cone')
41 %%% reading the perimeters
42 if perim_path(end) == 'l'
43 fprintf('Using a kml file for perimeters \n')
44 %%%%%% convert kml file to a struct
45 temp_struct = kml2struct(perim_path);
48 elseif perim_path(end) == '/'
50 perim_dat = ['kml';'shp'];
51 p_type = input_num('Type of perimeter file to use? (1) kml (2) shp', 2,1)
52 fprintf('Reading %s files in directory %s \n',perim_dat(p_type,:),perim_path)
54 d = dir([perim_path,'*.kml'])
56 p_struct = shape2struct(perim_path)
57 temp_struct = p_struct;
61 end % if perim_path...
65 %%%%%% find the perimeters in the struct, make new struct, find times of
71 for i = 1:length(temp_struct)
72 % find the perimters, which have the tag 'Polygon'
73 if strcmp(temp_struct(i).Geometry,'Polygon')
74 perim_count = perim_count + 1;
76 perim_idx = [perim_idx,i];
78 if perim_path(end) == 'l'
79 time_str = temp_struct(i).Name(end-13:end);
80 perim_date(perim_count) = datetime(time_str,'InputFormat','MM-dd-yyyy HHmm');
82 time_str = temp_struct(i).Name(end-12:end);
83 perim_date(perim_count) = datetime(time_str,'InputFormat','yyyyMMdd HHmm');
86 %create new stuct with only perims
87 perim_struct(perim_count).Lon = temp_struct(i).Lon;
88 perim_struct(perim_count).Lat = temp_struct(i).Lat;
89 perim_struct(perim_count).Name = temp_struct(i).Name;
90 perim_struct(perim_count).time = datenum(perim_date(perim_count));
91 %perim_time = datenum(perim_date);
92 %perim_struct(perim_count) = temp_struct(i);
93 %perim_struct(perim_count).time = 0;
94 % look for nan/inf in data last entry seems to always be a nan....
95 %inf_lat = ~isfinite(temp_struct(i).Lat);
102 perim_time = datenum(perim_date);
103 fprintf('There were %d perimetrs in the kml file \n',perim_count)
104 [sorted_times,sort_idx] = sort(perim_date);
106 %% compute scores and plot the data
108 perim_scores = zeros(perim_count,1);
109 perim_vars = zeros(perim_count,1);
110 figure(perim_count+1)
111 if size(red.fxlong) < 500
112 mesh(red.fxlong,red.fxlat,(red.tign-red.start_datenum))
114 mesh(red.fxlong(1:10:end,1:10:end),red.fxlat(1:10:end,1:10:end),(red.tign(1:10:end,1:10:end)-red.start_datenum))
117 title('Forecast and Perimeters');
118 xlabel('Lon'),ylabel('Lat'),zlabel('Simulation Time [days]')
119 for j = 1:perim_count
121 %p_lon = temp_struct(perim_idx(i)).Lon(1:end-1);
122 p_lon = perim_struct(i).Lon(1:end-1);
123 %p_lat = temp_struct(perim_idx(i)).Lat(1:end-1);
124 p_lat = perim_struct(i).Lat(1:end-1);
126 % only evaluate perims before the end of the simulation
127 % scatter plot the perim data
128 if perim_struct(i).time <= red.end_datenum - 0.2
129 %% ??? perim_time(i)-max(red.max_tign)
130 z = perim_struct(i).time*ones(size(p_lon));
131 z_interp = Fr(p_lon,p_lat);
132 %%% time difference is perimter time minus forecast time
133 %%% thus early forecast time gives a positive number and
134 %%% data likelihood would be higher
135 diff = (z-z_interp)*24;
136 diff = diff(~isnan(diff));
137 % test only fire arrival times before simulation end
140 cone = diff < max(diff)-0.1;
146 %make or load spline for data likelihood
147 if ~exist('p_spline','var')
148 if exist('p_spline.mat','file')
149 fprintf('Loading spline from mat file \n')
152 fprintf('Making and saving spline \n')
153 [p_spline,~,~]= make_spline(72,400);
154 save p_spline.mat p_spline
157 fprintf('Using spline in workspace \n')
159 end %like_score adjustments
162 %%%% output results %%%%
164 rel_error = abs(diff(~isnan(diff)))./z(~isnan(diff));
165 perim_scores(i) = mean(abs(diff));
166 %perim_scores(i) = mean(diff);
167 perim_vars(i) = var(diff);
168 title_spec = sprintf('Histogram of errors %s',perim_struct(i).Name);
169 figure(i),histogram(diff),title(title_spec)
170 xlabel('Forecast difference from IR perimeter [hours]')
171 ylabel('Number of perimeter points')
172 figure(perim_count+1),hold on
173 %scatter3(p_lon,p_lat,z_interp,'*')
174 scatter3(p_lon,p_lat,(z-red.start_datenum),'.')
176 fprintf('%s : Score %f \n', perim_struct(i).Name, perim_scores(i) );
177 fprintf(' mean %f var %f \n',mean(diff),var(diff));
179 else %%% using likelihood approach
181 %stretch=[0.5,10,5,10];
182 %dw = ones(size(diff));2
183 %likes = like2(dw,diff,stretch);
184 likes = p_spline(diff);
185 %%exp(likes) give "probabilty"
187 perim_scores(i) = mean(likes);
188 perim_vars(i) = var(likes);
189 title_spec = sprintf('Histogram of likelihoods %s',perim_struct(i).Name);
190 figure(i),histogram(likes),title(title_spec)
191 xlabel('Perimter point likelihood')
192 ylabel('Number of perimeter points')
193 figure(perim_count+1),hold on
194 %scatter3(p_lon,p_lat,z_interp,'*')
195 scatter3(p_lon,p_lat,(z-red.start_datenum),'.')
197 fprintf('%s : Score %f \n', perim_struct(i).Name, perim_scores(i) );
198 fprintf(' mean %f var %f \n',mean(likes),var(likes));
203 fprintf('%s perimter after simulation end \n',perim_struct(i).Name);
210 %% use all perims foir the final score ???
212 % score_mask = zeros(length(perim_struct),1);
213 % for k = 1:length(perim_struct)
214 % query_string = sprintf('Use %s in score?',perim_struct(k).Name);
215 % score_mask(k) = input_num(query_string,1);
217 % score_mask = logical(score_mask);
219 score_mask = logical([1 1 1 0 0]);
220 score = perim_scores(score_mask);
221 diff_var = perim_vars(score_mask);
222 score = mean(perim_scores(perim_scores ~= 0));