#!/usr/bin/perl

# Script to process .dat output from SpectrumLab and reformat to RMOB colorgramme standard
# 
# Changelog:
# 20071123 - added a footer to the output files
# 20071203 - now copies RMOB-latest to RMOB-yyyymm for archiving
# 20080229 - added a leap year bugfix so Feb 29 shows up
# 20080625 - bugfix to neaten up times that have counts <10


# Get current date and set input filename
@monthar = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) ;
@monthdar= ( 31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31) ;
($sec,$min,$hour,$day,$month,$year,$weekDay,$dayOfYear,$isDST) = localtime(time) ;
#leap year fix
if($month==1 && $day==29)
{
	print "It's February!\n" ;
	@monthdar= ( 31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31) ;
}
$montht = $monthar[$month] ;	# print "$montht\n" ;
$dayt   = $monthdar[$month] ;	# days in current month
$month++ ;
$year = $year + 1900 ;
if($min<10){$min = "0".$min ;}
if($day<10){$day = "0".$day ;}
if($month<10){$month = "0".$month ;}
$date = $year.$month;
my $fileh = "RMOB-".$date.".dat" ;


# Use date to open correct dat file
if(-e $fileh){$check=1 ;}
else{$fileh="RMOB-latest.dat" ;}
open(INFIL, $fileh) or die "Error opening input file ($fileh)\n" ;

# read in data
my @data = <INFIL> ;
close(INFIL) ;

# Copy the RMOB-latest file to RMOB-yyyymm.dat for archiving
my $log = "RMOBlog-".$year.$month.".dat" ;
open(LOG, ">$log") ;
foreach $line(@data){
	print LOG $line ;
}

# output file
$outfil = "jbomd_".$month.$year."rmob.txt" ; # terrier_012005rmob.txt
open(OUTFIL, ">$outfil") ;
$latest = "jbomd_latest_rmob.txt" ;
open(LATEST, ">$latest") ;

# write file header
print OUTFIL "$montht| 00h| 01h| 02h| 03h| 04h| 05h| 06h| 07h| 08h| 09h| 10h| 11h| 12h| 13h| 14h| 15h| 16h| 17h| 18h| 19h| 20h| 21h| 22h| 23h|" ;
print LATEST "$montht| 00h| 01h| 02h| 03h| 04h| 05h| 06h| 07h| 08h| 09h| 10h| 11h| 12h| 13h| 14h| 15h| 16h| 17h| 18h| 19h| 20h| 21h| 22h| 23h|" ;

# split and get useful bits
foreach $line(@data){
	($date,$hour,$count1,$count2,$reflc)=split(/\s\,\s/, $line) ;
#	print "$date\n" ;
	$newday = substr($date,6,2) ;	# 2007110516
	$newhour = substr($date,8,2) ;	# 2007110516
	$result[$hour][$newday] = $count1 ;	# array of arrays!
}

# write out reformatted data
for($i=1; $i<=$dayt; $i++){
	if($i<10){$d="0".$i ;}else{$d=$i ;}
	print OUTFIL "\n $d| " ;
        print LATEST "\n $d| " ;
	for($j=0; $j<24; $j++){
		if($result[$j][$i] eq ""){$result[$j][$i]="???" ;}
		if($result[$j][$i] <0 ){$result[$j][$i]=" -1" ;}	# hack 20080208 to fix spacing problems in rmob files
		if(($result[$j][$i]<10) && ($result[$i][$j]>=0) && ($result[$j][$i] ne "???")){$result[$j][$i]="  ".$result[$j][$i] ;}
		if(($result[$j][$i]<100) && ($result[$j][$i]>=10)){$result[$j][$i]=" ".$result[$j][$i] ;}
		print OUTFIL "$result[$j][$i]| " ;
		print LATEST "$result[$j][$i]| " ;
	}
}
print OUTFIL "\n" ;
print OUTFIL << "EOF" ;
These data were produced by Jodrell Bank Observatory's Meteor Detector (JBOMD) set up by Eddie Blackhurst and Megan Argo in 2007.
The data are provided freely but without any guarantees.  If you make use of these data, please let us know.
For more information, please see http://www.jb.man.ac.uk/viscen/meteor/
EOF
close(OUTFIL) ;
print LATEST "\n" ;
print LATEST << "EOF" ;
These data were produced by Jodrell Bank Observatory's Meteor Detector (JBOMD) set up by Eddie Blackhurst and Megan Argo in 2007.
The data are provided freely but without any guarantees.  If you make use of these data, please let us know.
For more information, please see http://www.jb.man.ac.uk/viscen/meteor/
EOF
close(LATEST) ;

