%
% PURPOSE: Unserialize the slate and plot the data.
%
% CHANGELOG:
%  1) rsm, 11 Nov 2008.  Using some of Brian S' code.
%  2)
%
% Next check for a Net Cdf file.  Read it in, if it's there.
%
tic
if( exist('ncname') ~= 1 )
    ncname = 'slate.nc';
end
fprintf('Reading Netcdf file %s header.\n',ncname);
if( exist(ncname) ~= 2 )
    fprintf('Error - Can''t find file named %s.\n',ncname);
    return
end
clear ncid cindx;
if( exist('mexcdf') )
    ncid = mexnc('OPEN',ncname,nc_nowrite_mode);
    [ndims,nVars,natts, unlimdimID] = mexnc('INQ',ncid);
else
    ncid = netcdf.open(ncname,'NC_NOWRITE');
    [ndims,nVars,natts,unlimdimID]= netcdf.inq(ncid);
end


toc
fprintf('Read variable names and parse.\n');
tic
%
% Put variable names on the Matlab stack:

clear nRecords ncvars;
for( i=1:nVars )
    if( exist('mexcdf') )
        [varname, vartype, ndims, dimids, natts] = mexnc('INQ_VAR',ncid,i-1);
    else
        [varname, vartype, dimids, natts] = netcdf.inqVar(ncid,i-1);
    end
    ncvars{i,1} = varname;
    ncvars{i,2} = vartype;
    ncvars{i,3} = dimids;
    ncvars{i,4} = natts;
    if( exist('mexcdf') )
        [dimname dimlen]=mexnc('INQ_DIM',ncid,dimids);
    else
        [dimname dimlen]=netcdf.inqDim(ncid,dimids);
    end
    ncvars{i,5} = dimname;
    ncvars{i,6} = dimlen;
    nRecords(i) = dimlen;
end
%
% Verify that each variable has the same number of records:
if( ~isempty(find(nRecords~=nRecords(1))) )
    fprintf('loadData: Some variables in %s have unequal dimensions.', ncname);
else
    %
    % Make it a scalar, since all dims are equal.
    nRecords = nRecords(1);
end
toc
fprintf('Load the data.\n');
tic
%
% Load the data into the array:
i=1;
units = cell(nVars,1);
varNames = cell(nVars,1);
for i=1:nVars,
    %
    % ??Pre-allocate memory??:
    %eval([ char(names(i)) ' = ones(nRecords,??) * NaN;']);
    %       '(1:nRecords,1:nCol) = ones(nRecords,nCol) * NaN;']);
    unit = [];
    try
        %att = netcdf.getAtt(ncid, ncvars{i,3}(1), 'units');
        unit = char(netcdf.getAtt(ncid, i-1, 'units'));
    end
    units{i} = unit;
    varName = char(ncvars(i,1));
    varNames{i} = varName;
    % Provide indicator that things are proceeding...
    %fprintf('i=%d, %s, %s\n',i, varName, unit );
    if mod(i, 5) == 0
        fprintf('.',i);
    end;
    
    if( exist('mexcdf') )
        eval([ char(ncvars(i,1)) ...
            ' = mexnc(''GET_VAR_DOUBLE'', ncid,' num2str(i-1) ');']);
    else
        if( i==1 )
            eval([ char(ncvars(i,1)) ...
                ' = netcdf.getVar(ncid,' num2str(i-1) ');']);
        else
            eval([ char(ncvars(i,1)) ...
                ' = netcdf.getVar(ncid,' num2str(i-1) ',''double'');']);
        end
    end
end
fprintf(' %d\n',i);
toc

time = Time;
if ~exist('startTime', 'var'),
    startTime = Time(1);
    Time = Time - startTime;
else
    if isempty (strfind(path, 'zoomAdaptiveDateTicks')),
        if ~isempty (strfind(path, 'LRAUV/review')),
            [startInd,endInd] = regexp(path, '\:[^\:]+LRAUV/review');
            revPath = path;
            revPath = [revPath(startInd+1:endInd) '/zoomAdaptiveDateTicks'];
            addpath( revPath );
            clear revPath;
        end
    end
    
    Time = Time / 86400 + datenum(1970,1,1);
end
%
% Fix netcdf conversion bug
%
indxOut = find( Time > Time(1) + 1e6 );
indxNeg = find( Time < Time(1) );
Time(indxOut) = NaN*[1:length(indxOut)]';
Time(indxNeg) = NaN*[1:length(indxNeg)]';
%
% Interpolate if requested
%
if exist('maxdt', 'var'),
    if( startTime == 0 )
        maxdt = maxdt / 86400;
    end
    fineTime = sort([[min(Time)+maxdt:maxdt:max(Time)]'; Time]);
    for i=1:nVars,
        if ~strcmp(char(ncvars(i,1)),'trajectory_id') && ~strcmp(char(ncvars(i,1)),'Time') ,
            evalStr = [ char(ncvars(i,1)) ' = interp1q(Time,' char(ncvars(i,1)) ',fineTime);'];
            eval(evalStr);
        end
    end
    Time = fineTime; clear fineTime;
end
%
% slate.nc is locked (can't rm it) until Matlab closes it.
%
if( exist('mexcdf') )
    mexnc('CLOSE',ncid);
else
    netcdf.close(ncid)
end
