Source code for pycif.plugins.datastreams.fluxes.wrfchem.times_in_wrf_file
import netCDF4 as nc
import datetime as dtm
[docs]
def times_in_wrf_file(filename):
"""Decode a WRF NetCDF file's ``Times`` variable into a list of datetimes.
Args:
filename (str): Path to the WRF NetCDF file to inspect.
Returns:
list[datetime.datetime]: One timestamp per record in the file's
``Times`` character-array variable, parsed with the format
``%Y-%m-%d_%H:%M:%S``.
"""
# This is from ctdas' wrfchem_helper.py
ncf = nc.Dataset(filename)
times_nc = ncf.variables["Times"][:]
ncf.close()
# Hope the utf-8 always works...
times_chr = [str(times_nc[nt, :], "utf-8")
for nt in range(times_nc.shape[0])]
times_dtm = [dtm.datetime.strptime(t_chr, "%Y-%m-%d_%H:%M:%S")
for t_chr in times_chr]
return times_dtm