Showing 1 of 4 files from the diff.

@@ -17,6 +17,8 @@
Loading
17 17
import shutil
18 18
import cgi
19 19
from pathlib import Path
20 +
import tarfile
21 +
import os
20 22
21 23
from . import conf
22 24
from astropy import log
@@ -266,5 +268,170 @@
Loading
266 268
        else:
267 269
            return columns
268 270
271 +
    def _parse_filename(self, filename):
272 +
        """Parses the file's name of a product
273 +
274 +
        Parses the file's name of a product following
275 +
        http://xmm-tools.cosmos.esa.int/external/xmm_user_support/documentation/dfhb/pps.html
276 +
277 +
        Parameters
278 +
        ----------
279 +
        filename : string, mandatory
280 +
            The name of the file to be parsed
281 +
282 +
        Returns
283 +
        -------
284 +
        A dictionary with field (as specified in the link above)
285 +
        as key and the value of each field
286 +
        """
287 +
        ret = {}
288 +
        ret["X"] = filename[0]
289 +
        ret["obsidentif"] = filename[1:11]
290 +
        ret["I"] = filename[11:13]
291 +
        ret["U"] = filename[13]
292 +
        ret["E"] = filename[14:17]
293 +
        ret["T"] = filename[17:23]
294 +
        ret["S"] = filename[23]
295 +
        ret["X-"] = filename[24:27]
296 +
        ret["Z"] = filename[28:]
297 +
        return ret
298 +
299 +
    def get_epic_images(self, filename, *, band=[], instrument=[],
300 +
                        get_detmask=False, get_exposure_map=False, path=""):
301 +
        """Extracts the European Photon Imaging Camera (EPIC) images from a given TAR file
302 +
303 +
        For a given TAR file obtained with:
304 +
            XMM.download_data(OBS_ID,level="PPS",extension="FTZ",filename=tarfile)
305 +
306 +
        This function extracts the EPIC images in a given band (or bands) and
307 +
        instrument (or instruments) from it
308 +
309 +
        The result is a dictionary containing the paths to the extracted EPIC
310 +
        images with keys being the band and the instrument
311 +
312 +
        If the band or the instrument are not specified this function will
313 +
        return all the available bands and instruments
314 +
315 +
        Additionally, ``get_detmask`` and ``get_exposure_map`` can be set to True.
316 +
        If so, this function will also extract the exposure maps and detector
317 +
        masks within the specified bands and instruments
318 +
319 +
        Examples
320 +
        --------
321 +
322 +
        Extract all bands and instruments::
323 +
            result = XMM.get_epic_images(tarfile,band=[1,2,3,4,5,8],
324 +
                                         instrument=['M1','M2','PN'],**kwargs)
325 +
326 +
        If we want to retrieve the band 3 for the instrument PN (p-n junction)::
327 +
            fits_image = result[3]['PN']
328 +
329 +
        ``fits_image`` will be the full path to the extracted FTZ file
330 +
331 +
        Extract the exposure and detector maps::
332 +
            result = XMM.get_epic_images(tarfile,band=[1,2,3,4,5,8],
333 +
                                         instrument=['M1','M2','PN'],
334 +
                                         get_detmask=True,
335 +
                                         get_exposure_map=True)
336 +
337 +
        If we want to retrieve exposure map in the band 3 for the instrument PN::
338 +
            fits_image = result[3]['PN_expo']
339 +
340 +
        For retrieving the detector mask in the band 3 for the instrument PN::
341 +
            fits_image = result[3]['PN_det']
342 +
343 +
        Parameters
344 +
        ----------
345 +
        filename : string, mandatory
346 +
            The name of the tarfile to be proccessed
347 +
        band : array of integers, optional, default []
348 +
            An array of intergers indicating the desired bands
349 +
        instruments : array of strings, optional, default []
350 +
            An array of strings indicating the desired instruments
351 +
        get_detmask : bool, optional
352 +
            If True, also extracts the detector masks
353 +
        get_exposure_map : bool, optional
354 +
            If True, also extracts the exposure maps
355 +
        path: string, optional
356 +
            If set, extracts the EPIC images in the indicated path
357 +
358 +
        Returns
359 +
        -------
360 +
        A dictionary of dictionaries with the full paths of the extracted
361 +
        EPIC images. The keys of each dictionary are the band for the first
362 +
        level dictionary and the instrument for the second level dictionaries
363 +
364 +
        Notes
365 +
        -----
366 +
        The structure and the content of the extracted compressed FITS files
367 +
        are described in details in the Pipeline Products Description
368 +
        [XMM-SOC-GEN-ICD-0024](https://xmm-tools.cosmos.esa.int/external/xmm_obs_info/odf/data/docs/XMM-SOC-GEN-ICD-0024.pdf).
369 +
        """
370 +
371 +
        _product_type = ["IMAGE_"]
372 +
        _instrument = ["M1", "M2", "PN", "EP"]
373 +
        _band = [1, 2, 3, 4, 5, 8]
374 +
        _path = ""
375 +
        if get_detmask:
376 +
            _product_type.append("DETMSK")
377 +
        if get_exposure_map:
378 +
            _product_type.append("EXPMAP")
379 +
        if path != "" and os.path.exists(path):
380 +
            _path = path
381 +
382 +
        ret = {}
383 +
        if band == []:
384 +
            band = _band
385 +
        else:
386 +
            for i in band:
387 +
                if i not in _band:
388 +
                    log.warning("Invalid band %u" % i)
389 +
                    band.remove(i)
390 +
391 +
        if instrument == []:
392 +
            instrument = _instrument
393 +
        else:
394 +
            for i in instrument:
395 +
                if i not in _instrument:
396 +
                    log.warning("Invalid instrument %s" % i)
397 +
                    instrument.remove(i)
398 +
        with tarfile.open(filename, "r") as tar:
399 +
            for i in tar.getmembers():
400 +
                paths = os.path.split(i.name)
401 +
                fname = paths[1]
402 +
                paths = os.path.split(paths[0])
403 +
                if paths[1] != "pps":
404 +
                    continue
405 +
                fname_info = self._parse_filename(fname)
406 +
                if fname_info["X"] != "P":
407 +
                    continue
408 +
                if not fname_info["I"] in instrument:
409 +
                    continue
410 +
                if not int(fname_info["S"]) in band:
411 +
                    continue
412 +
                if not fname_info["T"] in _product_type:
413 +
                    continue
414 +
                tar.extract(i, _path)
415 +
                if not ret.get(int(fname_info["S"])):
416 +
                    ret[int(fname_info["S"])] = {}
417 +
                b = int(fname_info["S"])
418 +
                ins = fname_info["I"]
419 +
                value = os.path.abspath(os.path.join(_path, i.name))
420 +
                if fname_info["T"] == "DETMSK":
421 +
                    ins = fname_info["I"] + "_det"
422 +
                elif fname_info["T"] == "EXPMAP":
423 +
                    ins = fname_info["I"] + "_expo"
424 +
                if ret[b].get(ins) and type(ret[b].get(ins)) == str:
425 +
                    log.warning("More than one file found with the "
426 +
                                "band %u and "
427 +
                                "the instrument: %s" % (b, ins))
428 +
                    ret[b][ins] = [ret[b][ins], value]
429 +
                elif ret[b].get(ins) and type(ret[b].get(ins)) == list:
430 +
                    ret[b][ins].append(value)
431 +
                else:
432 +
                    ret[b][ins] = value
433 +
434 +
        return ret
435 +
269 436
270 437
XMMNewton = XMMNewtonClass()
Files Coverage
astroquery 64.03%
Project Totals (200 files) 64.03%
5399.2
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading