clear
close all


fname_rain_ano='/tigress/GEOCLIM/LRGROUP/shared_data/precipitation/CMAP/precip.mon.mean_deseason.nc';
fname_rain='/tigress/GEOCLIM/LRGROUP/shared_data/precipitation/CMAP/precip.mon.mean_enhanced_monthly.nc';
fname_sss='/tigress/GEOCLIM/LRGROUP/shared_data/sss_satellite/all_sss_201109_201902.combine.smap.aquarius.nc';
fname_sss_ano='/tigress/GEOCLIM/LRGROUP/shared_data/sss_satellite/all_sss_201109_201902.combine.smap.aquarius_deseason.nc';
fname_sss_argo='/tigress/GEOCLIM/LRGROUP/shared_data/temp_salt_Argo/grid_scripps/RG_ArgoClim_Psal.nc';

fn_out=['obs_rain_salinity.nc'];
fco2_region=[0 360 -90 90];
trange=[datenum(1982,1,1) datenum(2018,1,1)];

[rain,lon_rain,lat_rain,time_rain]=read_ncall('fn',fname_rain,'vn','precip','xn','lon','yn','lat','tn','time','tb',datenum(1800,1,1),'ts',1/24,'xr',fco2_region(1:2),'yr',fco2_region(3:4),'ry','t','tr',trange);

[rain_ano,lon_rain,lat_rain,time_rain]=read_ncall('fn',fname_rain_ano,'vn','precip','xn','lon','yn','lat','tn','time','tb',datenum(1800,1,1),'ts',1/24,'xr',fco2_region(1:2),'yr',fco2_region(3:4),'ry','t','tr',trange);

[sss_sate,lon_sss,lat_sss,time_sss]=read_ncall('fn',fname_sss,'vn','sss','xn','lon','yn','lat','tn','time','tb',datenum(1900,1,1),'xr',fco2_region(1:2),'yr',fco2_region(3:4));
[sss_ano_sate,lon_sss,lat_sss,time_sss]=read_ncall('fn',fname_sss_ano,'vn','sss','xn','lon','yn','lat','tn','time','tb',datenum(1900,1,1),'xr',fco2_region(1:2),'yr',fco2_region(3:4));

[sss_ano_argo,lon_argo,lat_argo,time_argo,dep_argo]=read_ncall('fn',fname_sss_argo,'vn','ARGO_SALINITY_ANOMALY','xn','LONGITUDE','yn','LATITUDE','tn','TIME','zn','PRESSURE','tb',datenum(1900,1,1),'xr',fco2_region(1:2),'yr',fco2_region(3:4),'zr',[0 5],'cx',20);
sss_ano_argo=squeeze(sss_ano_argo);
sss_clim_argo=squeeze(ncread(fname_sss_argo,'ARGO_SALINITY_MEAN'));
sss_clim_argo=circshift(sss_clim_argo,20,1); sss_clim_argo=squeeze(sss_clim_argo(:,:,1))';
[jm,im,lm]=size(sss_ano_argo); sss_clim_argo=repmat(sss_clim_argo,[1,1,lm]);
sss_argo=sss_ano_argo+sss_clim_argo;
time_argo=datenum(2003,13+double(ncread(fname_sss_argo,'TIME')),15);
[sss_ano_argo,sss_clim_argo] = rmseason3d(sss_argo,time_argo);

[sss_ano_sate,sss_clim_sate] = rmseason3d(sss_sate,time_sss);

[jm,im,lm]=size(rain);
[lon,lat]=meshgrid([0.5:1:359.5],[-89.5:1:89.5]);
[jm,im]=size(lon);
rain_grid=nan(jm,im,lm);         rain_ano_grid=nan(jm,im,lm);
sss_sate_grid=nan(jm,im,lm);     sss_ano_sate_grid=nan(jm,im,lm); sss_clim_sate_grid=nan(jm,im,lm);
sss_argo_grid=nan(jm,im,lm);     sss_ano_argo_grid=nan(jm,im,lm); sss_clim_argo_grid=nan(jm,im,lm);
for l=1:lm
    rain_grid(:,:,l)=interp2(lon_rain,lat_rain,squeeze(rain(:,:,l)),lon,lat);
    rain_ano_grid(:,:,l)=interp2(lon_rain,lat_rain,squeeze(rain_ano(:,:,l)),lon,lat);
    [yr,mo,da]=datevec(time_rain(l));
    inds=find(time_sss>=datenum(yr,mo,1) & time_sss<=datenum(yr,mo+1,1));
    if ~isempty(inds)
       sss_sate_grid(:,:,l)=interp2(lon_sss,lat_sss,squeeze(sss_sate(:,:,inds)),lon,lat);
       sss_ano_sate_grid(:,:,l)=interp2(lon_sss,lat_sss,squeeze(sss_ano_sate(:,:,inds)),lon,lat);
       sss_clim_sate_grid(:,:,l)=interp2(lon_sss,lat_sss,squeeze(sss_clim_sate(:,:,inds)),lon,lat);
    end

    inda=find(time_argo>=datenum(yr,mo,1) & time_argo<=datenum(yr,mo+1,1));
    if ~isempty(inda)
       sss_argo_grid(:,:,l)=interp2(lon_argo,lat_argo,squeeze(sss_argo(:,:,inda)),lon,lat);
       sss_ano_argo_grid(:,:,l)=interp2(lon_argo,lat_argo,squeeze(sss_ano_argo(:,:,inda)),lon,lat);
       sss_clim_argo_grid(:,:,l)=interp2(lon_argo,lat_argo,squeeze(sss_clim_argo(:,:,inda)),lon,lat);
    end
end

toc;disp(['write nc...']); tic
[var_all]=load_var_all(1); lon=lon(1,:); lat=lat(:,1); time=time_rain;

create_mom6_1var3d(fn_out,'lon','lat','time','time_bnds',var_all(1,:),lon,lat,time-datenum(1900,1,1),[],permute(rain_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(2,:),lon,lat,time-datenum(1900,1,1),[],permute(rain_ano_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(3,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_sate_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(4,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_ano_sate_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(5,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_clim_sate_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(6,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_argo_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(7,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_ano_argo_grid,[2,1,3]));
create_mom6_1var3d_append(fn_out,'lon','lat','time','time_bnds',var_all(8,:),lon,lat,time-datenum(1900,1,1),[],permute(sss_clim_argo_grid,[2,1,3]));
ncwriteatt(fn_out,'/','data description',['this is regridded data from CMAP, SSS NASA, salinity Argo, only works in tropical Pacific region']);


function [var_all]=load_var_all(load_ind)

   if load_ind==1;
      %write fco2, fco2_ano, dpco2, dpco2_ano, pco2, pco2_ano, kw, kw_ano, fco2_kw, fco2_dpco2, fco2_ct, fco2_resi
      var_all{1,1}='rain';          var_all{1,2}='mm/d';         var_all{1,3}=['Average Monthly Rate of Precipitation from CMAP'];
      var_all{2,1}='rain_ano';      var_all{2,2}='mm/d';         var_all{2,3}=['Monthly Anomaly of Precipitation from CMAP'];
      var_all{3,1}='sss_sate';      var_all{3,2}='psu';          var_all{3,3}=['Monthly sea surface salinity from NASA satellite'];
      var_all{4,1}='sss_ano_sate';  var_all{4,2}='psu';          var_all{4,3}=['Monthly Anomaly of SSS from NASA satellite'];
      var_all{5,1}='sss_clim_sate'; var_all{5,2}='psu';          var_all{5,3}=['Monthly clim SSS from NASA satellite'];
      var_all{6,1}='sss_argo';      var_all{6,2}='psu';          var_all{6,3}=['Monthly gridded Argo surface salinity (2.5m) from Scripps gridded Argo'];
      var_all{7,1}='sss_ano_argo';  var_all{7,2}='psu';          var_all{7,3}=['Monthly Anomaly of gridded Argo surface salinity (2.5m) from Scripps gridded Argo'];
      var_all{8,1}='sss_clim_argo'; var_all{8,2}='psu';          var_all{8,3}=['Monthly clim gridded Argo surface salinity (2.5m) from Scripps gridded Argo'];
   end
end





