% extract OKMC profile time series
pin='~dmenemen/llc_4320/regions/OKMC/';
cd ~dmenemen/llc_4320/regions/OKMC/glider
format compact
more off

% dimensions of OKMC region
nx=576;
ny=779;
nz=90;

% integration period to extract
startTime=datenum([2011 9 13 0 0 0]);
endTime=datenum([2012 11 15 14 0 0]);
Time=startTime:(1/24):endTime;
nt=length(Time);

% Salt
fld='Salt';
eval(['mkdir ' fld])
suf=['_' int2str(nx) 'x' int2str(ny) 'x' int2str(nz)];
for t=3250:nt
    disp([t nt])
    fin=[pin fld '/' fld suf '.' datestr(Time(t),30)];
    tmp=readbin(fin,[nx ny nz]);
    for x=1:24:nx
        mx=min(nx,x+23);
        for y=1:26:ny
            my=min(ny,y+25);
            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])
suf=['_' int2str(nx) 'x' int2str(ny) 'x' int2str(nz)];
for t=1:nt
    disp([t nt])
    fin=[pin fld '/' fld suf '.' datestr(Time(t),30)];
    tmp=readbin(fin,[nx ny nz]);
    for x=1:24:nx
        mx=min(nx,x+23);
        for y=1:26:ny
            my=min(ny,y+25);
            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
