% This file is an example how to read TS time series

% Load grid information
cd ~dmenemen/llc_4320/regions/OKMC/grid
nx=576;
ny=779;
nz=90;
Depth=readbin('Depth_576x779',[nx ny]);
XC   =readbin(   'XC_576x779',[nx ny]);
YC   =readbin(   'YC_576x779',[nx ny]);
load thk90;

% Integration period
Time=datenum('13-Sep-2011'):(1/24):datenum('15-Nov-2012 14:00:00');
nt=length(Time);

% Create list of file names and indices
FileNames={};
Indices=XC*0;
n=1;
for x=1:24:nx
    mx=min(nx,x+23);
    for y=1:26:ny
        my=min(ny,y+25);
        FileNames{n}=['.x' num2str(x) '-' num2str(mx) ...
                    '.y' num2str(y) '-' num2str(my) ...
                    '.nz' num2str(nz) '.nt' num2str(nt)];
        Indices(x:mx,y:my)=n;
        n=n+1;
    end
end

% Extract time series at location (I,J)
cd ~dmenemen/llc_4320/regions/OKMC/glider
I=200;
J=300;
fnm=FileNames{Indices(I,J)};
c=textscan(fnm,'%*2c %d %*c %d %*c %d %*c %d %*2c %d %*2c %d');
ix=c{1}:c{2};
jx=c{3}:c{4};
lon=XC(ix,jx);
lat=YC(ix,jx);
salt =single(readbin([ 'Salt/Salt'  fnm],[length(ix) length(jx) nz nt]));
theta=single(readbin(['Theta/Theta' fnm],[length(ix) length(jx) nz nt]));
i=find(lon(:,1)==XC(I,J));
j=find(lat(1,:)==YC(I,J));

% Plot top 300 m
iz=find(dpt90<300);
clf reset
colormap jet
subplot(211)
mypcolor(Time,-dpt90(iz),squeeze(theta(i,j,iz,:)))
datetick('x',4,'keeplimits')
title(['Theta at ' int2str(lon(i,j)) 'E, ' int2str(lat(i,j)) 'N'])
ylabel('Depth (m)')
colorbar;
subplot(212)
mypcolor(Time,-dpt90(iz),squeeze(salt(i,j,iz,:)))
datetick('x',4,'keeplimits')
title(['Salt at ' int2str(lon(i,j)) 'E, ' int2str(lat(i,j)) 'N'])
xlabel('Month of 2011/2012')
ylabel('Depth (m)')
colorbar;
