function [ID,cretime,creator,destin,gdfver,gptver,destid,dummy1,dummy2] = gdfreadmainhead(fid,gdfnamelen,gdfid)
%
% function [ID,cretime,creator,destin,gdfver,gptver,destid,dummy1,dummy2]
%               = gdfreadmainhead(fid,gdfnamelen,gdfid)
%
%    GDFREADMAINHEAD - Read out main data header from binary GDF file
%
%    gdfreadmainhead(fid)
%
%    [ID, cretime, creator, destin, gdfver, gptver, destid, dummy1, dummy2] = gdfreadmainhead(fid)
%
%    [...] = gdfreadmainhead(fid,gdfnamelen)
%
%    [...] = gdfreadmainhead(fid,gdfnamelen,gdfid)
%
%    GDFREADMAINHEAD reads out the data at the start of a binary GDF data
%    file produced by GPT.
%
%    gdfreadmainhead(fid) - read the header data from the GDF file with
%    file ID FID and display it.  The file must previously have been opened
%    with the FOPEN command to produce the valid FID.
%
%    [ID, cretime, creator, destin, gdfver, gptver, destid, dummy1, dummy2]
%    = gdfreadmainhead(fid) - read out the header data and output each
%    piece of data as a separate variable.  The output variables are:
%
%        ID - GDF file ID; should be ID = 94325877 if the file is a valid
%            GDF file.
%        cretime - file creation date/time string
%        creator - app that created the file: should be GPT.
%        destin - destination.
%        gdfver - version of GDF-software.
%        gptver - version of creator (GPT).
%        destver - version of destination.
%        dummy1 - alignment (on 32-bit boundary).
%        dummy2 - empty dummy variable.
%
%    [...] = gdfreadmainhead(fid,gdfnamelen) - specify length of character
%    strings with GDFNAMELEN.  Default value is GDFNAMELEN = 16.
%
%    [...] = gdfreadmainhead(fid,gdfnamelen,gdfid) - also specify GDF file
%    ID.  Default value is ID = 94325877.
%
%    The header data has a standard structure, with the following format:
%
%    struct gdfmainhead
%
%    U32 ID ;		     /* ID so you can see that its a GDF  */
%    U32 cretime ; 	     /* Creation time			  */
%    U8  creator[GDFNAMELEN] ;  /* Creator of the DataFile 	  */
%    U8  destin[GDFNAMELEN] ;   /* Destination, "" means "General"   */
%
%    U8  gdfmaj ;		     /* Major version of GDF-software	  */
%    U8  gdfmin ;		     /* Minor version of GDF-software	  */
%
%    U8  cremaj ;		     /* Major version of creator	  */
%    U8  cremin ;		     /* Minor version of creator	  */
%    U8  desmaj ;		     /* Major version of destination or 0 */
%    U8  desmin ;		     /* Minor version of destination or 0 */
%    U8  dummy1 ;		     /* Alignment (on 32-bit boundary)    */
%    U8  dummy2 ;
%

% File is the property of Simon Jolly, Imperial College London.  File
% released under the GNU public license.
%
% File history
%
%   26-Jun-2008 S. Jolly
%       Full read-in of GPT .gdf file and conversion to Matlab format.
%
%======================================================================

    if nargin < 1
        error('Too few input variables: syntax is [...] = gdfreadmainhead(fid)') ;
    elseif nargin > 3
        error('Too many input variables: syntax is [...] = gdfreadmainhead(fid,gdfnamelen,gdfid)') ;
    elseif nargout > 9
        error('Too many output variables: syntax is [ID, cretime, creator, destin, gdfver, gptver, destid, dummy1, dummy2] = gdfreadmainhead(fid)') ;
    end

    if nargin < 3 || isempty(gdfid)
        gdfid = 94325877 ;
    end

    if nargin < 2 || isempty(gdfnamelen)
        gdfnamelen = 16 ;
    end

%    Check to see if the file pointer is at the start of the file

    position = ftell(fid) ;

    if position ~= 0
        warning('GDFREADMAINHEAD:notFileStart', ...
            ['File pointer is not at the start of the GDF file: seeking to file start...']) ;
        werenotatthestartofthefile = 1 ;
        status = fseek(fid, 0, 'bof') ;
    else
        werenotatthestartofthefile = 0 ;
    end

%    Read in main file header

    ID = fread(fid, 1, '*uint32') ;
    cretime = datestr(((fread(fid, 1, 'uint32=>double'))./60./60./24)+(365.*1970)+479) ;

    creatchar = fread(fid, gdfnamelen, '*uint8') ;
    [wordend] = find(creatchar == 0) ;
    creator = char(creatchar(1:wordend(1)-1))' ;

    destchar = fread(fid, gdfnamelen, '*uint8') ;
    [wordend] = find(destchar == 0) ;
    destin = char(destchar(1:wordend(1)-1))' ;

    if isempty(destin)
        destin = 'General' ;
    end

    gdfmaj = fread(fid, 1, 'uint8=>double') ;
    gdfmin = fread(fid, 1, 'uint8=>double') ;
    gdfver = gdfmaj + (gdfmin./100) ;

    cremaj = fread(fid, 1, 'uint8=>double') ;
    cremin = fread(fid, 1, 'uint8=>double') ;
    gptver = cremaj + (cremin./100) ;

    desmaj = fread(fid, 1, 'uint8=>double') ;
    desmin = fread(fid, 1, 'uint8=>double') ;
    destid = desmaj + (desmin./100) ;

    dummy1 = fread(fid, 1, '*uint8') ;
    dummy2 = fread(fid, 1, '*uint8') ;

%    If there are no output arguments, display the file header data

%    if nargout == 0
        disp(['GDF-version: ' num2str(gdfver)]) ;
        disp(['Creator    : ' creator]) ;
        disp(['  version  : ' num2str(gptver,'%3.2f')]) ;
        disp(['At         : ' cretime]) ;
        disp(['Binary mode: Compatible machine']) ;
        disp(['Destination: ' destin]) ;
%    end

%    Check to see if the file ID is correct

    if ID ~= gdfid
        warning('GDFREADMAINHEAD:idMismatch', ...
            ['File ID is not GPT: should be ' num2str(gdfid) ', is ' num2str(ID) '.']) ;
    end

%    Reset position of file pointer if it was moved

    if werenotatthestartofthefile
        status = fseek(fid, position, 'bof') ;
    end

    return
