Guest User

Untitled

a guest
Nov 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use PDL;
  7. use PDL::Image2D;
  8. use PDL::Fit::Gaussian;
  9.  
  10. use Prima;
  11. use PDL::Graphics::Prima::Simple -sequential;
  12.  
  13. use File::chdir;
  14.  
  15. use List::Util;
  16.  
  17. use constant {
  18. pi => 4*atan2(1,1),
  19. wconv => 1 / ( 2 * sqrt( log(2) ) ),
  20. };
  21.  
  22. my $folder = shift;
  23. local $CWD = $folder;
  24.  
  25. my $cache_name = 'data';
  26. my $tag = 'fourier';
  27. my $nd_tag = qr/g(\d+)/;
  28. my $handle_backgrounds = 0;
  29. my $too_large = 900;
  30. my $laser = sub{ sin(pi / 180 * 2 * shift)**2 };
  31.  
  32. my $files;
  33. if ( not -e $cache_name ) {
  34. $files = analyze_folder();
  35.  
  36. # cache data
  37. require Data::Dumper;
  38. open my $fh, '>', $cache_name;
  39. print $fh Data::Dumper::Dumper($files);
  40. } else {
  41. $files = do $cache_name;
  42. }
  43.  
  44. # print for humans
  45. use Data::Printer;
  46. p $files;
  47.  
  48. line_plot(make_piddles($files));
  49.  
  50. sub analyze_folder {
  51.  
  52. # find all fit(s) files
  53. my %files = map { $_, { file => $_ } } do {
  54. opendir( my $dh, $CWD );
  55.  
  56. grep { /\.fits?/ }
  57. readdir $dh;
  58. };
  59.  
  60. # keep only certain tags (e.g. fourier)
  61. if ($tag) {
  62. my @wrong_tag = grep { not /$tag/i } keys %files;
  63. delete @files{@wrong_tag};
  64. }
  65.  
  66. # remove background images
  67. unless ($handle_backgrounds) {
  68. my @bg = grep { /bg/ } keys %files;
  69. delete @files{@bg};
  70. }
  71.  
  72. # parse filenames
  73. foreach (keys %files) {
  74. # polarizer info
  75. if ( /(m?)(\d{2,})/ ) {
  76. $files{$_}{polarizer} = $2 * ($1 ? -1 : 1); # m indicates negative number
  77. $files{$_}{laser} = $laser->($files{$_}{polarizer});
  78. }
  79.  
  80. # nd info
  81. if ( $_ =~ $nd_tag ) {
  82. $files{$_}{nd} = $1;
  83. } else {
  84. $files{$_}{nd} = 0;
  85. }
  86. }
  87.  
  88. foreach (sort {$files{$a}{laser} <=> $files{$b}{laser}} keys %files) {
  89. my @w = analyze($_);
  90. $files{$_}{w} = \@w;
  91. $files{$_}{w_av} = 0.5 * List::Util::sum @w;;
  92. }
  93.  
  94. return \%files;
  95. }
  96.  
  97. sub make_piddles {
  98. my $files = shift;
  99.  
  100. my (@laser, @w_av);
  101. foreach (sort {$files->{$a}{laser} <=> $files->{$b}{laser}} keys %$files) {
  102. next if $files->{$_}{w_av} > $too_large;
  103.  
  104. push @laser, $files->{$_}{laser};
  105. push @w_av, $files->{$_}{w_av};
  106. }
  107.  
  108. my $pdl_laser = pdl \@laser;
  109. my $pdl_w_av = pdl \@w_av;
  110.  
  111. return $pdl_laser, $pdl_w_av;
  112. }
  113.  
  114. sub analyze {
  115. my $file = shift;
  116. return unless $file =~ /\.fits?$/;
  117.  
  118. print "processing file: $file\n";
  119.  
  120. my $pdl = rfits $file;
  121. my ($x_dim, $y_dim) = $pdl->dims;
  122. my ($val, $max_x, $max_y) = $pdl->max2d_ind;
  123.  
  124. my ($wx, $wy);
  125.  
  126. plot(
  127. -data => ds::Grid(
  128. $pdl,
  129. x_bounds => [1,$x_dim],
  130. y_bounds => [1,$y_dim],
  131. plotType => pgrid::Matrix(
  132. palette => pal::BlackToWhite,
  133. ),
  134. ),
  135. -max => ds::Pair(
  136. $max_x, $max_y,
  137. plotTypes => ppair::Crosses,
  138. colors => pdl(255, 0, 0)->rgb_to_color,
  139. ),
  140. onMouseClick => sub {
  141. my ($self, $button, undef, $x, $y) = @_;
  142. my $left = $button & mb::Left;
  143. my $right = $button & mb::Right;
  144.  
  145. return unless ($left or $right);
  146.  
  147. if ( $left ) {
  148. $x = int $self->x->pixels_to_reals($x);
  149. $y = int $self->y->pixels_to_reals($y); #-- #highlight fix
  150. } else {
  151. ($x, $y) = ($max_x, $max_y);
  152. }
  153.  
  154. ($wx, $wy) = xy_fit($pdl, $x, $y);
  155.  
  156. $self->get_parent->close;
  157. },
  158. );
  159.  
  160. return ($wx, $wy);
  161. }
  162.  
  163. sub xy_fit {
  164. my ($pdl, $x, $y) = @_;
  165.  
  166. my $x_lineout = $pdl->slice(",($y)");
  167. my $y_lineout = $pdl->slice("($x),");
  168.  
  169. my (undef, undef, $fwhm_x) = fitgauss1d(
  170. $x_lineout->sequence * 5.4,
  171. $x_lineout,
  172. );
  173.  
  174. my (undef, undef, $fwhm_y) = fitgauss1d(
  175. $y_lineout->sequence * 5.4,
  176. $y_lineout,
  177. );
  178.  
  179. return
  180. map { wconv * $_ }
  181. map {eval{$_->isa('PDL')} ? $_->list : $_}
  182. ($fwhm_x, $fwhm_y);
  183. }
Add Comment
Please, Sign In to add comment