Calculate time of meridian passage with GHPython

I am trying to calculate the time of meridian passage in Japan(GMT+9) with GHPython.

I tried this:

"""Provides a scripting component.
    Inputs:
        Lat: Latitude of epw file
        Long: Longitude of epw file
        Date: Set date
    Output:
        SunCtime: The time when the sun is due south in Japan """

import rhinoscriptsyntax as rs
import ephem

location = ephem.Observer()
location.lat,location.lon = Lat ,Long
location.date = Date

SunR9=location.previous_rising(ephem.Sun())
SunR=SunR9 + datatime.timedelta(hours=-9)
SunRtime=SunR.time()

SunS=location.next_setting(ephem.Sun())
SunS=SunS9 + datatime.timedelta(hours=-9)
SunStime=SunS.time()

SunCtime=(SunRtime+SunStime)/2

test.gh (6.4 KB)

I know that this cord is wrong, I just don’t know how to make it right.

Any help will be appreciated.
Thank you for your cooperation.

Hi,
It looks like your calculation is wrong, and your code is also wrong for the following reasons :

  1. You are using SunR9 and SunS9 in your code, but you have not defined them anywhere. You should use SunR and SunS instead, which are defined in the previous lines.
  2. The calculation of the meridian passage time is incorrect.

Thank you for your answer.

I’m a programming novice.
Your answer is very helpful.
I will try to fix the code soon.

I fixed the code.

"""Calculate the time when the sun is due south in Japan.
    Inputs:
        Lat: Latitude of epw file
        Long: Longitude of epw file
        Date: Set date
    Output:
        SunCtime: The time when the sun is due south in Japan """

import rhinoscriptsyntax as rs
import ephem
import time
import datetime

location = ephem.Observer()
location.lat = Lat
location.lon = Long
loation.date = Date

sun = ephem.Sun()

R = ephem.localtime(location.nest_rising(sun))
Rt = datetime.datetime(R)

S = ephem.localtime(location.nest_setting(sun))
St = datetime.datetime(S)

SunCtime = (Rt+St) / 2

When I run this code, I get an error.

Runtime error (ImportException): No module named ephem

Traceback:
line 13, in script

Do I need to install any software?
I would appreciate it if you could tell me if you know.

Your ephem seems to be this package: ephem · PyPI.

The python implementation used within Grasshopper / Rhino is called IronPython.
The “normal” / regular python implementation is called CPython. For CPython the use of pip as the package manager is very common. You can use it to install the ephem package. The ephem package can still work with the IronPython, but often it does not, so I’d suggest to either call your CPython code by for example defining a CLI that you from your GH_Python (which as i mentioned is using IronPython) or use hops as described in the following link (probably easier for a beginner):
Create CPython components using Hops in Grasshopper - Grasshopper / Hops - McNeel Forum

Sorry for the late reply.
Thank you for your answer.
I understand very well.