#--updated on 4/5/2017 with improved Sheng & Zwiers algorithm, ThL
#--corrected some interpolation preprocessing (compared to v4)
#--updated on 5/5/2017 with correction factor (30/46) on NOx vs. NO
#--updated on 9/5/2017 with output separation BB/anthro for SO2, NOx and NH3
#--corrected on 22/6/2017 for BB: undef values zeroed before remapping
#--corrected on 26/09/2017 for NOx units: PNNL dataset is kg NO2, VUA is kg NO. 
#  INCA expects NO2 in its AER version, this may have to be changed for the full INCA version
#
#INCA  conc_dms flx_nox flx_bc flx_pom flx_bbbc flx_bbpom flx_so2 flx_so4 flx_h2s flx_nh3
#CMIP6 species  NOx     BC     OC                         SO2                     NH3
# + NMVOC CO

#--INCA example file where dms_conc can be reused
fileINCAex="/home/oboucher/CMIP6/AER_EMISSIONS/INCAfile/sflx_lmdz_phy_1997.nc"

#--input directory for anthropogenic (non-BB) emissions
dirinPNNL="/prodigfs/project/input4MIPs/PNNL-JGCRI/emissions/CMIP/CEDS-2017-05-18/mon/"
##--Be careful if ever one needs the 3D (=AIR) data, the latest are in the different directory
##dirinPNNL_AIR="/prodigfs/project/input4MIPs/PNNL-JGCRI/emissions/CMIP/mon/CEDS-2017-08-30/"
#--input directory for anthropogenic (BB) emissions
dirinVUA="/prodigfs/project/input4MIPs/VUA/emissions/CMIP/VUA-CMIP-BB4CMIP6-1-2/mon/"

#--output directory
dirout="/data/"${USER}"/CMIP6/AEROSOL/"
if [ ! -d ${dirout} ] ; then mkdir -p ${dirout} ; fi

#--LMDz grid information
grid="144-143"
gridfile="/home/oboucher/CMIP6/GRID/grid-lmdz-lonlat_144x143"
nbpoint=$((144*141+2))

#--year
for year in {1850..2014}
do

#--species 
for species in "BC" "NOx" "OC" "SO2" "NH3"
do

if [ ${species} = "BC"  ] ; then speciesinca="bc"  ; fi
if [ ${species} = "NOx" ] ; then speciesinca="nox" ; fi
if [ ${species} = "OC"  ] ; then speciesinca="pom" ; fi
if [ ${species} = "SO2" ] ; then speciesinca="so2" ; fi
if [ ${species} = "NH3" ] ; then speciesinca="nh3" ; fi

#--finding correct file for PNNL data
if [ $year -ge 1750 -a $year -lt 1800 ]; then
year1=1750
year2=1799
elif [ $year -ge 1800 -a $year -lt 1850 ]; then 
year1=1800
year2=1849
elif [ $year == 1850 ]; then
year1=1850
year2=1850
elif [ $year -ge 1851 -a $year -lt 1900 ]; then 
year1=1851
year2=1899
elif [ $year -ge 1900 -a $year -lt 1950 ]; then 
year1=1900
year2=1949
elif [ $year -ge 1950 -a $year -lt 2000 ]; then 
year1=1950
year2=1999
elif [ $year -ge 2000 -a $year -lt 2015 ]; then 
year1=2000
year2=2014
else
echo 'Houston we have a problem for the PNNL data'
exit 1
fi

#--input file PNNL
filename=${dirinPNNL}${species}_em_anthro/gn/v20170519/${species}-em-anthro_input4MIPs_emissions_CMIP_CEDS-2017-05-18_gn_${year1}01-${year2}12.nc

#--output files
filenameout1=${dirout}flux_${speciesinca}_${year}.nc
filenameout2=${dirout}flux_lmdz_${speciesinca}_${year}.nc
filenameout3=${dirout}flux_vector_${speciesinca}_${year}.nc

echo ${filename} ${filenameout1} ${filenameout2} ${filenameout3}
rm -f ${filenameout1} ${filenameout2} ${filenameout3}

#--unfortunately idl not happy with PNNL netcdf files so need to ferretize files
#--I also sum over sectors and I extract the correct year as well
rm -f rewrite.jnl
cat << eod > rewrite.jnl
use "${filename}"
set region/t=16-jan-${year}:16-dec-${year}
save/clobber/file="${filenameout1}" ${species}_EM_ANTHRO[k=@sum]
eod

#--run ferret script
ferret << eod 
go rewrite.jnl
exit
eod

#--remap to LMDz grid
#--OC to POM conversion factor
#--otherwise change to capital letters if not (eg NOx)
if [ ${species} == "OC"  ] ; then 
echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1} ${filenameout2}
cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux -mulc,1.4 ${filenameout1} ${filenameout2}
else
echo cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux  ${filenameout1} ${filenameout2}
cdo remapcon,${gridfile} -chname,`echo ${species}_EM_ANTHRO | awk '{print toupper($0)}'`,flux ${filenameout1} ${filenameout2}
fi

#--Improved Sheng & Zwiers algorithm + transform into vector
rm -f regrid.pro
cat << eod >> regrid.pro
pro regrid
filename='${filenameout2}'
print, filename
NETCDFREAD,filename,'flux',flux,dimflux
NETCDFREAD,filename,'lat',lat,dimlat0
NETCDFREAD,filename,'lon',lon,dimlon0
NETCDFREAD,filename,'TIME',time,dimtime0
dimlat=dimlat0(0)
dimlon=dimlon0(0)
dimtime=dimtime0(0)
print, 'dim flux=', dimflux
A = float([	[3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$
		[1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$
		[1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.]   ])
fluxinit=flux
flux_check=flux
for lo=0,dimlon-1 do begin
for la=0,dimlat-1 do begin
flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*))
endfor
endfor			
m_bloq = make_array(dimlon,dimlat,12,value=0)		; Matrice booléenne "mois à bloquer ou non"
if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1
; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq
for lo=0,dimlon-1 do begin
for la=0,dimlat-1 do begin
whereneg = where(flux_check(lo,la,*) lt 0)		; (12 pts max) Identification de potentiels points à problèmes, corrigés négativement
nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
flux_corr=flux_check(lo,la,*)				; Création d'un vecteur pour recevoir les valeurs corrigées, initialisé à flux_check au cas où on n'ait rien à faire d'autre qu'une seule itération
A2 = A							; Je repars de la matrice A initiale, ce pour chaque point de grille
;							; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives
while nbannul ne 0 do begin				; Si l'on a effectivement des émissions corrigées négativement...
m_bloq(lo,la,whereneg) = 1				; Update de la matrice m_bloq
for m=0,11 do begin
if m eq 11 then begin					; Pour plus de facilité, mois précédents et suivants codés ici
p=10
s=0
endif else if m eq 0 then begin
p=11
s=1
endif else begin
p = m-1
s = m+1
endelse
if m_bloq(lo,la,m) then begin				; Je traite les mois bloqués en eux-mêmes
A2(p,m) = 0.
A2(m,m) = 1.
A2(s,m) = 0.
endif							; Fin du cas si l'on est sur un mois bloqué
if ~m_bloq(lo,la,m) then begin				; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué
if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin   		; Mois encadré de deux mois bloqués
A2(p,m) = 1./4.
A2(m,m) = 1./2.
A2(s,m) = 1./4.
endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin	; Mois précédent bloqué (uniquement)
A2(p,m) = 2./8.
A2(m,m) = 5./8.
A2(s,m) = 1./8.
endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin	; Mois suivant bloqué (uniquement)
A2(p,m) = 1./8.
A2(m,m) = 5./8.
A2(s,m) = 2./8.
endif
endif								; Fin du cas mois non bloqué
endfor								; Fin de la boucle sur les mois, balayage de la matrice
flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*))		; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2)
whereneg = where(flux_corr lt 0)				; (12 pts max) Ré-identification de potentiels nouveaux points à problèmes, corrigés négativement
nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
endwhile							; Fin du cas où l'on avait des problèmes d'émissions corrigées négativement
; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante :
;	valeur négative ou nulle <=> mois bloqué
;	valeur positive <=> mois à interpolation classique
flux(lo,la,*) = flux_corr					; En sortie de la boucle while, normalement flux_corr est complètement positif
endfor								; Fin boucle lat
endfor								; Fin boucle lon
nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1)
if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1))		; Je force à des valeurs négatives
;
month_in_year=12
nbpoint=${nbpoint}
flux2=fltarr(nbpoint,month_in_year)
flux2(*,*)=0.0
;
for l=0,month_in_year-1 do begin
flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon)
flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon)
endfor
;
pt=1
for j=1,dimlat-2  do begin
for i=0,dimlon-1  do begin
for l=0,month_in_year-1 do begin
  flux2(pt,l)=flux(i,j,l)
endfor
pt=pt+1
endfor
endfor
;
;saving netcdf file
;
fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $
            flx_${speciesinca}:fltarr(nbpoint,month_in_year) }
;
fluxstruct.vector=float(indgen(nbpoint)+1)
;;fluxstruct.time=float(indgen(month_in_year)+1)
fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345]
fluxstruct.flx_${speciesinca}=flux2
;
attributes = {units:strarr(3),long_name:strarr(3)}
attributes.units = ['vector','days since 1960-01-01','flux']
attributes.long_name = ['vector', 'time', 'flux']
;
dimensions = {isdim:intarr(3), links:intarr(2,3)}
       dimensions.isdim =  [1,1,0]  ; (1=dimension, 0=variable)
       dimensions.links = [ [-1,-1],[-1,-1],[0,1]    ]
;
netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions
;
end
eod

#
#--calling IDL
#
/opt/idl-6.4/idl/bin/idl << eod
.r netcdf
.r netcdfwrite
.r struct_dims
.r regrid
regrid
exit
eod
#

#--finding correct file for VUA data
if [ $year -ge 1750 -a $year -lt 1850 ]; then
year1=1750
year2=1849
elif [ $year -ge 1850 -a $year -lt 2016 ]; then 
year1=1850
year2=2015
else
echo 'Houston we have a problem for the VUA data'
exit 1
fi

#--now dealing with BB sources
filename=${dirinVUA}${species}"-em-biomassburning/gn/v20161213/"${species}"-em-biomassburning_input4MIPs_emissions_CMIP_VUA-CMIP-BB4CMIP6-1-2_gn_${year1}01-${year2}12.nc"

#--output files
filenameout1=${dirout}flux_${speciesinca}bb_${year}.nc
filenameout1b=${dirout}flux_0_${speciesinca}bb_${year}.nc
filenameout2=${dirout}flux_lmdz0_${speciesinca}bb_${year}.nc
filenameout3=${dirout}flux_vector_${speciesinca}bb_${year}.nc

echo ${filename} ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3}
rm -f ${filenameout1} ${filenameout1b} ${filenameout2} ${filenameout3}

#--unfortunately idl not happy with VUA netcdf files so need to ferretize files
#--I extract the correct year as well
rm -f rewrite.jnl
cat << eod > rewrite.jnl
set memory/size=100
use "${filename}"
set region/t=16-jan-${year}:16-dec-${year}
save/clobber/file="${filenameout1}" ${species}
eod

#--run ferret script
ferret << eod
go rewrite.jnl
exit
eod

#--replace undef with 0               --------------------------- Moved here (before remap) by ThL 22/06/2017
cdo setmisstoc,0.0 ${filenameout1} ${filenameout1b}

#--remap to LMDz grid
#--OC to POM conversion factor
#--as ferret returns NOX, treat NOx NOX inconsistency in names by converting to upper case
if [ ${species} != "OC"  ] ; then
echo cdo remapcon,${gridfile} -chname,`echo ${species} | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2}
cdo remapcon,${gridfile} -chname,`echo ${species} | awk '{print toupper($0)}'`,flux ${filenameout1b} ${filenameout2}
else
echo cdo remapcon,${gridfile} -chname,${species},flux -mulc,1.4 ${filenameout1b} ${filenameout2}
cdo remapcon,${gridfile} -chname,${species},flux -mulc,1.4 ${filenameout1b} ${filenameout2}
fi

#--Improved Sheng & Zwiers algorithm + transform into vector
rm -f regrid.pro
cat << eod >> regrid.pro
pro regrid
filename='${filenameout2}'
print, filename
NETCDFREAD,filename,'flux',flux,dimflux
NETCDFREAD,filename,'lat',lat,dimlat0
NETCDFREAD,filename,'lon',lon,dimlon0
NETCDFREAD,filename,'TIME',time,dimtime0
dimlat=dimlat0(0)
dimlon=dimlon0(0)
dimtime=dimtime0(0)
print, 'dim flux=', dimflux
A = float([	[3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8.],$
		[1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8., 0.],$
		[0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4., 1./8.],$
		[1./8., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1./8., 3./4.]   ])
fluxinit=flux
flux_check=flux
for lo=0,dimlon-1 do begin
for la=0,dimlat-1 do begin
flux_check(lo,la,*) = invert(A) ## transpose(fluxinit(lo,la,*))
endfor
endfor			
m_bloq = make_array(dimlon,dimlat,12,value=0)		; Matrice booléenne "mois à bloquer ou non"
if total(where(flux_check lt 0)) ne -1 then m_bloq(where(flux_check lt 0)) = 1
; Correction/adaptation de la matrice S&Z en fonction du masque booléen m_bloq
for lo=0,dimlon-1 do begin
for la=0,dimlat-1 do begin
whereneg = where(flux_check(lo,la,*) lt 0)		; (12 pts max) Identification de potentiels points à problèmes, corrigés négativement
nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
flux_corr=flux_check(lo,la,*)				; Création d'un vecteur pour recevoir les valeurs corrigées, initialisé à flux_check au cas où on n'ait rien à faire (à part 1 seule correction matricielle)
A2 = A							; Je repars de la matrice A initiale, ce pour chaque point de grille
;							; Potentiellement plusieurs passages pour éliminer toutes les valeurs négatives
while nbannul ne 0 do begin				; Si l'on a effectivement des émissions corrigées négativement...
m_bloq(lo,la,whereneg) = 1				; Update de la matrice m_bloq
for m=0,11 do begin
if m eq 11 then begin					; Pour plus de facilité, mois précédents et suivants codés ici
p=10
s=0
endif else if m eq 0 then begin
p=11
s=1
endif else begin
p = m-1
s = m+1
endelse
if m_bloq(lo,la,m) then begin				; Je traite les mois bloqués en eux-mêmes
A2(p,m) = 0.
A2(m,m) = 1.
A2(s,m) = 0.
endif							; Fin du cas si l'on est sur un mois bloqué
if ~m_bloq(lo,la,m) then begin				; Je traite les mois non bloqués, pour ceux adjacents à un mois bloqué
if m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin   		; Mois encadré de deux mois bloqués
A2(p,m) = 1./4.
A2(m,m) = 1./2.
A2(s,m) = 1./4.
endif else if m_bloq(lo,la,p) and ~m_bloq(lo,la,s) then begin	; Mois précédent bloqué (uniquement)
A2(p,m) = 2./8.
A2(m,m) = 5./8.
A2(s,m) = 1./8.
endif else if ~m_bloq(lo,la,p) and m_bloq(lo,la,s) then begin	; Mois suivant bloqué (uniquement)
A2(p,m) = 1./8.
A2(m,m) = 5./8.
A2(s,m) = 2./8.
endif
endif								; Fin du cas mois non bloqué
endfor								; Fin de la boucle sur les mois, balayage de la matrice
flux_corr = invert(A2) ## transpose(fluxinit(lo,la,*))		; Ré-itération de la multiplication matricielle avec la matrice A modifiée (A2)
whereneg = where(flux_corr lt 0)				; (12 pts max) Ré-identification de potentiels nouveaux points à problèmes, corrigés négativement
nbannul=n_elements(whereneg)*(total(whereneg) ne -1)
endwhile							; Fin du cas où l'on avait des problèmes d'émissions corrigées négativement
; *** IMPORTANT ! *** Pour signaler les mois bloqués, on prend la convention suivante :
;	valeur négative ou nulle <=> mois bloqué
;	valeur positive <=> mois à interpolation classique
flux(lo,la,*) = flux_corr					; En sortie de la boucle while, normalement flux_corr est complètement positif
endfor								; Fin boucle lat
endfor								; Fin boucle lon
nbnegtotal = n_elements(where(m_bloq eq 1)) * (total(where(m_bloq eq 1)) ne -1)
if nbnegtotal ne 0 then flux(where(m_bloq eq 1)) = -flux(where(m_bloq eq 1))		; Je force à des valeurs négatives
;
month_in_year=12
nbpoint=${nbpoint}
flux2=fltarr(nbpoint,month_in_year)
flux2(*,*)=0.0
;
for l=0,month_in_year-1 do begin
flux2(0,l)=TOTAL(flux(*,0,l))/float(dimlon)
flux2(nbpoint-1,l)=TOTAL(flux(*,dimlat-1,l))/float(dimlon)
endfor
;
pt=1
for j=1,dimlat-2  do begin
for i=0,dimlon-1  do begin
for l=0,month_in_year-1 do begin
  flux2(pt,l)=flux(i,j,l)
endfor
pt=pt+1
endfor
endfor
;
;saving netcdf file
;
fluxstruct={vector:fltarr(nbpoint),time:fltarr(month_in_year), $
            flx_bb${speciesinca}:fltarr(nbpoint,month_in_year) }
;
fluxstruct.vector=float(indgen(nbpoint)+1)
;;fluxstruct.time=float(indgen(month_in_year)+1)
fluxstruct.time=[15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345]
fluxstruct.flx_bb${speciesinca}=flux2
;
attributes = {units:strarr(3),long_name:strarr(3)}
attributes.units = ['vector','days since 1960-01-01','flux']
attributes.long_name = ['vector', 'time', 'flux']
;
dimensions = {isdim:intarr(3), links:intarr(2,3)}
       dimensions.isdim =  [1,1,0]  ; (1=dimension, 0=variable)
       dimensions.links = [ [-1,-1],[-1,-1],[0,1]    ]
;
netcdfwrite,'${filenameout3}',fluxstruct,clobber=1, attributes=attributes, dimensions=dimensions
;
end
eod

#
#--calling IDL
#
/opt/idl-6.4/idl/bin/idl << eod
.r netcdf
.r netcdfwrite
.r struct_dims
.r regrid
regrid
exit
eod
#

#--end loop on species
done

#--unfortunately idl use capital letters for variable names so need to change to small letters for now 
rm -f ${dirout}flux_vector_h2s_${year}.nc ${dirout}flux_vector_so4_${year}.nc
cdo expr,'flx_h2s=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_h2s_${year}.nc
cdo expr,'flx_so4=0.*FLX_SO2' ${dirout}flux_vector_so2_${year}.nc ${dirout}flux_vector_so4_${year}.nc

rm -f ${dirout}flux_vector_${year}.nc

#--combining everything into a single file with some final preprocessing
rm -f ${dirout}flux_vector_noxtot_${year}.nc ${dirout}flux_vector_so2tot_${year}.nc ${dirout}flux_vector_nh3tot_${year}.nc

#--deleting output file if already there
rm -f ${dirout}sflx_lmdz_cmip6_${year}.nc
#--merging all files into a single one
#--PNNL NOx is NO2 so no change in unit 
#--VUA NOx is NO so 46/30 ratio to convert to NO2
cdo merge -expr,'flx_bc=FLX_BC' ${dirout}flux_vector_bc_${year}.nc -expr,'flx_bbbc=FLX_BBBC' ${dirout}flux_vector_bcbb_${year}.nc -expr,'flx_pom=FLX_POM' ${dirout}flux_vector_pom_${year}.nc -expr,'flx_bbpom=FLX_BBPOM' ${dirout}flux_vector_pombb_${year}.nc -expr,'flx_nox=FLX_NOX' ${dirout}flux_vector_nox_${year}.nc -expr,'flx_bbnox=46.*FLX_BBNOX/30.;' ${dirout}flux_vector_noxbb_${year}.nc -expr,'flx_so2=FLX_SO2' ${dirout}flux_vector_so2_${year}.nc -expr,'flx_bbso2=FLX_BBSO2' ${dirout}flux_vector_so2bb_${year}.nc -expr,'flx_nh3=FLX_NH3' ${dirout}flux_vector_nh3_${year}.nc -expr,'flx_bbnh3=FLX_BBNH3' ${dirout}flux_vector_nh3bb_${year}.nc  ${dirout}flux_vector_h2s_${year}.nc ${dirout}flux_vector_so4_${year}.nc -selname,conc_dms ${fileINCAex} ${dirout}sflx_lmdz_cmip6_${year}.nc

ncrename -d VECTOR,vector -v VECTOR,vector ${dirout}sflx_lmdz_cmip6_${year}.nc
ncrename -d TIME,time     -v TIME,time     ${dirout}sflx_lmdz_cmip6_${year}.nc

#--cleaning up 
rm -f ${dirout}flux*_${year}.nc

#--end loop on years
done

#--cleaning the mess
rm -f ferret*
rm -f regrid.pro
