function nc_to_odv_surf

%warning off all; disp('Warnings being turned off- may hurt debugging');

thispath = fileparts(mfilename('fullpath'));
if(isempty (strfind(path, 'mexnc'))),
    path(path,[thispath '/../mexnc/']);
end
if(isempty (strfind(path, 'netcdf_toolbox'))),
    path(path,[thispath '/../netcdf_toolbox/netcdf/']);
    path(path,[thispath '/../netcdf_toolbox/netcdf/ncsource']);    
    path(path,[thispath '/../netcdf_toolbox/netcdf/nctype']);
    path(path,[thispath '/../netcdf_toolbox/netcdf/ncutility']);
end

nc_directory = '/hosts/tornado/ssdsdata/deployments/m0/200607';
%nc_directory = 'c:/ocr3000/NC_ascii/netcdf';

nc_filename = 'hourlyM0_20060731.nc';
    
fid = fopen([nc_directory '/' nc_filename]);
if(fid == -1)
    disp([nc_filename ' does not exist']);
    close;
    return;
end
fclose(fid);
nc_file = netcdf([nc_directory '/' nc_filename]);

variables = ncnames(var(nc_file));

epoch_start = datenum('1-jan-1970');


%finds all variables in nc_file that contain the string 'TIME'.  Puts these
%into a cell array, then searches the cell array for the first instance.
%Assumes this is the time array used to index the netcdf file.  
time_cell = strfind(ncnames(var(nc_file)), 'TIME');

for k = 1:length(time_cell),
    if isempty(time_cell{k})==0,
        time_index = k;
        break,
    end
end

time_esecs = nc_file{time_index}(:);

time_edays = time_esecs/(3600*24);

time = epoch_start + time_edays;

mdy = datestr(time, 'mm/dd/yyyy');
hhmm = datestr(time, 'HH:MM');

for row = 1:length(time),
    cruise{row,1} = nc_file.PLATFORM_NAME(:);
    cruise{row,2} = num2str(row);
    cruise{row,3} = '*';
    cruise{row,4} = mdy(row,1:10);
    cruise{row,5} = hhmm(row,1:5);
end

%depths_adcp = nc_file{'DEPTH_ADCP_HR'}(:);

%for dn=0:length(depths_adcp)-1,
%    east_adcp_name{dn*2+1} = ['EASTWARD_SEA_WATER_VELOCITY_HR_' num2str(depths_adcp(dn+1))];
%    east_adcp_name{dn*2+2} = 'QF';
%    north_adcp_name{dn*2+1} = ['NORTHWARD_SEA_WATER_VELOCITY_HR_' num2str(depths_adcp(dn+1))];
%    north_adcp_name{dn*2+2} = 'QF';
%end

depths_ts = nc_file{'DEPTH_TS_HR1'}(:);

%for dn=0:length(depths_ts)-1,
%    temp_name{dn*2+1} = ['SEA_WATER_TEMPERATURE_HR_' num2str(depths_ts(dn+1))];
%    temp_name{dn*2+2} = 'QF';
 %   temp_name{dn*2+1} = ['

%    sal_name{dn*2+1} = ['SEA_WATER_SALINITY_HR_' num2str(depths_ts(dn+1))];
%    sal_name{dn*2+2} = 'QF';
%end

header = {'Cruise', 'Station', 'Type', 'mon/day/yr', 'hh:mm',...
    'Longitude [degrees_east]', 'Latitude [degrees_north]', 'Bot.Depth [m]',...
    'DaysSince2003[d]','SensorDepth[m]','QF', 'Temperature[�C]', 'QF', 'Salinity', 'QF', ...
    'AIR_PRESSURE_HR', 'QF', 'AirTemperature[�C]',...
    'QF', 'EASTWARD_WIND_HR','QF', 'NORTHWARD_WIND_HR', 'QF', ...
    'WindSpd[m/s]', 'QF', 'WindDir[�]', 'QF'};%,...
%    temp_name{:}, sal_name{:}, east_adcp_name{:}, north_adcp_name{:}};
    
    %'Temperature[�C]', 'QF', 'Conductivity', 'QF', ...
    %'Pressure', 'QF', 'Salinity', 'QF'};


days_2003 = time - (datenum('1-Jan-2003'));
lon(1:length(time),1)= nc_file{(strmatch('LONGITUDE', ncnames(var(nc_file)),'exact'))}(:);
lat(1:length(time),1)= nc_file{(strmatch('LATITUDE', ncnames(var(nc_file)),'exact'))}(:);

%d_index = strmatch('DEPTH_TS_HR', ncnames(var(nc_file)),'exact');
depth(1:length(time),1) = 0; %sets "Bot.Depth" to 0, purely for file structure benefit

%exist = isempty(nc_file{'NominalDepth'}(:));
%if exist==0,
%    depth(1:length(time),1) = nc_file{'NominalDepth'}(:);
%else
%    depth(1:length(time),1) = nc_file{'DEPTH_TS_HR'}(:);

data(:,1) = lon;
data(:,2) = lat;
data(:,3) = depth;
data(:,4) = days_2003;
data(:,5) = depth; %for now sensor depth is all 0, since this is the surface file... 



air_press_index = strmatch('AIR_PRESSURE_HR', ncnames(var(nc_file)));

%adds air temperature and wind speed
for v = air_press_index:air_press_index+3,
    if strcmp(ncnames(nc_file{v}), 'AIR_TEMPERATURE_HR'),
        m = strmatch('AirTemperature[�C]', header);
    else
        m = strmatch(ncnames(nc_file{v}), header); %searches "header" cell array for var_name
    end
    data(1:length(time),m-5) = nc_file{v}(:); %puts data in appropriate place in data array
    data(1:length(time),m-4) = 0; % sets QF to 0 for now...
    if v==air_press_index+2,
        east_wind_temp  = nc_file{v}(:); % puts east speed into a temporary array
    elseif v==air_press_index+3,
        north_wind_temp = nc_file{v}(:); % puts north speed into a temporary array
    end
end
for w = 1:length(east_wind_temp),
    wind_speed(w) = ((east_wind_temp(w))^2 + (north_wind_temp(w))^2)^0.5;
    if (north_wind_temp(w)>=0 && east_wind_temp(w)<0),
        direc = 450-((180/pi)*atan2(north_wind_temp(w),east_wind_temp(w)));
    else
        direc = 90-((180/pi)*atan2(north_wind_temp(w),east_wind_temp(w)));
    end
    wind_direc(w) = direc;
end
n = strmatch('WindDir[�]', header);
data(1:length(time),n-5) = wind_direc(:);
data(1:length(time),n-4) = 0;
m = strmatch('WindSpd[m/s]', header);
data(1:length(time),m-5) = wind_speed(:); %puts data in appropriate place in data array
data(1:length(time),m-4) = 0; % sets QF to 0 for now...



%east_index = strmatch('EASTWARD_SEA_WATER_VELOCITY_HR', ncnames(var(nc_file)));

%east = nc_file{east_index}(:);
%north = nc_file{east_index+1}(:);

%for d = 0:length(depths_adcp)-1,
%    m = strmatch(east_adcp_name(d*2+1), header, 'exact'); %searches "header" cell array for var_name
%    data(1:length(time),m-5) = east(:,d+1);
%    data(1:length(time),m-4) = 0;
%end    

%for d = 0:length(depths_adcp)-1,
%    m = strmatch(north_adcp_name(d*2+1), header, 'exact'); %searches "header" cell array for var_name
%    data(1:length(time),m-5) = north(:,d+1);
%    data(1:length(time),m-4) = 0;
%end    



sea_temp = nc_file{'SEA_WATER_TEMPERATURE_HR'}(:);
sea_sal = nc_file{'SEA_WATER_SALINITY_HR'}(:);

%adds surface temperature and salinity from the TS string.  Since the TS
%string has NaN values for TS at the surface, this is useless.  Go figure.
%Need to find a different source for temp and salinity.
for d = 0:0,%length(depths_ts)-1,
    m = strmatch('Temperature[�C]', header, 'exact');
    data(1:length(time),m-5) = sea_temp(:,d+1);
    data(1:length(time),m-4) = 0;
    n = strmatch('Salinity', header, 'exact'); %searches "header" cell array for var_name
    data(1:length(time),n-5) = sea_sal(:,d+1);
    data(1:length(time),n-4) = 0;
    %    m = strmatch(temp_name(d*2+1), header, 'exact'); %searches "header" cell array for var_name
%    data(1:length(time),m-5) = sea_temp(:,d+1);
%    data(1:length(time),m-4) = 0;
end    

%for d = 0:length(depths_ts)-1,
%    m = strmatch(sal_name(d*2+1), header, 'exact'); %searches "header" cell array for var_name
%    data(1:length(time),m-5) = sea_sal(:,d+1);
%    data(1:length(time),m-4) = 0;
%end    

%delete ('M0Surf.txt');
%tblwrite_smb(data, header, cruise, 'M0Surf.txt','\t',4)
%delete ([nc_directory '/ascii_output/M0Surf.txt']);
tblwrite_smb(data, header, cruise, [nc_directory '/ascii_output/M0Surf.txt'],'\t',4, 0)


end