#! /bin/csh -f

set echo on
cd PRECIP

foreach YEAR (2000 2001 2002 2003 2004)
#oreach YEAR (1996 )
   echo reformatting data for YEAR = $YEAR

   set DOMFILE = /fis/cgd/cseg/csm/inputdata/atm/datm7/TN460/reformat_scripts/multi_year/domain.T62.nc
   set TIMFILE = /fis/cgd/cseg/csm/inputdata/atm/datm7/TN460/reformat_scripts/multi_year/time_prec.nc
   set ORGFILE = orig/prec.$YEAR.nc
   set NEWFILE = gxgxs.prec.T62.$YEAR.nc

   #--- copy fields
   ncks -h -O -v lat,lon           $DOMFILE     $NEWFILE
   ncks -h -A -v time              $TIMFILE     $NEWFILE
   ncks -h -A -v date,datesec,prc  $ORGFILE     $NEWFILE # clobbers time,lat,lon
   ncks -h -A -v time              $TIMFILE     $NEWFILE   

   #--- fix time coordinates ---
   ncatted -h -O -a      units,time,o,c,"days since $YEAR-01-01 00:00:00"         $NEWFILE

   #--- fix domain data ---
   # make separate domain file

   #--- fix prc variable attributes ---
   ncatted -h -O -a     short_name,prc,d,c,                                                   $NEWFILE
   ncatted -h -O -a  missing_value,prc,d,f,                                                   $NEWFILE
   ncatted -h -O -a          blend,prc,c,c,"G to 60S; X to 35S; G to 35N; X to 68N; S to 90N" $NEWFILE
   ncatted -h -O -a           data,prc,c,c,"GPCP (G), XieArkin (X), Serreze (S)"              $NEWFILE

   #--- fix global attributes ---
   ncatted -h -O -a       blend,global,d,c,                                       $NEWFILE
   ncatted -h -O -a        data,global,d,c,                                       $NEWFILE
   ncatted -h -O -a conventions,global,o,c,"CF-1.0"                               $NEWFILE
   ncatted -h -O -a     history,global,a,c," \nReformatted by `whoami`, `date`"   $NEWFILE

end
