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 9.52 KB
Newer Older
Qusai Al Shidi's avatar
Qusai Al Shidi committed
1
2
3
4
5
6
7
# Table of Contents

* [swmfpy](#.swmfpy)
* [swmfpy.io](#.swmfpy.io)
  * [read\_wdc\_ae](#.swmfpy.io.read_wdc_ae)
  * [read\_wdc\_asy\_sym](#.swmfpy.io.read_wdc_asy_sym)
  * [read\_gm\_log](#.swmfpy.io.read_gm_log)
8
* [swmfpy.tools](#.swmfpy.tools)
Qusai Al Shidi's avatar
Qusai Al Shidi committed
9
10
11
* [swmfpy.paramin](#.swmfpy.paramin)
  * [replace\_command](#.swmfpy.paramin.replace_command)
  * [read\_command](#.swmfpy.paramin.read_command)
12
13
14
15
* [swmfpy.web](#.swmfpy.web)
  * [get\_omni\_data](#.swmfpy.web.get_omni_data)
  * [download\_magnetogram\_hmi](#.swmfpy.web.download_magnetogram_hmi)
  * [download\_magnetogram\_adapt](#.swmfpy.web.download_magnetogram_adapt)
Qusai Al Shidi's avatar
Qusai Al Shidi committed
16

Qusai Al Shidi's avatar
Qusai Al Shidi committed
17
18
<a name=".swmfpy"></a>
## swmfpy
19

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

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
23
24
Modules
-------
25

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
26
These are automatically imported.
27

28
29
30
- `swmfpy.io` Input/Output tools.
- `swmfpy.paramin` PARAM.in editing tools.
- `swmfpy.web` Internet data downloading/uploading tools.
31

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
32
33
Extra Modules
-------------
34

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

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
37
*None yet.*
38

Qusai Al Shidi's avatar
Qusai Al Shidi committed
39
40
<a name=".swmfpy.io"></a>
## swmfpy.io
41

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
42
Input/Output tools
43

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

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
46
TODO: Move pandas dependancy elsewhere.
47

Qusai Al Shidi's avatar
Qusai Al Shidi committed
48
49
<a name=".swmfpy.io.read_wdc_ae"></a>
#### read\_wdc\_ae
50

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
51
52
53
```python
read_wdc_ae(wdc_filename)
```
Qusai Al Shidi's avatar
Qusai Al Shidi committed
54

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
55
56
57
Read an auroral electrojet (AE) indeces from Kyoto's World Data Center
text file into a dictionary of lists.

Qusai Al Shidi's avatar
Qusai Al Shidi committed
58
59
60
61
62
**Arguments**:

  
- `wdc_filename` _str_ - Filename of wdc data from
  http://wdc.kugi.kyoto-u.ac.jp/
Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
63

Qusai Al Shidi's avatar
Qusai Al Shidi committed
64
65
**Returns**:

Qusai Al Shidi's avatar
Qusai Al Shidi committed
66
67
68
- `dict` - Auroral indeces 'AL', 'AE', 'AO', 'AU'
- `datetime.datetime` - 'times'
- `int` - 'values'
Qusai Al Shidi's avatar
Qusai Al Shidi committed
69
70
71

<a name=".swmfpy.io.read_wdc_asy_sym"></a>
#### read\_wdc\_asy\_sym
Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
72
73
74
75

```python
read_wdc_asy_sym(wdc_filename)
```
Qusai Al Shidi's avatar
Qusai Al Shidi committed
76

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
77
Reads a WDC file for ASY/SYM data.
78

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

Qusai Al Shidi's avatar
Qusai Al Shidi committed
83
**Arguments**:
84

Qusai Al Shidi's avatar
Qusai Al Shidi committed
85
86
- `wdc_filename` _str_ - Relative filename (or file handle no.) to read.
  
87

Qusai Al Shidi's avatar
Qusai Al Shidi committed
88
**Returns**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
89

Qusai Al Shidi's avatar
Qusai Al Shidi committed
90
- `dict` - of values. {'[ASY-SYM]-[D-H]': 'times': [], 'values': []}
Qusai Al Shidi's avatar
Qusai Al Shidi committed
91
  
Qusai Al Shidi's avatar
Qusai Al Shidi committed
92

Qusai Al Shidi's avatar
Qusai Al Shidi committed
93
**Examples**:
94

Qusai Al Shidi's avatar
Qusai Al Shidi committed
95
96
97
98
99
100
101
102
103
104
105
  ```python
  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]')
  ```
  
  Important to note if there is bad data it will be filled as None.
106

Qusai Al Shidi's avatar
Qusai Al Shidi committed
107
108
<a name=".swmfpy.io.read_gm_log"></a>
#### read\_gm\_log
109

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
110
111
112
```python
read_gm_log(filename, colnames=None, dtypes=None, index_time=True)
```
Qusai Al Shidi's avatar
Qusai Al Shidi committed
113

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
114
115
Make a dictionary out of the indeces outputted
from the GM model log.
116

Qusai Al Shidi's avatar
Qusai Al Shidi committed
117
**Arguments**:
118

Qusai Al Shidi's avatar
Qusai Al Shidi committed
119
120
121
122
123
124
125
126
127
- `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]'.
  
128

Qusai Al Shidi's avatar
Qusai Al Shidi committed
129
**Returns**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
130

Qusai Al Shidi's avatar
Qusai Al Shidi committed
131
- `dict` - Dictionary of the log file
Qusai Al Shidi's avatar
Qusai Al Shidi committed
132
  
133

Qusai Al Shidi's avatar
Qusai Al Shidi committed
134
**Examples**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
135

Qusai Al Shidi's avatar
Qusai Al Shidi committed
136
137
138
139
140
141
142
143
  To plot AL and Dst get the log files
  ```python
  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')
  
  # Plot AL indeces
  plt.plot(geo['times', geo['AL'])
  ```
144

145
146
147
148
149
150
<a name=".swmfpy.tools"></a>
## swmfpy.tools

Tools to be used in swmfpy functions and classes. Some of the functions are
*hidden functions*.

Qusai Al Shidi's avatar
Qusai Al Shidi committed
151
152
<a name=".swmfpy.paramin"></a>
## swmfpy.paramin
153

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

Qusai Al Shidi's avatar
Qusai Al Shidi committed
156
157
<a name=".swmfpy.paramin.replace_command"></a>
#### replace\_command
158

Qusai Al Shidi's avatar
Autodoc    
Qusai Al Shidi committed
159
160
161
```python
replace_command(parameters, input_file, output_file='PARAM.in')
```
Qusai Al Shidi's avatar
Qusai Al Shidi committed
162

163
164
165
166
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
Qusai Al Shidi committed
167
**Arguments**:
168

Qusai Al Shidi's avatar
Qusai Al Shidi committed
169
- `parameters` _dict_ - Dictionary of strs with format
170
  replace = 'COMMAND': ['value', 'comments', ...]
Qusai Al Shidi's avatar
Qusai Al Shidi committed
171
172
173
174
  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.
175

Qusai Al Shidi's avatar
Qusai Al Shidi committed
176
**Returns**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
177

Qusai Al Shidi's avatar
Qusai Al Shidi committed
178
179
  A list of lines of the PARAM.in file that would be outputted.
  
180

Qusai Al Shidi's avatar
Qusai Al Shidi committed
181
**Raises**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
182

Qusai Al Shidi's avatar
Qusai Al Shidi committed
183
184
- `TypeError` - If a value given couldn't be converted to string.
  
185

Qusai Al Shidi's avatar
Qusai Al Shidi committed
186
**Examples**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
187

Qusai Al Shidi's avatar
Qusai Al Shidi committed
188
  ```python
189
  change['`SOLARWINDFILE`'] = [['T', 'UseSolarWindFile'],
Qusai Al Shidi's avatar
Qusai Al Shidi committed
190
191
192
193
  ['new_imf.dat', 'NameSolarWindFile']]
  # This will overwrite PARAM.in
  swmfpy.paramin.replace('PARAM.in.template', change)
  ```
Qusai Al Shidi's avatar
Qusai Al Shidi committed
194

Qusai Al Shidi's avatar
Qusai Al Shidi committed
195
196
<a name=".swmfpy.paramin.read_command"></a>
#### read\_command
Qusai Al Shidi's avatar
Qusai Al Shidi committed
197
198

```python
Qusai Al Shidi's avatar
Qusai Al Shidi committed
199
read_command(command, paramin_file='PARAM.in', **kwargs)
Qusai Al Shidi's avatar
Qusai Al Shidi committed
200
201
```

Qusai Al Shidi's avatar
Qusai Al Shidi committed
202
Get parameters of a certain command in PARAM.in file.
Qusai Al Shidi's avatar
Qusai Al Shidi committed
203

204
205
This will find the COMMAND and return a list of
values for the parameters.
Qusai Al Shidi's avatar
Qusai Al Shidi committed
206

Qusai Al Shidi's avatar
Qusai Al Shidi committed
207
**Arguments**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
208

209
- `command` _str_ - This is the COMMAND you're looking for.
Qusai Al Shidi's avatar
Qusai Al Shidi committed
210
211
212
213
214
215
- `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.
  
Qusai Al Shidi's avatar
Qusai Al Shidi committed
216

Qusai Al Shidi's avatar
Qusai Al Shidi committed
217
**Returns**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
218

219
220
- `list` - Values found for the COMMAND in file. Index 0 is
  COMMAND and the values follow (1 for first argument...)
Qusai Al Shidi's avatar
Qusai Al Shidi committed
221
  
Qusai Al Shidi's avatar
Qusai Al Shidi committed
222

Qusai Al Shidi's avatar
Qusai Al Shidi committed
223
**Raises**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
224

225
- `ValueError` - When the COMMAND is not found.
Qusai Al Shidi's avatar
Qusai Al Shidi committed
226
  
Qusai Al Shidi's avatar
Qusai Al Shidi committed
227

Qusai Al Shidi's avatar
Qusai Al Shidi committed
228
**Examples**:
Qusai Al Shidi's avatar
Qusai Al Shidi committed
229

Qusai Al Shidi's avatar
Qusai Al Shidi committed
230
231
232
233
234
235
236
237
238
239
  ```python
  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])
  ```
  
  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.
Qusai Al Shidi's avatar
Qusai Al Shidi committed
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
<a name=".swmfpy.web"></a>
## swmfpy.web

Tools to retrieve and send data on the web.

Here are a collection of tools to work with data on the internet. Thus,
this module mostly requires an internet connection.

<a name=".swmfpy.web.get_omni_data"></a>
#### get\_omni\_data

```python
get_omni_data(time_from, time_to, **kwargs)
```

Retrieve omni solar wind data over http.

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().

**Arguments**:

- `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.
  

**Returns**:

- `dict` - This will be a list of *all* columns
  available in the omni data set.
  

**Examples**:

  ```python
  import datetime
  import swmfpy.web
  
  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)
  ```

<a name=".swmfpy.web.download_magnetogram_hmi"></a>
#### download\_magnetogram\_hmi

```python
download_magnetogram_hmi(mag_time, hmi_map, **kwargs)
```

Downloads HMI vector magnetogram fits files.

This will download vector magnetogram FITS files from
Joint Science Operations Center (JSOC) near a certain hour.

This unfortunately depends on sunpy and drms, if you don't have it try,


```bash
pip install -U --user sunpy drms
```

**Arguments**:

- `mag_time` _datetime.datetime_ - Time after which to find
  vector magnetograms.
- `hmi_map` _str_ - JSOC prefix for hmi maps. Currently can only do
  'hmi.B_720s' and 'hmi.b_synoptic.small'.
  
  **kwargs:
- `download_dir` _str_ - Relative directory to download to.
- `verbose` _bool_ - (default False) print out the files it's downloading.
  

**Returns**:

- `str` - list of filenames downloaded.
  

**Raises**:

- `ImportError` - If module `drms` is not found.
- `FileNotFoundError` - If the JSOC doesn't have the magnetograms for that
  time.
  

**Examples**:

  ```python
  from swmfpy.web import download_magnetogram_hmi
  import datetime as dt
  
  # I am interested in the hmi vector magnetogram from 2014, 2, 18
  time_mag = dt.datetime(2014, 2, 18, 10)  # Around hour 10
  
  # Calling it will download
  filenames = download_magnetogram_hmi(mag_time=time_mag,
  hmi_map='B_720s',
  download_dir='mydir/')
  
  # To see my list
  print('The magnetograms I downloaded are:', filenames)
  
  # You may call and ignore the file list
  download_magnetogram_hmi(mag_time=time_mag,
  hmi_map='b_synoptic_small',
  download_dir='mydir')
  ```

<a name=".swmfpy.web.download_magnetogram_adapt"></a>
#### download\_magnetogram\_adapt

```python
download_magnetogram_adapt(time, map_type='fixed', **kwargs)
```

This routine downloads GONG ADAPT magnetograms.

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

**Arguments**:

- `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.
  

**Returns**:

- `str` - First unzipped filename found.
  

**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.
  

**Examples**:

  ```python
  import datetime as dt
  
  # 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/')
  ```