function add_timeinfo(FOUT,YEARREF,YEAR1,MONTH1,YEARN,MONTHN)

% set parameters 
if not(exist('FOUT'))
  FOUT='timeinfo.nc';
end 
% compute time information that is consistent with remaining HAPPI experiments 
ACCMONTH=[0 31 59 90 120 151 181 212 243 273 304 334]+...
         [0 -2 0 -1 0 -1 0 0 -1 0 -1 0];
MIDMONTH=[16 15 16 16 16 16 16 16 16 16 16 16];
DATESECS=[43200, 0, 43200, 0, 43200, 0, 43200, 43200, 0, 43200, 0, 43200]; 
if not(exist('YEARREF'))
  YEARREF=1850;
end
if not(exist('YEAR1'))
  YEAR1=2000;
end
if not(exist('MONTH1'))
  MONTH1=1;
end
if not(exist('YEARN'))
  YEARN=2000;
end
if not(exist('MONTHN'))
  MONTHN=12;
end
n=0;
datevec=[];
datevecsec=[];
timevec=[];
for YEAR=YEAR1:YEARN
  if YEAR==YEAR1
    M1=MONTH1;
  else
    M1=1;
  end
  if YEAR==YEARN
    MN=MONTHN;
  else
    MN=12;
  end
  for MONTH=M1:MN
    n=n+1;
    datevec(n)=YEAR*10000+MONTH*100+MIDMONTH(MONTH);
    datesecvec(n)=DATESECS(MONTH);
    timevec(n)=(YEAR-YEARREF)*365+ACCMONTH(MONTH)+MIDMONTH(MONTH)+DATESECS(MONTH)/86400;
  end
end

% write output to file 
try
  nccreate(FOUT,'time','datatype','double','Dimensions',{'time',inf});
end 
try
  nccreate(FOUT,'date','datatype','int32','Dimensions',{'time',inf});
end 
try
  nccreate(FOUT,'datesec','datatype','int32','Dimensions',{'time',inf});
end 

ncwrite(FOUT,'time',timevec);
ncwriteatt(FOUT,'time','units','days since 1850-01-01 00:00:00');
ncwriteatt(FOUT,'time','calendar','365_day');

ncwrite(FOUT,'date',datevec);
ncwriteatt(FOUT,'date','long_name','current date (YYYYMMDD)');

ncwrite(FOUT,'datesec',datesecvec);
ncwriteatt(FOUT,'datesec','long_name','current seconds of current date');


