Computing sub-solar point - pyephem

I am just getting started with PyEphem. My immediate task is, given a date and time compute the sub-solar point on Earth with latitude-longitude values. I'll dig into PyEphem to work this out but if someone has already done this, I'd appreciate sample code.

I went looking for the same answer as the OP. Many posts "mention" how PyEphem is the way to go but without providing the actual example.
Here is my working example to calculate the subsolar point. Mapping everything to a longitude between -180 and + 180 degrees.
greenwich = ephem.Observer()
greenwich.lat = "0"
greenwich.lon = "0"
greenwich.date = datetime.utcnow()
sun = ephem.Sun(greenwich)
sun.compute(greenwich.date)
sun_lon = math.degrees(sun.ra - greenwich.sidereal_time() )
if sun_lon < -180.0 :
sun_lon = 360.0 + sun_lon
elif sun_lon > 180.0 :
sun_lon = sun_lon - 360.0
sun_lat = math.degrees(sun.dec)
print "Subsolar Point Sun Lon:",sun_lon, "Lat:",sun_lat
I am no expert in PyEphem and there may be a better approach - but my testing so far has this work for my purposes.
p.s. yes.. Greenwich above is not actually set to the actual lat/lon... it's really only the Longitude of 0.0 that's needed to get the appropriate Sidereal time we need.

I cannot test actual code from where I am this morning, but: an object at declination ϕ should always ride right above the series of locations on earth that have latitude ϕ, so the latitude number is given to you directly by a body's .dec attribute (or .a_dec or .g_dec depending on your application).
Now, what about longitude?
Imagine the situation, which I suppose must occur roughly once a day, when Greenwich at 0° longitude looks up and sees the line in the sky of 0° right ascension right overhead. At that moment, a body in the sky at right ascension θ would be looking down at longitude θ assuming that longitude is positive going east, as is the case with PyEphem.
Now, what if Greenwich is looking up at a non-zero line of right ascension instead? Then it seems to me that we just need to subtract that from a body's right ascension in order to make longitude, because as the day proceeds and the Earth turns and lines of right ascension pass over Greenwich with bigger and bigger right ascensions assigned to them, any given body is going to pass west across the Earth and its longitude will dwindle and then go negative as it passes over the Western Hemisphere.
The line of right ascension overhead at Greenwich at any given moment can be determined by creating an Observer at 0° longitude and asking for its .sidereal_time() if I recall the Quick Reference correctly. So I think that the longitude beneath of a body might be:
lon = body.ra - greenwich.sidereral_time()
I will do a quick test with this later on today to see if reasonable numbers come out.

Related

Find users within a given radius

I have a mySQL table with user name, latitude and longitude of the user. I would like to get a list of user who are inside the circle of a given latitude and longitude with given distance. For example my input Lat= 78.3232 and Long = 65.3234 and distance = 5 km. I would like to get the list of users who are inside 5km distance from the point 78.3232 and 65.3234. Is it possible to solve this with single query? Or can you give me a hint start solving this query? I am new to the geo based information.
google gives me this :
http://www.movable-type.co.uk/scripts/latlong-db.html
it gives you a good idea to start your work.. based on the distance between points (your circle radius)
imo, you need to define all points (surface) that are into this "circle", and check if your users are into this surface... in fact, a good way to do this would be to define ranges... for example :
lat : 72, long between 65 & 67
lat : 73, long between 64 & 69
... ...
if you do it with your precision xx.yyyy that will increase consequently the possibilities... it's just a mathematical challenge... Good luck!
I think a good place to start is with the Pythagorean theorem a^2 + b^2 = c^2 where a would = the difference in lat from point to Origin and b would = the difference in long. and c would equal distance.
that solves the math point.
I would need some helping doing the conversion from distance in lat and distance in long.
is the going to be a local, state, or a world wide program? that will create changes as differences in longitude get small as you reach the poles and larger as your reach the equator.

How to get distance from earth center?

e = ephem.readtle(...)
e.compute('2012/02/04 07:55:00')
As far as I can see there's only e.elevation as a measure of distance which is relative to the sea level. At the moment I'm using a.elevation/1000 + 6371 to estimate the distance from the center of the earth.
I'm pretty sure that the exact earth center distance at the requested point in time is needed for the ephemeris calculations. Is this distance somewhere exposed and if not, why not and can that be changed?
I had thought that the answer would involving having to expose an ellipsoidal model of the earth from deep inside of the C code to Python to get you the information you need. But, having just gone through the Earth-satellite code, it turns out that the way that it converts the satellite's distance from the Earth center to its height is simply (from earthsat.c):
#if SSPELLIPSE
#else
*Height = r - EarthRadius;
#endif
Apparently the programmer planned to someday implement an ellipsoidal earth, and had an #if statement ready to guard the new code, but never wrote any.
So you can convert the height (“elevation”) back to a distance from the Earth's center by adding the value EarthRadius which is defined as:
#define EarthRadius 6378.16 /* Kilometers */
Since the elevation is, I believe, in meters, you will want to multiply EarthRadius by 1000.0 or else divide the elevation by 1000.0 to get the right result.

Angle to Mecca from current location with iPhone compass

I have a question about the Qibla direction, I am building an iPhone application which will show both North direction and Qibla direction, I am showing the north direction with the help of CLLocationManager and updating it with CLHeading as newHeading.magneticHeading, And i am showing the Qibla direction with the following code
double A = MECCA_LONGITUDE - lon;
double b = 90.0 - lat;
double c = 90.0 - MECCA_LATITUDE;
NSLog(#"tan -1( sin(%f) / ( sin(%f) * cot(%f) - cos(%f) * cos(%f)))", A, b, c, b, A);
double qibAngle = atan(sin(A) /( sin(b) * (1 / tan(c)) - cos(b) * cos(A) ));
NSLog(#"qib Angle- %f",qibAngle);
qibla.transform = CGAffineTransformMakeRotation(qibAngle * M_PI /180);
So, here i am getting the angle, but it does not update the angle when i rotate the device, Can anyone help me out, i know that i need to do some thing with heading , but i don't know what to do?
I assume the code you posted computes the angle between geographical north and the direction towards Mecca for the current location. All you need to do now is take into account the user's heading.
For example, suppose the user is located so Mecca is directly due West, and the user is facing directly due East. Since tan returns +/-90 degrees, the qibla angle would have to be -90 degrees. Now the adjustment should be obvious: you need to subtract 90 degrees from the qibla angle respective to geographical north (-90) to arrive at (-180) degrees, which is how much user needs to turn in order to face Mecca.
Simply put, you need to "undo" the user's deviation, and you do this by subtracting from the qibla angle the the user's heading, which is relative to geographical north.
With the maths out of the way, now you need to observe heading changes and recompute the qibla angle when the heading changes. Lastly, make sure to use the trueHeading property.
I'm probably going to lose points on this answer because I know absolutely nothing about ios, but, I believe atan returns a value in radians, and CGAffineTransformMakeRotation takes it's argument in radians as well , so the conversion qibAngle * M_PI /180 is not needed.
You might also want to re-title your post, since most people have no idea what Qibla is and wouldn't realize that it's about math and iOS. I only looked because I've heard calculating the right direction to Mecca is kind of a neat math problem.

iOS - Center position of two coordinates

I would like to create a MKCoordinateRegion (to zoom to the good region on the map) from the northeast and southwest points given by Google. For that I need to compute the coordinate of the center between these two coordinates. Any clue? I could do simple math but I will have problems with the equator...
Thanks!!!
Assuming you mean anti-meridian and not the equator then here goes (While all this works on a flattened map and should be good enough for your purpose, it's completely bung on a sphere. see note at the bottom).
What I've done in other cases is start at either point, and if the next point is more than 180 degrees to the east, I convert it so that it is less than 180 to the west like so
if(pointa.lon - pointb.lon > 180)
pointb.lon += 360:
else if (pointa.lon - pointb.lon < -180)
pointb.lon -= 360
At this time pointb.lon might be an invalid longitude like 190 but you can at least work out the mid-point between pointa and point b because they will be on a continuous scale, so you might have points 175 and 190. Then just get the mid-point between them as 182.5, then convert that to make sure it is within the usual limits and you get -177.5 as the latitude between the two points. Working out the latitude is easy.
Of course on a sphere this is wrong because the midpoint between (-180,89) and (180,89) is (0*,90) not (0,89).
* = could be anything
Also, couldn't you just zoomToRect made with the defined corners? It'd save you doing this calculation and then next one which would be to work out what zoom level you need to be at when centered on that point to include the two corners you know about. Since the Maps app doesn't appear to scroll over the anti-meridian I assume MKMapview can't either so your rectangle is going to have to have the northeast coord as the top right and the southwest as the bottom left.
This SO post has the code to zoom a map view to fit all its annotations.

How can we find the angle between two locations defined by latitude or longitude

I do not want any code but want to get reference that how can we find the angle between two locations defined by Latitude or Longitude...
If you do have reference then Please help me to solve my problem...
Thanx in advance....
The formula to calculate bearing is:
θ = atan2( sin(Δlong).cos(lat2), cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong) )
Bearing is a direction to move from one location to another location (starting from north and then clockwise). While angle in 2D starts from the east and then counter clockwise. So if an angle is what you need, later you'll need to add 90 degree to the result and then revert it (add minus).
Reference:
http://www.movable-type.co.uk/scripts/latlong.html
try the Atan method
Math.Atan2(x1-x2,y1-y2)
Try this website, it does all the calculations for you. Whereas the formulas, those can be found on wiki or any other sites. I like this site cos it managed to help me settle a lot of problems. And even if this web tool was developed in San Francisco, even I am from Singapore. This will work.
Latitude and Longitude web tool