function status = single_cdf2epic ( cdfname, wind_or_water )
% SINGLE_CDF2EPIC:  Takes a netcdf file that is in quasi-epic format ==> epic.
%
% USAGE:  status = single_cdf2epic ( cdfname, wind_or_water_flag );
%
% EXAMPLE:  status = single_cdf2epic ( 'a.cdf', 'wind' );

global epic_key_data;
global valid_range_data;

%
% Stuff everything we need into this.
global epic_data;

%
% epic_data fields
% ================
% dimid: 
%    array of netcdf dimids 
% ncid:
%    output file id 
% epic_filename:  
%    string, name of epic output file
% epic_global_atts:  
%    structure containing attributes name/value pairs for EPIC global
%    attributes
% epic_type:  
%    either 'profile' or 'time'
% input_filename:
%    Name of mat-file from whence the data comes.
% is_water:
%    1 if we are looking at water data.
% is_wind:
%    1 if we are looking at wind data.
% lat, lon:
%    should be scalar values for latitude and longitude.
% other_atts:  
%    structure array containing attributes name/value pairs for global
%    attributes that are NOT necessarily epic.
% time, time2:  
%    determine julian day
% variable:
%    structure array, contains dependent variable data.
%       name:  original name of variable
%       data:  original data of variable
%       epic_code:  epic code for this variable
%       other_atts:  structure array, non-epic attributes
%          name:  name of attribute
%          datatype:  netcdf data type
%          value:  attribute value


status = -1;

epic_data.input_filename = cdfname;

mexcdf ( 'setopts', 0 );

input_ncid = mexcdf('OPEN', cdfname, 'nowrite');
if ( input_ncid == -1 )
    fprintf ( 2, 'Could not open %s.\n', cdfname );
    return;
end


%
% Assume that if the time dimension has longer length
% than the depth dimension, the netcdf file is a time
% series file.  Otherwise assume profile.
[time_dimid, status] = mexcdf('DIMID', input_ncid, 'time');
if ( time_dimid == -1 )
    fprintf ( 2, 'Could not get time dimid from %s.\n', cdfname );
    return;
end
[depth_dimid, status] = mexcdf('DIMID', input_ncid, 'depth');
if ( time_dimid == -1 )
    fprintf ( 2, 'Could not get depth dimid from %s.\n', cdfname );
    return;
end
[dim_name, time_length, status] = mexcdf('DIMINQ', input_ncid, time_dimid);
if ( status == -1 )
    fprintf ( 2, 'Could not get time length from %s.\n', cdfname );
    return;
end
[dim_name, depth_length, status] = mexcdf('DIMINQ', input_ncid, depth_dimid);
if ( status == -1 )
    fprintf ( 2, 'Could not get depth length from %s.\n', cdfname );
    return;
end
if ( time_length > depth_length )
    epic_data.epic_type = 'time';
else
    epic_data.epic_type = 'profile';
end


%
% extract the data from the input file.

%
% dimensions first.
[epic_data.lat, status] = mexcdf('VARGET', input_ncid, 'lat', [0], [-1]);
if ( status == -1 )
    fprintf ( 2, 'Could not get ''lat'' out of %s.\n', epic_data.input_filename );
    return;
end
[epic_data.lon, status] = mexcdf('VARGET', input_ncid, 'lon', [0], [-1]);
if ( status == -1 )
    fprintf ( 2, 'Could not get ''lon'' out of %s.\n', epic_data.input_filename );
    return;
end
[epic_data.depth, status] = mexcdf('VARGET', input_ncid, 'depth', [0], [-1]);
if ( status == -1 )
    fprintf ( 2, 'Could not get ''depth'' out of %s.\n', epic_data.input_filename );
    return;
end
[epic_data.time, status] = mexcdf('VARGET', input_ncid, 'time', [0], [-1]);
if ( status == -1 )
    fprintf ( 2, 'Could not get ''time'' out of %s.\n', epic_data.input_filename );
    return;
end
[epic_data.time2, status] = mexcdf('VARGET', input_ncid, 'time2', [0], [-1]);
if ( status == -1 )
    fprintf ( 2, 'Could not get ''time2'' out of %s.\n', epic_data.input_filename );
    return;
end



%
% Now get the dependent variables
time_length = length(epic_data.time);
[ndims, nvars, ngatts, recdim, status] = mexcdf('INQUIRE', input_ncid);
if ( status == -1 )
    fprintf ( 2, 'Could not inquire into %s.\n', epic_data.input_filename );
    return;
end

epic_data.data = nan * zeros(time_length, nvars-2);

other_att_names = [ 'epic_code     '; ...
		    'name          '; ...
		    'long_name     '; ...
		    'generic_name  '; ...
		    'FORTRAN_format'; ...
		    'units         '; ...
		    'minimum       '; ...
		    'maximum       '; ...
		    'valid_range   ' ];
[num_att_names,c] = size(other_att_names);


%
% Get the name, data, and attributes of each dependent variable.
varcount = 0;
for varid = 0:nvars-1

    [varname, datatype, ndims, dim, natts, status] = mexcdf('VARINQ', input_ncid, varid );
    if ( status == -1 )
        fprintf ( 2, 'Could not inquire into varid %d in %s.\n', varid, epic_data.input_filename );
        return;
    end

    if ( ndims == 1 )
	;
    else
	varcount = varcount+1;
        epic_data.variable(varcount).name = varname;

        [vardata, status] = mexcdf('VARGET', input_ncid, varid, [0 0 0 0], [-1 -1 -1 -1]);
        epic_data.variable(varcount).data = vardata(:);

	[epic_data.variable(varcount).epic_code, status] = match_varname ( varname );
	if ( status == -1 )
	    fprintf ( 2, 'cdf2epic:  match_varname failed.\n' );
	    return;
	end

	%
	% get the attributes not listed in 'other_att_names'
        k = 0;
	other_atts = [];
        for attnum = 0:natts-1
        
            [attname, status] = mexcdf('ATTNAME', input_ncid, varid, attnum);
            if ( status == -1 )
                fprintf ( 2, '\tCouldn''t inquire into attnum %d for %s in %s.\n', attnum, varname, epic_data.input_filename );
                return;
            end
        
            match_found = 0;
            for epic_attnum = 1:num_att_names
                if ( strcmp(attname,deblank(other_att_names(epic_attnum,:))) )
                    match_found = 1;
                end
            end
        
            if ( match_found )
                ;
            else
                k = k + 1;
                other_atts(k).name = attname;
                [datatype, len, status] = mexcdf('ATTINQ', input_ncid, varid, attname);
                other_atts(k).datatype = datatype;
                [other_atts(k).value, status] = mexcdf('ATTGET', input_ncid, varid, attname);
            end
        
        end
        
        
        epic_data.variable(varcount).other_atts = other_atts;
        
    end

end


%
% do the global attributes.
attname = 'INST_TYPE';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.inst_type, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.inst_type = '';
end


attname = 'DATA_TYPE';
epic_global_atts.data_type = 'TIME';


attname = 'DATA_SUBTYPE';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.data_subtype, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.data_subtype = '';
end


attname = 'DATA_ORIGIN';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.data_origin, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.data_origin = '';
end


attname = 'COORD_SYSTEM';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.coord_system, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''GEOGRAPHICAL''\n', epic_data.input_filename, attname );
    epic_global_atts.coord_system = 'GEOGRAPHICAL';
end


attname = 'WATER_MASS';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.water_mass, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.water_mass = '';
end



attname = 'CONVENTIONS';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.conventions, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''EPIC-PMEL/TIME''\n', epic_data.input_filename, attname );
    epic_global_atts.conventions = 'EPIC-PMEL/TIME';
end


attname = 'CREATION_DATE';
epic_global_atts.creation_date = date;

%
% Now let's get the other required TIME_SERIES attributes.
attname = 'EXPERIMENT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.experiment, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.experiment = '';
end


attname = 'PROJECT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.project, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.project = '';
end

attname = 'MOORING';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.mooring, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.mooring = '';
end


attname = 'DELTA_T';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.delta_t, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    time = epic_data.time + epic_data.time2/8.64e7;
    delta_t = mean(diff(time))*24*60;
    epic_global_atts.delta_t = sprintf ( '%.4f minutes', delta_t );
    fprintf ( '\t%s:  no ''%s'' global attribute, computing as %s\n', epic_data.input_filename, attname, epic_global_atts.delta_t );
end

attname = 'COMPOSITE';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.composite, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''0''\n', epic_data.input_filename, attname );
    epic_global_atts.composite = 0;
end




attname = 'DATA_CMNT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.data_cmnt, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.data_cmnt = '';
end




attname = 'POS_CONSTANT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.pos_constant, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''0''\n', epic_data.input_filename, attname );
    epic_global_atts.pos_constant = 0;
end


attname = 'DEPTH_CONSTANT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.depth_constant, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''0''\n', epic_data.input_filename, attname );
    epic_global_atts.depth_constant = 0;
end


attname = 'WATER_DEPTH';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.water_depth, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''-99999''.\n', epic_data.input_filename, attname );
    epic_global_atts.water_depth = -99999;
end


attname = 'DESCRIPT';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.descript, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.descript = '';
end


attname = 'DRIFTER';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.drifter, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''0''\n', epic_data.input_filename, attname );
    epic_global_atts.drifter = 0;
end


attname = 'VAR_DESC';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.var_desc, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.var_desc = '';
end


attname = 'FILL_FLAG';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.fill_flag, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''0''\n', epic_data.input_filename, attname );
    epic_global_atts.fill_flag = 0;
end


attname = 'VAR_FILL';
[datatype, len, status] = mexcdf('ATTINQ', input_ncid, -1, attname);
if ( status ~= -1 )
    [epic_global_atts.var_fill, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
    if ( status == -1 )
	fprintf ( 2, 'What the hey?  ''attinq'' says that ''%s'' is there, but ''attget'' disagrees.\n', attname );
	return;
    end
else
    fprintf ( '\t%s:  no ''%s'' global attribute, setting to ''''\n', epic_data.input_filename, attname );
    epic_global_atts.var_fill = '';
end




epic_data.epic_global_atts = epic_global_atts;



%
% get any non-epic attributes
epic_atts = [ 'INST_TYPE    '; 
              'DATA_TYPE    ';
	      'DATA_SUBTYPE ';
	      'DATA_ORIGIN  ';
	      'COORD_SYSTEM ';
	      'WATER_MASS   ';
	      'CONVENTIONS  ';
	      'CREATION_DATE';
	      'EXPERIMENT   ';
	      'PROJECT      ';
	      'MOORING      ';
	      'DELTA_T      ';
	      'COMPOSITE    ';
	      'DATA_CMNT    ';
	      'POS_CONST    ';
	      'DEPTH_CONST  ';
	      'WATER_DEPTH  ';
	      'DESCRIPT     ';
	      'VAR_DESC     ';
	      'FILL_FLAG    ';
	      'VAR_FILL     ' ];
[num_epic_atts,c] = size(epic_atts);

	    
k = 0;
for attnum = 0:ngatts-1

    [attname, status] = mexcdf('ATTNAME', input_ncid, 'global', attnum);
    if ( status == -1 )
	fprintf ( 2, '\tCouldn''t inquire into attnum %d in %s.\n', attnum, epic_data.input_filename );
	return;
    end

    match_found = 0;
    for epic_attnum = 1:num_epic_atts
	if ( strcmp(attname,deblank(epic_atts(epic_attnum,:))) )
            match_found = 1;
	end
    end

    if ( match_found )
	;
    else
	k = k + 1;
	other_atts(k).name = attname;
	[other_atts(k).value, status] = mexcdf('ATTGET', input_ncid, 'global', attname);
	[other_atts(k).datatype, len, status] = mexcdf('ATTINQ', input_ncid, 'global', attname);
    end

end


epic_data.other_atts = other_atts;






%
% Determine the output filename.
[prefix, suffix] = strtok ( epic_data.input_filename, '.' );
epic_data.epic_filename = sprintf ( '%s_epic.cdf', prefix );


status = write_epic;
if ( status == -1 )
    fprintf ( 2, 'Could not inquire into %s.\n', epic_data.input_filename );
    return;
end


mexcdf ( 'close', input_ncid );

fprintf ( 1, '\n\n\n' );
status = 1;
return;

