%atn 9.Feb.2021
clear all
warning off

addpath('./ASTE_mlibrary/');		%<-- add this path, wherever it is you've downloaded
addpath('./ASTE_mlibrary/gcmfaces_mod/');

dirdiags_nc=['./'];			%<-- whevever nctiles_monthly and nctiles_monthly_climatology are located
dirgrid_nc =['./nctiles_grid/'];	%<-- directory for grid, inside should have tiles_params.mat

%the most important structure needed is the tile, located inside ASTE_mlibrary and also inside nctiles_grid
global tiles
load(['./ASTE_mlibrary/tiles_params.mat'],'tiles');	%tiles

%tiles = 
%  struct with fields:
%      tot: 45
%      wet: 29
%     list: [45×2 double]
%    tsize: [90 90]
%    fsize: [270 1350 1]
%      nfx: [270 0 270 180 450]
%      nfy: [450 0 270 270 270]
%     mask: {[270×450 double]  []  [270×270 double]  [180×270 double]  [450×270 double]}
%      lat: 13.7500
%    field: []
%    index: {1×29 cell}

%for the Nordic Seas, these are the tiles, which are HARDCODED in the function, leave as comment out here for info:
%itile=[5 6 7 8 11 14];	%tile 5-7 on face1 tile 8,11,14 on face3
%flag_aste=0;		%1 to put output into full ASTE domain compact size, 0 to leave as individual tiles (to save memory)

%Pick some zlevels and time slices as example:
iz=[1,5];		%0-10m (center at 5m, dz=10m) , 40-50m (center at 45m, dz = 10m)
itime=[167:168];	%entire time series 192 months cover Jan/2002 to Dec/2017, 168 = Dec/2016;
itime_clim=[6,9];	%for climatology, here use example for Jun, Sep

NS=[];	%Nordic Seas structure

%first, we test reading in grid, example as 'Depth','hFacC'. For grid (flag_field = -1), itime does not matter.
flag_field=-1;
NS.bathy=merge_tiles_nordic_seas_tracer({'Depth'},dirgrid_nc, 1,itime,flag_field);%iz=1 only for 2D field
NS.hFacC=merge_tiles_nordic_seas_tracer({'hFacC'},dirgrid_nc,iz,itime,flag_field);%iz multiple for 3D field

%now read in time-dependent (flag_field = 0), use THETA as example
flag_field=0;
NS.theta=merge_tiles_nordic_seas_tracer({'THETA'},[dirdiags_nc 'nctiles_monthly/'],iz,itime,flag_field);

%now read in climatology (flag_field = 1), use THETA as example, takes time between 1:12, here we pick Jun,Sep [6,9]
flag_field=1;
NS.theta_clim=merge_tiles_nordic_seas_tracer({'THETA'},[dirdiags_nc 'nctiles_monthly_climatology/'],iz,itime_clim,flag_field);

%now onto vector PAIR:
%grid: 5th argument flag_sign=0 (not flip sign), 6th argument flag_field = -1 to specify using function to read grid
flag_sign=0;flag_field=-1;
[tmpu,tmpv]=merge_tiles_nordic_seas_vector({'hFacW','hFacS'},dirgrid_nc,iz,itime,flag_sign,flag_field);%itime is dummy for grid
NS.hFacW=tmpu;
NS.hFacS=tmpv;

%time-dependent U,V, flag_sign = 1 (flip sign), flag_field = 0 (read time-varying field)
flag_sign=1;flag_field=0;
[tmpu,tmpv]=merge_tiles_nordic_seas_vector({'UVELMASS','VVELMASS'},[dirdiags_nc 'nctiles_monthly/'],iz,itime,flag_sign,flag_field);
NS.UVELMASS=tmpu;
NS.VVELMASS=tmpv;

%climatological (flag = 1) for months jun,sep (6,9)
flag_sign=1;flag_field=0;
[tmpu,tmpv]=merge_tiles_nordic_seas_vector({'UVELMASS','VVELMASS'},[dirdiags_nc 'nctiles_monthly_climatology/'],iz,itime_clim,flag_sign,flag_field);
NS.UVELMASS_clim=tmpu;
NS.VVELMASS_clim=tmpv;

clear tmpu tmpv

NS
%  struct with fields:
%
%            bathy: [270×180 double]
%            hFacC: [270×180×2 double]		%2 depth levels
%            theta: [270×180×2×2 double]	%2 depth levels x 2 time slices
%       theta_clim: [270×180×2×2 double]	%2 depth levels x 2 time slices
%            hFacW: [270×180×2 double]		%2 depth levels
%            hFacS: [270×180×2 double]		%2 depth levels
%         UVELMASS: [270×180×2×2 double]	%2 depth levels x 2 time slices
%         VVELMASS: [270×180×2×2 double]	%2 depth levels x 2 time slices
%    UVELMASS_clim: [270×180×2×2 double]	%2 depth levels x 2 time slices
%    VVELMASS_clim: [270×180×2×2 double]	%2 depth levels x 2 time slices

figure(1);clf;colormap(jet(21));
subplot(2,3,1);imagesc(squeeze(NS.theta(:,:,1,1))');colorbar('southoutside');grid;set(gca,'ydir','normal');
  title(['T(iz=' num2str(iz(1)) ',itime=' num2str(itime(1)) '), itile [5,6,7,8,11,14]']);
subplot(2,3,2);imagesc(squeeze(NS.UVELMASS(:,:,1,1))');colorbar('southoutside');grid;set(gca,'ydir','normal');
  title(['U [5,6,7,8,11,14]']);cc1=caxis;caxis([-max(cc1) max(cc1)]);
subplot(2,3,3);imagesc(squeeze(NS.VVELMASS(:,:,1,1))');colorbar('southoutside');grid;set(gca,'ydir','normal');
  title(['V [5,6,7,8,11,14]']);cc1=caxis;caxis([-max(cc1) max(cc1)]);
subplot(2,3,4);plot(squeeze(NS.theta(123,:,1,1)),'b.-');hold on;
               plot(squeeze(NS.theta(:,180,1,1)),'r.-');hold off;grid;
               legend('T(123,:,1,1)','T(:,180,1,1)');title('profile T');
subplot(2,3,5);plot(squeeze(NS.UVELMASS(123,:,1,1)),'b.-');hold on;
               plot(squeeze(NS.UVELMASS(:,180,1,1)),'r.-');hold off;grid;
               legend('U(123,:,1,1)','U(:,180,1,1)');title('profile U');
subplot(2,3,6);plot(squeeze(NS.VVELMASS(123,:,1,1)),'b.-');hold on;
               plot(squeeze(NS.VVELMASS(:,180,1,1)),'r.-');hold off;grid;
               legend('V(123,:,1,1)','V(:,180,1,1)','location','southeast');title('profile V');
figure(1);set(gcf,'paperunits','inches','paperposition',[0 0 12 8]);
fpr=['./merge_tiles_5_6_7_8_11_14.png'];print(fpr,'-dpng');
