wiki:installSunpy
Last modified 3 years ago Last modified on 05/26/22 08:04:24

Installing sunPy in a python virtual environment

A python virtual environment on linux or MacOS is a good way to experiment in python without affecting the system setup of python. Modules can be installed aside from the system version. Afterwards, the virtual environment can be deleted. Here, we install sunPy and use it in a simple example.

First, make sure you have a fairly recent version of python. At the bare minimum, make sure it's release 3 or more :

$ python --version
Python 3.9.6

Note that in order to get python version three, on some systems you may have to use "python3" rather than "python" for all commands.

First, build a virtual environment in an appropriately name directory and enter into it by sourcing the activation file :

$ python -m venv /home/oien/johnBsunPy
$ source /home/oien/johnBsunPy/bin/activate

At this point the system prompt will change to reflect the fact that you're in the virtual environment. To be sure you're in the virtual environment, check that the virtual environment versions of python and pip are being used :

(johnBsunPy) $ which python
/home/oien/johnBsunPy/bin/python
(johnBsunPy) $ which pip
/home/oien/johnBsunPy/bin/pip

That looks good. Now upgrade pip to the latest version, and install sunPy :

(johnBsunPy) $ pip install --upgrade pip
(johnBsunPy) $ pip install "sunpy[all]"

You can now use sunPy in the virtual environment :

(johnBsunPy) $ python
Python 3.9.6 (default, Jun 30 2021, 10:22:16) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sunpy.net import Fido, attrs as a
>>> result = Fido.search(a.Time('2021/02/02 16:00:00', '2021/02/02 16:30:00'), a.Instrument('big bear'), a.Physobs('intensity'))
>>> print(result.show("Provider","Source","fileurl"))
Results from 1 Provider:

14 Results from the VSOClient:
Provider Source                               fileurl                              
-------- ------ -------------------------------------------------------------------
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202160950Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161050Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161450Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161650Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161750Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161850Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202161950Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162050Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162150Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162250Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162350Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162450Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162650Bh.fits.fz
     NSO   GONG ftp://gong2.nso.edu/HA/haf/202102/20210202/20210202162750Bh.fits.fz

>>> exit()

You can leave the virtual environment with the "deactivate" command at the linux command line prompt, at which point your prompt will change back :

(johnBsunPy) $ deactivate
$ 

Another example search, this one using units and sampling :

from sunpy.net import Fido, attrs as a
import astropy.units as u

result = Fido.search(a.Instrument.aia, a.Wavelength(30.4 * u.nm), a.Time('2022/02/23 19:00:00', '2022/02/23 21:00:00'), a.Sample(0.5*u.hour))

print(result.show())