clear all
dxmin =10;
dxmax=50;
hmin=.1
profname='profile.txt';

xz=load(profname)
xdata=xz(:,1);
zdata=xz(:,2);

xgrid(1)=xdata(1);
zgrid(1)=zdata(1);
h0=-zdata(1);
xgrid(2)=xgrid(1)+dxmax;
i=1
while xgrid(i)<xdata(end)
    i=i+1;
    h=max(-zgrid(i-1),hmin);
    dx=dxmax*sqrt(h/h0)
    dx=max(dx, dxmin);
    xgrid(i)=xgrid(i-1)+dx;
    zgrid(i)=interp1(xdata,zdata,xgrid(i));
   
end
    
% xgrid=[xdata(1):dx:xdata(end)];
% zgrid=interp1(xdata,zdata,xgrid);

fi=fopen('griddata.txt','wt');
fprintf(fi,'nx      = %3i \n',length(xgrid)-1);
fprintf(fi,'ny      = %3i \n',0);
fprintf(fi,'dx      = %6.1f \n',dx);
fprintf(fi,'dy      = %6.1f \n',dx);
fprintf(fi,'xori    = %10.2f \n',xdata(1));
fprintf(fi,'yori    = %10.2f \n',0);
fprintf(fi,'alfa    = %10.2f \n',0);
fprintf(fi,'depfile = profile.dep \n')
fprintf(fi,'posdwn  = -1 \n')
fclose(fi);

fi=fopen('profile.dep','wt');
for i=1:1
    fprintf(fi,'%7.3f ',zgrid);
    fprintf(fi,'\n');
end
fclose(fi);
fi=fopen('x.dep','wt');
for i=1:1
    fprintf(fi,'%7.3f ',xgrid);
    fprintf(fi,'\n');
end
fclose(fi);
veg=zeros(size(xgrid));
veg(zgrid>-1)=1;
fi=fopen('veg.dep','wt');
for i=1:1
    fprintf(fi,'%7.3f ',veg');
    fprintf(fi,'\n');
end
fclose(fi);
p1=ones(size(xgrid))*.9;
fi=fopen('gdist1.inp','wt');
for i=1:3
    fprintf(fi,'%7.3f ',p1');
    fprintf(fi,'\n');
end
fclose(fi);
p2=zeros(size(xgrid))+.1;
fi=fopen('gdist2.inp','wt');
for i=1:3
    fprintf(fi,'%7.3f ',p2');
    fprintf(fi,'\n');
end
fclose(fi);
figure(1);
plot(xdata,zdata,'+',xgrid,zgrid,'.')
