function [flag,info_new]=fun_ncmergetime(fn1,fn2,fn_out,var)

flag=0;

% var='BC';

% info1=ncinfo(fn1);
% info2=ncinfo(fn2);

disp('Read fn1...');
time1=ncread(fn1,'time');
time_bnds1=ncread(fn1,'time_bnds');
nt1=numel(time1);
time_unit1=ncreadatt(fn1,'time','units');
time1_st=datenum(time_unit1(11:end));
time1_st_str=datestr(time1_st,'yyyy-mm-dd HH:MM:SS');
var1=ncread(fn1,var);

disp('Read fn2...');
time2=ncread(fn2,'time');
time_bnds2=ncread(fn2,'time_bnds');
nt2=numel(time2);
time_unit2=ncreadatt(fn2,'time','units');
time2_st=datenum(time_unit2(11:end));
time2_st_str=datestr(time2_st,'yyyy-mm-dd HH:MM:SS');
var2=ncread(fn2,var);
% time_offset=time2_st-time1_st;
time_offset = days365(time1_st_str,time2_st_str);

if time_offset<0
    error('time1 must < time2!');
end

disp('Combine data...');
time_new=nan(nt1+nt2,1);
time_bnds_new=nan(2,nt1+nt2);

time_new(1:nt1)=time1;
time_new(nt1+1:end)=time2+time_offset;

time_bnds_new(:,1:nt1)=time_bnds1;
time_bnds_new(:,nt1+1:end)=time_bnds2+time_offset;

time_unit_new=time_unit1;

var_new=cat(3,var1,var2);

clear var1 var2;

%%
disp('Write data...');
copyfile(fn1,fn_out);

ncwrite(fn_out,var,single(var_new));
ncwrite(fn_out,'time',single(time_new));
ncwrite(fn_out,'time_bnds',single(time_bnds_new));

info_new=ncinfo(fn_out);

flag=1;