#!/bin/csh -f

unset echo verbose

#*****************************************************************
# Get the file from the MSS if they don't exist locally
#*****************************************************************

# This file reads in files from HPSS
# $path_HPSS      The HPSS location for history files
# $path_history     The local directory for  history files
# $path_diag       The local directory for diag files
# $first_year     The first year of data
# $nyrs           The number of years of data 
# $casename      The casename  
# $compute_climo   # compute_climo is (0=ON,1=OFF) 


if ($#argv != 10) then
  echo "Usage: determine_output_attributes.csh: needs 10 arguments "
  exit
endif

set casetype = $1  # test or control
set casename = $2
set path_HPSS = $3
set path_history  = $4
set path_climo = $5
set path_diag  = $6
@ first_year = $7
set compute_climo = $8
set file_type = $9
set ts_filename = $10

##
## Pick a file for which we will do check
##
set rootname = ' ' 
if ( $compute_climo == 0) then
  if ($file_type == monthly_history) then
    set rootname     = ${casename}.cam.h0.    # new terminology
    set filename     = ${rootname}`printf "%04d" ${first_year}`-01.nc
    set filename_tar = ${rootname}`printf "%04d" ${first_year}`.tar   
    set fullpath_filename  = ${path_history}/${filename} 
  else
    set rootname     = ${casename}.cam2.h0.    # new terminology
    set filename     = $ts_filename 
    set fullpath_filename  = ${filename}
    set filename_tar = ${rootname}`printf "%04d" ${first_year}`.tar
  endif
else    
    set filename           = ${casename}_01_climo.nc
    set fullpath_filename  = ${path_climo}/${filename} 
endif

##
## Check file is present locally - otherwise try to get it from HPSS   
##

if (! -e ${fullpath_filename} || -z ${fullpath_filename} ) then
    hsi -q ls ${path_HPSS}${filename}     # Check whether history files are stored by month or year
    if(${status} == 0) then
	echo 'GETTING '${path_HPSS}${filename}''     
	hsi "get '${path_history}${filename}': '${path_HPSS}${filename}'"
    else 
        hsi -q ls ${path_HPSS}${filename_tar}    
	if(${status} == 0) then
	    echo 'GETTING '${path_HPSS}${filename_tar}
	    hsi "get '${path_history}/${filename_tar}': '${path_HPSS}${filename_tar}'"
	    tar -xvf ${path_history}/${filename_tar} -C ${path_history} 
	    rm -f ${path_history}/${filename_tar}
	endif     
    endif

endif

##
## If we didn't find any file with the filename, we assume it is an old file name
##

if (! -e ${fullpath_filename} || -z ${fullpath_filename} ) then
   
    set rootname     = ${casename}.cam2.h0.    # old terminology
    set filename     = ${rootname}`printf "%04d" ${first_year}`-01.nc
    set filename_tar = ${rootname}`printf "%04d" ${first_year}`.tar   
    set fullpath_filename  = ${path_history}/${filename} 
   
    if (! -e ${fullpath_filename} || -z ${fullpath_filename} ) then
	hsi -q ls ${path_HPSS}${filename}     # Check whether history files are stored by month or year
	if(${status} == 0) then
	    echo 'GETTING '${path_HPSS}${filename}''     
	    hsi "get '${path_history}${filename}': '${path_HPSS}${filename}'"
	else 
	    hsi -q ls ${path_HPSS}${filename_tar}    
	    if(${status} == 0) then	    
		echo 'GETTING '${path_HPSS}${filename_tar}
		hsi "get '${path_history}/${filename_tar}': '${path_HPSS}${filename_tar}'"
		tar -xvf ${path_history}/${filename_tar} -C ${path_history} 
		rm -f ${path_history}/${filename_tar}
	    endif
	endif
    endif

endif

##
## If we cannot still find any file with the filename, there is a problem
##

echo "ATTEMPT TO LOCATE FILE TO DETERMINE HISTORY ATTRIBUTES "  
if (! -e ${fullpath_filename} || -z ${fullpath_filename} ) then
      echo 'ERROR: cannot find a file with the required file name' 
      echo 'Check in the script:  determine_output_attributes.csh '
      exit
else
     echo 'FOUND '${fullpath_filename}
     echo 'FILE IS USED TO DETERMINE HISTORY FILES ATTRIBUTES'
endif


## ========================================================
## Get all attributes
## ========================================================

##
## Make a directory that contains the attributes 
##
if (! -e ${path_diag}/attributes) then
    mkdir ${path_diag}/attributes
endif


##
## Rootname
##
echo ${rootname} >  ${path_diag}/attributes/${casetype}_rootname

##
## Determine case grid
##
ncks  -q -d lat,0 -d lon,0 -d lev,0 -d ilev,0 -v gw $fullpath_filename  >&! /dev/null 
set var_present = $status

if ($var_present == 0) then
    set cam_grid = 'Lat/Lon'
else
    set cam_grid = 'SE'
endif

echo ${cam_grid} >  ${path_diag}/attributes/${casetype}_grid

##
## Determine case resolution
##
if ($cam_grid == SE) then
   ncl  $DIAG_CODE/get_res_se.ncl 'dataFile=addfile("'${fullpath_filename}'", "r")'  'resFile="'${path_diag}/attributes/${casetype}_res'"'  
else 
   echo ' ' > ${path_diag}/attributes/${casetype}_res 

endif


##
## Determine which variables are present
##


# List of variables that are time-independant
if ($cam_grid != SE) then 
       set non_time_var_list = (gw,hyam,hybm,hyai,hybi,P0,lev,ilev,lat,lon,P0)
else
       set non_time_var_list = (area,hyam,hybm,hyai,hybi,P0,lev,ilev,lat,lon,P0) 
endif
echo  ${non_time_var_list}  > ${path_diag}/attributes/${casetype}_non_time_var_list

# List of variables that are time-dependant
# set of required variables for AMWG package
set required_vars = (AODVIS AODDUST AODDUST1 AODDUST2 AODDUST3 \
                     ANRAIN ANSNOW AQRAIN AQSNOW AREI AREL AWNC AWNI \
                     CCN3 CDNUMC CLDHGH CLDICE CLDLIQ CLDMED CLDLOW CLDTOT   \
                     CLOUD DCQ DTCOND DTV FICE FLDS FLNS FLNSC FLNT FLNTC    \
                     FLUT FLUTC FREQI FREQL FREQR FREQS FSDS FSDSC FSNS FSNSC \
                     FSNTC FSNTOA FSNTOAC FSNT ICEFRAC ICIMR ICWMR IWC LANDFRAC \
                     LHFLX LWCF NUMICE NUMLIQ OCNFRAC OMEGA OMEGAT P0 PBLH PRECC \
                     PRECL PRECSC PRECSL PS PSL Q QFLX QRL QRS RELHUM SHFLX   \
                     SNOWHICE SNOWHLND SOLIN SWCF T TAUX TAUY TGCLDIWP \
                     TGCLDLWP TMQ TREFHT TS U UU V VD01 VQ VT VU VV WSUB Z3 \
                     CLD_MISR FMISR1 \
                     FISCCP1_COSP FISCCP1 CLDTOT_ISCCP \
                     MEANPTOP_ISCCP MEANCLDALB_ISCCP \
                     CLMODIS FMODIS1 \
                     CLTMODIS CLLMODIS CLMMODIS CLHMODIS CLWMODIS CLIMODIS \
                     IWPMODIS LWPMODIS REFFCLIMODIS REFFCLWMODIS \
                     TAUILOGMODIS TAUWLOGMODIS TAUTLOGMODIS \
                     TAUIMODIS TAUWMODIS TAUTMODIS PCTMODIS \
                     CFAD_DBZE94_CS CFAD_SR532_CAL \
                     CLDTOT_CAL CLDLOW_CAL CLDMED_CAL CLDHGH_CAL CLDTOT_CS2 U10 \
# variables for CAM-chem (needs to be clean up from duplicates of the CAM variables: AODDUST... ) \
		     CH4 CH4_CHML SFCH4 CO CO_CHMP CO_CHML SFCO DCOCHM DF_CO O3 O3_Prod O3_Loss O3_CHMP O3_CHML DF_O3 \
                     CH3CCL3 CH3CCL3_CHML ISOP C10H16 LNO_COL_PROD SFISOP SFC10H16 SFCH3OH SFC2H2 SFCH3COCH3 \
                     PHIS ODV_DST01 ODV_DST02 ODV_DST03 ODV_DST04 AODDUST1 AODDUST2 AODDUST3 AEROD_v \
                     SFO3 DO3CHM NO NO2 NOX NOY H2O Q OH H2O2 N2O HNO3 PAN C3H8 CH3COCH3 CH2O CH3OH C2H2 C2H6 \
                     C3H6 SO2 SO4 CB1 CB2 OC1 OC2 SOA SOAI SOAM SOAX SOAB SOAT NH4NO3 \
                     SOAI_PROD SOAM_PROD SOAX_PROD SOAB_PROD SOAT_PROD \
                     CB2SFWET OC2SFWET OC2WET SO4SFWET SOAISFWET SOATSFWET SOABSFWET SOAXSFWET SOAMSFWET \
                     DST01 DST02 DST03 DST04 SSLT01 SSLT02 SSLT03 SSLT04 SAD_TROP  \
                     SAD_ICE SAD_LNAT SAD_SULFC SAD_SO4NIT SAD_SOA SAD_BC jo3_a jno2 jpan jh2o2  \
                     SFSSLT01 SFSSLT02 SFSSLT03 SFSSLT04 SFDST01 SFDST02 \
                     SFDST03 SFDST04 DST01SFWET  DST02SFWET  DST03SFWET  DST04SFWET SSLT01SFWET SSLT02SFWET SSLT03SFWET SSLT04SFWET \
                     SFSO4 SO4_CHMP SO4_CHML DSO4CHM DTWR_SO2 DF_DST01 \
                     DF_DST02 DF_DST03 DF_DST04 DF_SSLT01 DF_SSLT02 DF_SSLT03 DF_SSLT04 DF_OC1 DF_OC2 DF_CB1 DF_CB2\
                     DF_SOAM DF_SOAI DF_SOAT DF_SOAB DF_SOAX DF_SO4 \
                     a2x_DSTWET1 a2x_DSTWET2 a2x_DSTWET3 a2x_DSTWET4 CB1_CLXF SFCB1 SFCB2 SFOC1 SFOC2 \
                     AQSO4_H2O2 AQSO4_O3  \
                     soa_a1 soa_a2 soa_c1 soa_c2 dst_a1 dst_a3 dst_a5 dst_a7 dst_c1 dst_c3 dst_c5 dst_c7 \
                     ncl_a1 ncl_a2 ncl_a3 ncl_a4 ncl_a6 ncl_c1 ncl_a2 ncl_c3 ncl_c4 ncl_c6 \
                     pom_a1 pom_c1 pom_a3 pom_c3 pom_a4 pom_c4 bc_a1 bc_a3 bc_a4 bc_c1 bc_c3 bc_c4 \
                     so4_a1 so4_a2 so4_a3 so4_a4 so4_a5 so4_a6 so4_a7 so4_c1 so4_c2 so4_c3 so4_c4 so4_c5 so4_c6 so4_c7 \
                     SFpom_a1 SFpom_a3 SFpom_a4 pom_a1_CLXF pom_a2_CLXF pom_a4_CLXF  \
                     pom_a1DDF pom_a2DDF pom_a4DDF  pom_a1SFWET pom_a2SFWET pom_a4SFWET \
                     pom_c1DDF pom_c2DDF pom_c4DDF  pom_c1SFWET pom_c2SFWET pom_c4SFWET \
                     SFbc_a1 SFbc_a3 SFbc_a4 bc_a1_CLXF bc_a2_CLXF bc_a4_CLXF  \
                     bc_a1DDF bc_a2DDF bc_a4DDF  bc_a1SFWET bc_a2SFWET bc_a4SFWET \
                     bc_c1DDF bc_c2DDF bc_c4DDF  bc_c1SFWET bc_c2SFWET bc_c4SFWET \
                     soa_a1_sfgaex1 soa_a2_sfgaex1  soa_a1DDF soa_a2DDF soa_a1SFWET soa_a2SFWET \
                     soa_c1DDF soa_c2DDF soa_c1SFWET soa_c2SFWET \
                     dst_a1SFWET dst_a3SFWET dst_a5SFWET dst_a7SFWET dst_a1DDF dst_a3DDF dst_a5DDF dst_a7DDF \
                     dst_c1SFWET dst_c3SFWET dst_c5SFWET dst_c7SFWET dst_c1DDF dst_c3DDF dst_c5DDF dst_c7DDF \
                     ncl_a1SFWET ncl_a2SFWET ncl_a3SFWET ncl_a4SFWET ncl_a6SFWET ncl_a1DDF ncl_a3DDF ncl_a4DDF ncl_a6DDF \
                     ncl_c1SFWET ncl_c2SFWET ncl_c3SFWET ncl_c4SFWET ncl_c6SFWET ncl_c1DDF ncl_c3DDF ncl_c4DDF ncl_c6DDF \
                     SFdst_a1 SFdst_a3 SFdst_a5 SFdst_a7 \
                     SFncl_a1 SFncl_a2 SFncl_a3 SFncl_a4 SFncl_a6 \
                     so4_a1_CHMP so4_a2_CHMP so4_a3_CHMP so4_a4_CHMP so4_a5_CHMP so4_a6_CHMP so4_a7_CHMP \
                     SFso4_a1 SFso4_a2 SFso4_a3 SFso4_a4 SFso4_a5 SFso4_a6 SFso4_a7 \
                     so4_a1_CLXF so4_a2_CLXF so4_a3_CLXF so4_a4_CLXF so4_a5_CLXF so4_a6_CLXF so4_a7_CLXF \
                     so4_a1_sfgaex1 so4_a2_sfgaex1 so4_a3_sfgaex1 so4_a4_sfgaex1 so4_a5_sfgaex1 so4_a6_sfgaex1 so4_a7_sfgaex1 \
                     so4_a1_sfnnuc1 so4_a2_sfnnuc1 so4_a3_sfnnuc1 so4_a4_sfnnuc1 so4_a5_sfnnuc1 so4_a6_sfnnuc1 so4_a7_sfnnuc1 \
                     so4_a1DDF so4_a2DDF so4_a3DDF so4_a4DDF so4_a5DDF so4_a6DDF so4_a7DDF \
                     so4_a1SFWET so4_a2SFWET so4_a3SFWET so4_a4SFWET so4_a5SFWET so4_a6SFWET so4_a7SFWET \
                     so4_c1DDF so4_c2DDF so4_c3DDF so4_c4DDF so4_c5DDF so4_c6DDF so4_c7DDF \
                     so4_c1SFWET so4_c2SFWET so4_c3SFWET so4_c4SFWET so4_c5SFWET so4_c6SFWET so4_c7SFWET )

set first_find = 1
set var_list = " "
foreach var ($required_vars)
   if ($file_type == monthly_history) then
	if ($cam_grid != SE) then 
	    ncks --quiet  -d lat,0 -d lon,0 -d lev,0 -d ilev,0 -v $var $fullpath_filename  >&! /dev/null 
	    set var_present = $status
	else  
	    ncks --quiet   -d ncol,0 -d lev,0 -d ilev,0 -v $var $fullpath_filename >&! /dev/null 
	    set var_present = $status
	endif
	if ($var_present == 0) then
	    if ($first_find) then
		set var_list = $var
		set first_find=0
	    else
		set var_list = ${var_list},$var
	    endif
	endif
   else
        if ($first_find) then
            set var_list = $var
            set first_find=0
        else
            set var_list = ${var_list},$var
        endif
   endif
end

echo  ${var_list}  > ${path_diag}/attributes/${casetype}_var_list
echo  ${required_vars}  > ${path_diag}/attributes/required_vars 

