function [status] = readmld_(MOBYfile)

% READMLD_ - Read MLML_DBASE Archive File into MATLAB global workspace
%
% Use as:  [status] = readmld_(MOBYfile)
% Input:   MOBYfile = name of MLML_DBASE binary Archive File
% Output:  status     = returns 1 on success, 0 on failure
%
% Example: readmld_('2005032422c.mby')
%
% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 13 Sep 2006; S. Flora - modified

global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string
Xdat = [];Vaux = [];Faux = [];Scl  = [];Fmt  = [];Mpts = [];Nvar = [];Npts = [];
DscS = '';SymS = '';CapS = '';FnmS = '';FhdS = '';FdtS = '';
global FnmS_buf
status = 1;     %Set status

Nargs = nargin;
if Nargs == 0
  help readmld_
  return
end

MOBYfile = lower(MOBYfile);
[PATH,NAME,EXT,VER] = fileparts(MOBYfile);     % Extract off the front the dir
MOBYfile = [NAME EXT];
if ~isempty(PATH)
  PATH = [PATH '\'];
end

% Find Nvar in file you are reading
MOBYfile = deblank(MOBYfile);
fid = fopen([PATH MOBYfile],'r','ieee-le');
if (fid == -1);
  status = 0;
  disp(['  READMLD_ Error (Could not open: ' PATH MOBYfile ')']);
  return
end;

fseek(fid,4,'bof');
Nvar1     = fread(fid,1,'long');
fclose(fid);

VR1   = 1:Nvar1;
VR2 = VR1;
Fields = 1:11;
f = repmat(1,11,1);
%***************************************************************
% (define statements from MLML_DBASE.H)
% Define sizes of structure elements

MAX_HDR_STR = 8;

% Define data types
ML_DT_BYTE   = 1;
ML_DT_INT2   = 2;
ML_DT_INT4   = 3;
ML_DT_REAL   = 4;
ML_DT_DBLE   = 5;
ML_DT_REAL16 = 6;
ML_DT_CREAL  = 7;
ML_DT_CDBLE  = 8;
ML_DT_CHAR   = 9;

% File information types
ML_FILE_FIX = 0;
ML_FILE_VAR = 1;

% file extension types
ML_EXT_END          = 0;
ML_EXT_AUX          = 1;
ML_EXT_GRAPH        = 2;
ML_EXT_GENERIC      = 3;      % Generic
ML_EXT_MOS_INFO     = 201;    % VAX Vaux
ML_EXT_MOS_INFO_2   = 202;    % DOS Vaux
ML_EXT_MOS2_INFO    = 211;    % VAX Vaux
ML_EXT_MOS2_INFO_2  = 212;    % DOS Vaux
ML_EXT_CZCS_INFO    = 502;    % DOS Vaux
ML_EXT_SIS_INFO     = 301;    % VAX Vaux
ML_EXT_SIS_INFO_2   = 302;    % DOS Vaux

ML_EXT_RADIOM_HDR   = 500;    % VAX Faux
ML_EXT_RADIOM_HDR_2 = 503;    % DOS Faux
ML_EXT_MOBY2_HDR    = 610;    % VAX Faux
ML_EXT_MOBY2_HDR_2  = 612;    % DOS Faux

% Definitions for the file and subprocess interfaces
ML_RECORD_SIZE = 256;
OLD_FILE_VERSION = 1.03;  % last MLDBASE version from the VAX
CURRENT_FILE_VERSION = 3.00;    % First DOS MLDbase version

%-------------------------------------------------------------------------------
% (1) Open Archive File
Flag = 0;
fid = fopen([PATH MOBYfile],'r','ieee-le');
if (fid == -1)
  status = 0;
  disp(['  READMLD_ Error (Could not open: ' MOBYfile ')']);
  return
end;

FnmS_buf = MOBYfile;

[MX NX] = size(Xdat);
if max(VR2) > NX;
  addvars_(max(VR2)-NX);
end


disp(['  READMLD_(''' MOBYfile ''');']);
% ALWAYS READ (2)
% (2) Read file_info_block (definition from MLML_DBASE_FILES.C)
MLvers = round(fread(fid,1,'float')*100)/100;            %  float version
if (round(MLvers.*100) ~= round(CURRENT_FILE_VERSION.*100)) & (MLvers ~= OLD_FILE_VERSION)
  fclose(fid);
  fprintf(1,'  READMLD_ Error (File has incompatible MLML_DBASE version (%d) )\n',MLvers);
  return
end;
Nvar_buf = fread(fid,1,'long');                           % was: long num_var
Hdr_len  = fread(fid,1,'long');                           % was: long hdr_len
File_fmt = fread(fid,1,'long');                           % was: long file_fmt

Faux_buf = [];
Vaux_buf = [];
Vaux_VR = NaN;
% ALWAYS READ (3)
% (3) Read var_size_block (definition from MLML_DBASE_FILES.C)
Vhdr_len(1) = fread(fid,1,'ushort');                    % was: unsigned short vhdr_len
Vdat_len(1) = fread(fid,1,'ushort');                    % was: unsigned short data_len
for I = 2:Nvar_buf,
  Vhdr_len(I) = Vhdr_len(1);
  Vdat_len(I) = Vdat_len(1);
end;

pos = ftell(fid);

% FILE VARIABLES (FhdS, FdtS and Faux)
% (4) Read file_header_block (definition from MLML_DBASE.H)
if f(8) == 1
  for I=1:MAX_HDR_STR,
    FhdS_buf(I,1:80) = char(fread(fid,[1,80],'uchar'));        % was: char hstr[MAX_HDR_STR][80]
  end;
else
  fseek(fid,pos+80*8,'bof');
end

if f(10) == 1
  FdtS_buf = deblank(char(fread(fid,[1,20],'uchar')));         % was: char created[20]
else
  fseek(fid,pos+80*8+20,'bof');
end
if f(9) == 1
  % (5) Read File MLML_extension_block (definition from MLML_DBASE.H)
  while 1
    Fext_type = fread(fid,1,'long');                       % was: long type
    if (Fext_type == ML_EXT_END);  break;  end;
    Fext_size = fread(fid,1,'long');                        % was: long size
    if (Fext_type == ML_EXT_MOBY2_HDR_2 | ML_EXT_RADIOM_HDR_2)
      Faux_buf(1,1) = Fext_type;
      Faux_buf(2,1) = Fext_size/8 + 2;
      Faux_buf(3:(Fext_size/8+2),1) = fread(fid,Fext_size/8,'double');
    else
      fprintf(1,'  File header extension NOT read (Type %d  Size %d)\n',Fext_type,Fext_size);
      fseek(fid,Fext_size,'cof');                           % Skip unknown Extension data
    end;
  end;
end

Mpts_buf = 0;
lenVR = length(VR1);
VRhdr_len = Vhdr_len(VR1);
VRdat_len = Vdat_len(VR1);

for I = 1:lenVR
  pntr = fseek_(fid,2,VR1(I));
  fseek(fid,pntr,'bof');                                       % go to beginning of header for Var #I
  if f(2) == 1
    % The presision of this fread statement was changed to uchar from char so the
    % ASCII character above 127 read in correctly.
    DscS_buf(I,1:80) = char(fread(fid,[1,80],'uchar'));       % was: char description[80]
  else
    fseek(fid,80,'cof');
  end

  if f(5) == 1
    SymS_buf(I,1:8)  = char(fread(fid,[1,8],'uchar'));         % was: char symbol[8]
  else
    fseek(fid,8,'cof');
  end

  if f(3) == 1
    FmtS = upper(char(fread(fid,[1,20],'uchar')));         % was: char fmt[20] (FORTRAN format)
    %   Need to convert FmtS from FORTRAN to c style useful to MATLAB (MF 25Jan96)


    F = fread(fid,1,'long');                                % was: long fmt_width;
    P = findstr(FmtS,'.');
    if ~isempty(P);
      F = F + str2num(FmtS(P+1:P+1))/10;
      if ~isempty(findstr(FmtS,'1PE'))  F = -F;  end;
    end;
    Fmt_buf(I) = F;
  else
    fseek(fid,20+4,'cof');                                        % go to beginning of header for Var #I
  end

  if f(1) == 1
    CapS_buf(I,1:32) = char(fread(fid,[1,32],'uchar'));        % was: char caption[32]
  else
    fseek(fid,32,'cof');                                        % go to beginning of header for Var #I
  end

  if f(4) == 1
    Scl_buf(1:3,I) = fread(fid,[3,1],'float');                  % was: float min_scl, max_scl, tick_int
    Scl_buf(4:5,I) = fread(fid,[2,1],'long');                   % was: long lbl_int, dec
  else
    fseek(fid,12+8,'cof');                                        % go to beginning of header for Var #I
  end


  % (7) Read Variable mlml_extension_block (definition from MLML_DBASE.H)
  R=0;  % pointer to last-used row# in Vaux(:,VR2(I))
  MOS_SIS = 0;
  while 1
    Vext_type = fread(fid,1,'long');                      % was: long type
    if (Vext_type == ML_EXT_END),  break;  end;
    Vext_size = fread(fid,1,'long');                      % was: long size
    Pos = ftell(fid);                                   % hold current File position
    if f(6) == 1
      % if (Vext_type == ML_EXT_MOS_INFO_2) fprintf(1,'  . Var# %d MOS1 Ext (DOS fmt)\n',VR2(I)); end
      % if (Vext_type == ML_EXT_SIS_INFO_2) fprintf(1,'  . Var# %d SIS Ext (DOS fmt)\n',VR2(I)); end
      % ??? needs to be checked
      [Vaux_buf1,Status] = readvx2_(fid);

      [MB NB] = size(Vaux_buf1);
      Vaux_buf(1:MB,I) = Vaux_buf1;
      fseek(fid,Pos+Vext_size,'bof');
      MOS_SIS = 1;
      if all(Vaux_VR ~= VR2(I))
        Vaux_VR = [Vaux_VR; VR2(I)];
      end
    else
      fseek(fid,Pos + Vext_size,'bof');                                        % go to beginning of header for Var #I
    end
  end;
  % (8) Read Variable data

  if f(11) == 1 | f(7) == 1
    pntr = fseek_(fid,4,VR1(I));
    fseek(fid,pntr,'bof');                            % go to beginning of header for Var #I
    Vsize = fread(fid,1,'long');                      % was: long size
    Vtype = fread(fid,1,'long');                      % was: data_type
    Velem = fread(fid,1,'long');                      % was: element_size
    Np = Vsize/Velem;                                 % # points (rows) = size/element_size
    oldMpts = Mpts;

    if nargout > 1
      Mpts_buf = max(Mpts_buf,Np);                   % max # points (i.e. longest Variable)
    elseif nargout < 2
      Mpts_buf = max(Mpts,Np);                        % max # points (i.e. longest Variable)
    end

    if nargout < 2
      if oldMpts < Mpts_buf
        lengthn_(Mpts_buf-oldMpts);
      end
    end
    Npts_buf(I) = Np;


    if f(7) == 1
      % This will put NaN's in Xdat_buf so it is not initialized to
      % zero's if the currect variable is shorter than the last.
      % This will only take care of NaN's at the top of Xdat_buf
      % If the next variable is longer than the last then code
      % later in this program will add NaN's to the bottom of
      % Xdat_buf
      if exist('Xdat_buf')
        [MX NX] = size(Xdat_buf);
        Xdat_buf(1:MX,I) = NaN*ones(MX,1);
      end

      if MLvers == CURRENT_FILE_VERSION
        NaN_val = 1.7010999925570362E+38;                        % First DOS MLDbase version
      elseif MLvers == OLD_FILE_VERSION
        NaN_val = 1.701099992557036E+38;                         % IEEE NaN value from VAX MLDBASE -
      end
      if (Vtype == ML_DT_BYTE)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'uchar');
        NaN_val = 128;
      elseif (Vtype == ML_DT_INT2)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'short');
        NaN_val = 32768;
      elseif (Vtype == ML_DT_INT4)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'long');
        NaN_val = hex2dec('80000000');
      elseif (Vtype == ML_DT_REAL)
        if exist('Xdat_buf')
          Xdat_buf(1:MX,I) = NaN*ones(MX,1);
        else
          Xdat_buf(1:Np,I) = NaN*ones(Np,1);
        end
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'float');
      elseif (Vtype == ML_DT_DBLE)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'double');
      elseif (Vtype == ML_DT_REAL16)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'double');
      elseif (Vtype == ML_DT_CREAL)
        Xdat_buf(1:Np,I) = fread(fid,[1,2*Np],'float');
      elseif (Vtype == ML_DT_CDBLE)
        Xdat_buf(1:Np,I) = fread(fid,[1,2*Np],'double');
      elseif (Vtype == ML_DT_CHAR)
        Xdat_buf(1:Np,I) = fread(fid,[Np,1],'char');
        NaN_val = 0;
      else
        fprintf(1,'Unknown data type (%d) in var # %d -- Data ignored\n',Vtype,I);
        NaN_val = 0;
      end;
      if NaN_val ~= 0
        % OLD WAY - REQUIRES BEING ABLE TO WRITE TO THE DISK WITH THE DATA
        % WHICH IS NOT POSSIBLE WITH A CD_ROM

        % When writting out MLDBASE Files Matlab's NaN value is replaced
        % with our IEEE standard number from VAX MLDBASE (AMACH(6) DMACH(6)).
        % This is done because Matlab has not seemed willing to give us
        % their value for NaN. So that we can read our data on any platform
        % and not be restricted to Matlab we do not use their NaN's. This
        % means we must replace our IEEE standard numbers with NaN.

        % The following 7 lines of codes are a work around for Matlab 5.1.
        % We write data out in float format, this includes 1.701099992557036E+38
        % our float NaN values. When Matlab 5.1 reads this number back in
        % it different by 3.7779e+022 therefore find(Data == NaN_val) does not
        % work. In Matlab 4.2c the difference is 0 and therefore find worked.
        % So to get around the problem I write out our NaN_val and then read
        % it back in so that the NaN_val will match the NaN values in the data
        % we are reading in and find will work.
        %
        %        nanfid = fopen('nan.bin','wb');
        %        fwrite(nanfid,NaN_val,'float');
        %        fclose(nanfid);
        %
        %        nanfid = fopen('nan.bin','rb');
        %        NaN_val = fread(nanfid,1,'float');
        %        fclose(nanfid);
        %        delete('nan.bin');
        %
        %        nanind = find(Xdat_buf(1:Np,I) == NaN_val);
        %        if ~isempty(nanind)
        %          Xdat_buf(nanind,I) = NaN;
        %        end

        % New Way - Don't like this as much but it does not require being able to
        % write the the disk containing the data
        nanind = find(Xdat_buf(1:Np,I) > 1.7E+38 & Xdat_buf(1:Np,I) < 1.8E+38);
        if ~isempty(nanind)
          Xdat_buf(nanind,I) = NaN;
        end
      end

    end
  end
end;

% This will add NaN's to the bottom of variables in Xdat_buf
% if Xdat_buf gets longer as it is added to Xdat_buf the
% previous variables will have zeros added to the bottom to
% make Xdat_buf larger
if exist('Xdat_buf')
  [MX NX] = size(Xdat_buf);
  % Only do this if there are Npts that are less than Mpts
  % otherwise there are no NaN's to add.
  if any(Npts_buf < MX)
    % Find the Npts that are less than Mpts (MX)
    Nind = find(Npts_buf < MX);
    for nn = 1:length(Nind)
      % Number of NaN's to add
      NaNadd = MX - Npts_buf(Nind(nn));

      % Add the NaN's
      Xdat_buf(Npts_buf(Nind(nn))+1:MX,Nind(nn)) = NaN*ones(NaNadd,1);
    end
  end
end

fclose(fid);

whos global
if f(1) > 0
  CapS(VR2,1:32) =  CapS_buf(1:length(VR1),1:32);
end;
if f(2) > 0,
  DscS(VR2,1:80) =  DscS_buf(1:length(VR1),1:80);
end;
if f(3) > 0;
  Fmt(1,VR2) =  Fmt_buf(1,1:length(VR1));
end;
if f(4) > 0;
  [MS NS] = size(Scl_buf);
  Scl(1:MS,VR2) =  Scl_buf(1:MS,1:length(VR1));
end;
if f(5) > 0;
  SymS(VR2,1:8) =  SymS_buf(1:length(VR1),1:8);
end;
if f(6) > 0;
  [MVB NVB] = size(Vaux_buf);
  if MVB ~= 0
    Vaux_VR = Vaux_VR(~isnan(Vaux_VR));
    Vaux(1:MVB,Vaux_VR) =  Vaux_buf(1:MVB,1:length(Vaux_VR));
  end
end;
CapS
if f(7) > 0;
  [MX NX] = size(Xdat_buf);

  % Add NaN's to Xdat if needed otherwise it is initialized to zeros
  if MX > Mpts;
    lengthn_(MX-Mpts);
  end

  blnkvar_(VR2,7);
  Xdat(1:MX,VR2) =  Xdat_buf(1:MX,1:length(VR1));
end;
if f(8) > 0;
  FhdS(1:8,1:80) =  FhdS_buf(1:8,1:80);
end;
if f(9) > 0;
  [MFB NFB] = size(Faux_buf);
  if MFB ~= 0
    Faux  =  Faux_buf(1:MFB,1);
  end
end;
if f(10) > 0;
  FdtS =  FdtS_buf;
end;

if isempty(deblank(FnmS));
  FnmS = MOBYfile;
end

% Check Variables
chckvar_;
chckfv_;

% Remove extra NaN's
shorten_(0);
status = 1;

%--------------------------------------------------------------------------
% SUB FUNCTION SECTION
%--------------------------------------------------------------------------
function status = addvars_(NV)

% ADDVARS_ - Add/Create MLDBASE Variables
%
% Add/Create MLDBASE variables with scales, description, caption,
% symbol, format.  Increasing the number of variables by NV.
%
% Use As:   status   = addvars_(NV)
%
% Input:    NV       = number of variables to add
% Output:   status   = returns 1 on success, 0 on failure
%
% Example:  status   = addvars_(3) --> 3 New data, caption,
%                      description etc. variables created
%
% See Also: BLNKVAR_, DELVARS_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 27 Jan 1996; W. Broenkow
% 30 Jan 1997; S. Flora - Changed the way Npts,Mpts and Nvar are updated
% 07 Nov 1997; S. Flora - works in Matlab 5.1
% 05 Sep 2000; S. Flora - added MAX_xxxS_LENGTH so in the future these can get changed more easily
%                         and added FCNname for error messages

%mlglobal
global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string

status = 1;
chckpts_;

% Hopefully in the future these functions will allow these...........
% to be changed in a configuration file
MAX_FHDS_STRS   = 8;
MAX_FHDS_LENGTH = 80;
MAX_DSCS_LENGTH = 80;
MAX_CAPS_LENGTH = 32;
MAX_SYMS_LENGTH = 8;

FCNname = '  BLANKVAR_';          % For error messages

value = NaN;

if isempty(Mpts) | Mpts == 0    % Added Mpts = 0, what delvars_ sets Mpts to
  Mpts = 1;
  Nvar = NV;
  Npts = zeros(1,NV);

  FhdS = char(32*ones(MAX_FHDS_STRS,MAX_FHDS_LENGTH));
  FdtS = date;
  FnmS = blanks(12);

  Faux = zeros(12,1);
  Faux(1,1) = 1;
  Faux(2,1) = 12;

  CapS(1:Nvar,1:MAX_CAPS_LENGTH)   =  char(32*ones(Nvar,MAX_CAPS_LENGTH));
  CapS                =  char(CapS);
  DscS(1:Nvar,1:MAX_DSCS_LENGTH)   =  char(32*ones(Nvar,MAX_DSCS_LENGTH));
  DscS                =  char(DscS);
  SymS(1:Nvar,1:MAX_SYMS_LENGTH)    =  char(32*ones(Nvar,MAX_SYMS_LENGTH));
  SymS                =  char(SymS);

  Fmt(1,1:Nvar)       = 11.3*ones(1,Nvar);
  Scl(1:15,1:Nvar)    = zeros(15,Nvar);

  Vaux(1:12,1:Nvar)   = zeros(12,Nvar);
  Vaux(1,:)           = ones(1,Nvar);
  Vaux(2,:)           = 12*ones(1,Nvar);
  Xdat(1:Mpts,1:Nvar) = NaN*ones(Mpts,Nvar);
else
  %  'ADDVARS_.M: Mpts Not Empty'
  VR = (max(Nvar)+1):max(Nvar + NV);    % New variables
  CapS(VR,1:MAX_CAPS_LENGTH) = char(32*ones(length(VR),MAX_CAPS_LENGTH));
  CapS                =  char(CapS);
  DscS(VR,1:MAX_DSCS_LENGTH) = char(32*ones(length(VR),MAX_DSCS_LENGTH));
  DscS                =  char(DscS);
  SymS(VR,1:MAX_SYMS_LENGTH)  = char(32*ones(length(VR),MAX_SYMS_LENGTH));
  SymS                =  char(SymS);

  Fmt(1,VR)          = 11.3*ones(1,length(VR));
  Scl(1:15,VR)       = zeros(15,length(VR));
  [MV NVV] = size(Vaux);
  Vaux(1:MV,VR)      = zeros(MV,length(VR));
  Vaux(1,VR)         = ones(1,length(VR));
  Vaux(2,VR)         = 12*ones(1,length(VR));

  if Mv == 1 & Nv == 1
    Xdat(1:Mpts,VR)    = value*ones(Mpts,length(VR));
  elseif Nv == 1 & Mv > 1
    Xdat(1:Mpts,VR)    = NaN*ones(Mpts,length(VR));
    valueM = value;
    for k = 1:length(VR)-1
      valueM = [valueM,value];
    end
    Xdat(1:length(valueM),VR)    = valueM;
  elseif Nv >1 & Mv > 1
    Xdat(1:Mpts,VR)    = NaN*ones(Mpts,length(VR));
    Xdat(1:Mv,VR(1:Nv))    = value;
  end

  % Only update Npts if value is not NaN
  if ~isnan(value)
    chckpts_;     % Updates Mpts, Npts,Nvar  end
  elseif isnan(value)
    [Mpts, Nvar] = size(Xdat);
    Npts = [Npts zeros(1,NV)];
  end
end;

status = 1;
%--------------------------------------------------------------------------
function chckpts_

% CHCKPTS_ - Updates Mpts, Npts, and Nvar to size of data array
%
% Use As:   chckpts_
%
% See Also: CHCKFV_, CHCKVAR_
%
% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 07 Nov 97; S. Flora - works in Matlab 5.1


global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string

%-------------------------------------
% Updates Mpts and Nvar
[Mpts, Nvar] = size(Xdat);

if isempty(Xdat)
  Mpts = 0;
  Npts = 0;
  Nvar = 0;
end

% Create Npts
Npts = Mpts*ones(1,Nvar);
NaNind = find(any(isnan(Xdat)) == 1);              % Find variables with NaN's
% If there are NaN's then replace that variable in Npts
for k = 1:length(NaNind)                           % Loop through variables with NaN's
  ind = find(isnan(Xdat(:,NaNind(k))) == 0);       % Find Numbers not NaN's
  if ~isempty(ind)                                 % Found numbers
    Npts(NaNind(k)) = max(ind);                     % Put the max(ind) in Npts
  elseif isempty(ind)                              % Didn't find any numbers
    Npts(NaNind(k)) = 0;                           % Put 0 in Npts
  end
end
%--------------------------------------------------------------------------
function pntr = fseek_(fid,option,VN)

% FSEEK_ - Find Position in a MLDBASE File
%
% Use As: pntr = fseek_(fid,option,VN)
%
% Inputs: fid       = file identifier of open file
%         option    = Which fseek postion to return
%                     1 = Beginning of File Auxiliary (Faux)
%                     2 = Beginning of Variable Header (DscS)
%                     3 = Beginning of Variable Auxiliary (Vaux)
%                     4 = Beginning of Data (Xdat)
%         VN        = Variable number if option = 2, 3 or 4
% Output: pntr      = Postion in file
%

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 4 Nov 97; S. Flora
% 11 Nov 97; S. Flora - works in Matlab 5.1
%  4 Apr 98; S. Flora - works in Matlab 5.2 - CHANGED Version Number

status = 1;
if nargin < 2
  help fseek_
  return
end
%-------------------------------------------------------------------------------
% (define statements from MLML_DBASE.H)
% Define sizes of structure elements
MAX_HDR_STR = 8;

% Define data types
ML_DT_BYTE   = 1;
ML_DT_INT2   = 2;
ML_DT_INT4   = 3;
ML_DT_REAL   = 4;
ML_DT_DBLE   = 5;
ML_DT_REAL16 = 6;
ML_DT_CREAL  = 7;
ML_DT_CDBLE  = 8;
ML_DT_CHAR   = 9;

% File information types
ML_FILE_FIX = 0;
ML_FILE_VAR = 1;

% file extension types
ML_EXT_END          = 0;
ML_EXT_AUX          = 1;
ML_EXT_GRAPH        = 2;
ML_EXT_MOS_INFO     = 201;    % VAX Vaux
ML_EXT_MOS_INFO_2   = 202;    % DOS Vaux
ML_EXT_SIS_INFO     = 301;    % VAX Vaux
ML_EXT_SIS_INFO_2   = 302;    % DOS Vaux
ML_EXT_RADIOM_HDR   = 500;    % VAX Faux
ML_EXT_RADIOM_HDR_2 = 502;    % DOS Faux

% Definitions for the file and subprocess interfaces
ML_RECORD_SIZE = 256;
OLD_FILE_VERSION = 1.03;  % last MLDBASE version from the VAX
CURRENT_FILE_VERSION = 3.00;    % First DOS MLDbase version


fseek(fid,0,'bof');

if status == 0;return;end
%-----------------------------------------------
% ALWAYS READ (2)
% (2) Read file_info_block (definition from MLML_DBASE_FILES.C)
MLvers = round(fread(fid,1,'float')*100)/100;            %  float version
if (MLvers ~= CURRENT_FILE_VERSION) & (MLvers ~= OLD_FILE_VERSION)
  fprintf(1,'  ERROR: File has incompatible MLML_DBASE version (%d)\n',MLvers);
  return
end;
Nvar1     = fread(fid,1,'long');                           % was: long num_var

%-----------------------------------------------
Hdr_len  = fread(fid,1,'long');                           % was: long hdr_len
File_fmt = fread(fid,1,'long');                           % was: long file_fmt

% ALWAYS READ (3)
% (3) Read var_size_block (definition from MLML_DBASE_FILES.C)
if (File_fmt==ML_FILE_FIX)
  Vhdr_len(1) = fread(fid,1,'ushort');                    % was: unsigned short vhdr_len
  Vdat_len(1) = fread(fid,1,'ushort');                    % was: unsigned short data_len
  for I = 2:Nvar1,
    Vhdr_len(I) = Vhdr_len(1);
    Vdat_len(I) = Vdat_len(1);
  end;
elseif (File_fmt==ML_FILE_VAR)
  for I = 1:Nvar1,
    Vhdr_len(I) = fread(fid,1,'ushort');
    Vdat_len(I) = fread(fid,1,'ushort');
  end;
end;

%-----------------------------------------------
% Find the Beginning of File Auxiliary (Faux)
if option == 1
  pos = ftell(fid);
  pntr = pos + 8*80 + 20;
end

% Find the Beginning of Variable Header (DscS)
if option == 2
  pntr = (Hdr_len + sum(Vhdr_len(1:VN-1)) + sum(Vdat_len(1:VN-1)))*256;
end

% Find the Beginning of Variable Auxiliary (Vaux)
if option == 3
  pntr = ((Hdr_len + sum(Vhdr_len(1:VN-1)) + sum(Vdat_len(1:VN-1)))*256)+164;
end

% Find the Beginning of Data (Xdat)
if option == 4
  pntr = (Hdr_len + sum(Vhdr_len(1:VN)) + sum(Vdat_len(1:VN-1)))*256;
end

%--------------------------------------------------------------------------
function [Vaux_buf,Status] = readvx2_(fid)

% READVX2_  - Read (DOS sytle) MOS or SIS Var extension data into Vaux_buf
%
% Use as  [Vaux_buf,Status] = readvx2_(fid)
%
% Input:  fid = identifier of opened (version 1.03) .MLD file
% Output: Vaux_buf = Column of SIS or MOS Var header extension data
%         Status   = returns 1 on success, 0 on failure
%
% See also  MLGLOBAL  READMLD_  READVM1_  READVS1_   VAUXTYP_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 21-Apr-1996 M. Feinholz Copyright (c) Moss Landing Marine Laboratories
% 06 Jul 96; S. FLora - Changed to only fid is input, and the program
%                       reads in Vext_type and Vext_size

% NOTE: This reads into Vaux(:,C) data which were previously read from the VAX
%       format via READVM1_.M or READVS1_.M, then written to 'DOS' format Archive
%       File via WRITML1_.M.  The DOS format re-orderd the first 30 rows of Var
%       extension data and Archived extension data as 8Byte floating point
%       'double' reals.
%

Status=0;

if nargin == 0
  help readvx2_
  return
end

fseek(fid,-8,'cof');
Vext_type = fread(fid,1,'long');                      % was: long type
Vext_size = fread(fid,1,'long');                      % was: long size

NumRow = Vext_size/8 + 2;
Vaux_buf = zeros(NumRow,1);

Vaux_buf(1,1) = Vext_type;
Vaux_buf(2,1) = Vext_size/8 + 2;

Vaux_buf(3:NumRow,1) = fread(fid,[NumRow-2,1],'double');

Status=1;
%--------------------------------------------------------------------------
function status = lengthn_(NE,value)

% LENGTHN_ - Increase Number of MLdbase data Elements
%
% Increase the number of data Elements in Xdat by adding NaN or a constant
% value to end of existing Variables.
%
% Use As:  status = lengthn_(NE,value)
% Input:   NE     = Number of data Elements to add
%          value  = value to pad to additional Elements, if
%                   not specified, NaN are added
% Output:  status = returns 1 on success, 0 on failure
%
% See Also: SHORTEN_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 15 Feb 96; W. Broenkow
% 03 Feb 97; S. Flora - fixed Nvar, Mpts problem
% 11 Nov 97; S. Flora - works in Matlab 5.1

status = 1;

%mlglobal
global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string

Nargs = nargin;
if Nargs == 0
  help lengthn_
  return
end

%Load data if Xdat is empty
if isempty(Xdat);
  status = 0;
  disp('  LENGTHN_ No Data in Workspace')
end

% Set value to put in new elements
if Nargs < 2
  value = NaN;
end
%-----------------------------------------------
% Updates Mpts and Nvar
[Mpts Nvar] = size(Xdat);

if status ==0;return;end
%*********************************************
% All error checking should be done before this line
% the following code will alter the MLDBASE data variables

ER = (Mpts+1):(Mpts+NE);
Xdat(ER,1:Nvar) = value*ones(length(ER),Nvar);

[Mpts, Nvar] = size(Xdat);
% Only update Npts if value is not NaN
if ~isnan(value)
  Npts = Mpts*ones(1,Nvar);
end

status = 1;
%--------------------------------------------------------------------------
function status = blnkvar_(VR,Fields,ER)

% BLNKVAR_ - Overwrite Contents of MLdbase Variable with NaN
%
% Overwrite the data in a range of MLdbase Variables; Variable header
% string fields are overwritten with blanks (char 32) and numeric values
% are overwritten with NaN or zero.
%
% Use As:   status = blnkvar_(VR,Fields*,ER*)	* = optional
%
% Input:    VR     = Variable Range to be blanked
%                    VR = 0, Blank all Variables
%           Fields = MLdbase fields: CapS, DscS, Fmt, Scl, SymS, Vaux, Xdat.
%                    If Fields is not given, data from all fields are blanked.
%                    Fields can be specified by digit or by 4 char string.
%                    0 = All fields
%                    1 = Variable Captions      blanks
%                    2 = Variable Description   blanks
%                    3 = Variable Format        zero
%                    4 = Variable Scales        zero
%                    5 = Variable Symbols       blanks
%                    6 = Variable Auxillary     zero
%                    7 = Data                   NaN
%                    8 = File Header Strings    blanks
%                    9 = File Auxiliary Array   zero
%           ER     = Element Range to be blanked, if not given or
%                    ER = 0, all Elements are blanked
% Output:   status = returns 1 on success, 0 on failure
%
% Example:  blnkvar_(3,[1 7],1:5) Blank the Caption and
%           Data for Variable 3 and Elements 1 through 5
%
% See Also: DELVARS_, ADDVARS_

% Mpts is not changed
% Npts is set to 0 for the blanked variables
% Nvar is not changed

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 27 Jan 96; rev 17 Feb 1996; W. Broenkow
% 05 Jul 96; S. Flora - changed Faux initialized to zeros, more checking
% 07 Nov 97; S. Flora - works in Matlab 5.1

status = 1;

global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string

if nargin == 0
  status = 0;
  help blnkvar_
  return
end

%-----------------------------------------------
%Load data if Xdat is empty
if isempty(Xdat);
  status = 0;
  disp('  BLNKVAR_ No Data in Workspace')
end

%-----------------------------------------------
[Mpts Nvar] = size(Xdat);
%-----------------------------------------------
if (nargin == 1)
  Fields = [1:9]';
  ER = 1:Mpts;
end

if (nargin == 2)
  ER = 1:Mpts;
end

% Default is to blank all
if VR == 0;
  VR = 1:Nvar;
end
if Fields == 0
  Fields = [1:9]';
end
if ER == 0;
  ER = 1:Mpts;
end

f = zeros(9,1);
[Mf,Nf] = size(Fields);
for j = 1:Mf
  if(Fields(j) > 0 & Fields(j) < 10)
    f(Fields(j)) = 1;   % default is to move all fields  
  end
end


%*********************************************
% All error checking should be done before this line.
% The following code will alter the MLDBASE data variables.
% When used with COPYBUF_ in READMLX_ and partial variables, we must.
% avoid copying null fields, which produces a matrix dimension error.

if f(1,1) >0,
  for k = 1:length(VR)
    CapS(VR(k),1:32) = blanks(32);
  end
end;
if f(2,1) >0,
  for k = 1:length(VR)
    DscS(VR(k),1:80) = blanks(80);
  end
end;
if f(3,1) >0,
  Fmt(1,VR)       = zeros(1,length(VR));
end;
if f(4,1) >0,
  Scl(1:15,VR)  = zeros(15,length(VR));
end;
if f(5,1) >0,
  for k = 1:length(VR)
    SymS(VR(k),1:8)  = blanks(8);
  end
end;
if f(6,1) >0,
  [MV NV] = size(Vaux);
  Vaux(1:MV,VR) = zeros(MV,length(VR));
end;
if f(7,1) >0,
  Xdat(ER,VR)   = NaN*ones(length(ER),length(VR));
end;
if f(8,1) >0,
  for F = 1:8
    FhdS(F,1:80) = blanks(80);
  end
end;
if f(9,1) >0,
  [MF NF] = size(Faux);
  Faux(3:MF,1) = zeros(MF-2,1);
end;

chckvar_;      % Updates Mpts, Npts and Nvar
chckfv_;

status = 1;

%--------------------------------------------------------------------------
function chckvar_

% CHCKVAR_ - Checks MLDBASE Data Variables
%
% CHCKVAR_ checks each of the MLDBASE Data Variables to make
% sure it is of the correct length and dimension. If it is
% not correct the variable is truncated or lengthened. Also,
% replaces infinite and complex data elements with NaN. The
% File Name string, File Date string, File Header string and
% File Auxillary array are not checked.
%
% Use As:  chckvar_
%
% Also See: CHCKFV_, CHCKPTS_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 28 Feb 96; S. Flora
% 26 Aug 96; S. Flora - added checking Vaux(1,:) so there are no zeros
% 07 Nov 97; S. Flora - works in Matlab 5.1

%mlglobal
global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string
%-------------------------------------
% Updates Mpts and Nvar
[Mpts, Nvar] = size(Xdat);

if Mpts == 0 | Nvar == 0
  Xdat = NaN;
end
chckpts_;
%-------------------------------------
% Checks Xdat for infinite and complex elements
if any(any(isinf(Xdat)))
  for k = 1:Nvar
    ER = find(isinf(Xdat(:,k)) == 1);
    Xdat(ER,k) = NaN*ones(length(ER),1);
  end
  disp('  CHCKVAR_ Infinite Elements found (Replaced with NaN''s)')
end

% Time Consuming but needed, Checks for complex numbers
if ~isreal(Xdat)
  for k = 1:Nvar
    if ~isreal(Xdat(:,k))   % A complex number exists in this var check
      for h = 1:Npts(1,k)     % each element
        if ~isreal(Xdat(h,k));
          Xdat(h,k) = NaN;
        end
      end
    end
  end
  disp('  CHCKVAR_ Complex Elements found (Replaced with NaN''s)')
end

%-------------------------------------
% Check Caption
str = '';
if ~isstr(CapS);CapS = '';end
[M N ] = size(CapS);
% Check Number of Caption Strings
if M < Nvar
  Madd  = (Nvar - M);
  Mstrt = max(Nvar) - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  for k = 1:length(VR)
    CapS(VR(k),1:32) = char(32);
  end
elseif M > Nvar
  CapS = CapS(1:Nvar,1:32);
end

[M N ] = size(CapS);
% Check Caption String length
if (N > 32 | N < 32)
  CapS = strpad_(CapS,32);
end
%-------------------------------------
% Check Descricption
str = '';
if ~isstr(DscS);DscS = '';end
[M N ] = size(DscS);
% Check Number of Descricption Strings
if M < Nvar
  Madd  = (Nvar - M);
  Mstrt = max(Nvar) - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  for k = 1:length(VR)
    DscS(VR(k),1:80) = blanks(80);
  end
elseif M > Nvar
  DscS = DscS(1:Nvar,1:80);
end

[M N ] = size(DscS);
% Check Descricption String length
if (N > 80 | N < 80)
  DscS = strpad_(DscS,80);
end
%-------------------------------------
% Check Symbols
str = '';
if ~isstr(SymS);SymS = '';end
[M N ] = size(SymS);
% Check Number of Caption Strings
if M < Nvar
  Madd  = (Nvar - M);
  Mstrt = Nvar - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  for k = 1:length(VR)
    SymS(VR(k),1:8) = blanks(8);
  end
elseif M > Nvar
  SymS = SymS(1:Nvar,1:8);
end

[M N ] = size(SymS);
% Check Symbols String length
if (N > 8 | N < 8)
  SymS = strpad_(SymS,8);
end
%-------------------------------------
% Check Format
[M N ] = size(Fmt);
% Check Number of Formats
if N < Nvar
  Madd  = (Nvar - N);
  Mstrt = Nvar - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  Fmt(1,VR) = zeros(1,length(VR));
elseif N > Nvar
  Fmt = Fmt(1,1:Nvar);
end
% Check dimension of Format
if M > 1
  Fmt = Fmt(1,1:Nvar);
end
%-------------------------------------
% Check Scl
[M N ] = size(Scl);
% Check number of Scales
if N < Nvar
  Madd  = (Nvar - N);
  Mstrt = Nvar - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  Scl(1:M,VR) = zeros(M,length(VR));
elseif N > Nvar
  Scl = Scl(1:M,1:Nvar);
end
% Check dimension of Scales
if M > 15 & Nvar ~= 0
  Scl = Scl(1:15,1:Nvar);
elseif M < 15 & Nvar ~= 0
  Madd  = (15 - M);
  Mstrt = 15 - Madd + 1;
  ER   = Mstrt:(Mstrt + Madd - 1);
  Scl(ER,1:Nvar) = zeros(length(ER),Nvar);
end
%-------------------------------------
% Check Vaux
% Max Vaux size
[MV NV ] = size(Vaux);
% Check Number of Vaux
if NV < Nvar
  Madd  = (Nvar - NV);
  Mstrt = Nvar - Madd + 1;
  VR   = Mstrt:(Mstrt + Madd - 1);
  Vaux(1:MV,VR) = zeros(MV,length(VR));
elseif NV > Nvar
  Vaux = Vaux(1:MV,1:Nvar);
end
%Check dimension
if MV < 12 & Nvar ~= 0
  Madd  = (12 - MV);
  Mstrt = 12 - Madd + 1;
  ER   = Mstrt:(Mstrt + Madd - 1);
  Vaux(ER,1:Nvar) = zeros(length(ER),Nvar);
end

% Check to make sure Vaux is not greater than the max
% Vaux size in element range 2, also make sure Vaux is
% not less than 12 elements
MVsize = max(Vaux(2,find(Vaux(2,:))));
if MVsize < 12; MVsize = 12;end
ind = find(Vaux(2,:) < 12 & Vaux(2,:) ~= 0);
if ~isempty(ind)
  Vaux(2,ind) = 12*ones(1,length(ind));
end
[MV NV ] = size(Vaux);
if ~isempty(MVsize)
  if MV > MVsize & Nvar ~= 0
    % Shorten Vaux
    Vaux = Vaux(1:MVsize,1:Nvar);
  elseif MV < MVsize
    Madd  = (MVsize - MV);
    Mstrt = MVsize - Madd + 1;
    ER   = Mstrt:(Mstrt + Madd - 1);
    Vaux(ER,1:Nvar) = zeros(length(ER),Nvar);
  end
end
% Make sure there are not type nones in the first Vaux
if min(Vaux(2,:)) <= 0;
  ind = find(Vaux(1,:) <= 0);
  if ~isempty(ind)
    Vaux(1,ind) = ones(1,length(ind));
    Vaux(2,ind) = 12*ones(1,length(ind));
  end
end
%--------------------------------------------------------------------------
function chckfv_

% CHCKFV_ - Checks MLDBASE File Variables
%
% Updates FhdS to correct size and checks Faux,
% FnmS and FdtS to make sure they are not empty.
%
% Use As:   chckfv_
%
% See Also: CHCKPTS_, CHCKVAR_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 14 Mar 96; S. Flora - Adapted from chckvar_
% 17 Jan 97; S. Flora - fixed Faux if is empty
% 22 May 00; S. Flora - changed so if Faux(1) is 1 the variable is one 12 long, consistent with writmld_

%mlglobal
global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string
%-------------------------------------
% Check File Header String
str = '';
if ~isstr(FhdS);FhdS = '';end
[M N ] = size(FhdS);
% Check Number of File Header Strings
if M < 8
  Madd  = (8 - M);
  Mstrt = 8 - Madd + 1;
  Add   = Mstrt:(Mstrt + Madd - 1);
  for k = 1:length(Add)
    FhdS(Add(k),1:80) = blanks(80);
  end
elseif M > 8
  FhdS = FhdS(1:8,1:80);
end

[M N ] = size(FhdS);
% Check Caption String length
if (N > 80 | N < 80)
  FhdS = strpad_(FhdS,80);
end
%-------------------------------------
% FnmS
if isempty(FnmS)
  FnmS = blanks(12);
end
%-------------------------------------
% FdtS
if isempty(FdtS)
  FdtS = blanks(20);
end
%-------------------------------------
% Faux
if isempty(Faux)
  Faux = zeros(12,1);
  Faux(1) = 1;
  Faux(2) = 12;
elseif ~isempty(Faux)
  [MF NF] = size(Faux);
  if Faux(1) == 1
    Faux(2) = 12;
    if MF > 12
      Faux = Faux(1:12);
    elseif MF < 12
      Faux = [Faux; repmat(0,12,1)];
      Faux = Faux(1:12);
    end
  else
    MFsize = Faux(2);
    if MFsize < 12; MFsize = 12; Faux(2) = 12;end
    if MF < MFsize
      Madd  = (MFsize - MF);
      Mstrt = MFsize - Madd + 1;
      ER   = Mstrt:(Mstrt + Madd - 1);
      Faux(ER,1) = zeros(length(ER),1);
      Faux(2) = MF;
    elseif MF > MFsize
      Faux(2) = MF;
    end
  end
end
%--------------------------------------------------------------------------
function status = shorten_(NE)

% SHORTEN_ - Decrease number of data Elements in MLdbase Variables
%
% Decrease the size of existing Variables by the specified number of
% elements.
%
% Use As:  status = shorten(NE)
% Input:       NE = Number of Elements to remove from end of Variables
%                   IF NE equals zero, Variables are shortened by
%                   removing trailing NaN's such that the last Element
%                   in at least one Variable contains a finite value.
% Output:  status = returns 1 on success, 0 on failure
%
% Also See: LENGTHN_

% Copyright (c) 1996 by Moss Landing Marine Laboratories

% 15 Feb 1996; W. Broenkow
% 21 Aug 96; S. Flora - added checking if NE == Mpts

status = 1;

%mlglobal
global Mpts Nvar FnmS FhdS FdtS Faux Xdat Npts DscS SymS CapS Scl Fmt Vaux
FnmS = char(FnmS);FdtS = char(FdtS);FhdS = char(FhdS);DscS = char(DscS);CapS = char(CapS);SymS = char(SymS);  % Makes these variables a string

Nargs = nargin;
if Nargs == 0
  help shorten_
  return
end

%Load data if Xdat is empty
if isempty(Xdat);
  status = 0;
  disp('  SHORTEN_ No Data in Workspace')
  return
end

%-----------------------------------------------
[Mpts,Nvar] = size(Xdat);    % Updates Mpts and Nvar
%-----------------------------------------------
if Nargs < 1
  status = 0;
  disp('  SHORTEN_ Requires: Number of Elements to Remove')
  % return required so the program does not kick you out when
  % it doen't find NV
  return
end

% Remove extra NaN's
if NE == 0;
  % Find the number of NaN's to remove
  NE = Mpts-max(Npts);

  % If no extras to remove then return
  if NE <= 0 | Mpts == 1;
    status = 1;
    return
  end

  % If Xdat is filled with NaN's so that NE == Mpts then
  % leave one row of NaN's in Xdat, this way you will still
  % have the correct number of variables
  if NE >= Mpts
    NE = Mpts - 1;
  end
end
%-----------------------------------------------
% Check Number of Elements
[s, NE] =  chckint_('  SHORTEN_','NE',NE,1,Mpts-1,1);
if s == 1 & status == 0;
  status = 0;
elseif s == 0
  status = 0;
end

if status ==0;return;end
%*********************************************
% All error checking should be done before this line
% the following code will alter the MLDBASE data variables

Xdat = Xdat(1:(Mpts-NE),1:Nvar);

if isempty(Xdat);Xdat = NaN;end

chckpts_;       % Updates Mpts, Nvar and Npts

status = 1;




