#!/usr/bin/perl -w # create_tn_page.pl Create thumbnail page. $version = "V1.03/September 14, 2005"; # September 14, 2005. V1.03. Check for and remove the trailing __ # in the subtitle obtained from the data translation table. # Output only file name of script, not including full file path. rcg # April 27, 2005 V1.02. Restrict color bar files to those that start # with the word colorbar since other files have htat text too. # Add code to better handle no image or data file present. rcg # March 16, 2005 V1.01. Make comparison with pattern cruiseid case # insensitive. [Does not work yet!] rcg # March 10, 2005. R. Groman # Copywrite Woods Hole Oceanographic Institution 2005 # Program to create a thumbnail page of images and links to # corresponding Matlab data files for the broadscale kriged data # Many necessary run-time and configuration values are read in from # $config_param{'directory_root'} # a configuration file specified as the first argument in the command # line. # # create_tn_page.pl ,,, # # or # create_tn_page.pl /my/dir/create_tn_page.conf biovolume TempAvg_aniso TempAvg # Assumptions: # 1. File type for the images are jpg and file type for the # Matlab data files is mat. # 2. The first row (row 0) of templates contains the table header # text. # 3. The first entry of all the template entries contain a table # header text, not a cruiseid. # To handle the fact that more than one key can map to the same value, # use the following code: # # @names = grep /$cruiseid/, @dirnames; # @file_names = @names; # foreach $key (keys %replace) { # if ($replace{$key} =~ m/$cruiseid/i ) { # @names = grep /$key/, @dirnames; # push @file_names, @names; # } # } print STDOUT ("Content-type: text/html\n\n"); $maxrows = 100; unshift (@INC, "/data/rgroman/perllib"); require ('sendmessage.pl'); require ('bob_setup.pl'); require ('bob_unsetup.pl'); require ('read_configuration_file.pl'); require ('read_data_replacement_table.pl'); &bob_setup; @input = split /,/, $ARGV[0]; if (defined $input[0]) { $configuration_file = $input[0]; } else { $configuration_file = undef} if (defined $input[1]) { $subdirectory = $input[1]; } else { $subdirectory = undef} if (defined $input[2]) { $image_file_pattern = $input[2]; } else { $image_file_pattern = undef} if (defined $input[3]) { $matlab_file_pattern = $input[3]; } else { $matlab_file_pattern = undef} @required = ( "data_replacement_table", "thumbnail_subdirectory", "web_root_directory"); &read_configuration_file($configuration_file); $debug = 'no'; if (exists $config_param{'debug'} and defined $config_param{'debug'} ) { if ($config_param{'debug'} =~ m/^y/i or $config_param{'debug'} =~ m/^1/ or $config_param{'debug'} =~ m/^t/i) {; $debug = "yes"; } elsif ($config_param{'debug'} =~ m/^keep/i ) { $debug = "keep"; } } $okay = 'yes'; unless (defined $subdirectory) { &sendmessage ($warning, "Subdirectory not defined as argument", ""); $okay = 'no'; } unless (defined $image_file_pattern ) { &sendmessage ($warning, "Data file pattern not defined as argument", ""); $okay = 'no'; } unless (defined $matlab_file_pattern) { &sendmessage ($warning, "Matlab file pattern not defined as argument", ""); $okay = 'no'; } for ($i=0; $i<=$#required; $i++) { unless (exists $config_param{$required[$i]} ) { $okay="no"; &sendmessage ($warning, "$required[$i] is missing from configuration ", "file=$configuration_file"); } else { print STDOUT ("# $required[$i]=$config_param{$required[$i]}\n") if $debug eq 'yes'; } } if ( $okay eq "no") { &sendmessage ($error, "One or more parameters are missing", "Cannot continue."); } else { $status = &read_data_replacement_table( $config_param{'data_replacement_table'}); if ($status eq 'okay' ) { &process_data; } } &bob_unsetup; undef $error; undef $warning; undef $version; undef $date; exit; #----------------------------------------------------------------- sub process_data { $full_data_dir = $config_param{'data_root_directory'} . '/' . $subdirectory . '/' . $config_param{'thumbnail_subdirectory'}; $full_data_dir =~ s$//$/$g; $full_matlab_files_dir = $config_param{'data_root_directory'} . '/' . $subdirectory; $full_matlab_files_dir =~ s$//$/$g; $full_web_dir = $config_param{'web_root_directory'} . '/' . $subdirectory . '/' . $config_param{'thumbnail_subdirectory'}; $full_web_dir =~ s$//$/$g; $full_web_matlab_dir = $config_param{'web_root_directory'} . '/' . $subdirectory; $full_web_matlab_dir =~ s$//$/$g; print STDOUT ("#***debug, full_data_dir=$full_data_dir

\n") if $debug eq 'yes'; print STDOUT ("#***debug, full_matlab_files_dir=$full_matlab_files_dir

\n") if $debug eq 'yes'; unless (opendir DATA_DIR, $full_data_dir) { &sendmessage($error, "Could not open directory", $full_data_dir); return; } @dirnames = readdir DATA_DIR; closedir DATA_DIR; print STDOUT ("#***debug,dirnames=@dirnames

\n") if $debug eq 'yes' ; print STDOUT ("#***debug,image_file_pattern=$image_file_pattern

\n") if $debug eq 'yes'; @original_image_file_names = grep /$image_file_pattern/, @dirnames; @original_image_file_names = grep /\.jpg/, @original_image_file_names; print STDOUT ("#***debug, original_image_file_names=@original_image_file_names

\n") if $debug eq 'yes'; unless (opendir DATA_DIR, $full_matlab_files_dir) { &sendmessage($error, "Could not open directory", $full_data_dir); return; } @matlab_dirnames = readdir DATA_DIR; closedir DATA_DIR; print STDOUT ("#***debug, matlab_dirnames=@matlab_dirnames

\n") if $debug eq 'yes'; @original_matlab_file_names = grep /$matlab_file_pattern/, @matlab_dirnames; print STDOUT ("#***debug, intermediate original_matlab_file_names=", "@original_matlab_file_names

\n") if $debug eq 'yes'; @original_matlab_file_names = grep /mat/, @original_matlab_file_names; print STDOUT ("#***debug, original_matlab_file_names=@original_matlab_file_names

\n") if $debug eq 'yes'; @colorbars = grep /^colorbar/, @dirnames; @colorbars = grep /\.jpg/, @colorbars; print STDOUT ("#***debug, colorbars=@colorbars

\n") if $debug eq 'yes'; # Output start of web page # has to be more robust to look for matching pieces ala other code print STDOUT ("

#***debug, image_file_pattern=$image_file_pattern

\n") if $debug eq 'yes'; $title = 'Krig Output Thumbnail Images'; if (exists $replace{$image_file_pattern} and defined $replace{$image_file_pattern} ) { $subtitle = $replace{$image_file_pattern}; print STDOUT ("

#***debug, subtitle=$subtitle

\n") if $debug eq 'yes'; $subtitle =~ s/__\d{1,}$//; $subtitle =~ s/_/ /g; $title = $title . '
for
' . $subtitle; } print STDOUT ("\n", "", $title, "\n\n", "

$title

\n", "\n\n\n"); print STDOUT ("\n","

Note: On some browsers, hold the shift key down ", "while clicking on the \"Matlab file\" link in order to ", "download the Matlab data file.

\n"); # Output table header unless (exists $config_param{'template_0'} and defined $config_param{'template_0'} ) { &sendmessage($error,"No header record defined in configuration file", "cannot continue"); return; } print STDOUT ("\n

"); @entries = split /,/, $config_param{'template_0'}; for ($i=0; $i<=$#entries; $i++) { $entries[$i] =~ s/\s//g; print STDOUT (" "); } print STDOUT ("\n"); # Extract out appropriate files based on cruiseid information from the # input template. for ($i=1; $i<=$maxrows; $i++) { unless (exists $config_param{"template_$i"} and defined $config_param{"template_$i"} ) { print STDOUT ("

#***debug, last template entry at i=$i

\n") if $debug eq 'yes'; last; } print STDOUT ("

#***debug, template_$i=",$config_param{"template_$i"}, "
\n") if $debug eq 'yes'; print STDOUT ("\n

"); @entries = split /,/, $config_param{"template_$i"}; $entries[0] =~ s/\s//g; print STDOUT (" "); for ($j=1; $j<=$#entries; $j++) { $entries[$j] =~ s/\s//g; $cruiseid = $entries[$j]; print STDOUT ("\n

#***debug, j=$j, cruiseid=$cruiseid

\n") if $debug eq 'yes'; unless ($cruiseid eq 'nd' ) { @image_file_names = grep /$cruiseid/, @original_image_file_names; @matlab_file_names = grep /$cruiseid/, @original_matlab_file_names; } else { $image_file_names[0] = 'nd'; $matlab_file_names[0] = 'nd'; } foreach $key (keys %replace) { if ($replace{$key} =~ m/$cruiseid/i ) { print STDOUT ( "

#***debug, replace{$key}=", "$replace{$key}
\n") if $debug eq 'yes'; @names = grep /$key/, @original_image_file_names; push @image_file_names, @names; @names = grep /$key/, @original_matlab_file_names; push @matlab_file_names, @names; } } # unless ($#image_file_names <= 0 and $#matlab_file_names <= 0) { # &sendmessage($warning, # "There are more than one image or matlab files found", # "For cruiseid $cruiseid in $subdirectory"); # return 'no good'; # } print STDOUT ("

#***debug, matlab_file_names=@matlab_file_names", "

\n") if $debug eq 'yes'; $image_exists = 'yes'; $matlab_exists = 'yes'; if ($#image_file_names < 0 or $image_file_names[0] eq 'nd') { $image_exists = 'no'; $image_file_names[0] = 'nd'; } if ($#matlab_file_names < 0 or $matlab_file_names[0] eq 'nd') { $matlab_exists = 'no'; $matlab_file_names[0] = 'no'; } $image_file_names[0] = $full_web_dir . '/' . $image_file_names[0]; print STDOUT ("

****debug, image_file_names[0]=$image_file_names[0]

\n") if $debug eq 'yes'; $original_image = $image_file_names[0]; $original_image =~ s!/$config_param{'thumbnail_subdirectory'}/!/!; $matlab_file_names[0] = $full_web_matlab_dir . '/' . $matlab_file_names[0]; if ($image_exists eq 'yes') { print STDOUT ("

"); } else { print STDOUT ("
No Matlab file"); } } print STDOUT ("

\n"); } print STDOUT ("\n

$entries[$i]
$entries[0] "); } else { print STDOUT ("No data"); } if ($matlab_exists eq 'yes') { print STDOUT ("
Matlab file
\n\n

"); $include_colorbar = 'no'; if (exists $config_param{'include_colorbar'} and defined $config_param{'include_colorbar'} ) { if ($config_param{'include_colorbar'} =~ m/y/i or $config_param{'include_colorbar'} eq '1') { $include_colorbar = 'yes'; } } foreach $file (@colorbars) { $colorbar = $full_web_dir . '/' . $file; $original_colorbar = $colorbar; $original_colorbar =~ s!/$config_param{'thumbnail_subdirectory'}/!/!; print STDOUT ("
$file

\n") if $include_colorbar eq 'yes'; } $script = $0; $script =~ s/.*\/(.*)/$1/; print STDOUT ("


From $script - Version: $version\n
"); print STDOUT ( "Configuration file is $configuration_file\n
") if defined $configuration_file and $debug eq 'yes'; print STDOUT ("Date of run: ", $date, "\n
") if $debug eq 'yes'; print STDOUT ("Using subdirectory $subdirectory\n
") if defined $subdirectory; print STDOUT ("
\n\n"); }