clear all;
close all;

compute  = 1 ; 

%if compute

datadir='/home/cerdata/provider/ghrsst/satellite/l4/glob/odyssea-nrt/data/2014/';
datadir2='/export/home1/ALBATROS/DATA/OSTIA/cmems.isac.cnr.it/Core/SST_GLO_SST_L4_NRT_OBSERVATIONS_010_001/METOFFICE-GLO-SST-L4-NRT-OBS-SST-V2/2014/';

SSTlat=double(ncread([datadir,'001/','20140101-IFR-L4_GHRSST-SSTfnd-ODYSSEA-GLOB_010-v2.0-fv1.0.nc'],'lat'));
SSTlon=double(ncread([datadir,'001/','20140101-IFR-L4_GHRSST-SSTfnd-ODYSSEA-GLOB_010-v2.0-fv1.0.nc'],'lon'));

SSTOlat=double(ncread([datadir2,'01/','20140101120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc'],'lat'));
SSTOlon=double(ncread([datadir2,'01/','20140101120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc'],'lon'));


nemodir1='/home/datawork-lops-siam-moawi/SIAMTEAM/xcouvela/NEMOWAVE/ORCA025/ORCA025_ERAIM/';
nemodir2='/home/datawork-lops-siam-moawi/SIAMTEAM/xcouvela/NEMOWAVE/ORCA025/ORCA025_ERAIM_CPL_NEW_BREIV16/AXELL03/';
%nemodir2='/home/datawork-lops-siam-moawi/SIAMTEAM/xcouvela/NEMOWAVE/ORCA025/ORCA025_ERAIM_CPL_NEW_BREIV16/AXELL03_90J/';

nemocoord='ORCA025_1d_20140101_20141231_grid_T.nc';

nav_lat=double(ncread([nemodir1,nemocoord],'nav_lat'));
nav_lon=double(ncread([nemodir1,nemocoord],'nav_lon'));

jn=near(nav_lat(1,:),25);
js=near(nav_lat(1,:),-25);

%nav_lat=nav_lat(:,jn:end);
%nav_lon=nav_lon(:,jn:end);

[lat,lon]=reorder_glo(nav_lat,nav_lon);

%used for m_proj
minLat=min(nav_lat(:));
maxLat=max(nav_lat(:));
minLon=min(nav_lon(:));
maxLon=max(nav_lon(:));

[SSTLAT,SSTLON]=meshgrid(SSTlat,SSTlon);
[SSTOLAT,SSTOLON]=meshgrid(SSTOlat,SSTOlon);


var1='tos';

TIME=ncread([nemodir1,'ORCA025_1d_20140101_20141231_grid_T.nc'],'time_counter');
time=TIME/86400+datenum(1900,1,1)+28;
D=datevec(time);

jj=1;
%for d=182:274 
for d=1:365 
disp(num2str(d))
m=D(d,2);
j=D(d,3);
     tmpSSTNCPL=ncread([nemodir1,'ORCA025_1d_20140101_20141231_grid_T.nc'],var1,[1 1 d],[inf inf 1]);
     tmpSSTCPL =ncread([nemodir2,'ORCA025_1d_20140101_20141231_grid_T.nc'],var1,[1 1 d],[inf inf 1]);

     tmpice=ncread([nemodir1,'ORCA025_1d_20140101_20141231_icemod.nc'],'siconc',[1 1 d],[inf inf 1]);
     SSTNCPL(:,:,jj)=reorder_glo(tmpSSTNCPL,nav_lon);
     SSTCPL(:,:,jj) =reorder_glo(tmpSSTCPL,nav_lon);
     ICE(:,:,jj)=reorder_glo(tmpice,nav_lon);
      

%     SST=ncread([datadir,num2str(d,'%03i'),'/','2014',num2str(m,'%02i'),num2str(j,'%02i'),...
%          '-IFR-L4_GHRSST-SSTfnd-ODYSSEA-GLOB_010-v2.0-fv1.0.nc'],'analysed_sst',[1 1 1],[inf inf inf])-273.15;
    
%     SSTgn(:,:,jj)=griddata(SSTLON,SSTLAT,SST,lon,lat);

     %OSTIA SST
     SSTO   =ncread([datadir2,num2str(m,'%02i'),'/','2014',num2str(m,'%02i'),num2str(j,'%02i'),...
                           '120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc'],'analysed_sst')-273.15;

     SSTOgn(:,:,jj)=griddata(SSTOLON,SSTOLAT,SSTO,lon,lat);

jj=jj+1;

end

MASK=SSTNCPL;
MASK(MASK==0)=NaN;
MASK(~isnan(MASK))=1;

IMASK=ICE;

IMASK(IMASK~=0)=NaN;
IMASK(~isnan(IMASK))=1;
MASK=MASK.*IMASK;

%SSTgn=SSTgn.*MASK;
SSTOgn=SSTOgn.*MASK;

SSTNCPL=SSTNCPL.*MASK;
SSTCPL=SSTCPL.*MASK;

SSTdiff=(SSTNCPL-SSTgn);
SSTdiffcpl=(SSTCPL-SSTgn);
SSTdiffmod=(SSTCPL-SSTNCPL);

meandiff=squeeze(nanmean(SSTdiff,3));

m_proj('Gall-Peters','lon',[minLon maxLon],'lat',[minLat maxLat])
m_pcolor(lon,lat,meandiff);shading flat;colorbar;
m_coast('patch',0.3*[.6 .6 .6],'edgecolor','k');
m_grid('box','fancy','tickdir','in','FontSize',15);


figure
m_pcolor(lon,lat,squeeze(nanmean(SSTdiff(:,:,1:90))));shading flat;colorbar;
m_coast('patch',0.3*[.6 .6 .6],'edgecolor','k');
m_grid('box','fancy','tickdir','in','FontSize',15);
figure
m_pcolor(lon,lat,isqueeze(nanmean(SSTdiff(:,:,192:274))));shading flat;colorbar;
m_coast('patch',0.3*[.6 .6 .6],'edgecolor','k');
m_grid('box','fancy','tickdir','in','FontSize',15);

j0=near(lat(1,:),-60);

figure;
plot(squeeze(nanmean(nanmean(SSTOgn(:,j0:js,:)))),'linewidth',2)
hold on
plot(squeeze(nanmean(nanmean(SSTNCPL(:,j0:js,:)))),'linewidth',2) 
plot(squeeze(nanmean(nanmean(SSTCPL(:,j0:js,:)))),'linewidth',2)
%plot(squeeze(nanmean(nanmean(SSTgn(:,j0:js,:))))) 
datetick
set(gca,'fontsize',12,'fontweight','bold')
grid
ylabel('SST (C)')
legend('OSTIA','No\_CPL','CPL\_ALL2');

figure;
plot(squeeze(nanmean(nanmean(SSTOgn(:,jn:j1,:)))),'linewidth',2)
hold on
plot(squeeze(nanmean(nanmean(SSTNCPL(:,jn:j1,:)))),'linewidth',2)
plot(squeeze(nanmean(nanmean(SSTCPL(:,jn:j1,:)))),'linewidth',2)
%plot(squeeze(nanmean(nanmean(SSTgn(:,j0:js,:))))) 
datetick
set(gca,'fontsize',12,'fontweight','bold')
grid
ylabel('SST (C)')
legend('OSTIA','No\_CPL','CPL\_ALL2');





figure;
hold on
plot(squeeze(nanmean(nanmean(SSTOgn(:,js:jn,:)))),'linewidth',2)
hold on
plot(squeeze(nanmean(nanmean(SSTNCPL(:,js:jn,:)))),'linewidth',2)
plot(squeeze(nanmean(nanmean(SSTCPL(:,js:jn,:)))),'linewidth',2)
%plot(squeeze(nanmean(nanmean(SSTgn(:,j0:js,:))))) 
datetick
set(gca,'fontsize',12,'fontweight','bold')
grid
ylabel('SST (C)')
legend('OSTIA','No\_CPL','CPL\_ALL2');



plot(squeeze(abs(nanmean(nanmean(SSTNCPL(:,j0:js,:)-SSTOgn(:,j0:js,:))))),'linewidth',2)
plot(squeeze(abs(nanmean(nanmean(SSTCPL(:,j0:js,:)-SSTOgn(:,j0:js,:))))),'linewidth',2)








plot(squeeze(nanmean(nanmean(SSTNCPL(:,jn:end,:)))))
hold on
plot(squeeze(nanmean(nanmean(SSTCPL(:,jn:end,:)))))
plot(squeeze(nanmean(nanmean(SSTOgn(:,jn:end,:)))))
plot(squeeze(nanmean(nanmean(SSTgn(:,jn:end,:)))))





stop

