|
15 | 15 |
|
16 | 16 | # ======================================================================= |
17 | 17 |
|
| 18 | +from crds import api |
18 | 19 | from crds.core import rmap, config, utils, timestamp, log, exceptions |
19 | 20 | from crds.certify import generic_tpn |
20 | 21 | from crds import data_file |
@@ -467,12 +468,35 @@ def locate_file(refname, mode=None, parameters=None): |
467 | 468 | parameters = dict() |
468 | 469 | if mode is None: |
469 | 470 | mode = config.get_crds_ref_subdir_mode(observatory="jwst") |
| 471 | + |
470 | 472 | if mode == "instrument": |
471 | | - try: |
472 | | - instrument = utils.header_to_instrument(parameters) |
473 | | - except KeyError: |
474 | | - instrument = utils.file_to_instrument(refname) |
475 | | - rootdir = locate_dir(instrument, mode) |
| 473 | + |
| 474 | + # Check if the file is already in the local cache |
| 475 | + for instrument in INSTRUMENTS: |
| 476 | + if instrument != 'all': |
| 477 | + rootdir = locate_dir(instrument, mode) |
| 478 | + if os.path.exists(os.path.join(rootdir, os.path.basename(refname))): |
| 479 | + break |
| 480 | + else: |
| 481 | + rootdir = None |
| 482 | + |
| 483 | + # Not in local cache. Try various other methods. |
| 484 | + if rootdir is None: |
| 485 | + try: |
| 486 | + instrument = utils.header_to_instrument(parameters) |
| 487 | + except KeyError: |
| 488 | + log.verbose('Cannot find instrument in header. Trying from file itself...', verbosity=80) |
| 489 | + try: |
| 490 | + instrument = utils.file_to_instrument(refname) |
| 491 | + except FileNotFoundError: |
| 492 | + log.verbose('Cannot find instrument from non-existent file.', verbosity=80) |
| 493 | + log.verbose('Attempt to contact server for meta information', verbosity=80) |
| 494 | + |
| 495 | + # If there is a server, get the instrument from there. |
| 496 | + instrument = api.get_file_info(api.get_default_context(observatory='roman'), os.path.basename(refname))['instrument'] |
| 497 | + |
| 498 | + rootdir = locate_dir(instrument, mode) |
| 499 | + |
476 | 500 | elif mode == "flat": |
477 | 501 | rootdir = config.get_crds_refpath("jwst") |
478 | 502 | else: |
|
0 commit comments