def local_start_message(): print_header(text=f'Rank {rank}', text_align='left') prompt(f"{'LOCAL DATA BLOCKS':<35}{sd.data_block_list_local}") prompt(f"{'LOCAL NUMBER OF DATA BLOCKS':<35}{sd.num_data_blocks_local}") obv_len = sd.observation_time_local prompt( f"{'OBSERVATION TIME':<35}{obv_len}sec = {conv_u(obv_len, 'time', 'sec', 'day'):.2f}d = {conv_u(obv_len, 'time', 'sec', 'year'):.2f}y" ) print_header()
def print_header(text=None, text_colour=None, length=81, text_align='center'): # default length = 81 if text == None: prompt(40 * colored('#*', 'green') + colored('#', 'green')) else: if text_align == 'left': prompt( colored('# ', 'green') + colored(f'{text:<78}', text_colour) + colored('#', 'green')) else: prompt( colored('# ', 'green') + colored(f'{text:^78}', text_colour) + colored('#', 'green'))
def local_start_message(): print_header(text=f'Rank {rank}', text_align='left') prompt(f"{'LOCAL DATA BLOCKS':<35}{sd.data_block_list_local}") prompt(f"{'LOCAL NUMBER OF DATA BLOCKS':<35}{sd.num_data_blocks_local}") print_header()
def global_start_message(): print_header() print_header(text='GENESYS') print_header(text='SIMULATING SATELLITE TOD') print_header() prompt(f"{'RUN TYPE':<35}{in_args.run_type}") prompt(f"{'# OF PROCESSES':<35}{size}") prompt(f"{'SIMULATION TAG':<35}{config['sim_tag']}") prompt(f"{'SPECIAL TAG':<35}{config['special_tag']}") prompt(f"{'NSIDE':<35}{config['nside']}") prompt(f"{'DETECTOR LIST':<35}{config['channel_detector_dict']}") prompt(f"{'NUMBER OF DATA BLOCKS':<35}{sd.num_data_blocks_global}") prompt( f"{'NUMBER OF SEGMENTS PER DATA BLOCK':<35}{config['num_segments_per_data_block']}" ) print_header()
def run_map_maker(): pointing_obj = Pointing() for channel_name in config['channel_detector_dict'].keys(): prompt(f"Rank {rank:^6} starting channel {channel_name}", nature='info') npix = hp.nside2npix(config['nside'][channel_name]) inv_cov_matrix_local, b_matrix_local, hitmap_local = cov_ut.get_empty_matrices( config['nside'][channel_name], config['pol_type']) for data_block in sd.data_block_list_local: prompt(f"Rank {rank:^6} \tstarting data block {data_block}", nature='info') io_obj.open_tod_file(channel_name, data_block, 'r') chan_com, chan_com_attr = io_obj.read_channel_common( ['fsamp', 'hwp_rpm', 'segl', 'pol_mod']) segment_list = sd.get_segment_list( data_block, config['num_segments_per_data_block']) for segment in segment_list: tod = {} seg_com, seg_com_attr = io_obj.read_segment_common( segment, ['time', 'psi_hwp']) if chan_com['pol_mod'] == 'passive': tod['psi_hwp'] = 0 else: tod['psi_hwp'] = seg_com[ 'psi_hwp'] + pointing_obj.get_hwp_phase( 0.0, chan_com['fsamp'], chan_com['segl'], chan_com['hwp_rpm']) for detector_name in config['channel_detector_dict'][ channel_name]: tod.update( io_obj.read_tod(segment, detector_name, ['signal', 'theta', 'phi', 'psi'])) hitpix = hp.ang2pix(config['nside'][channel_name], tod['theta'], tod['phi']) cov_ut.add_to_mm_matrices(hitpix, tod['psi'], tod['psi_hwp'], tod['signal'], inv_cov_matrix_local, b_matrix_local, hitmap_local, npix, config['pol_type']) io_obj.close_tod_file() prompt(f"Rank {rank:^6} acquired channel {channel_name} data.", nature='info') if in_args.run_type == "mpi": # Distributing and gathering the segments of the matrices in the proper process # The sky pixels are chunked and are handled by individual processes inv_cov_matrix_local_block = distribute_matrix( inv_cov_matrix_local, "cov_matrix", config['nside'][channel_name]) del inv_cov_matrix_local b_matrix_local_block = distribute_matrix( b_matrix_local, "b_matrix", config['nside'][channel_name]) del b_matrix_local hitmap_local_block = distribute_matrix( hitmap_local, "hitmap", config['nside'][channel_name]) del hitmap_local prompt( f"Rank {rank:^6} making covariance matrix for channel {channel_name}.", nature='info') cov_matrix_local_block = cov_ut.get_covariance_matrix( inv_cov_matrix_local_block, hitmap_local_block, config['pol_type']) prompt( f"Rank {rank:^6} estimating sky map for channel {channel_name}.", nature='info') sky_map_local_block = cov_ut.get_sky_map(cov_matrix_local_block, b_matrix_local_block, hitmap_local_block, config['pol_type']) cov_matrix_local_block_flat = cov_ut.flatten_block_matrix( cov_matrix_local_block, config['pol_type']) prompt( f"Rank {rank:^6} gathering maps for channel {channel_name}.", nature='info') sky_map = gather_map_segments(sky_map_local_block, 'sky_map', config['nside'][channel_name]) cov_matrix = gather_map_segments(cov_matrix_local_block_flat, 'cov_matrix', config['nside'][channel_name]) hitmap = gather_map_segments(hitmap_local_block, 'hitmap', config['nside'][channel_name]) if rank == 0: io_obj.write_map(sky_map, 'sky_map', channel_name) io_obj.write_map(cov_matrix, 'cov_map', channel_name) io_obj.write_map(hitmap, 'hit_map', channel_name) else: cov_matrix = cov_ut.get_covariance_matrix(inv_cov_matrix_local, hitmap_local, config['pol_type']) sky_map = cov_ut.get_sky_map(cov_matrix_full, b_matrix_local, hitmap_local, config['pol_type']) cov_ut.flatten_block_matrix(cov_matrix_full, config['pol_type']) io_obj.write_map(sky_map, 'sky_map', channel_name) io_obj.write_map( cov_ut.flatten_block_matrix(cov_matrix, config['pol_type']), 'cov_map', channel_name) io_obj.write_map(hitmap_local, 'hit_map', channel_name)
io_obj.make_top_data_directories(dir_list=['recon_dir']) #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* # The opening display messages #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* if rank == 0: global_start_message() if in_args.run_type == "mpi": comm.Barrier() time.sleep(rank * 0.1) local_start_message() comm.Barrier() #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#* # Everything is set up now for the map-makingrun. prompt(f"*** Rank {rank:^6} BEGINNING MAP-MAKING ***", nature='info') run_map_maker() stop_time_main = time.time() prompt( f"*** RANK {rank:^6} COMPLETE *** Time taken: {stop_time_main - start_time_main:.2f}s", nature='info') # Waiting for all the processes to finish if in_args.run_type == "mpi": comm.Barrier() if rank == 0: print_header() print_header(text='MAP-MAKING COMPLETE') print_header()
def run_simulation(): """ The actual simulation is performed here. By convention, observation starts at segment #1. Only the data blocks are distributed among mpi processes. Each process therefore loops over all channels and detectors Segments are further temporal sub-divisions on the data block The order of the loops are: data_block -> channel -> segment -> detector """ # The pointing_obj is common throughout the entire lifetime of the run pointing_obj = Pointing(instrument.params['scan_strategy']) input_maps_dict = load_maps() tod = {} block_count = 0 det_count = 0 proc_time_start = time.time() num_tot_det = sd.count_detectors(config['channel_detector_dict']) # Starting the loop over the blocks for data_block in sd.data_block_list_local: block_count += 1 prompt( f"Rank {rank:^6} starting data block {data_block}. ({block_count}/{sd.num_data_blocks_local})", nature='info') segment_list = sd.get_segment_list( data_block, config['num_segments_per_data_block']) # The satellite velocity is set to [0,0,0] is no orbital dipole is to be simulated if config['add_orbital_dipole']: t_start_block = get_t_start(segment_list[0], config['segment_length']) sat_vel_dict = get_satellite_velocity(t_start_block, config['segment_length'], segment_list) else: sat_vel_dict = dict.fromkeys(segment_list, np.array([0.0, 0.0, 0.0])) # Starting loop over channels for channel_name in config['channel_detector_dict'].keys(): if in_args.verbosity == 2: chan_time_start = time.time() det_count += len(config['channel_detector_dict'][channel_name]) prompt(f"Rank {rank:^6} \tobserving with channel {channel_name}", nature='info') channel = instrument.get_channel(channel_name) io_obj.open_tod_file(channel_name, data_block, 'w') # The I/O object for the output file channel_common, channel_common_attributes = get_channel_common( channel.params, channel_name) # Channel metadata io_obj.write_channel_common(channel_common, channel_common_attributes) # Starting loop over segments for segment in segment_list: t_start = get_t_start(segment, config['segment_length']) segment_common = { 'time': t_start, 'vsun': sat_vel_dict[segment] } # Segment metadata segment_common_attributes = { 'vsun': { 'info': '[x,y,z]', 'coords': config['coordinate_system'] }, 'time': { 'type': 'second' } } # psi_hwp is set to 0 if scanning is passive. if channel.params['pol_modulation'] != "passive": tod['psi_hwp'] = pointing_obj.get_hwp_phase( t_start=t_start, fsamp=channel.params['sampling_rate'], seg_len=config['segment_length'], hwp_spin_rate=channel.params['HWP']['spin_rate']) segment_common['psi_hwp'] = tod['psi_hwp'][0] else: segment_common['psi_hwp'] = 0.0 # Initialising the rotation quaternions for this segment. Common to all drtectors pointing_obj.generate_obv_quaternion( t_start=t_start, fsamp=channel.params['sampling_rate'], seg_len=config['segment_length'], coords=config['coordinate_system']) io_obj.write_segment_common(segment, segment_common, segment_common_attributes) for detector_name in config['channel_detector_dict'][ channel_name]: detector = channel.get_detector(detector_name) # The TOD is now compiled together by the detector object detector.observe_sky(pointing_obj, input_maps_dict[channel_name], tod, config['segment_length'], sat_vel_dict[segment], config['noise_type'], config['tod_write_field']) # Detector metadata scalars = { 'gain': 1.0, 'sigma0': detector.params['noise']['white_noise_rms'], 'fknee': detector.params['noise']['f_knee'], 'alpha': detector.params['noise']['noise_alpha'] } io_obj.write_tod(segment, detector_name, tod, config['tod_write_field']) io_obj.write_tod_scalars(segment, detector_name, scalars) io_obj.close_tod_file() if in_args.verbosity == 2: chan_time_stop = time.time() chan_time_taken = chan_time_stop - chan_time_start elapsed_time = chan_time_stop - proc_time_start estimated_time = elapsed_time * (sd.num_data_blocks_local * num_tot_det / det_count) prompt( f"Rank {rank:^6} \ttook {chan_time_taken:.2f}s for {channel_name}. Elapsed time {elapsed_time:.2f}s. Estimated total time {estimated_time:.2f}s", nature='info')
def global_start_message(): print_header() print_header(text='GENESYS') print_header(text='SIMULATING SATELLITE TOD') print_header() prompt(f"{'RUN TYPE':<35}{in_args.run_type}") prompt(f"{'# OF PROCESSES':<35}{size}") prompt(f"{'SIMULATION TAG':<35}{config['sim_tag']}") prompt(f"{'SPECIAL TAG':<35}{config['special_tag']}") prompt(f"{'SIMULATION POLARISATION TYPE':<35}{config['pol_type']}") prompt(f"{'COORDINATE SYSTEM':<35}{config['coordinate_system']}") prompt(f"{'NOISE TYPE':<35}{config['noise_type']}") prompt(f"{'ORBITAL DIPOLE':<35}{config['add_orbital_dipole']}") prompt(f"{'DETECTOR LIST':<35}{config['channel_detector_dict']}") prompt(f"{'# of DETECTORS':<35}{num_dets}") seg_len = config['segment_length'] prompt( f"{'SEGMENT LENGTH':<35}{seg_len}sec = {conv_u(seg_len, 'time', 'sec', 'hour'):.2f}h = {conv_u(seg_len, 'time', 'sec', 'day'):.4f}d" ) prompt(f"{'NUMBER OF DATA BLOCKS':<35}{sd.num_data_blocks_global}") prompt( f"{'NUMBER OF SEGMENTS PER DATA BLOCK':<35}{config['num_segments_per_data_block']}" ) obv_len = sd.observation_time_global prompt( f"{'TOTAL OBSERVATION TIME':<35}{obv_len}sec = {conv_u(obv_len, 'time', 'sec', 'day'):.2f}d = {conv_u(obv_len, 'time', 'sec', 'year'):.4f}y" ) print_header()
def huffman_compress(): for data_block in sd.data_block_list_local: prompt(f"Rank {rank:^6} starting data block {data_block}", nature='info') for channel_name in config['channel_detector_dict'].keys(): io_obj.open_tod_file(channel_name, data_block, 'r') io_obj_huff.open_tod_file(channel_name, data_block, 'w') segment_list = sd.get_segment_list( data_block, config['num_segments_per_data_block']) channel_common, channel_common_attributes = io_obj.read_channel_common( ['fsamp', 'det', 'polang', 'mbang', 'coords']) coords = channel_common.pop('coords') channel_common.update({ 'nside': config['huffman']['nside'], 'npsi': config['huffman']['npsi'] }) io_obj_huff.write_channel_common(channel_common, channel_common_attributes) for segment in segment_list: segment_common, segment_common_attributes = io_obj.read_segment_common( segment, ['time', 'vsun', 'psi_hwp']) pix_array = [[], [], []] for detector_name in config['channel_detector_dict'][ channel_name]: tod = io_obj.read_tod(segment, detector_name, ['theta', 'phi', 'psi']) # pixels pixels = hp.ang2pix(config['huffman']['nside'], tod['theta'], tod['phi']) delta = np.diff(pixels) delta = np.insert(delta, 0, pixels[0]) pix_array[0].append(delta) # psi psi_bins = np.linspace(0, 2 * np.pi, num=config['huffman']['npsi']) psi_index = np.digitize(tod['psi'], psi_bins) delta = np.diff(psi_index) delta = np.insert(delta, 0, psi_index[0]) pix_array[1].append(delta) # flag flag = np.ones(tod['theta'].size) delta = np.diff(flag) delta = np.insert(delta, 0, flag[0]) pix_array[2].append(delta) h = huffman.Huffman("", config['huffman']['nside']) h.GenerateCode(pix_array) huffarray = np.append( np.append(np.array(h.node_max), h.left_nodes), h.right_nodes) segment_common.update({ 'hufftree': huffarray, 'huffsymb': h.symbols }) io_obj_huff.write_segment_common(segment, segment_common, segment_common_attributes) for detector_name in config['channel_detector_dict'][ channel_name]: tod = io_obj.read_tod(segment, detector_name, ['signal', 'theta', 'phi', 'psi']) tod['tod'] = tod.pop('signal') scalars = io_obj.read_tod_scalars(segment, detector_name) # signal, noise and scalars io_obj_huff.write_tod(segment, detector_name, tod, tod_write_field=['tod']) io_obj_huff.write_tod_scalars(segment, detector_name, scalars) # flag flag = np.ones(tod['tod'].size) delta = np.diff(flag) delta = np.insert(delta, 0, flag[0]) io_obj_huff.write_tod( segment, detector_name, {'flag': np.void(bytes(h.byteCode(delta)))}, tod_write_field=['flag']) # pixels pixels = hp.ang2pix(config['huffman']['nside'], tod['theta'], tod['phi']) delta = np.diff(pixels) delta = np.insert(delta, 0, pixels[0]) io_obj_huff.write_tod( segment, detector_name, {'pix': np.void(bytes(h.byteCode(delta)))}, tod_write_field=['pix']) # psi psi_bins = np.linspace(0, 2 * np.pi, num=config['huffman']['npsi']) psi_index = np.digitize(tod['psi'], psi_bins) delta = np.diff(psi_index) delta = np.insert(delta, 0, psi_index[0]) io_obj_huff.write_tod( segment, detector_name, {'psi': np.void(bytes(h.byteCode(delta)))}, tod_write_field=['psi']) io_obj.close_tod_file() io_obj_huff.close_tod_file()