function ecohab2epic()

global epic_key_data;

%
% Stuff everything we need into this.
global epic_data;
global valid_range_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

get_epic_key_data ( '/usr/local/lib/epic.key' );
get_valid_range_data;


afid = fopen ( 'matlist', 'r' );

matfile = fgetl ( afid );
while ( matfile ~= -1 )
    
    epic_data = [];

    fprintf ( 1, 'processing %s.\n',matfile );
    epic_data.input_filename = matfile;

    input_data = load ( matfile );
    input_data = input_data.cruise;

    number_of_profiles = length(input_data.lat);
    number_of_depths = length(input_data.z);

    %
    % Go thru the 'input_data' structure and collect the names
    % of all the data with the right dimensions.  These are the
    % ones whose contents will get put into the output file.
    epic_data.varnames = [];
    all_names = fieldnames ( input_data );
    num_names = size(all_names,1);
    kept_name_index = 0;
    for j = 1:num_names
	field_size = size(getfield(input_data,all_names{j}));
	if ( (field_size(1) == number_of_depths) & (field_size(2) == number_of_profiles) )
	    kept_name_index = kept_name_index+1;
	    epic_data.variable(kept_name_index).name = all_names{j};
	end
    end


    %
    % match up the names with epic codes
    for j = 1:length(epic_data.variable)
	[epic_data.variable(j).epic_code, status] = match_varname ( epic_data.variable(j).name );
	if ( status == -1 )
	    fprintf ( 2, 'mat2prof:  match_varname failed.\n' );
	    return;
	end
    end

    %
    % Use the prefix to construct the name of each output file.
    [prefix,suffix] = strtok ( matfile, '.' );


    %
    % Loop thru each profile, writing an epic file for each.
    for pr_i = 1:number_of_profiles

	%pr_i

	%
	% construct the name of the output netcdf file.
	epic_data.epic_filename = sprintf ( '%s_c%03d.cdf', prefix, pr_i );

	%
	% Mark the file type as 'profile', as opposed to 'time series'.
	epic_data.epic_type = 'profile';

	epic_data.lat = input_data.lat(pr_i);
	epic_data.lon = input_data.lon(pr_i);

	%
	% force the longitude to be negative
	if ( epic_data.lon > 0 )
	    epic_data.lon = epic_data.lon * -1;
	end

	%
	% determine the time of this profile.
	% 'datest' contains the date as 'yymmdd' - integer form.
	% 'time' has the hour from the start of the day.
	the_time = julian(input_data.time(pr_i,:) );
	epic_data.time = floor(the_time);
	epic_data.time2 = floor((the_time - epic_data.time)*8.64e7);

	%
	% calculate the depth vector.
	% I assume that 'temp' is always present.
	naninds = find(isnan(input_data.temp(:,pr_i)));
	fininds = find(isfinite(input_data.temp(:,pr_i)));
	num_depths = length(input_data.z);
	if ( isempty(naninds) | isempty(fininds) )
	    epic_data.depth = input_data.z;
	else
	    epic_data.depth = input_data.z(1:fininds(length(fininds)));
	end
	epic_data.depth = epic_data.depth(:);
	depth_size = length(epic_data.depth);


	%
	% collect up the data for this profile.
	num_varnames = length(epic_data.variable);
	for j = 1:num_varnames
	    epic_data.variable(j).data = getfield ( input_data, epic_data.variable(j).name, {1:depth_size,pr_i} );
	    epic_data.variable(j).other_atts = [];
	end

	%
	% store the global attributes
	epic_global_atts.cruise = prefix;
	epic_global_atts.data_type = 'CTD';
	epic_global_atts.data_subtype = 'CTD';
	epic_global_atts.inst_type = '';
	epic_global_atts.data_origin = 'USGS/WHFC';
	epic_global_atts.coord_system = 'GEOGRAPHICAL';
	epic_global_atts.water_mass = '';
	epic_global_atts.cast = sprintf ( '%d', pr_i );
	epic_global_atts.bottle = 0;
	epic_global_atts.weather = '';
	epic_global_atts.sea_state = '';
	epic_global_atts.barometer= -99999;
	epic_global_atts.data_cmnt= '';
	epic_global_atts.wind_dir= -99999;
	epic_global_atts.wind_speed=-99999;
	epic_global_atts.visibility= '';
	epic_global_atts.cloud_type = '';
	epic_global_atts.cloud_amount = '';
	epic_global_atts.air_temp= -99999;
	epic_global_atts.wet_bulb= -99999;
	epic_global_atts.water_depth = input_data.waterdepth(pr_i);
	epic_global_atts.conventions = 'PMEL-EPIC/CTD';
	epic_global_atts.creation_date= date;
	epic_data.epic_global_atts = epic_global_atts;


        ind = 0;
        ind = ind+1;
        other_atts(ind).name = 'latitude';
	other_atts(ind).datatype = 'float';
	other_atts(ind).value =  epic_data.lat;

        ind = ind+1;
        other_atts(ind).name = 'longitude';
	other_atts(ind).datatype = 'float';
	other_atts(ind).value =  epic_data.lon;

        ind = ind+1;
        other_atts(ind).name = 'time';
	other_atts(ind).datatype = 'char';
	other_atts(ind).value = datestr ( jd2datenum(the_time), 0 );

        ind = ind+1;
        other_atts(ind).name = 'descript';
	other_atts(ind).datatype = 'char';
	other_atts(ind).value = input_data.descript;

        if ( isfield(input_data,'comment') )
            ind = ind+1;
            other_atts(ind).name = 'comment';
            other_atts(ind).datatype = 'char';
            value = input_data.comment(1,:);
            for jj = 2:size(input_data.comment,1)
                value = strcat ( value, ' ', input_data.comment(jj,:) );
            end
            other_atts(ind).value = value;
	end

	epic_data.other_atts = other_atts;
        ind = ind+1;
        other_atts(ind).name = 'history';
	other_atts(ind).datatype = 'char';
	other_atts(ind).value = sprintf ( '\n%s:  Converted to EPIC by ''ecohab2epic.m''\n', date );

        ind = ind+1;
        other_atts(ind).name = 'water_depth';
	other_atts(ind).datatype = 'float';
	other_atts(ind).value = input_data.waterdepth(pr_i);

        ind = ind+1;
        other_atts(ind).name = 'station_number';
	other_atts(ind).datatype = 'long';
	other_atts(ind).value = input_data.stanum(pr_i);

        ind = ind+1;
        other_atts(ind).name = 'station_id';
	other_atts(ind).datatype = 'char';
	other_atts(ind).value = input_data.staid(pr_i,:);

        epic_data.other_atts = other_atts;

	write_epic;




    end



    matfile = fgetl ( afid );

end
