function co2_ssds_netcdf (netCDF_path, data)
%blah blah blah

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

mooring = 'm0';
rad_name = 'co2_mbari8';
wavelengths = {'ZeroCorEquil'; 'WaterxCO2' ;'ZeroCorAir' ;'AirxCO2'; 'O2' };

disp('writing netcdf files for ssds');

ssds_nc_filename = [netCDF_path '/'  mooring ...
    '_' rad_name '.nc'];

id = fopen(ssds_nc_filename);
%if ssds_nc_file exists, open it, otherwise create a new one
if (id==-1)
    disp('Cannot find ssds netcdf file, creating new file');
    ssds_nc_file = netcdf(ssds_nc_filename, 'clobber');
    %Initialize dimensions
    ssds_nc_file('time') = 0; %makes time the unlimited dimension
    ssds_nc_file('depth') = 1;
    ssds_nc_file('latitude') = 1;
    ssds_nc_file('longitude') = 1;

    %Initialize variables, copies lat and lon from original netcdf
    ssds_nc_file{'time'} = nclong('time');
    ssds_nc_file{'depth'} = ncfloat ('depth');
    ssds_nc_file{'latitude'} = ncdouble('latitude');
    ssds_nc_file{'longitude'} = ncdouble('longitude');

    ssds_nc_file{'instrument_name'} = ncchar('depth','latitude', 'longitude');

    %Initialize one variable for each wavelength selected
    for lambda = 1:length(wavelengths),
        wavelength_name = (wavelengths{lambda});
        ssds_nc_file{wavelength_name} = ncfloat('time', 'depth','latitude', 'longitude');
        ssds_nc_file{wavelength_name}.long_name = wavelength_name;
    end

    
 
    % Reads in lat, lon, depth, and instrument name from nc_file
    ssds_nc_file{'depth'}(:) = data(1,2);
    ssds_nc_file{'latitude'}(:) = data(1,3);
    ssds_nc_file{'longitude'}(:) = data(1,4);
    ssds_nc_file{'instrument_name'} = rad_name;
    %ssds_lon = ssds_nc_file{'longitude'};

    % copy(lat, ssds_lat);
    %copy (lon, ssds_lon);



    disp(['New file created: ' ssds_nc_filename]);
else
    ssds_nc_file = netcdf(ssds_nc_filename, 'write');
end

all_times = data(:,1);
ssds_times = ssds_nc_file{'time'}(:);
if (isempty(ssds_times)),
    ssds_times=1;
end

%last_ssds_index = max(find(ssds_times>0)); %finds the last time written to the ssds_netcdf file

last_ssds_time = max(ssds_times);
last_ssds_index = find(all_times==last_ssds_time,1,'last');
if (isempty(last_ssds_index)),
    last_ssds_index=0;
end

end_index = max(find(all_times>ssds_times(end))); %finds the newest time entry

%end_index = 85000;
%if strcmp(fileroot, 'Ed10_median'),
%    return,
%end

for lambda = 1:length(wavelengths),
    if last_ssds_index < end_index,

        new_times = data(last_ssds_index+1:end_index,1);

        spectra = data(last_ssds_index+1:end_index,4+lambda);
        if isempty(spectra),
            break,
        end
        
        y = diff([0;new_times]);
        w = find(y>=0);
        mono_time = new_times(w); 
        mono_spectra = spectra(w,:);
        
        neg = 1;
        while (isempty(neg)==0),
            q = diff([0;mono_time]);
            w = find(q>0);
            neg = find(q<=0);
            mono_time = mono_time(w);
            mono_spectra = spectra(w,:);
        end
        
        count = length(new_times) - length(mono_time); %the number of negative elements that were eliminated
%        count = 0;
%        for k = last_ssds_index+1:end_index, % creates a monotonic record
%            if k==last_ssds_index+1,
%                mono_time = new_times(k-last_ssds_index);
%                mono_spectra = spectra(k-last_ssds_index,:);
%                prev_time = new_times(k-last_ssds_index);
%            elseif new_times(k-last_ssds_index) <= prev_time,
%                mono_time(end) = new_times(k-last_ssds_index);
%                mono_spectra(end,:) = spectra(k-last_ssds_index,:);
%                count = count+1;
%            else
%                mono_time(end+1) = new_times(k-last_ssds_index);
%                mono_spectra(end+1,:) = spectra(k-last_ssds_index,:);
%                prev_time = new_times(k-last_ssds_index);
%            end


%        end
%        wave_index = find(int32(nc_file{'wavelength'}(:))==wavelengths(lambda));
%        spectra_wave = mono_spectra(:,wave_index);
%        if isempty(spectra_wave),
%            break,
%        else
            ssds_nc_file{'time'}(last_ssds_index+1:end_index-count) = mono_time;
            wavelength_name = (wavelengths{lambda});
            ssds_nc_file{wavelength_name}(last_ssds_index+1:end_index-count) = mono_spectra;
%        end
    end
end

close(ssds_nc_file);


end
