[FIX] Added functions to scan and repair NetCDF files before loading#86
Merged
Conversation
tillmrtz
reviewed
Apr 7, 2026
Contributor
There was a problem hiding this comment.
This is where I changed decode_timedelta = False to decode_times=False. I don't really know what other changes I have made but it worked for all datasets I tried (approx. 8 different ones). I wanted to implement for another issue, however it's taking some more time and better to implement sooner than later.
Contributor
Author
There was a problem hiding this comment.
Ok, I removed decode_timedelta = False now, and it works for both datasets I have with the incorporate changes to identify the time unit problem.
tillmrtz
approved these changes
Apr 8, 2026
tillmrtz
left a comment
Contributor
There was a problem hiding this comment.
Changes for handling the time data looks fine to me.
tillmrtz
approved these changes
Apr 8, 2026
tillmrtz
left a comment
Contributor
There was a problem hiding this comment.
Issue with date format resolved hopefully
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
The purpose of this pull-request is to solve issue #85.
Some Seaglider basestation NetCDF files could not be opened with xarray.open_dataset(). The error raised was:
ValueError: unable to decode time units 'seconds since 1970-1-1 00:00:00'This occurred during CF (Climate and Forecast) time decoding in xarray.
The issue was traced to specific variables (e.g. compass_timeouts_times_truck) that had:
Because this variable with time units does not contain numerical values, the error was raised and the file could not be opened.
Modification:
I implemented a pre-processing repair step that scans all NetCDF files before loading.
For each file, it now disables automatic time decoding:
xr.open_dataset(..., decode_times=False)Then it identifies problematic variables, e.g., those that have a units attribute containing "since", or have a string/byte dtype (S, U, or O). For those variables, it now removes the misleading metadata and saves the .nc file. The original file is backed up as pxxxx_original.nc in an additional folder. All modifications are logged in a repair_log.txt file. The fix is conservative, i.e., there is no attempt to convert the string values into real timestamps, and only the metadata that is incorrect is removed. This ensures no accidental data corruption, full reversibility (as the original files are preserved), and minimal assumptions about the intended meaning of the data.
Checklist:
pytestto check that all tests pass.pre-commit run --all-filesto lint and format the code.Related Issues / Pull Requests:
Link any related issues, pull requests, or discussions: