| 1 | # | ||
| 2 | # Created by BAD2_demo.pm.PL | ||
| 3 | # ** DO NOT EDIT THIS FILE ** | ||
| 4 | # | ||
| 5 | package PDL::Demos::BAD2_demo; | ||
| 6 | use PDL; | ||
| 7 | use PDL::IO::Misc; | ||
| 8 | use PDL::Graphics::PGPLOT; | ||
| 9 | |||
| 10 | use File::Spec; | ||
| 11 | |||
| 12 | PDL::Demos::Routines->import(); | ||
| 13 | sub comment($); | ||
| 14 | sub act($); | ||
| 15 | sub output; | ||
| 16 | |||
| 17 | sub run { | ||
| 18 | |||
| 19 | |||
| 20 | $ENV{PGPLOT_XW_WIDTH}=0.6; | ||
| 21 | $ENV{PGPLOT_DEV}=$^O =~ /MSWin32/ ? '/GW' : "/XSERVE"; | ||
| 22 | |||
| 23 | # try and find m51.fits | ||
| 24 | $d = File::Spec->catdir( "PDL", "Demos" ); | ||
| 25 | $m51path = undef; | ||
| 26 | foreach my $path ( @INC ) { | ||
| 27 | my $check = File::Spec->catdir( $path, $d ); | ||
| 28 | if ( -d $check ) { $m51path = $check; last; } | ||
| 29 | } | ||
| 30 | barf "Unable to find directory ${m51path} within the perl libraries.\n" | ||
| 31 | unless defined $m51path; | ||
| 32 | |||
| 33 | comment q| | ||
| 34 | This demo is just a bit of eye-candy to show bad values in action, | ||
| 35 | and requires PGPLOT support in PDL. It makes use of the image of | ||
| 36 | M51 kindly provided by the Hubble Heritage group at the | ||
| 37 | Space Telescope Science Institute. | ||
| 38 | |||
| 39 | It also serves to demonstrate that you often don't need to change | ||
| 40 | your code to handle bad values, as the routines may 'do it' for you. | ||
| 41 | |||
| 42 | |; | ||
| 43 | |||
| 44 | act q| | ||
| 45 | |||
| 46 | # read in the image ($m51path has been set up by this demo to | ||
| 47 | # contain the location of the file) | ||
| 48 | $m51 = rfits "$m51path/m51.fits"; | ||
| 49 | |||
| 50 | # display it | ||
| 51 | $just = { JUSTIFY => 1 }; | ||
| 52 | imag $m51, $just; | ||
| 53 | |||
| 54 | # These are used to create the next image | ||
| 55 | ( $nx, $ny ) = $m51->dims; | ||
| 56 | $centre = [ $nx/2, $ny/2 ]; | ||
| 57 | |; | ||
| 58 | |||
| 59 | act q| | ||
| 60 | |||
| 61 | # now, let's mask out the central 40 pixels and display it | ||
| 62 | $masked = $m51->setbadif( $m51->rvals({CENTRE=>$centre}) < 40 ); | ||
| 63 | |||
| 64 | # since imag auto-scales the output, the bad values are not displayed | ||
| 65 | imag $masked, $just; | ||
| 66 | |||
| 67 | # compare the statistics of the images | ||
| 68 | # (as $PDL::verbose = 1, stats prints out the answers itself) | ||
| 69 | print "Original:\n"; $m51->stats; | ||
| 70 | print "Masked:\n"; $masked->stats; | ||
| 71 | |||
| 72 | |; | ||
| 73 | |||
| 74 | act q| | ||
| 75 | |||
| 76 | # let's filter it a little bit | ||
| 77 | use PDL::Image2D; | ||
| 78 | $nb = 15; | ||
| 79 | $filtered = med2d $masked, ones($nb,$nb), { Boundary => 'Truncate' }; | ||
| 80 | |||
| 81 | # this is a model of the diffuse component of M51 | ||
| 82 | imag $filtered, $just; | ||
| 83 | |||
| 84 | |; | ||
| 85 | |||
| 86 | act q| | ||
| 87 | |||
| 88 | # unsharp masking, to bring out the small-scale detail | ||
| 89 | $unsharp = $masked - $filtered; | ||
| 90 | |||
| 91 | imag $unsharp, $just; | ||
| 92 | |||
| 93 | |; | ||
| 94 | |||
| 95 | act q| | ||
| 96 | |||
| 97 | # add on some contours showing the large scale structure of the galaxy | ||
| 98 | imag $unsharp, $just; | ||
| 99 | hold; | ||
| 100 | cont $filtered; | ||
| 101 | rel; | ||
| 102 | |||
| 103 | |; | ||
| 104 | |||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 |