    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%    
function writep(infile, outfile, Year, Month, Day, Tau, Pressure, U, V, lat, lon, site, nominal_depth, update_time);
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Allow NaN only in Pressure, Tau, U, V, else shout
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
check_for_nans (Year, 'Year')
check_for_nans (Year, 'Month')
check_for_nans (Year, 'Day')
check_for_nans (Year, 'Tau')
check_for_nans (Year, 'lat')
check_for_nans (Year, 'lon')
check_for_nans (Year, 'nominal_depth')
check_for_nans (Year, 'update_time')
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Delete existing file, create new file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (exist(outfile,'file'))==2
fprintf('    Deleting existing file %s\n',outfile);
delete(outfile)
end

fprintf('    Creating new file %s\n',outfile);
nc=netcdf.create(outfile,'CLOBBER');
netcdf.close(nc);
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Write global attributes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tim = datenum(Year, Month, Day);
timdesc = median (diff(tim));


fprintf('    Writing global attributes\n');

ncwriteatt(outfile,'/','Metadata_Conventions', ...
     'Unidata Dataset Discovery v1.0, CF-1.5');
ncwriteatt(outfile,'/','title', ...
     ['Western Boundary Time Series measurements from inverted echo sounders']);
ncwriteatt(outfile,'/','summary', ...
     ['Inverted echosounder instruments in the Deep Western Boundary Current' ...
      ' east of Abaco Island, Bahamas.' ...
      ' The instrument sits on a tripod on the seafloor.' ...
      ' Data includes travel time and occasionally includes bottom pressure and bottom velocity.'...
      ' This data set includes all mooring deployments at site A.' ...
      ' See www.aoml.noaa.gov/phod/wbts for further details.']);
ncwriteatt(outfile,'/','keywords', ...
     ['PIES, IES, CPIES, inverted echo sounder, Deep Western Boundary Current,' ...
	      ' acoustic travel time, sound speed, Seafloor Pressure, Seafloor velocity, velocity']);
ncwriteatt(outfile,'/','date_created', ...
     datestr(now+7/24,'yyyy-mm-dd'));
     % Note: make sure "now+X/24" is in UTC
ncwriteatt(outfile,'/','institution', ...
     'National Oceanic and Atmoshpeheric Administration, Atlantic Oceanographic and Meteorological Laboratory');
ncwriteatt(outfile,'/','creator_name', ...
     'Molly Baringer');
ncwriteatt(outfile,'/','contributor_name', ...
     'Molly Baringer and Chris Meinen');
ncwriteatt(outfile,'/','contributor_role', ...
     'Principal Investigator');
ncwriteatt(outfile,'/','acknowledgement', ...
     ['This project was funded through NOAA (National Oceanic' ...
      ' and Atmospheric Administration), Office of Climate' ...
      ' Observations (OCO), USA.  It is part of the National ' ...
      'Science Foundation''s MOCHA project and the United Kingdom''s ' ...
      'National Environmental Research Council''s Rapid program']);
ncwriteatt(outfile,'/','project', ...
     'Western Boundary Time Series');
ncwriteatt(outfile,'/','site', ...
     site);
 ncwriteatt(outfile,'/','geospatial_lat_min', ...
     num2str(min(lat)));
ncwriteatt(outfile,'/','geospatial_lat_max', ...
     num2str(max(lat)));
ncwriteatt(outfile,'/','geospatial_lon_min', ...
     num2str(min(lon)));
ncwriteatt(outfile,'/','geospatial_lon_max', ...
     num2str(max(lon)));
ncwriteatt(outfile,'/','time_coverage_start', ...
     datestr(min(tim),'yyyy-mm-ddTHH:MM:SSZ'));
ncwriteatt(outfile,'/','time_coverage_end', ...
     datestr(max(tim),'yyyy-mm-ddTHH:MM:SSZ'));
ncwriteatt(outfile,'/','license', ...
     ['This work is licensed under the Creative Commons' ...
      ' Attribution 3.0 Unported License. To view a copy' ...
      ' of this license, visit ' ...
      'http://creativecommons.org/licenses/by/3.0/. ' ...
      'Please cite the original investigator(s), institution,' ...
      ' and the funding agency when you use the data.']);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Write coordinate variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf('    Writing coordinate variables\n');

tmpname='TIME';
ltim=length(tim);

nccreate(outfile,tmpname, ...
     'Dimensions',{tmpname,ltim}, ...
     'Datatype','double');
ncwrite(outfile,tmpname, ...
    tim(:)-datenum(2000,1,1));
ncwriteatt(outfile,tmpname, ...
       'long_name','Time');
ncwriteatt(outfile,tmpname, ...
       'standard_name','time');
ncwriteatt(outfile,tmpname, ...
       'units','days since 2000-01-01 00:00:00');
ncwriteatt(outfile,tmpname, ...
       'valid_min',min(tim-datenum(2000,1,1)));
ncwriteatt(outfile,tmpname, ...
       'valid_max',max(tim-datenum(2000,1,1)));
ncwriteatt(outfile,tmpname, ...
       'comment', ...
       sprintf('Processing steps: %s',timdesc));
ncwriteatt(outfile,tmpname, ...
       'axis','T');

   
nccreate(outfile,'LATITUDE', ...
   'Dimensions',{'LATITUDE',1}, ...
   'Datatype','double');
ncwrite(outfile,'LATITUDE', ...
  lat);
ncwriteatt(outfile,'LATITUDE', ...
     'long_name','Latitude');
ncwriteatt(outfile,'LATITUDE', ...
     'standard_name','latitude');
ncwriteatt(outfile,'LATITUDE', ...
     'units','degrees_north');
ncwriteatt(outfile,'LATITUDE', ...
     'valid_min',min(lat));
ncwriteatt(outfile,'LATITUDE', ...
     'valid_max',max(lat));
ncwriteatt(outfile,'LATITUDE', ...
     'comment', ...
     ['Understood as ship position at instrument deployment.' ...
      ' Expect some lateral uncertainty as instrument' ...
      ' sinks to seafloor.']);
ncwriteatt(outfile,'LATITUDE', ...
     'axis','Y');
ncwriteatt(outfile,'LATITUDE', ...
     'reference','WGS84');

nccreate(outfile,'LONGITUDE', ...
   'Dimensions',{'LONGITUDE',1}, ...
   'Datatype','double');
ncwrite(outfile,'LONGITUDE', ...
  lon);
ncwriteatt(outfile,'LONGITUDE', ...
     'long_name','Longitude');
ncwriteatt(outfile,'LONGITUDE', ...
     'standard_name','longitude');
ncwriteatt(outfile,'LONGITUDE', ...
     'units','degrees_east');
ncwriteatt(outfile,'LONGITUDE', ...
     'valid_min',min(lon));
ncwriteatt(outfile,'LONGITUDE', ...
     'valid_max',max(lon));
ncwriteatt(outfile,'LONGITUDE', ...
     'comment', ...
     ['Understood as ship position at instrument deployment.' ...
      ' Expect some lateral uncertainty as instrument' ...
      ' sinks to seafloor.']);
ncwriteatt(outfile,'LONGITUDE', ...
     'axis','X');
ncwriteatt(outfile,'LONGITUDE', ...
     'reference','WGS84');

nccreate(outfile,'DEPTH', ...
   'Dimensions',{'DEPTH',1}, ...
   'Datatype','double');
ncwrite(outfile,'DEPTH',nominal_depth);
ncwriteatt(outfile,'DEPTH', ...
     'long_name','Depth');
ncwriteatt(outfile,'DEPTH', ...
     'standard_name','depth');
ncwriteatt(outfile,'DEPTH', ...
     'units','meters');
ncwriteatt(outfile,'DEPTH', ...
     'positive','down');
ncwriteatt(outfile,'DEPTH', ...
     'valid_min',nominal_depth);
ncwriteatt(outfile,'DEPTH', ...
     'valid_max',nominal_depth);
ncwriteatt(outfile,'DEPTH', ...
     'comment', ...
     'Nominal depth derived from raw pressure measurements.');
ncwriteatt(outfile,'DEPTH', ...
     'axis','Z');
ncwriteatt(outfile,'DEPTH', ...
     'reference','mean_sea_level');
  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%   Write data variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fprintf('    Writing data variables\n');

fv=-999.9999;

% Pressure
tmpname='PRESSURE';
timname='TIME';
ltim=length(tim);
nccreate(outfile,tmpname, ...
     'Dimensions',{timname,ltim,'DEPTH',1, ...
       'LATITUDE',1,'LONGITUDE',1}, ...
     'Datatype','double');
tmpp=Pressure(:);
ff=find(isnan(tmpp));
tmpp(ff)=fv;
ncwrite(outfile,tmpname,tmpp);
ncwriteatt(outfile,tmpname, ...
       '_FillValue',fv);
ncwriteatt(outfile,tmpname,'units','dbar');
if nanmedian(Pressure(:))>100
  ncwriteatt(outfile,tmpname, ...
     'long_name','Sea Water Pressure at Seafloor');
  ncwriteatt(outfile,tmpname,'valid_min',0);
  ncwriteatt(outfile,tmpname,'valid_max',6000);
else
  ncwriteatt(outfile,tmpname, ...
     'long_name', ...
     'Anomaly of Sea Water Pressure at Seafloor');
  ncwriteatt(outfile,tmpname,'valid_min',-10);
  ncwriteatt(outfile,tmpname,'valid_max',10);
end
ncwriteatt(outfile,tmpname, ...
       'instrument_vendor', ...
       'University of Rhode Island, Narragansett, RI, USA');
ncwriteatt(outfile,tmpname, ...
       'instrument_type','Pressure on IES');

% Travel Time
tmpname='ACOUSTICTRAVELTIME';
timname='TIME';
ltim=length(tim);

nccreate(outfile,tmpname, ...
     'Dimensions',{timname,ltim,'DEPTH',1, ...
       'LATITUDE',1,'LONGITUDE',1}, ...
     'Datatype','double');
tmpp=Tau(:);
ff=find(isnan(tmpp));
tmpp(ff)=fv;
ncwrite(outfile,tmpname,tmpp);
ncwriteatt(outfile,tmpname, ...
       '_FillValue',fv);
ncwriteatt(outfile,tmpname,'units','s');
ncwriteatt(outfile,tmpname,'long_name', ...
       ['Roundtrip travel time of acoustic signal from the ' ...
    'seafloor to the sea surface and back']);
ncwriteatt(outfile,tmpname,'valid_min',0);
ncwriteatt(outfile,tmpname,'valid_max',10);
ncwriteatt(outfile,tmpname, ...
       'instrument_vendor', ...
       'University of Rhode Island, Narragansett, RI, USA');
ncwriteatt(outfile,tmpname, ...
       'instrument_type','IES');


% Bottom Velcoity: U
tmpname='UBOTTOMVELOCITY';
timname='TIME';
ltim=length(tim);

nccreate(outfile,tmpname, ...
     'Dimensions',{timname,ltim,'DEPTH',1, ...
       'LATITUDE',1,'LONGITUDE',1}, ...
     'Datatype','double');
tmpp=U(:);
ff=find(isnan(tmpp));
tmpp(ff)=fv;
ncwrite(outfile,tmpname,tmpp);
ncwriteatt(outfile,tmpname, ...
       '_FillValue',fv);
ncwriteatt(outfile,tmpname,'units','cm/s');
ncwriteatt(outfile,tmpname,'long_name', ...
       ['East/West component of the bottom velocity ']);
ncwriteatt(outfile,tmpname,'valid_min',0);
ncwriteatt(outfile,tmpname,'valid_max',10);
ncwriteatt(outfile,tmpname, ...
       'instrument_vendor', ...
       'University of Rhode Island, Narragansett, RI, USA');
ncwriteatt(outfile,tmpname, ...
       'instrument_type','Current meter on IES');

% Bottom Velcoity: V
tmpname='VBOTTOMVELOCITY';
timname='TIME';
ltim=length(tim);

nccreate(outfile,tmpname, ...
     'Dimensions',{timname,ltim,'DEPTH',1, ...
       'LATITUDE',1,'LONGITUDE',1}, ...
     'Datatype','double');
tmpp=V(:);
ff=find(isnan(tmpp));
tmpp(ff)=fv;
ncwrite(outfile,tmpname,tmpp);
ncwriteatt(outfile,tmpname, ...
       '_FillValue',fv);
ncwriteatt(outfile,tmpname,'units','cm/s');
ncwriteatt(outfile,tmpname,'long_name', ...
       ['North/South component of the bottom velocity ']);
ncwriteatt(outfile,tmpname,'valid_min',0);
ncwriteatt(outfile,tmpname,'valid_max',10);
ncwriteatt(outfile,tmpname, ...
       'instrument_vendor', ...
       'University of Rhode Island, Narragansett, RI, USA');
ncwriteatt(outfile,tmpname, ...
       'instrument_type','Current meter on IES');
  
  
return
