#!/usr/bin/env python from __future__ import print_function import sys import numpy as np import cv2 import time import ctypes import multiprocessing from astropy.coordinates import EarthLocation from astropy.time import Time from astropy.io import fits import astropy.units as u from utils import get_sunset_and_sunrise # Capture images def capture(buf,z1,t1,z2,t2,device,nx,ny,nz,tend): # Array flag first=True # Loop until reaching end time while float(time.time())tend: break # Exiting print("Exiting compress") # Main function if __name__ == '__main__': # Current time tnow=Time.now() # Set location loc=EarthLocation(lat=52.8344*u.deg,lon=6.3785*u.deg,height=10*u.m) # Reference altitude refalt=-6.0*u.deg # Get sunrise and sunset times state,tset,trise=get_sunset_and_sunrise(tnow,loc,refalt) # Start/end logic if state=="sun never rises": print("The sun never rises. Exiting program.") sys.exit() elif state=="sun never sets": print("The sun never sets.") tend=tnow+24*u.h elif (trise=tset): dt=np.floor((tset-tnow).to(u.s).value) print("The sun is above the horizon. Sunset at %s.\nWaiting %.0f seconds."%(tset.isot,dt)) tend=trise try: time.sleep(dt) except KeyboardInterrupt: sys.exit() print("Starting data acquisition. Acquisition will end at "+tend.isot) # Settings devid=int(sys.argv[1]) nx=720 ny=576 nz=250 # Initialize device device=cv2.VideoCapture(devid) # Set properties device.set(3,nx) device.set(4,ny) # Initialize arrays z1base=multiprocessing.Array(ctypes.c_uint8,nx*ny*nz) z1=np.ctypeslib.as_array(z1base.get_obj()).reshape(nz,ny,nx) t1base=multiprocessing.Array(ctypes.c_double,nz) t1=np.ctypeslib.as_array(t1base.get_obj()) z2base=multiprocessing.Array(ctypes.c_uint8,nx*ny*nz) z2=np.ctypeslib.as_array(z2base.get_obj()).reshape(nz,ny,nx) t2base=multiprocessing.Array(ctypes.c_double,nz) t2=np.ctypeslib.as_array(t2base.get_obj()) buf=multiprocessing.Value('i',0) # Set processes pcapture=multiprocessing.Process(target=capture,args=(buf,z1,t1,z2,t2,device,nx,ny,nz,tend.unix)) pcompress=multiprocessing.Process(target=compress,args=(buf,z1,t1,z2,t2,nx,ny,nz,tend.unix)) # Start pcapture.start() pcompress.start() # End try: pcapture.join() pcompress.join() except KeyboardInterrupt: pcapture.terminate() pcompress.terminate() # Release device device.release()