PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/other/Matlab/detection/patch3.m

http://github.com/jbeezley/wrf-fire
Objective C | 249 lines | 220 code | 29 blank | 0 comment | 18 complexity | 3d54a621f924530457ea2294d7c2bf74 MD5 | raw file
Possible License(s): AGPL-1.0
  1. % to create conus.kml:
  2. % download http://firemapper.sc.egov.usda.gov/data_viirs/kml/conus_hist/conus_20120914.kmz
  3. % and gunzip
  4. %
  5. % to create w.mat:
  6. % run Adam's simulation, currently results in
  7. %
  8. % /share_home/akochans/WRF341F/wrf-fire/WRFV3/test/em_utfire_1d_med_4km_200m
  9. % then in Matlab
  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);
  14. % save ~/w.mat w
  15. %
  16. % array at fire resolution every saved timestep
  17. % to create s.mat:
  18. % a=dir('wrfout_d01*');
  19. % s=read_wrfout_sel({a.name},{'FGRNHFX',Times});
  20. % save ~/s.mat s
  21. %
  22. % arrays at atm resolution every saved timestep
  23. % to create ss.mat
  24. % a=dir('wrfout_d01*')
  25. % s=read_wrfout_sel({a.name},{'Times','UAH','VAH'})
  26. % save ss s
  27. %
  28. % fuels.m is created by WRF-SFIRE at the beginning of the run
  29. % ****** REQUIRES Matlab 2013a - will not run in earlier versions *******
  30. % run patch_load first
  31. % figures
  32. figmap=2;
  33. fig3d=0;
  34. % convert tign_g to datenum
  35. w.time=datenum(char(w.times)');
  36. red.tign=(red.tign_g - max(red.tign_g(:)))/(24*60*60) + w.time;
  37. min_tign=min(red.tign(:));
  38. max_tign=max(red.tign(:));
  39. red.tign(find(red.tign==max_tign))=NaN; % squash the top
  40. if fig3d>0,
  41. figure(fig3d); clf
  42. hold off
  43. h=surf(red.fxlong,red.fxlat,red.tign-min_tign);
  44. xlabel('longitude'),ylabel('latitude'),zlabel('days')
  45. set(h,'EdgeAlpha',0,'FaceAlpha',0.5); % show faces only
  46. drawnow
  47. end
  48. cmap=cmapmod14;
  49. cmap2=cmap;
  50. cmap2(1:7,:)=NaN;
  51. plot_all_level2=true;
  52. figure(figmap);clf
  53. iframe=1;
  54. for i=1:length(r.x),
  55. x=r.x{i};
  56. [x.xlon,x.xlat]=meshgrid(x.lon,x.lat);
  57. if any(x.data(:)>6), % some data present - not all absent, water, or cloud
  58. figure(figmap);clf
  59. showmod14(x)
  60. hold on
  61. contour(red.fxlong,red.fxlat,red.tign,[x.time x.time],'-r');
  62. fprintf('image time %s\n',datestr(x.time));
  63. fprintf('min wind field time %s\n',datestr(ss.min_time));
  64. fprintf('max wind field time %s\n',datestr(ss.max_time));
  65. if x.time>=ss.min_time && x.time <= ss.max_time,
  66. step=interp1(ss.time,ss.num,x.time);
  67. step0=floor(step);
  68. if step0 < ss.steps,
  69. step1 = step0+1;
  70. else
  71. step1=step0;
  72. step0=step1-1;
  73. end
  74. w0=step1-step;
  75. w1=step-step0;
  76. uu=w0*ss.uh(:,:,step0)+w1*ss.uh(:,:,step1);
  77. vv=w0*ss.vh(:,:,step0)+w1*ss.vh(:,:,step1);
  78. fprintf('wind interpolated to %s from\n',datestr(x.time))
  79. fprintf('step %i %s weight %8.3f\n',step0,datestr(ss.time(step0)),w0)
  80. fprintf('step %i %s weight %8.3f\n',step1,datestr(ss.time(step1)),w1)
  81. fprintf('wind interpolated to %s from\n',datestr(x.time))
  82. sc=0.006;quiver(w.xlong,w.xlat,sc*uu,sc*vv,0);
  83. end
  84. hold off
  85. axis(red.axis)
  86. drawnow
  87. M(i)=getframe(gcf);
  88. end
  89. if any(x.data(:)>7) && fig3d>0,
  90. figure(fig3d)
  91. x.C2=cmap2(x.data+1,:); % translate data to RGB colormap, NaN=no detection
  92. x.C2=reshape(x.C2,[size(x.data),size(cmap2,2)]);
  93. hold on
  94. h2=surf(x.xlon,x.xlat,(v.time-min_tign)*ones(size(x.data)),x.C2);
  95. set(h2,'EdgeAlpha',0,'FaceAlpha',1); % show faces only
  96. hold off
  97. drawnow
  98. end
  99. end
  100. return
  101. tim_in = tim_all(bii);
  102. u_in = unique(tim_in);
  103. fprintf('detection times from first ignition\n')
  104. for i=1:length(u_in)
  105. fprintf('%8.5f days %s UTC %3i %s detections\n',u_in(i)-min_tign,...
  106. datestr(u_in(i)+w.time),sum(tim_in==u_in(i)),detection);
  107. end
  108. detection_bounds=input('enter detection bounds as [upper,lower]: ')
  109. bi = bii & detection_bounds(1) + min_tign <= tim_all ...
  110. & tim_all <= detection_bounds(2) + min_tign;
  111. % now detection selected in time and space
  112. lon=v.lon(bi);
  113. lat=v.lat(bi);
  114. res=v.res(bi);
  115. tim=tim_all(bi);
  116. tim_ref = mean(tim);
  117. fprintf('%i detections selected\n',sum(bi)),
  118. fprintf('mean detection time %g days from ignition %s UTC\n',...
  119. tim_ref-min_tign,datestr(tim_ref+w.time));
  120. fprintf('days from ignition min %8.5f max %8.5f\n',min(tim)-min_tign,max(tim)-min_tign);
  121. fprintf('longitude min %8.5f max %8.5f\n',min(lon),max(lon));
  122. fprintf('latitude min %8.5f max %8.5f\n',min(lat),max(lat));
  123. % detection selected in time and space
  124. lon=v.lon(bi);
  125. lat=v.lat(bi);
  126. res=v.res(bi);
  127. tim=tim_all(bi);
  128. % plot satellite detection points
  129. % plot3(lon,lat,tim,'ko'),
  130. % m=500; n=500;
  131. [m,n]=size(w.fxlong);
  132. for j=1:length(fuel), W(j)=fuel(j).weight;end
  133. for j=length(fuel)+1:max(c.nfuel_cat(:)),W(j)=NaN;end
  134. T = zeros(m,n);
  135. for j=1:n, for i=1:m
  136. T(i,j)=W(c.nfuel_cat(i,j));
  137. end,end
  138. while 1
  139. tscale = 1000; % multiply fuel.weight by this to get detection time scale
  140. a=input('enter [tscale dir add1p add1m add2p add2m] (1,rad,s/m,s/m)\n');
  141. % magic match to u(1) and viirs seems [1000 3*pi/4 -0.0002 -0.0001 0 0.00005]
  142. tscale=a(1)
  143. if tscale<=0, break, end
  144. dir=a(2)
  145. add1p=a(3)
  146. add1m=a(4)
  147. add2p=a(5)
  148. add2m=a(6)
  149. v1=[cos(dir),sin(dir)]; % main direction
  150. v2=[cos(dir+pi/2),sin(dir+pi/2)]; % secondary direction
  151. % find ignition point
  152. [i_ign,j_ign]=find(w.tign_g == min(w.tign_g(:)));
  153. % vector field (x,y) - (x_ign,y_ign)
  154. VDx=(w.fxlong-w.fxlong(i_ign,j_ign))*w.unit_fxlong;
  155. VDy=(w.fxlat-w.fxlat(i_ign,j_ign))*w.unit_fxlat;
  156. p1 = VDx*v1(1)+VDy*v1(2); % projections on main direction
  157. p2 = VDx*v2(1)+VDy*v2(2); % projections on secondary direction
  158. [theta,rho]=cart2pol(p1,p2); % in polar coordinates, theta between [-pi,pi]
  159. thetas=pi*[-3/2,-1,-1/2,0,1/2,1,3/2];
  160. deltas=[add2p add1m add2m add1p add2p add1m add2m];
  161. delta = interp1(thetas,deltas,theta,'pchip').*rho;
  162. tign_mod = tign + delta;
  163. % probability of detection, assuming selected times are close
  164. pmap = p_map(tscale*T/(24*60*60),tign_mod - tim_ref);
  165. [mm,nn]=size(w.fxlong);
  166. mi=1:ceil(mm/m):mm;
  167. ni=1:ceil(nn/n):nn;
  168. mesh_fxlong=w.fxlong(mi,ni);
  169. mesh_fxlat=w.fxlat(mi,ni);
  170. mesh_fgrnhfx=s.fgrnhfx(mi,ni,:);
  171. mesh_pmap = pmap(mi,ni);
  172. mesh_lon = mesh_fxlong(mi,ni);
  173. mesh_lat = mesh_fxlat(mi,ni);
  174. mesh_tign= tign(mi,ni);
  175. % draw the fireline
  176. figure(1)
  177. hold off, clf
  178. %contour(mesh_fxlong,mesh_fxlat,mesh_tign,[tim_ref,tim_ref]);
  179. % add the detection probability map
  180. %hold on
  181. h=pcolor(mesh_fxlong,mesh_fxlat,pmap);
  182. set(h,'EdgeAlpha',0,'FaceAlpha',0.5);
  183. colorbar
  184. cc=colormap; cc(1,:)=1; colormap(cc);
  185. hold on
  186. plot(w.fxlong(i_ign,j_ign),w.fxlat(i_ign,j_ign),'xk')
  187. fprintf('first ignition at %g %g, marked by black x\n',w.fxlong(i_ign,j_ign),w.fxlat(i_ign,j_ign))
  188. drawnow
  189. pause(1)
  190. % plot detection squares
  191. C=0.5*ones(1,length(res));
  192. rlon=0.5*res/w.unit_fxlong;
  193. rlat=0.5*res/w.unit_fxlat;
  194. X=[lon-rlon,lon+rlon,lon+rlon,lon-rlon]';
  195. Y=[lat-rlat,lat-rlat,lat+rlat,lat+rlat]';
  196. hold on
  197. hh=fill(X,Y,C);
  198. hold off
  199. grid,drawnow
  200. % hold on, (mesh_lon,mesh_lat,mesh_tim2),grid on
  201. assim_string='';if any(delta(:)),assim_string='assimilation',end
  202. daspect([w.unit_fxlat,w.unit_fxlong,1]) % axis aspect ratio length to length
  203. title(sprintf('Barker Canyon fire %s %s %s UTC',...
  204. assim_string, detection, datestr(tim_ref+w.time)))
  205. ylabel('latitude')
  206. xlabel('longitude')
  207. drawnow
  208. % evaluate likelihood
  209. ndetect=length(res);
  210. likelihood=zeros(1,ndetect);
  211. mask=cell(1,ndetect);
  212. for i=1:ndetect
  213. mask{i}=lon(i)-rlon(i) <= w.fxlong & w.fxlong <= lon(i)+rlon(i) & ...
  214. lat(i)-rlat(i) <= w.fxlat & w.fxlat <= lat(i)+rlat(i);
  215. likelihood(i)=ssum(pmap(mask{i}))/ssum(mask{i});
  216. end
  217. likelihood,
  218. total_likelihood=sum(likelihood) % should be really a product if they are independeny
  219. % but the sum seems to work little better
  220. end