|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Create Template Measurement Set |
| 4 | +
|
| 5 | +Creates a minimal MS with specified dimensions using CASA simobserve. |
| 6 | +Used as template for injecting synthetic RFI data. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/create_template_ms.py --output template_1024x1024.ms |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import shutil |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from casatasks import simobserve |
| 17 | + |
| 18 | + |
| 19 | +def create_template_ms( |
| 20 | + output_path, |
| 21 | + num_channels=1024, |
| 22 | + num_times=1024, |
| 23 | + integration_time=10.0, |
| 24 | + num_antennas=27, |
| 25 | + antennalist="vla.d.cfg", |
| 26 | + frequency="1.5GHz", |
| 27 | + bandwidth="128MHz", |
| 28 | +): |
| 29 | + """ |
| 30 | + Create template MS with simobserve. |
| 31 | +
|
| 32 | + Args: |
| 33 | + output_path: Path for output MS |
| 34 | + num_channels: Number of frequency channels (default: 1024) |
| 35 | + num_times: Number of time samples (default: 1024) |
| 36 | + integration_time: Integration time in seconds (default: 10s) |
| 37 | + num_antennas: Number of antennas (default: 27 for VLA) |
| 38 | + antennalist: Antenna configuration (default: vla.d.cfg) |
| 39 | + frequency: Center frequency (default: 1.5GHz L-band) |
| 40 | + bandwidth: Total bandwidth (default: 128MHz) |
| 41 | +
|
| 42 | + Returns: |
| 43 | + Path to created MS |
| 44 | + """ |
| 45 | + output_path = Path(output_path) |
| 46 | + |
| 47 | + # Calculate total observing time to get desired number of samples |
| 48 | + # num_times = total_time / integration_time |
| 49 | + total_time_sec = num_times * integration_time |
| 50 | + total_time_str = f"{total_time_sec}s" |
| 51 | + |
| 52 | + print("=" * 70) |
| 53 | + print("Creating Template Measurement Set") |
| 54 | + print("=" * 70) |
| 55 | + print(f"Output: {output_path}") |
| 56 | + print(f"Dimensions: {num_channels} channels × {num_times} times") |
| 57 | + print(f"Frequency: {frequency}") |
| 58 | + print(f"Bandwidth: {bandwidth} ({num_channels} channels)") |
| 59 | + print(f"Integration time: {integration_time}s") |
| 60 | + print(f"Total time: {total_time_str} ({total_time_sec/3600:.2f} hours)") |
| 61 | + print(f"Antennas: {num_antennas} ({antennalist})") |
| 62 | + print("=" * 70) |
| 63 | + |
| 64 | + # Create project directory |
| 65 | + project_name = output_path.stem |
| 66 | + project_dir = output_path.parent / project_name |
| 67 | + |
| 68 | + # Clean up if exists |
| 69 | + if project_dir.exists(): |
| 70 | + print(f"\nRemoving existing project: {project_dir}") |
| 71 | + shutil.rmtree(project_dir) |
| 72 | + |
| 73 | + # Run simobserve |
| 74 | + print(f"\nRunning simobserve...") |
| 75 | + print("This will create an empty MS with the specified structure...") |
| 76 | + |
| 77 | + simobserve( |
| 78 | + project=project_name, |
| 79 | + skymodel="", # Empty sky (no sources) |
| 80 | + inbright="", |
| 81 | + indirection="J2000 10h00m00.0s -30d00m00.0s", # Arbitrary direction |
| 82 | + incell="0.5arcsec", |
| 83 | + inwidth=bandwidth, |
| 84 | + incenter=frequency, |
| 85 | + innchan=num_channels, |
| 86 | + # Observation parameters |
| 87 | + obsmode="int", # Interferometer |
| 88 | + antennalist=antennalist, |
| 89 | + totaltime=total_time_str, |
| 90 | + integration=f"{integration_time}s", |
| 91 | + # Output |
| 92 | + thermalnoise="", # No noise |
| 93 | + graphics="none", |
| 94 | + verbose=False, |
| 95 | + ) |
| 96 | + |
| 97 | + # Find the created MS |
| 98 | + # simobserve creates: project/project.antennalist.ms |
| 99 | + ms_pattern = list(project_dir.glob("*.ms")) |
| 100 | + |
| 101 | + if not ms_pattern: |
| 102 | + raise FileNotFoundError(f"No MS created in {project_dir}") |
| 103 | + |
| 104 | + created_ms = ms_pattern[0] |
| 105 | + print(f"\n✓ MS created: {created_ms}") |
| 106 | + |
| 107 | + # Move to desired output location |
| 108 | + if output_path.exists(): |
| 109 | + shutil.rmtree(output_path) |
| 110 | + |
| 111 | + shutil.move(str(created_ms), str(output_path)) |
| 112 | + print(f"✓ Moved to: {output_path}") |
| 113 | + |
| 114 | + # Clean up project directory |
| 115 | + shutil.rmtree(project_dir) |
| 116 | + |
| 117 | + # Verify dimensions |
| 118 | + from casatools import table |
| 119 | + |
| 120 | + tb = table() |
| 121 | + tb.open(str(output_path)) |
| 122 | + nrows = tb.nrows() |
| 123 | + tb.close() |
| 124 | + |
| 125 | + # Open spectral window table |
| 126 | + tb.open(str(output_path / "SPECTRAL_WINDOW")) |
| 127 | + actual_channels = tb.getcol("NUM_CHAN") |
| 128 | + tb.close() |
| 129 | + |
| 130 | + print("\nVerification:") |
| 131 | + print(f" Total rows: {nrows}") |
| 132 | + print(f" Channels/SPW: {actual_channels}") |
| 133 | + print(f" Expected times: {num_times}") |
| 134 | + |
| 135 | + print(f"\n✓ Template MS ready: {output_path}") |
| 136 | + print(f" Size: {output_path.stat().st_size / 1024**2:.1f} MB") |
| 137 | + |
| 138 | + return output_path |
| 139 | + |
| 140 | + |
| 141 | +def main(): |
| 142 | + parser = argparse.ArgumentParser(description="Create template MS for RFI validation") |
| 143 | + parser.add_argument( |
| 144 | + "--output", |
| 145 | + default="template_1024x1024.ms", |
| 146 | + help="Output MS path (default: template_1024x1024.ms)", |
| 147 | + ) |
| 148 | + parser.add_argument( |
| 149 | + "--channels", type=int, default=1024, help="Number of channels (default: 1024)" |
| 150 | + ) |
| 151 | + parser.add_argument( |
| 152 | + "--times", type=int, default=1024, help="Number of time samples (default: 1024)" |
| 153 | + ) |
| 154 | + parser.add_argument( |
| 155 | + "--integration", |
| 156 | + type=float, |
| 157 | + default=10.0, |
| 158 | + help="Integration time in seconds (default: 10s)", |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "--antennas", type=int, default=27, help="Number of antennas (default: 27 VLA)" |
| 162 | + ) |
| 163 | + parser.add_argument( |
| 164 | + "--config", |
| 165 | + default="vla.d.cfg", |
| 166 | + help="Antenna configuration (default: vla.d.cfg)", |
| 167 | + ) |
| 168 | + parser.add_argument( |
| 169 | + "--frequency", default="1.5GHz", help="Center frequency (default: 1.5GHz)" |
| 170 | + ) |
| 171 | + parser.add_argument( |
| 172 | + "--bandwidth", default="128MHz", help="Total bandwidth (default: 128MHz)" |
| 173 | + ) |
| 174 | + |
| 175 | + args = parser.parse_args() |
| 176 | + |
| 177 | + create_template_ms( |
| 178 | + output_path=args.output, |
| 179 | + num_channels=args.channels, |
| 180 | + num_times=args.times, |
| 181 | + integration_time=args.integration, |
| 182 | + num_antennas=args.antennas, |
| 183 | + antennalist=args.config, |
| 184 | + frequency=args.frequency, |
| 185 | + bandwidth=args.bandwidth, |
| 186 | + ) |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + main() |
0 commit comments