Python Forum
Very strange error of indentation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very strange error of indentation
#1
Reference is made to the python code listed here http://reionization.org/wp-content/uploa...st2ra.html and, in details, to this piece one http://imgbox.com/uaj4dzLS
By running the file below, and being checked the proper indentations which match the original, surprisingly I obtain next awful prompt:

Output:
C:\Training>python bryna.py File "bryna.py", line 35 return cirs_ra ^ TabError: inconsistent use of tabs and spaces in indentation
Where is my fault? Thx in advance

# ---------bryna.py ------------
import numpy as np
from astropy.coordinates import SkyCoord, Angle, EarthLocation
from astropy.time import Time
from astropy import units
#
mjd = 55780.1
latitude = Angle('-26d42m11.94986s')
longitude = Angle('116d40m14.93485s')
#
obs_time = Time(mjd, format='mjd', location = (longitude, latitude))
lst_apparent = obs_time.sidereal_time('apparent')
print(lst_apparent)
#  Output1: 7h11m46.2716s
#
# The frame radio astronomers call the apparent or current epoch is the
# "true equator & equinox" frame, notated E_gamma in the USNO circular 179
# (http://aa.usno.navy.mil/publications/docs/Circular_179.php)
# astropy doesn't have this frame but it's pretty easy to adapt the CIRS frame
# by modifying the ra to reflect the difference between
# GAST (Grenwich Apparent Sidereal Time) and the earth rotation angle (theta).
# The earth rotation angle is most clearly explained in the 2nd paragraph of Chapter 6
# of the USNO circular (page 50)
# The relationship between E_gamma and CIRS is shown in the figure on page 57 of the circular

def egamma_to_cirs_ra(egamma_ra, time):
    from astropy import _erfa as erfa
    from astropy.coordinates.builtin_frames.utils import get_jd12
    era = erfa.era00(*get_jd12(Time(mjd, format='mjd'), 'ut1'))
    theta_earth = Angle(era, unit='rad')

    assert(isinstance(time, Time))
    gast = time.sidereal_time('apparent', longitude=0)
    cirs_ra = egamma_ra - (gast-theta_earth)
	return cirs_ra

# If we define a source at RA = apparent LST and Dec = latitude it should be at zenith
loc_obj = EarthLocation.from_geodetic(lon=longitude, lat=latitude)
# use CIRS but with a different ra to account for the difference between LST & earth's rotation angle
cirs_ra = egamma_to_cirs_ra(lst_apparent, Time(mjd, format='mjd'))

egamma_zenith_coord = SkyCoord(ra=cirs_ra, dec=latitude, frame='cirs',
                               obstime=Time(mjd, format='mjd'), location = loc_obj)
egamma_zenith_coord
# Output2
#<SkyCoord (CIRS: obstime=55780.1): (ra, dec) in deg
#    (107.78961213, -26.70331941)>
#
# check where it is in altaz (should be near zenith):
egamma_zenith_altaz = egamma_zenith_coord.transform_to('altaz')
egamma_zenith_altaz
# Output3
#<SkyCoord (AltAz: obstime=55780.1, location=(-2559302.5737783727, 5095070.526830904, -2848887.400942108) m, pressure=0.0 hPa, temperature=0.0 deg_C, relative_humidity=0, obswl=1.0 micron): (az, alt) in deg
#    (28.60732437, 89.99985797)>
#
(egamma_zenith_altaz.alt - Angle('90d')).to_string(unit=units.degree, sep=('deg', 'm', 's'))
# Output4
# '-0deg00m00.5113s'
#
# ------ EOF: bryna.py ----------
Reply
#2
I'm not sure we can tell. I think the code plug-in automatically converts tabs to spaces. All the indentation I am seeing is spaces.

What to do in this situation is to find the settings in your editor that make white space visible, or show hidden characters. Then you can see where the different indentation is. Alternatively, you can just per-emptively change all tabs to four spaces.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
My editor (geany) doesn't show anomalies, as per this screenshot http://imgbox.com/GUsclMUy
Thank you, administrator.
Reply
#4
You are only showing line endings. You need to also show whitespace.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
The problem is you have mixed tabs and spaces. Use just spaces. Normally editors have option to convert tab to spaces.
(Mar-21-2019, 07:11 PM)samsonite Wrote: My editor (geany) doesn't show anomalies,
Note, it's not that indentation level is incorrect, but that you have mix of tabs and space.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Nothing changes clicking On/Off white spaces, unfortunately. The figure shows white spaces settled on: http://imgbox.com/5uquVik4
Reply
#7
There is no error(inconsistent use of tabs and space) in code you posted.
As i did install astropy as test in your other thread i can run code.
Code is from a Jupyter Notebook,so running as script need to add print() to get output.
# ---------bryna.py ------------
import numpy as np
from astropy.coordinates import SkyCoord, Angle, EarthLocation
from astropy.time import Time
from astropy import units
#
mjd = 55780.1
latitude = Angle('-26d42m11.94986s')
longitude = Angle('116d40m14.93485s')
#
obs_time = Time(mjd, format='mjd', location = (longitude, latitude))
lst_apparent = obs_time.sidereal_time('apparent')
print(lst_apparent)
#  Output1: 7h11m46.2716s
#
# The frame radio astronomers call the apparent or current epoch is the
# "true equator & equinox" frame, notated E_gamma in the USNO circular 179
# (http://aa.usno.navy.mil/publications/docs/Circular_179.php)
# astropy doesn't have this frame but it's pretty easy to adapt the CIRS frame
# by modifying the ra to reflect the difference between
# GAST (Grenwich Apparent Sidereal Time) and the earth rotation angle (theta).
# The earth rotation angle is most clearly explained in the 2nd paragraph of Chapter 6
# of the USNO circular (page 50)
# The relationship between E_gamma and CIRS is shown in the figure on page 57 of the circular
 
def egamma_to_cirs_ra(egamma_ra, time):
    from astropy import _erfa as erfa
    from astropy.coordinates.builtin_frames.utils import get_jd12
    era = erfa.era00(*get_jd12(Time(mjd, format='mjd'), 'ut1'))
    theta_earth = Angle(era, unit='rad')
 
    assert(isinstance(time, Time))
    gast = time.sidereal_time('apparent', longitude=0)
    cirs_ra = egamma_ra - (gast-theta_earth)
    return cirs_ra    
 
# If we define a source at RA = apparent LST and Dec = latitude it should be at zenith
loc_obj = EarthLocation.from_geodetic(lon=longitude, lat=latitude)
# use CIRS but with a different ra to account for the difference between LST & earth's rotation angle
cirs_ra = egamma_to_cirs_ra(lst_apparent, Time(mjd, format='mjd'))
 
egamma_zenith_coord = SkyCoord(ra=cirs_ra, dec=latitude, frame='cirs', 
                               obstime=Time(mjd, format='mjd'), location = loc_obj)
print('-' * 20)                               
print(egamma_zenith_coord)
# Output2
#<SkyCoord (CIRS: obstime=55780.1): (ra, dec) in deg
#    (107.78961213, -26.70331941)>
#
# check where it is in altaz (should be near zenith):
egamma_zenith_altaz = egamma_zenith_coord.transform_to('altaz')
print('-' * 20)
print(egamma_zenith_altaz)
# Output3
#<SkyCoord (AltAz: obstime=55780.1, location=(-2559302.5737783727, 5095070.526830904, -2848887.400942108) m, pressure=0.0 hPa, temperature=0.0 deg_C, relative_humidity=0, obswl=1.0 micron): (az, alt) in deg
#    (28.60732437, 89.99985797)>
#
n = (egamma_zenith_altaz.alt - Angle('90d')).to_string(unit=units.degree, sep=('deg', 'm', 's'))
print('-' * 20)
print(n)
# Output4
# '-0deg00m00.5113s'
#
# ------ EOF: bryna.py ----------
Test:
Output:
(del_env) E:\div_code\del_env λ python as.py 7h11m46.2716s -------------------- <SkyCoord (CIRS: obstime=55780.1): (ra, dec) in deg (107.78961179, -26.70331941)> -------------------- <SkyCoord (AltAz: obstime=55780.1, location=(-2559302.57377837, 5095070.5268309, -2848887.40094211) m, pressure=0.0 hPa, temperature=0.0 deg_C, relative_humidity=0.0, obswl=1.0 micron): (az, alt) in deg (28.60357927, 89.99985796)> -------------------- -0deg00m00.5114s
Reply
#8
Then change all tabs to spaces. You should be able to do replace all with '\t' to ' '.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Mar-21-2019, 07:59 PM)samsonite Wrote: Nothing changes clicking On/Off white spaces, unfortunately. The figure shows white spaces settled on: http://imgbox.com/5uquVik4
on line 35 from the picture you have tab! all others are spaces
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Oh yes, snippsat code, renamed br.py, works properly! Thanks all
# ---------br.py ------------
import numpy as np
from astropy.coordinates import SkyCoord, Angle, EarthLocation
from astropy.time import Time
from astropy import units
#
mjd = 55780.1
latitude = Angle('-26d42m11.94986s')
longitude = Angle('116d40m14.93485s')
#
obs_time = Time(mjd, format='mjd', location = (longitude, latitude))
lst_apparent = obs_time.sidereal_time('apparent')
print(lst_apparent)
#  Output1: 7h11m46.2716s
#
# The frame radio astronomers call the apparent or current epoch is the
# "true equator & equinox" frame, notated E_gamma in the USNO circular 179
# (http://aa.usno.navy.mil/publications/docs/Circular_179.php)
# astropy doesn't have this frame but it's pretty easy to adapt the CIRS frame
# by modifying the ra to reflect the difference between
# GAST (Grenwich Apparent Sidereal Time) and the earth rotation angle (theta).
# The earth rotation angle is most clearly explained in the 2nd paragraph of Chapter 6
# of the USNO circular (page 50)
# The relationship between E_gamma and CIRS is shown in the figure on page 57 of the circular
  
def egamma_to_cirs_ra(egamma_ra, time):
    from astropy import _erfa as erfa
    from astropy.coordinates.builtin_frames.utils import get_jd12
    era = erfa.era00(*get_jd12(Time(mjd, format='mjd'), 'ut1'))
    theta_earth = Angle(era, unit='rad')
  
    assert(isinstance(time, Time))
    gast = time.sidereal_time('apparent', longitude=0)
    cirs_ra = egamma_ra - (gast-theta_earth)
    return cirs_ra    
  
# If we define a source at RA = apparent LST and Dec = latitude it should be at zenith
loc_obj = EarthLocation.from_geodetic(lon=longitude, lat=latitude)
# use CIRS but with a different ra to account for the difference between LST & earth's rotation angle
cirs_ra = egamma_to_cirs_ra(lst_apparent, Time(mjd, format='mjd'))
  
egamma_zenith_coord = SkyCoord(ra=cirs_ra, dec=latitude, frame='cirs', 
                               obstime=Time(mjd, format='mjd'), location = loc_obj)
print('-' * 20)                               
print(egamma_zenith_coord)
# Output2
#<SkyCoord (CIRS: obstime=55780.1): (ra, dec) in deg
#    (107.78961213, -26.70331941)>
#
# check where it is in altaz (should be near zenith):
egamma_zenith_altaz = egamma_zenith_coord.transform_to('altaz')
print('-' * 20)
print(egamma_zenith_altaz)
# Output3
#<SkyCoord (AltAz: obstime=55780.1, location=(-2559302.5737783727, 5095070.526830904, -2848887.400942108) m, pressure=0.0 hPa, temperature=0.0 deg_C, relative_humidity=0, obswl=1.0 micron): (az, alt) in deg
#    (28.60732437, 89.99985797)>
#
n = (egamma_zenith_altaz.alt - Angle('90d')).to_string(unit=units.degree, sep=('deg', 'm', 's'))
print('-' * 20)
print(n)
# Output4
# '-0deg00m00.5113s'
#
# ------ EOF: br.py ----------
#
# ----------- OUTPUT -------------
#C:\Training>python br.py
#7h11m46.2716s
#--------------------
#<SkyCoord (CIRS: obstime=55780.1): (ra, dec) in deg
#    (107.78961179, -26.70331941)>
#--------------------
#<SkyCoord (AltAz: obstime=55780.1, location=(-2559302.57377837, 5095070.5268309, -2848887.40094211) m, pressure=0.0 h
#Pa, temperature=0.0 deg_C, relative_humidity=0.0, obswl=1.0 micron): (az, alt) in deg
#    (28.60357927, 89.99985796)>
#--------------------
#-0deg00m00.5114s
# ---------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange argument count error rowan_bradley 3 659 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Strange error ValueError: dimension mismatch Anldra12 0 1,937 Aug-17-2021, 07:54 AM
Last Post: Anldra12
  Strange syntax error with f-strings Askic 6 4,075 Oct-16-2020, 10:40 AM
Last Post: Askic
  Indentation Error With If Else Statement JoeDainton123 3 1,776 Aug-02-2020, 08:29 PM
Last Post: deanhystad
  Indentation error... lol ironfelix717 8 3,647 Apr-02-2020, 02:31 PM
Last Post: buran
  Getting error message for indentation Shafla 5 2,774 May-07-2019, 08:56 PM
Last Post: Yoriz
  Help please - indentation error bil007 1 2,021 Mar-24-2019, 11:41 AM
Last Post: Yoriz
  Indentation error in Python code ErnestTBass 5 3,504 Feb-28-2019, 04:26 PM
Last Post: samsonite
  Strange Pandoc Error AreebSooYasir 0 2,013 Jul-24-2018, 04:30 AM
Last Post: AreebSooYasir
  Class - Error Message (Indentation) tkj80 3 4,440 Jan-20-2017, 07:48 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020