<commit_before>package Download;

#use strict;
use PBDBUtil;
use Classification;
use TimeLookup;
use Globals;
use DBTransactionManager;

# Flags and constants
my $DEBUG=0;			# The debug level of the calling program
my $dbh;				# The database handle
my $dbt;				# The new and improved database object
my $q;					# Reference to the parameters
my $s;					# Reference to the session data
my $sql;				# Any SQL string
my $rs;					# Generic recordset
my @errors;           # Possible errors in user input
$|=1;

# These arrays contain names of possible fields to be checked by a user in the
# download form.  When writing the data out to files, these arrays are compared
# to the query params to determine the file header line and then the data to
# be written out. 
my @collectionsFieldNames = qw(authorizer enterer modifier collection_no collection_subset reference_no collection_name collection_aka country state county latdeg latmin latsec latdir latdec lngdeg lngmin lngsec lngdir lngdec latlng_basis paleolatdeg paleolatmin paleolatsec paleolatdir paleolatdec paleolngdeg paleolngmin paleolngsec paleolngdir paleolngdec altitude_value altitude_unit geogscale geogcomments period epoch 10mybin max_interval_no min_interval_no emlperiod_max period_max emlperiod_min period_min emlepoch_max epoch_max emlepoch_min epoch_min emlintage_max intage_max emlintage_min intage_min emllocage_max locage_max emllocage_min locage_min zone research_group geological_group formation member localsection localbed localorder regionalsection regionalbed regionalorder stratscale stratcomments lithdescript lithadj lithification lithology1 fossilsfrom1 lithology2 fossilsfrom2 environment tectonic_setting pres_mode geology_comments collection_type collection_coverage collection_meth collection_size collection_size_unit museum collection_comments taxonomy_comments created modified release_date access_level lithification2 lithadj2 rock_censused_unit rock_censused spatial_resolution temporal_resolution feed_pred_traces encrustation bioerosion fragmentation sorting dissassoc_minor_elems dissassoc_maj_elems art_whole_bodies disart_assoc_maj_elems seq_strat lagerstatten concentration orientation preservation_quality abund_in_sediment sieve_size_min sieve_size_max assembl_comps taphonomy_comments);
my @occurrencesFieldNames = qw(authorizer enterer modifier occurrence_no genus_reso genus_name subgenus_reso subgenus_name species_reso species_name abund_value abund_unit reference_no comments created modified plant_organ plant_organ2);
my @reidentificationsFieldNames = qw(authorizer enterer modifier reid_no genus_reso genus_name subgenus_reso subgenus_name species_reso species_name reference_no comments created modified modified_temp plant_organ);
my @refsFieldNames = qw(authorizer enterer modifier reference_no author1init author1last author2init author2last otherauthors pubyr reftitle pubtitle pubvol pubno firstpage lastpage created modified publication_type comments project_name project_ref_no);
my @paleozoic = qw(cambrian ordovician silurian devonian carboniferous permian);
my @mesoCenozoic = qw(triassic jurassic cretaceous tertiary);

my %ecotaph = ();

my $csv;
my $OUT_HTTP_DIR = "/paleodb/data";
my $OUT_FILE_DIR = $ENV{DOWNLOAD_OUTFILE_DIR};
my $DATAFILE_DIR = $ENV{DOWNLOAD_DATAFILE_DIR};
my $outFileBaseName;

my $bestbothscale;

sub new {
	my $class = shift;
	$dbh = shift;
	$dbt = shift;
	$q = shift;
	$s = shift;
    $hbo = shift;
	my $self = {'dbh'=>$dbh,'dbt'=>$dbt,'q'=>$q,'s'=>$s,'hbo'=>$hbo};

	bless $self, $class;
	return $self;
}

# Main handling routine
sub buildDownload {
	my $self = shift;

	print "
	<center>
	<h2>The Paleobiology Database: Download Results</h2>
	</center>";
    

	$self->setupOutput ( );

	$self->retellOptions ( );

	if ( $q->param('time_scale') )	{
		$self->getTimeLookup ( );
	}
    
	if ( $q->param('ecology1') )	{
		$self->getEcology ( );
	}
    
	if ( $q->param('compendium_ranges') eq 'NO' )	{
		$self->getCompendiumAgeRanges ( );
	}

	$self->doQuery ( );
}


# Prints out the options which the user selected in summary form.
sub retellOptions {
	my $self = shift;

	my $html = "
	<center>
	<table border='0' width='600'>
	<tr>
	<td colspan='2' class='darkList'><b><font size='+1'>Download criteria</font></b></td>
	</tr>";

	# authorizer added 30.6.04 JA (left out by mistake?) 
	$html .= $self->retellOptionsRow ( "Authorizer", $q->param("authorizer") );
	$html .= $self->retellOptionsRow ( "Output data type", $q->param("output_data") );
	$html .= $self->retellOptionsRow ( "Output data format", $q->param("output_format") );
	$html .= $self->retellOptionsRow ( "Research group or project", $q->param("research_group") );
    
	# added by rjp on 12/30/2003
	if ($q->param('year')) {
		my $dataCreatedBeforeAfter = $q->param("created_before_after") . " " . $q->param("date") . " " . $q->param("month") . " " . $q->param("year");
		$html .= $self->retellOptionsRow ( "Data records created", $dataCreatedBeforeAfter);
	}
	
	# JA 31.8.04
	if ($q->param('pubyr')) {
		my $dataPublishedBeforeAfter = $q->param("published_before_after") . " " . $q->param("pubyr");
		$html .= $self->retellOptionsRow ( "Data records published", $dataPublishedBeforeAfter);
	}
	if ( $q->param("taxon_name") !~ /[ ,]/ )	{
		$html .= $self->retellOptionsRow ( "Taxon name", $q->param("taxon_name") );
	} else	{
		$html .= $self->retellOptionsRow ( "Taxon names", $q->param("taxon_name") );
	}
	$html .= $self->retellOptionsRow ( "Class", $q->param("class") );

    if ($q->param("max_interval_name")) {
    	$html .= $self->retellOptionsRow ( "Oldest interval", $q->param("max_eml_interval") . " " . $q->param("max_interval_name") );
    }
    if ($q->param("min_interval_name")) { 
	    $html .= $self->retellOptionsRow ( "Youngest interval", $q->param("min_eml_interval") . " " .$q->param("min_interval_name") );
    }

    # Lithologies or lithology
    if ( $q->param("lithology1") ) {
        $html .= $self->retellOptionsRow("Lithology: ", $q->param("include_exclude_lithology1") . " " .$q->param("lithology1"));
    } else {
        my $liths;
        if ( ! $q->param("lithology_carbonate") || ! $q->param("lithology_mixed") ||
             ! $q->param("lithology_siliciclastic") || ! $q->param("lithology_unknown") ){
            my $liths;
            if ( $q->param("lithology_carbonate") )	{
                $liths = "carbonate";
            }
            if ( $q->param("lithology_mixed") )	{
                $liths .= ", mixed";
            }
            if ( $q->param("lithology_siliciclastic") )	{
                $liths .= ", siliciclastic";
            }
            if ( $q->param("lithology_unknown") ) {
                $liths .= ", unknown";
            }    
            $liths =~ s/^, //;
            $html .= $self->retellOptionsRow ( "Lithologies", $liths );
        }
    }

    # Environment or environments
    if ( $q->param('environment') ) {
    	$html .= $self->retellOptionsRow ( "Environment", $q->param("include_exclude_environment") . " " .$q->param("environment") );
    } else {
        if (! $q->param("environment_carbonate") || ! $q->param("environment_unknown") ||
            ! $q->param("environment_siliciclastic") || ! $q->param("environment_terrestrial")) {
            my $envs;
            if ( $q->param("environment_carbonate") ) {
                $envs .= "carbonate";
            }
            if ( $q->param("environment_siliciclastic") ) {
                $envs .= ", siliciclastic";
            }
            if ( $q->param("environment_terrestrial") ) {
                $envs .= ", terrestrial";
            }
            if ( $q->param("environment_unknown") ) {
                $envs = ", unknown";
            }    
            $envs =~ s/^,//;
            $html .= $self->retellOptionsRow( "Environments", $envs);
        }
    }    

    # Collection types
    if (! $q->param('collection_type_archaeological') || ! $q->param('collection_type_biostratigraphic') ||
        ! $q->param('collection_type_paleoecologic') || ! $q->param('collection_type_taphonomic') || 
        ! $q->param('collection_type_taxonomic') || ! $q->param('collection_type_general_faunal/floral') ||
        ! $q->param('collection_type_unknown')) {
        my $colltypes;
        if ($q->param('collection_type_archaeological')) {
            $colltypes .= "archaeological";
        }
        if ($q->param('collection_type_biostratigraphic')) {
            $colltypes .= ", biostratigraphic";
        }
        if ($q->param('collection_type_paleoecologic')) {
            $colltypes .= ", paleoecologic";
        }
        if ($q->param('collection_type_taphonomic')) {
            $colltypes .= ", taphonomic";
        }<commit_msg>slight bug in displaying environments in retellOptions<commit_after>package Download;

#use strict;
use PBDBUtil;
use Classification;
use TimeLookup;
use Globals;
use DBTransactionManager;

# Flags and constants
my $DEBUG=0;			# The debug level of the calling program
my $dbh;				# The database handle
my $dbt;				# The new and improved database object
my $q;					# Reference to the parameters
my $s;					# Reference to the session data
my $sql;				# Any SQL string
my $rs;					# Generic recordset
my @errors;           # Possible errors in user input
$|=1;

# These arrays contain names of possible fields to be checked by a user in the
# download form.  When writing the data out to files, these arrays are compared
# to the query params to determine the file header line and then the data to
# be written out. 
my @collectionsFieldNames = qw(authorizer enterer modifier collection_no collection_subset reference_no collection_name collection_aka country state county latdeg latmin latsec latdir latdec lngdeg lngmin lngsec lngdir lngdec latlng_basis paleolatdeg paleolatmin paleolatsec paleolatdir paleolatdec paleolngdeg paleolngmin paleolngsec paleolngdir paleolngdec altitude_value altitude_unit geogscale geogcomments period epoch 10mybin max_interval_no min_interval_no emlperiod_max period_max emlperiod_min period_min emlepoch_max epoch_max emlepoch_min epoch_min emlintage_max intage_max emlintage_min intage_min emllocage_max locage_max emllocage_min locage_min zone research_group geological_group formation member localsection localbed localorder regionalsection regionalbed regionalorder stratscale stratcomments lithdescript lithadj lithification lithology1 fossilsfrom1 lithology2 fossilsfrom2 environment tectonic_setting pres_mode geology_comments collection_type collection_coverage collection_meth collection_size collection_size_unit museum collection_comments taxonomy_comments created modified release_date access_level lithification2 lithadj2 rock_censused_unit rock_censused spatial_resolution temporal_resolution feed_pred_traces encrustation bioerosion fragmentation sorting dissassoc_minor_elems dissassoc_maj_elems art_whole_bodies disart_assoc_maj_elems seq_strat lagerstatten concentration orientation preservation_quality abund_in_sediment sieve_size_min sieve_size_max assembl_comps taphonomy_comments);
my @occurrencesFieldNames = qw(authorizer enterer modifier occurrence_no genus_reso genus_name subgenus_reso subgenus_name species_reso species_name abund_value abund_unit reference_no comments created modified plant_organ plant_organ2);
my @reidentificationsFieldNames = qw(authorizer enterer modifier reid_no genus_reso genus_name subgenus_reso subgenus_name species_reso species_name reference_no comments created modified modified_temp plant_organ);
my @refsFieldNames = qw(authorizer enterer modifier reference_no author1init author1last author2init author2last otherauthors pubyr reftitle pubtitle pubvol pubno firstpage lastpage created modified publication_type comments project_name project_ref_no);
my @paleozoic = qw(cambrian ordovician silurian devonian carboniferous permian);
my @mesoCenozoic = qw(triassic jurassic cretaceous tertiary);

my %ecotaph = ();

my $csv;
my $OUT_HTTP_DIR = "/paleodb/data";
my $OUT_FILE_DIR = $ENV{DOWNLOAD_OUTFILE_DIR};
my $DATAFILE_DIR = $ENV{DOWNLOAD_DATAFILE_DIR};
my $outFileBaseName;

my $bestbothscale;

sub new {
	my $class = shift;
	$dbh = shift;
	$dbt = shift;
	$q = shift;
	$s = shift;
    $hbo = shift;
	my $self = {'dbh'=>$dbh,'dbt'=>$dbt,'q'=>$q,'s'=>$s,'hbo'=>$hbo};

	bless $self, $class;
	return $self;
}

# Main handling routine
sub buildDownload {
	my $self = shift;

	print "
	<center>
	<h2>The Paleobiology Database: Download Results</h2>
	</center>";
    

	$self->setupOutput ( );

	$self->retellOptions ( );

	if ( $q->param('time_scale') )	{
		$self->getTimeLookup ( );
	}
    
	if ( $q->param('ecology1') )	{
		$self->getEcology ( );
	}
    
	if ( $q->param('compendium_ranges') eq 'NO' )	{
		$self->getCompendiumAgeRanges ( );
	}

	$self->doQuery ( );
}


# Prints out the options which the user selected in summary form.
sub retellOptions {
	my $self = shift;

	my $html = "
	<center>
	<table border='0' width='600'>
	<tr>
	<td colspan='2' class='darkList'><b><font size='+1'>Download criteria</font></b></td>
	</tr>";

	# authorizer added 30.6.04 JA (left out by mistake?) 
	$html .= $self->retellOptionsRow ( "Authorizer", $q->param("authorizer") );
	$html .= $self->retellOptionsRow ( "Output data type", $q->param("output_data") );
	$html .= $self->retellOptionsRow ( "Output data format", $q->param("output_format") );
	$html .= $self->retellOptionsRow ( "Research group or project", $q->param("research_group") );
    
	# added by rjp on 12/30/2003
	if ($q->param('year')) {
		my $dataCreatedBeforeAfter = $q->param("created_before_after") . " " . $q->param("date") . " " . $q->param("month") . " " . $q->param("year");
		$html .= $self->retellOptionsRow ( "Data records created", $dataCreatedBeforeAfter);
	}
	
	# JA 31.8.04
	if ($q->param('pubyr')) {
		my $dataPublishedBeforeAfter = $q->param("published_before_after") . " " . $q->param("pubyr");
		$html .= $self->retellOptionsRow ( "Data records published", $dataPublishedBeforeAfter);
	}
	if ( $q->param("taxon_name") !~ /[ ,]/ )	{
		$html .= $self->retellOptionsRow ( "Taxon name", $q->param("taxon_name") );
	} else	{
		$html .= $self->retellOptionsRow ( "Taxon names", $q->param("taxon_name") );
	}
	$html .= $self->retellOptionsRow ( "Class", $q->param("class") );

    if ($q->param("max_interval_name")) {
    	$html .= $self->retellOptionsRow ( "Oldest interval", $q->param("max_eml_interval") . " " . $q->param("max_interval_name") );
    }
    if ($q->param("min_interval_name")) { 
	    $html .= $self->retellOptionsRow ( "Youngest interval", $q->param("min_eml_interval") . " " .$q->param("min_interval_name") );
    }

    # Lithologies or lithology
    if ( $q->param("lithology1") ) {
        $html .= $self->retellOptionsRow("Lithology: ", $q->param("include_exclude_lithology1") . " " .$q->param("lithology1"));
    } else {
        my $liths;
        if ( ! $q->param("lithology_carbonate") || ! $q->param("lithology_mixed") ||
             ! $q->param("lithology_siliciclastic") || ! $q->param("lithology_unknown") ){
            my $liths;
            if ( $q->param("lithology_carbonate") )	{
                $liths = "carbonate";
            }
            if ( $q->param("lithology_mixed") )	{
                $liths .= ", mixed";
            }
            if ( $q->param("lithology_siliciclastic") )	{
                $liths .= ", siliciclastic";
            }
            if ( $q->param("lithology_unknown") ) {
                $liths .= ", unknown";
            }    
            $liths =~ s/^, //;
            $html .= $self->retellOptionsRow ( "Lithologies", $liths );
        }
    }

    # Environment or environments
    if ( $q->param('environment') ) {
    	$html .= $self->retellOptionsRow ( "Environment", $q->param("include_exclude_environment") . " " .$q->param("environment") );
    } else {
        if (! $q->param("environment_carbonate") || ! $q->param("environment_unknown") ||
            ! $q->param("environment_siliciclastic") || ! $q->param("environment_terrestrial")) {
            my $envs;
            if ( $q->param("environment_carbonate") ) {
                $envs .= "carbonate";
            }
            if ( $q->param("environment_siliciclastic") ) {
                $envs .= ", siliciclastic";
            }
            if ( $q->param("environment_terrestrial") ) {
                $envs .= ", terrestrial";
            }
            if ( $q->param("environment_unknown") ) {
                $envs .= ", unknown";
            }    
            $envs =~ s/^,//;
            $html .= $self->retellOptionsRow( "Environments", $envs);
        }
    }    

    # Collection types
    if (! $q->param('collection_type_archaeological') || ! $q->param('collection_type_biostratigraphic') ||
        ! $q->param('collection_type_paleoecologic') || ! $q->param('collection_type_taphonomic') || 
        ! $q->param('collection_type_taxonomic') || ! $q->param('collection_type_general_faunal/floral') ||
        ! $q->param('collection_type_unknown')) {
        my $colltypes;
        if ($q->param('collection_type_archaeological')) {
            $colltypes .= "archaeological";
        }
        if ($q->param('collection_type_biostratigraphic')) {
            $colltypes .= ", biostratigraphic";
        }
        if ($q->param('collection_type_paleoecologic')) {
            $colltypes .= ", paleoecologic";
        }
        if ($q->param('collection_type_taphonomic')) {
            $colltypes .= ", taphonomic";
        }
{'fraction_non_alphanumeric': 0.13510260231571708, 'fraction_numerical': 0.005158775650578929, 'mean_word_length': 3.6525333333333334, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 199, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2714089', 'n_tokens_mistral': 5849, 'n_tokens_neox': 5564, 'n_words': 1238}