def main(machine): # Load calibration file calibration = Calibration() table = calibration.load_calibration_file(calibration_file) # Preparation: machine.connect() machine.home() # Movements: # -------------------------------------------------------------------------------------- # Setup initial configuration setup_movements = [('A1', 'B4'), ('A8', 'B7'), ('H8', 'E6'), ('D1', 'D2')] for origin, destination in setup_movements: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # Capture attacking piece out_point = chess_to_coord('B0', table) #[0], calibration.real_points['out_white'][1]) pick_and_place(machine, chess_to_coord('B4', table), out_point) t.sleep(1) # Simple movements movement_list = [('B7', 'B4'), ('E6', 'D5'), ('H1', 'H5'), ('D5', 'D6'), ('B4','B6'), ('D6','C7'), ('B6','F6'), ('C7','D7'), ('H5','H7'), ('D7','D8'), ('F6','F8')] for origin, destination in movement_list: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # Restore initial position cleanup_movements = [('H7', 'H1'), ('D2', 'D1'), ('B0', 'A1'), ('F8', 'F4'), ('D8', 'H8'), ('F4', 'A8')] for origin, destination in cleanup_movements: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # End machine.home() try: cxy.save_to_file('chess.gcode') except AttributeError: pass machine.disconnect()
def createKittiFormatedDataset(self, sampleCounter, configurations): # generate file name n_digits = len(str(sampleCounter)) output_file_name = "" # ex: 000000.txt for i in range(0, 6 - n_digits): output_file_name = output_file_name + "0" if len(output_file_name) is not 6: output_file_name = output_file_name + str(sampleCounter) # create the hierarchy of directories Path(self.kittiOutputSamplesDir).mkdir(parents=True, exist_ok=True) Path(self.kittiViewsDir).mkdir(parents=True, exist_ok=True) Path(self.kittiVelodyneDir).mkdir(parents=True, exist_ok=True) Path(self.kittiLabelsDir).mkdir(parents=True, exist_ok=True) Path(self.kittiCalibDir).mkdir(parents=True, exist_ok=True) # save image self.gtaSample.imageView.saveImage(self.gtaSample.imageView.kittiImage, self.kittiViewsDir, output_file_name + ".png") if configurations.includeIntensity[0]: # save point cloud - the full rotated point cloud saveKittiVelodyneFile( addIntensityToPointCloud( self.gtaSample.pcData.list_rotated_raw_pc, dummy_value=self.configurations.includeIntensity[1]), output_file_name + ".bin", self.kittiVelodyneDir) else: saveKittiVelodyneFile(self.gtaSample.pcData.list_rotated_raw_pc, output_file_name + ".bin", self.kittiVelodyneDir) # save calibration info calib = Calibration((self.gtaSample.imageView.kittiImage.shape[1], self.gtaSample.imageView.kittiImage.shape[0]), self.configurations.fov) calib.saveCalibrationFile(self.kittiCalibDir, output_file_name + ".txt") # labels info label = Labeling(self.gtaSample, calib, self.kittiLabelsDir, output_file_name + ".txt", configurations, fv_only_entities=True) if label.isEmpty: self.isEmpty = True print("Gta Sample doesn't have entities! It will be ignored.") # remove sample with empty label file os.remove(self.kittiVelodyneDir + output_file_name + ".bin") os.remove(self.kittiViewsDir + output_file_name + ".png") os.remove(self.kittiCalibDir + output_file_name + ".txt")
def __init__(self): self.cal = Calibration() self.last_integration_mjd_timestamp = None self.last_requested_freq_ghz = None self.zenith_opacity = None self.opacity_coeffs = None self.L_BAND_ZENITH_TAU = .008 self.frequency_range = None self.time_range = None self.db_time_range = None self.log = None
def main(machine): # Load calibration file calibration = Calibration() table = calibration.load_calibration_file(calibration_file) # Preparation: machine.connect() machine.home() # Movements: # -------------------------------------------------------------------------------------- # Setup initial configuration setup_movements = [('A1', 'B4'), ('A8', 'B7'), ('H8', 'E6'), ('D1', 'D2')] for origin, destination in setup_movements: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # Capture attacking piece out_point = chess_to_coord( 'B0', table) #[0], calibration.real_points['out_white'][1]) pick_and_place(machine, chess_to_coord('B4', table), out_point) t.sleep(1) # Simple movements movement_list = [('B7', 'B4'), ('E6', 'D5'), ('H1', 'H5'), ('D5', 'D6'), ('B4', 'B6'), ('D6', 'C7'), ('B6', 'F6'), ('C7', 'D7'), ('H5', 'H7'), ('D7', 'D8'), ('F6', 'F8')] for origin, destination in movement_list: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # Restore initial position cleanup_movements = [('H7', 'H1'), ('D2', 'D1'), ('B0', 'A1'), ('F8', 'F4'), ('D8', 'H8'), ('F4', 'A8')] for origin, destination in cleanup_movements: pick_and_place(machine, chess_to_coord(origin, table), chess_to_coord(destination, table)) t.sleep(5) machine.home() # End machine.home() try: cxy.save_to_file('chess.gcode') except AttributeError: pass machine.disconnect()
def run_calibration(args): calibration_info = { "model_name": args.model_name, "in_prototxt": './{}/deploy.prototxt'.format(args.model_name), "in_caffemodel": './{}/{}.caffemodel'.format(args.model_name, args.model_name), "iteration": 100, "enable_memory_opt": args.memory_opt, "histogram_bin_num": 2048, "math_lib_path": '../../calibration_tool/lib/calibration_math.so' } print(calibration_info) calib = Calibration(calibration_info) calib.calc_tables() calib.export_model('./{}/bmnet_{}_int8.caffemodel'.format( args.model_name, args.model_name)) calib.export_calirabtion_pb2('./{}/bmnet_{}_calibration_table'.format( args.model_name, args.model_name))
def __init__(self, z, kmin, kmax, N, Y200rhoc, sigma_8, BM0=0.0, alpha=0.0, sLnM0=0.1, beta=0.0, fnl=0, bfNL=False, bfNL_Ph=False, H0=67., Omegab0=0.02256 / 0.67**2, Omegam0=(0.1142 + 0.02256) / 0.67**2, ns=0.962, w0=-1.0, wa=0.0, Tcmb0=2.75, Ps=None, Mlim=1e14, Mcut=4e14): Distance.__init__(self, z, H0, Omegab0, Omegam0, ns, w0, wa, Tcmb0) Calibration.__init__(self, z, BM0=BM0, alpha=alpha, sLnM0=sLnM0, beta=beta) self.kmin = kmin self.kmax = kmax self.N = N self.Mlim = Mlim self.Y200rhoc = Y200rhoc self.h = H0 / 100 self.fnl = fnl self.bfNL = bfNL self.bfNL_Ph = bfNL_Ph self.sigma_8 = sigma_8 self.Ps = Ps
def setup(self): # load in the observation file for this object self.obs = Observation(self.filename) self.display = loupe() #''mosasaurus', # xsize=self.obs.xsize*self.obs.displayscale, # ysize=self.obs.ysize*self.obs.displayscale) # create a night object, associated with this observation self.night = Night(self.obs) # set up the Calibration self.calib = Calibration(self) # create a mask self.mask = Mask(self)
def getReferenceIntegration(self, cal_on, cal_off): cal = Calibration() cal_ondata = cal_on['DATA'] cal_offdata = cal_off['DATA'] cref, exposure = cal.total_power(cal_ondata, cal_offdata, cal_on['EXPOSURE'], cal_off['EXPOSURE']) tcal = cal_off['TCAL'] tsys = cal.tsys( tcal, cal_ondata, cal_offdata ) dateobs = cal_off['DATE-OBS'] timestamp = self.pu.dateToMjd(dateobs) tambient = cal_off['TAMBIENT'] elevation = cal_off['ELEVATIO'] return cref, tsys, exposure, timestamp, tambient, elevation
def onLoadButtonClicked(self): calibration_filename = self.calibrationFileLineEdit.text() points_filename = self.pointsFileLineEdit.text() if calibration_filename and points_filename: # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName( None, "Select SVG file", filter='*.svg') if filename: try: # Load image data from calibration file calibration = Calibration() calibration.load_zipfile(calibration_filename) self.loadImage(calibration) except Calibration.LoadException, e: QtGui.QMessageBox().critical( self, 'Error', 'Calibration file not compatible!') return False try: # Load points data from points file self.limits = Calibration.load_calibration_file( points_filename) except Calibration.LoadException, e: QtGui.QMessageBox().critical( self, 'Error', 'Points file not compatible!') return False try: # Load data from files self.trajectory.load_paths_from_svg(filename) # Set trajectories on controls trajectory_choices = [ "Trajectory #%d" % i for i, path in enumerate(self.trajectory.paths) ] self.trajectoryComboBox.clear() self.trajectoryComboBox.addItems(trajectory_choices) self.setTrajectoryControlsEnabled(True) self.update() except TrajectoryController.TrajectoryException, e: pass
def __init__(self): self.cal = Calibration() self.last_integration_mjd_timestamp = None self.last_requested_freq_hz = None self.last_zenith_opacity = None self.number_of_opacity_files = None self.opacity_coeffs = None self.L_BAND_ZENITH_TAU = .008
def getReferenceIntegration(self, cal_on, cal_off, scale): cal = Calibration() cal_ondata = cal_on['DATA'] cal_offdata = cal_off['DATA'] cref, exposure = cal.total_power(cal_ondata, cal_offdata, cal_on['EXPOSURE'], cal_off['EXPOSURE']) tcal = cal_off['TCAL'] * scale tsys = cal.tsys(tcal, cal_ondata, cal_offdata) dateobs = cal_off['DATE-OBS'] timestamp = self.pu.dateToMjd(dateobs) tambient = cal_off['TAMBIENT'] elevation = cal_off['ELEVATIO'] return cref, tsys, exposure, timestamp, tambient, elevation
def __init__(self): self.stream_parent_conn, self.stream_child_conn = mp.Pipe() model_path = '../data/faster_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb' self.odapi = DetectorAPI(path_to_ckpt=model_path) self.cal = Calibration() self.comm = Comm() self.stream = Stream(self.stream_child_conn) self.str = mp.Process(target=self.stream.run, args=()) self.eat = None self.socket_update = None self.frame_lock = mp.RLock() self.coords_lock = mp.RLock() self.coords = []
def onLoadButtonClicked(self): calibration_filename = self.calibrationFileLineEdit.text() points_filename = self.pointsFileLineEdit.text() if calibration_filename and points_filename: # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName(None, "Select SVG file", filter='*.svg' ) if filename: try: # Load image data from calibration file calibration = Calibration() calibration.load_zipfile(calibration_filename) self.loadImage(calibration) except Calibration.LoadException, e: QtGui.QMessageBox().critical(self, 'Error', 'Calibration file not compatible!') return False try: # Load points data from points file self.limits = Calibration.load_calibration_file(points_filename) except Calibration.LoadException, e: QtGui.QMessageBox().critical(self, 'Error', 'Points file not compatible!') return False try: # Load data from files self.trajectory.load_paths_from_svg(filename) # Set trajectories on controls trajectory_choices = [ "Trajectory #%d" % i for i, path in enumerate(self.trajectory.paths)] self.trajectoryComboBox.clear() self.trajectoryComboBox.addItems(trajectory_choices) self.setTrajectoryControlsEnabled(True) self.update() except TrajectoryController.TrajectoryException, e: pass
def __init__(self): #self.__src = np.float32([[490, 482], [820, 482], [1280, 670], [20, 670]]) #self.__dst = np.float32([[0, 0], [1280, 0], [1280, 720], [0, 720]]) #self.__src = np.float32([[490, 482], [810, 482], [1250, 720], [0, 720]]) #self.__dst = np.float32([[0, 0], [1280, 0], [1250, 720], [40, 720]]) self.__src = np.float32([[190, 720], [589, 457], [698, 457], [1145, 720]]) self.__dst = np.float32([[340, 720], [340, 0], [995, 0], [995, 720]]) self.__mtx, self.__dist = Calibration().get_from_pickle() self.__wraped_binary_image = None self.M = None self.converseM = None
def start(self): # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName( None, "Select calibration file", filter='*.zip') if filename: # Try to open the calibration file try: self.calibration = Calibration() self.calibration.load_zipfile(filename) self.calibration.start() self.loadImage() self.update() self.show() return True except Calibration.LoadException: QtGui.QMessageBox().critical(self, 'Error', 'File not compatible!') return False # except Exception, e: # QtGui.QMessageBox().critical(self, 'Error', str(e)) # return False else: return False
def parseMessage(self, client, message): """ Check who is the message from to redirect it to User / Tag / Camera / Calibration or create a new instance of User / Tag / Camera / Calibration :param client: :param message: :return: """ if self.cameras.has_key(str(client['address'])): #print "Message from Camera" self.cameras[str(client['address'])].push(message) elif self.users.has_key(str(client['address'])): print "Message from User" elif self.tags.has_key(str(client['address'])): print "Message from Tag" elif self.calibration.has_key(str(client['address'])): self.calibration[str(client['address'])].push(message) print "Message from Calibration" # This message is coming from an unknown client else: if message.split("-")[0] == "camera": self.cameras[str(client['address'])] = Camera(client, message.split("-")[1]) # Add Observers linking every user to every camera's update for key in self.users: if isinstance(self.users[key], User): self.cameras[str(client['address'])].new2DPointNotifier.addObserver(self.users[key].position.newPoint2DObserver) self.cameras[str(client['address'])].point2DdeletedNotifier.addObserver(self.users[key].position.point2DDeletedObserver) elif message.split("-")[0] == "tag": print "Hello TAG" # TODO elif message.split("-")[0] == "user": user = User(client, self.server, message.split("-")[1]) self.users[str(client['address'])] = user # Add Observers linking every user to every camera's update for key in self.cameras: if isinstance(self.cameras[key], Camera): self.cameras[key].new2DPointNotifier.addObserver(user.position.newPoint2DObserver) self.cameras[key].point2DdeletedNotifier.addObserver(user.position.point2DDeletedObserver) elif message == "calibration": self.calibration[str(client['address'])] = Calibration(self.cameras, self.server, client)
def run_calibration(arg): calibration_info = { "model_name": arg.model_name, "in_prototxt": os.path.join(arg.model_path, 'deploy_2x.prototxt'), "in_caffemodel": os.path.join(arg.model_path, 'espcn.caffemodel'), "iteration": 100, "enable_memory_opt": arg.memory_opt, "histogram_bin_num": 2048, "math_lib_path": "../../calibration_tool/build/calibration_math.so" } print(calibration_info) calib = Calibration(calibration_info) calib.calc_tables() calib.export_model( os.path.join(arg.model_path, 'bmnet_espcn_int8.caffemodel')) calib.export_calirabtion_pb2( os.path.join(arg.model_path, 'bmnet_espcn_calibration_table'))
def run_calibration(in_proto, in_caffemodel, out_table, out_caffemodel, thresholds=None): calibration_info = { "model_name": 'tune', "in_prototxt": in_proto, "in_caffemodel": in_caffemodel, "out_prototxt": './deploy_out.prototxt', "iteration": 1, "enable_memory_opt": 0, "math_lib_path": '../../calibration_tool/lib/calibration_math.so' } print(calibration_info) calib = Calibration(calibration_info) calib.tune_tables(thresholds) calib.export_model(out_caffemodel) calib.export_calirabtion_pb2(out_table)
def run_calibration(args): calibration_info = { "model_name": args.model_name, "in_prototxt": os.path.join(args.model_path, 'deploy.prototxt'), "in_caffemodel": os.path.join(args.model_path, 'custom.caffemodel'), "iteration": 100, "enable_memory_opt": args.memory_opt, "enable_calibration_opt": 1, "histogram_bin_num": 204800, "math_lib_path": './lib/calibration_math.so', "enable_concat_quantize": 0 } print(calibration_info) calib = Calibration(calibration_info) calib.calc_tables() calib.export_model('{}/bmnet_{}_int8.caffemodel'.format( args.model_path, args.model_name)) calib.export_calirabtion_pb2('{}/bmnet_{}_calibration_table'.format( args.model_path, args.model_name))
def reduce(filename='wasp94_140801.obs'): # generate an observation object obs = Observation(filename) # load the headers for this observation obs.loadHeaders() # create a night object, associated with this observation night = Night(obs) # create an observing log from all the image headers from this night night.obsLog() # set up the calibration calib = Calibration(obs) # loop through the CCD's needed for this observation, and make sure they are stitched #for n in obs.nNeeded: # ccd = CCD(obs,n=n,calib=calib) # ccd.createStitched(visualize=True) mask = Mask(calib) for a in mask.apertures: a.displayStamps(a.images) a.extractAll(remake=True)
def start(self): # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName(None, "Select calibration file", filter='*.zip' ) if filename: # Try to open the calibration file try: self.calibration = Calibration() self.calibration.load_zipfile(filename) self.calibration.start() self.loadImage() self.update() self.show() return True except Calibration.LoadException: QtGui.QMessageBox().critical(self, 'Error', 'File not compatible!') return False # except Exception, e: # QtGui.QMessageBox().critical(self, 'Error', str(e)) # return False else: return False
if setup.parent_directory.exists(): if args.overwrite == True: shutil.rmtree(setup.parent_directory, ignore_errors=True) else: logger.error( "directory already exists. exiting. use overwrite flag to delete") sys.exit() # do checks ... if not RunPreCheck(setupfile).run_all(): sys.exit() # get the current directory cwd = os.getcwd() # Calibrate calib = Calibration(setupfile) calib.PrepareCalibration() #calib.AdjustCalibTable() #calib.CreateAnalScript(calib.clbdirc, 'Calibration.db', 0) final_file = calib.clbdirc.joinpath(calib.final_chrtfile) # Run the model once success, message = acc.ForwardModel(calib.clbdirc, calib.userid, calib.catchid, final_file) if not success: logger.error('model failure') logger.info(message) sys.exit()
def load_calibration_from_file(self, filepath): self.points = Calibration.load_calibration_file(filepath)
def main(): args = get_args() Calibration(args.AP_BackHatCalib, args.Ref_Calib_Back, args.KabaschRotTransBack)
plt.rc('ytick', labelsize=14) plt.rc('lines', lw=2) base_folder = '2016-07-26/' ambient_folder = base_folder + 'ambient/600um/' dark_folder = base_folder + 'dark/' agitated_folder = base_folder + 'single/600um/agitated/' unagitated_folder = base_folder + 'single/600um/unagitated/' ext = '.fit' nf = {} ff = {} nf['calibration'] = Calibration( [dark_folder + 'nf_dark_' + str(i).zfill(3) + ext for i in xrange(10)], None, [ ambient_folder + 'nf_ambient_' + str(i).zfill(3) + '_0.1' + ext for i in xrange(10) ]) print 'NF calibration initialized' ff['calibration'] = Calibration( [dark_folder + 'ff_dark_' + str(i).zfill(3) + ext for i in xrange(10)], None, [ ambient_folder + 'ff_ambient_' + str(i).zfill(3) + '_0.1' + ext for i in xrange(10) ]) print 'FF calibration initialized' empty_data = {'images': [], 'fft': [], 'freq': []} for test in ['agitated', 'unagitated', 'baseline']: nf[test] = deepcopy(empty_data) ff[test] = deepcopy(empty_data)
REF_VIS = OutputsDir + OutputFolderName + "/visualize_frames_ref_32.csv" REF_ROAD = OutputsDir + OutputFolderName + "/road_normalized_ref.csv" # Output files OP_R2B = OutputsDir + OutputFolderName + "/road_proj_to_back.csv" OP_intialLabeling = OutputsDir + OutputFolderName + "/ContGazeIntialLabelsAllFrames_32_V4.csv" OP_VIS = VisualizeDir + "/meshsave_back_2.mat" OutDoorTagID = 300 B_R_offset = 0 # sync Offset in frame number between the back and the road videos F_offset = 0 noRotationFlag = 0 #1 print( "Calculating the rotation and translation matrices for the back camera" ) Calibration(AP_HAT_Calib_BACK, Ref_Calib_Back, KabaschRotTransBack, OutDoorTagID, noRotationFlag) print( "Calculating the rotation and translation matrices for the road camera" ) Calibration(AP_Road_calib, Ref_Calib_Road, KabaschRotTransRoad, OutDoorTagID, noRotationFlag) print( "Standardizing visualize and converting coordinates back to AprilTag format" ) standardize_visualizeT2(AP_HAT_Calib_BACK, OP_VIS, STD_VIS) print( "Standardizing AP_ROAD by considering only AprilTag marker frames and changing file schema" )
class Weather: def __init__(self): self.cal = Calibration() self.last_integration_mjd_timestamp = None self.last_requested_freq_ghz = None self.zenith_opacity = None self.opacity_coeffs = None self.L_BAND_ZENITH_TAU = .008 self.frequency_range = None self.time_range = None self.db_time_range = None self.log = None def _opacity_database(self, timestamp): # retrieve a list of opacity coefficients files, based on a given directory # and filename structure opacity_coefficients_filename = False opacity_files = glob.glob( '/home/gbtpipeline/weather/CoeffsOpacityFreqList_avrg_*.txt') if 0 == len(opacity_files): return False, False # sort the list of files so they are in chronological order opacity_files.sort() # check the date of each opacity coefficients file for idx, opacity_candidate_file in enumerate(opacity_files): dates = opacity_candidate_file.split('_')[-2:] opacity_file_timestamp = [] for date in dates: opacity_file_timestamp.append(int(date.split('.')[0])) opacity_file_starttime = opacity_file_timestamp[0] opacity_file_stoptime = opacity_file_timestamp[1] # when timestamp is older than available ranges if idx == 0 and timestamp < opacity_file_starttime: if self.log: self.log.doMessage( 'ERR', 'ERROR: Date is too early for opacities.') self.log.doMessage( 'ERR', ' Try setting zenith tau at command line.') self.log.doMessage('ERR', timestamp, '<', opacity_file_starttime) else: print 'ERROR: Date is too early for opacities.' print ' Try setting zenith tau at command line.' print timestamp, '<', opacity_file_starttime sys.exit(9) break if (opacity_file_starttime <= timestamp < opacity_file_stoptime): opacity_coefficients_filename = opacity_candidate_file opacity_db_range = (opacity_file_starttime, opacity_file_stoptime) break # uses most recent info if not opacity_coefficients_filename: # if the mjd in the index file comes after the date string in all of the # opacity coefficients files, then we can assume the current opacity # coefficients file will apply. a date string is only added to the opacity # coefficients file when it is no longer the most current. opacity_coefficients_filename = '/home/gbtpipeline/weather/CoeffsOpacityFreqList_avrg.txt' opacity_db_range = 'LATEST' # opacities coefficients filename if opacity_coefficients_filename and os.path.exists( opacity_coefficients_filename): if self.log: self.log.doMessage('DBG', 'Using coefficients from', opacity_coefficients_filename) else: print 'Using coefficients from', opacity_coefficients_filename coeffs = self._retrieve_opacity_coefficients( opacity_coefficients_filename) return coeffs, opacity_db_range else: if self.log: self.log.doMessage('ERR', 'No opacity coefficients file') else: print 'ERROR: No opacity coefficients file' return False, False def _retrieve_opacity_coefficients(self, opacity_coefficients_filename): """Return opacities (taus) derived from a list of coeffients These coefficients are produced from Ron Madalenna's getForecastValues script Keywords: infilename -- input file name needed for project name mjd -- date for data freq -- list of frequencies for which we seek an opacity Returns: a list of opacity coefficients for the time range of the dataset """ opacity_file = open(opacity_coefficients_filename, 'r') coeffs = [] if opacity_file: for line in opacity_file: # find the most recent forecast and parse out the coefficients for # each band # coeffs[0] is the mjd timestamp # coeffs[1] are the coefficients for 2-22 GHz # coeffs[2] are the coefficients for 22-50 GHz # coeffs[3] are the coefficients for 70-116 GHz coeffs.append((float(line.split('{{')[0]), [ float(xx) for xx in line.split('{{')[1].split('}')[0].split(' ') ], [ float(xx) for xx in line.split('{{')[2].split('}')[0].split(' ') ], [ float(xx) for xx in line.split('{{')[3].split('}')[0].split(' ') ])) else: print "WARNING: Could not read coefficients for Tau in", opacity_coefficients_filename return False return coeffs def retrieve_zenith_opacity(self, integration_mjd_timestamp, freq_hz, log=None): self.log = log freq_ghz = freq_hz / 1e9 # if less than 2 GHz, opacity coefficients are not available if freq_ghz < 2: return self.L_BAND_ZENITH_TAU # if the frequency is in the same range and # the time is within the same record (1 hr window) of the last # recorded opacity coefficients, then just reuse the last # zenith opacity value requested if ((self.frequency_range and (self.frequency_range[0] <= freq_ghz <= self.frequency_range[1])) and (self.time_range and (self.time_range[0] <= integration_mjd_timestamp <= self.time_range[1]))): return self.zenith_opacity self.last_integration_mjd_timestamp = integration_mjd_timestamp self.last_requested_freq_ghz = freq_ghz # if we don't have a db time range OR # we do have a time range AND # the range is 'LATEST' AND # timestamp is not w/in the same hour as the last set of coeffs, OR # the range has values AND # our new timestamp is not in the range # THEN # get another set of coefficients from a different file if ((not self.db_time_range) or (self.db_time_range == 'LATEST' and not (self.time_range[0] <= integration_mjd_timestamp < self.time_range[1])) or (self.db_time_range and not (self.db_time_range[0] <= integration_mjd_timestamp < self.db_time_range[1]))): log.doMessage( 'DBG', '-----------------------------------------------------') log.doMessage( 'DBG', 'Time or Frequency out of range. Determine new opacity') log.doMessage( 'DBG', '-----------------------------------------------------') log.doMessage('DBG', 'opacity', self.zenith_opacity) log.doMessage('DBG', 'timestamp', integration_mjd_timestamp, 'prev.', self.last_integration_mjd_timestamp) log.doMessage('DBG', 'freq', freq_ghz, 'prev.', self.last_requested_freq_ghz) log.doMessage('DBG', 'freq range', self.frequency_range) if bool(self.frequency_range): log.doMessage( 'DBG', ' freq in range == ', bool(self.frequency_range[0] <= freq_ghz <= self.frequency_range[1])) log.doMessage('DBG', 'time range', self.time_range) if bool(self.time_range): log.doMessage( 'DBG', ' time in range ==', bool(self.time_range and (self.time_range[0] <= integration_mjd_timestamp <= self.time_range[1]))) log.doMessage('DBG', 'DB time range', self.db_time_range) if bool(self.db_time_range): log.doMessage( 'DBG', ' time in DB range ==', bool(self.db_time_range and (self.db_time_range[0] <= integration_mjd_timestamp <= self.db_time_range[1]))) self.opacity_coeffs, self.db_time_range = self._opacity_database( integration_mjd_timestamp) if (not self.opacity_coeffs) or (not self.db_time_range): return False log.doMessage('DBG', 'DB time range:', self.db_time_range) for coeffs_line in self.opacity_coeffs: if (2 <= freq_ghz <= 22): self.frequency_range = (2, 22) coefficients_index = 1 elif (22 < freq_ghz <= 50): self.frequency_range = (22, 50) coefficients_index = 2 elif (50 < freq_ghz <= 116): self.frequency_range = (50, 116) coefficients_index = 3 if integration_mjd_timestamp >= coeffs_line[0]: prev_time = coeffs_line[0] prev_coeffs = coeffs_line[coefficients_index] elif integration_mjd_timestamp < coeffs_line[0]: next_time = coeffs_line[0] next_coeffs = coeffs_line[coefficients_index] break self.time_range = (prev_time, next_time) log.doMessage('DBG', 'Coefficient entry time range:', self.time_range) time_corrected_coeffs = [] for coeff in zip(prev_coeffs, next_coeffs): new_coeff = self.cal.interpolate_by_time( coeff[0], coeff[1], prev_time, next_time, integration_mjd_timestamp) time_corrected_coeffs.append(new_coeff) self.zenith_opacity = self.cal.zenith_opacity(time_corrected_coeffs, freq_ghz) log.doMessage('DBG', 'Zenith opacity:', self.zenith_opacity) return self.zenith_opacity
def setup(self): self.cal = Calibration()
class CalibrationWidget(WorkspaceWidget, CoreXYEventListener): def __init__(self, parent, machine): super(CalibrationWidget, self).__init__(parent, machine) self.name = "CalibrationWidget" self.icon = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'resources', 'icons', 'document-properties.png') self.tooltip = "Calibration workspace" self.calibration = None self.machine.add_listener(self) self.nextButton = None self.cancelButton = None self.progressBar = None self.targetPointLabel = None self.targetImageLabel = None self.toolheadPosLabel = None self.hintLabel = None self.graphicsView = None self.pixmap = QtGui.QPixmap() self.original_rect = None self.loadUI() self.update() def loadUI(self): # Load UI main_widget = load_ui( os.path.join(os.path.realpath(os.path.dirname(__file__)), 'CalibrationWidget.ui'), self) layout = QtGui.QVBoxLayout() layout.addWidget(main_widget) self.setLayout(layout) # Find references to controls self.nextButton = self.findChild(QtGui.QPushButton, "nextButton") self.cancelButton = self.findChild(QtGui.QPushButton, "cancelButton") self.progressBar = self.findChild(QtGui.QProgressBar, "progressBar") self.targetPointLabel = self.findChild(QtGui.QLabel, "targetPointLabel") self.targetImageLabel = self.findChild(QtGui.QLabel, "targetImageLabel") self.toolheadPosLabel = self.findChild(QtGui.QLabel, "toolheadPosLabel") self.hintLabel = self.findChild(QtGui.QLabel, "hintLabel") self.graphicsView = self.findChild(QtGui.QGraphicsView, "graphicsView") # Connect signals self.nextButton.clicked.connect(self.updateProgressBar) self.nextButton.clicked.connect(self.onNextButtonClicked) self.nextButton.setEnabled(False) self.hintLabel.setText("Please connect to the machine to start") self.cancelButton.clicked.connect(self.abort) def update(self): self.updateProgressBar() self.updateLabels() self.updateImage() def start(self): # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName( None, "Select calibration file", filter='*.zip') if filename: # Try to open the calibration file try: self.calibration = Calibration() self.calibration.load_zipfile(filename) self.calibration.start() self.loadImage() self.update() self.show() return True except Calibration.LoadException: QtGui.QMessageBox().critical(self, 'Error', 'File not compatible!') return False # except Exception, e: # QtGui.QMessageBox().critical(self, 'Error', str(e)) # return False else: return False def abort(self): try: self.calibration.abort() except (AttributeError, Calibration.CalibrationException) as e: print str(e) super(CalibrationWidget, self).abort() def onNextButtonClicked(self): # Get current pos and set it on the calibration object current_toohead_pos = (self.machine.x, self.machine.y) self.calibration.set_current_point_pos(current_toohead_pos) # Next point if not self.calibration.next(): filename, ext = QtGui.QFileDialog.getSaveFileName(None, "Output file", filter='*.xml') if filename: self.calibration.save_calibration_file(filename) else: QtGui.QMessageBox().critical(self, 'Error', 'File not selected') self.update() def updateProgressBar(self): try: self.progressBar.setValue(self.calibration.get_progress() * 100) except: self.progressBar.setValue(0) def updateToolheadPos(self): try: current_toolhead_pos = (self.machine.x, self.machine.y) self.toolheadPosLabel.setText( 'Current toolhead position: (%.2f, %.2f)' % current_toolhead_pos) except TypeError: self.toolheadPosLabel.setText('Current toolhead position: (?, ?)') def updateLabels(self): try: current_point = self.calibration.get_current_point_data() self.targetPointLabel.setText('Target point: %s' % current_point['name']) self.targetImageLabel.setText( 'Target image coordinates: (%.2f, %.2f)' % current_point['coordinates']) except (AttributeError, Calibration.CalibrationException) as e: self.targetPointLabel.setText('Target point: "Point"') self.targetImageLabel.setText('Target image coordinates: (u, v)') self.updateToolheadPos() def loadImage(self): if self.calibration.image_name: self.pixmap.loadFromData( self.calibration.image, os.path.splitext(self.calibration.image_name)[1]) self.original_rect = self.pixmap.rect() self.pixmap = self.pixmap.scaled(480, 339) return True else: return False def updateImage(self): # Load image and create scene scene = QtGui.QGraphicsScene() scene.addItem(QtGui.QGraphicsPixmapItem(self.pixmap)) # Draw markers if self.calibration and self.calibration.calibrating: current_point = self.calibration.get_current_point_data( )['coordinates'] scale_x = 480 / float(self.original_rect.width()) scale_y = 339 / float(self.original_rect.height()) try: pen = QtGui.QPen(QtGui.QColor(0, 255, 0)) pen.setWidth(3) scene.addEllipse(int(current_point[0]*scale_x-15), int(current_point[1]*scale_y-15), 30, 30, \ pen=pen) except Exception, e: print str(e) self.graphicsView.setScene(scene) self.graphicsView.show()
def parseMessage(self, client, message): """ Check who is the message from to redirect it to User / Tag / Camera / Calibration or create a new instance of User / Tag / Camera / Calibration :param client: :param message: :return: """ if self.cameras.has_key(str(client['address'])): #print "Message from Camera" self.cameras[str(client['address'])].push(message) # Update all cameras counters #Todo: Change this method for checking all cameras for lost point (auto check inside point2D ?) for key in self.cameras.keys(): self.cameras[key].update() elif self.users.has_key(str(client['address'])): print "Message from User" elif self.tags.has_key(str(client['address'])): print "Message from Tag" elif self.calibration.has_key(str(client['address'])): self.calibration[str(client['address'])].push(message) print "Message from Calibration" # This message is coming from an unknown client else: if message.split("-")[0] == "camera": self.cameras[str(client['address'])] = Camera( client, message.split("-")[1]) # Add Observers linking every user to every camera's update for key in self.users: if isinstance(self.users[key], User): self.cameras[str( client['address'])].new2DPointNotifier.addObserver( self.users[key].position.newPoint2DObserver) self.cameras[str( client['address'] )].point2DdeletedNotifier.addObserver( self.users[key].position.point2DDeletedObserver) elif message.split("-")[0] == "tag": self.tags[str(client['address'])] = Tag( self.server, client, message.split("-")[1]) for key in self.users: if isinstance(self.users[key], User): # Assign a Tag to User with no Tag if self.users[key].tag == None: self.users[key].setTag(self.tags[str( client['address'])]) elif message.split("-")[0] == "user": user = User(self.server, client, message.split("-")[1]) self.users[str(client['address'])] = user # Add Observers linking every user to every camera's update for key in self.cameras: if isinstance(self.cameras[key], Camera): self.cameras[key].new2DPointNotifier.addObserver( user.position.newPoint2DObserver) self.cameras[key].point2DdeletedNotifier.addObserver( user.position.point2DDeletedObserver) for key in self.tags: if isinstance(self.tags[key], Tag): # Assign a Tag to new User if self.tags[key].isAssigned() == False: user.setTag(self.tags[key]) elif message == "calibration": if (len(self.tags) > 0): self.calibration[str(client['address'])] = Calibration( self.server, client, self.cameras, self.tags.values()[0]) else: self.server.send_message( client, "Please connect a Tag first, and start Calibration again." )
# Paths results_directory = './results' calibration_src_images = './camera_cal' calibration_dst_images = 'Calibration_results' test_images_src = './test_images' test_images_des = 'test_images_results' test_vedios_src = './test_vedios' test_vedios_des = './output_vedios' # Calibration logger_obj = Logger(str(results_directory)) calibration_obj = Calibration(calibration_src_images, calibration_dst_images, (9, 6), logger_obj) ## Get calibration data camera_matrix, distortion_coefficent = calibration_obj.get_calibration_parameters( ) ## Get Parameters pipeline_params = Parameters(logger_obj, camera_matrix, distortion_coefficent) # Get Pipeline object Runner = Pipeline(pipeline_params) def main(): # Images Runner.process_test_images(test_images_src)
def loadData(self, shotNum, skipAPD = 1): """Load all the data for the given shot. Parameters: shotNum -- The shot number to load. skipAPD -- Set to 1 to skip the first APD (because of saturation). """ from numpy import arange self.shotNum = shotNum self.data = {} # Clear old data. self.filename = util.getDataFilePath(shotNum) nc = pycdf.CDF(self.filename) #print "Loading Raw Data\n" str_rawData = nc.var('str_rawdata').get() str_voltsPerBit = nc.var('str_voltsperbit').get() str_deltat = nc.var('str_deltat').get() str_zeroBit = nc.var('str_offset').get() acq_rawData = nc.var('rawdata').get() acq_voltsPerBit = nc.var('voltsperbit').get() acq_deltat = nc.var('deltat').get() acq_offset = nc.var('offset').get() roa = nc.var('roa').get() # take the poly list from the netCDF file (one entry for each channel) and unique-ify it # to have only one entry per poly plist_long = nc.var('poly').get() seen = {} for item in plist_long: seen[item] = 1 plist_short = seen.keys() plist_short.sort() # probably unnecessary, but just in case self.polyList = plist_short # load the calibration data after the file data, so that the poly list can be loaded for this shot self.calib = Calibration() self.calib.loadCalibration(shotNum, plist_short, skipAPD) # The number of data segments is one less than the total number of # segments. The last segment in the data file is used for calculating # the amplifier offsets. self.segments = arange(str_rawData.shape[1] - 1) apdIndex = 0 for poly in self.polyList: self.data[poly] = {} calib = self.calib.getPolyCalibration(poly) for segment in self.segments: numAPD = calib.numAPD ps = PolySegData() ps.calib = calib ps.poly = poly ps.segment = segment ps.shotNum=self.shotNum # If calib.skipAPD is 1, then we don't load the data for the # first APD. start = apdIndex + calib.skipAPD end = apdIndex + numAPD ps.str_voltsPerBit = str_voltsPerBit[start:end] ps.str_deltat = str_deltat[start:end] ps.str_zeroBit = str_zeroBit[start:end] ps.acq_voltsPerBit = acq_voltsPerBit[start:end] ps.acq_deltat = acq_deltat[start:end] ps.acq_offset = acq_offset[start:end] ps.roa = roa[start] # correction because p20 and p21 were flipped in the calib file # for Fall 2012 only, radial calib redone in December #if poly == 20: # ps.roa = 0.6030 #elif poly == 21: # ps.roa = 0.5181 ps.str_rawData = str_rawData[start:end, segment] ps.acq_rawData = acq_rawData[start:end, segment] # It's a bit inefficient to store the offset data with each # PolySegData object, but it is simple. ps.str_offsetRaw = str_rawData[start:end, len(self.segments)] ps.acq_offsetRaw = acq_rawData[start:end, len(self.segments)] # Set the usability flags for the DC and AC channels # The AC channels are set to be unused until AC calib is figured out ps.chanFlagDC = ones(numAPD - calib.skipAPD) #ps.chanFlagAC = ones(numAPD - calib.skipAPD) ps.chanFlagAC = zeros(numAPD - calib.skipAPD) self.data[poly][segment] = ps apdIndex += numAPD
class CalibrationWidget(WorkspaceWidget, CoreXYEventListener): def __init__(self, parent, machine): super(CalibrationWidget, self).__init__(parent, machine) self.name = "CalibrationWidget" self.icon = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'resources', 'icons', 'document-properties.png') self.tooltip = "Calibration workspace" self.calibration = None self.machine.add_listener(self) self.nextButton = None self.cancelButton = None self.progressBar = None self.targetPointLabel = None self.targetImageLabel = None self.toolheadPosLabel = None self.hintLabel = None self.graphicsView = None self.pixmap = QtGui.QPixmap() self.original_rect = None self.loadUI() self.update() def loadUI(self): # Load UI main_widget = load_ui(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'CalibrationWidget.ui'), self) layout = QtGui.QVBoxLayout() layout.addWidget(main_widget) self.setLayout(layout) # Find references to controls self.nextButton = self.findChild(QtGui.QPushButton, "nextButton") self.cancelButton = self.findChild(QtGui.QPushButton, "cancelButton") self.progressBar = self.findChild(QtGui.QProgressBar, "progressBar") self.targetPointLabel = self.findChild(QtGui.QLabel, "targetPointLabel") self.targetImageLabel = self.findChild(QtGui.QLabel, "targetImageLabel") self.toolheadPosLabel = self.findChild(QtGui.QLabel, "toolheadPosLabel") self.hintLabel = self.findChild(QtGui.QLabel, "hintLabel") self.graphicsView = self.findChild(QtGui.QGraphicsView, "graphicsView") # Connect signals self.nextButton.clicked.connect(self.updateProgressBar) self.nextButton.clicked.connect(self.onNextButtonClicked) self.nextButton.setEnabled(False) self.hintLabel.setText("Please connect to the machine to start") self.cancelButton.clicked.connect(self.abort) def update(self): self.updateProgressBar() self.updateLabels() self.updateImage() def start(self): # Ask the user for a file filename, ext = QtGui.QFileDialog.getOpenFileName(None, "Select calibration file", filter='*.zip' ) if filename: # Try to open the calibration file try: self.calibration = Calibration() self.calibration.load_zipfile(filename) self.calibration.start() self.loadImage() self.update() self.show() return True except Calibration.LoadException: QtGui.QMessageBox().critical(self, 'Error', 'File not compatible!') return False # except Exception, e: # QtGui.QMessageBox().critical(self, 'Error', str(e)) # return False else: return False def abort(self): try: self.calibration.abort() except (AttributeError, Calibration.CalibrationException) as e: print str(e) super(CalibrationWidget, self).abort() def onNextButtonClicked(self): # Get current pos and set it on the calibration object current_toohead_pos = (self.machine.x, self.machine.y) self.calibration.set_current_point_pos(current_toohead_pos) # Next point if not self.calibration.next(): filename, ext = QtGui.QFileDialog.getSaveFileName(None, "Output file", filter='*.xml') if filename: self.calibration.save_calibration_file(filename) else: QtGui.QMessageBox().critical(self, 'Error', 'File not selected') self.update() def updateProgressBar(self): try: self.progressBar.setValue(self.calibration.get_progress()*100) except: self.progressBar.setValue(0) def updateToolheadPos(self): try: current_toolhead_pos = (self.machine.x, self.machine.y) self.toolheadPosLabel.setText('Current toolhead position: (%.2f, %.2f)' % current_toolhead_pos) except TypeError: self.toolheadPosLabel.setText('Current toolhead position: (?, ?)') def updateLabels(self): try: current_point = self.calibration.get_current_point_data() self.targetPointLabel.setText('Target point: %s' % current_point['name']) self.targetImageLabel.setText('Target image coordinates: (%.2f, %.2f)'%current_point['coordinates']) except (AttributeError, Calibration.CalibrationException) as e: self.targetPointLabel.setText('Target point: "Point"') self.targetImageLabel.setText('Target image coordinates: (u, v)') self.updateToolheadPos() def loadImage(self): if self.calibration.image_name: self.pixmap.loadFromData(self.calibration.image, os.path.splitext(self.calibration.image_name)[1]) self.original_rect = self.pixmap.rect() self.pixmap = self.pixmap.scaled(480, 339) return True else: return False def updateImage(self): # Load image and create scene scene = QtGui.QGraphicsScene() scene.addItem(QtGui.QGraphicsPixmapItem(self.pixmap)) # Draw markers if self.calibration and self.calibration.calibrating: current_point = self.calibration.get_current_point_data()['coordinates'] scale_x = 480 / float(self.original_rect.width()) scale_y = 339 / float(self.original_rect.height()) try: pen = QtGui.QPen(QtGui.QColor(0, 255, 0)) pen.setWidth(3) scene.addEllipse(int(current_point[0]*scale_x-15), int(current_point[1]*scale_y-15), 30, 30, \ pen=pen) except Exception, e: print str(e) self.graphicsView.setScene(scene) self.graphicsView.show()
import sys libPathList = ['./lib/Python', './util'] for libPath in libPathList: sys.path.insert(0, libPath) from SetMeUp import SetMeUp from Calibration import Calibration setupfile = 'setup.yaml' calibrationfile = 'calib_params.tbl' # create the setup instance setup = SetMeUp(setupfile) calib = Calibration(setupfile) calib.AdjustCalibTable() calib.df.to_csv("updated_calib_params.tbl")
class Data: """Class providing an interface to the raw data NetCDF files.""" def __init__(self): """Constructor.""" self.filename = None # The filename the data was loaded from. # Laser fire times come from the laser diode data in mdsplus. self.laserFireTimes = None self.shotNum = None self.data = {} self.polyList = None self.segments = None self.prelasing = None def loadData(self, shotNum, skipAPD = 1): """Load all the data for the given shot. Parameters: shotNum -- The shot number to load. skipAPD -- Set to 1 to skip the first APD (because of saturation). """ from numpy import arange self.shotNum = shotNum self.data = {} # Clear old data. self.filename = util.getDataFilePath(shotNum) nc = pycdf.CDF(self.filename) #print "Loading Raw Data\n" str_rawData = nc.var('str_rawdata').get() str_voltsPerBit = nc.var('str_voltsperbit').get() str_deltat = nc.var('str_deltat').get() str_zeroBit = nc.var('str_offset').get() acq_rawData = nc.var('rawdata').get() acq_voltsPerBit = nc.var('voltsperbit').get() acq_deltat = nc.var('deltat').get() acq_offset = nc.var('offset').get() roa = nc.var('roa').get() # take the poly list from the netCDF file (one entry for each channel) and unique-ify it # to have only one entry per poly plist_long = nc.var('poly').get() seen = {} for item in plist_long: seen[item] = 1 plist_short = seen.keys() plist_short.sort() # probably unnecessary, but just in case self.polyList = plist_short # load the calibration data after the file data, so that the poly list can be loaded for this shot self.calib = Calibration() self.calib.loadCalibration(shotNum, plist_short, skipAPD) # The number of data segments is one less than the total number of # segments. The last segment in the data file is used for calculating # the amplifier offsets. self.segments = arange(str_rawData.shape[1] - 1) apdIndex = 0 for poly in self.polyList: self.data[poly] = {} calib = self.calib.getPolyCalibration(poly) for segment in self.segments: numAPD = calib.numAPD ps = PolySegData() ps.calib = calib ps.poly = poly ps.segment = segment ps.shotNum=self.shotNum # If calib.skipAPD is 1, then we don't load the data for the # first APD. start = apdIndex + calib.skipAPD end = apdIndex + numAPD ps.str_voltsPerBit = str_voltsPerBit[start:end] ps.str_deltat = str_deltat[start:end] ps.str_zeroBit = str_zeroBit[start:end] ps.acq_voltsPerBit = acq_voltsPerBit[start:end] ps.acq_deltat = acq_deltat[start:end] ps.acq_offset = acq_offset[start:end] ps.roa = roa[start] # correction because p20 and p21 were flipped in the calib file # for Fall 2012 only, radial calib redone in December #if poly == 20: # ps.roa = 0.6030 #elif poly == 21: # ps.roa = 0.5181 ps.str_rawData = str_rawData[start:end, segment] ps.acq_rawData = acq_rawData[start:end, segment] # It's a bit inefficient to store the offset data with each # PolySegData object, but it is simple. ps.str_offsetRaw = str_rawData[start:end, len(self.segments)] ps.acq_offsetRaw = acq_rawData[start:end, len(self.segments)] # Set the usability flags for the DC and AC channels # The AC channels are set to be unused until AC calib is figured out ps.chanFlagDC = ones(numAPD - calib.skipAPD) #ps.chanFlagAC = ones(numAPD - calib.skipAPD) ps.chanFlagAC = zeros(numAPD - calib.skipAPD) self.data[poly][segment] = ps apdIndex += numAPD def getSegments(self): """Return a list of available segment numbers.""" return self.segments def getPolyList(self): """Return the list of polys to fit.""" return self.polyList def getPolyListSortedROA(self): """Return the list of polys in order of increasing ROA.""" def sortFn(p1, p2): ps1 = self.getPolySegData(p1, 0) ps2 = self.getPolySegData(p2, 0) if ps1.roa == ps2.roa: return 0 elif ps1.roa > ps2.roa: return 1 else: return -1 tmpList = list(self.getPolyList()) tmpList.sort(sortFn) return tmpList def getPolySegData(self, poly, segment): """Get the data for a given poly and segment combination. Parameters: poly -- The poly number. segment -- The segment number. Returns: The data for the given poly and segment in the form of a PolySegData object. """ return self.data[poly][segment] def setPolySegData(self, poly, segment, ps): """Set the data for a given poly and segment to the given PolySegData object. Parameters: poly -- The poly number. segment -- The segment number. ps -- The PolySegData object. Returns: Nothing. """ self.data[poly][segment] = ps def getReadme(self, bCompact=True): """Return a string to save to the readme variable in mdsplus.""" ret = '' for segment in self.getSegments(): for poly in self.getPolyList(): ps = self.getPolySegData(poly, segment) st = None if bCompact: st = ps.getCompactErrWarnStr() else: st = ps.getErrWarnStr() if st != None: ret += '%s\n' % st return ret
class test_Calibration: def __init__(self): self.cal = None def setup(self): self.cal = Calibration() def test_total_power(self): array1 = np.ones(10) array2 = np.ones(10) * 2 result = self.cal.total_power(array1, array2) expected_result = np.ones(10) * 1.5 np.testing.assert_equal(result, expected_result) def test_tsky(self): ambient_temp_k = 310.1 freq_hz = 18.458 tau = .05 tsky = self.cal.tsky(ambient_temp_k, freq_hz, tau) expected_result = 13.432242475061472 ntest.assert_almost_equal(tsky, expected_result) def test_tsky_correction(self): array_size = 128 tsky_sig = np.ones(array_size) * 2 tsky_ref = np.ones(array_size) spillover = .123 tsky_correction = self.cal.tsky_correction(tsky_sig, tsky_ref, spillover) expected_result = np.ones(array_size) * .123 np.testing.assert_equal(tsky_correction, expected_result) def test_aperture_efficiency(self): reference_eta_a = .71 freq_hz = 23e9 efficiency = self.cal.aperture_efficiency(reference_eta_a, freq_hz) expected_result = 0.64748265789117276 ntest.assert_almost_equal(efficiency, expected_result) def test_main_beam_efficiency(self): reference_eta_a = .7 freq_hz = 23.7e9 efficiency = self.cal.main_beam_efficiency(reference_eta_a, freq_hz) expected_result = 0.6347374630868166 ntest.assert_almost_equal(efficiency, expected_result) def test_elevation_adjusted_opacity(self): zenith_opacity = .1 elevation = 45. adjusted_opacity = self.cal.elevation_adjusted_opacity(.1, 45.) expected_result = 0.07071067811865475 ntest.assert_almost_equal(adjusted_opacity, expected_result) def test__tatm(self): freq_hz = 23e9 temp_c = 40. atmospheric_effective_temp = self.cal._tatm(freq_hz, temp_c) expected_result = 298.88517422006998 ntest.assert_almost_equal(atmospheric_effective_temp, expected_result) def test_zenith_opacity(self): opacity_coefficients = [2, 1, 0] freq_ghz = 16.79 zenith_opacity = self.cal.zenith_opacity(opacity_coefficients, freq_ghz) expected_result = 18.79 ntest.assert_equal(zenith_opacity, expected_result) def test_tsys(self): tcal = 1. cal_off = np.ones(128) cal_on = np.ones(128)*3 tsys = self.cal.tsys(tcal, cal_on, cal_off) expected = 1. ntest.assert_equal(tsys, expected) def test_antenna_temp(self): tsys = .5 sig = np.ones(128) * 2 ref = np.ones(128) antenna_temp = self.cal.antenna_temp(tsys, sig, ref) expected_result = np.ones(128) * .5 np.testing.assert_equal(antenna_temp, expected_result) def test__ta_fs_one_state(self): sigref_state = [{'cal_on': None, 'cal_off': None, 'TP': None}, {'cal_on': None, 'cal_off': None, 'TP': None}] sigref_state[0]['cal_on'] = np.ones(128) sigref_state[0]['cal_off'] = np.ones(128) sigref_state[1]['cal_on'] = np.ones(128) sigref_state[1]['cal_off'] = np.ones(128) sigid = 0 refid = 1 assert False def test_ta_fs(self): assert False def test_ta_star(self): antenna_temp = np.ones(128) beam_scaling = 1. opacity = 0 spillover = 2. ta_star = self.cal.ta_star(antenna_temp, beam_scaling, opacity, spillover) expected = np.ones(128) * .5 np.testing.assert_equal(ta_star, expected) def test_jansky(self): ta_star = np.ones(128) aperture_efficiency = .1 jansky = self.cal.jansky(ta_star, aperture_efficiency) expected = np.ones(128) / .285 np.testing.assert_almost_equal(jansky, expected) def test_interpolate_by_time(self): reference1 = 0. reference1_time = 10000. reference2 = 100. reference2_time = 20000. result_time = 15000. result_value = self.cal.interpolate_by_time(reference1, reference2, reference1_time, reference2_time, result_time) expected = 50. ntest.assert_equal(result_value, expected)
class Weather: def __init__(self): self.cal = Calibration() self.last_integration_mjd_timestamp = None self.last_requested_freq_ghz = None self.zenith_opacity = None self.opacity_coeffs = None self.L_BAND_ZENITH_TAU = .008 self.frequency_range = None self.time_range = None self.db_time_range = None self.log = None def _opacity_database(self, timestamp): # retrieve a list of opacity coefficients files, based on a given directory # and filename structure opacity_coefficients_filename = False opacity_files = glob.glob('/home/gbtpipeline/weather/CoeffsOpacityFreqList_avrg_*.txt') if 0 == len(opacity_files): return False, False # sort the list of files so they are in chronological order opacity_files.sort() # check the date of each opacity coefficients file for idx, opacity_candidate_file in enumerate(opacity_files): dates = opacity_candidate_file.split('_')[-2:] opacity_file_timestamp = [] for date in dates: opacity_file_timestamp.append(int(date.split('.')[0])) opacity_file_starttime = opacity_file_timestamp[0] opacity_file_stoptime = opacity_file_timestamp[1] # when timestamp is older than available ranges if idx == 0 and timestamp < opacity_file_starttime: if self.log: self.log.doMessage('ERR', 'ERROR: Date is too early for opacities.') self.log.doMessage('ERR', ' Try setting zenith tau at command line.') self.log.doMessage('ERR', timestamp, '<', opacity_file_starttime) else: print 'ERROR: Date is too early for opacities.' print ' Try setting zenith tau at command line.' print timestamp, '<', opacity_file_starttime sys.exit(9) break if (opacity_file_starttime <= timestamp < opacity_file_stoptime): opacity_coefficients_filename = opacity_candidate_file opacity_db_range = (opacity_file_starttime, opacity_file_stoptime) break # uses most recent info if not opacity_coefficients_filename: # if the mjd in the index file comes after the date string in all of the # opacity coefficients files, then we can assume the current opacity # coefficients file will apply. a date string is only added to the opacity # coefficients file when it is no longer the most current. opacity_coefficients_filename = '/home/gbtpipeline/weather/CoeffsOpacityFreqList_avrg.txt' opacity_db_range = 'LATEST' # opacities coefficients filename if opacity_coefficients_filename and os.path.exists(opacity_coefficients_filename): if self.log: self.log.doMessage('DBG', 'Using coefficients from', opacity_coefficients_filename) else: print 'Using coefficients from', opacity_coefficients_filename coeffs = self._retrieve_opacity_coefficients(opacity_coefficients_filename) return coeffs, opacity_db_range else: if self.log: self.log.doMessage('ERR', 'No opacity coefficients file') else: print 'ERROR: No opacity coefficients file' return False, False def _retrieve_opacity_coefficients(self, opacity_coefficients_filename): """Return opacities (taus) derived from a list of coeffients These coefficients are produced from Ron Madalenna's getForecastValues script Keywords: infilename -- input file name needed for project name mjd -- date for data freq -- list of frequencies for which we seek an opacity Returns: a list of opacity coefficients for the time range of the dataset """ opacity_file = open(opacity_coefficients_filename, 'r') coeffs = [] if opacity_file: for line in opacity_file: # find the most recent forecast and parse out the coefficients for # each band # coeffs[0] is the mjd timestamp # coeffs[1] are the coefficients for 2-22 GHz # coeffs[2] are the coefficients for 22-50 GHz # coeffs[3] are the coefficients for 70-116 GHz coeffs.append((float(line.split('{{')[0]), [float(xx) for xx in line.split('{{')[1].split('}')[0].split(' ')], [float(xx) for xx in line.split('{{')[2].split('}')[0].split(' ')], [float(xx) for xx in line.split('{{')[3].split('}')[0].split(' ')])) else: print "WARNING: Could not read coefficients for Tau in", opacity_coefficients_filename return False return coeffs def retrieve_zenith_opacity(self, integration_mjd_timestamp, freq_hz, log=None): self.log = log freq_ghz = freq_hz/1e9 # if less than 2 GHz, opacity coefficients are not available if freq_ghz < 2: return self.L_BAND_ZENITH_TAU # if the frequency is in the same range and # the time is within the same record (1 hr window) of the last # recorded opacity coefficients, then just reuse the last # zenith opacity value requested if ((self.frequency_range and (self.frequency_range[0] <= freq_ghz <= self.frequency_range[1])) and (self.time_range and (self.time_range[0] <= integration_mjd_timestamp <= self.time_range[1]))): return self.zenith_opacity self.last_integration_mjd_timestamp = integration_mjd_timestamp self.last_requested_freq_ghz = freq_ghz # if we don't have a db time range OR # we do have a time range AND # the range is 'LATEST' AND # timestamp is not w/in the same hour as the last set of coeffs, OR # the range has values AND # our new timestamp is not in the range # THEN # get another set of coefficients from a different file if ((not self.db_time_range) or (self.db_time_range == 'LATEST' and not (self.time_range[0] <= integration_mjd_timestamp < self.time_range[1])) or (self.db_time_range and not (self.db_time_range[0] <= integration_mjd_timestamp < self.db_time_range[1]))): log.doMessage('DBG', '-----------------------------------------------------') log.doMessage('DBG', 'Time or Frequency out of range. Determine new opacity') log.doMessage('DBG', '-----------------------------------------------------') log.doMessage('DBG', 'opacity', self.zenith_opacity) log.doMessage('DBG', 'timestamp', integration_mjd_timestamp, 'prev.', self.last_integration_mjd_timestamp) log.doMessage('DBG', 'freq', freq_ghz, 'prev.', self.last_requested_freq_ghz) log.doMessage('DBG', 'freq range', self.frequency_range) if bool(self.frequency_range): log.doMessage('DBG', ' freq in range == ', bool(self.frequency_range[0] <= freq_ghz <= self.frequency_range[1])) log.doMessage('DBG', 'time range', self.time_range) if bool(self.time_range): log.doMessage('DBG', ' time in range ==', bool(self.time_range and (self.time_range[0] <= integration_mjd_timestamp <= self.time_range[1]))) log.doMessage('DBG', 'DB time range', self.db_time_range) if bool(self.db_time_range): log.doMessage('DBG', ' time in DB range ==', bool(self.db_time_range and (self.db_time_range[0] <= integration_mjd_timestamp <= self.db_time_range[1]))) self.opacity_coeffs, self.db_time_range = self._opacity_database(integration_mjd_timestamp) if (not self.opacity_coeffs) or (not self.db_time_range): return False log.doMessage('DBG', 'DB time range:', self.db_time_range) for coeffs_line in self.opacity_coeffs: if (2 <= freq_ghz <= 22): self.frequency_range = (2, 22) coefficients_index = 1 elif (22 < freq_ghz <= 50): self.frequency_range = (22, 50) coefficients_index = 2 elif (50 < freq_ghz <= 116): self.frequency_range = (50, 116) coefficients_index = 3 if integration_mjd_timestamp >= coeffs_line[0]: prev_time = coeffs_line[0] prev_coeffs = coeffs_line[coefficients_index] elif integration_mjd_timestamp < coeffs_line[0]: next_time = coeffs_line[0] next_coeffs = coeffs_line[coefficients_index] break self.time_range = (prev_time, next_time) log.doMessage('DBG', 'Coefficient entry time range:', self.time_range) time_corrected_coeffs = [] for coeff in zip(prev_coeffs, next_coeffs): new_coeff = self.cal.interpolate_by_time(coeff[0], coeff[1], prev_time, next_time, integration_mjd_timestamp) time_corrected_coeffs.append(new_coeff) self.zenith_opacity = self.cal.zenith_opacity(time_corrected_coeffs, freq_ghz) log.doMessage('DBG', 'Zenith opacity:', self.zenith_opacity) return self.zenith_opacity
class Weather: def __init__(self): self.cal = Calibration() self.last_integration_mjd_timestamp = None self.last_requested_freq_hz = None self.last_zenith_opacity = None self.number_of_opacity_files = None self.opacity_coeffs = None self.L_BAND_ZENITH_TAU = .008 def _retrieve_opacity_coefficients(self, opacity_coefficients_filename): """Return opacities (taus) derived from a list of coeffients These coefficients are produced from Ron Madalenna's getForecastValues script Keywords: infilename -- input file name needed for project name mjd -- date for data freq -- list of frequencies for which we seek an opacity Returns: a list of opacity coefficients for the time range of the dataset """ opacity_file = open(opacity_coefficients_filename, 'r') coeffs = [] if opacity_file: for line in opacity_file: # find the most recent forecast and parse out the coefficients for # each band # coeffs[0] is the mjd timestamp # coeffs[1] are the coefficients for 2-22 GHz # coeffs[2] are the coefficients for 22-50 GHz # coeffs[3] are the coefficients for 70-116 GHz coeffs.append((float(line.split('{{')[0]), \ [float(xx) for xx in line.split('{{')[1].split('}')[0].split(' ')], \ [float(xx) for xx in line.split('{{')[2].split('}')[0].split(' ')], \ [float(xx) for xx in line.split('{{')[3].split('}')[0].split(' ')])) else: print "WARNING: Could not read coefficients for Tau in", opacity_coefficients_filename return False return coeffs def retrieve_zenith_opacity(self, integration_mjd_timestamp, freq_hz): freq_ghz = freq_hz/1e9 # if less than 2 GHz, opacity coefficients are not available if freq_ghz < 2: return self.L_BAND_ZENITH_TAU # if the frequency is the same as the last requested and # this time is within the same record (1 hr window) of the last # recorded opacity coefficients, then just reuse the last # zenith opacity value requested if self.last_requested_freq_hz and self.last_requested_freq_hz == freq_hz and \ integration_mjd_timestamp >= self.last_integration_mjd_timestamp and \ integration_mjd_timestamp < self.last_integration_mjd_timestamp + .04167: return self.last_zenith_opacity self.last_integration_mjd_timestamp = integration_mjd_timestamp self.last_requested_freq_hz = freq_hz # retrieve a list of opacity coefficients files, based on a given directory # and filename structure opacity_coefficients_filename = False opacity_files = glob.glob(\ '/users/rmaddale/Weather/ArchiveCoeffs/CoeffsOpacityFreqList_avrg_*.txt') self.number_of_opacity_files = len(opacity_files) if 0 == self.number_of_opacity_files: #doMessage(logger, msg.WARN, 'WARNING: No opacity coefficients file') print 'WARNING: No opacity coefficients file' return False # sort the list of files so they are in chronological order opacity_files.sort() # the following will become True if integration_mjd_timestamp is older than available ranges # provided in the opacity coefficients files tooearly = False # check the date of each opacity coefficients file for idx, opacity_candidate_file in enumerate(opacity_files): dates = opacity_candidate_file.split('_')[-2:] opacity_file_timestamp = [] for date in dates: opacity_file_timestamp.append(int(date.split('.')[0])) opacity_file_starttime = opacity_file_timestamp[0] opacity_file_stoptime = opacity_file_timestamp[1] # set tooearly = True when integration_mjd_timestamp is older than available ranges if idx == 0 and integration_mjd_timestamp < opacity_file_starttime: tooearly = True break if integration_mjd_timestamp >= opacity_file_starttime \ and integration_mjd_timestamp < opacity_file_stoptime: opacity_coefficients_filename = opacity_candidate_file break if not opacity_coefficients_filename: if tooearly: #doMessage(logger, msg.ERR, 'ERROR: Date is too early for opacities.') #doMessage(logger, msg.ERR, ' Try setting zenith tau at command line.') sys.exit(9) else: # if the mjd in the index file comes after the date string in all of the # opacity coefficients files, then we can assume the current opacity # coefficients file will apply. a date string is only added to the opacity # coefficients file when it is no longer the most current. opacity_coefficients_filename = \ '/users/rmaddale/Weather/ArchiveCoeffs/CoeffsOpacityFreqList_avrg.txt' # opacities coefficients filename if opacity_coefficients_filename and os.path.exists(opacity_coefficients_filename): #doMessage(logger, msg.DBG, 'Using coefficients from', opacity_coefficients_filename) self.opacity_coeffs = self._retrieve_opacity_coefficients(opacity_coefficients_filename) else: #doMessage(logger, msg.WARN, 'WARNING: No opacity coefficients file') print 'WARNING: No opacity coefficients file' return False for coeffs_line in self.opacity_coeffs: if integration_mjd_timestamp >= coeffs_line[0]: prev_time = coeffs_line[0] if (freq_ghz >= 2 and freq_ghz <= 22): prev_coeffs = coeffs_line[1] elif (freq_ghz > 22 and freq_ghz <= 50): prev_coeffs = coeffs_line[2] elif (freq_ghz > 50 and freq_ghz <= 116): prev_coeffs = coeffs_line[3] elif integration_mjd_timestamp < coeffs_line[0]: next_time = coeffs_line[0] if (freq_ghz >= 2 and freq_ghz <= 22): next_coeffs = coeffs_line[1] elif (freq_ghz > 22 and freq_ghz <= 50): next_coeffs = coeffs_line[2] elif (freq_ghz > 50 and freq_ghz <= 116): next_coeffs = coeffs_line[3] break time_corrected_coeffs = [] for coeff in zip(prev_coeffs, next_coeffs): new_coeff = self.cal.interpolate_by_time(coeff[0], coeff[1], prev_time, next_time, integration_mjd_timestamp) time_corrected_coeffs.append(new_coeff) zenith_opacity = self.cal.zenith_opacity(time_corrected_coeffs, freq_ghz) self.last_zenith_opacity = zenith_opacity return zenith_opacity
if args.overwrite == True: shutil.rmtree(setup.parent_directory, ignore_errors=True) else: logger.error( "directory already exists. exiting. use overwrite flag to delete") sys.exit() # do checks ... if not RunPreCheck(setupfile).run_all(): sys.exit() if not RunCalibCheck(setupfile).run_all(): sys.exit() # get the current directory cwd = os.getcwd() # Calibrate calib = Calibration(setupfile) calib.PrepareCalibration() calib.AdjustCalibTable() #logger.info(calib.df) calib() logger.info(calib.failed_iterations) # make sure we are in the parent directory... os.chdir(cwd) # Validate #--------- valid = Validation(setupfile) valid.PrepareValidation() valid.run_validation()
def __init__(self): super().__init__() self.threadpool = QtCore.QThreadPool() print("Max thread count " + str(self.threadpool.maxThreadCount())) self.worker = SweepWorker(self) self.noSweeps = 1 # Number of sweeps to run self.serialLock = threading.Lock() self.serial = serial.Serial() self.dataLock = threading.Lock() self.data : List[Datapoint] = [] self.data21 : List[Datapoint] = [] self.referenceS11data : List[Datapoint] = [] self.referenceS21data : List[Datapoint] = [] self.calibration = Calibration() self.markers = [] self.serialPort = self.getport() # self.serialSpeed = "115200" self.color = QtGui.QColor(160, 140, 20, 128) self.referenceColor = QtGui.QColor(0, 0, 255, 32) self.setWindowTitle("NanoVNA Saver") layout = QtWidgets.QGridLayout() scrollarea = QtWidgets.QScrollArea() outer = QtWidgets.QVBoxLayout() outer.addWidget(scrollarea) self.setLayout(outer) scrollarea.setWidgetResizable(True) self.resize(1150, 950) scrollarea.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding) self.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding) widget = QtWidgets.QWidget() widget.setLayout(layout) scrollarea.setWidget(widget) self.s11SmithChart = SmithChart("S11") self.s21SmithChart = SmithChart("S21") self.s11LogMag = LogMagChart("S11 Return Loss") self.s21LogMag = LogMagChart("S21 Gain") self.charts : List[Chart] = [] self.charts.append(self.s11SmithChart) self.charts.append(self.s21SmithChart) self.charts.append(self.s11LogMag) self.charts.append(self.s21LogMag) left_column = QtWidgets.QVBoxLayout() right_column = QtWidgets.QVBoxLayout() layout.addLayout(left_column, 0, 0) layout.addLayout(right_column, 0, 1) ################################################################################################################ # Sweep control ################################################################################################################ sweep_control_box = QtWidgets.QGroupBox() sweep_control_box.setMaximumWidth(400) sweep_control_box.setTitle("Sweep control") sweep_control_layout = QtWidgets.QFormLayout(sweep_control_box) self.sweepStartInput = QtWidgets.QLineEdit("") self.sweepStartInput.setAlignment(QtCore.Qt.AlignRight) sweep_control_layout.addRow(QtWidgets.QLabel("Sweep start"), self.sweepStartInput) self.sweepEndInput = QtWidgets.QLineEdit("") self.sweepEndInput.setAlignment(QtCore.Qt.AlignRight) sweep_control_layout.addRow(QtWidgets.QLabel("Sweep end"), self.sweepEndInput) self.sweepCountInput = QtWidgets.QLineEdit("") self.sweepCountInput.setAlignment(QtCore.Qt.AlignRight) self.sweepCountInput.setText("1") sweep_control_layout.addRow(QtWidgets.QLabel("Sweep count"), self.sweepCountInput) self.btnColorPicker = QtWidgets.QPushButton("█") self.btnColorPicker.setFixedWidth(20) self.setSweepColor(self.color) self.btnColorPicker.clicked.connect(lambda: self.setSweepColor(QtWidgets.QColorDialog.getColor(self.color, options=QtWidgets.QColorDialog.ShowAlphaChannel))) sweep_control_layout.addRow("Sweep color", self.btnColorPicker) self.sweepProgressBar = QtWidgets.QProgressBar() self.sweepProgressBar.setMaximum(100) self.sweepProgressBar.setValue(0) sweep_control_layout.addRow(self.sweepProgressBar) self.btnSweep = QtWidgets.QPushButton("Sweep") self.btnSweep.clicked.connect(self.sweep) sweep_control_layout.addRow(self.btnSweep) left_column.addWidget(sweep_control_box) ################################################################################################################ # Marker control ################################################################################################################ marker_control_box = QtWidgets.QGroupBox() marker_control_box.setTitle("Markers") marker_control_box.setMaximumWidth(400) marker_control_layout = QtWidgets.QFormLayout(marker_control_box) mouse_marker = Marker("Mouse marker", QtGui.QColor(20, 255, 20)) mouse_marker.updated.connect(self.dataUpdated) self.markers.append(mouse_marker) marker1 = Marker("Marker 1", QtGui.QColor(255, 0, 20)) marker1.updated.connect(self.dataUpdated) label, layout = marker1.getRow() marker_control_layout.addRow(label, layout) self.markers.append(marker1) marker2 = Marker("Marker 2", QtGui.QColor(20, 0, 255)) marker2.updated.connect(self.dataUpdated) label, layout = marker2.getRow() marker_control_layout.addRow(label, layout) self.markers.append(marker2) self.s11SmithChart.setMarkers(self.markers) self.s21SmithChart.setMarkers(self.markers) self.mousemarkerlabel = QtWidgets.QLabel("") self.mousemarkerlabel.setMinimumWidth(160) marker_control_layout.addRow(QtWidgets.QLabel("Mouse marker:"), self.mousemarkerlabel) self.marker1label = QtWidgets.QLabel("") marker_control_layout.addRow(QtWidgets.QLabel("Marker 1:"), self.marker1label) self.marker2label = QtWidgets.QLabel("") marker_control_layout.addRow(QtWidgets.QLabel("Marker 2:"), self.marker2label) left_column.addWidget(marker_control_box) ################################################################################################################ # Statistics/analysis ################################################################################################################ s11_control_box = QtWidgets.QGroupBox() s11_control_box.setTitle("S11") s11_control_layout = QtWidgets.QFormLayout() s11_control_box.setLayout(s11_control_layout) s11_control_box.setMaximumWidth(400) self.s11_min_swr_label = QtWidgets.QLabel() s11_control_layout.addRow("Min VSWR:", self.s11_min_swr_label) self.s11_min_rl_label = QtWidgets.QLabel() s11_control_layout.addRow("Return loss:", self.s11_min_rl_label) left_column.addWidget(s11_control_box) s21_control_box = QtWidgets.QGroupBox() s21_control_box.setTitle("S21") s21_control_layout = QtWidgets.QFormLayout() s21_control_box.setLayout(s21_control_layout) s21_control_box.setMaximumWidth(400) self.s21_min_gain_label = QtWidgets.QLabel() s21_control_layout.addRow("Min gain:", self.s21_min_gain_label) self.s21_max_gain_label = QtWidgets.QLabel() s21_control_layout.addRow("Max gain:", self.s21_max_gain_label) left_column.addWidget(s21_control_box) tdr_control_box = QtWidgets.QGroupBox() tdr_control_box.setTitle("TDR") tdr_control_layout = QtWidgets.QFormLayout() tdr_control_box.setLayout(tdr_control_layout) tdr_control_box.setMaximumWidth(400) self.tdr_velocity_dropdown = QtWidgets.QComboBox() self.tdr_velocity_dropdown.addItem("Jelly filled (0.64)", 0.64) self.tdr_velocity_dropdown.addItem("Polyethylene (0.66)", 0.66) self.tdr_velocity_dropdown.addItem("PTFE (Teflon) (0.70)", 0.70) self.tdr_velocity_dropdown.addItem("Pulp Insulation (0.72)", 0.72) self.tdr_velocity_dropdown.addItem("Foam or Cellular PE (0.78)", 0.78) self.tdr_velocity_dropdown.addItem("Semi-solid PE (SSPE) (0.84)", 0.84) self.tdr_velocity_dropdown.addItem("Air (Helical spacers) (0.94)", 0.94) self.tdr_velocity_dropdown.insertSeparator(7) self.tdr_velocity_dropdown.addItem("RG174 (0.66)", 0.66) self.tdr_velocity_dropdown.addItem("RG316 (0.69)", 0.69) self.tdr_velocity_dropdown.addItem("RG402 (0.695)", 0.695) self.tdr_velocity_dropdown.insertSeparator(11) self.tdr_velocity_dropdown.addItem("Custom", -1) self.tdr_velocity_dropdown.setCurrentIndex(1) # Default to PE (0.66) self.tdr_velocity_dropdown.currentIndexChanged.connect(self.updateTDR) tdr_control_layout.addRow(self.tdr_velocity_dropdown) self.tdr_velocity_input = QtWidgets.QLineEdit() self.tdr_velocity_input.setDisabled(True) self.tdr_velocity_input.setText("0.66") self.tdr_velocity_input.textChanged.connect(self.updateTDR) tdr_control_layout.addRow("Velocity factor", self.tdr_velocity_input) self.tdr_result_label = QtWidgets.QLabel() tdr_control_layout.addRow("Estimated cable length:", self.tdr_result_label) left_column.addWidget(tdr_control_box) ################################################################################################################ # Calibration ################################################################################################################ calibration_control_box = QtWidgets.QGroupBox("Calibration") calibration_control_box.setMaximumWidth(400) calibration_control_layout = QtWidgets.QFormLayout(calibration_control_box) b = QtWidgets.QPushButton("Calibration ...") self.calibrationWindow = CalibrationWindow(self) b.clicked.connect(self.calibrationWindow.show) calibration_control_layout.addRow(b) left_column.addWidget(calibration_control_box) ################################################################################################################ # Spacer ################################################################################################################ left_column.addSpacerItem(QtWidgets.QSpacerItem(1, 1, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Expanding)) ################################################################################################################ # Reference control ################################################################################################################ reference_control_box = QtWidgets.QGroupBox() reference_control_box.setMaximumWidth(400) reference_control_box.setTitle("Reference sweep") reference_control_layout = QtWidgets.QFormLayout(reference_control_box) btnSetReference = QtWidgets.QPushButton("Set current as reference") btnSetReference.clicked.connect(self.setReference) self.btnResetReference = QtWidgets.QPushButton("Reset reference") self.btnResetReference.clicked.connect(self.resetReference) self.btnResetReference.setDisabled(True) self.btnReferenceColorPicker = QtWidgets.QPushButton("█") self.btnReferenceColorPicker.setFixedWidth(20) self.setReferenceColor(self.referenceColor) self.btnReferenceColorPicker.clicked.connect(lambda: self.setReferenceColor( QtWidgets.QColorDialog.getColor(self.referenceColor, options=QtWidgets.QColorDialog.ShowAlphaChannel))) set_reference_layout = QtWidgets.QHBoxLayout() set_reference_layout.addWidget(btnSetReference) set_reference_layout.addWidget(self.btnReferenceColorPicker) reference_control_layout.addRow(set_reference_layout) reference_control_layout.addRow(self.btnResetReference) left_column.addWidget(reference_control_box) ################################################################################################################ # Serial control ################################################################################################################ serial_control_box = QtWidgets.QGroupBox() serial_control_box.setMaximumWidth(400) serial_control_box.setTitle("Serial port control") serial_control_layout = QtWidgets.QFormLayout(serial_control_box) self.serialPortInput = QtWidgets.QLineEdit(self.serialPort) self.serialPortInput.setAlignment(QtCore.Qt.AlignRight) # self.serialSpeedInput = QtWidgets.QLineEdit(str(self.serialSpeed)) # self.serialSpeedInput.setValidator(QtGui.QIntValidator()) # self.serialSpeedInput.setAlignment(QtCore.Qt.AlignRight) serial_control_layout.addRow(QtWidgets.QLabel("Serial port"), self.serialPortInput) # serial_control_layout.addRow(QtWidgets.QLabel("Speed"), self.serialSpeedInput) self.btnSerialToggle = QtWidgets.QPushButton("Open serial") self.btnSerialToggle.clicked.connect(self.serialButtonClick) serial_control_layout.addRow(self.btnSerialToggle) left_column.addWidget(serial_control_box) ################################################################################################################ # File control ################################################################################################################ self.fileWindow = QtWidgets.QWidget() self.fileWindow.setWindowTitle("Files") file_window_layout = QtWidgets.QVBoxLayout() self.fileWindow.setLayout(file_window_layout) reference_file_control_box = QtWidgets.QGroupBox("Import file") reference_file_control_layout = QtWidgets.QFormLayout(reference_file_control_box) self.referenceFileNameInput = QtWidgets.QLineEdit("") btnReferenceFilePicker = QtWidgets.QPushButton("...") btnReferenceFilePicker.setMaximumWidth(25) btnReferenceFilePicker.clicked.connect(self.pickReferenceFile) referenceFileNameLayout = QtWidgets.QHBoxLayout() referenceFileNameLayout.addWidget(self.referenceFileNameInput) referenceFileNameLayout.addWidget(btnReferenceFilePicker) reference_file_control_layout.addRow(QtWidgets.QLabel("Filename"), referenceFileNameLayout) file_window_layout.addWidget(reference_file_control_box) btnLoadReference = QtWidgets.QPushButton("Load reference") btnLoadReference.clicked.connect(self.loadReferenceFile) btnLoadSweep = QtWidgets.QPushButton("Load as sweep") btnLoadSweep.clicked.connect(self.loadSweepFile) reference_file_control_layout.addRow(btnLoadReference) reference_file_control_layout.addRow(btnLoadSweep) file_control_box = QtWidgets.QGroupBox() file_control_box.setTitle("Export file") file_control_box.setMaximumWidth(400) file_control_layout = QtWidgets.QFormLayout(file_control_box) self.fileNameInput = QtWidgets.QLineEdit("") btnFilePicker = QtWidgets.QPushButton("...") btnFilePicker.setMaximumWidth(25) btnFilePicker.clicked.connect(self.pickFile) fileNameLayout = QtWidgets.QHBoxLayout() fileNameLayout.addWidget(self.fileNameInput) fileNameLayout.addWidget(btnFilePicker) file_control_layout.addRow(QtWidgets.QLabel("Filename"), fileNameLayout) self.btnExportFile = QtWidgets.QPushButton("Export data S1P") self.btnExportFile.clicked.connect(self.exportFileS1P) file_control_layout.addRow(self.btnExportFile) self.btnExportFile = QtWidgets.QPushButton("Export data S2P") self.btnExportFile.clicked.connect(self.exportFileS2P) file_control_layout.addRow(self.btnExportFile) file_window_layout.addWidget(file_control_box) file_control_box = QtWidgets.QGroupBox() file_control_box.setTitle("Files") file_control_box.setMaximumWidth(400) file_control_layout = QtWidgets.QFormLayout(file_control_box) btnOpenFileWindow = QtWidgets.QPushButton("Files ...") file_control_layout.addWidget(btnOpenFileWindow) btnOpenFileWindow.clicked.connect(lambda: self.fileWindow.show()) left_column.addWidget(file_control_box) ################################################################################################################ # Right side ################################################################################################################ self.lister = QtWidgets.QPlainTextEdit() self.lister.setFixedHeight(80) charts = QtWidgets.QGridLayout() charts.addWidget(self.s11SmithChart, 0, 0) charts.addWidget(self.s21SmithChart, 1, 0) charts.addWidget(self.s11LogMag, 0, 1) charts.addWidget(self.s21LogMag, 1, 1) self.s11LogMag.setMarkers(self.markers) self.s21LogMag.setMarkers(self.markers) right_column.addLayout(charts) right_column.addWidget(self.lister) self.worker.signals.updated.connect(self.dataUpdated) self.worker.signals.finished.connect(self.sweepFinished)
def __init__(self, setup): # same as above ... super(self.__class__, self).__init__(setup) self.calib = Calibration(setup) # this is maybe a bad idea logger.info(self.calib.df)
class SeekThermal: __dev = None __rcvmsg = None __calib = Calibration() def __init__(self): self.__dev = usb.core.find(idVendor=0x289d, idProduct=0x0010) if None == self.__dev: raise ValueError('Device not found') # set the active configuration. With no arguments, the first configuration will be the active one self.__dev.set_configuration() # alias method to make code easier to read self.__rcvmsg = self.__dev.ctrl_transfer # get an endpoint instance cfg = self.__dev.get_active_configuration() intf = cfg[(0, 0)] custom_match = lambda e: usb.util.endpoint_direction( e.bEndpointAddress) == usb.util.ENDPOINT_OUT ep = usb.util.find_descriptor( intf, custom_match=custom_match) # match the first OUT endpoint assert ep is not None self.__setup() def __del__(self): for i in range(3): self.__send(0x41, 0x3C, 0, 0, '\x00\x00') def __send(self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None): assert (self.__dev.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, data_or_wLength, timeout) == len(data_or_wLength)) def __setup(self, ini=False): if True == ini: for i in range(3): self.__send(0x41, 0x3C, 0, 0, '\x00\x00') try: msg = '\x01' self.__send(0x41, 0x54, 0, 0, msg) except Exception as e: for i in range(3): self.__send(0x41, 0x3C, 0, 0, '\x00\x00') self.__send(0x41, 0x54, 0, 0, '\x01') # Some day we will figure out what all this init stuff is and # what the returned values mean. self.__send(0x41, 0x3C, 0, 0, '\x00\x00') ret = self.__rcvmsg(0xC1, 0x4E, 0, 0, 4) ret = self.__rcvmsg(0xC1, 0x36, 0, 0, 12) self.__send(0x41, 0x56, 0, 0, '\x20\x00\x30\x00\x00\x00') ret = self.__rcvmsg(0xC1, 0x58, 0, 0, 0x40) self.__send(0x41, 0x56, 0, 0, '\x20\x00\x50\x00\x00\x00') ret = self.__rcvmsg(0xC1, 0x58, 0, 0, 0x40) self.__send(0x41, 0x56, 0, 0, '\x0C\x00\x70\x00\x00\x00') ret = self.__rcvmsg(0xC1, 0x58, 0, 0, 0x18) self.__send(0x41, 0x56, 0, 0, '\x06\x00\x08\x00\x00\x00') ret = self.__rcvmsg(0xC1, 0x58, 0, 0, 0x0C) self.__send(0x41, 0x3E, 0, 0, '\x08\x00') ret = self.__rcvmsg(0xC1, 0x3D, 0, 0, 2) self.__send(0x41, 0x3E, 0, 0, '\x08\x00') self.__send(0x41, 0x3C, 0, 0, '\x01\x00') ret = self.__rcvmsg(0xC1, 0x3D, 0, 0, 2) def __getframe(self): while True: # Send read frame request self.__send(0x41, 0x53, 0, 0, '\xC0\x7E\x00\x00') try: dat = self.__dev.read(0x81, 0x3F60, 1000) dat += self.__dev.read(0x81, 0x3F60, 3000) dat += self.__dev.read(0x81, 0x3F60, 3000) dat += self.__dev.read(0x81, 0x3F60, 3000) except usb.USBError as e: # deinit self.__setup(True) return self.__getframe() tf = ThermalFrame(dat) # calibration ret = self.__calib.execute(tf) if False == ret: continue bmp = Image.new('RGB', (208, 156)) idx = 0 for hei in reversed(range(0, 156)): for wid in reversed(range(0, 208)): r = self.__calib.bmpdat[idx * 3] g = self.__calib.bmpdat[idx * 3 + 1] b = self.__calib.bmpdat[idx * 3 + 2] bmp.putpixel((wid, hei), (r, g, b)) idx += 1 return bmp def getInfo(self): frm = self.__getframe() maxt = float(self.__calib.max_val - 5950) / 40 # C mint = float(self.__calib.min_val - 5950) / 40 # C return {'image': frm, 'temperature': {'max': maxt, 'min': mint}}
###########################################RUN THIS ONLY ONCE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! """ Get camera calibration. Correct distortion of images on the camera_cal/calibration images This will get the correction factors for the camera lense and safe them to calibration.p by using Calibration.py """ from Calibration import Calibration import numpy as np import cv2 import matplotlib.pyplot as plt import matplotlib.image as mpimg #Runs all the chessboard images through Calibration.py a = range(1,21) for i in range(len(a)): nx = [6, 8, 9] ny = [5, 6, 7] fname = 'camera_cal/calibration' + str(a[i]+1) + '.jpg' print(fname) Calibration.image_calib(fname, nx, ny)