Merge commit 'b453692af7dadf1db40358c02cfa86d182db5d2'
[wrf-fire-matlab.git] / cycling / new_cycles.m
blob5f9bc8872fea1a4cfb7b97d5073a7215dc3b3e1d
1 function new_cycles(f)
2 % runs new cycling routine as much a spossible, run from within direcory
3 % with wrfout file and restart file. Requires fuels.m file
4 %input:
5 %    f - string with path to wrfout file
6 %output:
7 %   written into restart files
9 cycle = input_num('Which cycle? ',1);
11 if strcmp('t',f(end)) %read saved .mat file
12     w = read_wrfout_tign(f)
13 else %read wrfoutfile directly
14     ts = choose_time_step(f);
15     w = read_wrfout_tign(f,ts);
16 end
18 gs = input_num('What grid spacing?',250)
19 %make the path structure
20 ps1 = cluster_paths(w,1,gs);
21 savestr = sprintf('ps_%d.mat',cycle);
22 save(savestr,'ps1')
23 %new points interpolated along the paths
24 ps = interp_paths(ps1,0.9);
25 %ps = ps1;
27 %make analysis fire arrival time
28 tn = squish4(ps,1,1);
29 %avg ROS in the forecast and data estimate, adjr0 is experimental
30 %adjustment factor for adjr0 in namelist.fire
31 [r1,r2,adjr0,outer] = cone_compare(ps,tn);
32 fprintf('Forecast ROS: %f \n Data ROS: %f \n',r1,r2)
33 %compare forecast fire area with analysis fire area
34 area_diff = outer.a1-outer.a2;
35 ros_diff = r1-r2;
36 %load fuel information from the wrfout
37 fuels;
38 %figure,plot(fuel(2).fmc_g,fuel(2).ros_fmc_g);
39 %find most common fuel type in burned area
40 w.nfuel_cat(w.nfuel_cat==14) = NaN;
41 msk = w.tign_g<max(w.tign_g(:));
42 common_fuel = mode(w.nfuel_cat(msk));
43 %find fmc as a function of ROS
44 fitter = fit(fuel(common_fuel).ros_fmc_g',fuel(common_fuel).fmc_g','cubicspline');
45 %how much to adjust the fmc by
46 %need to adjust for slope of terrain, right now just use 1/2 of the
47 %difference
48 fmc_adjust = 1/2*(fitter(r2)-fitter(r1));
49 %close all
50 fprintf('Adjusting fuels by %f percent\n',fmc_adjust);
51 %adjiust fuel gloabally for starters
53 % put analysis fire arrival time struct
54 new_w = insert_analysis(w,ps,tn);
56 if cycle == 1
57     %restart with wrfinput file
58     wi = 'wrfinput_d0';
59     domain = input_num('Which wrfinput_d0x file to write FMC into? x = 1 ',1)
60     rst = [wi,num2str(domain)];
61     rst_bak = [rst,'.bak'];
62     cpy_str = sprintf('cp %s %s',rst,rst_bak);
64 else
65     %restart with wrfrst file
66     d = dir('wrfrst*');
67     for i = 1:length(d)
68         fprintf('%d :  %s\n',i,d(i).name)
69     end
70     r = input_num('Which restart file to use?',1)
71     rst = d(r).name;
72     rst_bak = [rst,'.bak'];
73     cpy_str = sprintf('cp %s %s',rst,rst_bak);
74 end
75 %copy the file used for the restart
76 system(cpy_str)
77 %restart from the analysis
78 ncreplace(rst,'TIGN_G',new_w.analysis);
79 %only change fmc content if a bigger fire has a bigger ROS
80 if ros_diff*area_diff>0
81    fmc_change(fmc_adjust,rst);
82 end
83 fprintf('Linking new namelist.input file\n')
84 link_namelist(cycle);
85 fprintf('All done. Copy files to directories and restart WRF-SFIRE\n');
87 end % function