function []=create_epic(cdf,v,vnames,lat,lon,start,dt,wdepth,idepth,units,cmt)
%CREATE_EPIC Creates netCDF time series with EPIC conventions 
%            from evenly spaced time series data
%     USAGE: []=create_epic(cdf,v,vnames,lat,lon,start,dt,wdepth,idepth,units)
%
% where:
%  cdf  = cdf file name to file to be created
%  v = matrix of time series in columns
%  vnames = character matrix with names of variables, 1 per row
%  lat  = latitude
%  lon  = longitude
%  start = [yyyy mm dd hh mi sc] 
%  dt    = time interval in seconds
%  wdepth = water depth in meters
%  idepth = instrument depth in meters
%  units = character matrix with units of variables, 1 per row
%
%  Rich Signell   7-9-92  rsignell@crusty.er.usgs.gov
%
[m,n]=size(v);
%
% if only 1 time series, it's ok if it's a row vector
if(m==1),v=v',[m,n]=size(v);end;
%
[mm,nn]=size(vnames);
[mu,nu]=size(units);
if(mm~=n),
  disp(' must have a name for each variable')
  return
end
% Create netCDF file 
cdfid = mexcdf('nccreate',cdf,'NC_CLOBBER');
%
% Define dimensions
dims(1) = mexcdf('ncdimdef',cdfid,'time',m);
dims(2) = mexcdf('ncdimdef',cdfid,'depth',1);
dims(3) = mexcdf('ncdimdef',cdfid,'lat',1);
dims(4) = mexcdf('ncdimdef',cdfid,'lon',1);

% Define variables
%
lonid   = mexcdf('ncvardef',cdfid,'lon','NC_FLOAT',1,dims(4));
  mexcdf('ncattput',cdfid,lonid,'units','NC_CHAR',-1,'degrees');
%
latid   = mexcdf('ncvardef',cdfid,'lat','NC_FLOAT',1,dims(3));
  mexcdf('ncattput',cdfid,latid,'units','NC_CHAR',-1,'degrees');
%
depthid = mexcdf('ncvardef',cdfid,'depth','NC_FLOAT',1,dims(2));
  mexcdf('ncattput',cdfid,depthid,'units','NC_CHAR',-1,'m');
%
for i=1:n,
  varid(i) = mexcdf('ncvardef',cdfid,vnames(i,:),'NC_FLOAT',4,dims);
  mexcdf('ncattput',cdfid,varid(i),'long_name','NC_CHAR',-1,vnames(i,:));
  mexcdf('ncattput',cdfid,varid(i),'units','NC_CHAR',-1,units(i,:));
  mexcdf('ncattput',cdfid,varid(i),'valid_range','NC_FLOAT',2,[-1.e16 1.e16]);
end
%
timeid  = mexcdf('ncvardef',cdfid,'time','NC_LONG',1,dims(1));
  mexcdf('ncattput',cdfid,timeid,'units','NC_CHAR',-1,'Julian days');
  mexcdf('ncattput',cdfid,timeid,'epic_code','NC_LONG',1,624);
%
time2id = mexcdf('ncvardef',cdfid,'time2','NC_LONG',1,dims(1));
  mexcdf('ncattput',cdfid,time2id,'units','NC_CHAR',-1,...
'milliseconds from midnight');

% Create time base
t0=start(4)*3600+start(5)*60+start(6);
jul0=floor(julian(start));

t=[t0:dt:t0+dt*(m-1)];
time2=rem(t,3600*24)*1000;
time=floor(t/(3600*24))+jul0;

stop=gregorian(last(time));
[stop(4),stop(5),stop(6)]=ms2hms(last(time2));

% Create Global attributes
mexcdf('ncattput',cdfid,'NC_GLOBAL','latitude','NC_FLOAT',1,lat);
mexcdf('ncattput',cdfid,'NC_GLOBAL','longitude','NC_FLOAT',1,lon);
mexcdf('ncattput',cdfid,'NC_GLOBAL','water_depth','NC_FLOAT',1,wdepth);
mexcdf('ncattput',cdfid,'NC_GLOBAL','base_date','NC_SHORT',3,start(1:3));
mexcdf('ncattput',cdfid,'NC_GLOBAL','start_time','NC_LONG',6,start);
mexcdf('ncattput',cdfid,'NC_GLOBAL','stop_time','NC_LONG',6,stop);
mexcdf('ncattput',cdfid,'NC_GLOBAL','DATA_TYPE','NC_CHAR',-1,'TIME');
mexcdf('ncattput',cdfid,'NC_GLOBAL','COORD_SYSTEM','NC_CHAR',-1,'GEOGRAPHICAL');
mexcdf('ncattput',cdfid,'NC_GLOBAL','COMMENT','NC_CHAR',-1,cmt);
%
% End define mode
mexcdf('ncendef',cdfid);

% Store coordinate (independent) variables
mexcdf('ncvarput1',cdfid,lonid,0,-lon);
mexcdf('ncvarput1',cdfid,latid,0,lat);
mexcdf('ncvarput1',cdfid,depthid,0,idepth(n));
mexcdf('ncvarput',cdfid,timeid,0,m,time);
mexcdf('ncvarput',cdfid,time2id,0,m,time2);
%
% Store data (dependent) variables 
for i=1:n,
  v(:,i)=replace(v(:,i),NaN,1.e35);
  mexcdf('ncvarput',cdfid,varid(i),[0 0 0 0],[m 1 1 1],v(:,i));
end
%
% Close netCDF file
mexcdf('ncclose',cdfid);
