#! /bin/csh -f

echo $user `date`
echo $0
echo $cwd

# set echo on

set PERIOD = 3650
set WPWD   = CCSM
set PROJ   = 93300006

cp -p /fis/cgd/cseg/csm/inputdata/atm/datm7/CLMNCEP/reformat_scripts/time_*.nc .

foreach YEAR (2000 2001           )
   msrcp "mss:/TQIAN/LSMF/NCEPDATA/$YEAR-??.nc" .
   foreach MONTH (01 02 03 04 05 06 07 08 09 10 11 12)
      echo reformatting data for YEAR, MONTH = $YEAR,$MONTH
   
      set ORGFILE = $YEAR-$MONTH.nc
      set NEWFILE = clmncep.T62.$YEAR-$MONTH.nc

      #--- copy all existing data ---
      cp $ORGFILE $NEWFILE

      #--- add time coordinates ---
      if ( $MONTH == 02 ) then
         set TIMFILE = time_28.nc
      else if ( $MONTH == 04 || $MONTH == 06 || $MONTH == 09 || $MONTH == 11 ) then
         set TIMFILE = time_30.nc
      else
         set TIMFILE = time_31.nc
      endif
      ncks -h -A -v time   $TIMFILE  $NEWFILE   
      ncatted -h -O -a  units,time,o,c,"days since $YEAR-$MONTH-01 00:00:00"  $NEWFILE

      #--- add global attributes ---
      ncatted -h -O -a       title,global,o,c,"CLM-NCEP datm input data"            $NEWFILE
      ncatted -h -O -a conventions,global,o,c,"CF-1.0"                              $NEWFILE
      ncatted -h -O -a     history,global,a,c,"Original data from NCAR mss:/TQIAN/LSMF/NCEPDATA " $NEWFILE
      ncatted -h -O -a     history,global,a,c,"\n Time coords added by `whoami`, `date`"   $NEWFILE

   end
   #--- save to mss ---
   msrcp -period $PERIOD -wpwd $WPWD -proj $PROJ clmncep.T62.$YEAR-??.nc mss:/CSM/inputdata/atm/datm7/CLMNCEP &

   #--- done with original files ---
   sleep 20
   rm $YEAR-??.nc
end


