function status = cdf2epic ( cdflist, wind_or_water, epic_key_location )
% CDF2EPIC:  list of netcdf ==> epic files
%
% USAGE:  status = cdf2epic ( cdflist, wind_or_water_flag, epic_key_location );
%
% EXAMPLE:  status = cdf2epic ( 'ptrfile', 'wind', '/usr/local/lib/epic.key' );
%
% PARAMETERS:
%   status:
%      -1 if the routine failed
%   cdflist:
%      list of pathnames of netcdf files to turn into epic
%   wind_or_water:
%      Some vector data, such as 'u_1' and 'v_1' can be interpreted
%      as either wind current data or water current data.  Supply
%      this parameter as either 'wind' or 'water'.
%   epic_key_location:
%      With a standard epic installation in unix systems, this should
%      be '/usr/local/lib/epic.key'.  Your situation may be different.

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;

if ( nargin < 1 )
    fprintf ( 1, 'Need at least two arguments.\n' );
    help cdf2epic;
    return;
end
if ( nargin < 2 )
    fprintf ( 1, 'Assuming vector data is water, not wind...\n' );
    wind_or_water = 'water';
    fprintf ( 1, 'Assuming location of epic.key file is ''/usr/local/lib/epic.key''.\n' );
    epic_key_location = '/usr/local/lib/epic.key';
end
if ( nargin < 3 )
    fprintf ( 1, 'Assuming location of epic.key file is ''/usr/local/lib/epic.key''.\n' );
    epic_key_location = '/usr/local/lib/epic.key';
end

get_epic_key_data ( epic_key_location );
get_valid_range_data;



if ( strcmp ( wind_or_water, 'wind' ) )
    epic_data.is_water = 0;
    epic_data.is_wind = 1;
elseif ( strcmp ( wind_or_water, 'water' ) )
    epic_data.is_water = 1;
    epic_data.is_wind = 0;
else
    help cdf2epic;
    return;
end


afid = fopen ( cdflist, 'r' );
if ( afid == -1 )
    fprintf ( 2, 'Could not open %s.\n', cdflist );
    return;
end


cdfname = fgetl ( afid );
while ( cdfname ~= -1 )
 
    status = single_cdf2epic ( cdfname );
    if ( status == -1 )
	fprintf ( 2, 'single_cdf2epic failed.\n' );
	return;
    end

    cdfname = fgetl ( afid );
    epic_data = [];
    if ( strcmp ( wind_or_water, 'wind' ) )
        epic_data.is_water = 0;
        epic_data.is_wind = 1;
    else 
        epic_data.is_water = 1;
        epic_data.is_wind = 0;
    end

end

fclose ( afid );
status = -1;

clear global epic_key_data epic_data valid_range_data;

return;

