% routine to generate a "feature front" from an

% existing 3-d CTD field by inserting additional copied of CTDs

%

% displays a horizontal distribution and prompts the

% user to pick, view, and add CTD structure with the cursor





% display existing field with "hsec"

%hsec



% load colorcodes

load e:\atsee\ccodemat;



% view a series of ctds to view structure

st=0;

ii=0;

id=[];

disp('Zoom in by clicking two corners');

zoomer;



aa=axis;

while st==0;

    disp('click a location (western edge when done)');

    figure(1);

    [x,y]=ginput(1);

    id=find(abs(d(:,9)-y)<.01&abs(d(:,10)-x)<.015);

    if x<aa(1)+.02;

      st=1;

      id=[999];

    end;

    if ~isempty(id)&st~=1;

      plot(x,y,'.','color','y','markersize',50);

      text(d(id(1),10),d(id(1),9),num2str(d(id(1),11)),...

       'horizontalalignment','center','verticalalignment','middle');

      ii=ii+1;

      figure(3);

      plot(d(id,7),-1*d(id,3),'color',ccode(rem(ii,6),:));

      text(d(id(1),7),-1*d(id(ii*3),3),num2str(d(id(1),11)),...

         'color',ccode(rem(ii,6),:));

      set(gcf,'position',[475 50 400 420]);

      pause(1);

      hold on;

    end;   

end;



% choose a representative profile and insert XX times

xx=input('How many ctds do you want to add on each side of front?');

sta=input('What station represents stratified?');

ids=find(d(:,11)==sta);



% make sure this station is unique

idsd=find(diff(d(ids,3))<0);
if ~isempty(idsd);
  disp(['Multiple cases of Station ',num2str(sta)]);
  d(ids(idsd(1)),4)

  d(ids(idsd+1),4)
  wyd=input('Which yearday?');
  idsn=find(floor(d(ids,4))==wyd);
  idss=ids(idsn);
end;


disp(['Choose ',num2str(xx),' locations for stratified profiles']);

figure(1);

% get xx stations using the "makgrd.m" routine

makgrd;

% convert these to x&y from Boston with "ll2xy.m" setup to

% read the formgrid file and output an "x" and "y"

ll2xy;

load formgrid.out;

fg=formgrid;

xs=fg(:,7);ys=fg(:,6);

plot(xs,ys,'.','color','c','markersize',30);

for i=1:xx;

  % add to matrix "d"

  % note: sometimes these "data" files have 12 columns

  dnew=[x(i)*ones(length(idss),1) y(i)*ones(length(idss),1) ...

        d(idss,3:8) ys(i)*ones(length(idss),1) ...

        xs(i)*ones(length(idss),1) 1000*i+d(idss,11) ];

  d=[d; dnew];

end;



sta=input('What station represents mixed?');

ids=find(d(:,11)==sta);



% make sure this station is unique

idsd=find(diff(d(ids,3))<0);
if ~isempty(idsd);
  disp(['Multiple cases of Station ',num2str(sta)]);
  d(ids(idsd(1)),4)

  d(ids(idsd+1),4)
  wyd=input('Which yearday?');
  idsn=find(floor(d(ids,4))==wyd);
  ids=ids(idsn);
end;


disp(['Choose ',num2str(xx),' locations for mixed profiles']);

makgrd;

ll2xy;

load formgrid.out;

fg=formgrid;

xs=fg(:,7);ys=fg(:,6);

plot(xs,ys,'.','color','m','markersize',30);

for i=1:xx;

  % add to matrix "d"

  dnew=[x(i)*ones(length(ids),1) y(i)*ones(length(ids),1) ...

        d(ids,3:8) xs(i)*ones(length(ids),1) ...

        ys(i)*ones(length(ids),1) 1000*i+d(ids,11)];

  d=[d; dnew];

end;



newf=input('New filename (w/out the .data ext)?','s');

eval(['save f:\ctd\data\',newf,'.data d -ascii']);