% Calculates area inundated at baseflow for distributed flood waves

clear

km_max = 27;

% Load polygons
km_polys = csvread(['/Users/Colin/Documents/MATLAB/DFlow/'...
    'influences_chap2/data/abq10a12_1000m_polygon_pts.csv']);

% root directory for distributed models
root_dir = ['/Users/Colin/Documents/MATLAB/DFlow/abq_models/'...
    'abq2010add2012bath/'];

% 30 cms folder
loc_30cms = strcat(root_dir,'run3051_30cmsdist/');
% 150 cms folder
loc_150cms = strcat(root_dir,'run3052_150cmsdist/');

%% 30 cms baseflow area calculations
f30cms_out = partmap2variable(32,loc_30cms,...
    'abq10a12bf30cmsdistrst01_',...
    '_20160104_090000_map.nc');

% Determine which kilometer each element is in
for i = 1:km_max
    i_poly = km_polys(km_polys(:,3)==i,1:2);
    yn_poly = inpolygon(f30cms_out(:,3),f30cms_out(:,4),...
        i_poly(:,1),i_poly(:,2));
    f30cms_out(yn_poly==1,11) = i;
end

f30cms_areas = zeros(km_max,3);
for i = 1:km_max
    f30cms_areas(i,1) = sum(f30cms_out(f30cms_out(:,11)==i,6));
    f30cms_areas(i,2) = sum(f30cms_out(f30cms_out(:,11)==i&...
        f30cms_out(:,9)>0,6));
    f30cms_areas(i,3) = f30cms_areas(i,2) / f30cms_areas(i,1);
end

%% 150 cms baseflow area calculations
f150cms_out = partmap2variable(32,loc_150cms,...
    'abq10a12bf150cmsdistrst01_',...
    '_20160102_230000_map.nc');

for i = 1:km_max
    i_poly = km_polys(km_polys(:,3)==i,1:2);
    yn_poly = inpolygon(f150cms_out(:,3),f150cms_out(:,4),...
        i_poly(:,1),i_poly(:,2));
    f150cms_out(yn_poly==1,11) = i;
end

f150cms_areas = zeros(km_max,3);
for i = 1:km_max
    f150cms_areas(i,1) = sum(f150cms_out(f150cms_out(:,11)==i,6));
    f150cms_areas(i,2) = sum(f150cms_out(f150cms_out(:,11)==i&...
        f150cms_out(:,9)>0,6));
    f150cms_areas(i,3) = f150cms_areas(i,2) / f150cms_areas(i,1);
end