Verified Commit ffe49527 authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

Changed to more pythonic RuntimeWarning

parents 27995889 3b8c6b89
Pipeline #24244 passed with stage
in 1 minute and 3 seconds
- apt-get update -qy
- apt-get install -y python3 python3-numpy python3-pip
- python3 -m pip install pytest
- python3 -m pip install -r requirements.txt
- python3 -m pip install drms
- python3 -m pytest
......@@ -5,15 +5,24 @@ Changelog:
131 Qusai Al Shidi
137 Qusai Al Shidi
2 Camilla D. K. Harris
1 Gabor Toth
2 Gabor Toth
1 Camilla Dodge Koslowsky Harris
1 TKeebler
- 05ac345 **(2020-06-19, Qusai Al Shidi)**: _C:HEAD -> master, origin/master, origin/HEAD:_ merged amendment :s
- 23c6e26 **(2020-08-05, Gabor Toth)**: _C:HEAD -> carrington_rotation:_ Carrington Rotation function added and sunpy dependency removed
- 8e18ac8 **(2020-08-04, Qusai Al Shidi)**: _C:origin/master, origin/HEAD, master:_ Fixed LaTeX citation string
- 8211554 **(2020-08-04, Qusai Al Shidi)**: _C::_ Fixed install swmfpy line
- f1724e7 **(2020-08-04, Qusai Al Shidi)**: _C::_ Added instructions for python venv and also snippets to contributing page.
- d0a8308 **(2020-07-06, Qusai Al Shidi)**: _C:pybatstools:_ Fixed up `CONTRIBUTING.markdown` to be better and added issues guidelines
- 9549026 **(2020-07-06, Qusai Al Shidi)**: _C::_ Should be `shell` and not `bash` for markdown language alias
- 178dd8e **(2020-06-22, Qusai Al Shidi)**: _C::_ Merge branch 'tecplot-doc-update' into 'master'
- facad25 **(2020-06-22, Camilla Dodge Koslowsky Harris)**: _C::_ Tecplot documentation update
- 05ac345 **(2020-06-19, Qusai Al Shidi)**: _C::_ merged amendment :s
- 8d04c39 **(2020-06-19, Qusai Al Shidi)**: _C::_ optional requires `tecplot`
- 3e676fa **(2020-06-19, Qusai Al Shidi)**: _C::_ optional requires `tecplot`
- 991a3a2 **(2020-06-19, Qusai Al Shidi)**: _C:tag: v2020.5:_ v2020.5 commit
......@@ -76,6 +76,12 @@ def my_new_func(some_args):
# function body
There are helpful snippets for these:
- function $91
- class $92
- file $93
......@@ -15,6 +15,7 @@
* [replace\_command](#.swmfpy.paramin.replace_command)
* [read\_command](#.swmfpy.paramin.read_command)
* [](
* [carrington\_rotation\_number](
* [swmfpy.tecplottools](#.swmfpy.tecplottools)
* [apply\_equations](#.swmfpy.tecplottools.apply_equations)
......@@ -157,13 +158,6 @@ 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,
pip install -U --user sunpy drms
- `mag_time` _datetime.datetime_ - Time after which to find
......@@ -506,6 +500,25 @@ values for the parameters.
Tools to be used in swmfpy functions and classes. Some of the functions are
*hidden functions*.
<a name=""></a>
#### carrington\_rotation\_number
Returns the carrington rotation
- `the_time` _datetime.datetime/str_ - The time to convert to carrington
- `(int)` - Carrington Rotation
<a name=".swmfpy.tecplottools"></a>
## swmfpy.tecplottools
......@@ -10,10 +10,12 @@ This is a work in progress.
Clone into the directory you want to use it.
*Note*: swmfpy also is part of the SWMF and gets cloned into `SWMF/share/Python`. However, if you would like to [develop](CONTRIBUTING.markdown) for swmfpy make a clone and work that way and make a merge request.
There are two methods of installing swmfpy with with a virtual environment and without a virtual environment. Only use the virtual environment if your current environment is giving you trouble.
### Without Python venv
Install with [pip](
......@@ -29,6 +31,45 @@ Then import it into your python project.
import swmfpy
### With Python venv
Use this method if the above method is giving you trouble.
Set up a python 3 virtual environment:
$ python3 -m venv ~/.venv
This is important, make sure that it is in your `.profile` or `.bash_profile`:
$ echo "source ~/.venv/bin/activate" >> ~/.profile
*Note*: You might need to use `activate.csh` instead if using `csh` and `` in your `~/.config/fish/` instead if using `fish` shell. You are most likely using `bash` so no need to worry.
Next install the software. This will take a long time as you will be compiling `numpy` from scratch.
$ source ~/.venv/bin/activate
$ python3 -m pip install --user wheel -vvv
$ python3 -m pip install --user cython -vvv
$ python3 -m pip install --user git+
If you are using `tmux` or `GNU Screen` on a supercomputer you can safely detach your session and power off your computer and come back another time.
This should be fully installed now. You should be able to import:
import swmfpy
### Troubleshooting
If you have followed these carefully and still not been able to install please submit an Issue.
......@@ -46,3 +87,18 @@ Issues
If you are experiencing any issues or bugs please go to the [Issues]( page and create an issue. Make sure you include steps to recreate the problem in your post.
How to cite
You can cite this software on LaTeX like this:
author = {{Al Shidi, Qusai}},
title = {swmfpy},
url = {},
version = {2020.5},
date = {2020-06-19},
......@@ -13,7 +13,7 @@ with open('requirements.txt') as fh_requirements:
author='Qusai Al Shidi',
description='''A collection of tools for the Space Weather Modelling
......@@ -31,5 +31,6 @@ setuptools.setup(
"tecplottools": "tecplot",
"hmi": "drms",
......@@ -18,7 +18,7 @@ These are automatically imported.
__author__ = 'Qusai Al Shidi'
__license__ = 'MIT'
__version__ = '2020.5'
__version__ = '2020.6'
__maintainer__ = 'Qusai Al Shidi'
__email__ = ''
......@@ -5,6 +5,25 @@
__author__ = 'Qusai Al Shidi'
__email__ = ''
import datetime as dt
def carrington_rotation_number(the_time='now'):
"""Returns the carrington rotation
the_time (datetime.datetime/str): The time to convert to carrington
(int): Carrington Rotation
if the_time == 'now':
return carrington_rotation_number(
if isinstance(the_time, str):
return carrington_rotation_number(dt.datetime.fromisoformat(the_time))
return int((the_time.toordinal() - 676715.2247)/27.2753)
def _import_error_string(library):
return (
......@@ -13,14 +13,13 @@ import ftplib
from functools import lru_cache
import gzip
from operator import itemgetter
import os.path
import shutil
import urllib
import urllib.request
from dateutil import rrule
import drms
import numpy as np
import os
from sunpy.coordinates.sun import carrington_rotation_number
from .tools import _nearest
from .tools import _nearest, carrington_rotation_number
# Global defines
# This is straight from the format guide on spdf with nicer names as second col
......@@ -269,12 +268,6 @@ def download_magnetogram_hmi(mag_time, hmi_map='hmi.B_720s', **kwargs):
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,
pip install -U --user sunpy drms
mag_time (datetime.datetime): Time after which to find
vector magnetograms.
......@@ -315,6 +308,7 @@ def download_magnetogram_hmi(mag_time, hmi_map='hmi.B_720s', **kwargs):
import drms
get_urls = {
'hmi.B_720s': _get_urls_hmi_b720,
......@@ -363,9 +357,10 @@ def _get_urls_hmi_b_synoptic_small(client, mag_time):
generator that yields (datetime.datetime, str): Time of magnetogram,
suffix url of magnetogram
import drms
cr_number = int(round(carrington_rotation_number(mag_time)))
query_string = f'hmi.b_synoptic_small[{int(round(cr_number))}]'
cr_number = carrington_rotation_number(mag_time)
query_string = f'hmi.b_synoptic_small[{cr_number}]'
components = ['Bp', 'Bt', 'Br']
data = client.query(query_string, seg=components)
# Generator to find the nearest time
......@@ -385,6 +380,7 @@ def _get_urls_hmi_b720(client, mag_time):
generator that yields (datetime.datetime, str): Time of magnetogram,
suffix url of magnetogram
import drms
query_string = 'hmi.B_720s'
query_string += f'[{mag_time.year}.'
query_string += f'{str(mag_time.month).zfill(2)}.'
......@@ -487,7 +483,9 @@ def download_magnetogram_adapt(time, map_type='fixed', **kwargs):
for filename in filenames:
# Only try to download if the file does not exist
if os.path.isfile(directory+filename) == False:
if os.path.isfile(directory+filename) == True:
raise RuntimeWarning(f'{filename} exists, not downloading')
# open the file locally
with open(directory + filename, 'wb') as fhandle:
# try to download the magnetogram
......@@ -496,8 +494,6 @@ def download_magnetogram_adapt(time, map_type='fixed', **kwargs):
except ftplib.all_errors:
raise FileNotFoundError('Cannot download ', filename)
print(filename + ' already exists, skipping downloading')
# unzip the file
if '.gz' in filename:
#!/usr/bin/env python
"""Tests for
import datetime as dt
from os.path import isfile
from swmfpy import *
TIMES = (dt.datetime(2012, 12, 1), dt.datetime(2013, 1, 1))
def test_write_imf_from_omni():
assert isfile('IMF.dat')
#!/usr/bin/env python
"""Test for
from os.path import isfile
import datetime as dt
from swmfpy.web import *
TIME = dt.datetime(2016, 2, 3, 2, 1, 1)
def test_download_magnetogram_adapt():
assert isfile('adapt40311_03k012_201602030200_i00015600n1.fts')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment