Code Duplication    Length = 48-63 lines in 2 locations

src/spaceweather/omni.py 2 locations

@@ 270-332 (lines=63) @@
267
	return sw_df
268
269
270
def omnie_hourly(
271
	year,
272
	prefix=None,
273
	ext=None,
274
	local_path=None,
275
	url_base=None,
276
	cache=False,
277
):
278
	"""OMNI hourly data for year `year`
279
280
	Loads the OMNI hourly data for the given year,
281
	from the locally cached data.
282
	Use `local_path` to set a custom location if you
283
	have the omni data already available.
284
285
	Parameters:
286
	-----------
287
	year: int
288
		Year of the data.
289
	prefix: str, optional, default 'omni2'
290
		File prefix for constructing the file name as <prefix>_year.<ext>.
291
	ext: str, optional, default 'dat'
292
		File extension for constructing the file name as <prefix>_year.<ext>.
293
	local_path: str, optional
294
		Path to the locally stored data yearly files, defaults to the
295
		data location within the package.
296
	url_base: str, optional
297
		URL for the directory that contains the yearly files.
298
	cache: boolean, optional, default False
299
		Download files locally if they are not already available.
300
301
	Returns
302
	-------
303
	sw_df: pandas.DataFrame
304
		The parsed space weather data (hourly values).
305
306
		Raises an ``IOError`` if the file is not available.
307
308
	See Also
309
	--------
310
	read_omnie
311
	"""
312
	prefix = prefix or OMNI_PREFIX
313
	ext = ext or OMNI_EXT
314
	local_path = local_path or LOCAL_PATH
315
	url_base = url_base or OMNI_URL_BASE
316
317
	basename = "{0}_{1:04d}.{2}".format(prefix, year, ext)
318
	omnie_file = os.path.join(local_path, basename)
319
320
	# ensure that the file exists
321
	if not os.path.exists(omnie_file):
322
		warn("Could not find OMNI2 data {0}.".format(omnie_file))
323
		if cache:
324
			warn("Trying to cache to `{0}'".format(omnie_file))
325
			_dl_file(omnie_file, os.path.join(url_base, basename))
326
		else:
327
			warn(
328
				"Local data files not found, pass `cache=True` "
329
				"or run `sw.cache_omnie()` to download the file."
330
			)
331
332
	return read_omnie(omnie_file)
333
@@ 36-83 (lines=48) @@
33
LOCAL_PATH = resource_filename(__name__, os.path.join("data", OMNI_SUBDIR))
34
35
36
def cache_omnie(
37
	year,
38
	prefix=None,
39
	ext=None,
40
	local_path=None,
41
	url_base=None,
42
):
43
	"""Download OMNI2 data to local cache
44
45
	Downloads the OMNI2 (extended) data file to the local location.
46
47
	.. [#] https://spdf.gsfc.nasa.gov/pub/data/omni/low_res_omni/extended/
48
49
	Parameters
50
	----------
51
	year: int
52
		Year of the data.
53
	prefix: str, optional
54
		File prefix for constructing the file name as <prefix>_year.<ext>.
55
		Defaults to 'omni2'.
56
	ext: str, optional
57
		File extension for constructing the file name as <prefix>_year.<ext>.
58
		Defaults to 'dat'.
59
	local_path: str, optional
60
		Path to the locally stored data yearly files, defaults to the
61
		data location within the package.
62
	url_base: str, optional
63
		URL for the directory that contains the yearly files.
64
65
	Returns
66
	-------
67
	Nothing.
68
	"""
69
	prefix = prefix or OMNI_PREFIX
70
	ext = ext or OMNI_EXT
71
	local_path = local_path or LOCAL_PATH
72
	url_base = url_base or OMNI_URL_BASE
73
74
	basename = "{0}_{1:04d}.{2}".format(prefix, year, ext)
75
76
	if not os.path.exists(local_path):
77
		os.makedirs(local_path)
78
79
	omnie_file = os.path.join(local_path, basename)
80
	if not os.path.exists(omnie_file):
81
		url = os.path.join(url_base, basename)
82
		logging.info("%s not found, downloading from %s.", omnie_file, url)
83
		_dl_file(omnie_file, url)
84
85
86
def read_omnie(omnie_file):