
% Extract and order LMR data appropriately

pth='/d1/nsteiger/proxy-data/LMR_NCDC_database_v.0.2.0/';

% load([pth 'LMR_meta.mat'])
% load([pth 'LMR_data.mat'])

load([pth 'LMR_meta_aprmar.mat'])
load([pth 'LMR_data_aprmar.mat'])
%%
% Get names of proxy data from the meta data
% meta_names=table2cell(lmrmeta(:,1));
% meta_lat=cell2mat(table2cell(lmrmeta(:,5)));
% meta_lon=cell2mat(table2cell(lmrmeta(:,6)));
% meta_archive=table2cell(lmrmeta(:,8));
% meta_res=cell2mat(table2cell(lmrmeta(:,10)));

meta_names=table2cell(lmr_meta_aprmar(:,1));
meta_lat=cell2mat(table2cell(lmr_meta_aprmar(:,5)));
meta_lon=cell2mat(table2cell(lmr_meta_aprmar(:,6)));
meta_archive=table2cell(lmr_meta_aprmar(:,8));
meta_res=cell2mat(table2cell(lmr_meta_aprmar(:,10)));

msrmt=table2cell(lmr_meta_aprmar(:,9));
%%
% individual fix for on of the proxies missing name
%lmr_data_names(81)=meta_names(207); % use only for calendar year option

% Use metadata information to build up other proxy information. NOTE: The
% two data files loaded here are NOT in the same order, so I have to fix
% that
archive=cell(1,length(meta_names));
p_lat=zeros(length(meta_names),1);p_lon=zeros(length(meta_names),1);
for i=1:length(meta_names)
    idx=find(strcmp(meta_names,lmr_data_names(i)));
    if isempty(idx);disp(['missing id ' num2str(i)]);continue;end 
    p_lat(i)=meta_lat(idx);
    p_lon(i)=meta_lon(idx);
    archive(i)=meta_archive(idx); 
    ids(i)=idx;   
end

% BELOW IS COMMENTED OUT BECAUSE THERE ACTUALLY AREN'T ANY FALSE HIGH
% RESOLUTION PROXIES IN THE LMR DATABASE, IT JUST PICKS OUT PROXIES THAT
% DECREASE IN RESOLUTION BACK IN TIME BUT START OUT AT ANNUAL RESOLUTION
% 
% KLUDGE ALERT: remove false annual resolution proxies
% k=1;
% for j=1:length(archive)     
% X=lmr2k_data(:,j);
% dX0=sum(diff(X)==0); % number of values with no slope between them
% dN=sum(~isnan(X)); % number of non-nan values
%    if dX0/dN > 0.1
%      % allow only 10% of values to have no slope between them. Some
%      % records start out annual, but degrade later, so we can't just
%      % throw out everything that has no slope.
%      fh(k)=j;
%      k=k+1;
%    end
% end
fh=[];disp('Not attempting to remove false hi-res proxies...')


% REMOVE GREATER THAN TWO YEAR RESOLUTION
ntann=find(meta_res>2);
%resAvg(r) = mean(diff(tn));
%resMax(r) = max(diff(tn));
%resMin(r) = min(diff(tn));

% Combine both sets
na=union(fh,ntann);

% Remove the selected data
archive(na)=[];
msrmt(na)=[];
p_lat(na)=[];
p_lon(na)=[];
lmr2k_data(:,na)=[];
lmr2k_names=lmr_data_names;
lmr2k_names(na)=[];

save('lmr2k_proxydata_aprmar_v0.2.0.mat','archive','p_lat','p_lon','lmr2k_data','lmr2k_names','year','msrmt')
%save('lmr2k_proxydata_v0.2.0.mat','archive','p_lat','p_lon','lmr2k_data','lmr2k_names','year')


