function []=asc2epic(innam,netnam,varnum,varnam,vunits,epcode,instid,sdepth,slat,slon,start,dt,wdepth,idepth,cmt,mv,scipi,exper,mtype)
% asc2epic - Ascii to EPIC netcdf formated Time series from evenly spaced time series data.
% This version assumes that all time series variables are at the same
% latitude, longitude, and depth
%
% EXAMPLE #1								run interactively
%	asc2cdf								and complete dialogue boxes
%
% EXAMPLE #2 								to run from command prompt
%asc2cdf('9123e1dg24.data','9123e24.cdf',[3 4 5],{'e';'n';'t'},{'c';'c';'d'},[2;5;9.1],...
%{'v';'v';'t-1'},[50;100;150],'55.5n','70w',...
%[1990 2 3 12 30 00],86400,416,500,'testing',-20,'susan','progtest','sub')
%
% EXAMPLE #3 				get data from an IPF file 
%	 							see end of program for template of an IPF
%								NOTE: name of IPF file MUST start with a letter (not a number)
%	asc2cdf('ipfname')	may include path and the '.m' in the IPFxxx.m name
%	asc2cdf('ipf9123e1dg24')	may include path and the '.m' in the IPFxxx.m name
%
% RESTRICTIONS  
%   	a) There is no provision to skip records at the beginning of the file,
%	 	therefore text preceeding the data needs to be removed.
%		b) The IPF needs to be either in the search path or in the subdirectory
%		in use.

% Scripts needed - gregorian.m julian.m ms2hms.m hms2h.m getlabel.m
% ep_standard.nc needed (a netcdf file which includes the variable attributes of all
%	the variables in the input file.)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is self described.
% Program written in Matlab v7.1.0 SP3
% Program ran on PC with Windows XP Professional OS.
%
% "Although this program has been used by the USGS, no warranty, 
% expressed or implied, is made by the USGS or the United States 
% Government as to the accuracy and functioning of the program 
% and related program material nor shall the fact of distribution 
% constitute any such warranty, and no responsibility is assumed 
% by the USGS in connection therewith."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% Created by Fran Hotchkiss 1 Feb 2000, based on a version of asc2cdf.m. Not modified for
%	running without an ipf file. 
% Asc2cdf.m history:
% Created 	by Susan Tarbell Aug, 1997 
% Modified 	by Susan Tarbell Nov, 1999 to update code
% asc2cdf 02	modified by Susan Tarbell to correct names of files on .log file

% defaults used when creating log file

cmmd=0;										% input not from command line
makipf=0;									% input from interactive inputdlg boxes

%	-----------------------------------------------------------------------
if nargin == 1												% work from IPF file which loads data

% IPF name - innam is name of first argument of function, which in this case is actually IPFname
	slash=find(innam == '\');							% find slashes to remove path on IPF name
	if(isempty(slash) == 1) 							% if no slashes
		ipfpath=cd;
		ipfnam=innam; 
	else														% if there are slashes
		ipfpath=innam(1:slash);
		lastsl=slash(length(slash));
		ipfnam=innam(lastsl+1:length(innam));
	end

	ipfdot=find(ipfnam == '.');
	if isempty(ipfdot)
		eval(ipfnam)											% load IPF file
	else
		eval([ipfnam(1:ipfdot-1)]);						% load ipf file
	end

% output file name (netnam in IPF)
	slash=find(netnam == '\');							% find slashes to remove path of output name
	if(isempty(slash) == 1) 							% if no slashes
		outpath=cd;
		outnam=netnam; 
	else														% if there are slashes
		outpath=netnam(1:slash);
		lastsl=slash(length(slash));
		outnam=netnam(lastsl+1:length(netnam));
	end

% ascii file input name (ascfile in IPF)
	slash=find(ascfile == '\');							% find slashes to remove path of output name
	if(isempty(slash) == 1) 							% if no slashes
		ascpath=cd;
		ascnam=ascfile; 
	else														% if there are slashes
		ascpath=ascfile(1:slash);
		lastsl=slash(length(slash));
		ascnam=ascfile(lastsl+1:length(ascfile));
	end

	ascdot=find(ascnam == '.');						%  
	if isempty(ascdot)
		ascname=ascfile;
	else
		ascname=ascfile(1:ascdot-1);
	end
%keyboard
	if ascname(1:1) > '0' & ascname(1:1) <= '9'		% an 'X' was placed in front of name
		eval(['data=X',ascname,';']);					% change file name to generic 'data'
	else
		eval(['data=',ascname,';']);
	end

	siz=size(data);										% get size of file

	varnam=varin(1:length(varin));
	vunits=varunit(1:length(varin));
	instid=instru(1:length(varin));

		data=data(:,varnum);								% file with selected variables
end															% end if nargin == 1

%	-----------------------------------------------------------------------

if (nargin == 0)											% input from dialog boxes

																% get the input ascii time series
[ascnam,ascpath]=uigetfile('*.asc*;*.nb*;*.txt;*.data','Get ASCII file with no headers');
innam=[ascpath,ascnam];

																% create output file

ipfdot=find(ascnam == '.');
sugnam=['a',ascnam(1:ipfdot),'cdf'];				% suggested output file name
[outnam,outpath]= uiputfile(sugnam,'name and create output file');
netnam=[outpath,outnam];

f=netcdf(netnam,'clobber');							%open netCDF file 

	ascfile=[ascpath,ascnam];
	eval(['load ',ascfile,';']);						% load input data
	dot=find(ascnam =='.');								% need to eliminate suffix
	dat=ascnam(1:dot-1);									% get matlab data name

	if dat(1) >= '0' & dat(1) <= '9'					% if input name starts with number Matlab
		eval(['data=X',dat,';']);						% will put an 'x' in front of the name
	else												
		eval(['data=',dat,';']);
	end

	siz=size(data);										% get size of file


		makipf=1;
		prompt={'Select which columns to include:',...     	 
		 'Enter the latitude          (negative = South):',...
    	 'Enter the longitude         (negative = Weat) :',...
     	 'Enter magnetic variation    (negative = West):',...
     	 'Enter instrument depth      (in meters):',...
     	 'Enter the water depth       (in meters):',...
     	 'Enter start time yyyy mm dd hh mm ss in GMT/Z/CUT:',...
     	 'Enter the sample rate in seconds 86400s = 1 day :'};
   		def={'3 4 5','55 30','-66.45','-14.8','2001.5','5495',...
					'1991 1 13 12 07 30','86400'};
   		title=['Input parameters for ASC2CDF '];
   		lineNo=1;
   		inputa=inputdlg(prompt,title,lineNo,def);

% convert from cell to either character or number

		zvarnum=char(inputa(1));		varnum=str2num(zvarnum);
												f.inlat=char(inputa(2)); 			
												f.inlon=char(inputa(3)); 		
		zmv=char(inputa(4)); 			f.magnetic_variation=ncfloat(str2num(zmv));	
		zidepth=char(inputa(5)); 		f.inst_depth=ncfloat(str2num(zidepth));
      zwdepth=char(inputa(6)); 		f.water_depth=ncfloat(str2num(zwdepth));
      										f.WATER_DEPTH=nclong(str2num(zwdepth));
		zt0=char(inputa(7)); 			f.tstart=ncfloat(str2num(zt0));	
		ztsamp=char(inputa(8)); 		f.samp_rate=ncfloat(str2num(ztsamp));
												f.DELTA_T=ncchar(ztsamp);


		prompt={'Enter the experiment name:',...
    		'Enter the mooring type:',...
    		'Enter the name of the Principal Investigator:',...
     	 	'Enter any comment:'};
   		def={'test of program','subsurface','S. Tarbell','testing, testing, testing'};
  		title=['More input parameters for ASC2CDF '];
   		inputb=inputdlg(prompt,title,lineNo,def);

% convert from cell to either character or number
		f.EXPERIMENT=char(inputb(1));
		f.moortype=char(inputb(2));
		f.scipi=char(inputb(3));
		f.DATA_CMNT=char(inputb(4));

% need to cycle the number of variables through this loop

varin=[];	varunit=[];	instru=[];  epcode=[]; sdepth=[]; 		%initialize
senid='v-999';
		for i=1:length(varnum)
			prompt={['Enter the ',int2str(i),' variable name:'],...
	    		 'Enter the units:',...
	     	 'Enter the epic code:',...
			 'Enter the sensor identifier:',...
			 'Enter the sensor depth in meters:'};
			str=(['var_no_',int2str(i)]);
	   		def={str,'cm/sec','999',senid,zidepth};
	   		lineNo=1;
	   		title=['Variable number ',int2str(i)];
	   		inputc=inputdlg(prompt,title,lineNo,def);


				varin=[varin,inputc(1)];
				varunit=[varunit,inputc(2)];
				epcode=char(epcode,inputc{3});
				instru=[instru,inputc(4)];
				senid=inputc{4};
				sdepth=char(sdepth,inputc{5});
		end	
				
				epcode(1,:)=[];
				epcode=str2num(epcode);
				sdepth(1,:)=[];

				varnam=varin(1:length(varin));
				vunits=varunit(1:length(varunit));
				instid=instru(1:length(instru));


				data=data(:,varnum);						%file with selected variables

end															% end if nargin == 0

%	-----------------------------------------------------------------------

if nargin > 1												% get info from command line
	cmmd=1;													% command line
	eval(['load ',innam,';']);							% load the ascii file

	slash=find(innam == '\');							% find slashes to remove path
	if(isempty(slash) == 1) 							% if no slashes
		ascpath=cd;
		dot=find(innam == '.'); 
		ascnam=innam(1:dot-1); 
	else														% if there are slashes
		ascpath=innam(1:slash);
		lastsl=slash(length(slash));
		dot=find(innam == '.'); 
		ascnam=innam(lastsl+1:dot-1);
	end

	if ascnam(1:1) > '0' & ascnam(1:1) <= '9'		% an 'X' was placed in front of name
		eval(['data=X',ascnam,';']);					% change file name to generic 'data'
	else
		eval(['data=',ascnam,';']);
	end

	data=data(:,varnum);									% file with selected variables

	siz=size(data);										% get size of file


																% separate path from outputname
	slash=find(netnam == '\');							% find slashes
	if(isempty(slash) == 1) 							% if no slashes
		outpath=cd;
		outnam=netnam; 
	else														% if there are slashes
		outpath=innam(1:slash);
		lastsl=slash(length(slash));
		outnam=netnam(lastsl+1:length(netnam));
	end


	f=netcdf(netnam,'c');  

   f.inlat=slat;							f.inlon=slon;						f.magnetic_variation=ncfloat(mv);
  	f.tstart=ncfloat(start);			f.samp_rate=ncfloat(dt);		f.DELTA_T=ncchar(num2str(dt));
  	f.water_depth=ncfloat(wdepth);	f.WATER_DEPTH=nclong(wdepth);	f.inst_depth=ncfloat(idepth);
  	f.DATA_CMNT=cmt;				  		f.scipi=scipi;						f.EXPERIMENT=exper;
  	f.moortype=mtype;
	f.sensor_depth=ncfloat(sdepth);
end															% end if nargin >1

% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

% *********************************************************************************
% ************ have data and input parameters   *********  create output **********
% Define dimensions

f('depth')=1;	f('lat')=1; 	f('lon')=1;	f('time')= 0;

																% Global parameters needed for epic
f.CREATION_DATE = ncchar(datestr(now,0))		% time of file creation
f.DATA_TYPE=ncchar('TIME');
f.DATA_SUBTYPE = ncchar(' ');
f.DATA_ORIGIN = ncchar('USGS/WHFC');
f.COORD_SYSTEM = ncchar('GEOGRAPHICAL');
f.WATER_MASS = ncchar('?');
f.PROJECT = ncchar(' ');
f.COMPOSITE = nclong(0);
f.POS_CONST = nclong(0);
f.DEPTH_CONST = nclong(0);
f.DESCRIPT = ncchar(' ');
f.DRIFTER = nclong(0);
f.FILL_FLAG = nclong(0);
f.history = ncchar('Converted from ascii to EPIC by asc2epic.m');
f.meters_from_bottom=ncfloat(f.water_depth(:)-f.inst_depth(:));

% convert lat and lon to both decimal degrees & deg min

[latr,lonr]=convertll(f.inlat(:),f.inlon(:));

f.latitude=ncfloat(latr);
f.lat_dm=num2str(latr);
f.longitude=ncfloat(lonr);
f.lon_dm=num2str(lonr);

% Define the scientific variables and attributes

  varmin=min(data(:,:));											% get min/max values
  varmax=max(data(:,:));
  
 eps = netcdf('ep_standard.nc','read'); 
  
for i=1:length(varnum)                             
   copy(eps{varnam{i}},f,0,1);
	f{varnam{i}}.units=ncchar(vunits{i});      				% define an attribute
	f{varnam{i}}.epic_code=nclong(epcode(i,:));        	%   "         "
	f{varnam{i}}.serial_number=ncchar(instid{i});       	%   "         "
	f{varnam{i}}.sensor_depth=ncfloat(sdepth(i,:));       %   "         "
	f{varnam{i}}.minimum=ncfloat(varmin(:,i));      		%   "         "  
	f{varnam{i}}.maximum=ncfloat(varmax(:,i));       		%   "         "  
end


% Create the time base

t0=(f.tstart(4)*3600 + f.tstart(5)*60 + f.tstart(6));  		% hhmmss
julian0=floor(julian(f.tstart(:)));									% julian day
tim=[t0:f.samp_rate(:):t0+(length(data)*f.samp_rate(:)-1)];	% create time base

tim2=rem(tim,3600*24)*1000;											% separate into time2 
tim1=floor(tim/(3600*24))+julian0;									% and time (date)

copy(eps{'time'},f,0,1);
copy(eps{'time2'},f,0,1);
copy(eps{'lat'},f,0,1);
copy(eps{'lon'},f,0,1);
copy(eps{'depth'},f,0,1);

close (eps)

% now write out the data for all variables

len=size(data,1);
f{'time'}(1:len)=tim1;		            							%get date (julian day)
f{'time2'}(1:len)=tim2;            									%get time (seconds)
f{'depth'}(1)=sdepth(1,1);
f{'lat'}(1)=latr;
f{'lon'}(1)=lonr;

for k=1:length(varnum)
	f{varnam{k}}(1:len)=data(:,k);
end

% *********************************************************************************
% ***************** time series created ******* now create more Global attributes ******
%								create elapsed time for data
		fcn = length(data);
		startdate=f{'time'}(1); 						% get date of first dc
		stopdate=f{'time'}(fcn);			% get date of last dc
      elapday=stopdate-startdate;					% number of elapsed days
      
      fstime = ep_datenum([f{'time'}(1) f{'time2'}(1)]);
		fltime = ep_datenum([f{'time'}(fcn) f{'time2'}(fcn)]);

		f.start_time=datestr(fstime,0);
		f.stop_time=datestr(fltime,0);
		f.elapsed_days=elapday;
	

close(f);													% Close netCDF file (not ncclose)

% print output log file
% create log name, 
% the same as the output file name except with a .log extension
%--------------------------------
dott=find(netnam == '.');
lognam=netnam(1:dott-1);
namstr=([lognam,'.log']);
loga=fopen(namstr,'w');

str=['Output created by program asc2epic.       '];   % first output line
str=[str '      Log creation date ' datestr(now)];
fprintf(loga,'\n\n\n%s\n\n',str);

str=(['Name of ASCII input file:           ',ascpath,'\',ascnam]);
fprintf(loga,'%s\n',str);

str=(['Name of NETcdf/EPIC output file:    ',outpath,'\',outnam]);
fprintf(loga,'%s\n\',str);

if(cmmd == 1)														% command line input
	fprintf(loga,'Input parameters came from the Matlab command line.\n\n');
elseif (makipf == 1)												% create online
	fprintf(loga,'Input parameters created using asc2cdf dialog boxes.\n\n');
else																	% from IPF file
	ipfid=[ipfpath,ipfnam];
	str=(['Name of Input Parameter File (IPF): ',ipfpath,'\',ipfnam,'.m']);
	fprintf(loga,'%s\n\n',str);
end

 fclose('all');

loga=fopen(namstr,'a');												% append ncdump to log file
ncdump(netnam,loga);
fclose('all');

%****************************************************************************
%****************************************************************************
%****************************************************************************

function [hms]=ms2hms(secs)	% converts seconds to integer hour,minute,seconds

sec=round(secs/1000);
hour=floor(sec/3600);
min=floor(rem(sec,3600)/60);
sec=round(rem(sec,60));
hms=[hour(:) min(:) sec(:)];

%****************************************************************************
%This is an axample of an IPF (Input Parameter File). 
%It may be used as a template for creating actual IPF files
%
%	restriction - the IPF (Input Parameter File) needs to be either in the 
%				directory in use or in the search path.
%

% opening input and output files
%ascfile	='9123e1dg24.data';				% input data name including path
%eval(['load ',ascfile,';']);				% load input data
%netnam	='9123e1dg24.cdf';				% output name including path
%f=netcdf(netnam,'clobber');				% open netCDF file
%
%f=fatnames(f,100);							% for program purposes
%
% specifying global information
%f.inlat  =ncchar(['31 4.88S']);					% can be either deg and minutes or
%f.inlon  =ncchar(['39 9.40W']);					% or in decimal degrees
%f.tstart =ncfloat([1991 01 13 12 07 30]);		% yyyy mm dd hh mm ss 
%f.samp_rate   =ncfloat(86400);						% in seconds
%f.DELTA_T=ncchar('86400');
%f.water_depth =ncfloat(4160);						% in meters
%f.inst_depth  =ncfloat(4060);						% in meters
%f.DATA_CMNT     ='Testing';					% any comment
%f.magnetic_variation  =ncfloat(-20.5);			% magnetic variation, negative is west
%f.scipi    ='Susan A. Tarbell';			% responsible scientist
%f.EXPERIMENT    ='test asc2cdf';				% experiment designation
%f.moortype ='subsurface';					% mooring type
% other f.xxx may be included, but the above are required
%f.MOORING = ncchar('1234');
%f.INST_TYPE = ncchar('VACM');
%f.VAR_DESC = ncchar('v:v:t');

% specifying variable information
%varnum=[3 4 5];								% column numbers of input variables
%epcode=[999 ;998; 997];					% epic code values
%sdepth=[304.5; 1000; 2050.75];			% sensor depth if different from instrument depth
%% creating cell arrays {} so one doesn't have to worry about length of variable
%varin={'east' 'north' 'temp'};
%varunit={'cm/sec'; 'cm/sec' ;'deg C.'};
%instru={'v-999' 'v-999' 't-9999'};		% instrument and sensor identifiers
%end of IPF
%****************************************************************************
