%function  netcdf_eco(p)
% NETCDF_eco - create a netcdf file from the eco data
%
% Use as: netcdf_eco(p)
%
% Inputs: p = contains property information, i.e. filenames, mooring name, etc

% RPM
% 20 Jul 2004

%===========
% Open File
%===========
try,
    % Write to log file
    %fid=fopen([p.ldir,'/',p.log],'at+');
    %fprintf(fid,'%s\n',['  5. NETCDF_ECO: ' datestr(now), ' ', p.name]);
    %fclose(fid);
    
    ncFilename = ['i:\CIMT\ecofl_filt_day.nc'];
    outfile = netcdf(ncFilename, 'clobber'); % outfile is a netcdf object
    if isempty(outfile)
        error(['Unable to create netcdf file: ' ncFilename]);
    end
    
    %
    eco = [rjtime;ecofl_lp;ecofl_cal;];
    % assign vars
    offset=0;
    rjtime = eco(:,1);
    ecocal = eco(:,2);
    ecoraw = eco(:,3);
    
    
    %=================================================================
    % Construct Netcdf schema for a single microcat on eqpac moorings
    %=================================================================
    [Year Month Day]     = datevec(now);
    dateS                = sprintf('%02i/%02i/%04i', Month, Day, Year);
    
    outfile.conventions  ='COARDS/PMEL-EPIC';
    outfile.directory    = 'i:\CIMT\';
    outfile.creationDate = dateS;
    outfile.lastModified = dateS;
    
    fprintf(1,'Creating netcdf schema')
    outfile.project = 'CIMT';
    outfile.mooring = 'M0';
    outfile.inst_type = 'MBARI';
    outfile.description  = ['M0 data from WetLabs ECOFL sensor'];
    outfile.source='MBARI, Center for Integrated Marine Technology';
    outfile.data_info='Contact Francisco Chavez chfr@mbari.org';
    outfile.file_info='Contact Reiko Michisaki reiko@mbari.org';
    outfile.data_cmnt='Data calibrated according to WetLabs calculations, www.wetlabs.com';
    outfile.var_desc=['Calibrated from raw counts, daily averaged, and filtered (lowpass).'];
    outfile.missing_value=-999;
    outfile.FillValue_   =-999;
%36.83 N 121.90 W
    lon = str2num('-121.90');
    if lon < 0
        lonS = [num2str(abs(lon)) 'W'];
    else
        lonS = [num2str(lon) 'E'];
    end
    lat = str2num(36.83);
    if lat < 0
        latS = [num2str(abs(lat)) 'S'];
    else
        latS = [num2str(lat) 'N'];
    end
    
    outfile.longitude = lonS;
    outfile.latitude  = latS;
    outfile.waterDepth             = 70;
    outfile.keywords               = ['CIMT, mooring,M0, MBARI, ECOFL, WETLABS'];
    outfile.instumentType          = ['WETLABS ECOFL Fluorometer'];
    
    outfile('latitude')            = 1;                     % Dimension
    outfile{'latitude'}            = ncdouble('latitude');  % Specify Co-ordinate variable dimensions
    outfile{'latitude'}.long_name  = 'Latitude';
    outfile{'latitude'}.units      = 'degrees_north';
    outfile{'latitude'}.symbol     = 'lat';
    outfile{'latitude'}(:)         = lat;
    
    outfile('longitude')           = 1;                     % Dimension
    outfile{'longitude'}           = ncdouble('longitude'); % Specify Co-ordinate variable dimensions
    outfile{'longitude'}.long_name = 'Longitude';
    outfile{'longitude'}.units     = 'degrees_west';
    outfile{'longitude'}.symbol    = 'lon';
    outfile{'longitude'}(:)        = lon;
    
    outfile('depth')               = 1;                     % Dimension
    outfile{'depth'}               = ncdouble('depth'); % Specify Co-ordinate variable dimensions
    outfile{'depth'}.long_name     = 'Depth';
    outfile{'depth'}.units         = 'meters';
    outfile{'depth'}.positive      = 'down';
    outfile{'depth'}(:)            = 0;
    
    outfile('time')                = 0;                     % Define Unlimited dimension
    outfile{'time'}                = ncdouble('time');
    outfile{'time'}.long_name      = 'time GMT';
    outfile{'time'}.units          = 'running day of year, since 1/1/88, and fractional time';
    outfile{'time'}.symbol         = 'GMT';
    
    outfile{'raw'}                = ncfloat('time','depth','longitude','latitude');
    outfile{'raw'}.long_name      = ['MO: calibrated data'];
    outfile{'raw'}.units          = 'counts';
    outfile{'raw'}.symbol         = ['ECOFL Calibrated'];
    outfile{'raw'}.valid_range    = -999,60;
    outfile{'raw'}.FillValue_     = -999;
    outfile{'raw'}.missing_value  = -999;
    
    outfile{'ecocal'}                = ncfloat('time','depth','longitude','latitude');
    outfile{'ecocal'}.long_name      = ['M0:low pass filtered daily averaged calibrated data'];
    outfile{'ecocal'}.units          = 'mg m-3';
    outfile{'ecocal'}.symbol         = ['ECOFL Cal Filtered'];
    outfile{'ecocal'}.FillValue_     = -999;
    outfile{'ecocal'}.missing_value  = -999;
    
   
    outfile{'time'}(1:length(rjtime)) = rjtime;
    %outfile{'siam_time'}(:) = ecosiam;
    outfile{'raw'}(:) = ecoraw;
    outfile{'ecocal'}(:)  = ecocal;
    ncquit(outfile);
    [r,c]=size(rjtime);
    %fprintf(1,['Netcdf file created: ',datestr(ecosdn(1)),' to ',datestr(ecosdn(r))])
    
catch,
    % Something went wrong, capture error message and write to log file
    [LASTMSG, LASTID] = lasterr;
    disp(['** PROCESSING HALTED** Error: ',LASTMSG]);
    disp([' Last ID: ', LASTID]);
    %fid=fopen([p.ldir,'/',p.log],'at+');
    %fprintf(fid,'%s\n',['NETCDF_ECO']);
    %fprintf(fid,'%s\n',['     **PROCESSING HALTED** Error:  ', LASTMSG ]);
    %fprintf(fid,'%s\n',['     Last ID: ', LASTID ]);  
    %fprintf(fid,'%s\n',':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::');
    %fprintf(fid,'\n');
    %fclose(fid);
    %exit;
end
