-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
235 lines (195 loc) · 5.7 KB
/
files.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
from contextlib import contextmanager
@contextmanager
def environ(**env):
"""Temporarily set environment variables inside the context manager and
fully restore previous environment afterwardsself.
Pass the env vars as keywords.
Code from https://gist.github.com/igniteflow/7267431#gistcomment-2553451.
Example
-------
>>> with environ(USER='xyz'):
>>> print(os.environ['USER'])
>>> print(os.environ['USER'])
"""
original_env = {key: os.getenv(key) for key in env}
os.environ.update(env)
try:
yield
finally:
for key, value in original_env.items():
if value is None:
del os.environ[key]
else:
os.environ[key] = value
def expand_path(path):
"""Perform a full unix-like path expansion on `path`."""
return os.path.expandvars(os.path.expanduser(path))
def make_dirs_for_file(filename):
"""Make all of the parent directories for a file at `filename`."""
dirname = os.path.dirname(filename)
if len(dirname) > 0:
os.makedirs(dirname, exist_ok=True)
def get_mcal_file_path(*, meds_dir, medsconf, tilename):
"""Get the path to the output metacal file.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
Returns
-------
mcal_pth : str
The path to the metacal file.
"""
return os.path.join(
meds_dir,
'metacal',
medsconf,
"%s_metacal.fits" % tilename)
def get_meds_file_path(*, meds_dir, medsconf, tilename, band):
"""Get the MEDS file for a given tile and band.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
band : str
The band as a string (e.g., `'r'`).
Returns
-------
meds_file : str
The path to the MEDS file.
"""
return os.path.join(
meds_dir,
'meds',
medsconf,
tilename,
"%s_%s_meds-%s.fits.fz" % (tilename, band, medsconf))
def get_truth_catalog_path(*, meds_dir, medsconf, tilename):
"""Get the truth catalog path.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
Returns
-------
truth_file_path : str
The path to the truth file.
"""
return os.path.join(
meds_dir,
'simple_des_y3_sims',
medsconf,
'truthcats',
'%s_truthcat.fits' % tilename
)
def get_band_info_file(*, meds_dir, medsconf, tilename, band):
"""Get the path of the YAML file holding the info dict for the
`tilename` and `band`.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
bands : str
A bands (e.g., 'r').
Returns
-------
info_file : str
The YAML file with the coadd + SE information.
"""
return os.path.join(
meds_dir,
'simple_des_y3_sims',
medsconf,
'band_info_files',
'%s_%s_info.yaml' % (tilename, band))
def get_swarp_files_path(*, meds_dir, medsconf):
"""Get the path of files related to swarp (txt files of exp lists
and then also the final detection coadd),
that go into the swarp coadd for this `tilename` and `band`.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
bands : str
A bands (e.g., 'r').
Returns
-------
path : str
The path of list files for coadd generation.
"""
return os.path.join(
meds_dir,
'simple_des_y3_sims',
medsconf,
'swarp_files')
def get_nwgint_path(*, meds_dir, medsconf, band):
"""Get the path of the ngwint files used as intermediaries
that go into the swarp coadd for this `tilename` and `band`.
Parameters
----------
meds_dir : str
The DESDATA/MEDS_DIR path where the info file is located.
medsconf : str
The MEDS file version (e.g., 'y3v02').
tilename : str
The DES coadd tilename (e.g., 'DES2122+0001').
bands : str
A bands (e.g., 'r').
Returns
-------
path : str
The path of list files for coadd generation.
"""
return os.path.join(
meds_dir,
'simple_des_y3_sims',
medsconf,
'nwgint',
'sources-%s'%band)
def get_piff_path_from_image_path(*, image_path, piff_run):
"""Get the piff path from the image path.
Parameters
----------
image_path : str
A path to an immask file
piff_run : str
e.g. y3a1-v29
Returns
-------
psf_path : str
The path to the PSF.
"""
img_bname = os.path.basename(image_path)
piff_bname = img_bname.replace('immasked.fits.fz', 'piff.fits')
expnum = int(piff_bname.split('_')[0][1:])
exp_dir = os.path.join(
'$PIFF_DATA_DIR',
piff_run,
str(expnum),
)
psf_path = os.path.join(
exp_dir,
piff_bname,
)
return psf_path