% 
% FILE: ascii_to_eps.m
%
%	This is an example MATLAB script which reads in an ASCII
%	formatted data file using Mex-function ascii2mat() and 
%	writes out variables in netCDF file using Mex-function 
%	mexeps().
%
%-----------------------------------------------------------------
%Willa Zhu                           Phone:    206-526-6208
%NOAA/PMEL/OCRD                      FAX:      206-526-6744
%7600 Sand Point Way NE              OMNET:    TAO.PMEL
%Seattle, WA 98115                   Internet: willa@pmel.noaa.gov
%-----------------------------------------------------------------

% reads input data file -----------------------------------------
file_name = 'data/fu6b.wnd';			%input data file name
time_format = 'x,i2,i2,x,i2,i2,i2';	%hr, min, day, mon, yr
data_format = '3f8.2,f6.1,f6.2,6x,f6.2,18x,f6.1';
full_format = [time_format ',' data_format];     %full data format
nhdr_skip = 1;	 % number of headers in input file to skip 

%nrow_read = 10; %use this as 4th arugment to ascii2mat() if you
	         %want number of rows read to be "nrow_read".

% call ascii2mat() to load variables (by default all rows)
var = ascii2mat(file_name,full_format,nhdr_skip,10);

% variable indices ---------------------------------------------- 
i_uw=6;
i_vw=7;
i_sw=8;
i_dw=9;
i_at=10;
i_rh=11;
i_sr=12;
ind = [i_uw i_vw i_sw i_dw i_at i_rh i_sr];

% assign variable axis -----------------------------------------
lon = 156+0.2/60;  %East
lat = 0.5/60;      %North
depth = 0;

% time axis must be in form of YYmmddhhmmss.fff 
time=(((((var(:,5)*100+var(:,4))*100+var(:,3))*100 ...
      +var(:,1))*100+var(:,2))*100)*100.000;

% assign variable shape --------------------------------------- 
[nt m]=size(time);
var_shape = [1 1 1 nt];

% assign variable code (refer to epic.key file) ---------------
uw_code = 422;
vu_code = 423;
sw_code = 401;
dw_code = 410; 
at_code = 21;
rh_code= 910;
sr_code = 133;
var_code = [uw_code vu_code sw_code dw_code at_code rh_code sr_code];

% create output pointer file and netCDF file ------------------
out_pointer='fu6b.ptr';            % pointer file name
out_datafile = 'fu6b.cdf';         % data file name
mexeps('openw',out_pointer);	   % open pointer file
mexeps('setnextw',out_datafile);   % open data file

% write global attribute in netCDf file ----------------------
time_range = '0100  8 Feb 93   2200  9 Mar 93';
comment1 = 'This is an example of using Mex-function mexeps() to write netCDF file.';
comment2 = 'The original input data is ASCII formatted data file: Fu6b.wnd';
comment3 = 'If you have questions, contact Willa Zhu (willa@pmel.noaa.gov, x6785)';

mexeps('putattval','TIME_RANGE',time_range);
mexeps('putattval','comment1',comment1);
mexeps('putattval','comment2',comment2);
mexeps('putattval','comment3',comment3);

% write out axes and variables ------------------------------
n_var = 7;
for I = 1:n_var
  mexeps('putaxis',var_code(I),1,502,'lon',' ','degree_east','even',lon);
  mexeps('putaxis',var_code(I),2,500,'lat',' ','degree_north','even',lat);
  mexeps('putaxis',var_code(I),3,3,'depth',' ','m','even',depth);
  mexeps('puttaxis',var_code(I),'time',' ',' ','even',time);
  mexeps('putvar',var_code(I),var(:,ind(I)),var_shape);
end

% close pointer file and data files ------------------------
mexeps('close');
