function out = firstreportedvalue(inpath,freq) % out = firstreportedvalue(inpath,freq) % inputs: % - inpath : string with full path to .csv file created by realtimeobservationdatamaker.pl; can just be name if % this is run from directory where .csv files live (preferred usage) % - freq : frequency vector of year subunits -- required to assure accuracy upto day % outputs: % - out : first-reported-value data structure, with fields .x, .t, and .tfr (time of first report) % % This program assumes that the first vintage date of all is listed for the first observation: this is typically done in % ALFRED. % % Author: Seth Pruitt, 2/26/08 % sjpruitt@ucsd.edu if nargin ~= 2 error('firstreportedvalue requires two args') end mtchr = '(\w+)[\.]'; mtchd = regexp(inpath,mtchr,'tokens'); rootname = char(mtchd{1,1}); data = importdata(inpath); out.t = unique(data.data(:,1:3),'rows'); for k = (1:length(out.t)) tv = out.t(k,:); allo = find(ismember(data.data(:,1:3),tv,'rows')); sngl = utl_sngl_tm_vctr( data.data(allo,5:7), freq ); [mn,mni]=min(sngl); out.x(k,1) = data.data(allo(mni),4); out.tfr(k,:) = data.data(allo(mni),5:7); end %% Save step dessave = []; while isempty(dessave) disp(' ') disp('> Do you want to save the output structure firstreportedvalue created?') disp(' - answer "y" for SAVE or "n" for NO SAVE (repl double quotes with single)') dessave = input(' ---> '); if ~isstr(dessave) disp(' ');disp(' !! YOUR INPUT IS NOT A STRING (bracketed by single quotes)');disp(' '); dessave = []; elseif max(dessave ~= 'y') & max(dessave ~= 'n') disp(' ');disp(' !! YOUR INPUT MUST be "y" or "n"');disp(' '); dessave =[]; end end switch dessave case 'n'; disp(' '); disp('* You have chosen to NOT SAVE the output structure') case 'y'; disp(' '); desfilename=[]; while isempty(desfilename) disp('* You have chosen to SAVE the output structure') disp('> You can specify the filename with a whole path from the root (e.g. "c:/data/thisfile"),') disp([' a path from the Matlab current directory (which is "',cd,'") (e.g. "somedir/thisfile"),']) disp(' or just the filename (e.g. "thisfile")') disp(' Or you can input the word "default" for the default naming (preferred option)') disp(' (in all of these, replace the examples double quotes with single') desfilename = input(' ---> '); if ~isstr(desfilename) disp(' ');disp(' !! YOUR INPUT IS NOT A STRING (bracketed by single quotes)');disp(' '); desfilename = []; end end if isempty(strmatch(desfilename,'default')) save(desfilename,'out'); disp(' '); else disp(' '); outputfile = [rootname,'_firstreportedvalue']; save(outputfile,'out'); end otherwise error('Something unexpected has happened at the save step'); end