Subversion Repositories lagranto.ocean

Rev

Blame | Last modification | View Log | Download | RSS feed

#!/bin/bash
#----------------------------------------------------------

# --- input/output dir
idir="/atmosdyn/schemms/oras5/raw/cdf"
odir="/atmosdyn/schemms/oras5/cdf.grid"
pdir="/home/schemms/lagranto.ocean/ora.preprocess"
cd $idir


# --- create target grid
cat > targetgrid << endfile
gridtype=lonlat
xsize=1441
ysize=721
xfirst=-180.0
xinc=0.25
yfirst=-90.0
yinc=0.25
endfile

#----------------------------------------------------------
# Loop over all files
#----------------------------------------------------------

for f in *"_5d_"*"T_01.nc"
do

    # ----- extract the date -------------
    # -- take final date of 5-day average
    date=${f:17:8}

    # ----- extract the base -------------
    base=${f:0:31}

    echo "Working on: " $base "Date: " $date

    # ----- prepare file names -----------
    fu=${base}"U_01.nc"
    fv=${base}"V_01.nc"
    ft=${base}"T_01.nc"
    fw=${base}"W_01.nc"

    # ----- prepare file -----------------
    \cp ${pdir}/transform.unstagger.oras5.3d.py run.py

    sed -i 's/INFILE_U/'${fu}'/g' run.py
    sed -i 's/INFILE_V/'${fv}'/g' run.py
    sed -i 's/INFILE_W/'${fw}'/g' run.py
    sed -i 's/INFILE_T/'${ft}'/g' run.py


    # ----- Destaggering/Rotation U/V-----
    python run.py
    
    # --- set missing data
    cdo setmissval,-999.99 tmp.nc tmp
    
    # ----- no netcdf4 support in Bergen
        #cdo -b f32 -f nc -copy tmp tmp2
    #\mv tmp2 tmp
    
    # ----- set land to mdv
    ncap2 -O -h -s 'where(S==0) U=-999.99' tmp tmp
    ncap2 -O -h -s 'where(S==0) V=-999.99' tmp tmp
    ncap2 -O -h -s 'where(S==0) W=-999.99' tmp tmp
    ncap2 -O -h -s 'where(S==0) T=-999.99' tmp tmp
    ncap2 -O -h -s 'where(S==0) SIGMAT=-999.99' tmp tmp
    ncap2 -O -h -s 'where(S==0) S=-999.99' tmp tmp

    # ----- Interpolate to regular grid
    cdo remapbil,targetgrid tmp remaptmp.nc
        \mv tmp remaptmp.nc

        \rm tmp.nc tmp

    # ----- convert lat/lon/time to float
    ncap2 -O -h -s 'lat=float(lat)' remaptmp.nc remaptmp.nc
    ncap2 -O -h -s 'lon=float(lon)' remaptmp.nc remaptmp.nc
    ncap2 -O -h -s 'time=float(time)' remaptmp.nc remaptmp.nc

    # ----- correct time axis
    yr=$(echo $date | cut -c1-4)
    mn=$(echo $date | cut -c5-6)
    dd=$(echo $date | cut -c7-8)
    hh="00"

    dat=${yr}'-'${mn}'-'${dd}
    time=${hh}':00'

    diff=$(( ($(date -u --date=${dat}' '${time} +%s) - $(date -u --date="1979-01-01 00:00" +%s) )/(60*60) ))

    # --- create new and updated time variable
    g=remaptmp.nc
    ncap2 -h -s "time[time]=${diff}.f" ${g} ${g} --ovr
    ncatted -h -O -a standard_name,time,o,c,"time" ${g}
    ncatted -h -O -a long_name,time,o,c,"time" ${g}
    ncatted -h -O -a units,time,o,c,"hours since 1979-01-01 00:00" ${g}
    ncatted -h -O -a calendar,time,o,c,"gregorian" ${g}

    # ---- set constants file
    ncatted -h -O -a constants_file_name,global,c,c,"P${date}_00" ${g}
    ncatted -h -O -a history,global,d,,'' ${g}
    ncatted -h -O -a CDI,global,d,,'' ${g}
    ncatted -h -O -a CDO,global,d,,'' ${g}


    # ----- move output ------------------
    echo "Created " "P"${date}"_00"
    \mv remaptmp.nc ${odir}/"P"${date}"_00"
    \rm run.py *tmp*
    #\rm ./-h.pid*.ncap2.tmp tmp tmp2 $f


done

\rm targetgrid*


#----------------------------------------------------------
exit 0
#----------------------------------------------------------