#!/bin/ksh

## Script permettant d'extraire depuis les moyennes mensuelles de inca (96x95x39)
#les donnees necessaires a lmdz pour les concentrations d'aerosols

##Attention les fichiers lev_file.nc et climato_1944_LOI_IPCC_HISTOR.nc sont disponibles sous svn 
# au meme emplacement que ce script. 
# les variables issues de  lev_file.nc  ne sont valables que pour la grille 96x95x39

set -ex

##############################################
## repertoire de travail sur le scratchdir
##############################################

DIRWORK=$SCRATCHDIR/tmp
mkdir -p ${DIRWORK}

cd ${DIRWORK}
\rm -f *
cp /ccc/scratch/cont003/dsm/p86cozic/CLIMATO/FILES/lev_file.nc exemple.nc
cp  /ccc/scratch/cont003/dsm/p86cozic/CLIMATO/climato_1944_LOI_IPCC_HISTOR.nc exemple2.nc


## config nom, path de l'experience
##############################################
EXPER=ESM.2xCO2
TYPE=2fCO2

DIREXP=$STOREDIR/IGCM_OUT/IPSLCM5CHT/PROD/${TYPE}/${EXPER}/CHM/Analyse/MM

mkdir -p  $STOREDIR/IGCM_OUT/IPSLCM5CHT/PROD/${TYPE}/${EXPER}/CHM/Analyse/CLIMATO

for ANNEE in   1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005

do

    FICHIER=$DIREXP/${EXPER}_${ANNEE}_allMM_inca_avgr.nc

## extraction des especes
#############################################
    cp $FICHIER tmp.nc
    ncks -v lat,lon,ps,pmid,temp,AIPOMM,ASBCM,AIBCM,ASPOMM,CSSSM,ASSSM,CIDUSTM,ASSO4M,LOAD_ASSO4M,CSSO4M,LOAD_CSSO4M,SSSSM,LOAD_SSSSM,LOAD_AIPOMM,LOAD_ASBCM,LOAD_AIBCM,LOAD_ASPOMM,LOAD_CSSSM,LOAD_ASSSM,LOAD_CIDUSTM tmp.nc tmp1.nc


    ncap -O -s "zrho=pmid/(temp*287.04);AIPOMM=AIPOMM*zrho*1e9;ASBCM=ASBCM*zrho*1e9;AIBCM=AIBCM*zrho*1e9;ASPOMM=ASPOMM*zrho*1e9;CSSSM=CSSSM*zrho*1e9;ASSSM=ASSSM*zrho*1e9;CIDUSTM=CIDUSTM*zrho*1e9;ASSO4M=ASSO4M*zrho*1e9;SSSSM=SSSSM*zrho*1e9;CSSO4M=CSSO4M*zrho*1e9" tmp1.nc tmp1.nc

    ncpdq -a -presnivs tmp1.nc tmp1bis.nc 
    ncpdq -O -a -lat tmp1bis.nc tmp2.nc 

    ncap -s "AIPOMM=float(AIPOMM);ASBCM=float(ASBCM);AIBCM=float(AIBCM);ASPOMM=float(ASPOMM);CSSSM=float(CSSSM);ASSSM=float(ASSSM);CIDUSTM=float(CIDUSTM);ASSO4M=float(ASSO4M);SSSSM=float(SSSSM);CSSO4M=float(CSSO4M);lat=double(lat);lon=double(lon)" tmp2.nc tmp3.nc 
    
    ncatted -O -a units,AIBCM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,AIPOMM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,ASBCM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,CSSSM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,ASPOMM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,CIDUSTM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,ASSSM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,SSSSM,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,CSSO4M,o,c,"ug/m3" tmp3.nc tmp3.nc
    ncatted -O -a units,ASSO4M,o,c,"ug/m3" tmp3.nc tmp3.nc
    
    ncrename -O -v LOAD_ASSO4M,load_SO4  tmp3.nc tmp3.nc
    ncrename -O -v ASSO4M,SO4  tmp3.nc tmp3.nc
    ncrename -O -v LOAD_CSSO4M,load_CSSO4M  tmp3.nc tmp3.nc  
    ncrename -O -v LOAD_SSSSM,load_SSSSM  tmp3.nc tmp3.nc   
    ncrename -O -v LOAD_AIPOMM,load_AIPOMM  tmp3.nc tmp3.nc  
    ncrename -O -v LOAD_ASBCM,load_ASBCM  tmp3.nc tmp3.nc   
    ncrename -O -v LOAD_AIBCM,load_AIBCM  tmp3.nc tmp3.nc   
    ncrename -O -v LOAD_ASPOMM,load_ASPOMM  tmp3.nc tmp3.nc  
    ncrename -O -v LOAD_CSSSM,load_CSSSM  tmp3.nc tmp3.nc   
    ncrename -O -v LOAD_ASSSM,load_ASSSM  tmp3.nc tmp3.nc   
    ncrename -O -v LOAD_CIDUSTM,load_CIDUSTM  tmp3.nc tmp3.nc
    ncrename -O -d time_counter,TIME  tmp3.nc tmp3.nc
    ncrename -O -d presnivs,lev  tmp3.nc tmp3.nc
    
    ncks -A -v lev,b,ap,lev_bnds exemple.nc tmp3.nc
    ncks -A -v p0 exemple2.nc tmp3.nc
    
    mv tmp3.nc $STOREDIR/IGCM_OUT/IPSLCM5CHT/PROD/${TYPE}/${EXPER}/CHM/Analyse/CLIMATO/climato_${ANNEE}_${EXPER}.nc
    \rm  tmp*.nc

done

