%% ncoutbinned.m: writes AUV data to netcdf format;
%% Note that if you are writing to netcdf from matlab, you must
%% have installed the netcdf toolbox and have it in your path.
%% Usage : 
%% ncout1;
%%
%% Revision history
%% ----------------------------
%% August 2000
%% ascii write standards established during August 2000 MUSE to match 
%% ROV ascii data files:
%% John Ryan and Mike McCann
%%
%% June 2001
%% Brian Schlining coded a template for netcdf writing from MATLAB, ensuring
%% that the file would meet standards of self description and portability.
%% John Ryan adapted template to the suite of science data collected by the
%% MBARI Odyssey IIb AUV operated during MUSE day operations.
%%
%% September 2001
%% Nicole Tervalon simplified and modified to use as generic netcdf output
%% script for use during ALTEX October 2001 test cruise
%% ----------------------------


%%----------------------------
%%   netcdf 
%%----------------------------
 xpfile=['databinned.nc'];
 disp(['Exporting data to netcdf file ' xpfile]);
 
 timeunits = ['Days since ',num2str(startyear),'-',num2str(startmonth),'-',num2str(startday),' 00:00:00'];


 outfile = netcdf(xpfile, 'clobber'); % outfile is a netcdf object
 if isempty(outfile)
    error(['Unable to create ' xpfile]);
 end
 
 missingval = -999;
 if (exist('depthnav')==1), 
   idx=find(isnan(depthnav)); 
   if ~isempty(idx); depthnav(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('P1')==1), 
   idx=find(isnan(p1)); 
   if ~isempty(idx); p1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('P2')==1), 
   idx=find(isnan(p2)); 
   if ~isempty(idx); p2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('depth1')==1), 
   idx=find(isnan(depth1)); 
   if ~isempty(idx); depth1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('depth2')==1), 
   idx=find(isnan(depth2)); 
   if ~isempty(idx); depth2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('Xpos')==1), 
   idx=find(isnan(Xpos)); 
   if ~isempty(idx); Xpos(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('Ypos')==1), 
   idx=find(isnan(Ypos)); 
   if ~isempty(idx); Ypos(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('T1')==1),  
   idx=find(isnan(T1)); 
   if ~isempty(idx); T1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('T2')==1), 
   idx=find(isnan(T2)); 
   if ~isempty(idx); T2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('C1')==1), 
   idx=find(isnan(C1)); 
   if ~isempty(idx); C1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('C2')==1), 
   idx=find(isnan(C2)); 
   if ~isempty(idx); C2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('S1')==1), 
   idx=find(isnan(S1)); 
   if ~isempty(idx); S1(idx)=repmat(missingval,size(idx)); end 
 end
 if (exist('S2')==1), 
   idx=find(isnan(S2)); 
   if ~isempty(idx); S2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('D1')==1), 
   idx=find(isnan(D1)); 
   if ~isempty(idx); D1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('D2')==1), 
   idx=find(isnan(D2)); 
   if ~isempty(idx); D2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('OBSbin')==1), 
   idx=find(isnan(OBSbin)); 
   if ~isempty(idx); OBSbin(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('O2')==1), 
   idx=find(isnan(O2)); 
   if ~isempty(idx); O2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('FLOW1')==1), 
   idx=find(isnan(FLOW1)); 
   if ~isempty(idx); FLOW1(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('FLOW2')==1), 
   idx=find(isnan(FLOW2)); 
   if ~isempty(idx); FLOW2(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('pitch')==1), 
   idx=find(isnan(pitch)); 
   if ~isempty(idx); pitch(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('roll')==1), 
   idx=find(isnan(roll)); 
   if ~isempty(idx); roll(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('yaw')==1), 
   idx=find(isnan(yaw)); 
   if ~isempty(idx); yaw(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('IPSPRES')==1), 
   idx=find(isnan(IPSPRES)); 
   if ~isempty(idx); IPSPRES(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('IPSDEPTH')==1), 
   idx=find(isnan(IPSDEPTH)); 
   if ~isempty(idx); IPSDEPTH(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('IPSDRAFT')==1), 
   idx=find(isnan(IPSDRAFT)); 
   if ~isempty(idx); IPSDRAFT(idx)=repmat(missingval,size(idx)); end
 end
 if (exist('IPSDRAFTRAW')==1), 
   idx=find(isnan(IPSDRAFTRAW)); 
   if ~isempty(idx); IPSDRAFTRAW(idx)=repmat(missingval,size(idx)); end
 end

 if( exist('ipstime') ~= 1 | ...
     length(IPSPRES)     ~= length(binabstime) | ...
     length(IPSDEPTH)    ~= length(binabstime) | ...
     length(IPSDRAFT)    ~= length(binabstime) | ...
     length(IPSDRAFTRAW) ~= length(binabstime)   ...
   )
   saveIps = 0;
 else
   saveIps = 1;
 end
 
 %%---------------------------
 %% Add global Attributes
 %%---------------------------
 [Year Month Day]     = datevec(now);
 dateS                = sprintf('%02i/%02i/%04i', Month, Day, Year);
 
 outfile.conventions  = 'MBARI/auv/ALTEX';
 outfile.creationDate = dateS;
 outfile.lastModified = dateS;
 outfile.platform     = 'ALTEX';
 outfile.keywords     = 'AUV, temperature, salinity, density, optical backscatter, oxygen';
 
 %%-----------------------------------------------------------------------------
 % Coordinate variable (using time as key) cannot have any missing values; check
 %%-----------------------------------------------------------------------------
 % if any(isnan(ILAT) | isnan(ILAT) | isnan(ILAT) | isnan(ILAT));
 if any(isnan(binabstime));
  disp(['Coordinate variable (time) has missing data; no netcdf file written.']);
  return
 end
 
 
 
 %% Time, unlimited dimension, double precision needed
 outfile('time')                = 0;                     % Define Unlimited dimension
 outfile{'time'}                = ncdouble('time');
 outfile{'time'}.long_name      = 'time GMT';
 outfile{'time'}.units          = timeunits;

if (exist('P1')==1), 
 %% Pressure computed at CTD1
 outfile{'P1'}                = ncfloat('time');
 outfile{'P1'}.long_name      = 'Pressure';
 outfile{'P1'}.units          = 'dbar';
 outfile{'P1'}.FillValue_     = -999;
 outfile{'P1'}.missing_value  = -999;
end

if (exist('P2')==1), 
 %% Pressure computed at CTD2
 outfile{'P2'}                = ncfloat('time');
 outfile{'P2'}.long_name      = 'Pressure';
 outfile{'P2'}.units          = 'dbar';
 outfile{'P2'}.FillValue_     = -999;
 outfile{'P2'}.missing_value  = -999;
end
 
if (exist('depthnav')==1), 
 %% Navigation Depth 
 outfile{'Depthnav'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Depthnav'}.long_name = 'Depth';
 outfile{'Depthnav'}.units     = 'm';
 outfile{'Depthnav'}.FillValue_     = -999;
 outfile{'Depthnav'}.missing_value  = -999;
end

if (exist('depth1')==1), 
 %% CTD1 Depth 
 outfile{'Depth1'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Depth1'}.long_name = 'Depth';
 outfile{'Depth1'}.units     = 'm';
 outfile{'Depth1'}.FillValue_     = -999;
 outfile{'Depth1'}.missing_value  = -999;
end

if (exist('depth2')==1), 
 %% CTD2 Depth 
 outfile{'Depth2'}           = ncfloat('time'); % Specify Co-ordinate variable dimensions
 outfile{'Depth2'}.long_name = 'Depth';
 outfile{'Depth2'}.units     = 'm';
 outfile{'Depth2'}.FillValue_     = -999;
 outfile{'Depth2'}.missing_value  = -999;
end

if (exist('Xpos')==1), 
 %% X position (dead reckoning)
 outfile{'Xpos'}                = ncfloat('time');
 outfile{'Xpos'}.long_name      = 'X position (dead reckoning)';
 outfile{'Xpos'}.units          = 'm';
 outfile{'Xpos'}.FillValue_     = -999;
 outfile{'Xpos'}.missing_value  = -999;
end

if (exist('Ypos')==1), 
 %% Y position (dead reckoning)
 outfile{'Ypos'}                = ncfloat('time');
 outfile{'Ypos'}.long_name      = 'Y position (dead reckoning)';
 outfile{'Ypos'}.units          = 'm';
 outfile{'Ypos'}.FillValue_     = -999;
 outfile{'Ypos'}.missing_value  = -999;
end

if (exist('T1')==1), 
 %% Temperature 1
 outfile{'T1'}                = ncfloat('time');
 outfile{'T1'}.long_name      = 'Temperature';
 outfile{'T1'}.units          = 'deg C';
 outfile{'T1'}.FillValue_     = -999;
 outfile{'T1'}.missing_value  = -999;
end

if (exist('T2')==1), 
 %% Temperature 2
 outfile{'T2'}                = ncfloat('time');
 outfile{'T2'}.long_name      = 'Temperature';
 outfile{'T2'}.units          = 'deg C';
 outfile{'T2'}.FillValue_     = -999;
 outfile{'T2'}.missing_value  = -999; 
end

if (exist('C1')==1), 
 %% Conductivity 1
 outfile{'C1'}                = ncfloat('time');
 outfile{'C1'}.long_name      = 'Conductivity';
 outfile{'C1'}.units          = 'Siemens/m';
 outfile{'C1'}.FillValue_     = -999;
 outfile{'C1'}.missing_value  = -999;
end 

if (exist('C2')==1), 
 %% Conductivity 2
 outfile{'C2'}                = ncfloat('time');
 outfile{'C2'}.long_name      = 'Conductivity';
 outfile{'C2'}.units          = 'Siemens/m';
 outfile{'C2'}.FillValue_     = -999;
 outfile{'C2'}.missing_value  = -999;
end

if (exist('S1')==1), 
 %% Salinity 1
 outfile{'S1'}                = ncfloat('time');
 outfile{'S1'}.long_name      = 'Salinity';
 outfile{'S1'}.units          = 'psu';
 outfile{'S1'}.FillValue_     = -999;
 outfile{'S1'}.missing_value  = -999;
end

if (exist('S2')==1), 
 %% Salinity 2
 outfile{'S2'}                = ncfloat('time');
 outfile{'S2'}.long_name      = 'Salinity';
 outfile{'S2'}.units          = 'psu';
 outfile{'S2'}.FillValue_     = -999;
 outfile{'S2'}.missing_value  = -999;
end

if (exist('D1')==1), 
 %% Density 1
 outfile{'D1'}                = ncfloat('time');
 outfile{'D1'}.long_name      = 'Density';
 outfile{'D1'}.units          = 'kg/m**3';
 outfile{'D1'}.FillValue_     = -999;
 outfile{'D1'}.missing_value  = -999;
end

if (exist('D2')==1), 
 %% Density 2
 outfile{'D2'}                = ncfloat('time');
 outfile{'D2'}.long_name      = 'Density';
 outfile{'D2'}.units          = 'kg/m**3';
 outfile{'D2'}.FillValue_     = -999;
 outfile{'D2'}.missing_value  = -999; 
end

if (exist('OBSbin')==1), 
 %% Optical Backscatter 
 outfile{'OBSbin'}                = ncfloat('time');
 outfile{'OBSbin'}.long_name      = 'Optical Backscatter';
 outfile{'OBSbin'}.units          = 'volts (uncalibrated)';
 outfile{'OBSbin'}.FillValue_     = -999;
 outfile{'OBSbin'}.missing_value  = -999;
end

if (exist('O2')==1), 
 %% Oxygen 
 outfile{'O2'}                = ncfloat('time');
 outfile{'O2'}.long_name      = 'Oxygen concentration';
 outfile{'O2'}.units          = 'mL/L';
 outfile{'O2'}.FillValue_     = -999;
 outfile{'O2'}.missing_value  = -999;
end

if (exist('FLOW1')==1), 
 %% Flow 1 
 outfile{'FLOW1'}                = ncfloat('time');
 outfile{'FLOW1'}.long_name      = 'Flow Rate';
 outfile{'FLOW1'}.units          = 'L/min';
 outfile{'FLOW1'}.FillValue_     = -999;
 outfile{'FLOW1'}.missing_value  = -999;
end

if (exist('FLOW2')==1), 
 %% Flow 2 
 outfile{'FLOW2'}                = ncfloat('time');
 outfile{'FLOW2'}.long_name      = 'Flow Rate';
 outfile{'FLOW2'}.units          = 'L/min';
 outfile{'FLOW2'}.FillValue_     = -999;
 outfile{'FLOW2'}.missing_value  = -999;
end  

if (exist('roll')==1), 
 %% Roll 
 outfile{'roll'}                = ncfloat('time');
 outfile{'roll'}.long_name      = 'Roll';
 outfile{'roll'}.units          = 'Degrees';
 outfile{'roll'}.FillValue_     = -999;
 outfile{'roll'}.missing_value  = -999;
end

if (exist('pitch')==1), 
 %% Pitch 
 outfile{'pitch'}                = ncfloat('time');
 outfile{'pitch'}.long_name      = 'Pitch';
 outfile{'pitch'}.units          = 'degrees';
 outfile{'pitch'}.FillValue_     = -999;
 outfile{'pitch'}.missing_value  = -999;
end

if (exist('yaw')==1), 
 %% Yaw 
 outfile{'yaw'}                = ncfloat('time');
 outfile{'yaw'}.long_name      = 'Yaw';
 outfile{'yaw'}.units          = 'Degrees';
 outfile{'yaw'}.FillValue_     = -999;
 outfile{'yaw'}.missing_value  = -999;
end

if( saveIps )
  if (exist('IPSPRES')==1), 
   %% IPSPRES 
   outfile{'IPSPRES'}                = ncfloat('time');
   outfile{'IPSPRES'}.long_name      = 'IPSPRES';
   outfile{'IPSPRES'}.units          = 'Degrees';
   outfile{'IPSPRES'}.FillValue_     = -999;
   outfile{'IPSPRES'}.missing_value  = -999;
  end

  if (exist('IPSDEPTH')==1), 
   %% IPSDEPTH 
   outfile{'IPSDEPTH'}                = ncfloat('time');
   outfile{'IPSDEPTH'}.long_name      = 'IPSDEPTH';
   outfile{'IPSDEPTH'}.units          = 'Degrees';
   outfile{'IPSDEPTH'}.FillValue_     = -999;
   outfile{'IPSDEPTH'}.missing_value  = -999;
  end

  if (exist('IPSDRAFT')==1), 
   %% IPSDRAFT 
   outfile{'IPSDRAFT'}                = ncfloat('time');
   outfile{'IPSDRAFT'}.long_name      = 'IPSDRAFT';
   outfile{'IPSDRAFT'}.units          = 'Degrees';
   outfile{'IPSDRAFT'}.FillValue_     = -999;
   outfile{'IPSDRAFT'}.missing_value  = -999;
  end

  if (exist('IPSDRAFTRAW')==1), 
   %% IPSDRAFTRAW 
   outfile{'IPSDRAFTRAW'}                = ncfloat('time');
   outfile{'IPSDRAFTRAW'}.long_name      = 'IPSDRAFTRAW';
   outfile{'IPSDRAFTRAW'}.units          = 'Degrees';
   outfile{'IPSDRAFTRAW'}.FillValue_     = -999;
   outfile{'IPSDRAFTRAW'}.missing_value  = -999;
  end
end

outfile{'time'}(1:length(binabstime)) = binabstime;
if (exist('P1')==1), 
 outfile{'P1'}(:) = P1*10; end
if (exist('P2')==1), 
 outfile{'P2'}(:) = P2*10; end
if (exist('depthnav')==1), 
 outfile{'Depthnav'}(:) = depthnav; end
if (exist('depth1')==1), 
 outfile{'Depth1'}(:) = depth1; end
if (exist('depth2')==1), 
 outfile{'Depth2'}(:) = depth2; end
if (exist('Xpos')==1), 
 outfile{'Xpos'}(:) = Xpos; end
if (exist('Ypos')==1), 
 outfile{'Ypos'}(:) = Ypos; end
if (exist('T1')==1), 
 outfile{'T1'}(:) = T1; end
if (exist('T2')==1), 
 outfile{'T2'}(:) = T2; end
if (exist('C1')==1), 
 outfile{'C1'}(:) = C1; end
if (exist('C2')==1), 
 outfile{'C2'}(:) = C2; end
if (exist('S1')==1), 
 outfile{'S1'}(:) = S1; end
if (exist('S2')==1), 
 outfile{'S2'}(:) = S2; end
if (exist('D1')==1), 
 outfile{'D1'}(:) = D1; end
if (exist('D2')==1), 
 outfile{'D2'}(:) = D2; end
if (exist('OBSbin')==1), 
 outfile{'OBSbin'}(:) = OBSbin; end
if (exist('O2')==1), 
 outfile{'O2'}(:) = O2; end
if (exist('FLOW1')==1), 
 outfile{'FLOW1'}(:) = FLOW1; end
if (exist('FLOW2')==1), 
 outfile{'FLOW2'}(:) = FLOW2; end
if (exist('pitch')==1), 
 outfile{'pitch'}(:) = pitch; end
if (exist('roll')==1), 
 outfile{'roll'}(:) = roll; end
if (exist('yaw')==1), 
 outfile{'yaw'}(:) = yaw; end
 if( saveIps )
    if (exist('IPSPRES')==1), 
     outfile{'IPSPRES'}(:) = IPSPRES; end
    if (exist('IPSDEPTH')==1), 
     outfile{'IPSDEPTH'}(:) = IPSDEPTH; end
    if (exist('IPSDRAFT')==1), 
     outfile{'IPSDRAFT'}(:) = IPSDRAFT; end
    if (exist('IPSDRAFTRAW')==1), 
     outfile{'IPSDRAFTRAW'}(:) = IPSDRAFTRAW; end
 end
 close(outfile)
