bin/epw2wea.pl
changeset 49 66e192fe711e
equal deleted inserted replaced
48:de81adde2cae 49:66e192fe711e
       
     1 #!/usr/bin/perl
       
     2 #
       
     3 # Convert EnergyPlus EPW weather file to DaySim wea format or CSV
       
     4 #
       
     5 # Axel Jacobs, 15 Dec 2013
       
     6 
       
     7 use strict;
       
     8 use warnings;
       
     9 
       
    10 my $output;           # Default output is to STDOUT
       
    11 my $csv = 0;          # Output CSV file, not DaySim wea format
       
    12 my $delimiter = ' ';  # Delimiter. Default is ' ' which is for DaySim wea
       
    13 my $delim = ' ';
       
    14 
       
    15 while (@ARGV) {
       
    16 	$_ = $ARGV[0];
       
    17 	if (m/-o/) {         # Output file. Default is STDOUT
       
    18 		$output = $ARGV[1];
       
    19 		shift @ARGV;
       
    20 	} elsif (m/-c/) {    # Output CSV file, not DaySim wea format
       
    21 		$csv = 1;
       
    22 	} elsif (m/-d/) {    # Delimiter for CSV output
       
    23 		$delim = $ARGV[1];
       
    24 		shift @ARGV;
       
    25 	} elsif (m/^-\w/) {
       
    26 		die("epw2wea: Bad option: $_\n");
       
    27 	} else {
       
    28 		last;
       
    29 	}
       
    30 	shift @ARGV;
       
    31 }
       
    32 if ($csv == 1) {
       
    33 	$delimiter = $delim;     # Delimiter for CSV output
       
    34 }
       
    35 
       
    36 if ($output) {
       
    37    open(OUTPUT, '>', $output)
       
    38 			or die("Cannot open file '$output' for writing: $!\n");
       
    39 } else {
       
    40    *OUTPUT = *STDOUT;
       
    41 }
       
    42 
       
    43 # Extract latitude, longitude etc from first input line
       
    44 my $line = <>;
       
    45 #$line =~ s/\r//;    # Remove DOS line ending
       
    46 chomp $line;
       
    47 my @fields = split(/,/, $line);
       
    48 my $city = $fields[1];
       
    49 my $country = $fields[3];
       
    50 my $lat = $fields[6];
       
    51 my $lon = -1 * $fields[7];
       
    52 my $mer = -15 * $fields[8];
       
    53 my $ele = $fields[9];
       
    54 
       
    55 if ($csv == 1) {
       
    56 	my @epw_header = ('Year', 'Month', 'Day', 'Hour', 'Minute',
       
    57 			'Uncertainty', 'DryBulb', 'DewPoint', 'RelHum', 'Press',
       
    58 			'Ees0', 'Eesn0', 'Eird', 'Eeg', 'Eesn', 'Eed',
       
    59 			'Evg', 'Evsn', 'Evd', 'Lz',
       
    60 			'WindDir', 'WindSpeed',
       
    61 			'TotalSkyCover', 'OpaqueSkyCover', 'Visibility', 'Ceiling',
       
    62 			'PresentObs', 'PresentCode',
       
    63 			'PrecWater', 'AerosolOpticalDepth', 'SnowDepth', 'LastSnowfall'
       
    64 	);
       
    65 	print OUTPUT join($delimiter, @epw_header) . "\n";
       
    66 } else {
       
    67 	print OUTPUT <<EndOfHeader;
       
    68 place ${city}_$country
       
    69 latitude $lat
       
    70 longitude $lon
       
    71 time_zone $mer
       
    72 site_elevation $ele
       
    73 weather_data_file_units 1
       
    74 EndOfHeader
       
    75 }
       
    76 
       
    77 # Get rid of remaining header rows
       
    78 for (my $i = 7; $i >= 1; $i--) {
       
    79 	$line = <>;
       
    80 }
       
    81 
       
    82 
       
    83 # Process body
       
    84 while (<>) {
       
    85 	my $line = $_;
       
    86 	$line =~ s/\r//g;    # Remove DOS line ending
       
    87 	chomp $line;
       
    88 	@fields = split(/,/, $line);
       
    89 	my $month = $fields[1];
       
    90 	my $day   = $fields[2];
       
    91 	my $hour  = $fields[3];
       
    92 	my $hour2 = sprintf '%.3f', $hour - .5;
       
    93 	# Ignore minute column.
       
    94 	my $Eeg   = $fields[13];
       
    95 	my $Eesn  = $fields[14];
       
    96 	my $Eed   = $fields[15];
       
    97 	my $Evg   = $fields[16];
       
    98 	my $Evsn  = $fields[17];
       
    99 	my $Evd   = $fields[18];
       
   100 	my @columns = ();
       
   101 	if ($csv == 1) {
       
   102 		# Output all columns if CSV output is requested
       
   103 		@columns = @fields;
       
   104 	} else {
       
   105 		# Only output the columns relevant to DaySim wea format
       
   106 		@columns = (
       
   107 			$month,
       
   108 			$day,
       
   109 			$hour2,
       
   110 			$Eesn,
       
   111 			$Eed
       
   112 		);
       
   113 	}
       
   114 	print OUTPUT join($delimiter, @columns) . "\n";
       
   115 }
       
   116 close(OUTPUT);
       
   117 
       
   118 #EOF