예제 #1
0
    def __init__(self,
                 init_data=False,
                 init_mask=False,
                 init_weight=False,
                 shape=(4096, 2048),
                 sourcefile=None):
        """Create a new DESImage

        Create an empty DESImage

        In mast cases, DESImage.create or DESImage.load will be safer
        and more covenient.
        """
        super().__init__(
            data=np.zeros(shape, dtype=data_dtype) if init_data else None,
            sourcefile=sourcefile)

        #self.data = np.zeros(shape, dtype=data_dtype) if init_data else None

        self.mask = None
        if init_mask:
            self.init_mask()

        self.weight = None
        if init_weight:
            self.init_weight()

        self.variance = None
        #self.header = FITSHDR({})
        #self.pri_hdr = self.header
        self.mask_hdr = FITSHDR({})
        self.weight_hdr = FITSHDR({})
예제 #2
0
class DESSingleImage:
    def __init__(self):
        self.header = FITSHDR({})
        self.pri_hdr = FITSHDR({})

    # If we index this object, assume we are after keywords in the header
    def __getitem__(self, key):
        if key in self.header:
            return self.header[key]
        return self.pri_hdr[key]

    def __setitem__(self, key, value):
        self.header[key] = value

    def write_key(self, key, value, comment=None):
        """
        Add or alter a header keyword/value pair, with optional comment field
        """
        if comment is None:
            self.header[key] = value
        else:
            self.header.add_record({
                'name': key,
                'value': value,
                'comment': comment
            })
예제 #3
0
    def __init__(self, data, header={}, pri_hdr=None, sourcefile=None):
        """Create a new DESDataImage

        :Parameters:
            - `data`: the numpy data array
            - `header`: the header
            - `pri_hdr`: the primary header of the FITS file

        """
        super().__init__()
        self.data = data
        self.sourcefile = sourcefile

        if getattr(header, "add_record", None) is None:
            self.header = FITSHDR(header)
        else:
            self.header = header

        if pri_hdr is None:
            self.pri_hdr = self.header

        if getattr(pri_hdr, "add_record", None) is None:
            self.pri_hdr = FITSHDR(pri_hdr)
        else:
            self.pri_hdr = pri_hdr
예제 #4
0
    def __init__(self,tim, gain=4.0, seed=None, folding_threshold=1.e-5):
        """Initialize the BuildStamp object with the CCD-level properties we need."""
        self.band = tim.band.strip().upper()
        # GSParams should be used when galsim object is initialized
        self.gsparams = galsim.GSParams(maximum_fft_size=2L**30L,\
                                        folding_threshold=folding_threshold) 
        #print('FIX ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        self.gsdeviate = galsim.BaseDeviate()
        #if seed is None:
        #    self.gsdeviate = galsim.BaseDeviate()
        #else:
        #    self.gsdeviate = galsim.BaseDeviate(seed)

        self.wcs = tim.getWcs()
        self.psf = tim.getPsf()
        # Tractor wcs object -> galsim wcs object
        temp_hdr = FITSHDR()
        subwcs = tim.wcs.wcs.get_subimage(tim.wcs.x0, tim.wcs.y0,
                                  int(tim.wcs.wcs.get_width())-tim.wcs.x0,
                                  int(tim.wcs.wcs.get_height())-tim.wcs.y0)
        subwcs.add_to_header(temp_hdr)
        # Galsim uses astropy header, not fitsio
        hdr = fits.Header()
        for key in temp_hdr.keys(): hdr[key]=temp_hdr[key]
        self.galsim_wcs = galsim.GSFitsWCS(header=hdr)
        del subwcs,temp_hdr,hdr
        
        # zpscale equivalent to magzpt = self.t.ccdzpt+2.5*np.log10(self.t.exptime)
        self.zpscale = tim.zpscale      # nanomaggies-->ADU conversion factor
        self.nano2e = self.zpscale*gain # nanomaggies-->electrons conversion factor
예제 #5
0
    def new_header(og_header: FITSHDR) -> FITSHDR:
        """
        Modifies an existing XMM Newton fits image header, removes some elements, and adds a little extra
        information. The new header is then used for PSF corrected fits image files.

        :param og_header: The header from the fits image that has been PSF corrected.
        :return: The new, modified, fits header.
        :rtype: FITSHDR
        """
        # These are nuisance entries that I want gone
        remove_list = ["CREATOR", "CONTINUE", "XPROC0", "XDAL0"]

        # FITSHDR takes a dictionary as its main content argument
        new_header_info = {}
        # We want to copy most of the original header
        for e in og_header:
            # But those that are in the remove list aren't copied over.
            if e not in remove_list:
                new_header_info[e] = og_header[e]
            # Also change the creator entry for a little self serving brand placement
            elif e == "CREATOR":
                new_header_info[e] = "XGA"

        # Setting some new headers for further information
        new_header_info["COMMENT"] = "THIS IMAGE HAS BEEN PSF CORRECTED BY XGA"
        new_header_info["PSFBins"] = bins
        new_header_info["PSFAlgorithm"] = "Richardson-Lucy"
        new_header_info["PSFAlgorithmIterations"] = iterations
        new_header_info["PSFModel"] = psf_model

        return FITSHDR(new_header_info)
예제 #6
0
    def __init__(self, bpm, header={}, pri_hdr=None, sourcefile=None):
        """Create a new DESBPMImage

        :Parameters:
            - `bpm`: the numpy data array
            - `header`: the header
            - `pri_hdr`: the primary header of the FITS file

        """
        self.mask = bpm
        self.sourcefile = sourcefile

        if getattr(header, "add_record", None) is None:
            self.header = FITSHDR(header)
        else:
            self.header = header

        if pri_hdr is None:
            self.pri_hdr = self.header

        if getattr(pri_hdr, "add_record", None) is None:
            self.pri_hdr = FITSHDR(pri_hdr)
        else:
            self.pri_hdr = pri_hdr
예제 #7
0
 def __init__(self):
     self.header = FITSHDR({})
     self.pri_hdr = FITSHDR({})
예제 #8
0
# Read in the read noise from a fits file generated with Bernie's ngxhrg
# noisemode 'last' uses one realization because the full one takes
# a long time to create
if noisemode == 'last':
    noise = fitsio.read(noisefile)
    data_cube_Q[-1, :, :] += noise  # Adding to only the final time
elif noisemode == 'full':
    noise = fitsio.read(noisefile)
    data_cube_Q += noise  # Adding the noise at all reads

# Convert charge to signal, clipping values<0 and >2**16
data_cube_S = np.array(np.clip(data_cube_Q / gain, 0, 65535), dtype=np.uint16)

# Write simple header, todo: add more thorough comments
hdr = FITSHDR()
hdr['GAIN'] = gain
hdr['ILLUMIN'] = I
hdr['QE'] = QE
hdr['RNGSEED'] = rngseed
if (lipcmode == 'true'):
    hdr['LINIPC'] = lipc_alpha[0]
if (nlmode == 'quadratic'):
    hdr['BETA'] = nlbeta
if (nlmode == 'quartic'):
    hdr['NLCOEFFS_c2'] = nlcoeffs_arr[0]
    hdr['NLCOEFFS_c3'] = nlcoeffs_arr[1]
    hdr['NLCOEFFS_c4'] = nlcoeffs_arr[2]
if (bfemode == 'true'):
    hdr['BFE_A00'] = a_coeff[2][2]  # Hard-coded to expect 5x5 a coeffs
예제 #9
0
def write_catalog(fname,
                  x,
                  y,
                  z,
                  w,
                  type="DATA",
                  format="fits",
                  coord="PSEUDO_EQUATORIAL"):
    if format == "txt":
        np.savetxt(fname, np.transpose([x, y, z, w]))

    elif format == "fits":
        # current DM supported
        DM_VERSION = "2.5.0"

        # Extracted from AsciiToFits.py written by Daniele Tavagnacco

        if coord == "PSEUDO_EQUATORIAL":
            columns = [['SOURCE_ID', -1, 'f8'], ['RA', x, 'f8'],
                       ['DEC', y, 'f8'], ['REDSHIFT', z, 'f8'],
                       ['WEIGHT', -1, 'f8'], ['DENSITY', w, 'f8']]
        elif coord == "CARTESIAN":
            columns = [['SOURCE_ID', -1, 'f8'], ['COMOV_X', x, 'f8'],
                       ['COMOV_Y', y, 'f8'], ['COMOV_Z', z, 'f8'],
                       ['WEIGHT', -1, 'f8'], ['DENSITY', w, 'f8']]

        header_keywords = {
            "TELESCOP": "EUCLID",
            "INSTRUME": "LE3GC-MOCKS",
            "FILENAME": fname,
            "CAT_ID": "MOCK",
            "COORD": coord,
            "ANGLE": "DEGREES"
        }

        # fname_nodir = fname[[pos for pos, char in enumerate(fname) if char == '/'][-1]+1:]
        # zshell = fname[fname.find('zshell'):-5]
        # header_keywords = {'TELESCOP' : 'EUCLID  ',
        #                    'INSTRUME' : 'LE3IDSELID',
        #                    'FILENAME' : fname_nodir,
        #                    'CAT_TYPE' : type,
        #                    'CAT_NAME' : 'MOCK-LE3GC',
        #                    'CAT_NOBJ' : x.size,
        #                    'COORD   ' : 'EQUATORIAL',
        #                    'ANGLE   ' : 'DEG     ',
        #                    'COMPLETE' : 1.0,
        #                    'PURITY  ' : 1.0,
        #                    'SELECT  ' : zshell}

        extension = "CATALOG"

        xmlKeys = {
            "pf": "PK_LE3_GC_WindowMultipoles",
            "instr": "LE3_GC_MOCKS",
            "id": "MOCK",
            "coord": coord
        }

        print("Preparing FITS structure")
        types = []
        # keep just wanted columns ...bad but..
        for c in columns:
            types.append((c[0], c[2]))

        hdr = FITSHDR()
        print("+ Add keywords")
        for k in header_keywords:
            hdr[k] = header_keywords[k]

        keep_table = {}
        for c in columns:
            # if required but not existing (-1) fill with ones
            if (c[1] is -1):
                # add tmp column with correct name and position, bu only ones
                print(str("+  -Col '%s' filled" % c[0]))
                keep_table[c[0]] = np.ones_like(columns[1][1],
                                                dtype=np.float64)
            else:
                # keep column with  requested position in the input file
                print(str("+  -Col '%s' from '%s'" % (c[0], c[1])))
                keep_table[c[0]] = c[1].astype(np.float64)

        fullsize = len(keep_table) * len(c[1]) * 8 / 1024 / 1024.
        print(str("+   ~%.2f MB in memory" % fullsize))

        # now write some data
        print(str("+ Write FITS: %s" % fname))
        fits = FITS(fname, 'rw', clobber=True)
        fits.write_table(data=keep_table, header=hdr, extname=extension)
        fits.close()

        print("+ Preparing XML product")

        with open(fname.replace(input.cat4le3_format, "xml"), "w+") as f:

            f.write(
                '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n'''
            )
            if (type == "RANDOM"):
                f.write(
                    '''<p1:DpdLE3gcInputRandCat xmlns:p1="http://euclid.esa.org/schema/dpd/le3/gc/inp/catrandin">\n'''
                )
            else:
                f.write(
                    '''<p1:DpdLE3gcInputDataCat xmlns:p1="http://euclid.esa.org/schema/dpd/le3/gc/inp/catdatain">\n'''
                )
            f.write('''  <Header>\n''')
            f.write(
                str('''    <ProductId>%s</ProductId>\n''' %
                    header_keywords["FILENAME"].split('.')[0]))
            f.write(
                '''    <ProductType>dpdLE3gcInputRandCat</ProductType>\n''')
            f.write('''    <SoftwareName>LE3_GC_test</SoftwareName>\n''')
            f.write('''    <SoftwareRelease>1.0</SoftwareRelease>\n''')
            f.write(
                '''    <ManualValidationStatus>UNKNOWN</ManualValidationStatus>\n'''
            )
            f.write('''    <PipelineRun>LE3_GC_Test_Inputs</PipelineRun>\n''')
            f.write('''    <ExitStatusCode>OK</ExitStatusCode>\n''')
            f.write(
                str('''    <DataModelVersion>%s</DataModelVersion>\n''' %
                    DM_VERSION))
            f.write(
                str('''    <MinDataModelVersion>%s</MinDataModelVersion>\n''' %
                    DM_VERSION))
            f.write('''    <ScientificCustodian>LE3</ScientificCustodian>\n''')
            f.write('''    <AccessRights>\n''')
            f.write(
                '''      <EuclidConsortiumRead>true</EuclidConsortiumRead>\n'''
            )
            f.write(
                '''      <EuclidConsortiumWrite>true</EuclidConsortiumWrite>\n'''
            )
            f.write(
                '''      <ScientificGroupRead>true</ScientificGroupRead>\n''')
            f.write(
                '''      <ScientificGroupWrite>true</ScientificGroupWrite>\n'''
            )
            f.write('''    </AccessRights>\n''')
            f.write('''    <Curator>\n''')
            f.write('''      <Name>SDC-IT</Name>\n''')
            f.write('''    </Curator>\n''')
            f.write(str('''    <Creator>%s</Creator>\n''' % xmlKeys["pf"]))
            f.write(
                '''    <CreationDate>2019-10-31T12:12:12Z</CreationDate>\n''')
            f.write('''  </Header>\n''')
            f.write('''  <Data>\n''')
            f.write(
                str('''  <Instrument>%s</Instrument>\n''' % xmlKeys["instr"]))
            f.write(str('''  <Catalog_ID>%s</Catalog_ID>\n''' % xmlKeys["id"]))
            f.write(str('''  <CoordType>%s</CoordType>\n''' %
                        xmlKeys["coord"]))
            f.write('''  <Catalog format="le3.gc.cat.test" version="0.2">\n''')
            f.write('''    <DataContainer filestatus="PROPOSED">\n''')
            f.write(str('''      <FileName>%s.fits</FileName>\n''' % fname))
            f.write('''    </DataContainer>\n''')
            f.write('''  </Catalog>\n''')
            f.write('''  </Data>\n''')
            if (type == "RANDOM"):
                f.write('''</p1:DpdLE3gcInputRandCat>\n''')
            else:
                f.write('''</p1:DpdLE3gcInputDataCat>\n''')
            f.close()

        print("files %s and %s written" %
              (fname, fname.replace(input.cat4le3_format, "xml")))

    else:

        print("ERROR: unrecognized format in write_catalog")
        sys.exit(-1)