% extract SPURS1 profile time series
pin='~dmenemen/llc_4320/regions/SPURS1/';
cd ~dmenemen/llc_4320/regions/SPURS1/tiles
suf='_17209.9179.1_144.115.89';
format compact
more off

% dimensions of SPURS1 region
nx=144;
ny=115;
nz=89;

% integration period to extract
mints=dte2ts('13-Sep-2011',25,2011,9,10);
maxts=dte2ts('15-Nov-2012',25,2011,9,10);
ts=mints:144:maxts;
nt=length(ts);

% Salt
fld='Salt';
eval(['mkdir ' fld])
for t=1:nt
    disp([t nt])
    fin=[pin fld '/' myint2str(ts(t),10) '_' fld suf];
    tmp=readbin(fin,[nx ny nz]);
    for x=1:24:nx
        mx=min(nx,x+23);
        for y=1:23:ny
            my=min(ny,y+22);
            fout=[fld '/' fld '.x' num2str(x) '-' num2str(mx) ...
                  '.y' num2str(y) '-' num2str(my) ...
                  '.nz' num2str(nz) '.nt' num2str(nt)];
            writebin(fout,tmp(x:mx,y:my,:),1,'real*4',t-1);
        end
    end
end

% Theta
fld='Theta';
eval(['mkdir ' fld])
for t=1:nt
    disp([t nt])
    fin=[pin fld '/' myint2str(ts(t),10) '_' fld suf];
    tmp=readbin(fin,[nx ny nz]);
    for x=1:24:nx
        mx=min(nx,x+23);
        for y=1:23:ny
            my=min(ny,y+22);
            fout=[fld '/' fld '.x' num2str(x) '-' num2str(mx) ...
                  '.y' num2str(y) '-' num2str(my) ...
                  '.nz' num2str(nz) '.nt' num2str(nt)];
            writebin(fout,tmp(x:mx,y:my,:),1,'real*4',t-1);
        end
    end
end
