function status = write_epic()
% WRITE_EPIC:  writes epic file

global epic_data;
global epic_key_data;


status = -1;

mexcdf ( 'setopts', 0 );

fprintf ( 1, '%s ==> %s...\n', epic_data.input_filename, epic_data.epic_filename );

[ncid, status] = mexcdf ( 'create', epic_data.epic_filename, 'clobber' );
epic_data.ncid = ncid;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  Could not create %s.\n', epic_data.epic_filename );
    return;
end

status = write_global_atts;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_global_atts'' failed\n' );
    return;
end
status = write_dimension_defs;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_dimension_defs'' failed\n' );
    return;
end
status = write_coordinate_var_defs;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_coordinate_var_defs'' failed\n' );
    return;
end
status = write_coordinate_var_data;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_coordinate_var_data'' failed\n' );
    return;
end
status = write_coordinate_var_atts;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_coordinate_var_atts'' failed\n' );
    return;
end
status = write_data_vars;
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  ''write_data_vars'' failed\n' );
    return;
end



status = mexcdf ( 'close', epic_data.ncid );
if ( status == -1 )
    fprintf ( 2, 'write_outfile:  Could not close %s.\n', epicfile );
    return;
end


%
% If we got this far, all must be ok.
status = 1;
return;







%
% This function writes the required epic global attributes and
% copies any other global attributes over as well.
function status = write_global_atts
%disp ( sprintf ( '\twrite_global_atts:') );

global epic_data;

%
% first do the attributes required for all epic programs.
% INST_TYPE, DATA_TYPE, DATA_SUBTYPE, DATA_ORIGIN, COORD_SYSTEM
attname = 'INST_TYPE';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.inst_type), ...
		   epic_data.epic_global_atts.inst_type );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end

attname = 'DATA_TYPE';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.data_type), ...
		   epic_data.epic_global_atts.data_type );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end

attname = 'DATA_SUBTYPE';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.data_subtype), ...
		   epic_data.epic_global_atts.data_subtype );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end


attname = 'DATA_ORIGIN';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.data_origin), ...
		   epic_data.epic_global_atts.data_origin );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end

attname = 'COORD_SYSTEM';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.coord_system), ...
		   epic_data.epic_global_atts.coord_system );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end

attname = 'CREATION_DATE';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.creation_date), ...
		   epic_data.epic_global_atts.creation_date );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end


attname = 'CONVENTIONS';
status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
		   length(epic_data.epic_global_atts.conventions), ...
		   epic_data.epic_global_atts.conventions );
if ( status == -1 )
    fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
    return;
end


%
% Time Series Attributes
if ( strcmp ( epic_data.epic_type, 'time' ) )

    attname = 'EXPERIMENT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		   length(epic_data.epic_global_atts.experiment), ...
    		   epic_data.epic_global_atts.experiment );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end

    attname = 'PROJECT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		   length(epic_data.epic_global_atts.project), ...
    		   epic_data.epic_global_atts.project );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end

    attname = 'MOORING';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		   length(epic_data.epic_global_atts.mooring), ...
    		   epic_data.epic_global_atts.mooring );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end

    attname = 'DELTA_T';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
                   length(epic_data.epic_global_atts.delta_t), ...
    		   epic_data.epic_global_atts.delta_t );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end

    attname = 'COMPOSITE';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.composite), ...
    		   epic_data.epic_global_atts.composite );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'DATA_CMNT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
                   length(epic_data.epic_global_atts.data_cmnt), ...
    		   epic_data.epic_global_atts.data_cmnt );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'POS_CONST';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.pos_constant), ...
    		   epic_data.epic_global_atts.pos_constant );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'DEPTH_CONST';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.depth_constant), ...
    		   epic_data.epic_global_atts.depth_constant );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'WATER_DEPTH';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.water_depth), ...
    		   epic_data.epic_global_atts.water_depth );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'DESCRIPT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
                   length(epic_data.epic_global_atts.descript), ...
    		   epic_data.epic_global_atts.descript );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'DRIFTER';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.drifter), ...
    		   epic_data.epic_global_atts.drifter );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'VAR_DESC';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
                   length(epic_data.epic_global_atts.var_desc), ...
    		   epic_data.epic_global_atts.var_desc );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end

    attname = 'FILL_FLAG';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
                   length(epic_data.epic_global_atts.fill_flag), ...
    		   epic_data.epic_global_atts.fill_flag );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'VAR_FILL';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
                   length(epic_data.epic_global_atts.var_fill), ...
    		   epic_data.epic_global_atts.var_fill );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end



end


%
% 
if ( strcmp ( epic_data.epic_type, 'profile' ) )

    attname = 'CRUISE';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		   length(epic_data.epic_global_atts.cruise), ...
    		   epic_data.epic_global_atts.cruise );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'CAST';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		   length(epic_data.epic_global_atts.cast), ...
    		   epic_data.epic_global_atts.cast );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'BOTTLE';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
    		   1, epic_data.epic_global_atts.bottle );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'WEATHER';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.weather), ...
    		       epic_data.epic_global_atts.weather );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'SEA_STATE';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.sea_state), ...
    		       epic_data.epic_global_atts.sea_state );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'BAROMETER';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
    		       1, epic_data.epic_global_atts.barometer );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'DATE_CMNT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.data_cmnt), ...
    		       epic_data.epic_global_atts.data_cmnt );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end



    attname = 'WIND_DIR';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
    		       1, epic_data.epic_global_atts.wind_dir );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end



    attname = 'WIND_SPEED';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
    		       1, epic_data.epic_global_atts.wind_speed );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'VISIBILITY';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.visibility), ...
    		       epic_data.epic_global_atts.visibility );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'CLOUD_TYPE';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.cloud_type), ...
    		       epic_data.epic_global_atts.cloud_type );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'CLOUD_AMOUNT';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'char', ...
    		       length(epic_data.epic_global_atts.cloud_amount), ...
    		       epic_data.epic_global_atts.cloud_amount );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'AIR_TEMP';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'float', ...
    		       1, epic_data.epic_global_atts.air_temp );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'WET_BULB';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'float', ...
    		       1, epic_data.epic_global_atts.wet_bulb );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


    attname = 'WATER_DEPTH';
    status = mexcdf ( 'attput', epic_data.ncid, 'global', attname, 'long', ...
    		       1, epic_data.epic_global_atts.water_depth );
    if ( status == -1 )
        fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attname );
        return;
    end


end




%
% OK, now do any other attributes.
%num_other_atts = length ( epic_data.other_atts );
%if ( num_other_atts > 0 )
%    attnames = fieldnames ( epic_data.other_atts );
%    for i = 1:length(attnames)
%    
%        attvalue = getfield ( epic_data.other_atts, attnames{i} );
%        if ( ischar(attvalue) )
%    
%            status = mexcdf ( 'attput', epic_data.ncid, 'global', attnames{i}, 'char', ...
%            		       length(attvalue), attvalue );
%            if ( status == -1 )
%                fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attnames{i} );
%                return;
%            end
%    
%        else
%    
%            status = mexcdf ( 'attput', epic_data.ncid, 'global', attnames{i}, 'float', ...
%            		       length(attvalue), attvalue );
%            if ( status == -1 )
%                fprintf ( 2, 'write_global_atts:  couldn''t write %s attribute.\n', attnames{i} );
%                return;
%            end
%    
%        end
%    
%    end
%end
num_other_atts = length ( epic_data.other_atts );
if ( num_other_atts > 0 )
    for i = 1:num_other_atts
    
    
        status = mexcdf ( 'attput', epic_data.ncid, 'global', ...
                           epic_data.other_atts(i).name, epic_data.other_atts(i).datatype, ...  
			   length(epic_data.other_atts(i).value), ...  
			   epic_data.other_atts(i).value );
        if ( status == -1 )
            status = mexcdf ( 'attput', epic_data.ncid, 'global', ...  
			      'dud', epic_data.other_atts(i).datatype, ...  
			      length(epic_data.other_atts(i).value), ...  
			      epic_data.other_atts(i).value );
            if ( status == -1 ) 
		fprintf ( 2, 'write_global_atts:  really tried, but couldn''t write %s attribute.\n', epic_data.other_atts(i).name ); 
		return;
            end

            status = mexcdf ( 'attrename', epic_data.ncid, 'global', ...  
			      'dud', epic_data.other_atts(i).name ); 
            if ( status == -1 )
                fprintf ( 2, 'write_global_atts:  couldn''t rename dud attribute as %s.\n', epic_data.other_atts(i).name );
                return;
            end

		
        end
    
    end
end



status = 1;
return;




%
% Define the dimensions of the new file.  They will be
% "time", "lat", "lon", and "depth".  There will be only
% one depth dimension here, whereas in the input file,
% there may have been many.
function status = write_dimension_defs

global epic_data;


status = mexcdf('dimdef', epic_data.ncid, 'time', length(epic_data.time) );
if ( status == -1 )
    fprintf ( 2, 'write_dimension_defs:  could not define time in output cdf file.\n' );
    return;
end

epic_data.dimid(1) = status;




status = mexcdf('dimdef', epic_data.ncid, 'depth', length(epic_data.depth));
if ( status == -1 )
    fprintf ( 2, 'write_dimension_defs:  could not define depth in output cdf file.\n' );
    return;
end

epic_data.dimid(2) = status;




status = mexcdf('dimdef', epic_data.ncid, 'lat', [1]);
if ( status == -1 )
    fprintf ( 2, 'write_dimension_defs:  could not define lat in output cdf file.\n' );
    return;
end

epic_data.dimid(3) = status;




status = mexcdf('dimdef', epic_data.ncid, 'lon', [1]);
if ( status == -1 )
    fprintf ( 2, 'write_dimension_defs:  could not define lat in output cdf file.\n' );
    return;
end

epic_data.dimid(4) = status;



return;










%
% Now define the coordinate variables.  These are
% time, time2, depth, lat, and then lon.  Well,
% "time2" really isn't a coordinate variable, but
% let's define it now anyway.
function status = write_coordinate_var_defs

global epic_data;

status = mexcdf('vardef', epic_data.ncid, 'time', 'long', 1, epic_data.dimid(1) );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_defs:  Could not define time.\n' );
    return;
end

status = mexcdf('vardef', epic_data.ncid, 'time2', 'long', 1, epic_data.dimid(1) );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_defs:  Could not define time2.\n' );
    return;
end

status = mexcdf('vardef', epic_data.ncid, 'depth', 'float', 1, epic_data.dimid(2) );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_defs:  Could not define depth.\n' );
    return;
end

status = mexcdf('vardef', epic_data.ncid, 'lat', 'float', 1, epic_data.dimid(3) );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_defs:  Could not define lat.\n' );
    return;
end

status = mexcdf('vardef', epic_data.ncid, 'lon', 'float', 1, epic_data.dimid(4) );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_defs:  Could not define lon.\n' );
    return;
end



return;





%
% Extract the lat, lon, depth, and time data from
% the 'epic_data' structure and write to the output file.
function status = write_coordinate_var_data

%disp ( sprintf ( '\twrite_coordinate_var_data:') );

global epic_data;

status = mexcdf ( 'endef', epic_data.ncid );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not end def stage on ouput file.\n' );
    return;
end

[output_varid, status] = mexcdf('varid', epic_data.ncid, 'time');
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not get time varid from output file.\n' );
    return;
end


status = mexcdf('varput', epic_data.ncid, output_varid, [0], [length(epic_data.time)], epic_data.time);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not put time data input output file.\n' );
    return;
end




%
% time2
[output_varid, status] = mexcdf('varid', epic_data.ncid, 'time2');
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not get time2 varid from output file.\n' );
    return;
end


status = mexcdf('varput', epic_data.ncid, output_varid, [0], [size(epic_data.time,1)], epic_data.time2);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not put time2 data into output file.\n' );
    return;
end


%
% depth
[output_varid, status] = mexcdf('varid', epic_data.ncid, 'depth');
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not get output depth varid.\n' );
    return;
end

status = mexcdf('varput', epic_data.ncid, output_varid, [0], [size(epic_data.depth,1)], epic_data.depth);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not put depth data into output file.\n' );
    return;
end


%
% lat
[output_varid, status] = mexcdf('varid', epic_data.ncid, 'lat');
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not get lat varid from output file.\n' );
    return;
end


status = mexcdf('varput', epic_data.ncid, output_varid, [0], [1], epic_data.lat);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not put lat data into output file.\n' );
    return;
end



%
% lon
[output_varid, status] = mexcdf('varid', epic_data.ncid, 'lon');
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not get lon varid from output file.\n' );
    return;
end

status = mexcdf('varput', epic_data.ncid, output_varid, [0], [1], epic_data.lon);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_data:  Could not put lon data into output file.\n' );
    return;
end


return;








%
% Writes out epic compliant attributes for coordinate variables.
function status = write_coordinate_var_atts

%disp ( sprintf ( '\twrite_coordinate_var_atts:') );
global epic_data;


status = mexcdf ( 'redef', epic_data.ncid );
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put output file back into define mode.\n' );
    return;
end


%
% time
varname = 'time';
[varid, status] = mexcdf('VARID', epic_data.ncid, varname);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not get time varid from output file.\n' );
    return;
end

attname = 'FORTRAN_format';
attvalue = 'F10.2';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end

attname = 'units';
attvalue = 'True Julian Day';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'type';
attvalue = 'EVEN';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'epic_code';
attvalue = 624;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%d'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end




%
% time2
varname = 'time2';
[varid, status] = mexcdf('VARID', epic_data.ncid, varname);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not get %s varid from output file.\n', varname );
    return;
end

attname = 'FORTRAN_format';
attvalue = 'F10.2';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end

attname = 'units';
attvalue = 'msec since 0:00 GMT';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'type';
attvalue = 'EVEN';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'epic_code';
attvalue = 624;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%d'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end




%
% depth
varname = 'depth';
[varid, status] = mexcdf('VARID', epic_data.ncid, varname);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not get %s varid from output file.\n', varname );
    return;
end

attname = 'FORTRAN_format';
attvalue = 'F10.2';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end

attname = 'units';
attvalue = 'm';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'type';
attvalue = 'EVEN';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'epic_code';
attvalue = 3;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%d'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end




%
% lat
varname = 'lat';
[varid, status] = mexcdf('VARID', epic_data.ncid, varname);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not get %s varid from output file.\n', varname );
    return;
end

attname = 'FORTRAN_format';
attvalue = 'F10.2';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end

attname = 'units';
attvalue = 'degrees_north';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'type';
attvalue = 'EVEN';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'epic_code';
attvalue = 500;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%d'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end



%
% lon
varname = 'lon';
[varid, status] = mexcdf('VARID', epic_data.ncid, varname);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not get %s varid from output file.\n', varname );
    return;
end

attname = 'FORTRAN_format';
attvalue = 'F10.2';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end

attname = 'units';
attvalue = 'degrees_west';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'type';
attvalue = 'EVEN';
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%s'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end


attname = 'epic_code';
attvalue = 502;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_coordinate_var_atts:  Could not put ''%d'' as ''%s'' for %s into input file.\n', attvalue, attname, varname );
    return;
end







return;

















%
% Write out the dependent variables. 
function status = write_data_vars
 
global epic_data epic_key_data valid_range_data;

%disp ( sprintf ( '\twrite_data_vars:') );


%
% end define stage first
% Then go back to define mode to handle first variable.
status = mexcdf ( 'endef', epic_data.ncid );
if ( status == -1 )
    fprintf ( 2, 'write_data_vars:  could not end define stage for output file.\n' );
    return;
end

for i = 1:length(epic_data.variable)

    status = mexcdf ( 'redef', epic_data.ncid );
    if ( status == -1 )
        fprintf ( 2, 'write_data_vars:  could not enter define stage for output file.\n' );
        return;
    end

    ind = find(epic_key_data.epic_code == epic_data.variable(i).epic_code);
    if ( isempty(ind) )
	fprintf ( 2, 'write_data_vars:  could not match up epic codes.\n' );
	status = -1;
	return;
    end

    output_varname = sprintf ( '%s_%d', deblank(epic_key_data.short_name{ind}), epic_data.variable(i).epic_code );

    fprintf ( 1, '\t\tinput name %s ==> epic name %s\n', epic_data.variable(i).name, output_varname );

    status = mexcdf('VARDEF', epic_data.ncid, output_varname, 'float', 4, epic_data.dimid );
    if ( status == -1 )
	fprintf ( 2, 'write_data_vars:  Could not define %s in output file.\n', output_varname );
	return;
    end

    out_varid = status;
    status = write_epic_var_atts ( out_varid, epic_data.variable(i).epic_code, i );
    if ( status == -1 )
	fprintf ( 2, 'write_data_vars:  ''write_epic_var_atts'' failed.\n' );
	return;
    end


    %
    % write the min, max, and valid_range attributes
    attname = 'minimum';
    attvalue = gmin(epic_data.variable(i).data);
    if ( isempty(attvalue) )
	attvalue = 1e35;
    end
    attlen = 1;
    status = mexcdf('ATTPUT', epic_data.ncid, out_varid, attname, 'float', attlen, attvalue);
    if ( status == -1 )
        fprintf ( 2, 'write_data_vars:  could not write %s attribute for varid %d in output file.\n', attname, varid );
        return;
    end

    attname = 'maximum';
    attvalue = gmax(epic_data.variable(i).data);
    if ( isempty(attvalue) )
	attvalue = -1e35;
    end
    attlen = 1;
    status = mexcdf('ATTPUT', epic_data.ncid, out_varid, attname, 'float', attlen, attvalue);
    if ( status == -1 )
        fprintf ( 2, 'write_data_vars:  could not write %s attribute for varid %d in output file.\n', attname, varid );
        return;
    end

    attname = 'valid_range';
    attvalue = [valid_range_data.low(epic_data.variable(i).epic_code), valid_range_data.high(epic_data.variable(i).epic_code) ];
    attlen = 2;
    status = mexcdf('ATTPUT', epic_data.ncid, out_varid, attname, 'float', attlen, attvalue);
    if ( status == -1 )
        fprintf ( 2, 'write_data_vars:  could not write %s attribute for varid %d in output file.\n', attname, varid );
        return;
    end




    %
    % end define stage so we can write the data.
    % Then go back to define mode to handle next variable.
    status = mexcdf ( 'endef', epic_data.ncid );
    if ( status == -1 )
	fprintf ( 2, 'write_data_vars:  could not end define stage for output file.\n' );
	return;
    end
    
    datum = epic_data.variable(i).data;
    ind = find(isnan(datum));
    datum(ind) = 1e35*ones(size(ind));
    status = mexcdf('VARPUT', epic_data.ncid, out_varid, [0 0 0 0], [-1 -1 -1 -1], datum );
    if ( status == -1 )
        fprintf ( 2, 'write_data_vars:  could not put data into varid %d in output file.\n', out_varid );
        return;
    end


end % for varid

return;











%
% Write the particular epic attributes for the
% dependent variables.
function status = write_epic_var_atts ( varid, epcode, k )
%disp ( 'write_epic_var_atts:  ' );

global epic_data epic_key_data;

ind = find(epic_key_data.epic_code == epcode);
if ( isempty(ind) )
    fprintf ( 2, 'write_data_vars:  could not match up epic codes.\n' );
    status = -1;
    return;
end

attname = 'epic_code';
attvalue = epcode;
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'long', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


attname = 'name';
attvalue = epic_key_data.short_name{ind};
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


attname = 'long_name';
attvalue = epic_key_data.long_name{ind};
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


attname = 'generic_name';
attvalue = epic_key_data.generic_name{ind};
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


attname = 'FORTRAN_format';
attvalue = epic_key_data.fortran_format{ind};
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


attname = 'units';
attvalue = epic_key_data.units{ind};
attlen = length(attvalue);
status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, 'char', attlen, attvalue);
if ( status == -1 )
    fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
    return;
end


%
% write out all the others
for i = 1:length(epic_data.variable(k).other_atts)
    attname = epic_data.variable(k).other_atts(i).name;
    atttype = epic_data.variable(k).other_atts(i).datatype;
    attvalue = epic_data.variable(k).other_atts(i).value;
    attlen = length(attvalue);
    status = mexcdf('ATTPUT', epic_data.ncid, varid, attname, atttype, attlen, attvalue);
    if ( status == -1 )
        fprintf ( 2, 'write_epic_var_atts:  could not write %s attribute for varid %d in output file.\n', attname, varid );
        return;
    end

end



return;














function xnew=gmin(x)
%function xnew=gmin(x)
% just like min, except that it skips over bad points
[imax,jmax]=size(x);

for j=1:jmax
       good=find(finite(x(:,j)));
       if length(good)>0
          xnew(j)=min(x(good,j));
       else
          xnew(j)=NaN;
       end
end




function xnew=gmax(x)
%function xnew=gmax(x)
% just like max, except that it skips over bad points
[imax,jmax]=size(x);

for j=1:jmax
       good=find(finite(x(:,j)));
       if length(good)>0
          xnew(j)=max(x(good,j));
       else
          xnew(j)=NaN;
       end
end
