function read_esrl_index (in)
%
% in is the indeces name
% e.g. amon
% loads and sorts etc amon.us.data.txt
%
% saves as amon.mat
%

filenames=dir([in '.*'])

eval(['load ' filenames.name])
% loads XXx13 array
% each row is a year labelled in amo(:,1)
% each month of that year in amo(:,2:13)

eval (['amo = ' in ';'])

year = amo(:,1);
amo_year = (year*ones(1,12))';
clear year;

amo=amo(:,2:13)';

[m,n]=size(amo);

amo = reshape(amo,m*n,1);

amo_year = reshape(amo_year, m*n,1);

amo_mos = (ones(n,1)*[1:12])';
amo_mos = reshape(amo_mos, m*n,1);

amo_day = ones(m*n,1)*15.;

amo_date = datenum(amo_year, amo_mos, amo_day);

amo = fillmiss(amo, -99.99, 10, NaN);

%
% Note that some series may have a different special value than -99.99
% e.g. sahelrain, noi, mei, espi, aao uses -999, pdo, oni, censo, attrib uses -9.9
if strcmpi (in(1:2), 'sa') || strcmpi (in(1:2), 'no') || strcmpi (in(1:2), 'me') || strcmpi (in(1:2), 'es') || strcmpi (in(1:2), 'aa')
    amo = fillmiss(amo, -999, 10, NaN);
elseif strcmpi (in(1:2), 'pd') || strcmpi (in(1:2), 'ce') || strcmpi (in(1:2), 'at') || strcmpi (in(1:2), 'sw') || strcmpi (in(1:2), 'on')
    amo = fillmiss(amo, -9.9, 1, NaN);
end

%shorten the series to get rid of NaN's
I = find(isnan(amo));
amo(I) = [];
amo_date(I) = [];
amo_year(I) = [];
amo_mos(I) = [];
amo_day(I) = [];

clear I anom ans d m n

eval([in ' = amo;'])
eval([in '_date = amo_date;'])

clear amo amo_date amo_year amo_mos amo_day

eval(['save ' in '.mat ' in ' ' in '_date'])

