function ssds_netcdf_multwavelengths (netCDF_path, fileroot, mooring, rad_name) disp('writing netcdf files for ssds'); wavelengths= [410 440 490 510 550 610 650 670 700]; %opens and reads the netcdf file being used for plotting nc_filename = ([netCDF_path '/' fileroot '.nc']); fid = fopen(nc_filename); if(fid == -1) close; nc_filename = [netCDF_path '/' mooring.name '_ocr_' fileroot '.nc']; fid = fopen(nc_filename); if (fid == -1) close; return end end fclose(fid); nc_file = netcdf(nc_filename); % max_valid = nc_file.max_valid(:); % if(isempty(max_valid)), max_valid = inf; end % min_valid = nc_file.min_valid(:); % if(isempty(min_valid)), min_valid = -inf; end %finds the variable with multiple dimensions - contains the %radiometer data for vars = length(ncnames(var(nc_file))) : -1 : 1, var_dims = length(dim(nc_file{vars})); if(var_dims > 1) var_name = char(ncnames(nc_file{vars})); break; end end ssds_nc_filename = [netCDF_path '/ssds_netcdf/' mooring.name ... '_' rad_name '_' fileroot '_ssds.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; % ssds_nc_file(wavelength_name) = 1; %Initialize variables, copies lat and lon from original netcdf ssds_nc_file{'time'} = nclong('time'); % lat = nc_file{'latitude'}; % lon = nc_file{'longitude'}; ssds_nc_file{'latitude'} = ncdouble('latitude'); ssds_nc_file{'longitude'} = ncdouble('longitude'); ssds_nc_file{'depth'} = ncfloat ('depth'); ssds_nc_file{'instrument_name'} = ncchar('depth','latitude', 'longitude'); %Initialize one variable for each wavelength selected for lambda = 1:length(wavelengths), wavelength_name = num2str(wavelengths(lambda)); ssds_nc_file{wavelength_name} = ncfloat('time', 'depth','latitude', 'longitude'); ssds_nc_file{wavelength_name}.long_name = [var_name '_' wavelength_name]; end % Reads in lat, lon, depth, and instrument name from nc_file ssds_nc_file{'latitude'}(:) = nc_file{'latitude'}(:); ssds_nc_file{'longitude'}(:) = nc_file{'longitude'}(:); ssds_nc_file{'depth'}(:) = nc_file{'depth'}(:); ssds_nc_file{'instrument_name'} = var_name; % 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 = nc_file{'time'}(:); 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); if (isempty(last_ssds_index)), last_ssds_index=0; end end_index = max(find(all_times>ssds_times(end),1,'last')); %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 = nc_file{'time'}(last_ssds_index+1:end_index); spectra = squeeze(nc_file{var_name}(last_ssds_index+1:end_index,:)); 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 = num2str(wavelengths(lambda)); ssds_nc_file{wavelength_name}(last_ssds_index+1:end_index-count) = spectra_wave; end end end close(nc_file); close(ssds_nc_file); end