#! /bin/csh -f

set echo on

foreach YEAR (2000 2001 2002 2003 2004)
#oreach YEAR (1996 )
   echo working on year = $YEAR =============================================================
foreach VAR  ( dn10 q_10 slp_ sst_ t_10 u_10 v_10 )
#oreach VAR  ( dn10                               )
   echo working on variable = $VAR ---------------------------------------------------------

   set DOMFILE = /fis/cgd/cseg/csm/inputdata/dx7/tn460/reformat_scripts/multi_year/domain.T62.nc
   set TIMFILE = /fis/cgd/cseg/csm/inputdata/dx7/tn460/reformat_scripts/multi_year/time_states.nc 
   set ORGFILE = orig/$VAR.$YEAR.nc
   set NEWFILE = ncep.$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
   ncks -h -A -v time               $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          note,$VAR,c,c,"NCEP 6 hour forecast data"            $NEWFILE

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

   ncatted -h -O -a       title,global,o,c,"NCEP 6 hour forecast 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
