%parseSciData_out.m

clear
load CPF29MAR16.mat;
prof=10;

%yanwu parameters
dep_deep_upwelling = 30;
dep_middeep_upwelling = 20;
dep_midshallow_upwelling = 10;
dep_shallow_upwelling = 5;
flag_temp_deep = 0;
flag_temp_middeep = 0;
flag_temp_midshallow=0;
flag_temp_shallow=0;


j=0;
outFID = fopen('SciData.txt', 'w');
for i=1:size(P,2)
    if( ~isnan(P(prof,i))&& P(prof,i)<41)
      %fprintf(outFID,'cp[%d] = new double[]{%f,%f,%f};\n',j,P(prof,i),T(prof,i),S(prof,i));
      fprintf(outFID,'%f,%f,%f\n',P(prof,i),T(prof,i),S(prof,i));
      j=j+1;
    
      if( P(prof,i)<dep_deep_upwelling && flag_temp_deep==0)
          temp(1)=T(prof,i);
          flag_temp_deep=1;
      end
      if( P(prof,i)<dep_middeep_upwelling && flag_temp_middeep==0)
          temp(2)=T(prof,i);
          flag_temp_middeep=1;
      end
      if( P(prof,i)<dep_midshallow_upwelling && flag_temp_midshallow==0)
          temp(3)=T(prof,i);
          flag_temp_midshallow=1;
      end
      if( P(prof,i)<dep_shallow_upwelling && flag_temp_shallow==0)
          temp(4)=T(prof,i);
          flag_temp_shallow=1;
      end
      
    end 
end

fclose(outFID);

mean_t = mean(temp);

tempDiff = 0.0; 
for (v=1:1:4) 

    if ((temp(v) - mean_t) >= 0.0) 
        tempDiff = tempDiff + (temp(v) - mean_t); 
    else 
        tempDiff = tempDiff - (temp(v) - mean_t); 
    end
        
end

tempDiff = tempDiff / 4.0; 