clear all;
close all;
set_machine_et;

oceandir = [datadir 'DYNAMO/'];
plotdir = [oceandir 'plots/nc_plots/'];

cruise = 'DYNAMO_2011';
version_num = 'v1';
ncfilename =  [oceandir 'DYNAMO_2011_flux_met_sea_stable_10min_' version_num '.nc'];
matfilename =  [oceandir 'DYNAMO_2011_flux_met_sea_stable_10min_' version_num '.mat'];

load(matfilename);

ncload2(ncfilename);
ncdisp(ncfilename);

%% make plots to check file

figure;
plot(t, prate,'o', t, Pcombo_1min,'.');
grid on;
legend('prate 10 min','Pcombo 1 min');
datetick('x','mm/dd');
export_fig([plotdir cruise '_P'],'-png','-r300','-nocrop');

fq = fields(q);
nf = length(fq);
ndepth = length(q.depth);
ntime = length(q.t);
nrl = length(q.rl_start);
ndwl = length(q.dwl_start);
nboth = length(q.both_start);
nstable = length(q.stable_start);

vartypes = cell(nf,1);
var_names = fq;
drow = nan(nf,1);
dcol = nan(nf,1);
for i = 1:nf
    eval(['thisvar = q.' fq{i} ';']);
    vartype = class(thisvar);
    vartypes(i) = {vartype};
    [drow(i), dcol(i)] = size(thisvar);
    if contains(string(fq(i)), '_') == 1
        var_names(i) = {strrep(string(fq(i)),'_',' ')};
    end
end

d2 = find(drow == ndepth & dcol == ntime);
dt = find(drow == 1 & dcol == ntime);
dd = find(drow == 1 & dcol == ndepth);
ds = find(drow == 1 & dcol == nstable);
dr = find(drow == 1 & dcol == nrl);
dw = find(drow == 1 & dcol == ndwl);
db = find(drow == 1 & dcol == nboth);
nd = length(d2) + length(dt) + length(dd) + length(ds) + length(dr) + length(dw) + length(db);
disp([' out of ' strnd(nf,0) ' total vars, ' strnd(nd,0) ' are accounted for']);


figure;
%%% 1-D time fields
counter = 1;
for i = 1:4:length(dt)
    clf;
    for j = 1:4
       if (i+j-1) <= length(dt)
           subplot(2,2,j); hold on;
           plot(t, q.(fq{dt(i+j-1)}),'.');
           title(var_names(dt(i+j-1)));
           datetick('x','mm/yyyy','keeplimits');
           grid on;
       end
   end
   export_fig([plotdir cruise '_time_' sprintf('%i',counter)],'-png','-r300','-nocrop');
   counter = counter + 1;
end

cbpath = '/Users/eliz/Documents/MATLAB/colortables/';
load([cbpath 'percent_11lev.rgb']);
percent_11lev = percent_11lev/256;

% depth field
clf;
plot((1:ndepth), depth, 'o');
title('depth');
grid on;
xlabel('number of depths');
export_fig([plotdir cruise '_depth'],'-png','-r300','-nocrop');

%%% 2-D fields
counter = 1;
for i = 1:4:length(d2)
   clf;
   for j = 1:4
       if (i+j-1) <= length(d2)
           subplot(2,2,j); hold on;
           pcolor(t, depth, q.(fq{d2(i+j-1)}));
           ylabel('depth');
           set(gca,'yticklabel',(0:50:350));
           ylim([0 350]);
           c = colorbar;
           datetick('x','mm/yyyy','keeplimits');
           if contains(string(var_names(d2(i+j-1))),'N2') == 1
               colormap(gca,'redblue');
               caxis([-.0002 .0002]);
           elseif strcmp(string(var_names(d2(i+j-1))),'SIG') == 1
               colormap(gca,percent_11lev);
               caxis([22 24]);
           elseif strcmp(string(var_names(d2(i+j-1))),'T') == 1
               colormap(gca,percent_11lev);
               caxis([22 30]);
           elseif strcmp(string(var_names(d2(i+j-1))),'S') == 1
               colormap(gca,percent_11lev);
               caxis([34.6 36]);
           elseif contains(string(var_names(d2(i+j-1))),'dz') == 1
               colormap(gca,'redblue');
               caxis([-0.25 0.25]);
           end
           axis ij;
           shading flat;
           title(var_names(d2(i+j-1)));
           xlabel('');
           grid on;
       end
   end
   export_fig([plotdir cruise '_depthtime_' sprintf('%i',counter)],'-png','-r300','-nocrop');
   counter = counter + 1;
end

%%% stable fields
counter = 1;
for i = 1:6:length(ds)
    clf;
    for j = 1:6
        if (i+j-1) <= length(ds)
           subplot(2,3,j); hold on;
           plot(1:nstable, q.(fq{ds(i+j-1)}),'o');
           title(var_names(ds(i+j-1)));
           xlabel('all stable layers');
           grid on;
        end
   end
   export_fig([plotdir cruise '_stable'],'-png','-r300','-nocrop');
   counter = counter + 1;
end

%%% rl fields
counter = 1;
for i = 1:8:length(dr)
    clf;
    for j = 1:7
       if (i+j-1) <= length(dr)
           subplot(2,4,j); hold on;
           plot(1:nrl, q.(fq{dr(i+j-1)}),'o');
           title(var_names(dr(i+j-1)));
           xlabel('rain layer #');
           grid on;
       end
   end
   export_fig([plotdir cruise '_rl'],'-png','-r300','-nocrop');
   counter = counter + 1;
end


%%% dwl fields
counter = 1;
for i = 1:8:length(dw)
    clf;
    for j = 1:7
       if (i+j-1) <= length(dw)
           subplot(2,4,j); hold on;
           plot(1:ndwl, q.(fq{dw(i+j-1)}),'o');
           title(var_names(dw(i+j-1)));
           xlabel('diurnal warm layer #');
           grid on;
       end
   end
   export_fig([plotdir cruise '_dwl'],'-png','-r300','-nocrop');
   counter = counter + 1;
end

%%% both fields
for i = 1:8:length(db)
    clf;
    for j = 1:7
       if (i+j-1) <= length(db)           
           subplot(2,4,j); hold on;
           plot(1:nboth, q.(fq{db(i+j-1)}),'o');
           title(var_names(db(i+j-1)));
           xlabel('both: rain warm combo layer #');
           grid on;
       end
   end
   export_fig([plotdir cruise '_both'],'-png','-r300','-nocrop');
   counter = counter + 1;
end
close all;