%potc_grid_hawaii.m
% routine to read in nominally calibrated of mmp gridded files,
%   calculate potential conductivity
%     and bin average potc on a specified theta grid
%  developed to quantify conductivity cell drift

thetagrid=[2.65:.1:5.65];
theta_centers=[2.7:.1:5.6];
n=[0:62];

for ii=1:63
i=ii-1;
infile=['/j2/data/mvp/hawaii_test/grdfmt/grd',sprintf('%4.4i',(i)),'.mat']
if exist(infile,'file')==2;
eval(['load ',infile]);
potc=sw_cndr(s_ave',thetave',0)*sw_c3515;
for j=1:30
k=find(thetave>=thetagrid(j) & thetave<=thetagrid(j+1));
  if(length(k)>2)
  coefs=polyfit(thetave(k),potc(k)',1);
  potc_grid(ii,j)=polyval(coefs,theta_centers(j));
 else
  potc_grid(ii,j)=nan;
 end
end

else
potc_grid(ii,1:30)=nan;
end

end

save potc_grid_hawaii n thetagrid theta_centers potc_grid


