% Created a climatology based on obseravtions taken from hydrobase and
% sodb.  Climatolgy is presented on density surfaces (23.0 to 28.4 kgm in
% steps of 0.2 and 0.1) for 3 time periods: 
%   - data from 1940 to 1988: weighted to 1970
%   - data from 1988-2000: mean of 1992
%   - Argo data from 2000 to Jan 2007: mean of 2005 and corrected for pressure
%       errors (excludes oxygen)
%
% Is presented in both 2*5deg grid boxes and 1*1deg grid boxes for
% insitu-temperature (degC), salinity (psu), pressure on surface (dbar) and oxygen
% (mmol/l).
%
% Will be seasonal biases in any surfaces found in the mixed layer - 
% these have not been removed.
%
% Written by Kieran Helm (26-5-2008)
% Includes errors (29-5-2008)
clear; clc;

%% Load data 
d=[230:2:258 260:284];
[t1,t2,w_lat,w_lon]=kh_load_nddata('ns4',d,'t'); %t1=historical, t2=modern
[p1,p2,w_lat,w_lon]=kh_load_nddata('ns4',d,'p');
[s1,s2,w_lat,w_lon]=kh_load_nddata('ns4',d,'s');
[o1,o2,w_lat,w_lon]=kh_load_nddata('ns4',d,'o');
t3=kh_load_nddata2('ns_argo_tns',d,'a_dgrid'); 
s3=kh_load_nddata2('ns_argo_sns',d,'a_dgrid');
p3=kh_load_nddata2('ns_argo_pns',d,'a_dgrid');

%% Grid and save data
cs=[1 1];
lat=[90-cs(1)/2:-cs(1):-90+cs(1)/2]';
lon=[-180+cs(2)/2:cs(2):180-cs(2)/2]';
density=[d/10];

[t_1970 t_1970_err]=kh_interp_ignore_3D(t1,w_lat,w_lon,cs(1),cs); 
[t_1992 t_1992_err]=kh_interp_ignore_3D(t2,w_lat,w_lon,cs(1),cs);
[t_2005 t_2005_err]=kh_interp_ignore_3D(t3,w_lat,w_lon,cs(1),cs);
save('temperature','t_1970','t_1992','t_2005','t_1970_err','t_1992_err','t_2005_err','lat','lon','density')

[s_1970 s_1970_err]=kh_interp_ignore_3D(s1,w_lat,w_lon,cs(1),cs); 
[s_1992 s_1992_err]=kh_interp_ignore_3D(s2,w_lat,w_lon,cs(1),cs);
[s_2005 s_2005_err]=kh_interp_ignore_3D(s3,w_lat,w_lon,cs(1),cs);
save('salinity','s_1970','s_1992','s_2005','s_1970_err','s_1992_err','s_2005_err','lat','lon','density')

[p_1970 p_1970_err]=kh_interp_ignore_3D(p1,w_lat,w_lon,cs(1),cs); 
[p_1992 p_1992_err]=kh_interp_ignore_3D(p2,w_lat,w_lon,cs(1),cs);
[p_2005 p_2005_err]=kh_interp_ignore_3D(p3,w_lat,w_lon,cs(1),cs);
save('pressure','p_1970','p_1992','p_2005','p_1970_err','p_1992_err','p_2005_err','lat','lon','density')

[o_1970 o_1970_err]=kh_interp_ignore_3D(o1,w_lat,w_lon,cs(1),cs); 
[o_1992 o_1992_err]=kh_interp_ignore_3D(o2,w_lat,w_lon,cs(1),cs);
save('oxygen','o_1970','o_1992','o_1970_err','o_1992_err','lat','lon','density')

% kh_pcolor(t_1970(:,:,30),[0:15],1) % a check on the variables