短暂日出1分钟

时间:2019-04-01 11:08:51

标签: 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()

momnet的问题是,如果我尝试在日落之后的一分钟内更改主代码。代码在这里失败。

    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'

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

欢呼

1 个答案:

答案 0 :(得分:0)

一旦在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)

您应该找到没有错误的运行。祝你好运!