%Set length and set up initial float locations
water_depth = 1750;


x = (scope-1)*water_depth/2; %calculate 1/2 catenary length
lcb_length = cat_bot+x; %lower cable length is caternary height above bottom + 1/2 catenary length


fc_buoyancy = 600-72;  %entry for total cable float buoyancy in lbs
fc_specific_buoyancy = 24;  %lbs/meter

fc_length = fc_buoyancy/fc_specific_buoyancy;  %meters

fc_density = 700;  %kg/m^3 (approximate for syntactic foam)
fc_buoyancy = (fc_buoyancy/2.2)*9.8;  %conversion from lbs to Newtons


uc_length = water_depth*scope-lcb_length-fc_length; %upper cable length is total cable - lower cable and floated length

sw_density = 1025;

%compute diameter and mass per meter.
fc_diameter = sqrt((4*fc_buoyancy)/(pi*fc_length*9.8*(1025-fc_density)))  %meters
fc_mass = fc_density*pi*fc_diameter*fc_diameter/4  %kg per meter
%neutral_mass = sw_density*pi*fc_diameter*fc_diameter/4  %kg per meter

%fc_length = fc_length + neutral_length;  %Add neutral length, keeping total buoyancy and diam the same.

last_anchor_node = 21;

fprintf('Last Anchor Node = %d\n',last_anchor_node)

 %Create .spec file
 fid = fopen('Lower_Cable.spec','w');
 fprintf(fid,'#define WATER_DEPTH %6.3f \n',water_depth);
 
 fprintf(fid,'#define AMP %6.3f \n',wave_amp);
 fprintf(fid,'#define PER %6.3f \n',wave_per);
 fprintf(fid,'#define WIND %6.3f \n',wind_spd);
 
 fprintf(fid,'#define CURR_0 %6.3f \n',curr_profile(1));
 fprintf(fid,'#define CURR_50 %6.3f \n',curr_profile(2));
 fprintf(fid,'#define CURR_100 %6.3f \n',curr_profile(3));
 fprintf(fid,'#define CURR_150 %6.3f \n',curr_profile(4));
 fprintf(fid,'#define CURR_200 %6.3f \n',curr_profile(5));
 fprintf(fid,'#define CURR_500 %6.3f \n',curr_profile(6));
 fprintf(fid,'#define CURR_1000 %6.3f \n',curr_profile(7));
 fprintf(fid,'#define CURR_1500 %6.3f \n',curr_profile(8));
 fprintf(fid,'#define CURR_1750 %6.3f \n',curr_profile(9));
 
 fprintf(fid,'#define SNUB_LENGTH %6.3f \n',snub_length);
 
 fprintf(fid,'#define LCB_LENGTH %8.2f \n',lcb_length);
 fprintf(fid,'#define LCB_NODE_SPEC (%d, 0.05) (%d, .90) (%d, 0.05) \n',round(4*0.05*lcb_length/node_sp),round(0.90*lcb_length/node_sp),round(4*0.05*lcb_length/node_sp));

 first_FC_node = last_anchor_node + round(4*0.05*lcb_length/node_sp) + round(0.90*lcb_length/node_sp) + round(4*0.05*lcb_length/node_sp) + 1;
 
 fprintf('First Floated Cable Node = %d\n',first_FC_node)
 
 fprintf(fid,'#define FC_LENGTH %8.2f \n',fc_length);
 fprintf(fid,'#define FC_NODE_SPEC (%d, 1.0)\n',4*round(fc_length/node_sp));
 fprintf(fid,'#define FC_MASS %8.2f \n',fc_mass);
 fprintf(fid,'#define FC_DIAM %6.3f \n',fc_diameter);
 
 
 last_FC_node = first_FC_node +  4*round(fc_length/node_sp) - 1;
 fprintf('Last Floated Cable Node = %d\n',last_FC_node)
 
  
  first_UC_node = last_FC_node + 1;
  fprintf('First Upper Cable Node = %d\n',first_UC_node)  
 
 fprintf(fid,'#define UC_LENGTH %8.2f \n',uc_length);
 fprintf(fid,'#define UC_NODE_SPEC (%d, 0.05) (%d, .9) (%d, .05) \n',4*round(0.05*uc_length/node_sp),round(0.9*uc_length/node_sp),4*round(0.05*uc_length/node_sp));
 
 first_SNUB_node = first_UC_node + 4*round(0.05*uc_length/node_sp) + round(0.9*uc_length/node_sp) + 4*round(0.05*uc_length/node_sp)+ 11; %first snubber node is upper cable nodes + 11 for bending strain relief and termination
 fprintf('First Snubber Node = %d\n',first_SNUB_node)
 
 n_snub_nodes = 4*round(snub_length/node_sp);
 fprintf(fid,'#define SNUB_NODE_SPEC (%d, 1.0) \n',n_snub_nodes);

 
 fclose(fid);
 clear fid
 
nodes_of_interest = [1:last_anchor_node+10 first_FC_node-10:first_UC_node+150 first_UC_node+1000 first_UC_node+2000 first_UC_node+3000 first_SNUB_node-20:first_SNUB_node+n_snub_nodes];
%nodes_of_interest = The first 31 nodes above the anchor, the floated cable nodes including an extra 10 on the bottom side and extra 150 on the upper side, three node in the middle of the upper cable and all of the termination and snubber nodes  
cmd = ['save ',results_filenm,'-SPEC.mat last_anchor_node first_FC_node last_FC_node first_UC_node first_SNUB_node nodes_of_interest'];
eval(cmd);   
