Perl Script for Downloading GOES Data

This perl script will log on to the Wallops Center and download any recent data for the specified buoy. The buoys are descriminated by what's known as the platform id.

#!/usr/bin/perl
#
# RETRIEVE_GOES_BUFFER:  Downloads binary data file from Wallops.
#
#  original author: J. Doutt
#  date: 5 June 2001
#  rev:  010718.1020
#  Modified by John Evans, 2 February 2002
#
#
#  This file contains the subroutines needed for the "gomoos" processing
#
#  getdata       PERL "expect" script for getting the GEOS data & saving
#                   into a file
#
# USAGE:  > retrieve_goes_buffer.pl platform_id binary_file
#
# PARAMETERS:
#    platform_id:
#        This is an 8 character alphanumeric id that is unique for each
#        buoy that transmits thru goes.  For example, the platform id 
#        for A0103 is "0440465E"
#    binary_file:
#        each message (data payload from Wallops) is saved to this file.
#        The file is not decoded here.



use Expect;

	$num_args = @ARGV;
	if ( $num_args != 2 ) {
		printf ( STDERR "Expecting 2 input arguments, got %d instead.\n", $num_args );
		exit -1;
	}

	$platform_id = $ARGV[0];
	$output_file = $ARGV[1];

	printf ( STDOUT "platform id is set to %s\n\n", $platform_id );

	printf ( STDOUT "calling getdata...\n\n" );
	&getdata ( "128.154.62.173", "usgs01", "wallopscda", "uomdoa", "aaaaaa", $platform_id, $output_file );


	exit 0;




########## getdata ######################################################
#
sub getdata  {
#
#  auth: J. Doutt
#  date: 31 May 2001
#  rev:  0106011.1400
#  
#  retrievs data GOES data from DCS.NOAA.GOV
#  automatically creates date-related file containing the data
#  deletes that file if there is no data in it
#
#  returns the timestamp part of the file name if successful, -1 if unsuccessful

	use strict;


	my($hostname,$NAME1,$PSWD1,$NAME2,$PSWD2,$PLATFORM1,$OUTPUTFILE);
	($hostname,$NAME1,$PSWD1,$NAME2,$PSWD2,$PLATFORM1,$OUTPUTFILE)=@_;

	my($telnet);
	my($logtime,$logfile,$timeout);
	my($do_message_file);

	$timeout=120;


	#
	#  Select whether we want to download a message file or a platform ID
	#  0 for getting data from platform ID
	#  list id number for getting data from a message file
	#  $do_message_file = 1;  #  >=1 to download a message file   (e.g. list_id 1)
	#  $do_message_file = 2;  #  >=1 to download a message file   (e.g. list_id 1)
	$do_message_file = 0;  #   =0 to download a platform ID    (e.g. 3130646)




	# Set up file name for logging the data
	#
	my($sec,$min,$hour,$mday,$mon,$year,@tmp);      # for getting the time
	($sec,$min,$hour,$mday,$mon,$year,@tmp)=localtime(time);

	$logtime = sprintf("%4d%02d%02d.%02d%02d",$year+1900,$mon+1,$mday,$hour,$min);
	$logfile = $OUTPUTFILE;
	printf ( STDOUT "log file set to $logfile...\n\n" );
	print "$logfile\n";



	#
	# Wallops CDA FireWall
	# spawn telnet 128.154.62.173
	($telnet = Expect->spawn("telnet $hostname"))  || die"Couldn't spawn telnet, $!";
	#           expect     "User:"
	unless ($telnet->expect($timeout,"User:"))  {
		die "Never got username prompt on $hostname, ".$telnet->exp_error()."\n";
	}


	#
	# send  "$NAME1\r"
	print $telnet  "$NAME1\r";


	#
	# send_user "\[Sent name;  waiting for password\]\n"
	# expect    "assword:"
	unless ($telnet->expect($timeout,"assword:"))  {
		die "Never got password prompt on $hostname, ".$telnet->exp_error()."\n";
	}


	#
	# send   "$PSWD1\r"
	print $telnet   "$PSWD1\r";



	#
	# Need a <CR> to get going!!
	# expect "begin logging on"
	unless ($telnet->expect($timeout,"begin logging on"))  {
		die "Never got 'begin logging on' prompt on $hostname, ".$telnet->exp_error()."\n";
	}


	#
	#           send   "\r"
	print $telnet   "\r";


	#
	# Now log onto the .....
	# expect     "Username:"
	# send "$NAME2\r"
	unless ($telnet->expect($timeout,"Username:"))  {
		die "Never got 2nd username prompt on $hostname, ".$telnet->exp_error()."\n";
	}
	print $telnet "$NAME2\r";




	#
	# expect     "Password:"
	# send "$PSWD2\r"
	unless ($telnet->expect($timeout,"Password:"))  {
		die "Never got 2nd password prompt on $hostname, ".$telnet->exp_error()."\n";
	}
	print $telnet "$PSWD2\r";




	#
	# Now we're logged on
	# download the data
	#
	# expect ">"
	unless ($telnet->expect($timeout,">"))  {
		die "Never got '>' prompt on $hostname, ".$telnet->exp_error()."\n";
	}



	#
	#    select whether we want to download message file, or platform ID
	if ($do_message_file)  {

		#
		# This was the old shorthand
		# send "do msg list_id 1\r"   
		# send "DOWNLOAD MESSAGE_FILE LIST_ID 1\r"
		# print $telnet  "download message_file list_id 1\r";
		# print $telnet  "download message_file list_id 2\r";
		print $telnet  "download message_file list_id $do_message_file\r";

	} else  {

		#
		#
		# This instead of previous for downloading OLD instead of new data
		# send "do msg platform_id $PLATFORM1\r"   
		print $telnet  "download message_file platform_id $PLATFORM1\r";
		# expect ">>"
		# send "Y\r"

	}   #  end if ... else


	unless ($telnet->expect($timeout,">>"))  {
		die "Never got '>>' prompt on $hostname, ".$telnet->exp_error()."\n";
	}


	#
	# start logging the data
	$telnet->log_file($logfile);
	print $telnet "Y\r";


	#
	# Need the following to wait before quitting!!
	#    expect ">";`
	#    send "bye\r";
	unless ($telnet->expect($timeout,">"))  {
		#die "Never got final '>' prompt on $hostname, ".$telnet->exp_error()."\n";
		print  "Never got final '>' prompt on $hostname, ".$telnet->exp_error()."\n";
		$telnet->log_file("/dev/null");
		print $telnet "bye\r";
	}


	#
	#  Close log file
	print $telnet "bye\r";


	#
	#      Wait until all output from TELNET session is finished
	unless ($telnet->expect($timeout,"nnection close"))  {
		die "Never got final 'Connection closed' prompt on $hostname, ".$telnet->exp_error()."\n";
	}



	return $logfile;

}