#!/bin/csh -f

set file = $1
set arch = `uname`

# Check BASEDIR
# -------------
if( ! $?BASEDIR ) then
      echo " "
      echo "Environment variable BASEDIR must be defined before use\!"
      echo " "
      exit 7
endif

if( -e $BASEDIR/$arch/bin/hdfdump ) then
        set hdfdump   =   hdfdump   # Old HDF4 Format
else
        set  hdfdump  =   ncdump
        set  version  =  `file -b $1`	
	if("$version" == "Hierarchical Data Format (version 4) data") then
            if( -e $BASEDIR/$arch/bin/ncdump-hdf4 ) then
                       set hdfdump =  ncdump-hdf4
            endif
        endif
endif 

set nymdb = `$BASEDIR/$arch/bin/$hdfdump -h $file | grep time:begin_date     | cut  -d " " -f 3`
set nhmsb = `$BASEDIR/$arch/bin/$hdfdump -h $file | grep time:begin_time     | cut  -d " " -f 3`
set dtinc = `$BASEDIR/$arch/bin/$hdfdump -h $file | grep time:time_increment | cut  -d " " -f 3`
set tmax  = `$BASEDIR/$arch/bin/$hdfdump -h $file | grep "time = "      | grep -v "time:"`
if( $tmax[3] == UNLIMITED ) then
     set tmax = `echo $tmax | cut -d " " -f 6 | cut -c2-`
else
     set tmax = $tmax[3]
endif

set  length = `echo $nymdb | awk '{print length}'`
if( $length == 5 ) set nymdb = 000$nymdb
if( $length == 6 ) set nymdb =  00$nymdb
if( $length == 7 ) set nymdb =   0$nymdb

@    nhmsb = $nhmsb
if( $nhmsb < 10     ) set nhmsb = 0$nhmsb
if( $nhmsb < 100    ) set nhmsb = 0$nhmsb
if( $nhmsb < 1000   ) set nhmsb = 0$nhmsb
if( $nhmsb < 10000  ) set nhmsb = 0$nhmsb
if( $nhmsb < 100000 ) set nhmsb = 0$nhmsb

@ mod1  =   $dtinc % 100
@ mod2  =   $dtinc % 10000
@ nsecf = ( $dtinc / 10000 ) * 3600 + ( $mod2 / 100 ) * 60 + $mod1
@ ndt   = ( $tmax - 1 ) * $nsecf


set ending_date = `$GEOSUTIL/post/tick $nymdb $nhmsb $ndt`
set       nymde = $ending_date[1]
set       nhmse = $ending_date[2]

set  length = `echo $nymde | awk '{print length}'`
if( $length == 5 ) set nymde = 000$nymde
if( $length == 6 ) set nymde =  00$nymde
if( $length == 7 ) set nymde =   0$nymde

#set month0 = `echo $nymdb | cut -c5-6`
#set month1 = `echo $nymde | cut -c5-6`

#echo  Begin Date: $nymdb
#echo  Begin Time: $nhmsb
#echo Ending Date: $nymde
#echo Ending Time: $nhmse
#echo    Time Inc: $dtinc

echo $nymdb $nhmsb $nymde $nhmse $nsecf
