fixing ndt boundary conditions tile and domain bounds, adding debug prints
[wrf-fire-matlab.git] / detection / camp_cycles.m
blob140e33178a9c09937d7d2c009aabfd15fb183999
1 function cycles(varargin)
2 ! date
4 % base_datestr='2013-08-11 00:00:00';
5 % for the Camp fire
6 base_datestr='2018-11-08 12:00:00';
8 %multiplier to go to different time frame(s)
9 mult = 1/3;
11 base=datenum(base_datestr);
12 num_cycles=5;
13 spinup_time=ones(1,num_cycles)*mult;
14 cycle_length=ones(1,num_cycles)*mult;
15 cycle_start =[0,2,3,4,5,6]*mult
16 spinup_time =[2,1,1,1,1,1]*mult
17 times_format='yyyy-mm-dd_HH:MM:SS';
18 for i=1:num_cycles
19     t(i).forecast_time=cycle_start(i+1)+1*mult;
20     t(i).obs_start=cycle_start(i);
21     t(i).obs_end=cycle_start(i+1)-1e-6;
22     t(i).replay_start=cycle_start(i);
23     t(i).replay_end=cycle_start(i+1);
24     t(i).run_end=cycle_start(i+1)+2*mult;
25     t(i).perimeter_time=t(i).replay_end*24*3600;
26     forecast_times{i}=datestr(base+t(i).forecast_time,times_format);
27     print_times(i)
28 end
29 print_times_table
31 if nargin,
32     i=varargin{1};
33     force=1;
34 else
35     i=input_num('cycle number',1);
36     force=0;
37 end
38 %force = 1;
39 link_namelist_command=sprintf('rm -f namelist.input; ln -s namelist.input_%i namelist.input',i);
40 if i==0,
41     fprintf('Initial simulation from %s to at least %s\n',base_datestr,forecast_times{1})
42     fprintf('Execute %s now?\n',link_namelist_command);
43     if input_num('0/1',1,force)
44         if system(link_namelist_command),
45              error('failed')
46         end
47     end
48     disp('Run WRF-SFIRE and continue when done\n')
49     
50 else
51     print_times(i)
52     system('ls -lh wrfout*')
53     wrfout_time = base+t(i).forecast_time;
54     wrfout{i}=['wrfout_d03_',datestr(wrfout_time,times_format)];
55     if ~exist(wrfout{i},'file')
56         fprintf('file %s does not exist\n',wrfout{i})
57         wrfout_time = wrfout_time - 23.5/24;  % no wrfout produced on restart => written 30 min later
58         wrfout{i}=['wrfout_d03_',datestr(wrfout_time,times_format)];
59     end
60     wrfrst{i}=['wrfrst_d03_',datestr(base+t(i).replay_start,times_format)];
61     fprintf('%s %s %s %s\n','Reading fire arrival time at',forecast_times{i},' from ',wrfout{i})
62     if t(i).replay_start==0;
63        rewrite='wrfinput_d03';
64        restart='.false.';
65     else
66        rewrite=wrfrst{i};
67        restart='.true.';
68     end
69     fprintf('%s %s\n','Will write modified time into     ',rewrite)
70     rewrite_bak=[rewrite,'.bak'];
71     q=input_num(['1 to copy ',rewrite,' to ',rewrite_bak],1,force);
72     if q,
73        if system(['cp ',rewrite,' ',rewrite_bak]),
74            warning('copy failed')
75        end
76     end
77     w=read_wrfout_tign(wrfout{i},forecast_times{i});
78     if isempty(w)
79         display('Time step not found. Taking final timestep instead')
80         w=read_wrfout_tign(wrfout{i}) % this will take final timestep
81     end
82     wrfout_bak=[wrfout{i},'.bak'];
83     q=input_num(['1 to move ',wrfout{i},' to ',wrfout_bak],1,force);
84     if q,
85        movefile(wrfout{i},wrfout_bak);
86     end
87     time_bounds=[t(i).obs_start,t(i).obs_end,t(i).replay_start,t(i).replay_end]+base;
88     savew=sprintf('w_%i',i);
89     fprintf('saving to %s\n',savew)
90     cycle=i;
91     save(savew,'w','cycle','time_bounds','t')
92     p=detect_fit_level2(cycle,time_bounds,[],w,force)
93     print_times(i)
94     fprintf('perimeter_time=%10.3f\nrestart=%s\n',t(i).perimeter_time,restart)
95     q=sprintf('replace TIGN_G in %s and run\n %s\n [0/1]',rewrite,link_namelist_command);
96     if input_num(q,1,force)
97         ncreplace(rewrite,'TIGN_G',p.spinup)
98         if system(link_namelist_command),
99              error('link failed')
100         end
101     end
102     disp('Run WRF-SFIRE and continue when done\n')
105 function print_times(ii)
106 ptime(ii,'Forecast used     ',t(ii).forecast_time)  
107 ptime(ii,'Observations start',t(ii).obs_start)
108 ptime(ii,'Observations end  ',t(ii).obs_end)
109 ptime(ii,'Replay start      ',t(ii).replay_start)
110 ptime(ii,'Replay end        ',t(ii).replay_end)
111 ptime(ii,'Run end           ',t(ii).run_end)
112 fprintf('perimeter_time=%10.3f\n',t(ii).perimeter_time)
115 function print_times_table
116 fmt='  %g %g %g %g %g\n';
117 fprintf(['Cycle             ',fmt],1:length(t))
118 fprintf(['Forecast used     ',fmt],t(:).forecast_time)  
119 fprintf(['Observations start',fmt],t(:).obs_start)
120 fprintf(['Observations end  ',fmt],t(:).obs_end)
121 fprintf(['Replay start      ',fmt],t(:).replay_start)
122 fprintf(['Replay end        ',fmt],t(:).replay_end)
123 fprintf(['Run end           ',fmt],t(:).run_end)
126 function ptime(ii,s,t)
127         fprintf('Cycle %i %s%7.3f days %s\n',ii,s,t,datestr(t+base,'dd-mmm-yyyy HH:MM:SS'))
129