2 % download http://firemapper.sc.egov.usda.gov/data_viirs/kml/conus_hist/conus_20120914.kmz
6 % run Adam's simulation, currently results in
8 % /share_home/akochans/WRF341F/wrf-fire/WRFV3/test/em_utfire_1d_med_4km_200m
10 % arrays needed only once
11 % f='wrfout_d01_2013-08-20_00:00:00';
12 % t=nc2struct(f,{'Times'},{}); n=size(t.times,2)
13 % w=nc2struct(f,{'Times','TIGN_G','FXLONG','FXLAT','UNIT_FXLAT','UNIT_FXLONG','XLONG','XLAT','NFUEL_CAT'},{},n);
16 % array at fire resolution every saved timestep
18 % a=dir('wrfout_d01*');
19 % s=read_wrfout_sel({a.name},{'FGRNHFX',Times});
22 % arrays at atm resolution every saved timestep
24 % a=dir('wrfout_d01*')
25 % s=read_wrfout_sel({a.name},{'Times','UAH','VAH'})
28 % fuels.m is created by WRF-SFIRE at the beginning of the run
30 % ****** REQUIRES Matlab 2013a - will not run in earlier versions *******
32 % run patch_load first
38 % convert tign_g to datenum
39 w.time=datenum(char(w.times)');
40 red.tign=(red.tign_g - max(red.tign_g(:)))/(24*60*60) + w.time;
41 min_tign=min(red.tign(:));
42 max_tign=max(red.tign(:));
44 red.tign(find(red.tign==max_tign))=NaN; % squash the top
48 h=surf(red.fxlong,red.fxlat,red.tign-min_tign);
49 xlabel('longitude'),ylabel('latitude'),zlabel('days')
50 set(h,'EdgeAlpha',0,'FaceAlpha',0.5); % show faces only
63 [x.xlon,x.xlat]=meshgrid(x.lon,x.lat);
64 if any(x.data(:)>6), % some data present - not all absent, water, or cloud
68 contour(red.fxlong,red.fxlat,red.tign,[x.time x.time],'-r');
69 fprintf('image time %s\n',datestr(x.time));
70 fprintf('min wind field time %s\n',datestr(ss.min_time));
71 fprintf('max wind field time %s\n',datestr(ss.max_time));
72 if x.time>=ss.min_time && x.time <= ss.max_time,
73 step=interp1(ss.time,ss.num,x.time);
83 uu=w0*ss.uh(:,:,step0)+w1*ss.uh(:,:,step1);
84 vv=w0*ss.vh(:,:,step0)+w1*ss.vh(:,:,step1);
85 fprintf('wind interpolated to %s from\n',datestr(x.time))
86 fprintf('step %i %s weight %8.3f\n',step0,datestr(ss.time(step0)),w0)
87 fprintf('step %i %s weight %8.3f\n',step1,datestr(ss.time(step1)),w1)
88 fprintf('wind interpolated to %s from\n',datestr(x.time))
89 sc=0.006;quiver(w.xlong,w.xlat,sc*uu,sc*vv,0);
96 if any(x.data(:)>7) && fig3d>0,
98 x.C2=cmap2(x.data+1,:); % translate data to RGB colormap, NaN=no detection
99 x.C2=reshape(x.C2,[size(x.data),size(cmap2,2)]);
101 h2=surf(x.xlon,x.xlat,(v.time-min_tign)*ones(size(x.data)),x.C2);
102 set(h2,'EdgeAlpha',0,'FaceAlpha',1); % show faces only
110 tim_in = tim_all(bii);
111 u_in = unique(tim_in);
112 fprintf('detection times from first ignition\n')
114 fprintf('%8.5f days %s UTC %3i %s detections\n',u_in(i)-min_tign,...
115 datestr(u_in(i)+w.time),sum(tim_in==u_in(i)),detection);
117 detection_bounds=input('enter detection bounds as [upper,lower]: ')
118 bi = bii & detection_bounds(1) + min_tign <= tim_all ...
119 & tim_all <= detection_bounds(2) + min_tign;
120 % now detection selected in time and space
127 fprintf('%i detections selected\n',sum(bi)),
128 fprintf('mean detection time %g days from ignition %s UTC\n',...
129 tim_ref-min_tign,datestr(tim_ref+w.time));
130 fprintf('days from ignition min %8.5f max %8.5f\n',min(tim)-min_tign,max(tim)-min_tign);
131 fprintf('longitude min %8.5f max %8.5f\n',min(lon),max(lon));
132 fprintf('latitude min %8.5f max %8.5f\n',min(lat),max(lat));
134 % detection selected in time and space
140 % plot satellite detection points
142 % plot3(lon,lat,tim,'ko'),
144 [m,n]=size(w.fxlong);
145 for j=1:length(fuel), W(j)=fuel(j).weight;end
146 for j=length(fuel)+1:max(c.nfuel_cat(:)),W(j)=NaN;end
149 T(i,j)=W(c.nfuel_cat(i,j));
153 tscale = 1000; % multiply fuel.weight by this to get detection time scale
154 a=input('enter [tscale dir add1p add1m add2p add2m] (1,rad,s/m,s/m)\n');
155 % magic match to u(1) and viirs seems [1000 3*pi/4 -0.0002 -0.0001 0 0.00005]
157 if tscale<=0, break, end
164 v1=[cos(dir),sin(dir)]; % main direction
165 v2=[cos(dir+pi/2),sin(dir+pi/2)]; % secondary direction
167 % find ignition point
168 [i_ign,j_ign]=find(w.tign_g == min(w.tign_g(:)));
170 % vector field (x,y) - (x_ign,y_ign)
171 VDx=(w.fxlong-w.fxlong(i_ign,j_ign))*w.unit_fxlong;
172 VDy=(w.fxlat-w.fxlat(i_ign,j_ign))*w.unit_fxlat;
174 p1 = VDx*v1(1)+VDy*v1(2); % projections on main direction
175 p2 = VDx*v2(1)+VDy*v2(2); % projections on secondary direction
176 [theta,rho]=cart2pol(p1,p2); % in polar coordinates, theta between [-pi,pi]
177 thetas=pi*[-3/2,-1,-1/2,0,1/2,1,3/2];
178 deltas=[add2p add1m add2m add1p add2p add1m add2m];
179 delta = interp1(thetas,deltas,theta,'pchip').*rho;
181 tign_mod = tign + delta;
183 % probability of detection, assuming selected times are close
184 pmap = p_map(tscale*T/(24*60*60),tign_mod - tim_ref);
186 [mm,nn]=size(w.fxlong);
189 mesh_fxlong=w.fxlong(mi,ni);
190 mesh_fxlat=w.fxlat(mi,ni);
191 mesh_fgrnhfx=s.fgrnhfx(mi,ni,:);
192 mesh_pmap = pmap(mi,ni);
193 mesh_lon = mesh_fxlong(mi,ni);
194 mesh_lat = mesh_fxlat(mi,ni);
195 mesh_tign= tign(mi,ni);
200 %contour(mesh_fxlong,mesh_fxlat,mesh_tign,[tim_ref,tim_ref]);
202 % add the detection probability map
204 h=pcolor(mesh_fxlong,mesh_fxlat,pmap);
205 set(h,'EdgeAlpha',0,'FaceAlpha',0.5);
207 cc=colormap; cc(1,:)=1; colormap(cc);
210 plot(w.fxlong(i_ign,j_ign),w.fxlat(i_ign,j_ign),'xk')
211 fprintf('first ignition at %g %g, marked by black x\n',w.fxlong(i_ign,j_ign),w.fxlat(i_ign,j_ign))
215 % plot detection squares
217 C=0.5*ones(1,length(res));
218 rlon=0.5*res/w.unit_fxlong;
219 rlat=0.5*res/w.unit_fxlat;
220 X=[lon-rlon,lon+rlon,lon+rlon,lon-rlon]';
221 Y=[lat-rlat,lat-rlat,lat+rlat,lat+rlat]';
228 % hold on, (mesh_lon,mesh_lat,mesh_tim2),grid on
229 assim_string='';if any(delta(:)),assim_string='assimilation',end
230 daspect([w.unit_fxlat,w.unit_fxlong,1]) % axis aspect ratio length to length
231 title(sprintf('Barker Canyon fire %s %s %s UTC',...
232 assim_string, detection, datestr(tim_ref+w.time)))
237 % evaluate likelihood
239 likelihood=zeros(1,ndetect);
240 mask=cell(1,ndetect);
242 mask{i}=lon(i)-rlon(i) <= w.fxlong & w.fxlong <= lon(i)+rlon(i) & ...
243 lat(i)-rlat(i) <= w.fxlat & w.fxlat <= lat(i)+rlat(i);
244 likelihood(i)=ssum(pmap(mask{i}))/ssum(mask{i});
247 total_likelihood=sum(likelihood) % should be really a product if they are independeny
248 % but the sum seems to work little better