forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
findfile.py
433 lines (301 loc) · 14.4 KB
/
findfile.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import re
import os
import time
import glob
import datetime
import fitsio
import subprocess
import numpy as np
import astropy.io.fits as fits
from collections import OrderedDict
from astropy.table import Table, vstack
from delta8_limits import d8_limits
from astropy.io.fits import getval, getheader
from utils import run_command
from pkg_resources import resource_filename
supported = ['gold',\
'kE',\
'zmax',\
'vmax',\
'lumfn',\
'lumfn_step',\
'ddp',\
'ddp_n8']
def safe_reset(supported=True, printonly=False, debug=False):
if supported:
fpaths = supported_files(dryrun=True)
fpaths += supported_files(dryrun=False)
else:
fpaths += unsupported_files(dryrun=True)
fpaths += unsupported_files(dryrun=False)
for fpath in fpaths:
'''
if ~os.path.exists(fpath):
continue
'''
try:
immutable = fetch_header(fpath=fpath, name='IMMUTABLE')
except KeyError as E:
immutable = 'NOT DEFINED'
to_keep = (immutable == 'TRUE')
print('RESET: {} with IMMUTABILITY {} KEEP {}'.format(fpath.ljust(80), immutable.ljust(20), to_keep))
if to_keep:
continue
if not printonly:
cmd = f'rm -rf {fpath}'
out = run_command(cmd, noid=True)
def call_signature(dryrun, argv):
if dryrun:
print('\n\nCall signature: python3 ' + ' '.join(argv) + '\n\n')
def gather_cat(fpaths):
if len(fpaths) == 0:
return None
assert np.all(np.array([os.path.isfile(x) for x in fpaths])), 'Failed to find {}'.format(fpaths)
for fpath in fpaths:
print(f'\tFetching {fpath}.')
tables = [Table.read(x) for x in fpaths]
tables = vstack(tables)
# TODO: Headers, e.g. Area, ngal etc.
tables.meta = {}
return tables
def write_desitable(opath, table, test=False):
if test:
table = Table()
table['a'] = [1, 4]
table['b'] = [2.0, 5.0]
table['c'] = ['x', 'y']
opath = './test.fits'
assert 'fits' in opath
table.write(opath, overwrite=True)
cmds = []
cmds.append(f'chgrp desi {opath}')
cmds.append(f'chmod 700 {opath}')
print('\n\n')
for cmd in cmds:
output = subprocess.check_output(cmd, shell=True)
print(cmd, output)
def fetch_fields(survey):
assert survey in ['desi', 'gama'], f'Fields for {survey} survey are not supported.'
fpath = resource_filename('DESI', f'data/{survey}_fields.txt')
fields = np.loadtxt(fpath, comments="#", delimiter=",", unpack=False, dtype=str)
return fields
def release_dir(user=os.environ['USER'], survey='gama', version=None):
# E.g. /cosma/home/durham/dc-wils7/data/GAMA4/
if version == 'latest':
ff = glob.glob('/cosma/home/durham/{}/data/v*'.format(user))
ff.sort(key=os.path.getmtime)
return ff[-1]
elif version != None:
return '/cosma/home/durham/{}/data/{}/'.format(user, version)
else:
return '/cosma/home/durham/{}/data/GAMA4/'.format(user)
def overwrite_check(opath, ext=None):
if os.path.isfile(opath):
exist = True
if ext != None:
hdul = fits.open(opath)
exist = False
# print(ext)
# print(hdul.info())
for hdu in hdul:
hdr = hdu.header
try:
if hdr['EXTNAME'] == 'BOUNDARY':
exist = True
print(f'WARNING: Found existing BOUNDARY extension to {opath} and overwrite forbidden (--nooverwrite).')
except KeyError as E:
pass
else:
print(f'{opath} found on disk and overwrite forbidden (--nooverwrite).')
if exist:
exit(0)
def fetch_header(ftype=None, name=None, ext=1, allsupported=False, dryrun=False, prefix=None, field=None, utier='{utier}', survey=None, realz=0, debug=False, version=None, fpath=None):
if allsupported:
result = OrderedDict()
defined = []
for ss in supported:
additions = fetch_header(ftype=ss, name=name, ext=ext, dryrun=dryrun, prefix=prefix, field=field, utier=utier, survey=survey, realz=realz, debug=debug, version=version)
for xx in defined:
additions.pop(xx, None)
if additions:
defined += list(additions.keys())
result[ss] = additions
return result
if fpath is None:
fpath = findfile(ftype=ftype, dryrun=dryrun, prefix=prefix, field=field, utier=utier, survey=survey, realz=realz, debug=debug, version=version)
if debug:
print(f'Fetching header of {fpath}')
if name:
return getval(fpath, name, ext)
else:
hdr = getheader(fpath, ext)
cards = [card for card in hdr.cards if np.all(~np.isin(card[0][:5], ["TUNIT", "TNULL", "TTYPE", "TFORM", "XTENS", "NAXIS", "BITPI", "PCOUN", "GCOUN"]))]
result = {}
for card in cards:
try:
result[card[0]] = card[1]
except Exception as E:
print(f'WARNING: {E}')
return result
def findfile(ftype, dryrun=False, prefix=None, field=None, utier='{utier}', survey=None, realz=0, debug=False, version=None, oversample=1, log=False, ddp_count=-1):
if version == None:
if 'NERSC_HOST' in os.environ:
gold_dir = os.environ['CSCRATCH'] + '/norberg/GAMA4/'
elif 'GOLD_DIR' in os.environ:
gold_dir = os.environ['GOLD_DIR']
elif 'GITHUB_ACTIONS' in os.environ:
gold_dir = 'GAMA4/'
else:
gold_dir = os.environ['HOME'] + '/data/GAMA4/'
print('Warning: GOLD_DIR not defined in environment; assuming {gold_dir}')
else:
gold_dir = release_dir(version=version)
rand_dir = gold_dir + '/randoms/'
# Special cases:
if ftype == 'config':
return gold_dir + '/configs/config.yaml'
if ftype == 'jackknife':
if dryrun:
dryrun = '_dryrun'
else:
dryrun = ''
return gold_dir + '/randoms/jackknife{}{}.yaml'.format(prefix.replace('randoms', ''), dryrun)
if survey == None:
survey = 'gama'
print('WARNING: defaulting to survey=gama')
survey = survey.lower()
# Special case:
if (ftype == 'gold') & dryrun & (survey == 'gama'):
return os.environ['CODE_ROOT'] + '/data/gama_gold_dryrun.fits'
fields = fetch_fields(survey)
if field != None:
valid = (field in fields) | ('ALL' in field)
assert valid, print(f'Requested field {field} is not valid ({fields})')
if dryrun:
dryrun = '_dryrun'
debug = True
else:
dryrun = ''
if realz >= 50:
raise ValueError('Randoms realizations limisted to max. of 50')
if ftype == 'ddp_limit':
if log:
return gold_dir + '/logs/' + '{}_ddrp_limit.log'.format(survey)
else:
fpath = gold_dir + '/ddrp_limits/' + '{}_ddrp_limit_{}.fits'.format(survey, ddp_count)
if ddp_count == 'all':
fpath = fpath.replace('.fits', '.txt')
return fpath
if isinstance(field, list):
return [findfile(ftype, dryrun=dryrun, prefix=prefix, field=ff, utier=utier) for ff in field]
if ftype == 'summary_log':
return gold_dir + 'summary.log'
if field == None:
file_types = {'gold': {'dir': gold_dir, 'id': f'{survey}', 'ftype': 'gold'},\
'kE': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'kE'},\
'zmax': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'zmax'},\
'vmax': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'vmax'},\
'lumfn': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'lumfn'},\
'lumfn_step': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'lumfn_step'},\
'ddp': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp'},\
'ddp_n8': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp_n8'}}
parts = file_types[ftype]
fpath = parts['dir'] + '/{}_{}{}.fits'.format(parts['id'], parts['ftype'], dryrun)
else:
file_types = {'ddp_n8_d0': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp_n8_d0_{}'.format(utier)},\
'ddp_n8_d0_vmax': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp_n8_d0_{}_vmax'.format(utier)},\
'ddp_n8_d0_lumfn': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp_n8_d0_{}_lumfn'.format(utier)},\
'ddp_n8_d0_lumfn_step': {'dir': gold_dir, 'id': f'{survey}_gold', 'ftype': 'ddp_n8_d0_{}_lumfn_step'.format(utier)},\
'randoms': {'dir': rand_dir, 'id': 'randoms', 'ftype': realz},\
'randoms_n8': {'dir': rand_dir, 'id': 'randoms_N8', 'ftype': realz},\
'randoms_bd': {'dir': rand_dir, 'id': 'randoms_bd', 'ftype': realz},\
'randoms_bd_ddp_n8': {'dir': rand_dir, 'id': 'randoms_bd_ddp_n8', 'ftype': realz},\
'volavg_fillfactor': {'dir': rand_dir, 'id': 'volavg_fillfactor', 'ftype': '_{}_{}'.format(realz, utier)},\
'boundary': {'dir': rand_dir, 'id': 'boundary', 'ftype': realz}
}
parts = file_types[ftype]
if oversample > 1:
oversample = f'_x{oversample}'
else:
oversample = ''
fpath = f'' + parts['dir'] + '/{}_{}{}_{}{}.fits'.format(parts['id'], field, oversample, parts['ftype'], dryrun)
if prefix != None:
assert 'randoms' in prefix;
dirname = os.path.dirname(fpath)
fpath = os.path.basename(fpath)
fpath = fpath.replace('randoms', prefix)
fpath = dirname + '/' + fpath
if debug:
print(f'DEBUG: findfile returns {fpath}')
fpath = fpath.replace('//', '/')
if ftype == 'boundary':
assert log == True
if log:
fpath = os.path.dirname(fpath) + '/logs/' + os.path.basename(fpath).split('.')[0] + '.log'
return fpath
def supported_files(dryrun=None):
try:
dryrun = os.environ['DRYRUN']
except Exception as E:
dryrun = ''
fpaths = []
for survey in ['desi', 'gama']:
for xx in supported:
fpaths.append(findfile(xx, dryrun=False, survey=survey))
fields = fetch_fields(survey)
for field in fields:
for prefix in [None, 'randoms_ddp1']:
fpaths.append(findfile('randoms', dryrun=False, field=field, prefix=prefix, survey=survey))
fpaths.append(findfile('randoms_n8', dryrun=False, field=field, prefix=prefix, survey=survey))
fpaths.append(findfile('randoms_bd', dryrun=False, field=field, prefix=prefix, survey=survey))
fpaths.append(findfile('randoms_bd_ddp_n8', dryrun=False, field=field, prefix=prefix, survey=survey))
for ii, _ in enumerate(d8_limits):
fpaths.append(findfile('ddp_n8_d0', dryrun=False, field=field, utier=ii, survey=survey))
fpaths.append(findfile('ddp_n8_d0_vmax', dryrun=False, field=field, utier=ii, survey=survey))
fpaths.append(findfile('ddp_n8_d0_lumfn', dryrun=False, field=field, utier=ii, survey=survey))
return fpaths
def unsupported_files(dryrun=None):
fpaths = supported_files(dryrun=dryrun)
gold_paths = sorted(glob.glob(os.environ['GOLD_DIR'] + '/*.fits'))
rand_paths = sorted(glob.glob(os.environ['GOLD_DIR'] + '/randoms/*.fits'))
all_paths = gold_paths + rand_paths
unsupported = [x for x in all_paths if x not in fpaths]
unsupported = [x for x in unsupported if 'dryrun' not in x]
return unsupported
def file_check(dryrun=None):
fpaths = supported_files(dryrun=dryrun)
unsupported = unsupported_files(dryrun=dryrun)
print('\n\n---- SUPPORTED FPATHS ----\n')
for fp in fpaths:
if os.path.isfile(fp):
mtime = os.path.getmtime(fp)
mtime = datetime.datetime.utcfromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
try:
immutable = fetch_header(fpath=fp, name='IMMUTABLE')
except:
immutable = 'UNDEFINED'
else:
mtime = ''
immutable = 'UNDEFINED'
print('{}\t\t{}\t{}\t{}'.format(fp.ljust(100), os.path.isfile(fp), mtime, immutable))
print('\n\n---- UNSUPPORTED FPATHS ----\n')
for fp in unsupported:
if os.path.isfile(fp):
mtime = os.path.getmtime(fp)
mtime = datetime.datetime.utcfromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
try:
immutable =fetch_header(fpath=fp, name='IMMUTABLE')
except:
immutable = 'UNDEFINED'
else:
mtime = ''
immutable = 'UNDEFINED'
print('{}\t\t{}\t{}\t{}'.format(fp.ljust(100), os.path.isfile(fp), mtime, immutable))
return ~np.all([os.path.isfile(fp) for fp in fpaths])
if __name__ == '__main__':
# failure = file_check()
# print('\n\nSuccess: {}\n\n'.format(~failure))
# safe_reset(printonly=True)
# fetch_header('/cosma5/data/durham/dc-wils7/GAMA4/randoms/randoms_R1_0.fits', name='IMMUTABLE')
fields = fetch_fields('desi')
print(fields)