 clc;clear all;


addpath(genpath('/data/brian/Tools'))
%typhoon
hisnc = '/data/hipub/temp/COAWST_WRF4.0.3/Projects/f3owf_test/20220519.typ/ocean_his_coupled.nc'
%winter
hisnc = '/data/hipub/temp/COAWST_WRF4.0.3/Projects/f3owf_test/20220517.winter/ocean_his_coupled.nc'
%summer
hisnc = '/data/hipub/temp/COAWST_WRF4.0.3/Projects/f3owf_test/20220507.summer/ocean_his_coupled.nc';

hisnc = '/data2/brian/ocean_his_coupled.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin2/backfill_domain2/winter_ini/merged_coawst_domain2_winter05_bdy.nc';
varargin = '/data/brian/grid_rom/rom_20m/domin2/backfill_domain2/typhoon_ini/merged_coawst_domain2_typhoon_bdy.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin2/backfill_domain2/summer_ini/merged_coawst_domain2_backfill_bdy2.nc'


varargin2 = '/data/brian/grid_rom/rom_20m/domin3/hycom_30day/merged_coawst_bdy.nc';
varargin = '/data/brian/grid_rom/rom_20m/domin3/typhoon_ini/merged_coawst_domain3_typhoon_bdy.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin3/summer_ini/merged_coawst_domain3_bdy.nc'

varargin = '/data/brian/grid_rom/rom_20m/domin1/model_ini/merged_coawst_summer_domain1_bdy.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin1/model_ini/merged_coawst_winter_domain1_bdy.nc'

varargin = '/data/brian/grid_rom/rom_20m/domin7/summer_ini/merged_summer_domain7_bdy.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin7/winter_ini/merged_coawst_domain3_winter_20m_bdy.nc'

varargin = '/data/brian/grid_rom/rom_20m/domin6/model_ini/merged_coawst_summer_domain6_bdy.nc'
varargin = '/data/brian/grid_rom/rom_20m/domin6/model_ini/merged_coawst_winter_domain6_bdy.nc'

modelgrid = '/data/brian/grid_rom/rom_20m/domin2/backfill_domain2/backfill_grid_h/roms_domain2_backill_h_20m_grid.nc'
modelgrid = '/data/brian/grid_rom/rom_20m/domin6/roms_domain6_20m_grid.nc'
modelgrid = '/data/brian/grid_rom/rom_20m/domin7/roms_domain7_20m_grid.nc'
modelgrid = '/data/brian/grid_rom/rom_20m/domin3/roms_domain3_20m_grid.nc'
modelgrid = '/data/brian/grid_rom/rom_20m/domin1/roms_domain1_20m_grid.nc'

wdr = '/data/brian/grid_rom/rom_20m/domin2/backfill_domain2/winter_ini';


eval(['cd ',wdr])

tic

varargin = '/data/brian/grid_rom/new_ini/merged_coawst_domain7_bdy.nc'
modelgrid = '/data/brian/grid_rom/new_ini/roms_domain7_20m_grid.nc'
hisnc = '/data2/brian/ocean_his_coupled.nc'


numX=ncread(hisnc,'lon_rho');
numY=ncread(hisnc,'lat_rho');
his_lon=ncread(hisnc,'lon_rho',[1 1],[length(numX)-1 1]);
his_lat=ncread(hisnc,'lat_rho',[1 1],[1 length(numY)-1]);
hisu_lon=ncread(hisnc,'lon_u',[1 1],[length(numX)-1 1]);
hisu_lat=ncread(hisnc,'lat_u',[1 1],[1 length(numY)-1]);
hisv_lon=ncread(hisnc,'lon_v',[1 1],[length(numX)-1 1]);
hisv_lat=ncread(hisnc,'lat_v',[1 1],[1 length(numY)-1]);
%lim of 
lon = ncread(modelgrid,'lon_rho');
lat = ncread(modelgrid,'lat_rho');
ulon = lon(1:length(lon(:,1))-1,:);ulat=lat(1:length(lon(:,1))-1,:);
vlon = lon(:,1:length(lat(1,:))-1);vlat=lat(:,1:length(lat(1,:))-1);

xl=min(min(lon));xr=max(max(lon));
yb=min(min(lat));yt=max(max(lat));

xg=his_lon;
yg=his_lat;
xg1=hisv_lon;
yg1=hisv_lat;
xg2=hisu_lon;
yg2=hisu_lat;
[ii] = find(xg>=xl & xg<=xr);
[jj] = find(yg>=yb & yg<=yt);

[iiv] = find(xg1>=xl & xg1<=xr);
[jjv] = find(yg1>=yb & yg1<=yt);

[iiu] = find(xg2>=xl & xg2<=xr);
[jju] = find(yg2>=yb & yg2<=yt);

%%%%%%%%%%%%%%%%%%%
temp_his = ncread(hisnc,'temp');
salt_his = ncread(hisnc,'salt');
u_his = ncread(hisnc,'u');
v_his = ncread(hisnc,'v');
ubar_his = ncread(hisnc,'ubar');
vbar_his = ncread(hisnc,'vbar');
z_his = ncread(hisnc,'zeta');
%oceant = ncread(hisnc,'ocean_time');

%otime = ncread(varargin2,'zeta_time');

t_s = ncread(varargin2,'temp_south');
t_e = ncread(varargin2,'temp_east');
t_n = ncread(varargin2,'temp_north');
t_w = ncread(varargin2,'temp_west');

s_s = ncread(varargin2,'salt_south');
s_e = ncread(varargin2,'salt_east');
s_n = ncread(varargin2,'salt_north');
s_w = ncread(varargin2,'salt_west');

u_s = ncread(varargin2,'u_south');
u_e = ncread(varargin2,'u_east');
u_n = ncread(varargin2,'u_north');
u_w = ncread(varargin2,'u_west');

ub_s = ncread(varargin2,'ubar_south');
ub_e = ncread(varargin2,'ubar_east');
ub_n = ncread(varargin2,'ubar_north');
ub_w = ncread(varargin2,'ubar_west');

vb_s = ncread(varargin2,'vbar_south');
vb_e = ncread(varargin2,'vbar_east');
vb_n = ncread(varargin2,'vbar_north');
vb_w = ncread(varargin2,'vbar_west');

z_s = ncread(varargin2,'zeta_south');
z_e = ncread(varargin2,'zeta_east');
z_n = ncread(varargin2,'zeta_north');
z_w = ncread(varargin2,'zeta_west');

v_s = ncread(varargin2,'v_south');
v_e = ncread(varargin2,'v_east');
v_n = ncread(varargin2,'v_north');
v_w = ncread(varargin2,'v_west');

zt = ncread(varargin2,'zeta_time');
v2t = ncread(varargin2,'v2d_time');
st = ncread(varargin2,'salt_time');
v3t = ncread(varargin2,'v3d_time');
tt = ncread(varargin2,'temp_time');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[x_lat x_z] = meshgrid(lat(1,:),linspace(1,15,15));
[y_lon y_z] = meshgrid(lon(:,1),linspace(1,15,15));


[ux_lat ux_z] = meshgrid(ulat(1,:),linspace(1,15,15));
[uy_lon uy_z] = meshgrid(ulon(:,1),linspace(1,15,15));

[vx_lat vx_z] = meshgrid(vlat(1,:),linspace(1,15,15));
[vy_lon vy_z] = meshgrid(vlon(:,1),linspace(1,15,15));

%time = [13 17 21 25 29 33 37 41 45 49 53 57 1 5 9]
%time = linspace(5,57,53)
time = linspace(5,80,76)
time = linspace(5,121,117)
for i = (1:53)
%for i = (1:117)
%for i = (1:117)
    t_e(:,:,i) = griddata(his_lat,linspace(1,15,15),squeeze(temp_his(1:799,ii(1),:,time(i)))',x_lat,x_z)';
    t_w(:,:,i) = griddata(his_lat,linspace(1,15,15),squeeze(temp_his(1:799,ii(length(ii)),:,time(i)))',x_lat,x_z)';7
    t_s(:,:,i) = griddata(his_lon,linspace(1,15,15),squeeze(temp_his(jj(1),1:799,:,time(i)))',y_lon,y_z)';
    t_n(:,:,i) = griddata(his_lon,linspace(1,15,15),squeeze(temp_his(jj(length(jj)),1:799,:,time(i)))',y_lon,y_z)';
    
    s_e(:,:,i) = griddata(his_lat,linspace(1,15,15),squeeze(salt_his(1:799,ii(1),:,time(i)))',x_lat,x_z)';
    s_w(:,:,i) = griddata(his_lat,linspace(1,15,15),squeeze(salt_his(1:799,ii(length(ii)),:,time(i)))',x_lat,x_z)';
    s_s(:,:,i) = griddata(his_lon,linspace(1,15,15),squeeze(salt_his(jj(1),1:799,:,time(i)))',y_lon,y_z)';
    s_n(:,:,i) = griddata(his_lon,linspace(1,15,15),squeeze(salt_his(jj(length(jj)),1:799,:,time(i)))',y_lon,y_z)';
    
    
    u_e(:,:,i) = griddata(hisu_lat,linspace(1,15,15),squeeze(u_his(1:799,iiu(1),:,time(i)))',ux_lat,ux_z)'*0.5;
    u_w(:,:,i) = griddata(hisu_lat,linspace(1,15,15),squeeze(u_his(1:799,iiu(length(iiu)),:,time(i)))',ux_lat,ux_z)'*0.5;
    u_s(:,:,i) = griddata(hisu_lon,linspace(1,15,15),squeeze(u_his(jju(1),1:799,:,time(i)))',uy_lon,uy_z)'*0.5;
    u_n(:,:,i) = griddata(hisu_lon,linspace(1,15,15),squeeze(u_his(jju(length(jju)),1:799,:,time(i)))',uy_lon,uy_z)'*0.5;
    
    v_e(:,:,i) = griddata(hisv_lat,linspace(1,15,15),squeeze(v_his(1:799,iiv(1),:,time(i)))',vx_lat,vx_z)'*0.5;
    v_w(:,:,i) = griddata(hisv_lat,linspace(1,15,15),squeeze(v_his(1:799,iiv(length(iiv)),:,time(i)))',vx_lat,vx_z)'*0.5;
    v_s(:,:,i) = griddata(hisv_lon,linspace(1,15,15),squeeze(v_his(jjv(1),1:799,:,time(i)))',vy_lon,vy_z)'*0.5;
    v_n(:,:,i) = griddata(hisv_lon,linspace(1,15,15),squeeze(v_his(jjv(length(jjv)),1:799,:,time(i)))',vy_lon,vy_z)'*0.5;
    
    ub_e(:,i) = interp1(hisu_lat,squeeze(ubar_his(1:799,iiu(1),time(i)))',ux_lat(1,:))'*0.5;
    ub_w(:,i) = interp1(hisu_lat,squeeze(ubar_his(1:799,iiu(length(iiu)),time(i)))',ux_lat(1,:))'*0.5;
    ub_s(:,i) = interp1(hisu_lon,squeeze(ubar_his(jju(1),1:799,time(i)))',uy_lon(1,:))'*0.5;
    ub_n(:,i) = interp1(hisu_lon,squeeze(ubar_his(jju(length(jju)),1:799,time(i)))',uy_lon(1,:))'*0.5;
    
    vb_e(:,i) = interp1(hisv_lat,squeeze(vbar_his(1:799,iiv(1),time(i)))',vx_lat(1,:))'*0.5;
    vb_w(:,i) = interp1(hisv_lat,squeeze(vbar_his(1:799,iiv(length(iiv)),time(i)))',vx_lat(1,:))'*0.5;
    vb_s(:,i) = interp1(hisv_lon,squeeze(vbar_his(jjv(1),1:799,time(i)))',vy_lon(1,:))'*0.5;
    vb_n(:,i) = interp1(hisv_lon,squeeze(vbar_his(jjv(length(jjv)),1:799,time(i)))',vy_lon(1,:))'*0.5;
    
    z_e(:,i) = interp1(his_lat,squeeze(z_his(1:799,ii(1),time(i)))',x_lat(1,:))'*0.5;
    z_w(:,i) = interp1(his_lat,squeeze(z_his(1:799,ii(length(ii)),time(i)))',x_lat(1,:))'*0.5;
    z_s(:,i) = interp1(his_lon,squeeze(z_his(jj(1),1:799,time(i)))',y_lon(1,:))'*0.5;
    z_n(:,i) = interp1(his_lon,squeeze(z_his(jj(length(jj)),1:799,time(i)))',y_lon(1,:))'*0.5;
    
    
end
zt(:) = linspace(0,14.75,60);
v2t(:)= linspace(0,14.75,60);
v3t(:)= linspace(0,14.75,60);
st(:)= linspace(0,14.75,60);
tt(:)= linspace(0,14.75,60);

zt(:) = linspace(0,18.75,76);
v2t(:)= linspace(0,18.75,76);
v3t(:)= linspace(0,18.75,76);
st(:)= linspace(0,18.75,76);
tt(:)= linspace(0,18.75,76);

zt = linspace(0,29,117);
v2t= linspace(0,29,117);
v3t= linspace(0,29,117);
st= linspace(0,29,117);
tt= linspace(0,29,117);

ncwrite(varargin,'temp_south',t_s);
ncwrite(varargin,'temp_east',t_e);
ncwrite(varargin,'temp_north',t_n);
ncwrite(varargin,'temp_west',t_w);
ncwrite(varargin,'salt_south',s_s);
ncwrite(varargin,'salt_east',s_e);
ncwrite(varargin,'salt_north',s_n);
ncwrite(varargin,'salt_west',s_w);
ncwrite(varargin,'u_south',u_s);
ncwrite(varargin,'u_east',u_e);
ncwrite(varargin,'u_north',u_n);
ncwrite(varargin,'u_west',u_w);
ncwrite(varargin,'v_south',v_s);
ncwrite(varargin,'v_east',v_e);
ncwrite(varargin,'v_north',v_n);
ncwrite(varargin,'v_west',v_w);
ncwrite(varargin,'vbar_south',vb_s);
ncwrite(varargin,'vbar_east',vb_e);
ncwrite(varargin,'vbar_north',vb_n);
ncwrite(varargin,'vbar_west',vb_w);
ncwrite(varargin,'zeta_south',z_s);
ncwrite(varargin,'zeta_east',z_e);
ncwrite(varargin,'zeta_north',z_n);
ncwrite(varargin,'zeta_west',z_w);
ncwrite(varargin,'ubar_south',ub_s);
ncwrite(varargin,'ubar_east',ub_e);
ncwrite(varargin,'ubar_north',ub_n);
ncwrite(varargin,'ubar_west',ub_w);
ncwrite(varargin,'zeta_time',zt);
ncwrite(varargin,'salt_time',st);
ncwrite(varargin,'temp_time',tt);
ncwrite(varargin,'v2d_time',v2t);
ncwrite(varargin,'v3d_time',v3t);

clearvars -except *_his


close(nc);


u_s = griddata(hisu_lat,linspace(1,15,15),squeeze(u_his(1:799,iiu(1),:,2))',ux_lat,ux_z)

figure;pcolor(hisu_lat(1,jju),linspace(1,15,15),squeeze(ubar_his(jju(:),iiu(1),2))');shading interp;colorbar
figure;pcolor(ux_lat,ux_z,u_s);shading interp;colorbar;

figure;plot(hisu_lat(1,jju),squeeze(ubar_his(jju(:),iiu(1),2))')
utest = interp1(hisu_lat,squeeze(ubar_his(1:799,iiu(1),2))',ux_lat(1,:))
plot(utest)

temp_e = griddata(his_lat,linspace(1,15,15),squeeze(temp_his(1:799,ii(1),:,2))',x_lat,x_z);
temp_e = griddata(his_lon,linspace(1,15,15),squeeze(temp_his(jj(1),1:799,:,2))',y_lon,y_z);
temp_s = griddata(his_lon,linspace(1,15,15),squeeze(temp_his(jj(length(jj)),1:799,:,i))',y_lon,y_z);
figure;pcolor(his_lon(ii,1),linspace(1,15,15),squeeze(temp_his(jj(1),ii(:),:,2))');shading interp;colorbar
figure;pcolor(y_lon,y_z,t_s(:,:,1)');shading interp;colorbar;


lenth()