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