commit 06b4444a28a1396109b077a852b7ce8be5fc4c6f Author: Gary Steers Date: Tue Apr 6 21:48:44 2021 +0100 initial diff --git a/README.md b/README.md new file mode 100644 index 0000000..187f04b --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ + + +# Timelapse Scripts + +In here are scripts useful for processing timelapse files + +## getdayinfo.py + +This is an example of how to query the sunrise/sunset time in UTC, using the api here: +https://sunrise-sunset.org/api + +## timelapse_files.py + +Using the logic from above will pasrse a directory structure and validate time is within daylight hours. + +The ``options.ttype = 'civil'`` changes teh default to civil (my preference), it assumes the following directory structure: + + YYYY/MM/DD/somename_YYYY-MM-DD_HHMM.jpg + e.g. + 2017/09/08/mycapture_2017-09-08_1243.jpg + +It will walk the tree and print to STDOUT any files in sunset/sunrise time. It also stores a copy of the sunset/sunrise so that it only has to be looked up once per date. diff --git a/getdayinfo.py b/getdayinfo.py new file mode 100755 index 0000000..a0086c3 --- /dev/null +++ b/getdayinfo.py @@ -0,0 +1,71 @@ +#!/usr/bin/python +import os, urllib2 as url, json +import datetime as dt +from optparse import OptionParser + +# Greenwich Observatory +lat="51.4826" +lng="0.000" + +optparse = OptionParser(usage="Usage: %prog [options]") +optparse.add_option('-d','--date',dest='date',help='Set date to qurey YYYY-MM-DD [default=today]',default='today') +optparse.add_option('-t','--timetype',dest='ttype',help='Set sunrise/sunset type (standard,civil,nautical,astronomical) [default=standard] see https://www.timeanddate.com/astronomy/different-types-twilight.html',default='standard') +optparse.add_option('-i','--interval',dest='interval',help='Minute Interval [default=1, max=60]',default='1') +optparse.add_option('-x','--lat',dest='lat',help='Latitude defaults to ' + lat,default=lat) +optparse.add_option('-y','--lng',dest='lng',help='Longitude defaults to ' + lng,default=lng) +(options, args) = optparse.parse_args() + +def min_round(x,base): + minute = dt.datetime.strptime(str(int(base * int(float(x)/base))),'%M') + return minute.strftime("%M") + +def riseset_info(lat,lng,date,ttype): + ret = {} + apiUrl="https://api.sunrise-sunset.org/json?lat=" + str(lat) + "&lng=" + str(lng) + "&date=" + str(date) + response = url.urlopen(apiUrl).read() + result = json.loads(response) + + if ttype == 'standard': + sunrise = result['results']['sunrise'] + sunset = result['results']['sunset'] + elif ttype == 'civil': + sunrise = result['results']['civil_twilight_begin'] + sunset = result['results']['civil_twilight_end'] + elif ttype == 'nautical': + sunrise = result['results']['nautical_twilight_begin'] + sunset = result['results']['nautical_twilight_end'] + elif ttype == 'astronomical': + sunrise = result['results']['astronomical_twilight_begin'] + sunset = result['results']['astronomical_twilight_end'] + else: + print + print "ERROR: Invalid Time Type" + print + optparse.print_help() + print + os._exit(1) + + + srtime = dt.datetime.strptime(sunrise,"%I:%M:%S %p") + sstime = dt.datetime.strptime(sunset,"%I:%M:%S %p") + + sunrise_H = srtime.strftime("%H") + sunrise_M = srtime.strftime("%M") + sunrise_MR = min_round(x=sunrise_M,base=int(options.interval)) + srtime = dt.datetime.strptime(sunrise_H + ':' + str(sunrise_MR),"%H:%M") + ret['sunrise'] = srtime.strftime("%H%M") + + sunset_H = sstime.strftime("%H") + sunset_M = sstime.strftime("%M") + sunset_MR = min_round(x=sunset_M,base=int(options.interval)) + if sunset_M != sunset_MR: + sunset_MR = sunset_MR + int(options.interval) + if sunset_MR == 60: + sunset_H = sunset_H + 1 + sunset_MR = 0 + sstime = dt.datetime.strptime(sunset_H + ':' + str(sunset_MR),"%H:%M") + ret['sunset'] = sstime.strftime("%H%M") + return ret + +print riseset_info(lat=options.lat,lng=options.lng,date=options.date,ttype=options.ttype) + diff --git a/timelapse_files.py b/timelapse_files.py new file mode 100755 index 0000000..f71385d --- /dev/null +++ b/timelapse_files.py @@ -0,0 +1,124 @@ +#!/usr/bin/python +import os, urllib2 as url, json +import datetime as dt +from optparse import OptionParser + +# Greenwich Observatory +lat="51.4826" +lng="0.000" + +optparse = OptionParser(usage="Usage: %prog [options]") +optparse.add_option('-d','--date',dest='date',help='Set date to qurey YYYY-MM-DD [default=today]',default='today') +optparse.add_option('-t','--timetype',dest='ttype',help='Set sunrise/sunset type (standard,civil,nautical,astronomical) [default=standard] see https://www.timeanddate.com/astronomy/different-types-twilight.html',default='standard') +optparse.add_option('-i','--interval',dest='interval',help='Minute Interval [default=1, max=60]',default='1') +optparse.add_option('-x','--lat',dest='lat',help='Latitude defaults to ' + lat,default=lat) +optparse.add_option('-y','--lng',dest='lng',help='Longitude defaults to ' + lng,default=lng) +optparse.add_option('-f','--force',action='store_const',const=1,dest='force',help='Force lookup/update',default=0) +(options, args) = optparse.parse_args() + +options.ttype = 'civil' + +def min_round(x,base): + minute = dt.datetime.strptime(str(int(base * int(float(x)/base))),'%M') + return minute.strftime("%M") + +def riseset_info(lat,lng,date,ttype): + ret = {} + apiUrl="https://api.sunrise-sunset.org/json?lat=" + str(lat) + "&lng=" + str(lng) + "&date=" + str(date) + response = url.urlopen(apiUrl).read() + result = json.loads(response) + + if ttype == 'standard': + sunrise = result['results']['sunrise'] + sunset = result['results']['sunset'] + elif ttype == 'civil': + sunrise = result['results']['civil_twilight_begin'] + sunset = result['results']['civil_twilight_end'] + elif ttype == 'nautical': + sunrise = result['results']['nautical_twilight_begin'] + sunset = result['results']['nautical_twilight_end'] + elif ttype == 'astronomical': + sunrise = result['results']['astronomical_twilight_begin'] + sunset = result['results']['astronomical_twilight_end'] + else: + print + print "ERROR: Invalid Time Type" + print + optparse.print_help() + print + os._exit(1) + + srtime = dt.datetime.strptime(sunrise,"%I:%M:%S %p") + sstime = dt.datetime.strptime(sunset,"%I:%M:%S %p") + + sunrise_H = srtime.strftime("%H") + sunrise_M = srtime.strftime("%M") + sunrise_MR = min_round(x=sunrise_M,base=int(options.interval)) + srtime = dt.datetime.strptime(sunrise_H + ':' + str(sunrise_MR),"%H:%M") + ret['sunrise'] = srtime + ret['sunrise_text'] = srtime.strftime("%H%M") + + sunset_H = sstime.strftime("%H") + sunset_M = sstime.strftime("%M") + sunset_MR = min_round(x=sunset_M,base=int(options.interval)) + if sunset_M != sunset_MR: + sunset_MR = sunset_MR + int(options.interval) + if sunset_MR == 60: + sunset_H = int(sunset_H) + 1 + sunset_MR = 0 + sstime = dt.datetime.strptime(str(sunset_H) + ':' + str(sunset_MR),"%H:%M") + ret['sunset'] = sstime + ret['sunset_text'] = sstime.strftime("%H%M") + return ret + + +# +# Generating file list and getting sunrise/sunset data +# +years = os.listdir('.') +for year in years: + if not os.path.isfile(year): + months = os.listdir(year+'/.') + for month in months: + if not os.path.isfile(year+'/'+month): + days = os.listdir(year+'/'+month+'/.') + for day in days: + ymd=year+'/'+month+'/'+day + if not os.path.isfile(ymd): + #print year+'/'+month+'/'+day + if options.force or not os.path.isfile(ymd+'/sunrise.info') or not os.path.isfile(ymd+'/sunset.info'): + riseset = riseset_info(lat=options.lat,lng=options.lng,date=year+'-'+month+'-'+day,ttype=options.ttype) + if options.force or not os.path.isfile(ymd+'/sunrise.info'): + sunrise_file = open(ymd+'/sunrise.info','w') + sunrise_file.write(riseset['sunrise_text']) + sunrise_file.close() + #print year+'-'+month+'-'+day+' Sunrise: ' + str(riseset['sunrise']) + if options.force or not os.path.isfile(ymd+'/sunset.info'): + sunset_file = open(ymd+'/sunset.info','w') + sunset_file.write(riseset['sunset_text']) + sunset_file.close() + #print year+'-'+month+'-'+day+' Sunset: ' + str(riseset['sunset']) + riseset['date'] = year+'-'+month+'-'+day + else: + riseset = {} + riseset['date'] = year+'-'+month+'-'+day + sunrise_file = open(ymd+'/sunrise.info','r') + riseset['sunrise_text'] = sunrise_file.read() + riseset['sunrise'] = dt.datetime.strptime(riseset['sunrise_text'],"%H%M") + sunrise_file.close() + sunset_file = open(ymd+'/sunset.info','r') + riseset['sunset_text'] = sunset_file.read() + riseset['sunset'] = dt.datetime.strptime(riseset['sunset_text'],"%H%M") + sunset_file.close() + files = os.listdir(ymd+'/.') + for file in files: + if file != 'sunrise.info' and file != 'sunset.info': + filetemp = file.split('_') + filetime_text = filetemp[2].split('.') + filetime = dt.datetime.strptime(filetime_text[0],"%H%M") + if filetime < riseset['sunrise'] or filetime > riseset['sunset']: + continue + else: + filemin = filetime.strftime("%M") + if str(filemin) == str(min_round(x=filemin,base=int(options.interval))): + print ymd+'/'+file \ No newline at end of file