bin/epw2wea.pl
author Axel Jacobs <axel@jaloxa.eu>
Tue, 11 Mar 2014 00:28:22 +0000
changeset 59 626ea3bad930
parent 49 66e192fe711e
permissions -rwxr-xr-x
man pages for ltpict and ltview

#!/usr/bin/perl
#
# Convert EnergyPlus EPW weather file to DaySim wea format or CSV
#
# Axel Jacobs, 15 Dec 2013

use strict;
use warnings;

my $output;           # Default output is to STDOUT
my $csv = 0;          # Output CSV file, not DaySim wea format
my $delimiter = ' ';  # Delimiter. Default is ' ' which is for DaySim wea
my $delim = ' ';

while (@ARGV) {
	$_ = $ARGV[0];
	if (m/-o/) {         # Output file. Default is STDOUT
		$output = $ARGV[1];
		shift @ARGV;
	} elsif (m/-c/) {    # Output CSV file, not DaySim wea format
		$csv = 1;
	} elsif (m/-d/) {    # Delimiter for CSV output
		$delim = $ARGV[1];
		shift @ARGV;
	} elsif (m/^-\w/) {
		die("epw2wea: Bad option: $_\n");
	} else {
		last;
	}
	shift @ARGV;
}
if ($csv == 1) {
	$delimiter = $delim;     # Delimiter for CSV output
}

if ($output) {
   open(OUTPUT, '>', $output)
			or die("Cannot open file '$output' for writing: $!\n");
} else {
   *OUTPUT = *STDOUT;
}

# Extract latitude, longitude etc from first input line
my $line = <>;
#$line =~ s/\r//;    # Remove DOS line ending
chomp $line;
my @fields = split(/,/, $line);
my $city = $fields[1];
my $country = $fields[3];
my $lat = $fields[6];
my $lon = -1 * $fields[7];
my $mer = -15 * $fields[8];
my $ele = $fields[9];

if ($csv == 1) {
	my @epw_header = ('Year', 'Month', 'Day', 'Hour', 'Minute',
			'Uncertainty', 'DryBulb', 'DewPoint', 'RelHum', 'Press',
			'Ees0', 'Eesn0', 'Eird', 'Eeg', 'Eesn', 'Eed',
			'Evg', 'Evsn', 'Evd', 'Lz',
			'WindDir', 'WindSpeed',
			'TotalSkyCover', 'OpaqueSkyCover', 'Visibility', 'Ceiling',
			'PresentObs', 'PresentCode',
			'PrecWater', 'AerosolOpticalDepth', 'SnowDepth', 'LastSnowfall'
	);
	print OUTPUT join($delimiter, @epw_header) . "\n";
} else {
	print OUTPUT <<EndOfHeader;
place ${city}_$country
latitude $lat
longitude $lon
time_zone $mer
site_elevation $ele
weather_data_file_units 1
EndOfHeader
}

# Get rid of remaining header rows
for (my $i = 7; $i >= 1; $i--) {
	$line = <>;
}


# Process body
while (<>) {
	my $line = $_;
	$line =~ s/\r//g;    # Remove DOS line ending
	chomp $line;
	@fields = split(/,/, $line);
	my $month = $fields[1];
	my $day   = $fields[2];
	my $hour  = $fields[3];
	my $hour2 = sprintf '%.3f', $hour - .5;
	# Ignore minute column.
	my $Eeg   = $fields[13];
	my $Eesn  = $fields[14];
	my $Eed   = $fields[15];
	my $Evg   = $fields[16];
	my $Evsn  = $fields[17];
	my $Evd   = $fields[18];
	my @columns = ();
	if ($csv == 1) {
		# Output all columns if CSV output is requested
		@columns = @fields;
	} else {
		# Only output the columns relevant to DaySim wea format
		@columns = (
			$month,
			$day,
			$hour2,
			$Eesn,
			$Eed
		);
	}
	print OUTPUT join($delimiter, @columns) . "\n";
}
close(OUTPUT);

#EOF