Merge branch 'femwind' of github.com:janmandel/wrf-fire-matlab into femwind
[wrf-fire-matlab.git] / detection / cycles.m
bloba94185e6bc8599b668846ee8ac32c208fbe75f05
1 function cycles(varargin)
2 ! date
3 base_datestr='2013-08-11 00:00:00';
4 base=datenum(base_datestr);
5 num_cycles=5;
6 spinup_time=ones(1,num_cycles);
7 cycle_length=ones(1,num_cycles);
8 cycle_start =[0,2,3,4,5,6]
9 spinup_time =[2,1,1,1,1,1]
10 times_format='yyyy-mm-dd_HH:MM:SS';
11 for i=1:num_cycles
12     t(i).forecast_time=cycle_start(i+1)+1;
13     t(i).obs_start=cycle_start(i);
14     t(i).obs_end=cycle_start(i+1)-1e-6;
15     t(i).replay_start=cycle_start(i);
16     t(i).replay_end=cycle_start(i+1);
17     t(i).run_end=cycle_start(i+1)+2;
18     t(i).perimeter_time=t(i).replay_end*24*3600;
19     forecast_times{i}=datestr(base+t(i).forecast_time,times_format);
20     print_times(i)
21 end
22 print_times_table
24 if nargin,
25     i=varargin{1};
26     force=1;
27 else
28     i=input_num('cycle number',1);
29     force=0;
30 end
31 %force = 1;
32 link_namelist_command=sprintf('rm -f namelist.input; ln -s namelist.input_%i namelist.input',i);
33 if i==0,
34     fprintf('Initial simulation from %s to at least %s\n',base_datestr,forecast_times{1})
35     fprintf('Execute %s now?\n',link_namelist_command);
36     if input_num('0/1',1,force)
37         if system(link_namelist_command),
38              error('failed')
39         end
40     end
41     disp('Run WRF-SFIRE and continue when done\n')
42     
43 else
44     print_times(i)
45     system('ls -l wrfout*')
46     wrfout_time = base+t(i).forecast_time;
47     wrfout{i}=['wrfout_d01_',datestr(wrfout_time,times_format)];
48     if ~exist(wrfout{i},'file')
49         fprintf('file %s does not exist\n',wrfout{i})
50         wrfout_time = wrfout_time - 23.5/24;  % no wrfout produced on restart => written 30 min later
51         wrfout{i}=['wrfout_d01_',datestr(wrfout_time,times_format)];
52     end
53     wrfrst{i}=['wrfrst_d01_',datestr(base+t(i).replay_start,times_format)];
54     fprintf('%s %s %s %s\n','Reading fire arrival time at',forecast_times{i},' from ',wrfout{i})
55     if t(i).replay_start==0;
56        rewrite='wrfinput_d01';
57        restart='.false.';
58     else
59        rewrite=wrfrst{i};
60        restart='.true.';
61     end
62     fprintf('%s %s\n','Will write modified time into     ',rewrite)
63     rewrite_bak=[rewrite,'.bak'];
64     q=input_num(['1 to copy ',rewrite,' to ',rewrite_bak],1,force);
65     if q,
66        if system(['cp ',rewrite,' ',rewrite_bak]),
67            warning('copy failed')
68        end
69     end
70     w=read_wrfout_tign(wrfout{i},forecast_times{i});
71     wrfout_bak=[wrfout{i},'.bak'];
72     q=input_num(['1 to move ',wrfout{i},' to ',wrfout_bak],1,force);
73     if q,
74        movefile(wrfout{i},wrfout_bak);
75     end
76     time_bounds=[t(i).obs_start,t(i).obs_end,t(i).replay_start,t(i).replay_end]+base;
77     savew=sprintf('w_%i',i);
78     fprintf('saving to %s\n',savew)
79     cycle=i;
80     save(savew,'w','cycle','time_bounds','t')
81     p=detect_fit_level2(cycle,time_bounds,[],w,force)
82     fprintf('Saving p\n');
83     pstr = sprintf('p_%i.mat',i);
84     save(pstr,'p')
85     print_times(i)
86     fprintf('perimeter_time=%10.3f\nrestart=%s\n',t(i).perimeter_time,restart)
87     q=sprintf('replace TIGN_G in %s and run\n %s\n [0/1]',rewrite,link_namelist_command);
88     if input_num(q,1,force)
89         use_analysis = input_num('Use analyis for restart? [0]',0,1)
90         if use_analysis
91             p.analysis= max(p.forecast,p.analysis);
92             p.spinup = p.analysis;
93         end
94         ncreplace(rewrite,'TIGN_G',p.spinup)
95         if system(link_namelist_command),
96              error('link failed')
97         end
98     end
99     %compute ros adjustment factor
100     %ra = ros_adjust(p.forecast,p.analysis,p.observations_end_time,w.nfuel_cat);
101     %fprintf('Recomended ROS adjust factor: %f \n',r    disp('Run WRF-SFIRE and continue when done\n')
104 function print_times(ii)
105 ptime(ii,'Forecast used     ',t(ii).forecast_time)  
106 ptime(ii,'Observations start',t(ii).obs_start)
107 ptime(ii,'Observations end  ',t(ii).obs_end)
108 ptime(ii,'Replay start      ',t(ii).replay_start)
109 ptime(ii,'Replay end        ',t(ii).replay_end)
110 ptime(ii,'Run end           ',t(ii).run_end)
111 fprintf('perimeter_time=%10.3f\n',t(ii).perimeter_time)
114 function print_times_table
115 fmt='  %g %g %g %g %g\n';
116 fprintf(['Cycle             ',fmt],1:length(t))
117 fprintf(['Forecast used     ',fmt],t(:).forecast_time)  
118 fprintf(['Observations start',fmt],t(:).obs_start)
119 fprintf(['Observations end  ',fmt],t(:).obs_end)
120 fprintf(['Replay start      ',fmt],t(:).replay_start)
121 fprintf(['Replay end        ',fmt],t(:).replay_end)
122 fprintf(['Run end           ',fmt],t(:).run_end)
125 function ptime(ii,s,t)
126         fprintf('Cycle %i %s%7.3f days %s\n',ii,s,t,datestr(t+base,'dd-mmm-yyyy HH:MM:SS'))
128         end