Skip to content

Instantly share code, notes, and snippets.

@greed9
Last active August 27, 2023 01:06
Show Gist options
  • Save greed9/f9b186a9c1aecfa61cb8f4eeb1841dd8 to your computer and use it in GitHub Desktop.
Save greed9/f9b186a9c1aecfa61cb8f4eeb1841dd8 to your computer and use it in GitHub Desktop.
Awk program to convert NOAA textual solar event files to CSV
# flares.awk
# Convert text file of solar data in the format here:
# https://services.swpc.noaa.gov/text/solar-geophysical-event-reports.txt
# to CSV
#
# see README for some info
BEGIN {
state=0 # init
}
# Skip blank lines
/^\s*$/ {
next
}
# Grab the date
/Edited Events for/ {
date_start = $5 " " $6 " " $7
#printf( "%s\n", date_val)
next
}
/#Event/ {
state = 1 # found header
print "event,begin,begin_date_time,begin_epoch_time,Max,End,Obs,Q,Type,Loc/Frq,Particulars,Reg"
}
# raw data lines to CSV
/^[0-9]+/ {
if( state == 1 ){
gsub("+", "", $0)
for(i=1 ; i <=((NF > 9 ) ? 9 : NF); i++) {
gsub("#", "", $i) # get rid of hash signs
gsub("////", "-1", $i) # Making missing values -1 instead
gsub("B", "", $i) # The B means already in progress from yesterday
# Drop the optional plus
if( $i != "+") {
printf( "%s,", $i)
}
if( i == 2 ){
# convert string dates to unix epoch date/times
hr_str = substr($2,1,2)
min_str = substr($2,3,2)
split(date_start, dt_pieces, " ")
#print date_start
year = dt_pieces[1]
#print year
mon_name = dt_pieces[2]
day = dt_pieces[3]
mon = to_month(mon_name)
date_str = year " " mon " " day " " hr_str " " min_str " " 0
epoch_dt = mktime(date_str)
gsub(" ", "-", date_str)
printf( "%s,%d,", date_str, epoch_dt)
}
}
# consolidate free-format fields into one string/field
particulars = ""
for(i = 10 ; i < NF ; i++)
{
particulars = particulars " " $i
}
printf("%s\n", particulars)
}
}
# Based on:
#http://het.as.utexas.edu/HET/Projects/WindRose/selecttimes.awk
function to_month(text, mon) {
#
# Convert a month name to a month number
#
if (text ~ /Jan/ ) mon = 1
if (text ~ /Feb/ ) mon = 2
if (text ~ /Mar/ ) mon = 3
if (text ~ /Apr/ ) mon = 4
if (text ~ /May/ ) mon = 5
if (text ~ /Jun/ ) mon = 6
if (text ~ /Jul/ ) mon = 7
if (text ~ /Aug/ ) mon = 8
if (text ~ /Sep/ ) mon = 9
if (text ~ /Oct/ ) mon = 10
if (text ~ /Nov/ ) mon = 11
if (text ~ /Dec/ ) mon = 12
if (mon == 0) {
printf("BAD month %s\n",text);
}
return mon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment