path(path,'/home/ruoying/Software/ecomviz/')
path(path,'/home/ruoying/ROMS/matlab/tools/')

run /home/ruoying/Software/opnml_matlab5/startup

load /home/ruoying/MERHAB/CTD/2003oc391.mat

t=cruise.temp;
s=cruise.sal;
z=cruise.z;
x=cruise.lon;
y=cruise.lat;
jd=cruise.time;

%== Only use Leg 1 in-situ data inx=83
inx=257;
t=t(:,1:inx); s=s(:,1:inx);
%for k=1:inx
% ii=find(~isnan(t(:,k))); tt=t(ii); tt=shapiro1(tt',2); t(ii,k)=tt'; 
% ii=find(~isnan(s(:,k))); ss=s(ii); ss=shapiro1(ss',2); s(ii,k)=ss';
%end

x=x(1:inx); y=y(1:inx);
jd=jd(1:inx,:); jd=julian(jd)-julian([2003 1 1 0 0 0]);

x=ones(185,1)*x;
y=ones(185,1)*y;
z=z*ones(1,inx);
jd=ones(185,1)*jd';

ii=find(~isnan(t) & ~isnan(s));

t=t(ii); s=s(ii); x=x(ii); y=y(ii); z=z(ii); jd=jd(ii);
x=-1*x;  [x,y]=ll2xy(y,x);

%== output .mir3d file
fm='%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %4i \n';
fid1=fopen('merhab.t.mir3d','w');
fid2=fopen('merhab.s.mir3d','w');

fprintf(fid1,'%s \n','Merhab Leg 1 temperature obs');
fprintf(fid1,'%s \n','2003');
fprintf(fid2,'%s \n','Merhab Leg 1 salinity obs');
fprintf(fid2,'%s \n','2003');

for k=1:length(ii)
 k
 fprintf(fid1,fm,jd(k),x(k),y(k),-1*z(k),t(k),1,1);
 fprintf(fid2,fm,jd(k),x(k),y(k),-1*z(k),s(k),1,1);
end
fclose(fid1)
fclose(fid2)

