function [new_data, new_meta_data, header, append, data, meta_data, time_esecs] ...
    = nc_to_ascii (nc_directory, mat_directory, nc_filename)

thispath = fileparts(mfilename('fullpath'));
if(isempty (strfind(path, 'mexnc'))),
    path(path,[thispath '/../mexnc/']);
end
if(isempty (strfind(path, 'netcdf_toolbox'))),
    path(path,[thispath '/../netcdf_toolbox/netcdf/']);
    path(path,[thispath '/../netcdf_toolbox/netcdf/ncsource']);    
    path(path,[thispath '/../netcdf_toolbox/netcdf/nctype']);
    path(path,[thispath '/../netcdf_toolbox/netcdf/ncutility']);
end

mid = fopen([mat_directory nc_filename '.mat']);
if(mid ~= -1),
    load ([mat_directory nc_filename '.mat'], 'data', 'meta_data', 'header', 'time_esecs');
    data_prev = data;
    meta_data_prev = meta_data;
    time_prev = time_esecs;
else
    data_prev = [];
    time_prev = [];
end

data = [];
meta_data = [];
header = []; time_esecs=[];

fid = fopen([nc_directory '/' nc_filename '.nc']);
if(fid == -1)
    disp([nc_filename ' does not exist']);
    close;
    return;
end
fclose(fid);
nc_file = netcdf([nc_directory '/' nc_filename '.nc']);


depth_index = strmatch('depth', ncnames(var(nc_file)));
if isempty(depth_index), depth_index = strmatch('Depth', ncnames(var(nc_file))); end
if isempty(depth_index), depth_index = strmatch('NominalDepth', ncnames(var(nc_file))); end
if isempty(depth_index), depth_index = strmatch('DEPTH', ncnames(var(nc_file))); end
if isempty(depth_index), depth_index = strmatch('HR_DEPTH', ncnames(var(nc_file))); end

depth = nc_file{depth_index(1)}(:);


time_index = strmatch_smb('TIME', ncnames(var(nc_file)), 'mid');
if isempty(time_index), time_index = strmatch('esecs', ncnames(var(nc_file))); end
if isempty(time_index), time_index = strmatch('time', ncnames(var(nc_file))); end

time_esecs = nc_file{time_index(1)}(:);
time_edays = time_esecs/(3600*24);
epoch_start = datenum('1-jan-1970');
time = epoch_start + time_edays;

mdy = datestr(time, 'mm/dd/yyyy');
hhmm = datestr(time, 'HH:MM');

lat_index = strmatch('LAT', ncnames(var(nc_file)));
if isempty(lat_index), lat_index = strmatch('Latitude', ncnames(var(nc_file))); end
if isempty(lat_index), lat_index = strmatch('LATITUDE', ncnames(var(nc_file))); end
if isempty(lat_index), lat_index = strmatch('latitude', ncnames(var(nc_file))); end


lat = nc_file{lat_index(1)}(:);

lon_index = strmatch('LON', ncnames(var(nc_file)));
if isempty(lon_index), lon_index = strmatch('Longitude', ncnames(var(nc_file))); end
if isempty(lon_index), lon_index = strmatch('LONGITUDE', ncnames(var(nc_file))); end
if isempty(lon_index), lon_index = strmatch('longitude', ncnames(var(nc_file))); end

lon = nc_file{lon_index(1)}(:);

var_name = {};
for vars = length(ncnames(var(nc_file))) : -1 : 1,
    var_dims = length(dim(nc_file{vars}));
    if(var_dims > 3) && isempty(var_name);
        var_name{1} = char(ncnames(nc_file{vars}));
        variable(1,:,:) = nc_file{vars}(:);
    elseif (var_dims >3)
        var_name{end+1} = char(ncnames(nc_file{vars}));
        temp = nc_file{vars}(:);
        [d, e] = size (temp);
        [f, g, h] = size(variable);
        if e<h,
            temp(:,e+1:h) = NaN; %inserts NaNs in new variable if it does have values for each depth
        elseif e>h,
            variable(:,:,h+1:e) = NaN; % inserts NaNs in variable array to expand and allow variables with additional depths/dimensions
        end
        variable(end+1,:,:) = temp (:,:,:);
    end
end

for t = length(time_prev)+1:length(time), 
% for t = 5001: 10000,
    for z = 1:length(depth),
        meta_data{(t + (length(depth)-1)*(t-1))+(z-1),1} = mdy(t,1:10);
        meta_data{(t + (length(depth)-1)*(t-1))+(z-1),2} = hhmm(t,1:5);
        data((t + (length(depth)-1)*(t-1))+(z-1),1) = time_esecs(t);
        data((t + (length(depth)-1)*(t-1))+(z-1),2) = depth(z);
        data((t + (length(depth)-1)*(t-1))+(z-1),3) = lat;
        data((t + (length(depth)-1)*(t-1))+(z-1),4) = lon;
        for v = 1:length(var_name),
            data((t + (length(depth)-1)*(t-1))+(z-1),4+v) = variable(v,t,z);
        end
    end
end

header{1} = 'mm/dd/yyyy';
header{2} = 'HH:MM';
header{3} = char(ncnames(nc_file{time_index}));
header{4} = char(ncnames(nc_file{depth_index}));
header{5} = char(ncnames(nc_file{lat_index}));
header{6} = char(ncnames(nc_file{lon_index}));

for h = 1:length(var_name),
    header{6+h} = var_name{h};
end

append = 0;

if ((mid ~= -1) && exist('data', 'var')),
    new_data = [data_prev; data(length(data_prev)+1:end,:)];
    new_meta_data = [meta_data_prev; meta_data(length(data_prev)+1:end,:)];

    data = new_data;
    meta_data = new_meta_data;
    clear new_data new_meta_data
    new_data = data(length(data_prev)+1:end,:);
    new_meta_data = meta_data(length(data_prev)+1:end,:);
    append = 1;
else
    new_data = data;
    new_meta_data = meta_data;
end




end
