python - Ephem日出-1分钟

标签 python subtraction pyephem

我试图让代码在日出前一分钟运行,但自从更新代码(通过另一个问题)以来,我已经更改了 GMT 的时区,因此在删除一分钟时我无法正确获取语法。

sunriselessonemin = (ephem.date(sunrise)) + (1*ephem.minute)

日出是通过以下方式获得的

sunrise, sunset = ephem.localtime(home.next_rising(sun)),ephem.localtime(home.next_setting(sun))

有人可以帮助菜鸟吗? 谢谢

编辑得更清楚:-)

这是我的原始代码。我使用的树莓派在凌晨 4 点通过 crontab 重新启动,启动后,该脚本就会运行。它在英国运行良好,但现在我不在那个时区,我需要按照布兰登之前的建议添加一些本地时区工作。

import sys
import os
import time
import ephem

#find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '45.226691', '0.013133' #your lat long 
sun.compute(home)
sunrise, sunset = ephem.localtime(home.next_rising(sun)),ephem.localtime(home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)

print "prog started at(home.date) =  %s" %(home.date)
print "datetime = %s" % time.strftime("%Y/%-m/%-d %H:%M:%S")
print "sunrise = %s" %sunrise
print "sunset = %s" %sunset
print "daylight mins = %s" %(daylightminutes)

testmode = "yes" #yes or no

def dostuff() :
    if testmode == "yes" or sunrise <= ephem.now() <= sunriselessonemin: #if time now is within a minute of sunrise, start taking pictures
        print "it's sunrise!"
        if testmode == "yes" : 
            print "TESTMODE - Taking 10 images with 10 seconds in between and uploading made mp4 to Dropbox"
        FRAMES = daylightminutes # number of images you want in timelapse video
        if testmode == "yes" : 
            FRAMES = 10
        FPS_IN = 8 # number of images per second you want in video
        FPS_OUT = 8 # number of fps in finished video 24 is a good value
        TIMEBETWEEN = 60 # number of seconds between pictures, 60 = 1 minute
        #take the pictures needed for the time lapse video
        if testmode == "yes" : 
            TIMEBETWEEN = 10
        frameCount = 1
        while frameCount < (FRAMES + 1):
            print "taking image number ", frameCount, " of ", daylightminutes
            datetimenowis = ephem.now() 
            imageNumber = str(frameCount).zfill(7)
            os.system("raspistill -o /home/pi/image%s.jpg"%(imageNumber)) # -rot 270 need for cam on side (put -rot 270 before -o)
            os.system("/usr/bin/convert /home/pi/image%s.jpg -pointsize 72 -fill white -annotate +40+1590 'Chicken Cam %s' /home/pi/image%s.jpg"%(imageNumber,datetimenowis,imageNumber))
            frameCount += 1
            time.sleep(TIMEBETWEEN - 10) #Takes roughly 6 seconds to take a picture & 4 to add text to image
        #record current time and date in variable datetime
        datetimenowis = time.strftime("%Y%m%d-%H%M")
        print "It's sunset, processing images into one mp4 video.  Time now is ",  datetimenowis
        # make the timelapse video out of the images taken
        os.system("avconv -r %s -i /home/pi/image%s.jpg -r %s -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 /home/pi/timelapse%s.mp4" %(FPS_IN,'%7d',FPS_OUT,datetimenowis))
        #send the timelapse video to dropbox
        print "Sending mp4 video to dropbox."
        from subprocess import call
        photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 timelapse%s.mp4" %(datetimenowis,datetimenowis) 
        call ([photofile], shell=True)
        print "mp4 uploaded to dropbox!  Cleaning up."
        #remove the timelapse video copy and all images it is made up of that are held localy on the Rpi
        os.system("rm /home/pi/timelapse%s.mp4"%(datetimenowis))
        os.system("rm /home/pi/image*")
        print "Finished, exiting program."
        sys.exit()

while ephem.now() <= sunrise:
        time.sleep(1)
        dostuff()

妈妈网的问题是,如果我尝试在日落前一分钟触发主代码。代码在这里失败。

    pi@raspberrypi ~ $ sudo python timelapseV3.py
Traceback (most recent call last):
  File "timelapseV3.py", line 13, in <module>
    sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 
'float'

我似乎无法像以前那样在日出前一分钟启动代码。

干杯

最佳答案

一旦你在 PyEphem 日期上运行 localtime() ,你就将它从 PyEphem 可以推理的值移开,而是创建一个 Python 原生 datetime 的值模块知道。要使用 PyEphem 中的值,请保留 PyEphem 返回的原始值的副本,并用它进行数学运算:

sunrise, sunset = home.next_rising(sun), home.next_setting(sun)
sunrise_localtime, sunset_localtime = ephem.localtime(sunrise), ephem.localtime(sunset)
daylightminutes = (sunset_localtime - sunrise_localtime) * 1440 # find howmany minutes of daylight there are
sunriselessonemin = ephem.date(sunrise + 1*ephem.minute)

您应该发现它运行时没有错误。祝你好运!

关于python - Ephem日出-1分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55453726/

相关文章:

python - Django/DRF 应用程序收到 AttributeError : 'function' object has no attribute 'get_extra_actions'

python - 如何在 Python 中创建颜色渐变?

python - 是否可以在字典中使用两个(非嵌套)for 循环?

Matlab - 在没有for语句的情况下将向量减去矩阵的每一行

astronomy - PyEphem - 计算下一次满月时的国际空间站凌日

python - 在 Python virtualenv 中查找每个编译模块的大小?

vba - 从两个 NOW 中减去日期和时间

java - 逆波兰表示法计算器 : subtraction of two positive numbers returning a minus?

python - 使用 ephem 计算 "Solar Noon",转换为本地时间

python-3.x - 在linux上安装ephem