function oct_nested_forcing(child_grd,parent_frc,child_frc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  compute the forcing file of the embedded grid
%
%  Further Information:  
%  http://www.croco-ocean.org
%  
%  This file is part of CROCOTOOLS
%
%  CROCOTOOLS is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published
%  by the Free Software Foundation; either version 2 of the License,
%  or (at your option) any later version.
%
%  CROCOTOOLS is distributed in the hope that it will be useful, but
%  WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software
%  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
%  MA  02111-1307  USA
%
%  Copyright (c) 2004-2006 by Pierrick Penven 
%  e-mail:Pierrick.Penven@ird.fr  
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
extrapmask=0;
%
% Title
%
title=['Forcing file for the embedded grid :',child_frc,...
' using oct_parent forcing file: ',parent_frc];
disp(' ')
disp(title)
%
% Read in the embedded grid
%
disp(' ')
disp(' Read in the embedded grid...')
ncid = netcdf.open(child_grd, 'NC_NOWRITE');
parent_grd=ncid.parent_grid(:);
imin=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'grd_pos'));
imax=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'grd_pos'));
jmin=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'grd_pos'));
jmax=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'grd_pos'));
refinecoeff=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'refine_coef'));
result=close(ncid);
ncid = netcdf.open(parent_grd, 'NC_NOWRITE');
Lp=netcdf.inqDimLen(ncid, netcdf.inqDimID(ncid, 'xi_rho'));
Mp=netcdf.inqDimLen(ncid, netcdf.inqDimID(ncid, 'eta_rho'));
if extrapmask==1
  mask=netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'mask_rho'));
else
  mask=[];
end
result=close(ncid);
%
% Read in the oct_parent forcing file
%
disp(' ')
disp(' Read in the oct_parent forcing file...')
ncid = netcdf.open(parent_frc, 'NC_NOWRITE');

smst = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'sms_time'));
smsc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'sms_time'), 'cycle_length');
shft = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'shf_time'));
shfc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'shf_time'), 'cycle_length');
swft = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'swf_time'));
swfc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'swf_time'), 'cycle_length');
sstt = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'sst_time'));
sstc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'sst_time'), 'cycle_length');
ssst = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'sss_time'));
sssc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'sss_time'), 'cycle_length');
srft = netcdf.getVar(ncid, netcdf.inqVarID(ncid, 'srf_time'));
srfc = netcdf.getAtt(ncid, netcdf.inqVarID(ncid, 'srf_time'), 'cycle_length');

result=close(ncid);
%
% Create the forcing file
%
disp(' ')
disp(' Create the forcing file...')
oct_create_nestedforcing(child_frc,parent_frc,child_grd,title,smst,...
                         shft,swft,srft,sstt,ssst,smsc,...
                         shfc,swfc,srfc,sstc,sssc)
%
% oct_parent indices
%
[igrd_r,jgrd_r]=meshgrid((1:1:Lp),(1:1:Mp));
[igrd_p,jgrd_p]=meshgrid((1:1:Lp-1),(1:1:Mp-1));
[igrd_u,jgrd_u]=meshgrid((1:1:Lp-1),(1:1:Mp));
[igrd_v,jgrd_v]=meshgrid((1:1:Lp),(1:1:Mp-1));
%
% the children indices
%
ipchild=(imin:1/refinecoeff:imax);
jpchild=(jmin:1/refinecoeff:jmax);
irchild=(imin+0.5-0.5/refinecoeff:1/refinecoeff:imax+0.5+0.5/refinecoeff);
jrchild=(jmin+0.5-0.5/refinecoeff:1/refinecoeff:jmax+0.5+0.5/refinecoeff);
[ichildgrd_p,jchildgrd_p]=meshgrid(ipchild,jpchild);
[ichildgrd_r,jchildgrd_r]=meshgrid(irchild,jrchild);
[ichildgrd_u,jchildgrd_u]=meshgrid(ipchild,jrchild);
[ichildgrd_v,jchildgrd_v]=meshgrid(irchild,jpchild);
%
% interpolations
% 
disp(' ')
disp(' Do the interpolations...')                 
np_id = netcdf.open(parent_frc, 'NC_NOWRITE');
ncid = netcdf.open(child_frc, 'NC_WRITE');
disp('the wind...')
for tindex=1:length(smst)
  oct_interpvar3d(np_id,ncid,igrd_u,jgrd_u,ichildgrd_u,jchildgrd_u,'sustr',mask,tindex)
  oct_interpvar3d(np_id,ncid,igrd_v,jgrd_v,ichildgrd_v,jchildgrd_v,'svstr',mask,tindex)
end
disp('heat flux...')
for tindex=1:length(shft)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'shflux',mask,tindex)
end
disp('freshwater flux...')
for tindex=1:length(swft)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'swflux',mask,tindex)
end
disp('SST and sst correction...')
for tindex=1:length(sstt)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'SST',mask,tindex)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'dQdSST',mask,tindex)
end
disp('SSS...')
for tindex=1:length(ssst)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'SSS',mask,tindex)
end
disp('shortwave radiation...')
for tindex=1:length(srft)
  oct_interpvar3d(np_id,ncid,igrd_r,jgrd_r,ichildgrd_r,jchildgrd_r,'swrad',mask,tindex)
end
result=close(np_id);
result=close(ncid);
disp(' ')
disp(' Done ')
%
% Make a plot
%
disp(' ')
disp(' Make a plot...')
figure(1)
oct_plot_nestforcing(child_frc,'SSS',[1 6],1)
