#! /usr/local/bin/perl -w

$clockfiduc="/astroc/data/fits_frf_facom/clkfeduc.list";
$frfdir    ="/astroc/data/ebisawa/fits_frf_facom-2005-03-27/";
#@monthdir=split(" ",`ls -d $frfdir/????`);
@monthdir=split(" ",`ls -d $frfdir/8703`);

foreach $month (@monthdir){
    $month=~/\S+(\d\d\d\d)/;
    $thismonth=$1;
    if(!-e $thismonth){
	$command = "mkdir $thismonth\n";
	print $command;
	system($command);
    }
    $frf = `ls $month/FR*.fits*`;
    @eachfrf = split(" ",$frf);
    chdir($thismonth);
    system("pwd");
    if(-e "time_correction.log"){
	$command ="rm -f time_correction.log\n";
	system($command);
    }
    open(TMP,">>time_correction.log");
    foreach $eachfrf (@eachfrf){
	$eachfrf=~/\/(FR\d\d\d\d\d\d\.\S\d\d\d\d\.fits)/;
	$thisfrf = $1;
	print TMP "In= ",$eachfrf," Out= ",$thisfrf,"\n";
	open(COM,">tmp.csh");
	print COM "/astroc/soft/unix/frf/FRF_time_correction <<%%\n";
	print COM "$eachfrf\n";
	print COM "$thisfrf\n";
	print COM "$clockfiduc\n";
	print COM "%%\n";
	close(COM);
	$command="csh -vx tmp.csh >> time_correction.log\n";
	system($command);
        $command ="gzip -f $thisfrf\n";
	system($command);
    }
    close(TMP);
    $command ="rm -f tmp.csh\n";
    system($command);
    chdir("..");
}

