#! /bin/csh -f

set echo on

cd RADFLX

foreach YEAR (2000 2001 2002 2003 2004)
#oreach YEAR (2000 )
   echo working on year = $YEAR =============================================================
foreach VAR  (lwdn swdn swup)
#oreach VAR  (lwdn          )
   echo working on variable = $VAR ---------------------------------------------------------

   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_radflx.nc 
   set ORGFILE = orig/$VAR.$YEAR.nc
   set NEWFILE = giss.$VAR.T62.$YEAR.nc

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

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

   echo --- fix domain data ---
   # make separate domain file
  
   echo --- fix variable attributes ---
   ncatted -h -O -a  missing_value,$VAR,d,f,                $NEWFILE
   ncatted -h -O -a          units,$VAR,o,c,"W/m2"          $NEWFILE
   ncatted -h -O -a  sampling_time,$VAR,o,c,"daily average" $NEWFILE

   echo --- fix global attributes ---
   ncatted -h -O -a        grid,global,d,c,                                       $NEWFILE
   ncatted -h -O -a       dates,global,d,c,                                       $NEWFILE
   ncatted -h -O -a conventions,global,d,c,                                       $NEWFILE

   ncatted -h -O -a       title,global,o,c,"GISS daily mean radiation time series"        $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
end
