Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

DOCUMENTATION.markdown 8.99 KB
Newer Older
1

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
2
# swmfpy
3

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
4
5
SWMF Tools
==========
6

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
7
8
A collection of tools to read, write, visualize with the
Space Weather Modeling Framework (SWMF).
9

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
10
11
Modules
-------
12

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
13
These are automatically imported.
14

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
15
16
17
- swmfpy.io: Input/Output tools.
- swmfpy.paramin: PARAM.in editing tools.
- swmfpy.web: Internet data downloading/uploading tools.
18

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
19
20
Extra Modules
-------------
21

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
22
These are not automatically imported. Might have extra dependancies.
23

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
24
*None yet.*
25
26


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
27
28
# swmfpy.web
Tools to retrieve and send data on the web.
29

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
30
31
SWMF Web Tools
==============
32

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
33
34
Here are a collection of tools to work with data on the internet. Thus,
this module mostly requires an internet connection.
35
36


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
37
38
39
40
41
## get_omni_data
```python
get_omni_data(time_from, time_to, **kwargs)
```
Retrieve omni solar wind data over http.
42

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
43
44
45
This will download omni data from https://spdf.gsfc.nasa.gov/pub/data/omni
and put it into a dictionary. If your data is large, then make a csv and
use swmfpy.io.read_omni_data().
46

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
47
48
49
50
51
Args:
    time_from (datetime.datetime): The start time of the solar wind
                                   data that you want to receive.
    time_to (datetime.datetime): The end time of the solar wind data
                                 you want to receive.
52

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
53
54
55
Returns:
    dict: This will be a list of *all* columns
          available in the omni data set.
56

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
57
58
59
60
Examples:
    ```python
    import datetime
    import swmfpy.web
61

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
62
63
64
65
66
    storm_start = datetime.datetime(year=2000, month=1, day=1)
    storm_end = datetime.datetime(year=2000, month=2, day=15)
    data = swmfpy.web.get_omni_data(time_from=storm_start,
                                    time_to=storm_end)
    ```
67
68


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
69
70
71
72
73
## download_magnetogram_adapt
```python
download_magnetogram_adapt(time, map_type='fixed', **kwargs)
```
This routine downloads GONG ADAPT magnetograms.
74

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
75
76
77
Downloads ADAPT magnetograms from ftp://gong2.nso.edu/adapt/maps/gong/
to a local directory. It will download all maps with the regex file
pattern: adapt4[0,1]3*yyyymmddhh
78

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
79
80
81
82
83
84
85
86
Args:
    time (datetime.datetime): Time in which you want the magnetogram.
    map_type (str): (default: 'fixed')
                    Choose either 'fixed' or 'central' for
                    the map type you want.
    **kwargs:
        download_dir (str): (default is current dir) Relative directory
                            where you want the maps to be downloaded.
87

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
88
89
Returns:
    str: First unzipped filename found.
90

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
91
92
93
94
95
96
Raises:
    NotADirectoryError: If the adapt maps directory
                        is not found on the server.
    ValueError: If map_type is not recognized.
                (i.e. not 'fixed' or 'central')
    FileNotFoundError: If maps were not found.
97

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
98
99
100
Examples:
    ```python
    import datetime as dt
101

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
102
103
104
105
106
107
    # Use datetime objects for the time
    time_flare = dt.datetime(2018, 2, 12, hour=10)
    swmfpy.web.download_magnetogram_adapt(time=time_flare,
                                          map_type='central',
                                          download_dir='./mymaps/')
    ```
108
109


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
110
111
# swmfpy.io
Input/Output tools
112

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
113
114
Input/Output
============
115

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
116
Here are tools to read and write files relating to SWMF.
117

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
118
TODO: Move pandas dependancy elsewhere.
119
120


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
## read_wdc_ae
```python
read_wdc_ae(wdc_filename)
```
Read an auroral electrojet (AE) indeces from Kyoto's World Data Center
text file into a dictionary of lists.

Args:
 wdc_filename (str): Filename of wdc data from
                     http://wdc.kugi.kyoto-u.ac.jp/
Returns:
 dict: {
        Auroral indeces 'AL', 'AE', 'AO', 'AU' (int): {
             'times' (datetime.datetime): List of datetime objects
                                          corresponding to time in UT.
             'values' (int): List of indeces.
       }


## read_wdc_asy_sym
```python
read_wdc_asy_sym(wdc_filename)
```
Reads a WDC file for ASY/SYM data.
145

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
146
147
148
Reads an ASY/SYM file downloaded from
http://wdc.kugi.kyoto-u.ac.jp/aeasy/index.html
and puts it into a dictionary.
149

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
150
151
Args:
    wdc_filename (str): Relative filename (or file handle no.) to read.
152

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
153
154
155
Returns:
    dict: of values.
    {'[ASY-SYM]-[D-H]': 'times': [], 'values': []}
156

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
157
158
159
160
161
162
163
164
165
166
Examples:
    ```
    indeces = swmfpy.io.read_wdc_asy_sym('wdc.dat')
    # Plot data
    plt.plot(indeces['SYM-H']['times'],
             indeces['SYM-H']['values'],
             label='SYM-H [nT]'
             )
    plt.xlabel('Time [UT]')
    ```
167

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
168
Important to note if there is bad data it will be filled as None.
169
170


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
171
172
173
174
## read_omni_csv
```python
read_omni_csv(filename, filtering=False, **kwargs)
```
175
176
177
Take an OMNI csv file from cdaweb.sci.gsfc.nasa.gov
and turn it into a pandas.DataFrame.

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
178
179
180
181
182
183
184
185
186
187
Args:
    fnames (dict): dict with filenames from omni .lst files.
                   The keys must be: density, temperature,
                                     magnetic_field, velocity
    filtering (bool): default=False Remove points where the value
                      is >sigma (default: sigma=3) from mean.
    **kwargs:
        coarseness (int): default=3, Number of standard deviations
                          above which to remove if filtering=True.
        clean (bool): default=True, Clean the omni data of bad data points
188

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
189
190
Returns:
    pandas.DataFrame: object with solar wind data
191
192
193
194
195
196
197

Make sure to download the csv files with cdaweb.sci.gsfc.nasa.gov
the header seperated into a json file for safety.

This only tested with OMNI data specifically.


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
198
199
200
201
## write_imf_input
```python
write_imf_input(data, outfilename='IMF.dat', enable_rb=True, **kwargs)
```
202
203
204
Writes the pandas.DataFrame into an input file
that SWMF can read as input IMF (IMF.dat).

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
205
206
207
208
209
210
Args:
    data: pandas.DataFrame object with solar wind data
    outfilename: The output file name for ballistic solar wind data.
                 (default: "IMF.dat")
    enable_rb: Enables solar wind input for the radiation belt model.
               (default: True)
211
212
213
214
215
216

Other paramaters:
    gse: (default=False)
        Use GSE coordinate system for the file instead of GSM default.


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
217
218
219
220
221
222
## read_gm_log
```python
read_gm_log(filename, colnames=None, dtypes=None, index_time=True)
```
Make a dictionary out of the indeces outputted
from the GM model log.
223

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
224
225
226
227
228
229
230
231
232
Args:
    filename (str): The relative filename as a string. (or file handle no.)
    colnames ([str]): (default: None) Supply the name of the columns.
                                      If None it will use second line
                                      of log file.
    dtypes ([types]): (default: None) Provide types for the columns, if
                                      None then all will be float.
    index_time (bool): (default: True) Make a column of dt.datetime objects
                                       in dictionary key 'Time [UT]'.
233

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
234
235
Returns:
    Dictionary of the log file
236

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
237
238
239
240
241
Examples:
    To plot AL and Dst get the log files
    ```
    geo = swmfpy.io.read_gm_log('run/GM/IO2/geoindex_e20140215-100500.log')
    dst = swmfpy.io.read_gm_log('run/GM/IO2/log_e20140215-100500.log')
242

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
243
244
245
    # Plot AL indeces
    plt.plot(geo['times', geo['AL'])
    ```
246
247
248



Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
249
# swmfpy.paramin
250

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
251
252
PARAM.in Tools
--------------
253

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
254
These tools are to help script the editing of PARAM.in files.
255
256


Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
257
258
259
260
## replace_command
```python
replace_command(parameters, input_file, output_file='PARAM.in')
```
261
262
263
264
Replace values for the parameters in a PARAM.in file.

Note, if you have repeat commands this will replace all the repeats.

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
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
Args:
    parameters (dict): Dictionary of strs with format
                    replace = {'#COMMAND': ['value', 'comments', ...]}
                    This is case sensitive.
    input_file (str): String of PARAM.in file name.
    output_file (str): (default 'PARAM.in') The output file to write to.
                       A value of None will not output a file.
Returns:
    A list of lines of the PARAM.in file that would be outputted.

Raises:
    TypeError: If a value given couldn't be converted to string.

Examples:
    ```
    change['#SOLARWINDFILE'] = [['T', 'UseSolarWindFile'],
                                ['new_imf.dat', 'NameSolarWindFile']]
    # This will overwrite PARAM.in
    swmfpy.paramin.replace('PARAM.in.template', change)
    ```


## read_command
```python
read_command(command, paramin_file='PARAM.in', **kwargs)
```
Get parameters of a certain command in PARAM.in file.
292

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
293
This will find the [`COMMAND`](#COMMAND) and return a list of values for the parameters.
294

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
295
296
297
298
299
300
301
Args:
    command (str): This is the [`COMMAND`](#COMMAND) you're looking for.
    paramin_file (str): (default: 'PARAM.in') The file in which you're
                        looking for the command values.
    **kwargs:
        num_of_values (int): (default: None) Number of values to take from
                             command.
302

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
303
304
305
Returns:
    list: Values found for the [`COMMAND`](#COMMAND) in file. Index 0 is [`COMMAND`](#COMMAND) and
          the values follow (1 for first argument...)
306

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
307
308
Raises:
    ValueError: When the [`COMMAND`](#COMMAND) is not found.
309

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
310
311
312
313
314
Examples:
    start_time = swmfpy.paramin.read_command('#STARTTIME')
    end_time = swmfpy.paramin.read_command('#ENDTIME')
    print('Starting month is ', start_time[1])
    print('Ending month is ', end_time[1])
315

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
316
317
318
This will treat all following lines as values for the command. To suppress
this, try using the `num_of_values` keyword. This is helpful if your
PARAM.in is comment heavy.
319