blib/lib/PDL/Demos/Transform_demo.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 68 CC 5 SCAR 28.7
CC
5
Cov
0%
run
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 17.6
1 #
2 package PDL::Demos::Transform_demo;
3  
4 use PDL;
5 use PDL::Graphics::PGPLOT::Window;
6 use PDL::Transform;
7  
8 use File::Spec;
9  
10 PDL::Demos::Routines->import();
11 sub comment($);
12 sub act($);
13 sub output;
14  
15 sub run {
16 local($PDL::debug) = 0;
17 local($PDL::verbose) = 0;
18  
19 ##$ENV{PGPLOT_XW_WIDTH}=0.6;
20 $ENV{PGPLOT_DEV} = $^O =~ /MSWin32/ ? '/GW' :
21 defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XWIN";
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 illustrates the PDL::Transform module.
35  
36 It requires PGPLOT support in PDL and makes use of the image of
37 M51 kindly provided by the Hubble Heritage group at the
38 Space Telescope Science Institute.
39  
40 |;
41  
42 act q|
43 # PDL::Transform objects embody coordinate transformations.
44  
45 use PDL::Transform;
46  
47 # set up a simple linear scale-and-shift relation
48
49 $t = t_linear( Scale=>[2,-1], Post=>[100,0]);
50 print $t;
51 |;
52  
53 act q|
54 # The simplest way to use PDL::Transform is to transform a set of
55 # vectors. To do this you use the "apply" method.
56  
57 # Define a few 2-vectors:
58 $xy = pdl([[0,1],[1,2],[10,3]]);
59 print "xy: ", $xy;
60  
61 # Transform the 2-vectors:
62 print "Transformed: ", $xy->apply( $t );
63 |;
64  
65 act q|
66 # You can invert and compose transformations with 'x' and '!'.
67 $u = t_linear( Scale=>10 ); # A new transformation (simple x10 scale)
68 $xy = pdl([[0,1],[10,3]]); # Two 2-vectors
69 print "xy: ", $xy;
70 print "xy': ", $xy->apply( !$t ); # Invert $t from earlier.
71 print "xy'': ", $xy->apply( $u x !$t ); # Hit the result with $u.
72 |;
73  
74 act q|
75 # PDL::Transform is useful for data resampling, and that's perhaps
76 # the best way to demonstrate it. First, we do a little bit of prep work:
77  
78 # Read in an image ($m51path has been set up by this demo to
79 # contain the location of the file). Transform is designed to
80 # work well with FITS images that contain WCS scientific coordinate
81 # information, but works equally well in pixel space.
82  
83 $m51 = rfits("$m51path/m51.fits",{hdrcpy=>1});
84
85 # we use a floating-point version of the image in some of the demos
86 # to highlight the interpolation schemes. (Note that the FITS
87 # header gets deep-copied automatically into the new variable).
88  
89 $m51_fl = $m51->float;
90  
91 # Define a nice, simple scale-by-3 transformation.
92  
93 $ts = t_scale(3);
94  
95 |;
96  
97 act q|
98 #### Resampling with ->map and no FITS interpretation works in pixel space.
99  
100 ### Create a PGPLOT window, and display the original image
101 $dev = $^O =~ /MSWin32/ ? '/GW' :
102 defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XW";
103 $win = pgwin( dev=> $dev, nx=>2, ny=>2, Charsize=>2, J=>1, Size=>[8,6] );
104  
105 $win->imag( $m51 , { DrawWedge=>0, Title=>"M51" } );
106  
107 ### Grow m51 by a factor of 3; origin is at lower left
108 # (the "pix" makes the resampling happen in pixel coordinate
109 # space, ignoring the FITS header)
110  
111 $win->imag( $m51->map( $ts, {pix=>1} ) );
112 $win->label_axes("","","M51 grown by 3 (pixel coords)");
113  
114 ### Shrink m51 by a factor of 3; origin still at lower left.
115 # (You can invert the transform with a leading '!'.)
116  
117 $win->imag( $m51->map( !$ts, {pix=>1} ) );
118 $win->label_axes("","","M51 shrunk by 3 (pixel coords)");
119  
120 |;
121  
122 act q|
123 # You can work in scientific space (or any other space) by
124 # wrapping your main transformation with something that translates
125 # between the coordinates you want to act in, and the coordinates
126 # you have. Here, "t_fits" translates between pixels in the data
127 # and arcminutes in the image plane.
128  
129 ### Clear the panel and start over
130  
131 $win->panel(4); # (Clear whole window on next plot)
132 $win->imag( $m51, { Title=>"M51" } );
133  
134 ### Scale in scientific coordinates.
135 # Here's a way to scale in scientific coordinates:
136 # wrap our transformation in FITS-header transforms to translate
137 # the transformation into scientific space.
138  
139 $win->imag( $m51->map( !$ts->wrap(t_fits($m51)), {pix=>1} ) );
140 $win->label_axes("","","M51 shrunk 3x (sci. coords)");
141  
142 |;
143  
144 act q|
145 # If you don't specify "pix=>1" then the resampler works in scientific
146 # FITS coordinates (if the image has a FITS header):
147  
148 ### Scale in scientific coordinates (origin at center of galaxy)
149 $win->fits_imag( $m51->map( $ts, $m51->hdr ), { Title=>"M51 3x" } );
150  
151 ### Instead of setting up a coordinate transformation you can use the
152 # implicit FITS header matching. Just tweak the template header:
153 $tohdr = $m51->hdr_copy;
154 $tohdr->{CDELT1} /= 3; # Magnify 3x in horiz direction
155 $tohdr->{CDELT2} /= 3; # Magnify 3x in vert direction
156
157 ### Resample to match the new FITS header
158 # (Note that, although the image is scaled exactly the same as before,
159 # this time the scientific coordinates have scaled too.)
160 $win->fits_imag( $m51->map( t_identity(), $tohdr ), { Title=>"3x (FITS)" } );
161 |;
162  
163 act q|
164 ### The three main resampling methods are "sample", "linear", and "jacobian".
165 # Sampling is fastest, linear interpolation is better. Jacobian resampling
166 # is slow but prevents aliasing under skew or reducing transformations.
167
168 $win->fits_imag( $m51_fl , {Title=>"M51"} );
169  
170 $win->fits_imag( $m51_fl->map( $ts, $m51_fl, { method=>"sample" } ),
171 {Title=>"M51 x3 (sampled)"} );
172  
173 $win->fits_imag( $m51_fl->map( $ts, $m51_fl, { method=>"linear" } ),
174 { Title=>"M51 x3 (interp.)"} );
175  
176 $win->fits_imag( $m51_fl->map( $ts, $m51_fl, { method=>"jacobian" } ),
177 { Title=>"M51 x3 (jacob.)"} );
178  
179 |;
180  
181 act q|
182 ### Linear transformations are only the beginning. Here's an example
183 # using a simple nonlinear transformation: radial coordinate transformation.
184  
185 ### Original image
186 $win->fits_imag( $m51 ,{Title=>"M51"});
187
188 ### Radial structure in M51 (linear radial scale; origin at (0,0) by default)
189 $tu = t_radial( u=>'degree' );
190 $win->fits_imag( $m51_fl->map($tu), { Title=>"M51 radial (linear)", J=>0});
191  
192 ### Radial structure in M51 (conformal/logarithmic radial scale)
193 $tu_c = t_radial( r0=>0.1 ); # Y axis 0 is at 0.1 arcmin
194 $win->panel(3);
195 $win->fits_imag( $m51_fl->map($tu_c),
196 { Title=>"M51 radial (conformal)",
197 YRange=>[0,4] } );
198  
199  
200 |;
201 # NOTE:
202 # need to 'double protect' the \ in the label_axes()
203 # since it's being evaluated twice (I think)
204 #
205  
206  
207 act q|
208  
209 #####################
210 # Wrapping transformations allows you to work in a convenient
211 # space for what you want to do. Here, we can use a simple
212 # skew matrix to find (and remove) logarithmic spiral structures in
213 # the galaxy. The "unspiraled" images shift the spiral arms into
214 # approximate straight lines.
215  
216 $sp = 3.14159; # Skew by 3.14159
217
218 # Skew matrix
219 $t_skew = t_linear(pre => [$sp * 130, 0] , matrix => pdl([1,0],[-$sp,1]));
220
221 # When put into conformal radial space, the skew turns into 3.14159
222 # radians per scale height.
223 $t_untwist = t_wrap($t_skew, $tu_c);
224  
225 # Press enter to see the result of these transforms...
226 |;
227  
228 act q|
229 ##############################
230 # Note that you can use ->map and ->unmap as either PDL methods
231 # or transform methods; what to do is clear from context.
232  
233 # Original image
234 $win->fits_imag($m51, {Title => "M51"} );
235  
236 # Skewed
237 $win->fits_imag( $m51_fl->map( $t_skew ),
238 { Title => "M51 skewed by \\\\gp in spatial coords" } );
239  
240 # Untwisted -- show that m51 has a half-twist per scale height
241 $win->fits_imag( $m51_fl->map( $t_untwist ),
242 { Title => "M51 unspiraled (\\\\gp / r\\\\ds\\\\u)"} );
243  
244 # Untwisted -- the jacobean method uses variable spatial filtering
245 # to eliminate spatial artifacts, at significant computational cost
246 # (This may take some time to complete).
247 $win->fits_imag( $m51_fl->map( $t_untwist, {m=>jacobean}),
248 { Title => "M51 unspiraled (\\\\gp / r\\\\ds\\\\u; antialiased)" } );
249 |;
250  
251  
252 $win->close;
253  
254 act q|
255 ### Native FITS interpretation makes it easy to view your data in
256 ### your preferred coordinate system. Here we zoom in on a 0.2x0.2
257 ### arcmin region of M51, sampling it to 100x100 pixels resolution.
258
259 $m51 = float $m51;
260 $data = $m51->match([100,100],{or=>[[-0.05,0.15],[-0.05,0.15]]});
261 $s = "M51 closeup ("; $ss=" coords)";
262 $ps = " (pixels)";
263  
264 $dev = $^O =~ /MSWin32/ ? '/GW' :
265 defined($ENV{PGPLOT_DEV}) ? $ENV{PGPLOT_DEV} : "/XW";
266 $w1 = pgwin( dev=> $dev, size=>[4,4], charsize=>1.5, justify=>1 );
267 $w1->imag( $data, 600, 750, { title=>"${s}pixel${ss}",
268 xtitle=>"X$ps", ytitle=>"Y$ps" } );
269 $w1->hold;
270  
271 $w2 = pgwin( dev=> $dev, size=>[4,4], charsize=>1.5, justify=>1 );
272 $w2->fits_imag( $data, 600, 750, { title=>"${s}sci.${ss}", dr=>0 } );
273 $w2->hold;
274  
275 # Now please separate the two X windows on your screen, and press ENTER.
276 ###############################
277 |;
278  
279 act q|
280 ### Now rotate the image 360 degrees in 10 degree increments.
281 ### The 'match' method resamples $data to the rotated scientific
282 ### coordinate system in $hdr. The "pixel coordinates" window shows
283 ### the resampled data in their new pixel coordinate system.
284 ### The "sci. coordinates" window shows the data remaining fixed in
285 ### scientific space, even though the pixels that represent them are
286 ### moving and rotating.
287  
288 $hdr = $data->hdr_copy;
289
290 for( $rot=0; $rot<=360; $rot += 10 ) {
291 $hdr->{CROTA2} = $rot;
292  
293 $d = $data->match($hdr);
294
295 $w1->imag( $d, 600, 750 );
296 $w2->fits_imag($d, 600, 750, {dr=>0});
297 }
298 |;
299  
300 act q|
301 ### You can do the same thing even with nonsquare coordinates.
302 ### Here, we resample the same region in scientific space into a
303 ### 150x50 pixel array.
304
305 $data = $m51->match([150,50],{or=>[[-0.05,0.15],[-0.05,0.15]]});
306 $hdr = $data->hdr_copy;
307  
308 $w1->release;
309 $w1->imag( $data, 600, 750, { title=>"${s}pixel${ss}",
310 xtitle=>"X$ps", ytitle=>"Y$ps", pix=>1 } );
311 $w1->hold;
312  
313 for( $rot=0; $rot<=750; $rot += 5 ) {
314 $hdr->{CROTA2} = $rot;
315 $d = $data->match($hdr);
316 $w1->imag($d, 600, 750); $w2->fits_imag($d, 600, 750, {dr=>0});
317 }
318  
319 |;
320  
321  
322 comment q|
323  
324 This concludes the PDL::Transform demo.
325  
326 Be sure to check the documentation for PDL::Transform::Cartography,
327 which contains common perspective and mapping coordinate systems
328 that are useful for work on the terrestrial and celestial spheres,
329 as well as other planets &c.
330  
331 |;
332  
333 $w1->release; $w1->close; undef $w1;
334 $w2->release; $w2->close; undef $w2;
335 undef $win;
336 }
337  
338 1;