%Matlab/Octave sript to demonstrate reading the Elder Creek precipitation time series

data_dir = '/users/nir/Documents/data/angelo/';

file = [data_dir 'AngeloReserveData_1990-01-01to2006-11-17.tab'];

%read the columns of the data file
[a b c d e f g h i j] = textread(file, '%s %s %s %s %d %d %d %d %d %d %*');

%extract dates and times
date_string = char(a(2:end));
year_a = str2num(date_string(:, 1:4));
month_a = str2num(date_string(:, 6:7));
day_a = str2num(date_string(:, 9:10));
time_string = char(b(2:end));
hour_a = str2num(time_string(:, 1:2));

hour_seq_a = 24*datenum(year_a, month_a, day_a) + hour_a; %assigns each hour a unique integer (roughly, a count of hours starting at 1 CE)

%extract hourly precipitation (mm)
ppt_a = j(2:end);

%set negative values to NaN; it may also be necessary to filter out other questionable values 
ppt_a(find(ppt_a < 0)) = NaN;

clear a b c d e f g h i j date_string time_string



%create a vector of precipitation each hour over the time range, with missing hours assigned NaN values
start_hour = min(hour_seq_a);
end_hour = max(hour_seq_a);
n_hours = end_hour - start_hour + 1;
ppt_hourly = NaN * zeros(n_hours, 1);

ppt_hourly(hour_seq_a - start_hour + 1) = ppt_a;

