[5617] | 1 | #!/bin/ksh |
---|
| 2 | # X. Wang 2020 |
---|
| 3 | # |
---|
| 4 | # FUNCTIONALITY: |
---|
| 5 | # to prepare the forcing files for running the new driver. |
---|
| 6 | # |
---|
| 7 | # HOW: |
---|
| 8 | # 1) to create two new nc files for firstyear-1 and lastyear+1 respectively, given a forcing dataset |
---|
| 9 | # 2) to create softlink to other original forcing files. |
---|
| 10 | # 3) the resulting files are saved in the user-defined output path |
---|
| 11 | # |
---|
| 12 | # |
---|
| 13 | # USAGE: |
---|
| 14 | # ./AddScript inputpath outputpath |
---|
| 15 | # where the input path for looking for the forcing data ; |
---|
| 16 | # the desired output path to store the newly created files |
---|
| 17 | # |
---|
| 18 | # ./AddYears /projsu/igcmg/IGCM/SRF/METEO/CRU-NCEP/v8/twodeg/ user_desired_path |
---|
| 19 | # |
---|
| 20 | # Information: |
---|
| 21 | # The forcing files are saved in the default IGCM path: |
---|
| 22 | # CICLAD: /projsu/igcmg/IGCM/ |
---|
| 23 | # IRENE: /ccc/work/cont003/igcmg/igcmg/IGCM |
---|
| 24 | # jean zay: /gpfswork/rech/psl/commun/IGCM |
---|
| 25 | # obelix: /home/orchideeshare/igcmg/IGCM |
---|
| 26 | # |
---|
| 27 | # At this path, choose the interested forcing directory |
---|
| 28 | # For example adding SRF/METEO/CRUJRA/v2.1/twodeg/ |
---|
| 29 | # The final input=/projsu/igcmg/IGCM/SRF/METEO/CRUJRA/v2.1/twodeg |
---|
| 30 | # |
---|
| 31 | # This script is modified and generalized based on a script of Jan Polcher. |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | #-------------------------------------------------------------------- |
---|
| 35 | |
---|
| 36 | input=$1 |
---|
| 37 | output=$2 |
---|
| 38 | |
---|
| 39 | [ -d output ] || mkdir -p $output |
---|
| 40 | |
---|
| 41 | ############################################################################# |
---|
| 42 | # get the existing nc files of first and last year in the current directory |
---|
| 43 | firstyear_nc=`ls $input/*nc | xargs -n 1 basename | head -n 1 ` |
---|
| 44 | lastyear_nc=`ls $input/*nc | xargs -n 1 basename | tail -n 1 ` |
---|
| 45 | |
---|
| 46 | # find the first year and last year |
---|
| 47 | if [[ $input == *"CRU-NCEP"* || $input == *"WFDEI"* ]] |
---|
| 48 | then |
---|
| 49 | firstyear=`ls $input/*nc | xargs -n 1 basename | head -n 1 | awk '{split($0,a,"_");print a[3]}' | awk -F'.' '{print $1}' ` |
---|
| 50 | lastyear=`ls $input/*nc | xargs -n 1 basename | tail -n 1 | awk '{split($0,a,"_");print a[3]}' | awk -F'.' '{print $1}' ` |
---|
| 51 | else |
---|
| 52 | firstyear=`ls $input/*nc | xargs -n 1 basename | head -n 1 | awk '{split($0,a,"_");print a[4]}' | awk -F'.' '{print $1}' ` |
---|
| 53 | lastyear=`ls $input/*nc | xargs -n 1 basename | tail -n 1 | awk '{split($0,a,"_");print a[4]}' | awk -F'.' '{print $1}' ` |
---|
| 54 | fi |
---|
| 55 | |
---|
| 56 | # to test if the new extra file already exist or not |
---|
| 57 | firstyearsize=`ls -l $input/*$firstyear*nc | awk '{print $5}'` |
---|
| 58 | lastyearsize=`ls -l $input/*$lastyear*nc | awk '{print $5}'` |
---|
| 59 | |
---|
| 60 | testyear=$((firstyear + 1)) |
---|
| 61 | testyearsize=`ls -l $input/*$testyear*nc | awk '{print $5}'` |
---|
| 62 | |
---|
| 63 | firstyear_sizeratio=`echo "scale=2 ; $firstyearsize / $testyearsize" | bc` |
---|
| 64 | lastyear_sizeratio=`echo "scale=2 ; $lastyearsize / $testyearsize" | bc` |
---|
| 65 | |
---|
| 66 | if [[ $firstyear_sizeratio < 0.1 ]];then |
---|
| 67 | echo "Maybe the first new forcing file is already created and need not redo." |
---|
| 68 | fi |
---|
| 69 | |
---|
| 70 | if [[ $lastyear_sizeratio < 0.1 ]];then |
---|
| 71 | echo "Maybe the last new forcing file is already created and need not redo. Exit" |
---|
| 72 | exit 1 |
---|
| 73 | fi |
---|
| 74 | |
---|
| 75 | # prepare to create the new forcing files for firstyear-1 and lastyear+1 |
---|
| 76 | firstyear_less1=$((firstyear - 1)) |
---|
| 77 | lastyear_more1=$((lastyear + 1)) |
---|
| 78 | |
---|
| 79 | substr=`ls $input/*nc | xargs -n 1 basename | head -n 1 | sed s/"_$firstyear.nc"//` |
---|
| 80 | |
---|
| 81 | new_firstyear_nc=$substr"_$firstyear_less1.nc" |
---|
| 82 | new_lastyear_nc=$substr"_$lastyear_more1.nc" |
---|
| 83 | |
---|
| 84 | timeunit=`ncdump -h $input$firstyear_nc | grep units | grep seconds | awk '{split($0,a,"=");print a[2]}'` |
---|
| 85 | # |
---|
| 86 | # create the new first year file by using the last 12 hours of $firstyear_nc |
---|
| 87 | # |
---|
| 88 | if [[ ! -e $input$new_firstyear_nc && $firstyear_sizeratio > 0.1 ]] ; then |
---|
| 89 | tlen=`ncdump -h $input$firstyear_nc | grep currently | cut -d "(" -f 2 | cut -d " " -f 1` |
---|
| 90 | tst=$(( $tlen - 12 )) |
---|
| 91 | if [[ "$firstyear_nc" == *"CRU"* || "$firstyear_nc" == *"cru"* ]] |
---|
| 92 | then |
---|
| 93 | ncks -h -F -d tstep,$tst,$tlen $input$firstyear_nc $output/$new_firstyear_nc |
---|
| 94 | ncatted -h -a units,timeplussix,o,c,"seconds since $firstyear_less1-01-01 00:00:00" $output/$new_firstyear_nc |
---|
| 95 | else |
---|
| 96 | ncks -h -F -d time,$tst,$tlen $input$firstyear_nc $output/$new_firstyear_nc |
---|
| 97 | fi |
---|
| 98 | ncatted -h -a units,time,o,c,"seconds since $firstyear_less1-01-01 00:00:00" $output/$new_firstyear_nc |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | fi |
---|
| 102 | # |
---|
| 103 | # |
---|
| 104 | # Create the new last year file by using the first 12 hours of $lastyear_nc |
---|
| 105 | # |
---|
| 106 | if [[ ! -e $input$new_lastyear_nc && $firstyear_sizeratio > 0.1 ]] ; then |
---|
| 107 | if [[ "$lastyear_nc" == *"CRU"* || "$lastyear_nc" == *"cru"* ]] |
---|
| 108 | then |
---|
| 109 | ncks -h -F -d tstep,1,12 $input$lastyear_nc $output/$new_lastyear_nc |
---|
| 110 | ncatted -h -a units,timeplussix,o,c,"seconds since $lastyear_more1-01-01 00:00:00" $output/$new_lastyear_nc |
---|
| 111 | else |
---|
| 112 | ncks -h -F -d time,1,12 $input$lastyear_nc $output/$new_lastyear_nc |
---|
| 113 | fi |
---|
| 114 | ncatted -h -a units,time,o,c,"seconds since $lastyear_more1-01-01 00:00:00" $output/$new_lastyear_nc |
---|
| 115 | fi |
---|
| 116 | |
---|
| 117 | # 2) create links to all other files, if the output dir is different from the input dir. |
---|
| 118 | ls $output |
---|
| 119 | |
---|
| 120 | if [ "$input" != "$output" ]; then |
---|
| 121 | for i in `ls $input/*nc` |
---|
| 122 | do |
---|
| 123 | newname=`basename $i` |
---|
| 124 | if [ ! -f "$output/$newname" ];then |
---|
| 125 | ln -s $i $output/$newname |
---|
| 126 | fi |
---|
| 127 | done |
---|
| 128 | fi |
---|