source: TOOLS/AddYears

Last change on this file was 5617, checked in by acosce, 3 years ago

New script for Orchidee users
1) Suppose the forcing data covers 1860-2012, this script creates two new nc files for 1859 and 2013 respectively, and save them to the user-defined output path
2) for the original forcing data files, this script creates soft link to them, at the user-defined output path.

The users can use this script to prepare forcing files if they need other products, when using the new driver.
This script has been tested with CRUJRA (two deg and half deg of of recent version), WFDEI, CRU-NCEP (two deg and one deg).

(Xiaoni Wang-Faivre)

File size: 4.8 KB
Line 
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
36input=$1
37output=$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
43firstyear_nc=`ls $input/*nc | xargs -n 1 basename | head -n 1 `
44lastyear_nc=`ls $input/*nc | xargs -n 1 basename | tail -n 1 `
45
46# find the first year and last year
47if [[ $input == *"CRU-NCEP"* || $input == *"WFDEI"* ]] 
48then
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}' `
51else
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}' `
54fi
55
56# to test if the new extra file already exist or not
57firstyearsize=`ls -l  $input/*$firstyear*nc | awk '{print $5}'`
58lastyearsize=`ls -l  $input/*$lastyear*nc | awk '{print $5}'`
59
60testyear=$((firstyear + 1))
61testyearsize=`ls -l  $input/*$testyear*nc | awk '{print $5}'`
62
63firstyear_sizeratio=`echo "scale=2 ; $firstyearsize / $testyearsize" | bc`
64lastyear_sizeratio=`echo "scale=2 ; $lastyearsize / $testyearsize" | bc`
65
66if [[ $firstyear_sizeratio  < 0.1 ]];then
67    echo "Maybe the first new forcing file is already created and need not redo."
68fi
69
70if [[ $lastyear_sizeratio  < 0.1 ]];then
71    echo "Maybe the last new forcing file is already created and need not redo. Exit"
72    exit 1
73fi
74
75# prepare to create the new forcing files for firstyear-1 and lastyear+1
76firstyear_less1=$((firstyear - 1))
77lastyear_more1=$((lastyear + 1))
78
79substr=`ls $input/*nc | xargs -n 1 basename | head -n 1 | sed s/"_$firstyear.nc"//`
80
81new_firstyear_nc=$substr"_$firstyear_less1.nc"
82new_lastyear_nc=$substr"_$lastyear_more1.nc"
83
84timeunit=`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#
88if [[ ! -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   
101fi
102#
103#
104# Create the new last year file by using the first 12 hours of $lastyear_nc
105#
106if [[ ! -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
115fi
116
117# 2) create links to all other files, if the output dir is different from the input dir.
118ls $output
119
120if [ "$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
128fi
Note: See TracBrowser for help on using the repository browser.