-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_metadata.py
More file actions
69 lines (56 loc) · 2.58 KB
/
split_metadata.py
File metadata and controls
69 lines (56 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
##############################################################################
# - Creates maps of galaxy counts and galaxy density in each tomographic bin.
##############################################################################
import os
import sys
from configuration import PipelineConfig as PC
from astropy.table import Table
from output_utils import colour_string
# SETTINGS #
config_file = sys.argv[1]
cf = PC(config_file, stage='splitMetadata')
#######################################################
# START OF SCRIPT #
#######################################################
# Load the metadata
t = Table.read(cf.data_files.metadata)
# Output directory (same as the directory containing the downloaded data)
PATH_OUT = cf.paths.data
# Cycle through the global fields
for fd in cf.fields:
print(colour_string(fd, 'orange'))
# Define a mask for selecting data within the bounds of the field
ra_min, ra_max, dec_min, dec_max = cf.get_field_boundaries(fd)
if ra_max < ra_min:
coord_mask = ((t['ra2000'] >= ra_min) | (t['ra2000'] <= ra_max)) * \
(t['dec2000'] >= dec_min) * (t['dec2000'] <= dec_max)
else:
coord_mask = (t['ra2000'] >= ra_min) * (t['ra2000'] <= ra_max) * \
(t['dec2000'] >= dec_min) * (t['dec2000'] <= dec_max)
# Determine whether to split the metadata by band (filter)
if cf.split_by_band:
for b in cf.bands.all:
fname = f'{cf.data_files.metadata[:-5]}_{fd}_{b}.fits'
# Only bother with the next steps if the target file doesn't exist
if os.path.exists(fname):
print(f'Metadata file for {fd} in band {b} already exists; '
'skipping...')
continue
print(b)
# Mask to select rows using the current filter
band_mask = (t['filter'] == b)
# See if other columns might also contain data for this band
if b in cf.bands.altnames:
for b_alt in cf.bands.altnames[b]:
band_mask |= (t['filter'] == b_alt)
# Combine with the field mask
mask = coord_mask * band_mask
# Save the masked table to the destination file
t[mask].write(fname, format='fits')
else:
fname = f'{cf.data_files.metadata[:-5]}_{fd}.fits'
# Only bother with the next steps if the target file doesn't exist
if os.path.exists(fname):
print(f'Metadata file for {fd} already exists; skipping...')
else:
t[coord_mask].write(fname, format='fits')