% Load the ADCP files quickly...
curr_dir = pwd;

% Do 150 kHz first
adcp_150_dir = '/Users/Alain/Desktop/MIO/Outpace/ADCP/Processed_files/150_STA/';
cd(adcp_150_dir);

files = dir('*.nc');
nf = length(files);
ndep = 0;
nobs = 0;
for iFile = 1:nf
    dep = ncread(files(iFile).name,'DEPH');
    if ndep<length(dep)
        ndep = length(dep);
    end
    lon = ncread(files(iFile).name,'LONGITUDE');
    nobs = nobs + length(lon);
end

adcp_150_lon = NaN(1,nobs);
adcp_150_lat = NaN(1,nobs);
adcp_150_u = NaN(ndep,nobs);
adcp_150_v = NaN(ndep,nobs);
adcp_150_u_wo_tide = NaN(ndep,nobs);
adcp_150_v_wo_tide = NaN(ndep,nobs);
adcp_150_time = NaN(1,nobs);
adcp_150_z = NaN(1,ndep);

counter = 1;

for iFile = 1:nf
    tem = ncread(files(iFile).name,'UVEL_ADCP');
    [nz,no] = size(tem);
    adcp_150_lon(counter:counter+no-1) = ncread(files(iFile).name,'LONGITUDE');
    adcp_150_lat(counter:counter+no-1) = ncread(files(iFile).name,'LATITUDE');
    adcp_150_u(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'UVEL_ADCP');
    adcp_150_v(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'VVEL_ADCP');
    adcp_150_u_wo_tide(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'UVEL_ADCP_CORTIDE');
    adcp_150_v_wo_tide(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'VVEL_ADCP_CORTIDE');
    if nz == ndep
        adcp_150_z = -ncread(files(iFile).name,'DEPH');
    end
    time = ncread(files(iFile).name,'DATE_TIME_UTC');
    for iobs = 1:no
        adcp_150_time(counter+iobs-1) = datenum(time(:,iobs)','yyyymmddHHMMSS');
    end
    counter = counter+no;
end

% Now load up the 38 kHz
adcp_38_dir = '/Users/Alain/Desktop/MIO/Outpace/ADCP/Processed_Files/38_STA/';
cd(adcp_38_dir);

files = dir('*.nc');
nf = length(files);
ndep = 0;
nobs = 0;
for iFile = 1:nf
    dep = ncread(files(iFile).name,'DEPH');
    if ndep<length(dep)
        ndep = length(dep);
    end
    lon = ncread(files(iFile).name,'LONGITUDE');
    nobs = nobs + length(lon);
end

adcp_38_lon = NaN(1,nobs);
adcp_38_lat = NaN(1,nobs);
adcp_38_u = NaN(ndep,nobs);
adcp_38_v = NaN(ndep,nobs);
adcp_38_u_wo_tide = NaN(ndep,nobs);
adcp_38_v_wo_tide = NaN(ndep,nobs);
adcp_38_time = NaN(1,nobs);
adcp_38_z = NaN(1,ndep);

counter = 1;

for iFile = 1:nf
    tem = ncread(files(iFile).name,'UVEL_ADCP');
    [nz,no] = size(tem);
    adcp_38_lon(counter:counter+no-1) = ncread(files(iFile).name,'LONGITUDE');
    adcp_38_lat(counter:counter+no-1) = ncread(files(iFile).name,'LATITUDE');
    adcp_38_u(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'UVEL_ADCP');
    adcp_38_v(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'VVEL_ADCP');
    adcp_38_u_wo_tide(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'UVEL_ADCP_CORTIDE');
    adcp_38_v_wo_tide(1:nz,counter:counter+no-1) = ncread(files(iFile).name,'VVEL_ADCP_CORTIDE');
    if nz == ndep
        adcp_38_z = -ncread(files(iFile).name,'DEPH');
    end
    time = ncread(files(iFile).name,'DATE_TIME_UTC');
    for iobs = 1:no
        adcp_38_time(counter+iobs-1) = datenum(time(:,iobs)','yyyymmddHHMMSS');
    end
    counter = counter+no;
end

clearvars -except adcp* curr_dir
clear adcp_150_dir adcp_38_dir
cd(curr_dir);
clear curr_dir
