reload =1;

gliderdir = '/Users/donglai/RO/EDDIES/glider/';

% load glider data
if exist([gliderdir 'ru02_eddies.mat'],'file') ~= 0 & reload ~= 1
    gpath = [gliderdir 'ru02_eddies.mat'];
    load(gpath);
    'gliderdata loaded.'
else
    gliderdatadir = [gliderdir '/data/'];
    gliderctdfiles = dir([gliderdatadir '*ctd.dat']);
    glidersamfiles = dir([gliderdatadir '*sam.dat']);
    nctdfiles = length(gliderctdfiles);
    nsamfiles = length(glidersamfiles);
    
    % loading CTD data
    gliderdata = [];
    for ii = 1:nctdfiles,
        gliderdatain = textread([gliderdatadir gliderctdfiles(ii).name],'','commentstyle','matlab');
        gliderdata = [gliderdata;gliderdatain];
    end %for
    
    % loading SAM data
    samdata = [];
    for ii = 1:nsamfiles,
        samin = textread([gliderdatadir glidersamfiles(ii).name],'','commentstyle','matlab','headerlines',8);
        samdata = [samdata;samin];
    end %for
    
    ets = samdata(:,1);
    gts = ets/3600/34 + datenum(1970,1,1);
    sglat = samdata(:,2);
    sglon = samdata(:,3);
    sdepth = -samdata(:,4);
    bb532 = samdata(:,5);
    c532 = samdata(:,6);
    c650 = samdata(:,7);
    
    et = gliderdata(:,1);
    gt = et/3600/24 + datenum(1970,1,1);
    glat = gliderdata(:,2);
    glon = gliderdata(:,3);
    gdepth = -gliderdata(:,4);
    gcond = gliderdata(:,5);
    gtemp = gliderdata(:,6);
    gsal = gliderdata(:,7);
    gden = gliderdata(:,8);
    gsound = gliderdata(:,9);
    
    gpath = [gliderdir 'ru02_eddies.mat'];
    save(gpath, 'et','gt','glat','glon','gdepth','gcond','gtemp','gsal','gden','gsound','ets','gts','sglat','sglon','sdepth','bb532','c532','c650','gliderdata','samdata');
    'gliderdata saved.'
end %if

% define contour plot grid
rlat = (max(glat)-min(glat))/100;
rdepth = (max(gdepth)-min(gdepth))/201;
gglat = [min(glat):rlat:max(glat)];
ggdepth = [min(gdepth):rdepth:max(gdepth)]';

[ggglat,gggdepth,gggsal]=griddata(glat,gdepth,gsal,gglat,ggdepth);
gggtemp=griddata(glat,gdepth,gtemp,gglat,ggdepth);
gggden=griddata(glat,gdepth,gden,gglat,ggdepth);
gggsound=griddata(glat,gdepth,gsound,gglat,ggdepth);

%ggcc532=griddata(sglat,sgdepth,cc532,gglat,ggdepth);
%ggb532 =griddata(sglat,sgdepth,b532,gglat,ggdepth);

gtime = ['(' datestr(gt(1),31) ' to ' datestr(gt(end),31) ' GMT)'];

% plot salinity
subplot(2,2,1)
asal = gca;
contourf(ggglat,gggdepth,gggsal,64,'linestyle','none');
set(asal,'clim',[36.5,37.0]);
xl = str2num(get(asal,'xticklabel'));
xldeg = fix(xl);
xlmin = (xl-fix(xl))*60;
xll = [num2str(xldeg), repmat(':',length(xl),1), num2str(xlmin)];
set(asal,'xticklabel',xll);
xlabel('Latitude');
ylabel('Depth');
%title(['Glider N. Transect of A4 ' gtime ' -- Salinity']);
title('A4 Salinity');
colorbar

subplot(2,2,2)
atemp = gca;
contourf(ggglat,gggdepth,gggtemp,64,'linestyle','none');
%set(atemp,'clim',[36.5,37.0]);
xl = str2num(get(atemp,'xticklabel'));
xldeg = fix(xl);
xlmin = (xl-fix(xl))*60;
xll = [num2str(xldeg), repmat(':',length(xl),1), num2str(xlmin)];
set(atemp,'xticklabel',xll);
xlabel('Latitude');
ylabel('Depth');
%title(['Glider N. Transect of A4 ' gtime ' -- Temperature (C)']);
title('A4 Temp(C)');
colorbar

subplot(2,2,3)
aden = gca;
contourf(ggglat,gggdepth,gggden,64,'linestyle','none');
%set(atemp,'clim',[36.5,37.0]);
xl = str2num(get(aden,'xticklabel'));
xldeg = fix(xl);
xlmin = (xl-fix(xl))*60;
xll = [num2str(xldeg), repmat(':',length(xl),1), num2str(xlmin)];
set(aden,'xticklabel',xll);
xlabel('Latitude');
ylabel('Depth');
%title(['Glider N. Transect of A4 ' gtime ' -- Density (kg/m^3)']);
title('A4 Density');
colorbar

subplot(2,2,4)
asound = gca;
contourf(ggglat,gggdepth,gggsound,64,'linestyle','none');
%set(atemp,'clim',[36.5,37.0]);
xl = str2num(get(asound,'xticklabel'));
xldeg = fix(xl);
xlmin = (xl-fix(xl))*60;.
xll = [num2str(xldeg), repmat(':',length(xl),1), num2str(xlmin)];
set(asound,'xticklabel',xll);
xlabel('Latitude');
ylabel('Depth');
%title(['Glider N. Transect of A4 ' gtime ' -- Sound Speed (m/s)']);
title('A4 Sound Spd (m/s)');
colorbar

%eval(['print -djpeg100 -r150 ' gliderdir 'a4_northertransect2.jpg']);

gf = figure
ht = scatter(glat,gdepth,20,gsal,'o','filled');
caxis(gca,[36.5,37.0]);

gf3 = figure
hbb532 = scatter3(sglon,sglat,sdepth,50,bb532,'o','filled');
caxis(gca,[0,0.003]);