% en435_read_ctd_headers.m
% R/V Endeavor CTD list from CTD datafiles headers
%
% I N P U T:
%
% CTD data,
% FILENAME: en435***.cdn where *** is station #
% VARIABLES:
% 1 - timeS: time [elapsed seconds]
% 2 - pr: pressure [db]
% 3 - t090: temperature, pri, ITS-90 [deg C]
% 4 - c0S/m: conductivity, primary [S/m]
% 5 - t190: temperature, sec, ITS-90 [deg C]
% 6 - c1S/m: conductivity, secondary [S/m]
% 7 - upoly0: userpoly 0 (from Voltage 0: Fluorescence (mkg/L))
% 8 - upoly1: userpoly 1 (from Voltage 1, Turbidity)
% 9 - par: irradiance (PAR) (mikro Einsteins/m^2sec^2)
% 10 - xmiss: transmissometer
% 11 - alt: altimeter [m]
% 12 - sbeox0ML/L: Oxygen, SBE, primary [ml/l]
% 13 - spar: surface irradiance (SPAR) (mikro Einsteins/m^2sec^2)
% 14 - sal00: salinity, PSS-78 [PSU], T0, C0
% 15 - sal11: salinity, PSS-78 [PSU], T1, C1
% 16 - sigma-é00: density, sigma-theta [kg/m^3], T0, C0
% 17 - sigma-é11: density, sigma-theta [kg/m^3], T1, C1
% 18 - flag:  0.000e+00
%
% O U T P U T:
% CTD list datafile,
% FILENAME: en435_ctd_list.mat (or .txt)
% VARIABLES:
% 1 - station #
% 2 - year
% 3 - month
% 4 - day
% 5 - hours
% 6 - minutes
% 7 - seconds
% 8 - decimal latitude 
% 9 - decimal longitude

clear;
flops(0);

firstStation=1;
lastStation=166;
ctdList=firstStation:1:lastStation;
% Save Option.
saveOption='yes';
%saveOption='no';
%--------------------------------------------------------------------------
% ---- Path, names.
path_name_en435='~/en435/CTD/process/'; % USE YOUR PATH here
path_name=path_name_en435;
crNum=435;
cr_name='Endeavor 435';
cr_date='May (17-31), 2007';
% ---- CTD data files info.
headerLastLineNum=113;
year=2007;
% ---- CTD data files Headers info.
stationNumberCharacters = [31:33];
line_stNum=2;
stationDateCharacters = [24:43];
line_date=9;
stationLatCharacters = [19:26];
line_lat=10;
stationLonCharacters = [20:28];
line_lon=11;
%*******************
% LOOP: Station#
%******************* 
header_end_info=strvcat(blanks(1),...
    ['Assume that all CTD files have ' num2str(headerLastLineNum) ' lines and the last one is "END"'],...
    ['CHECK: is this correct?']);
ctdInfo=[];
for stNum = ctdList   % i - CTD Station#
   % Write input file names to string.
   if stNum < 10
      j = ['ctd00' num2str(stNum) '.cdn'];
   elseif  stNum >=10 & stNum<100
      j = ['ctd0' num2str(stNum) '.cdn'];
   else 
      j = ['ctd' num2str(stNum) '.cdn'];
   end
   % Open file for reading.
   pfid = fopen([path_name j]);
   if pfid<0
       disp(['Station# ' num2str(stNum) ' not found']);
       return;
   end
   % First lines are headers.
   for lineNum=1:headerLastLineNum
      % Read line from file header, keep new line character (fgets).
      line=fgets(pfid);
      % Get CTD Info from line.
      if lineNum==line_stNum
         % Station number.
         lineInfo = line(stationNumberCharacters);
         ctdNum = str2num(lineInfo);
         if isempty(ctdNum) | ctdNum~=stNum
         % ----- C O R R E C T I O N  1 ----
            % I know that the following Headers have mistakes in filename:
            %   (1) Name in the header line is ctd0048.dat instead of ctd048.dat
            %   (2) Name in the header line is ctd0049.dat instead of ctd049.dat
            %   (3) Names in the header line is ctdgom1.dat instead of ctd126.dat
            %   (4) Names in the header line is ctdgom2b.dat instead of ctd127.dat
            if stNum==48 | stNum==49 | stNum==126 | stNum==127
               ctdNum=stNum;
            % ----- C O R R E C T I O N  1  END ----
            else
               disp(['Station #' num2str(stNum) ': WRONG Station# in the Headers, line #' num2str(lineNum)]);
               return;
            end
         end % For if isempty(ctdNum) | ctdNum~=stNum
      elseif lineNum==line_date
         % Date.
         lineInfo = line(stationDateCharacters);
         ctdMonName = lineInfo(1:3);
         ctdDay = lineInfo(5:6);
         ctdYear = lineInfo(8:11);
         if strcmp(ctdMonName,'Apr')
            ctdMon='4';
         elseif strcmp(ctdMonName,'May')
            ctdMon='5';
         elseif strcmp(ctdMonName,'Jun')
            ctdMon='6';
         elseif strcmp(ctdMonName,'Jul')
            ctdMon='7';
         elseif strcmp(ctdMonName,'Aug')
            ctdMon='8';
         elseif strcmp(ctdMonName,'Sep')
            ctdMon='9';
         else
            disp([ctdMonName ' ?']);
            return;
         end
         if strcmp(ctdYear,num2str(year))
            ctdYear=num2str(year);
         else
            disp([ctdYear ' ?']);
            return;
         end
         ctdDate = strcat(ctdMon,ctdDay,ctdYear);
         ctdDate = str2num(ctdDate);
         ctdYear_num=str2num(ctdYear);
         ctdMon_num=str2num(ctdMon);
         ctdDay_num=str2num(ctdDay);
         % Time.
         ctdTime = lineInfo(13:20);
         ctdTime_num = str2num(strrep(ctdTime,':',char(9)));
      elseif lineNum==line_lat
         % Latitude.
         lineInfo=line(stationLatCharacters);
         ctdLat_num = str2num(lineInfo);
         lat_deg = ctdLat_num(1);
         lat_min = ctdLat_num(2);
         ctdLat = lat_deg + lat_min/60;            
      elseif lineNum==line_lon
         % Longitude.
         lineInfo=line(stationLonCharacters);
         ctdLon_num = str2num(lineInfo);
         lon_deg = ctdLon_num(1);
         lon_min = ctdLon_num(2);
         ctdLon = lon_deg + lon_min/60;
      end
      % ---- CHECK HEADERS last line ------------------------------------- 
      % I know for now - all stations have 113 HEADERS LINES,
      % if there are stations with other number, check the problem and make
      % correction here:
      % ----- CORRECTION 2 ----
      %if crNum==185 & stNum==18
      %   headerLastLineNum_st=96;
      % ----- CORRECTION 2 END ----
      %else
         headerLastLineNum_st=headerLastLineNum;
      %end
      if lineNum==headerLastLineNum_st & ~strcmp(line(1:5),'*END*')
         disp([j ', Bad Headers END, CHECK the number of lines']);
         return;
         % To have the info - all stations with "bad" numbers of headers lines - use this
         % code:
         %header_end_info=strvcat(header_end_info,[j ', Bad Headers END, CHECK the number of lines']);
      end 
      % ---- CHECK HEADERS last line   END -------------------------------- 
   end % For for lineNum=1:
   % Close open file.
   fclose(pfid);
   % Make a matrix with CTD Info.
   ctdInfo = [ctdInfo;ctdNum ctdYear_num ctdMon_num ctdDay_num ...
              ctdTime_num ctdLat ctdLon];
end % for i = 1:lastStation
%******************* 
% Write a matrix to an ASCII delimited or MAT file.
%******************* 
if strcmp(saveOption,'yes')
    % (1) Save data matrix as ASCII tab delimited file with the first
   % commented line - headers.
   variables_out='% Station#, year, month, day, hour, minute, second, latitude (degN), longitude (degW)';
   % Write formated data to file- matrix only.
   dlmwrite(['TEMP.txt'],ctdInfo,'\t');
   % Make text file - headers.
   pfid=fopen('headers.txt','w');
   fprintf(pfid,'%s\n',[variables_out]);
   fclose(pfid);
   % Concatenate 'Headers' file and 'Matrix' file.
   p=['! cat headers.txt TEMP.txt > en' num2str(crNum) '_ctd_list.txt'];
   eval(p);
   % Remove text files - headers and TEMP.
   ! rm headers.txt  TEMP.txt
   % (2) Save as MATfile.
   p=['en' num2str(crNum) '_ctd_list=ctdInfo;'];
   eval(p);
   p=['save en' num2str(crNum) '_ctd_list en' num2str(crNum) '_ctd_list;'];
   eval(p);
   % Display info:
   disp(strvcat(blanks(1),['OUTPUT FILE SAVED, filename: en' num2str(crNum) '_ctd_list.mat and .txt, size ' num2str(size(ctdInfo))],...
     'Variables name:','Station#, year, month, day, hours, minutes, seconds, decimal latitude, decimal longitude',...
     ['Location: ' path_name],... 
     blanks(1)));
   p=['! mv en' num2str(crNum) '_ctd_list.* ~/en435/CTD/process/'];
   eval(p);
else
   disp(strvcat(blanks(1),'Output matrix - CTD stations list (FILE NOT SAVED).','OUTPUT matrix:',['name = ctdInfo, size = ' num2str(size(ctdInfo))],...
     'Variables name:','Station#, year, month, day, hours, minutes, seconds, decimal latitude, decimal longitude',...
     blanks(1)));
end % For if strcmp(saveOption,'yes')
%******************* 
% Display 'bad' headers info.
%******************* 
if size(header_end_info,1)>3
    disp([header_end_info]);
end
