vtsuperdarn / davitpy

DEPRECATED The DaViT Python project
http://vtsuperdarn.github.com/davitpy/
GNU General Public License v3.0
37 stars 59 forks source link

Can't get IRI to run, missing iri_sub #268

Closed heinerasmus closed 7 years ago

heinerasmus commented 8 years ago

Hi, I've tried to run the example code for IRI (notebook). The following message apear:

UnboundLocalError: local variable 'iri_sub' referenced before assignment

If I have a look into the init.py file where the error occurs the 'iri_sub' is defined there but in principle tries to import itself?

The igrf example however works fine. Do I miss something?

here is the code: `from datetime import datetime as dt from davitpy.models import * from davitpy import utils import numpy as np import matplotlib.pyplot as plt

Inputs

jf = [True]_50 jf[2:6] = [False]_4 jf[20] = False jf[22] = False jf[27:30] = [False]*3 jf[32] = False jf[34] = False jmag = 0. alati = 40. along = -80. iyyyy = 2012 mmdd = 806 dhour = 12. heibeg, heiend, heistp = 80., 500., 10. oarr = np.zeros(100)

Call fortran subroutine

outf,oarr = iri.iri_sub(jf,jmag,alati,along,iyyyy,mmdd,dhour,heibeg,heiend,heistp,oarr)

Check that it worked by plotting vertical electron density profile

plt.figure(figsize=(5,8))

alt = np.arange(heibeg,heiend,heistp) ax = plt.plot(outf[0,0:len(alt)],alt)

plt.xlabel(r'Electron density [m$^{-3}$]') plt.ylabel('Altitude [km]') plt.grid(True) plt.rcParams.update({'font.size': 12}) `

Shirling-VT commented 8 years ago

Hi,

Which branch are you using? I just ran the same code in the notebook of the develop branch and it's working well.

On Mon, Aug 15, 2016 at 3:14 AM, Heiner Asmus notifications@github.com wrote:

Hi, I've tried to run the example code for IRI (notebook). The following message apear:

UnboundLocalError: local variable 'iri_sub' referenced before assignment

If I have a look into the init.py file where the error occurs the 'iri_sub' is defined there but in principle tries to import itself?

The igrf example however works fine. Do I miss something?

here is the code: `from datetime import datetime as dt from davitpy.models import * from davitpy import utils import numpy as np import matplotlib.pyplot as plt Inputs

jf = [True] _50 jf[2:6] = [False]_4 jf[20] = False jf[22] = False jf[27:30] = [False]*3 jf[32] = False jf[34] = False jmag = 0. alati = 40. along = -80. iyyyy = 2012 mmdd = 806 dhour = 12. heibeg, heiend, heistp = 80., 500., 10. oarr = np.zeros(100) Call fortran subroutine

outf,oarr = iri.iri_sub(jf,jmag,alati,along,iyyyy,mmdd,dhour,heibeg, heiend,heistp,oarr) Check that it worked by plotting vertical electron density profile

plt.figure(figsize=(5,8))

alt = np.arange(heibeg,heiend,heistp) ax = plt.plot(outf[0,0:len(alt)],alt)

plt.xlabel(r'Electron density [m$^{-3}$]') plt.ylabel('Altitude [km]') plt.grid(True) plt.rcParams.update({'font.size': 12}) `

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/vtsuperdarn/davitpy/issues/268, or mute the thread https://github.com/notifications/unsubscribe-auth/AIEZK6Wk9c9TWfv1DbJEBbH014AiC7pzks5qgBHvgaJpZM4JkKaS .

Best wishes, Xueling

asreimer commented 8 years ago

Hey @heinerasmus,

Do you still need help with this? I've tried running your code in a fresh install of davitpy and I am able to run it just fine. Here's the plot I get:

figure_1

heinerasmus commented 8 years ago

Hey, thanks for your feedback. @Shirling-VT , the version number I've found is 0.5. However I just extracted the notebook example code and wanted to run it in spyder. @asreimer , I reinstalled the whole package following the install instructions. I'm using Ubuntu 16.04 as guest in a virtual box. Python still can't find the initial definition of iri_sub.

Here is the full error message: runfile('/home/heiner/Python/davit.py', wdir='/home/heiner/Python') ERROR:root:/usr/local/lib/python2.7/dist-packages/davitpy-0.5-py2.7-linux-x86_64.egg/davitpy/models/iri/init.pyc -> models.iri.iri_sub: cannot import name iri_sub Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/davitpy-0.5-py2.7-linux-x86_64.egg/davitpy/models/iri/init.py", line 48, in iri_sub from iri import iri_sub ImportError: cannot import name iri_sub Traceback (most recent call last):

File "", line 1, in runfile('/home/heiner/Python/davit.py', wdir='/home/heiner/Python')

File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile execfile(filename, namespace)

File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile builtins.execfile(filename, *where)

File "/home/heiner/Python/davit.py", line 32, in outf,oarr = iri.iri_sub(jf,jmag,alati,along,iyyyy,mmdd,dhour,heibeg,heiend,heistp,oarr)

File "/usr/local/lib/python2.7/dist-packages/davitpy-0.5-py2.7-linux-x86_64.egg/davitpy/models/iri/init.py", line 56, in iri_sub return iri_sub(jf, jmag, alati, along, iyyyy, mmdd, dhour, heibeg,

UnboundLocalError: local variable 'iri_sub' referenced before assignment

Cheers

asreimer commented 8 years ago

I think the trouble is that there is a compilation error when compiling the iri model. If you look in the location where davitpy is installed on your machine (not the davitpy git repository) do you see an iri.so file?

We need to know what branch of davitpy you are using in the git repository (master, develop, etc.) you can find this out by running git branch in the root directory of davitpy.

Also, we need to see the output of running setup.py. For example, here is the output I get when running python setup.py install --user >> log.txt on the master branch of davitpy on my machine: log.txt

asreimer commented 8 years ago

I see from the error message you copied that you must be using the master branch. So we just need the rest of the info I asked for above.

heinerasmus commented 8 years ago

@asreimer thanks for the hint. The *.so files of the models are there. Here is the log file from the installation log.txt

Thanks again for your help.

asreimer commented 8 years ago

@heinerasmus you are trying to run this code using Spyder? That seems to be the only difference between your setup and mine. Perhaps Spyder is the problem here.

Here's what I did and it works. I've tested your example code on multiple machines (OpenSUSE, Ubuntu, and Fedora) and I am not able to reproduce the error that you get. Let's have you try this outside of Spyder.

Please try this exact thing and report back:

1) Create a file in my home directory called temp.py 2) Paste this code into the temp.py file:

from datetime import datetime as dt
from davitpy.models import iri #used to be from davitpy.models import * but this is generally not a good idea to use
from davitpy import utils
import numpy as np
import matplotlib.pyplot as plt

# Inputs
jf = [True]*50
jf[2:6] = [False]*4
jf[20] = False
jf[22] = False
jf[27:30] = [False]*3
jf[32] = False
jf[34] = False
jmag = 0.
alati = 40. 
along = -80.
iyyyy = 2012
mmdd = 806 
dhour = 12. 
heibeg, heiend, heistp = 80., 500., 10. 
oarr = np.zeros(100)
# Call fortran subroutine
outf,oarr = iri.iri_sub(jf,jmag,alati,along,iyyyy,mmdd,dhour,heibeg,heiend,heistp,oarr)

# Check that it worked by plotting vertical electron density profile
plt.figure(figsize=(5,8))

alt = np.arange(heibeg,heiend,heistp)
ax = plt.plot(outf[0,0:len(alt)],alt)

plt.xlabel(r'Electron density [m$^{-3}$]')
plt.ylabel('Altitude [km]')
plt.grid(True)
plt.rcParams.update({'font.size': 12})

plt.show()

3) In a terminal, run the python file with python temp.py

Hopefully we have a positive result.

heinerasmus commented 8 years ago

@asreimer yes I tried to run the code using Spyder. I've followed your instructions but I get the same Error in the end. An additional import error occurs when running the code via terminal. Somehow the basemap module cannot be imported. However I've tried to import it in Spyder and it works. Weird.

asreimer commented 8 years ago

Hey @heinerasmus, sounds like something is wrong in your python installation maybe. I'm not sure what the problem is and if I can't reproduce the error, unfortunately I can't be of much help. It's weird that you are getting a basemap error since we aren't using basemap in the example code I posted above.

I just noticed you are using from davitpy.models import *. Generally, using import * is a bad idea: https://stackoverflow.com/questions/2386714/why-is-import-bad So maybe replace the line in the code above with this instead: from davitpy.models import iri and see if you are able to get this to work? I've editted my comment above for your convenience. Using * could be causing a weird thing to happen in conjunction with spyder. We are doing a bit of name clobbering in our __init__.py file for the iri model and that could be causing your problems in conjunction with the import *.

One last thing you could try (which would be a bit of work) is to make a new virtual machine with Ubuntu 16.04 in it and only install davitpy. Don't install spyder and see if you can get davitpy working.

heinerasmus commented 8 years ago

Hey @asreimer, unfortunately I still have the same error even without using Spyder. The basemap package is imported in the plotUtils.py in the Utils folder.

I just tried the following with python run in terminal

import numpy as np from davitpy.models import iri outf, oarr = iri.iri_sub([True]*50,0.,40.,-80.,2012,806,12.,80.,500.,10.,np.zeros(100))

Here I get the same error UnboundLocalError: local variable 'iri_sub' referenced before assignment. Seems to me that it is not a problem with Spyder. I think there went something wrong during installation of the package since iri_sub is not really defined. In the function I found in the iri init it tries to import itself and as far as I understand that is the point where python raise the error. I attached the file.

init.py.zip

Thanks for your efforts @asreimer

asreimer commented 8 years ago

Unfortunately I'm not sure what the problem is. I haven't tried this on Ubuntu 16.04, but @ksterne has a VM of that. Perhaps he can see if he gets the same problem or not.

asreimer commented 7 years ago

Since this issue has stagnated and we can't reproduce the error, I'm closing it.