#!/bin/csh -f
set echo verbose
set DIAG_HOME = /fs/cgd/data0/stevens/DIAG
set dirpath =  $proj/diag/b31.002/proc
#set dirpath =  $proj/diag/cam3_3_fv_1.9x2.5/proc
#set dirpath =  /datalocal/ccpc/rneale/cam3_3_fv_1.9x2.5/proc
setenv CASENAME b31.002
setenv RGB $DIAG_HOME/rgb
setenv firstyr 1979
setenv lastyr  2000
setenv WKDIR `pwd`
set casepath = $dirpath/$CASENAME


ls {$casepath}.cam?.h0.{$firstyr}-??.nc > $WKDIR/$CASENAME.monthly



@ yr = $firstyr + 1
while ($yr <= $lastyr)
  set year = `printf "%04d" $yr`
  ls {$casepath}.cam?.h0.{$year}-??.nc >> $WKDIR/$CASENAME.monthly
  @ yr++
end

# Jan/Feb in this year, Dec in year-1 

set nino_years = (1983 1987 1992 1995 1998) 
set nina_years = (1985 1989 1999 2000) 


#b31.002
if ($CASENAME == b31.002) then
    set nino_years = (0026 0032 0035 0044 0047 0049 0053 0061 0067 0080 0084 0091 0096)
    set nina_years = switch( q )
	case (0021 0024 0033 0039 0043 0048 0051 0055 0064 0068 0081 0085 0093)
endif

#b31.001 
if ($CASENAME == b31.001) then
    set nino_years = (0022 0025 0030 0032 0037 0044 0048 0054 0056 0058 0062 0064 0068 0080 0082 0086)
    set nina_years = (0021 0024 0029 0033 0038 0043 0047 0053 0057 0059 0061 0063 0067 0075 0079 0081 0085)
endif

if (! -e $WKDIR/$CASENAME.WARM-COLD.nc) then

foreach year ( $nino_years )
    if ($year > $firstyr && $year <= $lastyr) then
	@ year--
	set syear = `printf "%04d" $year`  #
	ls {$casepath}.cam?.h0.{$syear}-{12}.nc >> $WKDIR/$CASENAME.nino
	@ year++
	set syear = `printf "%04d" $year`  # 
	ls {$casepath}.cam?.h0.{$syear}-{01,02}.nc >> $WKDIR/$CASENAME.nino    
    endif
end

foreach year ( $nina_years )
    if ($year > $firstyr && $year <= $lastyr) then
	@ year--
	set syear = `printf "%04d" $year`  #
	ls {$casepath}.cam?.h0.{$syear}-{12}.nc >> $WKDIR/$CASENAME.nina
	@ year++
	set syear = `printf "%04d" $year`  # 
	ls {$casepath}.cam?.h0.{$syear}-{01,02}.nc >> $WKDIR/$CASENAME.nina    
    endif
end

set nino_files = `paste $WKDIR/$CASENAME.nino`
set nina_files = `paste $WKDIR/$CASENAME.nina`

ncea $nino_files $WKDIR/$CASENAME.nino.nc
ncea $nina_files $WKDIR/$CASENAME.nina.nc

# Don't want to diff LANDFRAC
ncdiff -x -v LANDFRAC $WKDIR/$CASENAME.nino.nc $WKDIR/$CASENAME.nina.nc $WKDIR/$CASENAME.WARM-COLD.nc
ncea $WKDIR/$CASENAME.nino.nc $WKDIR/$CASENAME.nina.nc $WKDIR/$CASENAME.WARM+COLD.nc

/bin/rm $WKDIR/$CASENAME.nin*

endif

echo ENSO WARM MINUS COLD PLOTS
ncl < wmc_test.ncl




exit 0
