source: TOOLS/INTERP_NUDGE/interp_from_era.ksh @ 3097

Last change on this file since 3097 was 3097, checked in by jgipsl, 8 years ago

Added case to create yearly files

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1#!/bin/ksh
2set verbose echo
3#
4# This script will interpolate horizontaly ERA-I
5# output to the desired model grid
6#
7# Contact Josefine Ghattas
8###############################################################################
9###############################################################################
10###############################################################################
11# BEGIN USER DEFINITION
12###############################################################################
13
14# gridfile : file grilles_gcm.nc containing longitudes and latitudes of the destination grid
15gridfile=/ccc/store/cont003/dsm/p86ghatt/IGCM_OUT/LMDZ/ELI-144x142x79/ATM/Output/Grid/ELI-144x142x79_1979_grilles_gcm.nc
16
17# indir : path for directory containing the files to interpolate
18#indir=/u/rech/psl/rpsl376/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_PL
19indir=/ccc/work/cont003/subipsl/subipsl/ERAI/NETCDF/GLOBAL_075/4xdaily/AN_PL/
20# insuffix : suffix in input files
21insuffix=.aphei.GLOBAL_075.nc
22
23# varlist : list of variables to interpolate
24varlist="u v ta r"
25
26# Variables for output :
27# outdir : directory to store output files
28outdir=$WORKDIR/NUDGEFILES/ERAI-144x142x79
29# outsuffix : suffix to output files
30outsuffix=_erai.nc
31
32# Time variables :
33# first_year : first year to interpolate
34first_year=1979
35# last_year : last year to interpolate
36last_year=2016
37
38# Frequency of output files : yearly or monthly
39# OutFreq='monthly' or OutFrequ='yearly'
40OutFreq='yearly'
41 
42# rundir : temporary run directory
43rundir=`pwd`/rundir
44echo rundir=$rundir
45
46###############################################################################
47# END USER DEFINITION
48###############################################################################
49###############################################################################
50###############################################################################
51if [ ! -d ${rundir} ] ; then
52    mkdir -p ${rundir}
53fi
54cp -f era2gcm.ksh $rundir/.
55cd $rundir
56
57###############################################################################
58# For all variables do :
59#   1) Interpolate all month and years for each variable
60#   2) Add a time step last in each file coming from next month
61###############################################################################
62# Loop over all variables
63for var in $varlist ; do
64    year=$first_year
65    month=01
66
67    # 1 - Loop over all files and interpolate the variable
68    while [[ ${year} -le ${last_year} ]] ; do
69        # Create output directory if it does not exist
70        if [ ! -d $outdir/${year} ] ; then
71            mkdir -p $outdir/${year}
72        fi
73
74        # Define source and target file
75        filein=${indir}/${year}/${var}.${year}${month}${insuffix}
76        fileout=${outdir}/${year}/${var}_${year}${month}${outsuffix}
77
78        # Interpolate filein and save in fileout
79        echo Interpolate file $filein and save in file $fileout
80        ./era2gcm.ksh $gridfile $filein tmp.nc $var
81        mv tmp.nc $fileout
82        echo
83
84
85        # Find next month and year
86        case $month in
87            12)
88                nextyear=`expr $year + 1`
89                nextmonth=01
90                ;;
91            *)
92                nextyear=$year
93                nextmonth=`expr $month + 1`
94                if [[ $nextmonth -le 9 ]] ; then
95                    nextmonth=0$nextmonth
96                fi
97                ;;
98        esac
99        # Update month and year
100        year=$nextyear
101        month=$nextmonth
102    done
103
104
105    # Do different treatement if monthly or yearly output files are wanted
106    if [[ $OutFreq = 'monthly' ]] ; then
107    # 2 - Loop over all files and add a time step :
108    #     take the first time step from next coming month and add last in current month
109    echo Before add step
110
111    year=$first_year
112    month=01
113    while [[ ${year} -le ${last_year} ]] ; do
114        # Find next month and year
115        case $month in
116            12)
117                nextyear=`expr $year + 1`
118                nextmonth=01
119                ;;
120            *)
121                nextyear=$year
122                nextmonth=`expr $month + 1`
123                if [[ $nextmonth -le 9 ]] ; then
124                    nextmonth=0$nextmonth
125                fi
126                ;;
127        esac
128       
129        file=${outdir}/${year}/${var}_${year}${month}${outsuffix}
130        nextfile=${outdir}/${year}/${var}_${nextyear}${nextmonth}${outsuffix}
131        if [ -f ${nextfile} ] ; then
132            echo First time step from file ${nextfile} will be added last in file ${file}
133            cp -f $file file.nc
134            cp -f $nextfile next.nc
135            ncks -O -d TIME,0,0 next.nc tmp.nc
136            ncrcat -O file.nc tmp.nc tmptot.nc
137            mv tmptot.nc $file
138        else
139            echo Next file ${nextfile} does not exist. Nothing will be done for ${file}
140        fi
141
142        # Update month and year
143        year=$nextyear
144        month=$nextmonth
145    done
146
147    elif [[ $OutFreq = 'yearly' ]] ; then
148        # 3a. Concate the monthly files into yearly files
149        mkdir -p  ${outdir}/allyears
150       
151        year=$first_year
152        while [[ ${year} -le ${last_year} ]] ; do
153        echo Now merge year=$year for variable $var
154            cdo mergetime ${outdir}/${year}/${var}_${year}* ${outdir}/allyears/${var}_${year}${outsuffix}
155            year=`expr $year + 1`
156        done
157
158        # 3b.Add a last timestep in the end of the yearly files from next coming year
159            year=$first_year
160            while [[ ${year} -le ${last_year} ]] ; do
161              # Find next month and year
162              nextyear=`expr $year + 1`
163               
164              file=${outdir}/allyears/${var}_${year}${outsuffix}
165              nextfile=${outdir}/allyears/${var}_${nextyear}${outsuffix}
166              if [ -f ${nextfile} ] ; then
167                 echo First time step from file ${nextfile} will be added last in file ${file}
168                 cp -f $file file.nc
169                 cp -f $nextfile next.nc
170                 ncks -O -d TIME,0,0 next.nc tmp.nc
171                 ncrcat -O file.nc tmp.nc tmptot.nc
172                 mv tmptot.nc $file
173              else
174                 echo Next file ${nextfile} does not exist. Nothing will be done for ${file}
175              fi
176
177              # Update month and year
178              year=$nextyear
179          done
180    fi
181   
182# End of loop variable list
183done
184
185### Copy gridfile in output dir
186cp -f $gridfile $outdir/grilles_gcm.nc
187   
188exit
Note: See TracBrowser for help on using the repository browser.