#! /bin/csh -f
# *******************************************************
cd /data/ncei1/sohc/ri_sohc
set file_year = "../data0/current_year.dat"
set year=`cat $file_year`
echo $year

ls  /data/ncei1/sohc/nc_data0/$year/ohc_*.nc > ./jobfiles/sohc_files_list.dat
ls /data/ncei1/sohc/ri_sohc/statistics/ohc_*$year*.dat > ./jobfiles/stat_files_list.dat

set file_new="./jobfiles/new_files_list.dat"
rm  $file_new

f95 -o pick_up_new_files.exe pick_up_new_files.f
./pick_up_new_files.exe
# *******************************************************
set nr=`wc -l $file_new`
if($nr[1] == 0)then
echo "no new data file has been processed"
goto job_end
endif

# *******************************************************
set main_nx_ny = "write_change_nx_ny.f"
set runnn1 = "runnn_nx_ny.exe"

set main_dump_stat = "main_dump_stat_sohc.f"
set main_doc = "f77_docs/main_dump_stat_sohc_doc.f"
set sub1 = "sub_statistics_sohc.f"
set runnn2 = "data_dump_stat.exe"

# *******************************************************
set nc_input = "sohc_all.nc"
set dump_output = "data/sohc_all_dump.dat"
set stat_output = "data/sohc_all_statistics.dat"
# *******************************************************

set nr=`wc -l $file_new`

 @ j =1
# while($j <= 15)
while($j <= $nr[1])

set new_file_txt = `awk -v ln=$j '{if (NR==ln) print $0}' $file_new`

set infile="/data/ncei1/sohc/nc_data0/"$year"/"$new_file_txt".nc"
set statfile="/data/ncei1/sohc/ri_sohc/statistics/"$new_file_txt".dat"
# set dumpfile="/data/ncei1/sohc/ri_sohc/dump_data/"$new_file_txt".dat"

cp $infile $nc_input

echo $infile
echo $statfile
# echo $dumpfile

cp $main_doc $main_dump_stat

f95 -std=legacy -fno-second-underscore -o $runnn1 $main_nx_ny  /data/starfs1/libs/netcdf-3.6.3/lib/libnetcdf.a
./$runnn1

csh change_nx_ny.csh

f95 -std=legacy -fno-second-underscore -o $runnn2 $main_dump_stat $sub1 /data/starfs1/libs/netcdf-3.6.3/lib/libnetcdf.a
 ./$runnn2

# cp $dump_output $dumpfile
cp $stat_output $statfile
# *******************************************************
@ j++
end

csh plot_sohc_stat.csh

job_end:

