volurl = 'duros_active_profile_volume.nc';

ids = ncread(volurl, 'id');

volumeC = ncread(volurl, 'volumeC');
volmean = nanmean(volumeC,2);

volmin = nanmin(volumeC,2) - volmean;
volmax = nanmax(volumeC,2) - volmean;
volrange = nanmax(volumeC,2) - nanmin(volumeC,2);

volabsmax = max(abs([volmin volmax]), [], 2);

tr = jarkus_transects('output', {'id', 'cross_shore', 'lat', 'lon'});
idx = ismember(tr.id, ids);
tr.id = tr.id(idx);
tr.lat = tr.lat(idx,:);
tr.lon = tr.lon(idx,:);

ampl = min(abs(tr.cross_shore([1 end])));
cridx = abs(tr.cross_shore) < ampl;
tr.cross_shore = tr.cross_shore(cridx);
tr.lat = tr.lat(:,cridx);
tr.lon = tr.lon(:,cridx);

scalefactor = max([-volmin(:); volmax(:)]) / ampl;
for i = 1:size(tr.lat,1)
    latmin(i) = interp1(tr.cross_shore, tr.lat(i,:)', volmin(i)/scalefactor);
    lonmin(i) = interp1(tr.cross_shore, tr.lon(i,:)', volmin(i)/scalefactor);
    latmax(i) = interp1(tr.cross_shore, tr.lat(i,:)', volmax(i)/scalefactor);
    lonmax(i) = interp1(tr.cross_shore, tr.lon(i,:)', volmax(i)/scalefactor);
    
    lathalfrange(i) = interp1(tr.cross_shore, tr.lat(i,:)', volrange(i)/2/scalefactor);
    lonhalfrange(i) = interp1(tr.cross_shore, tr.lon(i,:)', volrange(i)/2/scalefactor);
    latabsmax(i) = interp1(tr.cross_shore, tr.lat(i,:)', volabsmax(i)/scalefactor);
    lonabsmax(i) = interp1(tr.cross_shore, tr.lon(i,:)', volabsmax(i)/scalefactor);
end
idx0 = tr.cross_shore == 0;
lat0 = tr.lat(:,idx0);
lon0 = tr.lon(:,idx0);

KMLline([latmin;latmax], [lonmin;lonmax],...
    'fileName', 'ranges.kml',...
    'lineWidth', 5)
KMLscatter(lat0, lon0, zeros(size(lon0)),...
    'scalenormalState', .5,...
    'CBcLim', [-1 1],...
    'fileName', 'origins.kml',...
    'colorbar', false)
KMLscatter(lathalfrange', lonhalfrange', ones(size(lonhalfrange'))/2,...
    'scalenormalState', .5,...
    'CBcLim', [-1 1],...
    'fileName', 'halfrange.kml',...
    'colorbar', false)
KMLscatter(latabsmax, lonabsmax, ones(size(latmax)),...
    'scalenormalState', .5,...
    'CBcLim', [-1 1],...
    'fileName', 'absmax.kml',...
    'colorbar', false)
 
KMLmerge_files(...
    'fileName', 'volranges.kml',...
    'sourceFiles', {'ranges.kml', 'origins.kml', 'halfrange.kml', 'absmax.kml'});