Skip to content

feat: refresh F10.7 and ap data handling with partial data support - #74

Merged
greglucas merged 1 commit into
SWxTREC:mainfrom
mananapr:feat/refresh-ap-data
Sep 8, 2026
Merged

greglucas merged 1 commit into
SWxTREC:mainfrom
mananapr:feat/refresh-ap-data

Conversation

@mananapr

@mananapr mananapr commented Jan 6, 2025 •

Copy link
Copy Markdown

Refresh F10.7 and ap data handling with partial data support and automatic updates

Closes #69

@greglucas greglucas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you @mananapr!

I've got a few comments/suggestions, but happy to get this in!

Comment thread pymsis/utils.py Outdated
"""
dates = np.asarray(dates, dtype=np.datetime64)
data = _DATA or _load_f107_ap_data()
data = _DATA or _load_f107_ap_data(dates[-1])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a long running process with _DATA already loaded, then this won't update the data for us. I think the logic should probably be a new if block here instead after the original loading potentially?

Suggested change
data = _DATA or _load_f107_ap_data(dates[-1])
# Load/download data right away.
data = _DATA or _load_f107_ap_data()
if dates[-1] > data["dates"][-1]:
data = _new_function_to_download_recent_data()

Comment thread pymsis/utils.py Outdated

# Check if the file needs to be refreshed after parsing
if last_obs_date is not None:
file_mod_time = datetime.fromtimestamp(os.path.getmtime(_F107_AP_PATH))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need file modification time? I think we should just leave that out and only rely on the requested date being greater than the observations we have.

Comment thread pymsis/utils.py Outdated
Comment on lines +158 to +160
if ",OBS," in line:
# Capture last observed date
last_obs_date = np.datetime64(line.split(",")[0])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get this from the _DATA numpy arrays? Maybe warn_data contains this information already and we could do something like (have not tried this) _DATA["dates"][~_DATA["warn_data"]][-1].

Comment thread pymsis/utils.py Outdated
writer.writeheader()
writer.writerows(sorted_data)

os.remove(_PARTIAL_F107_AP_PATH)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be wary of this because if there is any download error above, this file will be left hanging around.

A few quick thoughts:

  • Do we need to save it to disk, or could you store it in a BytesIO object in memory instead?
  • Would it make sense to save _DATA arrays with np.savez() instead of the csv files for easier manipulation? (Then you wouldn't need to mess with the dictreader/writer.
  • Honestly, now that I'm thinking about it more, is this just more headache than it is worth and we should just use your logic below and re-download the full file for the simplest case. More bandwidth, but at least we are getting the full file built for us rather than building it from bits and pieces.

@mananapr

Copy link
Copy Markdown
Author

Hi @greglucas, I've pushed another commit based on your suggestions.

  • I have removed the partial download functionality because like you said, it is a lot of headache
  • I am getting the last observation date from the warnings data like you mentioned.
  • I am still keeping the check on file update date to prevent pymsis from re-downloading the SW file each time the program runs in case the end date is not present in the csv file

Let me know what you think.

@greglucas greglucas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mananapr, this looks great to me. Just one minor suggestion.

Comment thread pymsis/utils.py Outdated
"""
dates = np.asarray(dates, dtype=np.datetime64)
data = _DATA or _load_f107_ap_data()
if dates[-1] > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone didn't sort their inputs/request, we should use a max here I think. Could you also add a quick comment about what this if statement is doing.

Suggested change
if dates[-1] > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:
# If our requested data time is after the cached values we have, go and download a new file
# to refresh the local file cache
if dates.max() > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @greglucas, I've commented the date comparison check

@greglucas

Copy link
Copy Markdown
Member

@mananapr I pushed a commit up to your branch to try and fix up some of the failing tests. Unfortunately, it was more involved than I hoped, so there is a bit more work there than anticipated. It also apparently didn't fix the issue :-/ But it does work locally for me. Feel free to drop that commit if you don't like it and continue on in a different way to try and get the tests to pass too.

@mananapr

mananapr commented Feb 3, 2025

Copy link
Copy Markdown
Author

@mananapr I pushed a commit up to your branch to try and fix up some of the failing tests. Unfortunately, it was more involved than I hoped, so there is a bit more work there than anticipated. It also apparently didn't fix the issue :-/ But it does work locally for me. Feel free to drop that commit if you don't like it and continue on in a different way to try and get the tests to pass too.

Ah, I totally missed that. Will look into the changes, thanks!

@greglucas
greglucas force-pushed the feat/refresh-ap-data branch from f12ec35 to 9cdae71 Compare September 8, 2026 22:51
Add tests to make sure the downloads are triggered appropriately.

Change the condition to only refresh if the data being requested is
before the current time AND after the last date in the file.

Co-authored-by: Manan Singh <manan.singh@digantara.co.in>
@greglucas
greglucas force-pushed the feat/refresh-ap-data branch from 9cdae71 to 634a4e4 Compare September 8, 2026 22:54
@greglucas

Copy link
Copy Markdown
Member

I reworked some of the file download utilities recently so this needed to be updated. I made the changes and force-pushed to your branch leaving you as a co-author on the commit. Thanks for this addition!

@greglucas
greglucas merged commit de1364b into SWxTREC:main Sep 8, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Downloaded SW file is not refreshed if it is out of date

2 participants