Skip to content

Commit c3ec1bb

Browse files
Merge pull request #1342 from spedas/t_1341
Adds support to load USGS Variometer data, adds test cases for loading variometer data.
2 parents 8346a30 + a73ae1b commit c3ec1bb

3 files changed

Lines changed: 178 additions & 2 deletions

File tree

pyspedas/projects/themis/ground/gmag.py

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def gmag(
4444
no_update=False,
4545
time_clip=False,
4646
force_download=False,
47+
sampling_rate=1,
4748
):
4849
"""
4950
Load ground magnetometer data from the THEMIS mission.
@@ -84,7 +85,9 @@ def gmag(
8485
force_download: bool, optional
8586
Download file even if local version is more recent than server version
8687
Default: False
87-
88+
sampling_rate: int, optional
89+
Specify a sampling rate for loading variometer data. Accepts 1 (Hz) or 10 (Hz).
90+
Default: 1
8891
Returns
8992
-------
9093
dict
@@ -94,10 +97,28 @@ def gmag(
9497
--------
9598
>>> from pyspedas.projects.themis import gmag
9699
>>> from pyspedas import tplot
100+
>>> from pyspedas import subtract_median
97101
>>>
98102
>>> # Load ground magnetometer data for specific sites and time range
99103
>>> gmag_vars = gmag(sites=['ccnv','bmls'], trange=['2013-11-05', '2013-11-06'])
100104
>>> tplot(['thg_mag_bmls', 'thg_mag_ccnv'])
105+
>>>
106+
>>> # Load variometer data for specific sites and time range:
107+
>>> gmag_vars = gmag(sites=['s61a','anmo'], trange=['2026-02-24', '2026-02-25'])
108+
>>> subtract_median(['thg_mag_s61a', 'thg_mag_anmo'])
109+
>>> tplot(['thg_mag_s61a-m', 'thg_mag_anmo-m'])
110+
>>>
111+
>>> # Load 10 Hz variometer data for specific sites and time range:
112+
>>> gmag_vars = gmag(sites=['s61a','anmo'], sampling_rate=10, trange=['2026-02-24', '2026-02-25'])
113+
>>> subtract_median(['thg_mag_s61a_100ms', 'thg_mag_anmo_100ms'])
114+
>>> tplot(['thg_mag_s61a_100ms-m', 'thg_mag_anmo_100ms-m'])
115+
>>>
116+
>>> # Load 10 Hz variometer data for specific sites and time range using the 10 Hz file name format:
117+
>>> gmag_vars = gmag(sites=['s61a_100ms','anmo_100ms'], trange=['2026-02-24', '2026-02-25'])
118+
>>> subtract_median(['thg_mag_s61a_100ms', 'thg_mag_anmo_100ms'])
119+
>>> tplot(['thg_mag_s61a_100ms-m', 'thg_mag_anmo_100ms-m'])
120+
>>>
121+
101122
"""
102123

103124
if sites is None:
@@ -250,6 +271,48 @@ def gmag(
250271
"weyb",
251272
"wgry",
252273
]
274+
variometer_sites=[
275+
"anmo",
276+
"casy",
277+
"ccm",
278+
"cola",
279+
"cor",
280+
"dgmt",
281+
"dwpf",
282+
"ecsd",
283+
"eymn",
284+
"e46a",
285+
"e62a",
286+
"goga",
287+
"hrv",
288+
"j47a",
289+
"kbs",
290+
"kevo",
291+
"kono",
292+
"ksu1",
293+
"k30b",
294+
"k50a",
295+
"mbwa",
296+
"mstx",
297+
"m63a",
298+
"o20a",
299+
"pab",
300+
"p57a",
301+
"qspa",
302+
"rssd",
303+
"r49a",
304+
"sba",
305+
"sfjd",
306+
"spmn",
307+
"sspa",
308+
"s61a",
309+
"t47a",
310+
"u38b",
311+
"wci",
312+
"whtx",
313+
"wvt",
314+
"352a"
315+
]
253316
sites = (
254317
thm_sites
255318
+ tgo_sites
@@ -266,6 +329,7 @@ def gmag(
266329
+ fmi_sites
267330
+ aair_sites
268331
+ carisma_sites
332+
+ variometer_sites
269333
)
270334

271335
if group is not None:
@@ -274,6 +338,29 @@ def gmag(
274338
if not isinstance(sites, list):
275339
sites = [sites]
276340

341+
# check for sites in Variometer group
342+
variometer = []
343+
for site in sites:
344+
s_rate=check_variometer(site)
345+
# if check_variometer determines site
346+
# to be 1 Hz sampling rate, check if
347+
# sampling_rate argument has been set
348+
if (s_rate == 1) and (sampling_rate is not None):
349+
# if so, pass sampling_rate value
350+
# (case where variometer base names
351+
# are passed, but 10 Hz sampling rate
352+
# data is to be loaded)
353+
354+
# check if sampling_rate is valid:
355+
if sampling_rate not in [1,10]:
356+
# throw error
357+
logging.error('sampling_rate must be either 1 or 10. Setting to default value of 1')
358+
sampling_rate=1
359+
360+
variometer.append(sampling_rate)
361+
else:
362+
variometer.append(s_rate)
363+
277364
# check for sites in Greenland
278365
greenland = []
279366
for site in sites:
@@ -295,6 +382,7 @@ def gmag(
295382
notplot=notplot,
296383
stations=sites,
297384
greenland=greenland,
385+
variometer=variometer,
298386
time_clip=time_clip,
299387
no_update=no_update,
300388
force_download=force_download,
@@ -482,3 +570,51 @@ def check_greenland(station_name):
482570
return 1
483571

484572
return 0
573+
574+
def check_variometer(station_name):
575+
"""
576+
Check if a given station belongs to the Variometers group.
577+
If so, check if the given stationname is formatted to request
578+
data in the 10 Hz sampling rate.
579+
580+
Parameters
581+
----------
582+
station_name : str
583+
The name of the station to check.
584+
585+
Returns
586+
-------
587+
int
588+
Returns sampling rate (1 or 10) if the station is in the variometers group, otherwise returns 0.
589+
590+
Examples
591+
--------
592+
>>> from pyspedas.projects.themis.ground.gmag import check_variometer
593+
>>> check_variometer("s61a")
594+
1
595+
>>> check_variometer("s61a_100ms")
596+
10
597+
"""
598+
599+
# check if station_name contains "_100ms". If it does,
600+
# create copy of station name with the "_100ms"
601+
# substring removed
602+
if "_100ms" in station_name.lower():
603+
station_name_base=station_name.lower()
604+
station_name_base=station_name_base.replace("_100ms","")
605+
else:
606+
station_name_base=station_name.lower()
607+
608+
gmag_dict = themis_gmag_dict.get_gmag_list()
609+
for station in gmag_dict:
610+
# compare a copy of station name with any "_100ms"
611+
# substring removed with the station ccode
612+
if station["ccode"].lower() == station_name_base:
613+
if (
614+
station["variom"] is not None and station["variom"].lower() == "y"
615+
):
616+
if "_100ms" in station_name.lower():
617+
return 10
618+
else:
619+
return 1
620+
return 0

pyspedas/projects/themis/load.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def load(trange=['2013-11-5', '2013-11-6'],
1414
datatype=None, # ASK data, ESD (3d L2 ESA)
1515
stations=None, # ground mag and ASK data
1616
greenland=None, # also for ground mag data
17+
variometer=None, # variometer check and sampling rate
1718
prefix='',
1819
suffix='',
1920
get_support_data=False,
@@ -177,10 +178,20 @@ def load(trange=['2013-11-5', '2013-11-6'],
177178
return
178179
else:
179180
pathformat = []
180-
for site, in_greenland in zip(stations, greenland):
181+
for site, in_greenland, s_variometer in zip(stations, greenland, variometer):
181182
if site == 'idx':
182183
# THEMIS GMAG index files are only L1
183184
pathformat.append('thg/l1/mag/idx/%Y/thg_l1_idx_%Y%m%d_v??.cdf')
185+
elif s_variometer == 1:
186+
pathformat.append('thg/' + level + '/variometers/' + site
187+
+ '/%Y/thg_' + level + '_mag_' + site
188+
+ '_%Y%m%d_v??.cdf')
189+
elif s_variometer == 10:
190+
if "_100ms" in site.lower():
191+
site=site.replace("_100ms","")
192+
pathformat.append('thg/' + level + '/variometers/' + site
193+
+ '/%Y/thg_' + level + '_mag_' + site
194+
+ '_100ms_%Y%m%d_v??.cdf')
184195
elif in_greenland:
185196
pathformat.append('thg/greenland_gmag/' + level
186197
+ '/' + site + '/%Y/thg_' + level

pyspedas/projects/themis/tests/test_themis.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,40 @@ def test_check_gmag(self):
3737
self.assertTrue(check_gmag('ccnv') == 1)
3838
self.assertTrue(check_gmag('abcd') == 0)
3939

40+
def test_check_greenland(self):
41+
"""Check if a gmag station is in the greenland group."""
42+
from pyspedas.projects.themis.ground.gmag import check_greenland
43+
self.assertTrue(check_greenland('bfe') == 1)
44+
self.assertTrue(check_greenland('bou') == 0)
45+
46+
def test_check_variometer(self):
47+
"""Check if a gmag station is in the 1 Hz variometer group or 10 Hz variometer group or not."""
48+
from pyspedas.projects.themis.ground.gmag import check_variometer
49+
self.assertTrue(check_variometer('anmo') == 1)
50+
self.assertTrue(check_variometer('anmo_100ms') == 10)
51+
self.assertTrue(check_variometer('bou') == 0)
52+
4053
def test_load_gmag_data(self):
4154
"""Load gmag."""
4255
pyspedas.projects.themis.gmag(varnames=['thg_mag_amer'], sites='amer')
4356
self.assertTrue(data_exists('thg_mag_amer'))
4457

58+
def test_load_gmag_variometer_1_hz_data(self):
59+
"""Load gmag variometer 1 Hz data."""
60+
pyspedas.projects.themis.gmag(varnames=['thg_mag_s61a'], sites='s61a',trange=['2026-02-24', '2026-02-25'])
61+
self.assertTrue(data_exists('thg_mag_s61a'))
62+
63+
def test_load_gmag_variometer_10_hz_data_notag(self):
64+
"""Load gmag variometer 10 Hz data using the sampling_rate argument and not the time resolution tag."""
65+
pyspedas.projects.themis.gmag(varnames=['thg_mag_s61a_100ms'], sites='s61a',sampling_rate=10,trange=['2026-02-24', '2026-02-25'])
66+
self.assertTrue(data_exists('thg_mag_s61a_100ms'))
67+
68+
def test_load_gmag_variometer_10_hz_data_tag(self):
69+
"""Load gmag variometer 10 Hz data using the time resolution tag and not the sampling_rate argument."""
70+
pyspedas.projects.themis.gmag(varnames=['thg_mag_s61a_100ms'], sites='s61a_100ms',trange=['2026-02-24', '2026-02-25'])
71+
self.assertTrue(data_exists('thg_mag_s61a_100ms'))
72+
73+
4574

4675
class LoadTestCases(unittest.TestCase):
4776
"""Test themis load functions."""

0 commit comments

Comments
 (0)