%% read and save Jarkus


NCfile='http://dtvirt5.deltares.nl:8080/thredds/dodsC/opendap/rijkswaterstaat/jarkus/profiles/transect.nc';

bal = nc_varget(NCfile,'time_bathy');
daj=bal(:,1424)+datenum('1970-01-01');   % indices transects 1424:1434

load surveylines

for i=1:4
    if i==1
        yr='2007'
        d='d07';
        Jarkus(i,1).Mdate=(daj(end-3));
        Jarkus(i,1).date=datestr(daj(end-3));
    end
    if i==2
        yr='2008'
        d='d08';
        Jarkus(i,1).Mdate=(daj(end-2));
        Jarkus(i,1).date=datestr(daj(end-2));
    end
    if i==3
        yr='2009'
        d='d09';
        Jarkus(i,1).Mdate=(daj(end-1));
        Jarkus(i,1).date=datestr(daj(end-1));
    end
    if i==4
        yr='2010'
        d='d10';
        Jarkus(i,1).date=datestr(daj(end));
        Jarkus(i,1).Mdate=(daj(end));
    end
    
    disp('Loading Transects')
    eval([d '_450 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11450, ' yr ');'])
    eval([d '_469 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11469, ' yr ');'])
    eval([d '_488 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11488, ' yr ');'])
    eval([d '_535 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11535, ' yr ');'])
    eval([d '_510 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11510, ' yr ');'])
    eval([d '_560 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11560, ' yr ');'])
    eval([d '_586 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11586, ' yr ');'])
    eval([d '_611 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11611, ' yr ');'])
    eval([d '_636 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11636, ' yr ');'])
    eval([d '_662 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11662, ' yr ');'])
    eval([d '_687 = jarkus_readTransectDataNetcdf (NCfile,''Delfland'',11687, ' yr ');'])
    
    %%
    disp('Making Jarkus grid')
    eval([d '_450.xRD = ' d '_450.xRD(~isnan(' d '_450.zi)); ' d '_450.yRD =' d '_450.yRD(~isnan(' d '_450.zi)); ' d '_450.zi =' d '_450.zi(~isnan(' d '_450.zi));'])
    eval([d '_469.xRD =' d '_469.xRD(~isnan(' d '_469.zi)); ' d '_469.yRD =' d '_469.yRD(~isnan(' d '_469.zi)); ' d '_469.zi =' d '_469.zi(~isnan(' d '_469.zi));'])
    eval([d '_488.xRD =' d '_488.xRD(~isnan(' d '_488.zi)); ' d '_488.yRD =' d '_488.yRD(~isnan(' d '_488.zi)); ' d '_488.zi =' d '_488.zi(~isnan(' d '_488.zi));'])
    eval([d '_510.xRD =' d '_510.xRD(~isnan(' d '_510.zi)); ' d '_510.yRD =' d '_510.yRD(~isnan(' d '_510.zi)); ' d '_510.zi =' d '_510.zi(~isnan(' d '_510.zi));'])
    eval([d '_535.xRD =' d '_535.xRD(~isnan(' d '_535.zi)); ' d '_535.yRD =' d '_535.yRD(~isnan(' d '_535.zi)); ' d '_535.zi =' d '_535.zi(~isnan(' d '_535.zi));'])
    eval([d '_560.xRD =' d '_560.xRD(~isnan(' d '_560.zi)); ' d '_560.yRD =' d '_560.yRD(~isnan(' d '_560.zi)); ' d '_560.zi =' d '_560.zi(~isnan(' d '_560.zi));'])
    eval([d '_586.xRD =' d '_586.xRD(~isnan(' d '_586.zi)); ' d '_586.yRD =' d '_586.yRD(~isnan(' d '_586.zi)); ' d '_586.zi =' d '_586.zi(~isnan(' d '_586.zi));'])
    eval([d '_611.xRD =' d '_611.xRD(~isnan(' d '_611.zi)); ' d '_611.yRD =' d '_611.yRD(~isnan(' d '_611.zi)); ' d '_611.zi =' d '_611.zi(~isnan(' d '_611.zi));'])
    eval([d '_636.xRD =' d '_636.xRD(~isnan(' d '_636.zi)); ' d '_636.yRD =' d '_636.yRD(~isnan(' d '_636.zi)); ' d '_636.zi =' d '_636.zi(~isnan(' d '_636.zi));'])
    eval([d '_662.xRD =' d '_662.xRD(~isnan(' d '_662.zi)); ' d '_662.yRD =' d '_662.yRD(~isnan(' d '_662.zi)); ' d '_662.zi =' d '_662.zi(~isnan(' d '_662.zi));'])
    eval([d '_687.xRD =' d '_687.xRD(~isnan(' d '_687.zi)); ' d '_687.yRD =' d '_687.yRD(~isnan(' d '_687.zi)); ' d '_687.zi =' d '_687.zi(~isnan(' d '_687.zi));'])
    
    
    eval(['Jarkus(i,1).grids.Xi=[' d '_450.xRD(:) ; ' d '_469.xRD(:) ; ' d '_488.xRD(:) ; ' d '_510.xRD(:) ; ' d '_535.xRD(:) ; ' d '_560.xRD(:) ; ' d '_586.xRD(:) ; ' d '_611.xRD(:) ; ' d '_636.xRD(:) ; ' d '_662.xRD(:) ; ' d '_687.xRD(:) ];'])
    eval(['Jarkus(i,1).grids.Yi=[' d '_450.yRD(:) ; ' d '_469.yRD(:) ; ' d '_488.yRD(:) ; ' d '_510.yRD(:) ; ' d '_535.yRD(:) ; ' d '_560.yRD(:) ; ' d '_586.yRD(:) ; ' d '_611.yRD(:) ; ' d '_636.yRD(:) ; ' d '_662.yRD(:) ; ' d '_687.yRD(:) ];'])
    eval(['Jarkus(i,1).grids.Zi=[' d '_450.zi(:) ; ' d '_469.zi(:) ; ' d '_488.zi(:) ; ' d '_510.zi(:) ; ' d '_535.zi(:) ; ' d '_560.zi(:) ; ' d '_586.zi(:) ; ' d '_611.zi(:) ; ' d '_636.zi(:) ; ' d '_662.zi(:) ; ' d '_687.zi(:) ];'])
    
    
    Xi=Jarkus(i,1).grids.Xi(~isnan(Jarkus(i,1).grids.Zi));
    Yi=Jarkus(i,1).grids.Yi(~isnan(Jarkus(i,1).grids.Zi));
    Zi=Jarkus(i,1).grids.Zi(~isnan(Jarkus(i,1).grids.Zi));
    
    disp('Interpolating on TUD grid')
    for il=1:22
        il;
        Jarkus(i,1).transects.elevation(il,:)= griddata(Xi,Yi,Zi,Surveylines{il,1}.xi,Surveylines{il,1}.yi);
    end
end
%%
save('JarkusVB.mat','Jarkus')


%%
break
%% visualise profiles
load surveylines
%%
figure
hold on
for il=1:22
    plot(Surveylines{il,1}.xi,Surveylines{il,1}.yi)
    plot(Surveylines{il,1}.xi(61),Surveylines{il,1}.yi(61),'ok')
    
end
%plot(d10.xRD(483:end),d10.yRD(483:end),'m')  %423 equals landward end of TUD profiles
plot(d07_450.xRD(:),d07_450.yRD(:),'m');
plot(d07_469.xRD(:),d07_469.yRD(:),'m')
plot(d07_488.xRD(:),d07_488.yRD(:),'m')
plot(d07_510.xRD(:),d07_510.yRD(:),'m')
plot(d07_535.xRD(:),d07_535.yRD(:),'m')
plot(d07_560.xRD(:),d07_560.yRD(:),'k')
plot(d07_586.xRD(:),d07_586.yRD(:),'m')
plot(d07_611.xRD(:),d07_611.yRD(:),'m')
plot(d07_636.xRD(:),d07_636.yRD(:),'m')
plot(d07_662.xRD(:),d07_662.yRD(:),'m')
plot(d07_687.xRD(:),d07_687.yRD(:),'m')

axis equal
grid on
