forked from geomagpy/magpy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.rst
2702 lines (2190 loc) · 88.6 KB
/
README.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MagPy
=====
**MagPy (or GeomagPy) is a Python package for analysing and displaying
geomagnetic data.**
Version Info: (please note: this package is still in a development state
with frequent modifcations) please check the release notes.
MagPy provides tools for geomagnetic data analysis with special focus on
typical data processing routines in observatories. MagPy provides
methods for data format conversion, plotting and mathematical procedures
with specifically geomagnetic analysis routines such as basevalue and
baseline calculation and database handling. Among the supported data
formats are *ImagCDF, IAGA-02, WDC, IMF, IAF, BLV*, and many more. Full
installation also provides a graphical user interface, *xmagpy*.
Typical usage for reading and visualising data looks like this:
::
#!/usr/bin/env python
from magpy.stream import read
import magpy.mpplot as mp
stream = read(path_or_url='filename')
mp.plot(stream)
Below you will find a quick guide to usage of the MagPy package. The
quickest approach can be accomplished when skipping everything except
the tutorials.
1. INSTALLATION
---------------
1.1 Windows installation - WinPython Package
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.1.1 Install NASA `CDF <https://cdf.gsfc.nasa.gov/>`__ support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- enables CDF support for formats like ImagCDF
- package details and files at http://cdf.gsfc.nasa.gov/
- download and install a recent version of CDF e.g.
cdf36\_2\_1-setup-32.exe
- Note: please use 32 bit installer.
1.1.2 Install MagPy for Windows
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- find the MagPy Windows installer here (under Downloads):
http://www.conrad-observatory.at
- download and execute magpy-0.x.x.exe
- all required packages are included in the installer
1.1.3 Post-installation information
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- MagPy should have a sub-folder in the Start menu. Here you will find
two items:
::
* python -> opens a python shell ready for MagPy
* xmagpy -> opens the MagPy graphical user interface
1.2 Linux/MacOs installation - Anaconda
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.2.1 Install `Anaconda <https://www.continuum.io/downloads>`__ on your operating system
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- download files from https://www.continuum.io/Downloads (tested with
Anaconda2 for Python 2.7)
- see https://docs.continuum.io/anaconda/install for more details
1.2.2 Install NASA CDF support
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- http://cdf.gsfc.nasa.gov/
- download and install the latest cdf version for your operating system
1.2.3 Install MagPy and SpacePy (required for CDF support)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- open a Terminal
- ... known issues: eventually change to the anaconda2/bin directory
before running python (if not set as default)
- ... check by starting python in the terminal
- run './pip install spacepy'
- ... known issues: installation of spacepy eventually requires a
fortran compiler
- ... e.g. Linux: install gcc
- ... e.g. MacOs: install gcc and gfortran
- run './pip install geomagpy'
- ... known issues: e.g. Linux: MySQL-python problem -> install
libmysqlclient-dev on linux (e.g. debian/ubuntu: sudo apt-get install
libmysqlclient-dev)
1.2.4 Post-installation information
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- please note that anaconda provides a full python environment with
many packages not used by MagPy
- for a "slim" installation follow the "from scratch" instructions
below (for experienced users)
- for upgrades: run './pip install geomagpy version==new-version'.
Installation provides both shell based magpy and the graphical user
interface xmagpy
- running magpy: \* type "python" in a terminal -> opens a python shell
ready for MagPy \* type "xmagpy" in a terminal -> open the graphical
user interface of MagPy \* !! MacOS: !! type "xmagpyw" in a terminal
-> open the graphical user interface of MagPy (since v0.3.95)
- adding a shortcut for xmagpy: coming soon
1.3 MacOs installation - MacPorts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.3.1 Install `MacPorts <https://www.macports.org/>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1.3.2 coming soon
^^^^^^^^^^^^^^^^^
1.4 Platform independent installations - Docker
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.4.1 Install `Docker <https://www.docker.com/>`__ (toolbox) on your operating system
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
- https://docs.docker.com/engine/installation/
1.4.2 Get the MagPy Image
^^^^^^^^^^^^^^^^^^^^^^^^^
::
- open a docker shell
>>> docker pull geomagpy/magpy:latest
>>> docker run -d --name magpy -p 8000:8000 geomagpy/magpy:latest
1.4.3 Open a browser
^^^^^^^^^^^^^^^^^^^^
::
- open address http://localhost:8000 (or http://"IP of your VM":8000)
- NEW: first time access might require a token or passwd
>>> docker logs magpy
will show the token
- run python shell (not conda)
- in python shell
>>> %matplotlib inline
>>> from magpy.stream import read
>>> ...
1.5 Install from source (experts only)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Requirements: - Python 2.7,3.x (*xmagpy* will only work with python 2.7)
Recommended: - Python packages: \* NasaCDF \* SpacePy \* pexpect (for
SSH support)
- Other useful Software:
- MySQL (database features)
- NetCDF4 (support is currently in preparation)
- Webserver (e.g. Apache2, PHP)
1.5.1 Linux
^^^^^^^^^^^
A) Get python packages and other extensions (for other distros than
debian/ubuntu install similar packages):
::
sudo apt-get install python-numpy python-scipy python-matplotlib python-nose python-wxgtk2.8 python-wxtools python-dev build-essential python-networkx python-h5py python-f2py gfortran ncurses-dev libhdf5-serial-dev hdf5-tools libnetcdf-dev python-netcdf python-serial python-twisted owfs python-ow python-setuptools git-core mysql-server python-mysqldb libmysqlclient-dev
sudo pip install ffnet
sudo pip install pexpect
sudo pip install pyproj
B) Get CDF and Omni database support:
a) CDF (NASA): http://cdf.gsfc.nasa.gov/html/sw\_and\_docs.html
(tested with 3.6.1.0, please check validity of commands below to
make command for any future versions)
tar -zxvf cdf36\_1-dist-all.tar.gz cd cdf36\* make OS=linux
ENV=gnu CURSES=yes FORTRAN=no UCOPTIONS=-O2 SHARED=yes all sudo
make INSTALLDIR=/usr/local/cdf install
b) SpacePy (Los Alamos):
https://sourceforge.net/projects/spacepy/files/spacepy/ (tested
with 0.1.6)
sudo pip install spacepy
C) Install MagPy
a) Using pip
sudo pip install GeomagPy
- specific version: sudo pip install GeomagPy==v0.3.9
b) Using github (latest development versions)
git clone git://github.com/GeomagPy/MagPy.git cd MagPy\* sudo
python setup.py install
1.5.2 Windows
^^^^^^^^^^^^^
| Tested on XP, Win7, Win10 a) Get a current version of Python(x,y) and
install it optionally select packages ffnet and netcdf during install
- for cdf support b) Download nasaCDF packages and install (see links
above) c) get python-spacepy package d) download and unpack
GeomagPy-x.x.x.tar.gz e) open a command window f) go to the unpacked
directory e.g. cd
c::raw-latex:`\user`:raw-latex:`\Downloads`:raw-latex:`\GeomagPy`
| g) execute "setup.py install"
2. A quick guide to MagPy
-------------------------
written by R. Leonhardt, R. Bailey (April 2017)
2.1 Getting started
~~~~~~~~~~~~~~~~~~~
Start python. Import all stream methods and classes using:
::
from magpy.stream import *
Please note that this import will shadow any already existing ``read``
method.
2.2 Reading and writing data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MagPy supports the following data formats and thus conversions between
them: - WDC: World Data Centre format - JSON: JavaScript Object Notation
- IMF: Intermagnet Format - IAF: Intermagnet Archive Format - NEIC: WGET
data from USGS - NEIC - IAGA: IAGA 2002 text format - IMAGCDF:
Intermagnet CDF Format - GFZKP: GeoForschungsZentrum KP-Index format -
GSM19/GSM90: Output formats from GSM magnetometers - POS1: POS-1 binary
output - BLV: Baseline format Intermagnet - IYFV: Yearly mean format
Intermagnet
... and many others. To get a full list, use:
::
from magpy.stream import *
print(PYMAG_SUPPORTED_FORMATS)
You will find several example files provided with MagPy. The ``cdf``
file is stored along with meta information in NASA's common data format
(cdf). Reading this file requires a working installation of Spacepy cdf.
If you do not have any geomagnetic data file you can access example data
by using the following command (after ``import *``):
::
data = read(example1)
The data from ``example1`` has been read into a MagPy *DataStream* (or
*stream*) object. Most data processing routines in MagPy are applied to
data streams.
Several example data sets are provided within the MagPy package:
- ``example1``: `INTERMAGNET <http://www.intermagnet.org>`__ CDF
(ImagCDF) file with 1 second xyzf data
- ``example2``: `INTERMAGNET <http://www.intermagnet.org>`__ Archive
format (IAF) file for one month with 1 min, 1 hour, K and mean data
- ``example3``: MagPy readable DI data file with data from 1 single DI
measurement
- ``example4``: MagPy Basevalue file (PYSTR) with analysis results of
several DI data
2.2.1 Reading
^^^^^^^^^^^^^
For a file in the same directory:
::
data = read(r'myfile.min')
... or for specific paths in Linux:
::
data = read(r'/path/to/file/myfile.min')
... or for specific paths in Windows:
::
data = read(r'c:\path\to\file\myfile.min')
Pathnames are related to your operating system. In this guide we will
assume a Linux system. Files that are read in are uploaded to the memory
and each data column (or piece of header information) is assigned to an
internal variable (key). To get a quick overview of the assigned keys in
any given stream (``data``) you can use the following method:
::
print(data._get_key_headers() )
2.2.2 Writing
^^^^^^^^^^^^^
After loading data from a file, we can save the data in the standard
IAGA02 and IMAGCDF formats with the following commands.
To create an IAGA-02 format file, use:
::
data.write(r'/path/to/diretory/',format_type='IAGA')
To create an `INTERMAGNET <http://www.intermagnet.org>`__ CDF (ImagCDF)
file:
::
data.write(r'/path/to/diretory/',format_type='IMAGCDF')
The filename will be created automatically according to the defined
format. By default, daily files are created and the date is added to the
filename in-between the optional parameters ``filenamebegins`` and
``filenameends``. If ``filenameends`` is missing, ``.txt`` is used as
default.
2.2.3 Other possibilities for reading files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
To read all local files ending with .min within a directory (creates a
single stream of all data):
::
data = read(r'/path/to/file/*.min')
Getting magnetic data directly from an online source such as the WDC:
::
data = read(r'ftp://thewellknownaddress/single_year/2011/fur2011.wdc')
Getting *kp* data from the GFZ Potsdam:
::
data = read(r'http://www-app3.gfz-potsdam.de/kp_index/qlyymm.tab')
(Please note: data access and usage is subjected to the terms and
conditions of the individual data provider. Please make sure to read
them before accessing any of these products.)
No format specifications are required for reading. If MagPy can handle
the format, it will be automatically recognized.
Getting data for a specific time window for local files:
::
data = read(r'/path/to/files/*.min',starttime="2014-01-01", endtime="2014-05-01")
... and remote files:
::
data = read(r'ftp://address/fur2013.wdc',starttime="2013-01-01", endtime="2013-02-01")
Reading data from the INTERMAGNET Webservice (starting soon):
::
data = read('http://www.intermagnet.org/test/ws/?id=WIC')
2.2.4 Selecting timerange
^^^^^^^^^^^^^^^^^^^^^^^^^
The stream can be trimmed to a specific time interval after reading by
applying the trim method, e.g. for a specific month:
::
data = data.trim(starttime="2013-01-01", endtime="2013-02-01")
.. raw:: html
<!--#### 2.2.5 Tutorial
For the ongoing quick example please use the following steps. This will create daily IAGA02 files within the directory. Please make sure that the directory is empty before writing data to it.
A) Load example data
Within the MagPy package, several example data sets are provided:
example1: [INTERMAGNET] CDF (ImagCDF) file with 1 second data
example2: [INTERMAGNET] Archive format (IAF) file with 1 min, 1 hour, K and mean data
example3: MagPy readable DI data file with data from 1 single DI measurement
example4: MagPy Basevalue file (PYSTR) with analysis results of several DI data
# Replace example1 with a full path, if you have your own data
data = read(example1)
B) Store it locally in your favorite directory
data.write('/tmp/',filenamebegins='MyExample_', format_type='IAGA')
Please note that storing data in a different formt might require additional meta information. Checkout section (i) on how to deal with these aspects.-->
2.3 Getting help on options and usage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.3.1 Python's help function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Information on individual methods and options can be obtained as
follows:
For basic functions:
::
help(read)
For specific methods related to e.g. a stream object "data":
::
help(data.fit)
Note that this requires the existence of a "data" object, which is
obtained e.g. by data = read(...). The help text can also be shown by
directly calling the *DataStream* object method using:
::
help(DataStream.fit)
.. raw:: html
<!--#### 2.3.2 Tutorial
help(data.fit)-->
2.3.2 MagPy's logging system
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MagPy automatically logs many function options and runtime information,
which can be useful for debugging purposes. This log is saved by default
in the temporary file directory of your operating system, e.g. for Linux
this would be ``/tmp/magpy.log``. The log is formatted as follows with
the date, module and function in use and the message leve
(INFO/WARNING/ERROR):
::
2017-04-22 09:50:11,308 INFO - magpy.stream - Initiating MagPy...
Messages on the WARNING and ERROR level will automatically be printed to
shell. Messages for more detailed debugging are written at the DEBUG
level and will not be printed to the log unless an additional handler
for printing DEBUG is added.
Custom loggers can be defined by creating a logger object after
importing MagPy and adding handlers (with formatting):
::
from magpy.stream import *
import logging
logger = logging.getLogger()
hdlr = logging.FileHandler('testlog.log')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
The logger can also be configured to print to shell (stdout, without
formatting):
::
import sys
logger = logging.getLogger()
stdoutlog = logging.StreamHandler(sys.stdout)
logger.addHandler(stdoutlog)
2.4 Plotting
~~~~~~~~~~~~
You will find some example plots at the `Conrad
Observatory <http://www.conrad-observatory.at>`__.
2.4.1 Quick (and not dirty)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
import magpy.mpplot as mp
mp.plot(data)
2.4.2 Some options
^^^^^^^^^^^^^^^^^^
Select specific keys to plot:
::
mp.plot(data,variables=['x','y','z'])
Defining a plot title and specific colors (see ``help(mp.plot)`` for
list and all options):
::
mp.plot(data,variables=['x','y'],plottitle="Test plot",
colorlist=['g', 'c'])
2.4.3 Data from multiple streams
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Various datasets from multiple data streams will be plotted above one
another. Provide a list of streams and an array of keys:
::
mp.plotStreams([data1,data2],[['x','y','z'],['f']])
.. raw:: html
<!--#### 2.4.4 Tutorial
Read a second stream
otherdata = read(WDC)
Plot xyz data from both streams
mp.plotStreams([data,otherdata]) -->
2.5 Flagging data
~~~~~~~~~~~~~~~~~
The flagging procedure allows the observer to mark specific data points
or ranges. Falgs are useful for labelling data spikes, storm onsets,
pulsations, disturbances, lightning strikes, etc. Each flag is asociated
with a comment and a type number. The flagtype number ranges between 0
and 4:
- 0: normal data with comment (e.g. "Hello World")
- 1: data marked by automated analysis (e.g. spike)
- 2: data marked by observer as valid geomagnetic signature (e.g. storm
onset, pulsation). Such data cannot be marked invalid by automated
procedures
- 3: data marked by observer as invalid (e.g. lightning, magnetic
disturbance)
- 4: merged data (e.g. data inserted from another source/instrument as
defined in the comment)
Flags can be stored along with the data set (requires CDF format output)
or separately in a binary archive. These flags can then be applied to
the raw data again, ascertaining perfect reproducibility.
2.5.1 Mark data spikes
^^^^^^^^^^^^^^^^^^^^^^
Load a data record with data spikes:
::
datawithspikes = read(example1)
Mark all spikes using the automated function ``flag_outlier`` with
default options:
::
flaggeddata = datawithspikes.flag_outlier(timerange=timedelta(minutes=1),threshold=3)
Show flagged data in a plot:
::
mp.plot(flaggeddata,['f'],annotate=True)
2.5.2 Flag time range
^^^^^^^^^^^^^^^^^^^^^
Flag a certain time range:
::
flaglist = flaggeddata.flag_range(keys=['f'], starttime='2012-08-02T04:33:40',
endtime='2012-08-02T04:44:10',
flagnum=3, text="iron metal near sensor")
Apply these flags to the data:
::
flaggeddata = flaggeddata.flag(flaglist)
Show flagged data in a plot:
::
mp.plot(flaggeddata,['f'],annotate=True)
2.5.3 Save flagged data
^^^^^^^^^^^^^^^^^^^^^^^
To save the data together with the list of flags to a CDF file:
::
flaggeddata.write('/tmp/',filenamebegins='MyFlaggedExample_', format_type='PYCDF')
To check for correct save procedure, read and plot the new file:
::
newdata = read("/tmp/MyFlaggedExample_*")
mp.plot(newdata,annotate=True, plottitle='Reloaded flagged CDF data')
2.5.4 Save flags separately
^^^^^^^^^^^^^^^^^^^^^^^^^^^
To save the list of flags seperately from the data in a pickled binary
file:
::
fullflaglist = flaggeddata.extractflags()
saveflags(fullflaglist,"/tmp/MyFlagList.pkl"))
These flags can be loaded in and then reapplied to the data set:
::
data = read(example1)
flaglist = loadflags("/tmp/MyFlagList.pkl")
data = data.flag(flaglist)
mp.plot(data,annotate=True, plottitle='Raw data with flags from file')
2.5.5 Drop flagged data
^^^^^^^^^^^^^^^^^^^^^^^
For some analyses it is necessary to use "clean" data, which can be
produced by dropping data flagged as invalid (e.g. spikes). By default,
the following method removes all data marked with flagtype numbers 1 and
3.
::
cleandata = flaggeddata.remove_flagged()
mp.plot(cleandata, ['f'], plottitle='Flagged data dropped')
2.6 Basic methods
~~~~~~~~~~~~~~~~~
2.6.1 Filtering
^^^^^^^^^^^^^^^
MagPy's ``filter`` uses the settings recommended by
`IAGA <http://www.iaga-aiga.org/>`__/`INTERMAGNET <http://www.intermagnet.org>`__.
Ckeck ``help(data.filter)`` for further options and definitions of
filter types and pass bands.
First, get the sampling rate before filtering in seconds:
::
print("Sampling rate before [sec]:", cleandata.samplingrate())
Filter the data set with default parameters (``filter`` automatically
chooses the correct settings depending on the provided sanmpling rate):
::
filtereddata = cleandata.filter()
Get sampling rate and filtered data after filtering (please note that
all filter information is added to the data's meta information
dictionary (data.header):
::
print("Sampling rate after [sec]:", filtereddata.samplingrate())
print("Filter and pass band:", filtereddata.header.get('DataSamplingFilter',''))
2.6.2 Coordinate transformation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Assuming vector data in columns [x,y,z] you can freely convert between
xyz, hdz, and idf coordinates:
::
cleandata = cleandata.xyz2hdz()
2.6.3 Calculate delta F
^^^^^^^^^^^^^^^^^^^^^^^
If the data file contains xyz (hdz, idf) data and an independently
measured f value, you can calculate delta F between the two instruments
using the following:
::
cleandata = cleandata.delta_f()
mp.plot(cleandata,plottitle='delta F')
2.6.4 Calculate Means
^^^^^^^^^^^^^^^^^^^^^
Mean values for certain data columns can be obtained using the ``mean``
method. The mean will only be calculated for data with the percentage of
valid data (in contrast to missing data) points not falling below the
value given by the percentage option (default 95). If too much data is
missing, then no mean is calulated and the function returns NaN.
::
print(cleandata.mean('df', percentage=80))
The median can be calculated by defining the ``meanfunction`` option:
::
print(cleandata.mean('df', meanfunction='median'))
2.6.5 Applying offsets
^^^^^^^^^^^^^^^^^^^^^^
Constant offsets can be added to individual columns using the ``offset``
method with a dictionary defining the MagPy stream column keys and the
offset to be applied (datetime.timedelta object for time column, float
for all others):
::
offsetdata = cleandata.offset({'time':timedelta(seconds=0.19),'f':1.24})
2.6.6 Scaling data
^^^^^^^^^^^^^^^^^^
Individual columns can also be multiplied by values provided in a
dictionary:
::
multdata = cleandata.multiply({'x':-1})
2.6.7 Fit functions
^^^^^^^^^^^^^^^^^^^
MagPy offers the possibility to fit functions to data using either
polynomial functions or cubic splines (default):
::
func = cleandata.fit(keys=['x','y','z'],knotstep=0.1)
mp.plot(cleandata,variables=['x','y','z'],function=func)
2.6.8 Derivatives
^^^^^^^^^^^^^^^^^
Time derivatives, which are useful to identify outliers and sharp
changes, are calculated as follows:
::
diffdata = cleandata.differentiate(keys=['x','y','z'],put2keys = ['dx','dy','dz'])
mp.plot(diffdata,variables=['dx','dy','dz'])
2.6.9 All methods at a glance
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For a summary of all supported methods, see the section **List of all
MagPy methods** below.
2.7 Geomagnetic analysis
~~~~~~~~~~~~~~~~~~~~~~~~
2.7.1 Determination of K indices
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
MagPy supports the FMI method for determination of K indices. Please
consult the MagPy publication for details on this method and
application.
A month of one minute data is provided in ``example2``, which
corresponds to an `INTERMAGNET <http://www.intermagnet.org>`__ IAF
archive file. Reading a file in this format will load one minute data by
default. Accessing hourly data and other information is described below.
::
data2 = read(example2)
kvals = data2.k_fmi()
The determination of K values will take some time as the filtering
window is dynamically adjusted. In order to plot the original data (H
component) and K values together, we now use the multiple stream
plotting method ``plotStreams``. Here you need to provide a list of
streams and an array containing variables for each stream. The
additional options determine the appearance of the plot (limits, bar
chart):
::
mp.plotStreams([data2,kvals],[['x'],['var1']],
specialdict = [{},{'var1':[0,9]}],
symbollist=['-','z'],
bartrange=0.06)
``'z'`` in ``symbollist`` refers to the second subplot (K), which should
be plotted as bars rather than the standard line (``'-'``).
2.7.2 Automated geomagnetic storm detection
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Geomagnetic storm detection is supported by MagPy using two procedures
based on wavelets and the Akaike Information Criterion (AIC) as outlined
in detail in Bailey and Leonhardt (2016). A basic example of usage to
find an SSC using a Discrete Wavelet Transform (DWT) is shown below:
::
from magpy.stream import read
from magpy.opt.stormdet import seekStorm
stormdata = read("LEMI025_2015-03-17.cdf") # 1s variometer data
stormdata = stormdata.xyz2hdz()
stormdata = stormdata.smooth('x', window_len=25)
detection, ssc_list = seekStorm(stormdata, method="MODWT")
print("Possible SSCs detected:", ssc_list)
The method ``seekStorm`` will return two variables: ``detection`` is
True if any detection was made, while ``ssc_list`` is a list of
dictionaries containing data on each detection. Note that this method
alone can return a long list of possible SSCs (most incorrectly
detected), particularly during active storm times. It is most useful
when additional restrictions based on satellite solar wind data apply
(currently only optimised for ACE data, e.g. from the NOAA website):
::
satdata_ace_1m = read('20150317_ace_swepam_1m.txt')
satdata_ace_5m = read('20150317_ace_epam_5m.txt')
detection, ssc_list, sat_cme_list = seekStorm(stormdata,
satdata_1m=satdata_ace_1m, satdata_5m=satdata_ace_5m,
method='MODWT', returnsat=True)
print("Possible CMEs detected:", sat_cme_list)
print("Possible SSCs detected:", ssc_list)
2.7.3 Sq analysis
^^^^^^^^^^^^^^^^^
Methods are currently in preparation.
2.7.4 Validity check of data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A common and important application used in the geomagnetism community is
a general validity check of geomagnetic data to be submitted to the
official data repositories `IAGA <http://www.iaga-aiga.org/>`__, WDC, or
`INTERMAGNET <http://www.intermagnet.org>`__. Please note: this is
currently under development and will be extended in the near future. A
'one-click' test method will be included in xmagpy in the future,
checking:
A) Validity of data formats, e.g.:
::
data = read('myiaffile.bin', debug=True)
B) Completeness of meta-information
C) Conformity of applied techniques to respective rules
D) Internal consistency of data
E) Optional: regional consistency
2.7.5 Spectral Analysis and Noise
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For analysis of the spectral content of data, MagPy provides two basic
plotting methods. ``plotPS`` will calculate and display a power spectrum
of the selected component. ``plotSpectrogram`` will plot a spectrogram
of the time series. As usual, there are many options for plot window and
processing parameters that can be accessed using the help method.
::
data = read(example1)
mp.plotPS(data,key='f')
mp.plotSpectrogram(data,['f'])
2.8 Handling multiple streams
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.8.1 Merging streams
^^^^^^^^^^^^^^^^^^^^^
Merging data comprises combining two streams into one new stream. This
includes adding a new column from another stream, filling gaps with data
from another stream or replacing data from one column with data from
another stream. The following example sketches the typical usage:
::
print("Data columns in data2:", data2._get_key_headers())
newstream = mergeStreams(data2,kvals,keys=['var1'])
print("Data columns after merging:", data2._get_key_headers())
mp.plot(newstream, ['x','y','z','var1'],symbollist=['-','-','-','z'])
If column ``var1`` does not existing in data2 (as above), then this
column is added. If column ``var1`` had already existed, then missing
data would be inserted from stream ``kvals``. In order to replace any
existing data, use option ``mode='replace'``.
2.8.2 Differences between streams
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sometimes it is necessary to examine the differences between two data
streams e.g. differences between the F values of two instruments running
in parallel at an observatory. The method ``subtractStreams`` is
provided for this analysis:
::
diff = subtractStreams(data1,data2,keys=['f'])
2.9 The art of meta-information
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Each data set is accompanied by a dictionary containing meta-information
for this data. This dictionary is completely dynamic and can be filled
freely, but there are a number of predefined fields that help the user
provide essential meta-information as requested by
`IAGA <http://www.iaga-aiga.org/>`__,
`INTERMAGNET <http://www.intermagnet.org>`__ and other data providers.
All meta information is saved only to MagPy-specific archive formats
PYCDF and PYSTR. All other export formats save only specific information
as required by the projected format.
The current content of this dictionary can be accessed by:
::
data = read(example1)
print(data.header)
Information is added/changed by using:
::
data.header['SensorName'] = 'FGE'
Individual information is obtained from the dictionary using standard
key input:
::
print(data.header.get('SensorName'))
If you want to have a more readable list of the header information, do:
::