#!/usr/bin/perl # This script downloads the raw plion data set from www-graphics.stanford.edu. # # Usage: # getlion.perl [slab x y] # # This script will create 4 directories inside the current directory, # and then download the lion raw .rgb images through a http socket # connection, and store them in the 4 subdirectores. # # If no arguments, then it will download the entire data set. # if three arguments are specified, then it will download a # single image, from that SLAB, X, and Y. # # It is useful to download single images in the event that # some of the files get corrupted in transfer. # # (i.e. "getlion.perl 1 0 0" will get the first image of slab 1.) # Set up a few general paramaters... $server = "www-graphics.stanford.edu"; $port = 80; $dir = "software/lightpack/ftp/rawdata"; # Set this to true if you only want it to download images that have # not already been downloaded. This mode is useful if a subset of the # images have already been downloaded. $NOCLOBBER = 1; # The number of seconds to pause between each image. # If the pause is too short, too many processes will be forked and it # may overload the system. $pause = 0; if ($#ARGV == -1) { # No arguments, download entire data set. $SINGLEIMAGE = 0; } elsif ($#ARGV == 2) { # 3 arguments, download a single image. $SINGLEIMAGE = 1; $SINGLESLAB = $ARGV[0]; $SINGLEX = $ARGV[1]; $SINGLEY = $ARGV[2]; } else { printf("Usage:\n". "getlion.perl [slab x y]\n". "\n". "If no arguments, then it will download the entire data set.\n". "if three arguments are specified, then it will download a\n". "single image, from that SLAB, X, and Y.\n". "\n". "It is useful to download single images in the event that\n". "some of the files get corrupted in transfer.\n". "\n". "(i.e. \"getlion.perl 1 0 0\" will get the first image\n". "of slab 1.)\n"); exit; } # Make the directories for the 4 slabs of the lion. if (!-e "plion1") {system("mkdir plion1\n");} if (!-e "plion2") {system("mkdir plion2\n");} if (!-e "plion3") {system("mkdir plion3\n");} if (!-e "plion4") {system("mkdir plion4\n");} # This is similar to the "simple sample client" program # on p.343 of the O'Reilly _Programming_Perl_ book. # Set up parameters, etc.. $AF_INET = 2; $SOCK_STREAM = 2; $sockaddr = 'S n a4 x8'; $SIG{'INT'} = 'dokill'; sub dokill { kill 9, $child if $child; } chop($hostname = `hostname`); ($name, $aliases, $proto) = getprotobyname('tcp'); ($name, $aliases, $port) = getservbyname($port, 'tcp') unless $port =~ /^\d+$/;; ($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname); ($name, $aliases, $type, $len, $thataddr) = gethostbyname($server); $this = pack($sockaddr, $AF_INET, 0, $thisaddr); $that = pack($sockaddr, $AF_INET, $port, $thataddr); # Now retrieve the files. for ($slab=1; $slab <5; $slab++) { for ($x=0; $x < 32; $x++) { for ($y=0; $y < 16; $y++) { # If in SINGLESLAB mode, skip every iteration except the # desired one. if ($SINGLEIMAGE && (($slab != $SINGLESLAB) || ($x != $SINGLEX) || ($y != $SINGLEY))) { next; } # Set up the current file name $filename = "plion".$slab."/plion".$slab."_project.$x.$y.rgb"; # If in NOCLOBBER mode, skip files that already exist. if ($NOCLOBBER && ((-e $filename) && !(-z $filename))) { printf("skipping $filename -- it already exists.\n"); next; } # If we get here, we're downloading this file... printf("Getting $filename...\n"); # Make the socket filehandle. if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { # print "socket ok\n"; } # If regular socket fails, try $SOCK_STREAM = 1 elsif (socket(S, $AF_INET, 1, $proto)) { $SOCK_STREAM = 1; # print "socket ok\n"; } else { printf("Error: PERL function ". "\"socket(S, $AF_INET, $SOCK_STREAM, $proto)\"". " failed.\n". " Darn. I don't know why. :-(\n". " You might try running it from another\n". " computer, or send email to\n". " lightpack\@graphics.stanford.edu,\n". " and we'll try to work something else out.\n"); die $!; } # Give the socket an address. if (bind(S, $this)) { # print "bind ok\n"; } else { die $!; } # Call up the server. if (connect(S, $that)) { # print "connect ok\n"; } else { die $!; } # Set socket to be command buffered. select(S); $| = 1; select(STDOUT); # Open up the file for writing... open(OUTFILE, ">".$filename); # --- Send the request for a file sleep $pause; printf(S "GET /$dir/$filename HTTP/1.0\n\n\n"); # --- Handle the reply # First, skip over the header. # Header ends with a blank line. $reply = ; while (!($reply eq "\n")) { # For fun, print out size of image we're downloading. if ($reply =~ /Content-length/) { print $reply; } $reply = ; } # Save the rest to file while() { print OUTFILE; } close(OUTFILE); } } }