Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function to deal with yearly based file names #604

Merged
merged 7 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pyspedas/omni/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings
import astropy

from pyspedas.utilities.dailynames import dailynames
from pyspedas.utilities.dailynames import dailynames, yearlynames
from pyspedas.utilities.download import download
from pytplot import time_clip as tclip
from pytplot import cdf_to_tplot
Expand Down Expand Up @@ -31,16 +31,16 @@ def load(trange=['2013-11-5', '2013-11-6'],

if 'min' in datatype:
pathformat = level + '_' + datatype + '/%Y/omni_' + level + '_' + datatype + '_%Y%m01_v??.cdf'
remote_names = dailynames(file_format=pathformat, trange=trange, res=file_res)
elif 'hour' in datatype:
pathformat = 'hourly/%Y/omni2_h0_mrg1hr_%Y%m01_v??.cdf'
file_res = 24*3600*183.0 # 1 file every 6 months
get_ignore_data = True # required to load these files
remote_names = yearlynames(file_format=pathformat, trange=trange, resolution='half-year')
else:
logging.error('Invalid datatype: '+ datatype)
return

# find the full remote path names using the trange
remote_names = dailynames(file_format=pathformat, trange=trange, res=file_res)

out_files = []

Expand Down
70 changes: 70 additions & 0 deletions pyspedas/utilities/dailynames.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,73 @@ def dailynames(directory='',
files.append(directory + prefix + date + suffix)

return files

def yearlynames(
trange,
file_format="%Y%m%d",
resolution="year",
directory="",
prefix="",
suffix="",
):
"""
Creates a list of file names using a time range, resolution, and file format.

Parameters:
trange: list of str or list of datetime
Two-element list containing the start and end times for the file names.

resolution: str
Either 'year' or 'half-year' to determine the resolution.

file_format: str
Format of the file names using strftime directives.

directory: str
String containing the directory for the generated file names.

prefix: str
file name prefix.

suffix: str
file name suffix.

Returns:
List containing filenames.
"""

if trange is None:
raise ValueError("No trange specified")

start_date = (
trange[0]
if isinstance(trange[0], datetime)
else datetime.strptime(trange[0], "%Y-%m-%d")
)
end_date = (
trange[1]
if isinstance(trange[1], datetime)
else datetime.strptime(trange[1], "%Y-%m-%d")
)

dates = []

if resolution == "half-year":
# Adjust the start date to be 6 months prior to the specified start date
if start_date.month <= 6:
adjusted_start_date = datetime(start_date.year - 1, start_date.month + 6, 1)
else:
adjusted_start_date = datetime(start_date.year, start_date.month - 6, 1)

# Generate all the January 1st and July 1st dates within the range
potential_dates = [datetime(year, month, 1) for year in range(start_date.year, end_date.year + 1) for month in [1, 7]]

# Filter out dates outside the range
dates = [date for date in potential_dates if adjusted_start_date < date <= end_date]
elif resolution == "year":
dates = [datetime(year, 1, 1) for year in range(start_date.year, end_date.year + 1)]
else:
raise ValueError("Invalid resolution specified. Choose 'year' or 'half-year'.")

files = [directory + prefix + date.strftime(file_format) + suffix for date in dates]
return files
Loading