コード例 #1
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)
コード例 #2
0
               ['cl_rand', cl_rand, 'f8']]

    header_keywords = {}

    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:
        # keep column with  requested position in the input file
        print(str("+  -Col '%s'" % (c[0])))
        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))

    fname = input.cls_fname(zmin, zmax, run=input.pinocchio_first_run)
    print("## Writing FITS: %s" % fname)
    fitsfile = FITS(fname, 'rw')
    fitsfile.write_table(data=keep_table, header=hdr)
    fitsfile.close()

print("# DONE!")