#!/bin/tcsh -f

set namein  = 'era_i_rain_1x1_corrected_1979-2016.nc'
set nameout ='era_i_rain_corrected_1x1_1979-2016.nc'

cp $namein dum.nc
echo $namein' file is copied into dummy file'

ncrename -d lon,longitude -d lat,latitude dum.nc
echo 'dimensios are renamed'
ncrename -v lon,longitude -v lat,latitude dum.nc
echo 'lon and lat are renamed'

ncatted -a modulo,longitude,c,d,360.0 -a axis,longitude,c,c,X -a axis,latitude,c,c,Y dum.nc
echo 'longitude and  latitude attributes are created'

ncap2 -s 'longitude=float(longitude)' -o dum1.nc dum.nc
rm -f dum.nc
echo 'longitude is converted to float'
ncap2 -s 'latitude=float(latitude)' -o dum.nc dum1.nc
rm -f dum1.nc
echo 'latitude is converted to float'

ncap2 -s 'time=float(time/1440.0+28854.0)' -o dum1.nc dum.nc
rm -f dum.nc
echo 'time converted to days from 1900-01-01'

ncatted -a calendar,time,c,c,'julian' -a units,time,m,c,'days since 1900-01-01 00:00:00' -o dum.nc dum1.nc
rm -f dum1.nc
echo 'time attributes are created'

ncatted -a units,corfpr,c,c,'kg/m2*sec' -a long_name,corfpr,m,c,'corrected liquid precipitation (rain)' dum.nc
mv dum.nc $nameout
echo 'rain attributes are greated'

