%% day_input.m is a function file that requests from the user the required
%% dates of interest and returns these values.
%% 
%% Author: Phillip A. Webb
%% Last updated: December 30 2005
%%

function [year,dnos,dnof]=day_input

% year = year of interest (scalar)
% dnos = start daynumber of interest (scalar)
% dnof = final daynumber of interest (scalar)

pc = dlmread('dates.txt');
 A = exist( 'pc' ) ;
if A~=0
  year = (pc(:,1));
  dnos = (pc(:,2));
  dnof = (pc(:,3));
end
  
intype=menu('Date input format','day number','day and month');

%Day number and year
if intype==1

 %Get from the user the required input
 input1='Year:';
 input2='Initial daynumber:';
 input3='Final daynumber:';
 
 int_year=year;         %initial year
 int_dnos=dnos;            %initial initial daynumber
 int_dnof=dnof;            %initial final daynumber
 
 %Display the default values and allow the user to change them if required
 prompt={input1,input2,input3};
 dlg_title = 'Days of interest';
 num_lines = 1;
 def={int2str(int_year),int2str(int_dnos),int2str(int_dnof)};
 user_input=inputdlg(prompt,dlg_title,num_lines,def);

 year=str2double(user_input(1));            %year
 dnos=str2double(user_input(2));            %initial day number
 dnof=str2double(user_input(3));            %final day number
 
%Day, month, and year 
else 

 %Get from the user the required input
 input1='Year:';
 input2='Initial day:';
 input3='Initial month:';
 input4='Final day:';
 input5='Final month:';
 
 int_year=year;         %initial year
 int_days=1;            %initial initial day
 int_months=1;          %initial initial month
 int_dayf=1;            %initial final day
 int_monthf=1;          %initial final month
 
 %Display the default values and allow the user to change them if required
 prompt={input1,input2,input3,input4,input5};
 dlg_title = 'Days of interest';
 num_lines = 1;
 def={int2str(int_year),int2str(int_days),int2str(int_months),int2str(int_dayf),int2str(int_monthf)};
 user_input=inputdlg(prompt,dlg_title,num_lines,def);

 year=str2double(user_input(1));            %year
 days=str2double(user_input(2));            %initial day
 months=str2double(user_input(3));          %initial month
 dayf=str2double(user_input(4));            %final day 
 monthf=str2double(user_input(5));          %final month
 
 %Determine the required daynumbers
 dnos=daynum(days,months,year);             %initial day number
 dnof=daynum(dayf,monthf,year);             %final day number
 
end 
 
%If dnof is less than dnos, then reset dnof equal to dnos
if dnof<dnos
 disp('Input error - reseting dnof to dnos')
 dnof=dnos;
end

pc = fopen('dates.txt', 'w');
fprintf(pc, '% 4.f', year);
fprintf(pc, '% 3.f', dnos);
fprintf(pc, '% 3.f', dnof);
