Insolation

 view release on metacpan or  search on metacpan

lib/Insolation.pm  view on Meta::CPAN

      if ($DUSK >= 999999) {
				#--------------------#
				# Daylight at all times at this location on this day
				#--------------------#

				$dawn = 0;
				$dusk = 0;

      } elsif ($DUSK <= -999999) {
				#--------------------#
				# Nightime at all times at this location on this day
				#--------------------#

				$dawn = 0;
				$dusk = 0;

			} else {
				#--------------------#
				# Daylight and nightime at this location on this day
				#--------------------#

				my $DAWN   = (-$DUSK-$EQTIME) * 24 / $self->{CONSTANTS}->{TWOPI} + 12 - $self->{longitude} / 15 + $self->{SCRATCH}->{ITZONE};
				$DUSK   = ( $DUSK-$EQTIME) * 24 / $self->{CONSTANTS}->{TWOPI} + 12 - $self->{longitude} / 15 + $self->{SCRATCH}->{ITZONE};
				my $IDAWNH = int($DAWN);
				my $IDUSKH = int($DUSK);
				my $IDAWNM = int( ($DAWN - $IDAWNH) * 60);
				my $IDUSKM = int( ($DUSK - $IDUSKH) * 60);
				$dawn = sprintf("%02d:%02d", $IDAWNH, $IDAWNM);
				$dusk = sprintf("%02d:%02d", $IDUSKH, $IDUSKM);

			}

			# set the data in the object's data collector - that way you can output it the way you'd like xml,csv,txt,etc
			$self->{data}->{'day'}->{$date}->{'data'} = {
				'dawn'  => $dawn,
				'dusk'  => $dusk,
				'srinc' => sprintf("%.45f", $SRINC),
				'coszs' => sprintf("%.45f", $COSZS),
			};
			#printf "%10s - %5s - %5s - %.40f - %.40f\n", $date, $dawn, $dusk, $SRINC, $COSZS;
    }
  }
}

#----------------------------------------#
# output in xml
#----------------------------------------#
sub get_xml {
	my $self = shift;

	# only require if they use this method
	my $package = 'XML::Simple';
	eval {
		(my $pkg = $package) =~ s|::|/|g; # require need a path
  	require "$pkg.pm";
		import $package;
	};
	die $@ if( $@ );

	my $xs   = XML::Simple->new(
		AttrIndent => 1,
		RootName   => 'Insolation',
		KeyAttr    => [ 'date' => 'name'],
		NoAttr     => 1,
	);

	my $xml = $xs->XMLout($self->{data});
	#print "$xml";

	return $xml;
}

#----------------------------------------#
# output in csv
#----------------------------------------#
sub get_csv {
	my $self = shift;
	my $csv;

	foreach my $date (sort keys %{$self->{data}->{day}}) {
		my $dawn  = $self->{data}->{day}->{$date}->{data}->{dawn};
		my $dusk  = $self->{data}->{day}->{$date}->{data}->{dusk};
		my $srinc = $self->{data}->{day}->{$date}->{data}->{srinc};
		my $coszs = $self->{data}->{day}->{$date}->{data}->{coszs};
		$csv .= sprintf("%s,%s,%s,%5.2f,%.4f\n", $date, $dawn, $dusk, $srinc, $coszs);
	}

	return $csv;
}

#----------------------------------------#
# get the insolation for a computed value
#----------------------------------------#
sub get_ym_insolation {
	my $self  = shift;
	my $ym    = shift;
	my $year  = substr($ym, 0, 4);
	my $month = substr($ym, 5, 2);
	my $total = 0;

	if (! defined($self->{data}->{day}->{$ym . '-01'}->{data}->{srinc})) {
		print "front of the month doesn't exist...\n";
		return 0;
	} else {
		#print "front of the month looks good...\n";
	}

	my $DATMAX = $self->{CONSTANTS}->{DAYzMO}[($month - 1)]; # array indexed at zero instead of 1
	if ( ($month == 2) and ($self->QLEAPY($year)) ) {
		$DATMAX = 29;
	}
	if (! defined($self->{data}->{day}->{$ym . '-' . $DATMAX}->{data}->{srinc})) {
		print "end of the month doesn't exist...\n";
		return 0;
	} else {
		#print "end of the month looks good...\n";
	}

	# compute the month total
	for(my $d = 1; $d <= $DATMAX; $d++) {
		$total += $self->{data}->{day}->{$ym . sprintf("-%02d", $d)}->{data}->{srinc};



( run in 2.532 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )