|
1 | 1 | import sys |
| 2 | +import shutil |
2 | 3 | import logging |
3 | 4 | import argparse |
4 | 5 | from pathlib import Path |
5 | 6 |
|
| 7 | +import astropy.units as u |
6 | 8 | from astropy.io import fits |
7 | 9 |
|
8 | 10 | from stixcore.config.config import CONFIG |
| 11 | +from stixcore.products.product import Product |
9 | 12 | from stixcore.time.datetime import SCETime |
10 | 13 | from stixcore.util.logging import get_logger |
11 | 14 |
|
@@ -62,11 +65,24 @@ def find_fits(args): |
62 | 65 | choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"], |
63 | 66 | dest='log_level') |
64 | 67 |
|
| 68 | + parser.add_argument("-c", "--copy", |
| 69 | + help="copy the found files into that dir", type=str, |
| 70 | + default=None, dest='copy_dest') |
| 71 | + |
| 72 | + parser.add_argument("--parents", |
| 73 | + help="return the parents and not the fits itself", |
| 74 | + default=False, |
| 75 | + action='store_true', dest='get_parents') |
| 76 | + |
65 | 77 | args = parser.parse_args(args) |
66 | 78 |
|
67 | 79 | if args.log_level: |
68 | 80 | logger.setLevel(logging.getLevelName(args.log_level)) |
69 | 81 |
|
| 82 | + if args.copy_dest: |
| 83 | + args.copy_dest = Path(args.copy_dest) |
| 84 | + args.copy_dest.mkdir(parents=True, exist_ok=True) |
| 85 | + |
70 | 86 | fits_dir = Path(args.fits_dir) |
71 | 87 |
|
72 | 88 | include_levels = dict([(level, 1) for level in |
@@ -132,22 +148,36 @@ def find_fits(args): |
132 | 148 | continue |
133 | 149 |
|
134 | 150 | # prod = Product(c) |
135 | | - obt_beg = SCETime.from_string(fits.getval(c, 'OBT_BEG')) |
136 | | - obt_end = SCETime.from_string(fits.getval(c, 'OBT_END')) |
137 | | - |
138 | | - # obt filter overlapping |
139 | | - if not ((obt_beg >= args.start_obt and obt_end <= args.end_obt) or |
140 | | - (obt_beg >= args.start_obt and args.end_obt < obt_end) or |
141 | | - (obt_beg < args.start_obt and obt_end > args.start_obt)): |
| 151 | + try: |
| 152 | + f_beg = SCETime.from_string(fits.getval(c, 'OBT_BEG'))[0] |
| 153 | + f_end = SCETime.from_string(fits.getval(c, 'OBT_END'))[0] |
| 154 | + except Exception: |
| 155 | + f_beg = SCETime.from_float(fits.getval(c, 'OBT_BEG') * u.s) |
| 156 | + f_end = SCETime.from_float(fits.getval(c, 'OBT_END') * u.s) |
| 157 | + |
| 158 | + # obt filter included |
| 159 | + if not ((f_beg >= args.start_obt and f_end <= args.end_obt)): |
| 160 | + # should also overlap? |
| 161 | + # or (f_beg > args.start_obt and f_beg < args.end_obt) or |
| 162 | + # (f_end > args.start_obt and f_end < args.end_obt)): |
142 | 163 | continue |
| 164 | + if args.get_parents: |
| 165 | + p = Product(c) |
| 166 | + found.extend(p.find_parent_files(fits_dir)) |
143 | 167 |
|
144 | | - found.append(c) |
| 168 | + else: |
| 169 | + found.append(c) |
145 | 170 |
|
146 | 171 | logger.info(f'#candidates: {n_candidates}') |
147 | 172 | logger.info(f'#found: {len(found)}') |
148 | 173 |
|
149 | 174 | for f in found: |
150 | 175 | print(str(f)) |
| 176 | + if args.copy_dest: |
| 177 | + fits_parent_path = f.relative_to(fits_dir) |
| 178 | + target = args.copy_dest / fits_parent_path |
| 179 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 180 | + shutil.copy2(f, target) |
151 | 181 |
|
152 | 182 | return found |
153 | 183 |
|
|
0 commit comments