close all
clear all

name = 'DelMar_hamilton_v2';

ESP_wetweight = [-220  760 1550];  %Newtons
pigmass = [200 100 20];  %kg
net = (pigmass*9.81+ESP_wetweight)/4.45
config = 1;

%Current Profile Estimates from ADCP data
%profile_z = [0 -5 -10 -20 -40 -60 -80 -102];
%mean_profile = [17 17 17 15.5 12.5 10 9 9];   % Mean Profile
%max_obs_profile = [92 92 92 75 65 65 65 65];  % Max Observed Profile


  LAYOUT.title = name;

  
  ENV.depth = 100;
  ENV.tf = 0; 


  LAYOUT.stretch_hose_length = 10.0;
  
 LAYOUT.pigmass = pigmass(config);
 LAYOUT.ESP_wetweight = ESP_wetweight(config);

  currentscale = [2:.5:4.5];
  hose = [1 2 3]; %1 = Soft  2 = Nominal  3 = Stiff:q!
  
figure('Position',[1000 246 1028 1252],'PaperType','usletter','PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[0 0 8.5 11]);

  for i = 1:length(currentscale)

    for j = 1:length(hose)  

      ENV.currentscale = currentscale(i);
      LAYOUT.hose = hose(j);
      ENV.xwind = 5*ENV.currentscale;

      filenm = make_filename(ENV,LAYOUT,name);
      
      [segment n_length n_nodes nodes] = read_bom(filenm);

      
      
      StretchHose_Nodes = nodes(2)+1:nodes(1);  %196:225;
      ESP_Nodes = nodes(3)+1:nodes(2);  %192:195;
      PigWeight_Nodes =  nodes(7)+1:nodes(6);     %109:111;
      

      load([filenm '.mat']);
      ESP_x(i,j) = x(ESP_Nodes(end-1));
      ESP_z(i,j) = z(ESP_Nodes(end-1));
      ESP_waterdepth(i,j) = depth;

      BuoyDraft(i,j) = depth-z(end);
      HoseTension(i,j) = mean(T(StretchHose_Nodes))
      HoseLength(i,j) = ((x(StretchHose_Nodes(1))-x(StretchHose_Nodes(end))).^2+(z(StretchHose_Nodes(1))-z(StretchHose_Nodes(end))).^2).^.5;
      HoseStrain(i,j) = (HoseLength(i,j)-LAYOUT.stretch_hose_length)/LAYOUT.stretch_hose_length;
      TensionAboveESP(i,j) = T(ESP_Nodes(end));
      TensionBelowESP(i,j) = T(ESP_Nodes(1));
      TensionAbovePig(i,j) = T(PigWeight_Nodes(end));
      TensionBelowPig(i,j) = T(PigWeight_Nodes(1));
      TensionBelowBuoy(i,j) = T(end);

      subplot(3,3,i)
      plot(x,z);  %ESP at node 195, hard coded but should be read from .bom file.
      hold all;
     end
  lh = legend('Soft','Nominal','Stiff','Location','SouthEast');
  set(lh,'FontSize',8);
  
  for j = 1:length(hose) 
    plot(ESP_x(i,j),ESP_z(i,j),'k.');
    hold all
  end
  xlim([30 85]);
  xlabel('m');
  ylabel('m');
  title(['Current Profile = ' num2str(currentscale(i)) ' times mean profile'],'FontSize',8);
  end

  subplot(3,3,[7 8 9])
  plot(currentscale,ESP_waterdepth-ESP_z);
  xlabel('Current Scale (multiples of mean current profile)');
  ylabel('ESP Depth (m)')
  title([num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight']);

 
  print('-dpdf',['StaticInstrumentDepths_pm' num2str(pigmass(config)*2.2,3) '_espww' num2str(ESP_wetweight(config)/4.45,3) '-hl'   num2str(LAYOUT.stretch_hose_length) '.pdf']);
  print('-dpng',['StaticInstrumentDepths_pm' num2str(pigmass(config)*2.2,3) '_espww' num2str(ESP_wetweight(config)/4.45,3) '-hl'   num2str(LAYOUT.stretch_hose_length) '.png']);
  
  
figure('Position',[1000 246 1028 1252],'PaperType','usletter','PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[0 0 8.5 11]);
  for i = 1:3
    subplot(3,1,i)
    plot(currentscale,TensionBelowBuoy(:,i), ...
       currentscale,HoseTension(:,i), ...
       currentscale,TensionAboveESP(:,i), ...
       currentscale,TensionBelowESP(:,i), ...
       currentscale,TensionAbovePig(:,i), ...
       currentscale,TensionBelowPig(:,i));    
   legend('Tension Below Buoy',...
          'Mean Hose Tension',...
          'Tension Above ESP',...
          'Tension Below ESP',...
          'Tension Above Pig',...
          'Tension Below Pig');
      
    xlabel('Current Scale (multiples of mean current profile)');
    ylabel('Tension (lbs)');
    switch(i)
        case 1
         title(['Static Tension Results for Soft Hose  ' num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight']);
        case 2
         title(['Static Tension Results for Nominal Hose  ' num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight']);
        case 3
         title(['Static Tension Results for Stiff Hose  ' num2str(pigmass(config)*2.2,3) ' lb Pig wet weight,  ' num2str(ESP_wetweight(config)/4.45,3) ' lb ESP wet weight']);
    end
    
                   
  end
  

  print('-dpdf',['StaticTensions_pm' num2str(pigmass(config)*2.2,3) '_espww' num2str(ESP_wetweight(config)/4.45,3) '-hl'   num2str(LAYOUT.stretch_hose_length) '.pdf']);
  print('-dpng',['StaticTensions_pm' num2str(pigmass(config)*2.2,3) '_espww' num2str(ESP_wetweight(config)/4.45,3) '-hl'   num2str(LAYOUT.stretch_hose_length) '.png']);
 





