PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Astro/Aladin/LowLevel/LowLevel.pm

https://github.com/bradcavanagh/perl-modules-for-astronomy
Perl | 371 lines | 184 code | 137 blank | 50 comment | 20 complexity | 4f08b761d315fe3142c41bd91b114ee4 MD5 | raw file
  1. package Astro::Aladin::LowLevel;
  2. # ---------------------------------------------------------------------------
  3. #+
  4. # Name:
  5. # Astro::Aladin::LowLevel
  6. # Purposes:
  7. # Perl class designed to drive the standalone CDS Aladin Application
  8. # Language:
  9. # Perl module
  10. # Description:
  11. # This module drives the CDS Aladin Java application through an
  12. # anonymous pipe.
  13. #
  14. # This isn't an optimal solution, its a kludge hack and I hope
  15. # nobody I know is reading the code. There is a higher level class
  16. # full of convience methods for a reason. Use it!
  17. # Authors:
  18. # Alasdair Allan (aa@astro.ex.ac.uk)
  19. # Revision:
  20. # $Id: LowLevel.pm,v 1.2 2003/02/24 22:45:56 aa Exp $
  21. # Copyright:
  22. # Copyright (C) 2003 University of Exeter. All Rights Reserved.
  23. #-
  24. # ---------------------------------------------------------------------------
  25. =head1 NAME
  26. Astro::Aladin::LowLevel - Perl class designed to drive CDS Aladin Application
  27. =head1 SYNOPSIS
  28. my $aladin = new Astro::Aladin::LowLevel( );
  29. =head1 DESCRIPTION
  30. Drives the CDS Aladin Application through a anonymous pipe, expects the
  31. a copy of the standalone Aladin application to be installed locally
  32. and pointed to by the ALADIN_JAR environment variable.
  33. =cut
  34. # L O A D M O D U L E S --------------------------------------------------
  35. use strict;
  36. use vars qw/ $VERSION /;
  37. use File::Spec;
  38. use Carp;
  39. '$Revision: 1.2 $ ' =~ /.*:\s(.*)\s\$/ && ($VERSION = $1);
  40. # G L O B A L V A R I A B L E ---------------------------------------------
  41. # Don't know off the top of my head how to bless a typeglob into an
  42. # class. For now we're going to use a global scalar and carry the
  43. # filehandle around in that. This is a not nice kludge, but then
  44. # the entire modle is fairly icky so do I really care at this point?
  45. my $ALADIN = undef;
  46. # C O N S T R U C T O R ----------------------------------------------------
  47. =head1 REVISION
  48. $Id: LowLevel.pm,v 1.2 2003/02/24 22:45:56 aa Exp $
  49. =head1 METHODS
  50. =head2 Constructor
  51. =over 4
  52. =item B<new>
  53. Create a new instance from a hash of options
  54. $aladin = new Astro::Aladin::LowLevel( );
  55. returns a reference to an Aladin object.
  56. =cut
  57. sub new {
  58. my $proto = shift;
  59. my $class = ref($proto) || $proto;
  60. # bless the query hash into the class
  61. my $block = bless { DUMMY => undef }, $class;
  62. # Configure the object
  63. $block->configure( @_ );
  64. return $block;
  65. }
  66. # Q U E R Y M E T H O D S ------------------------------------------------
  67. =back
  68. =head2 Accessor Methods
  69. =over 4
  70. =item B<close>
  71. Closes the anonymous pipe to the aladin application
  72. $aladin->close();
  73. it should be noted that if you DON'T do this after finishing with
  74. the object you're going to have zombie Java VM hanging around eating
  75. up all your CPU. This is amougst the many reasons why you should
  76. use Astro::Aladin rather than Astro::Aladin::LowLevel to drive the
  77. Aladin Application.
  78. =cut
  79. sub close {
  80. my $self = shift;
  81. # set the "quit" command to Aladin
  82. print $ALADIN "quit\n";
  83. # close the pipe
  84. close( $ALADIN );
  85. $ALADIN = undef;
  86. }
  87. =item B<reopen>
  88. Reopen the anonymous pipe to the aladin application
  89. my $status = $aladin->reopen()
  90. returns undef if the pipe if defined and (presumably) already active.
  91. =cut
  92. sub reopen {
  93. my $self = shift;
  94. # check that the pipe is closed and undefined
  95. unless ( defined $self->{PIPE} ) {
  96. my $aladin_jar;
  97. if ( defined $ENV{"ALADIN_JAR"} ) {
  98. $aladin_jar = File::Spec->catfile($ENV{"ALADIN_JAR"});
  99. } else {
  100. croak( "Error: Environment variable \$ALADIN_JAR not defined".
  101. " see package README file");
  102. }
  103. # open the pipe to the application
  104. $ENV{ALADIN_MEM} = "128m" unless defined $ENV{ALADIN_MEM};
  105. open( $ALADIN ,"| java -mx$ENV{ALADIN_MEM} -jar $ENV{ALADIN_JAR} -script" );
  106. return;
  107. }
  108. return undef;
  109. }
  110. =item B<status>
  111. Prints out the status of the current stack.
  112. $aladin->status()
  113. =cut
  114. sub status {
  115. my $self = shift;
  116. # set the "status" command to Aladin
  117. print $ALADIN "status\n";
  118. }
  119. =item B<sync>
  120. Waits until all planes are ready
  121. $aladin->sync()
  122. =cut
  123. sub sync {
  124. my $self = shift;
  125. # set the "sync" command to Aladin
  126. print $ALADIN "sync\n";
  127. }
  128. =item B<export>
  129. Export a plane to a file
  130. $aladin->sync( $plane_number, $filename )
  131. =cut
  132. sub export {
  133. my $self = shift;
  134. my $number = shift;
  135. my $file = shift;
  136. # set the "export" command to Aladin
  137. print $ALADIN "export $number $file\n";
  138. }
  139. =item B<get>
  140. Gets images and catalogues from the server
  141. $aladin->get( $server, \@args, $object, $radius );
  142. $aladin->get( $server, $object );
  143. For example
  144. $aladin->get( "aladin", ["DSS1"], $object_name, $radius );
  145. $aladin->get( "aladin", ["DSS1", "LOW"], $object_name, $radius );
  146. $aladin->get( "aladin", [""], $object_name, $radius );
  147. the radius arguement can be omitted
  148. $aladin->get( "aladin", ["DSS1"], $object_name );
  149. or even more simply
  150. $aladin->get( "simbad", $object_name );
  151. always remember to sync after a series of request, or you might end
  152. up closing Aladin before its actually finished download the layers.
  153. =cut
  154. sub get {
  155. my ( $self, $server, $args_ref, $object, $radius );
  156. # Parse the incoming arguements and see whether we have
  157. # any arguements to pass to the image/catalog server
  158. if( scalar @_ == 5 ) {
  159. ( $self, $server, $args_ref, $object, $radius ) = @_;
  160. } elsif( scalar @_ == 4 ) {
  161. ( $self, $server, $args_ref, $object ) = @_;
  162. } elsif ( scalar @_ == 3 ) {
  163. ( $self, $server, $object ) = @_;
  164. $args_ref = [""];
  165. }
  166. # Call the _get() private method
  167. _get( $server, $args_ref, $object, $radius );
  168. }
  169. # C O N F I G U R E -------------------------------------------------------
  170. =back
  171. =head2 General Methods
  172. =over 4
  173. =item B<configure>
  174. Configures the object
  175. $aladin->configure( );
  176. =cut
  177. sub configure {
  178. my $self = shift;
  179. # Call the reopen() method to open the anonymous pipe
  180. reopen();
  181. }
  182. # T I M E A T T H E B A R --------------------------------------------
  183. =back
  184. =begin __PRIVATE_METHODS__
  185. =head2 Private methods
  186. These methods are for internal use only.
  187. =over 4
  188. =item B<get>
  189. Get an image or catalogue
  190. $aladin->get( $server, \@args, $object );
  191. =cut
  192. sub _get {
  193. my ( $server, $args_ref, $object, $radius ) = @_;
  194. # Grab the args array
  195. my @args = @{$args_ref};
  196. # process the args array
  197. my $args_string = "";
  198. for my $i ( 0 .. $#args ) {
  199. if( $i == 0 ) {
  200. $args_string = $args[$i];
  201. } else {
  202. $args_string = "," . $args[$i];
  203. }
  204. }
  205. # set the "status" command to Aladin
  206. if( $args_string eq "" ) {
  207. unless ( $radius ) {
  208. print "Sending: get $server() $object\n";
  209. print $ALADIN "get $server() $object\n";
  210. } else {
  211. print "Sending: get $server() $object $radius"."arcmin\n";
  212. print $ALADIN "get $server() $object $radius"."arcmin\n";
  213. }
  214. } else {
  215. unless ( $radius ) {
  216. print "Sending: get $server($args_string) $object\n";
  217. print $ALADIN "get $server($args_string) $object\n";
  218. } else {
  219. print "Sending: get $server($args_string) $object $radius"."arcmin\n";
  220. print $ALADIN "get $server($args_string) $object $radius"."arcmin\n";
  221. }
  222. }
  223. }
  224. =end __PRIVATE_METHODS__
  225. =head1 COPYRIGHT
  226. Copyright (C) 2003 University of Exeter. All Rights Reserved.
  227. This program was written as part of the eSTAR project and is free software;
  228. you can redistribute it and/or modify it under the terms of the GNU Public
  229. License.
  230. =head1 AUTHORS
  231. Alasdair Allan E<lt>aa@astro.ex.ac.ukE<gt>,
  232. =cut
  233. # L A S T O R D E R S ------------------------------------------------------
  234. 1;