Factory function that reads a FITS file and returns a f2nimage object.
    Use hdu to specify which HDU you want (primary = 0)
9604
def fromfits(infile, hdu = 0, verbose = True):
    
    
    pixelarray, hdr = ft.getdata(infile, hdu, header=True)
    pixelarray = np.asarray(pixelarray).transpose()
    #print pixelarray
    
    pixelarrayshape = pixelarray.shape
    if verbose :
        print "Input shape : (%i, %i)" % (pixelarrayshape[0], pixelarrayshape[1])
        print "Input file BITPIX : %s" % (hdr["BITPIX"])
    pixelarrayshape = np.asarray(pixelarrayshape)
    if verbose :
        print "Internal array type :", pixelarray.dtype.name
    
    return f2nimage(pixelarray, verbose = verbose)
706451
def tofits(outfilename, pixelarray, hdr = None, verbose = True):
    
    pixelarrayshape = pixelarray.shape
    if verbose :
        print "FITS export shape : (%i, %i)" % (pixelarrayshape[0], pixelarrayshape[1])

    if pixelarray.dtype.name == "bool":
        pixelarray = np.cast["uint8"](pixelarray)

    if os.path.isfile(outfilename):
        os.remove(outfilename)
    
    if hdr == None: # then a minimal header will be created 
        hdu = pyfits.PrimaryHDU(pixelarray.transpose())
    else: # this if else is probably not needed but anyway ...
        hdu = pyfits.PrimaryHDU(pixelarray.transpose(), hdr)

    hdu.writeto(outfilename)
    
    if verbose :
        print "Wrote %s" % outfilename
967761
def write_moc_fits_hdu(moc):
    

    # Ensure data are normalized.
    moc.normalize()

    # Determine whether a 32 or 64 bit column is required.
    if moc.order < 14:
        moc_type = np.int32
        col_type = 'J'
    else:
        moc_type = np.int64
        col_type = 'K'

    # Convert to the NUNIQ value which guarantees that one of the
    # top two bits is set so that the order of the value can be
    # determined.
    nuniq = []
    for (order, cells) in moc:
        uniq_prefix = 4 * (4 ** order)
        for npix in cells:
            nuniq.append(npix + uniq_prefix)

    # Prepare the data, and sort into numerical order.
    nuniq = np.array(nuniq, dtype=moc_type)
    nuniq.sort()

    # Create the FITS file.
    col = fits.Column(name='UNIQ', format=col_type, array=nuniq)

    cols = fits.ColDefs([col])
    rec = fits.FITS_rec.from_columns(cols)
    tbhdu = fits.BinTableHDU(rec)

    # Mandatory Keywords.
    tbhdu.header['PIXTYPE'] = 'HEALPIX'
    tbhdu.header['ORDERING'] = 'NUNIQ'
    tbhdu.header['COORDSYS'] = 'C'
    tbhdu.header['MOCORDER'] = moc.order
    tbhdu.header.comments['PIXTYPE'] = 'HEALPix magic code'
    tbhdu.header.comments['ORDERING'] = 'NUNIQ coding method'
    tbhdu.header.comments['COORDSYS'] = 'ICRS reference frame'
    tbhdu.header.comments['MOCORDER'] = 'MOC resolution (best order)'

    # Optional Keywords.
    tbhdu.header['MOCTOOL'] = 'PyMOC ' + version
    tbhdu.header.comments['MOCTOOL'] = 'Name of MOC generator'
    if moc.type is not None:
        tbhdu.header['MOCTYPE'] = moc.type
        tbhdu.header.comments['MOCTYPE'] = 'Source type (IMAGE or CATALOG)'
    if moc.id is not None:
        tbhdu.header['MOCID'] = moc.id
        tbhdu.header.comments['MOCID'] = 'Identifier of the collection'
    if moc.origin is not None:
        tbhdu.header['ORIGIN'] = moc.origin
        tbhdu.header.comments['ORIGIN'] = 'MOC origin'
    tbhdu.header['DATE'] = datetime.utcnow().replace(
        microsecond=0).isoformat()
    tbhdu.header.comments['DATE'] = 'MOC creation date'
    if moc.name is not None:
        tbhdu.header['EXTNAME'] = moc.name
        tbhdu.header.comments['EXTNAME'] = 'MOC name'

    return tbhdu
478960
public static Instance readInstance(File f) {
        try (Reader in = makeIn(f)) {
            return readInstance(in);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
135947
def load(hdfs_path, **kwargs):
    
    m, _ = common.parse_mode(kwargs.get("mode", "r"))
    if m != "r":
        raise ValueError("opening mode must be readonly")
    with open(hdfs_path, **kwargs) as fi:
        data = fi.read()
    fi.fs.close()
    return data
264367
def readbdfv1(filename, bdfext='.bdf', bhfext='.bhf'):
    
    return header.readbhfv1(filename, True, bdfext, bhfext)
866084
def cl_input(fname, unpack=True, anonpipe=True):
    
    try:
        manager = _get_storage_manager(fname)
    except ValueError:
        return fname

    return manager.cl_input(fname, unpack, anonpipe)
1168088
def save_fits(self, data, name):
        
        data = data.reshape(1, 1, data.shape[0], data.shape[0])
        new_file = pyfits.PrimaryHDU(data,self.img_hdu_list[0].header)
        new_file.writeto("{}".format(name), overwrite=True)
782096
def load_fits(self):
        '''
        

        '''

        log.info("Loading FITS file for %d." % (self.ID))
        with pyfits.open(self.fitsfile) as f:

            # Params and long cadence data
            self.loaded = True
            self.is_parent = False
            try:
                self.X1N = f[2].data['X1N']
            except KeyError:
                self.X1N = None
            self.aperture = f[3].data
            self.aperture_name = f[1].header['APNAME']
            try:
                self.bkg = f[1].data['BKG']
            except KeyError:
                self.bkg = 0.
            self.bpad = f[1].header['BPAD']
            self.cbv_minstars = []
            self.cbv_num = f[1].header.get('CBVNUM', 1)
            self.cbv_niter = f[1].header['CBVNITER']
            self.cbv_win = f[1].header['CBVWIN']
            self.cbv_order = f[1].header['CBVORD']
            self.cadn = f[1].data['CADN']
            self.cdivs = f[1].header['CDIVS']
            self.cdpp = f[1].header['CDPP']
            self.cdppr = f[1].header['CDPPR']
            self.cdppv = f[1].header['CDPPV']
            self.cdppg = f[1].header['CDPPG']
            self.cv_min = f[1].header['CVMIN']
            self.fpix = f[2].data['FPIX']
            self.pixel_images = [f[4].data['STAMP1'],
                                 f[4].data['STAMP2'], f[4].data['STAMP3']]
            self.fraw = f[1].data['FRAW']
            self.fraw_err = f[1].data['FRAW_ERR']
            self.giter = f[1].header['GITER']
            self.gmaxf = f[1].header.get('GMAXF', 200)
            self.gp_factor = f[1].header['GPFACTOR']
            try:
                self.hires = f[5].data
            except:
                self.hires = None
            self.kernel_params = np.array([f[1].header['GPWHITE'],
                                           f[1].header['GPRED'],
                                           f[1].header['GPTAU']])
            try:
                self.kernel = f[1].header['KERNEL']
                self.kernel_params = np.append(
                    self.kernel_params,
                    [f[1].header['GPGAMMA'],
                     f[1].header['GPPER']])
            except KeyError:
                self.kernel = 'Basic'
            self.pld_order = f[1].header['PLDORDER']
            self.lam_idx = self.pld_order
            self.leps = f[1].header['LEPS']
            self.mag = f[0].header['KEPMAG']
            self.max_pixels = f[1].header['MAXPIX']
            self.model = self.fraw - f[1].data['FLUX']
            self.nearby = []
            for i in range(99):
                try:
                    ID = f[1].header['NRBY%02dID' % (i + 1)]
                    x = f[1].header['NRBY%02dX' % (i + 1)]
                    y = f[1].header['NRBY%02dY' % (i + 1)]
                    mag = f[1].header['NRBY%02dM' % (i + 1)]
                    x0 = f[1].header['NRBY%02dX0' % (i + 1)]
                    y0 = f[1].header['NRBY%02dY0' % (i + 1)]
                    self.nearby.append(
                        {'ID': ID, 'x': x, 'y': y,
                         'mag': mag, 'x0': x0, 'y0': y0})
                except KeyError:
                    break
            self.neighbors = []
            for c in range(99):
                try:
                    self.neighbors.append(f[1].header['NEIGH%02d' % (c + 1)])
                except KeyError:
                    break
            self.oiter = f[1].header['OITER']
            self.optimize_gp = f[1].header['OPTGP']
            self.osigma = f[1].header['OSIGMA']
            self.planets = []
            for i in range(99):
                try:
                    t0 = f[1].header['P%02dT0' % (i + 1)]
                    per = f[1].header['P%02dPER' % (i + 1)]
                    dur = f[1].header['P%02dDUR' % (i + 1)]
                    self.planets.append((t0, per, dur))
                except KeyError:
                    break
            self.quality = f[1].data['QUALITY']
            self.saturated = f[1].header['SATUR']
            self.saturation_tolerance = f[1].header['SATTOL']
            self.time = f[1].data['TIME']
            self._norm = np.array(self.fraw)

            # Chunk arrays
            self.breakpoints = []
            self.cdpp_arr = []
            self.cdppv_arr = []
            self.cdppr_arr = []
            for c in range(99):
                try:
                    self.breakpoints.append(f[1].header['BRKPT%02d' % (c + 1)])
                    self.cdpp_arr.append(f[1].header['CDPP%02d' % (c + 1)])
                    self.cdppr_arr.append(f[1].header['CDPPR%02d' % (c + 1)])
                    self.cdppv_arr.append(f[1].header['CDPPV%02d' % (c + 1)])
                except KeyError:
                    break
            self.lam = [[f[1].header['LAMB%02d%02d' % (c + 1, o + 1)]
                        for o in range(self.pld_order)]
                        for c in range(len(self.breakpoints))]
            if self.model_name == 'iPLD':
                self.reclam = [[f[1].header['RECL%02d%02d' % (c + 1, o + 1)]
                               for o in range(self.pld_order)]
                               for c in range(len(self.breakpoints))]

            # Masks
            self.badmask = np.where(self.quality & 2 ** (QUALITY_BAD - 1))[0]
            self.nanmask = np.where(self.quality & 2 ** (QUALITY_NAN - 1))[0]
            self.outmask = np.where(self.quality & 2 ** (QUALITY_OUT - 1))[0]
            self.recmask = np.where(self.quality & 2 ** (QUALITY_REC - 1))[0]
            self.transitmask = np.where(
                self.quality & 2 ** (QUALITY_TRN - 1))[0]

            # CBVs
            self.XCBV = np.empty((len(self.time), 0))
            for i in range(99):
                try:
                    self.XCBV = np.hstack(
                        [self.XCBV,
                         f[1].data['CBV%02d' % (i + 1)].reshape(-1, 1)])
                except KeyError:
                    break

        # These are not stored in the fits file; we don't need them
        self.saturated_aperture_name = None
        self.apertures = None
        self.Xpos = None
        self.Ypos = None
        self.fpix_err = None
        self.parent_model = None
        self.lambda_arr = None
        self.meta = None
        self._transit_model = None
        self.transit_depth = None
230201
def extract_images_from_tscube(infile, outfile):
    
    inhdulist = fits.open(infile)
    wcs = pywcs.WCS(inhdulist[0].header)
    map_shape = inhdulist[0].data.shape

    t_eng = Table.read(infile, "EBOUNDS")
    t_scan = Table.read(infile, "SCANDATA")
    t_fit = Table.read(infile, "FITDATA")

    n_ebin = len(t_eng)
    energies = np.ndarray((n_ebin + 1))
    energies[0:-1] = t_eng["E_MIN"]
    energies[-1] = t_eng["E_MAX"][-1]

    cube_shape = (n_ebin, map_shape[1], map_shape[0])

    wcs_cube = wcs_utils.wcs_add_energy_axis(wcs, energies)

    outhdulist = [inhdulist[0], inhdulist["EBOUNDS"]]

    FIT_COLNAMES = ['FIT_TS', 'FIT_STATUS', 'FIT_NORM',
                    'FIT_NORM_ERR', 'FIT_NORM_ERRP', 'FIT_NORM_ERRN']
    SCAN_COLNAMES = ['TS', 'BIN_STATUS', 'NORM', 'NORM_UL',
                     'NORM_ERR', 'NORM_ERRP', 'NORM_ERRN', 'LOGLIKE']

    for c in FIT_COLNAMES:
        data = t_fit[c].data.reshape(map_shape)
        hdu = fits.ImageHDU(data, wcs.to_header(), name=c)
        outhdulist.append(hdu)

    for c in SCAN_COLNAMES:
        data = t_scan[c].data.swapaxes(0, 1).reshape(cube_shape)
        hdu = fits.ImageHDU(data, wcs_cube.to_header(), name=c)
        outhdulist.append(hdu)

    hdulist = fits.HDUList(outhdulist)
    hdulist.writeto(outfile, clobber=True)
    return hdulist
693157
def mim2fits(mimfile, fitsfile):
    
    region = Region.load(mimfile)
    region.write_fits(fitsfile, moctool='MIMAS {0}-{1}'.format(__version__, __date__))
    logging.info("Converted {0} -> {1}".format(mimfile, fitsfile))
    return
660626
def load_model(f, format=None, load_external_data=True):  # type: (Union[IO[bytes], Text], Optional[Any], bool) -> ModelProto
    '''
    
    '''
    s = _load_bytes(f)
    model = load_model_from_string(s, format=format)

    if load_external_data:
        model_filepath = _get_file_path(f)
        if model_filepath:
            base_dir = os.path.dirname(model_filepath)
            load_external_data_for_model(model, base_dir)

    return model
257951
def read_bytes(fd, byte_order, dtype, count):
    """"""
    return numpy.fromfile(fd, byte_order+dtype[-1], count).tostring()
1167146
def file_reader(self):
        """"""
        self.update_config()
        path = self.config['path']
        fourcc = self.metadata.get('fourcc')
        xlen, ylen = self.metadata.image_size()
        # set bits per pixel and component dimensions
        if fourcc in ('BGR[24]',):
            bpp = 24
            Y_shape = (ylen, xlen)
        elif fourcc in ('RGB[24]',):
            bpp = 24
            Y_shape = (ylen, xlen, 3)
        elif fourcc in ('IYU2',):
            bpp = 24
            Y_shape = (ylen, xlen, 1)
            UV_shape = (ylen, xlen)
        elif fourcc in ('UYVY', 'UYNV', 'Y422', 'HDYC', 'YVYU', 'YUYV',
                        'YV16', 'YUY2', 'YUNV', 'V422'):
            bpp = 16
            Y_shape = (ylen, xlen, 1)
            UV_shape = (ylen, xlen // 2)
        elif fourcc in ('IYUV', 'I420', 'YV12'):
            bpp = 12
            Y_shape = (ylen, xlen, 1)
            UV_shape = (ylen // 2, xlen // 2)
        elif fourcc in ('YVU9',):
            bpp = 9
            Y_shape = (ylen, xlen, 1)
            UV_shape = (ylen // 4, xlen // 4)
        else:
            self.logger.critical("Can't open %s files", fourcc)
            return
        bytes_per_frame = (xlen * ylen * bpp) // 8
        zlen = os.path.getsize(path) // bytes_per_frame
        if zlen < 1:
            self.logger.critical("Zero length file %s", path)
            return
        # set raw array slice parameters
        if fourcc in ('BGR[24]',):
            Y_slice = ((2, None, 3), (1, None, 3), (0, None, 3))
            UV_slice = None
        elif fourcc in ('RGB[24]',):
            Y_slice = ((0, None, 1),)
            UV_slice = None
        elif fourcc in ('IYU2',):
            Y_slice = ((1, None, 3),)
            UV_slice = ((0, None, 3), (2, None, 3))
        elif fourcc in ('UYVY', 'UYNV', 'Y422', 'HDYC'):
            # packed format, UYVY order
            Y_slice = ((1, None, 2),)
            UV_slice = ((0, None, 4), (2, None, 4))
        elif fourcc in ('YVYU',):
            # packed format, YVYU order
            Y_slice = ((0, None, 2),)
            UV_slice = ((3, None, 4), (1, None, 4))
        elif fourcc in ('YUYV', 'YUY2', 'YUNV', 'V422'):
            # packed format, YUYV order
            Y_slice = ((0, None, 2),)
            UV_slice = ((1, None, 4), (3, None, 4))
        elif fourcc in ('IYUV', 'I420'):
            # planar format, YUV order
            Y_size = xlen * ylen
            UV_size = UV_shape[0] * UV_shape[1]
            Y_slice = ((0,                Y_size,                 1),)
            UV_slice = ((Y_size,           Y_size + UV_size,       1),
                        (Y_size + UV_size, Y_size + (UV_size * 2), 1))
        elif fourcc in ('YV16', 'YV12', 'YVU9'):
            # planar format, YVU order
            Y_size = xlen * ylen
            UV_size = UV_shape[0] * UV_shape[1]
            Y_slice = ((0,                Y_size,                 1),)
            UV_slice = ((Y_size + UV_size, Y_size + (UV_size * 2), 1),
                        (Y_size,           Y_size + UV_size,       1))
        else:
            self.logger.critical("Can't open %s files", fourcc)
            return
        # set frame type
        if fourcc in ('BGR[24]', 'RGB[24]'):
            self.Y_type = 'RGB'
        else:
            self.Y_type = 'Y'
            self.UV_type = 'CbCr'
        file_frame = 0
        direction = 1
        with io.open(path, 'rb', 0) as raw_file:
            while True:
                self.update_config()
                if file_frame >= zlen:
                    if self.config['looping'] == 'off':
                        break
                    elif self.config['looping'] == 'repeat':
                        file_frame = 0
                        raw_file.seek(0)
                    else:
                        file_frame = zlen - 2
                        direction = -1
                elif file_frame < 0:
                    file_frame = 1
                    direction = 1
                if direction != 1:
                    raw_file.seek((direction - 1) * bytes_per_frame, io.SEEK_CUR)
                file_frame += direction
                raw_data = raw_file.read(bytes_per_frame)
                # convert to numpy arrays
                raw_array = numpy.frombuffer(raw_data, numpy.uint8)
                Y_data = []
                for start, end, step in Y_slice:
                    raw_data = raw_array[start:end:step]
                    Y_data.append(raw_data.reshape(Y_shape))
                if len(Y_data) > 1:
                    Y_data = numpy.dstack(Y_data)
                else:
                    Y_data = Y_data[0]
                if UV_slice:
                    UV_data = []
                    for start, end, step in UV_slice:
                        raw_data = raw_array[start:end:step]
                        UV_data.append(raw_data.reshape(UV_shape))
                    UV_data = numpy.dstack(UV_data)
                    # remove offset
                    UV_data = UV_data.astype(pt_float) - pt_float(128.0)
                else:
                    UV_data = None
                yield Y_data, UV_data
176180
def from_hdf5(cls, f):
        
        # TODO: this is duplicated code from PhaseSpacePosition
        if isinstance(f, str):
            import h5py
            f = h5py.File(f)

        pos = quantity_from_hdf5(f['pos'])
        vel = quantity_from_hdf5(f['vel'])

        time = None
        if 'time' in f:
            time = quantity_from_hdf5(f['time'])

        frame = None
        if 'frame' in f:
            g = f['frame']

            frame_mod = g.attrs['module']
            frame_cls = g.attrs['class']
            frame_units = [u.Unit(x.decode('utf-8')) for x in g['units']]

            if u.dimensionless_unscaled in frame_units:
                units = DimensionlessUnitSystem()
            else:
                units = UnitSystem(*frame_units)

            pars = dict()
            for k in g['parameters']:
                pars[k] = quantity_from_hdf5(g['parameters/'+k])

            exec("from {0} import {1}".format(frame_mod, frame_cls))
            frame_cls = eval(frame_cls)

            frame = frame_cls(units=units, **pars)

        potential = None
        if 'potential' in f:
            import yaml
            from ..potential.potential.io import from_dict
            _dict = yaml.load(f['potential'][()].decode('utf-8'))
            potential = from_dict(_dict)

        return cls(pos=pos, vel=vel, t=time,
                   frame=frame, potential=potential)
206753
def get_hash(fName, readSize, dire=pDir()):
    
    if not fileExists(fName, dire):
        return -1
    readSize = readSize * 1024 # bytes to be read
    fName = os.path.join(dire, fName) # name coupled with path
    with open(fName, 'rb') as f:
        size = os.path.getsize(fName)
        if size < readSize * 2:
            return -1
        data = f.read(readSize)
        f.seek(-readSize, os.SEEK_END)
        data += f.read(readSize)
    return md5(data).hexdigest()
461736
def open(self):
        

        if self.handle is None:
            self.handle = fits.open(self.fname, mode='readonly')

        if self.extn:
            if len(self.extn) == 1:
                hdu = self.handle[self.extn[0]]
            else:
                hdu = self.handle[self.extn[0],self.extn[1]]
        else:
            hdu = self.handle[0]
        if isinstance(hdu,fits.hdu.compressed.CompImageHDU):
            self.compress = True
        return hdu