% Created on 23/05/2018; Version 1.0
% Data used from 'E1_buoy.csv'

% Function to load E1 buoy data from 2013-2017 from the Western Channel
% Observatory 

% Function output 'data' - saves multiple variables into
% the same structure as 'data.variable'

% NB: Run 'E1_datetime.m' after to combine date and time into one parameter

% Example: data=load_E1_buoy_data('E1_buoy.csv')

%%
function data=load_E1_buoy_data(filename)

data.filename=filename

% open the data file - fid allocates file with file identifier
fid=fopen(filename); 

for i=1:2
    tline = fgetl(fid); %read a line of text
end

% Header - Date (YYMMDD), Time (HHMMSS), Latitude (DDMM.MMMM), Longitude (DDDMM.MMMM), Average wind speed (Kn), Average wind direction (deg), Max gust (Kn),Sea Temperature (degC), Air Temperature (degC), Humidity (%), Dew point (degC), Pressure (hPa), Latitude (DDMM.MMMM), Longitude (DDDMM.MMMM), Number of zero crossings, Havg (m), Tz (mean spectral period - s), Hmax(m), Hsig(m), Tsig(s), H10(m), T10(m), Tavg(s), TP(s), TP5, HMO, Mean direction (deg), Mean spread (deg), Date (YYMMDD), Time (HHMMSS), N PAR, Mean PAR (umol/photons/m2/s), SD PAR (umol/photons/m2/s),PAR_FLAG, N CDOM, Mean CDOM (QDE ppb), SD CDOM (QDE ppb),CDOM_FLAG, N Dissolved Oxygen, Mean Dissolved Oxygen (uM), SD Dissolved Oxygen (uM),OXYGEN_FLAG, N SST, Mean SST (degC), SD SST (degC),SST_FLAG, N Salinity, Mean Salinity (PSU), SD Salinity (PSU),SAL_FLAG, N Turbidity, Mean Turbidity (NTU), SD Turbidity (NTU),TURB_FLAG, N Chlorophyll fluorescence, Mean Chlorophyll (mg/m3), SD Chlorophyll (mg/m3),CHL_FLAG,,
% Line 1 - 130611,110005,5002.6100N,00422.5049W,14.2,240,19.9,,13,95.2,12.3,1011.42,5002.6100N,00422.5049W,183,1.08,5.9,2.37,1.65,7.1,1.95,7.1,5.7,8.3,7.9,1.75,219.6,29.8,130611,111000,120,633.42,11.81,0,120,1.89,0.13,,120,283.71,0.05,,120,13.01,0,,120,35.31,0,,120,0.41,0.02,,120,0.7,0.04,,,

J=1

while ischar(tline) & ~isempty(tline) % this tests to see if you are at the end of the file and sees if the line is empty
    tmp=textscan(tline,'%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','delimiter',',');
    
% Metadata %
    data.date(J)=tmp{1}; % date yy/mm/dd
    data.time(J)=tmp{2}; % time hh/mm/ss
    
    data.lat(J)=tmp{3}; % latitude DDMM.MMMM 
    data.long(J)=tmp{4}; % longitude DDMM.MMMM
    
% Parameter data - Met Office %
    data.avws(J)=tmp{5}; % average wind speed
    data.avwd(J)=tmp{6}; % average wind direction 
    data.maxg(J)=tmp{7}; % maximum gust 
    data.seatmp(J)=tmp{8}; % sea temperature (degC)
    data.airtmp(J)=tmp{9}; % air temperature
    data.humid(J)=tmp{10}; % humidity 
    data.dewp(J)=tmp{11}; % dew point 
    data.pres(J)=tmp{12}; % presure 
    
    data.lat2(J)=tmp{13}; % latitude DDMM.MMMM 
    data.long2(J)=tmp{14}; % longitude DDMM.MMMM
    
    data.noZ(J)=tmp{15}; % Number of zero crossings
    data.havg(J)=tmp{16}; % Havg (m)
    data.T2(J)=tmp{17}; % Tz (mean spectral period - s)
    data.hmax(J)=tmp{18}; % Hmax(m)
    data.hsig(J)=tmp{19}; % Hsig(m)
    data.Tsig(J)=tmp{20}; % Tsig(s)
    data.h10(J)=tmp{21}; % H10(m)
    data.T10(J)=tmp{22}; % T10(m)
    data.Tavg(J)=tmp{23}; % Tavg(s)
    data.TP(J)=tmp{24}; % TP(s)
    data.TP5(J)=tmp{25}; %  TP5
    data.hmo(J)=tmp{26}; % HMO
    data.Mndir(J)=tmp{27}; % Mean direction (deg)
    data.Mnspr(J)=tmp{28}; % Mean spread (deg)
    
    data.date2(J)=tmp{29}; % date yy/mm/dd
    data.time2(J)=tmp{30}; % time hh/mm/ss
    
% Parameter data - PML %
    data.Npar(J)=tmp{31}; % N PAR 
    data.Mnpar(J)=tmp{32}; % Mean PAR 
    data.SDpar(J)=tmp{33}; % SD PAR 
    data.PARflag(J)=tmp{34}; % Flag PAR 

    data.Ncdom(J)=tmp{35}; % N CDOM 
    data.Mncdom(J)=tmp{36}; % Mean CDOM 
    data.SDcdom(J)=tmp{37}; % SD CDOM 
    data.CDOMflag(J)=tmp{38}; % Flag CDOM
    
    data.NDissO(J)=tmp{39}; % N Dissolved Oxygen 
    data.MnDissO(J)=tmp{40}; % Mean Dissolved Oxygen 
    data.SDDissO(J)=tmp{41}; % SD Dissolved Oxygen
    data.DissOflag(J)=tmp{42}; % Flag Dissolved Oxygen
    
    data.Nsst(J)=tmp{43}; % N SST 
    data.Mnsst(J)=tmp{44}; % Mean SST 
    data.SDsst(J)=tmp{45}; % SD SST 
    data.SSTflag(J)=tmp{46}; % Flag SST
    
    data.Nsal(J)=tmp{47}; % N Salinity 
    data.Mnsal(J)=tmp{48}; % Mean Salinity 
    data.SDsal(J)=tmp{49}; % SD Salinity 
    data.SALflag(J)=tmp{50}; % Flag Salinity
    
    data.Nturb(J)=tmp{51}; % N Turbidity  
    data.Mnturb(J)=tmp{52}; % Mean Turbidity 
    data.SDturb(J)=tmp{53}; % SD Turbidity 
    data.TURBflag(J)=tmp{54}; % Flag Turbidity
    
    data.Nchl(J)=tmp{55}; % N Chlorophyll  
    data.Mnchl(J)=tmp{56}; % MeanN Chlorophyll 
    data.SDchl(J)=tmp{57}; % SD Chlorophyll 
    %data.Cflag(J)=tmp{58}; % Flag Chlorophyll
    
    J=J+1;  % only increment the data counter if new data read in  
    tline = fgetl(fid);
    
end 

fclose(fid); % close data file


