Skip to content

Commit

Permalink
BUG: fixed file date for F10.7 'daily' data
Browse files Browse the repository at this point in the history
Fixed the file date for F10.7 'daily' data, which can be obtained from the file as done in other files.  Simplified the code by creating a function to do this and inform users of any changes in a way that still allows the files to be processed.
  • Loading branch information
aburrell committed Aug 27, 2024
1 parent 958b5d7 commit fb63836
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions pysatSpaceWeather/instruments/methods/swpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,24 @@ def daily_dsd_download(name, today, data_path, mock_download_dir=None):
general.get_instrument_data_path(
'sw_{:s}'.format(data_name), tag='daily')
for data_name in ['f107', 'flare', 'ssn', 'sbfield']}

# Try and obtain the file date; otherwise assume it was issued today
fdate = find_issue_date(raw_txt)
if fdate is None:
fdate = today

outfiles = {
data_name: os.path.join(file_paths[data_name], '_'.join([
data_name, 'daily', '{:s}.txt'.format(
today.strftime('%Y-%m-%d'))]))
fdate.strftime('%Y-%m-%d'))]))
for data_name in file_paths.keys()}

# Check that the directories exist
for data_path in file_paths.values():
pysat.utils.files.check_and_make_path(data_path)

# Save the output
rewrite_daily_solar_data_file(today.year, outfiles, raw_txt)
rewrite_daily_solar_data_file(fdate.year, outfiles, raw_txt)

return

Expand Down Expand Up @@ -442,8 +448,7 @@ def solar_geomag_predictions_download(name, date_array, data_path,
"filename."]))
else:
# Parse text to get the date the prediction was generated
date_str = raw_txt.split(':Issued: ')[-1].split(' UTC')[0]
dl_date = dt.datetime.strptime(date_str, '%Y %b %d %H%M')
dl_date = find_issue_date(raw_txt, '%Y %b %d %H%M UTC')

# Parse the data to get the prediction dates
date_strs = raw_txt.split(':Prediction_dates:')[-1].split('\n')[0]
Expand Down Expand Up @@ -616,8 +621,7 @@ def geomag_forecast_download(name, date_array, data_path,
"been saved to an unexpected filename."]))
else:
# Parse text to get the date the prediction was generated
date_str = raw_txt.split(':Issued: ')[-1].split(' UTC')[0]
dl_date = dt.datetime.strptime(date_str, '%Y %b %d %H%M')
dl_date = find_issue_date(raw_txt, '%Y %b %d %H%M UTC')

# Separate out the data by chunks
ap_raw = raw_txt.split('NOAA Ap Index Forecast')[-1]
Expand Down Expand Up @@ -745,8 +749,7 @@ def kp_ap_recent_download(name, date_array, data_path, mock_download_dir=None):
"filename."]))
else:
# Parse text to get the date the prediction was generated
date_str = raw_txt.split(':Issued: ')[-1].split('\n')[0]
dl_date = dt.datetime.strptime(date_str, '%H%M UT %d %b %Y')
dl_date = find_issue_date(raw_txt)

# Data is the forecast value for the next three days
raw_data = raw_txt.split('# Date ')[-1]
Expand Down Expand Up @@ -858,8 +861,7 @@ def recent_ap_f107_download(name, date_array, data_path,
" saved to an unexpected filename."]))
else:
# Parse text to get the date the prediction was generated
date_str = raw_txt.split(':Issued: ')[-1].split(' UTC')[0]
dl_date = dt.datetime.strptime(date_str, '%Y %b %d %H%M')
dl_date = find_issue_date(raw_txt, '%Y %b %d %H%M UTC')

# Get to the forecast data
raw_data = raw_txt.split('45-DAY AP FORECAST')[-1]
Expand All @@ -880,6 +882,10 @@ def recent_ap_f107_download(name, date_array, data_path,
data = {'ap': pds.DataFrame(ap, index=ap_times, columns=['daily_Ap']),
'f107': pds.DataFrame(f107, index=f107_times, columns=['f107'])}

# Ensure there is a download date
if dl_date is None:
dl_date = ap_times[0]

# Write out the data files
for data_name in data.keys():
file_name = '{:s}_45day_{:s}.txt'.format(
Expand Down Expand Up @@ -963,3 +969,43 @@ def list_files(name, tag, inst_id, data_path, format_str=None):
files.loc[files.index[-1] + pds_offset] = files.values[-1]

return files


def find_issue_date(file_txt, date_fmt="%H%M UT %d %b %Y"):
"""Find the issue date for a SWPC file.
Parameters
----------
file_txt : str
String containing all text in a file.
date_fmt : str
Expected date format (default='%H%M UT %d %b %Y')
Returns
-------
dl_date : dt.datetime or NoneType
Datetime object with time that the file was issued or None if an
error is encountered
Note
----
Assumes the file has a line formatted as: ':Issued: <date_fmt>\n'
"""
dl_date = None

# Parse text to get the date the prediction was generated
if file_txt.find(':Issued:') >= 0:
date_str = file_txt.split(':Issued: ')[-1].split('\n')[0]

try:
dl_date = dt.datetime.strptime(date_str, date_fmt)
except Exception as err:
pysat.logger.critical(''.join(['Unexpected issued date format ',
repr(date_str), ' did not match ',
date_fmt, '; failed with error: ',
repr(err)]))
else:
pysat.logger.critical('Unable to find issue line in file')

return dl_date

0 comments on commit fb63836

Please sign in to comment.