% b1_convert_mc_ncwind.m

% reads 10Hz metcity tower wind files and saves to matlab table format

clear
close all
data_path = '/Users/byronb/Raw_Data_Files.noindex/MOSAIC_2019/_processed/metcity_tower_level2_v4/10Hz_turb_L2V4';
files = dir(fullfile(data_path,'*.nc'));

dt = [datetime(2019,11,13),datetime(2019,11,14)]; w = [1,1]; % dummy data for initial plot
figure; p1 = plot(dt,w); grid on; grid minor;
ylabel('W velocity m/s'); xlabel('Time');
p1.XDataSource = 'dt';
p1.YDataSource = 'w';

for ii = 1:length(files)
    display(['Loading ',files(ii).name])
    
    dfl = fullfile(data_path,files(ii).name);
    son_nc = ncstruct(dfl);
    
    % basetime to bring it to 0000Z on current day
    bt = datenum(1970,01,01,00,00,00) + double(son_nc.base_time)/86400;
    dnum = bt + son_nc.time_offset/86400/1000;
    dt = datetime(dnum,'convertfrom','datenum');
    
    son = table(dt,dnum);
    son_nc = rmfield(son_nc,{'base_time','time','time_offset'});
    son_nc = struct2table(son_nc);
    son = [son,son_nc];
    son = standardizeMissing(son,-9999);
    clear son_nc dnum dt bt
    
    sfl = fullfile(data_path,['metcity_wind_',files(ii).name(30:37),'.mat']);
    save(sfl,'son');
    
    dt = son.dt;
    w = son.metek_10m_w;
    refreshdata
    drawnow
end