my $prev_dst = ""; my $curr_dst = ""; my $dst_code = ""; my $dst_flag = "N"; my $dst_counter = 1; foreach (@extended_records) { my @ext_lines = split /\n/, $_; foreach my $line_ext (@ext_lines) { my @ext_fields = split ",", $line_ext; # Get the fields from the Extended File. my $obs_day = $ext_fields[0]; my $obs_month = $ext_fields[1]; my $obs_year = $ext_fields[2]; my $obs_hour = $ext_fields[3]; my $obs_minute = $ext_fields[4]; my $obs_seconds = 0; my $temp_c = $ext_fields[5]; my $humidity_rh = $ext_fields[6]; my $dewpoint_c = $ext_fields[7]; my $wind_dir_degrees = $ext_fields[12]; my $wind_speed_mph = $ext_fields[10]; my $wind_gust_mph = $ext_fields[11]; my $pressure_hpa = $ext_fields[9]; my $rain_last_minute = 0; my $rain_today = $ext_fields[13]; my $heat_index = $ext_fields[8]; my $rain_rate = $ext_fields[14]; my $soil_temp = $ext_fields[15]; my $weather_conditions = $ext_fields[16]; my $heat_word = $ext_fields[17]; my $ground_temp = $ext_fields[18]; my $solar = $ext_fields[19]; my $uv_index = $ext_fields[20]; my $solar_percentage = $ext_fields[21]; my $solar_description = $ext_fields[22]; my $et = $ext_fields[23]; my $burntime = $ext_fields[26]; my $sunshine = $ext_fields[27]; my $soil_moisture = $ext_fields[28]; my $leaf_wetness = $ext_fields[29]; my $cloud_height_metres = $ext_fields[32]; my $indoor_temp_c = $ext_fields[35]; my $indoor_humidity_rh = $ext_fields[36]; my $apparent_temp = $ext_fields[37]; my $apparent_solar_temp = $ext_fields[38]; my $humidex = $ext_fields[40]; my $air_density = $ext_fields[44]; # Format the collected data $temp_c =~ s/°C//; $dewpoint_c =~ s/°C//; $wind_dir_degrees =~ s/ °//; $wind_speed_mph =~ s/ mph//; $wind_gust_mph =~ s/ mph//; $pressure_hpa =~ s/ mb//; $rain_today =~ s/ mm//; $heat_index =~ s/°C//; $soil_temp =~ s/°C//; $solar_percentage =~ s/ %//; $humidex =~ s/°C//; # Get the Observation Date Time and keep in Local Format. my $obs_datetime_local = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $obs_hour, minute => $obs_minute, time_zone => 'Europe/London', ); # Get the Observation Date Time and Convert to UTC. my $obs_datetime_utc = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $obs_hour, minute => $obs_minute, time_zone => 'Europe/London', ); $obs_datetime_utc->set_time_zone( 'UTC' ); my $dst = $obs_datetime_local->is_dst(); # Combine the Current and Previous IsDST values to determine if the DST to GMT/UTC change has occured. $curr_dst = $dst; $dst_code = $prev_dst . $curr_dst; # If the Previous DST is set (1) and the Current DST is not set (0) then set the DST Flag. if ($dst_code eq "10") { $dst_flag = "Y"; } # If the DST Flag has been set then a change from DST to GMT/UTC has occurred. if ($dst_flag eq "Y") { # Subtract 1 from the Observation Hour to get the correct UTC Date/Time - construct the formated UTC Date & Time. my $dst_hour = $obs_hour - 1; my $dst_datetime_utc = DateTime->new ( year => $obs_year, month => $obs_month, day => $obs_day, hour => $dst_hour, minute => $obs_minute, ); $obs_datetime_utc = $dst_datetime_utc; # Reset the DST Flag and Counter after the hour requiring the change to UTC has been processed. if ($dst_counter == 59) { $dst_flag = "N"; $dst_counter = 1; } $dst_counter = $dst_counter + 1; } # Use the UTC Date/Time for the unformatted Observation Date & Time. my $rep_year = substr $obs_datetime_utc,0,4; my $rep_month = substr $obs_datetime_utc,5,2; my $rep_day = substr $obs_datetime_utc,8,2; my $rep_hour = substr $obs_datetime_utc,11,2; my $rep_minute = substr $obs_datetime_utc,14,2; my $rep_second = substr $obs_datetime_utc,17,2; # Output Record Line to a CSV File my $record_line = $rep_year . $rep_month . $rep_day . $rep_hour . $rep_minute . $rep_second . "," . $obs_datetime_utc . "," . $obs_datetime_local . "," . $temp_c . "," . $dewpoint_c . ",". $humidity_rh . "," . $wind_dir_degrees . "," . $wind_speed_mph . "," . $wind_gust_mph . "," . $pressure_hpa . "," . $rain_last_minute . "," . $rain_today . "," . $heat_index . "," . $rain_rate . "," . $soil_temp . "," . $weather_conditions . "," . $heat_word . "," . $ground_temp . "," . $solar . "," . $uv_index . "," . $solar_percentage . "," . $solar_description . "," . $et . "," . $burntime . "," . $sunshine . "," . $soil_moisture . "," . $leaf_wetness . "," . $cloud_height_metres . "," . $indoor_temp_c . "," . $indoor_humidity_rh . "," . $apparent_temp . "," . $apparent_solar_temp . "," . $humidex . "," . $air_density . "\n"; print $record_line; my $file = "C:\\WEATHER_DATA_COLLECTOR\\BATHGATE\\" . $rep_year . $rep_month . "_BATHGATE.CSV"; open (TXT, ">>", $file); print TXT $record_line; close (TXT); $prev_dst = $curr_dst; } }