Esempio n. 1
0
def test_fortranfiles_write():
    for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
        m = re.search("fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat", filename, re.I)
        if not m:
            raise RuntimeError("Couldn't match %s filename to regex" % filename)
        dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))

        counter = 0
        data = np.zeros(dims, dtype=m.group(1).replace("s", "<"))
        for k in range(dims[2]):
            for j in range(dims[1]):
                for i in range(dims[0]):
                    data[i, j, k] = counter
                    counter += 1
        tmpdir = tempfile.mkdtemp()
        try:
            testFile = path.join(tmpdir, path.basename(filename))
            f = FortranFile(testFile, "w", "<u4")
            f.write_record(data)
            f.close()
            originalfile = open(filename, "rb")
            newfile = open(testFile, "rb")
            assert_equal(originalfile.read(), newfile.read(), err_msg=filename)
            originalfile.close()
            newfile.close()
        finally:
            shutil.rmtree(tmpdir)
Esempio n. 2
0
def test_fortranfiles_mixed_record():
    filename = path.join(DATA_PATH, "fortran-mixed.dat")
    f = FortranFile(filename, 'r', '<u4')
    record = f.read_record('<i4,<f4,<i8,(2)<f8')
    assert_equal(record['f0'][0], 1)
    assert_allclose(record['f1'][0], 2.3)
    assert_equal(record['f2'][0], 4)
    assert_allclose(record['f3'][0], [5.6, 7.8])
Esempio n. 3
0
def read_association(listfile):
    assocFile = FortranFile(listfile, 'r')
    nassoc, columns = assocFile.read_ints()
    _tmp = (assocFile.read_reals(dtype=np.float32)).reshape((columns, nassoc)).transpose()
    assoc = pd.DataFrame(_tmp,
                         columns=['halo_id', 'level', 'halo_mass', 'gal_id', 'gal_mass'])

    assoc[['halo_id', 'level', 'gal_id']] =  assoc[['halo_id', 'level', 'gal_id']].astype(np.int32)
    return assoc
Esempio n. 4
0
def read_halo_list(listfile):
    haloFile = FortranFile(listfile, 'r')
    nhalos, columns = haloFile.read_ints()
    _tmp = (haloFile.read_reals(dtype=np.float32)).reshape((columns, nhalos)).transpose()
    halos = pd.DataFrame(_tmp,
                         columns=['id', 'level', 'mass', 'x', 'y', 'z', 'rvir'])
    halos[['id', 'level']] = halos[['id', 'level']].astype(int)

    return halos
def import_data_G(name="G_0.01965", folder_name="./OUT/"):

    f = FortranFile(folder_name+name, 'r')

    version = f.read_reals(dtype='f4')
    time, Ra, Ra_c, P, Ha, Di, Pr, Le = f.read_reals(dtype='f4')
    nradius, ntheta, nphi, azsym = f.read_reals(dtype='f4') # be careful, all of them are reals
    radius = f.read_reals(dtype='f4')
    theta = f.read_reals(dtype='f4') #colatitude

    phi = np.arange(1, int(nphi)+1)/nphi*2.*np.pi/azsym #longitude (not read from file!)

    Vr = np.empty([nphi, ntheta, nradius])
    Vt = np.empty_like(Vr)
    Vp = np.empty_like(Vr)
    Temperature = np.empty_like(Vr)
    Composition = np.empty_like(Vr)

    for ir in np.arange(nradius):
        for it in np.arange(ntheta):
            Vr[:,it,ir]=f.read_reals(dtype='f4')
            Vt[:,it,ir]=f.read_reals(dtype='f4')
            Vp[:,it,ir]=f.read_reals(dtype='f4')
            Composition[:,it,ir]=f.read_reals(dtype='f4')
            Temperature[:,it,ir]=f.read_reals(dtype='f4')
    
    return time, Ra, Ra_c, P, Ha, Di, Pr, Le, nradius, ntheta, nphi, azsym, radius, theta, phi, Vr, Vt, Vp, Temperature, Composition
Esempio n. 6
0
def test_fortran_eof_ok(tmpdir):
    filename = path.join(str(tmpdir), "scratch")
    np.random.seed(1)
    with FortranFile(filename, 'w') as f:
        f.write_record(np.random.randn(5))
        f.write_record(np.random.randn(3))
    with FortranFile(filename, 'r') as f:
        assert len(f.read_reals()) == 5
        assert len(f.read_reals()) == 3
        with pytest.raises(FortranEOFError):
            f.read_reals()
Esempio n. 7
0
def test_fortran_bogus_size(tmpdir):
    filename = path.join(str(tmpdir), "scratch")
    np.random.seed(1)
    with FortranFile(filename, 'w') as f:
        f.write_record(np.random.randn(5))
        f.write_record(np.random.randn(3))
    with open(filename, "w+b") as f:
        f.write(b"\xff\xff")
    with FortranFile(filename, 'r') as f:
        with pytest.raises(FortranFormattingError):
            f.read_reals()
Esempio n. 8
0
def read_two_body_gms(nb):
    f = FortranFile('V2b_AO_cholesky.mat', 'r')
    nd = f.read_record(dtype=np.int32)
    ng = f.read_record(dtype=np.int32)[1]
    L = np.zeros((nb, nb, ng))
    for ig in range(ng):
        X = f.read_reals(np.float)
        L[:, :, ig] = X.reshape((nb, nb))
    #L = L[:,:,:1]
    return 2.0 * np.tensordot(L, L, axes=((-1), (-1))).transpose(
        (0, 1, 3, 2)), L
Esempio n. 9
0
def write_output(output, i, type):
    # find difference between the start of the run and the current timestep,
    # and add the model timestep because the output routine used to be buggy
    minute_diff = get_timestep(i) - get_timestep(0) + get_deltim()
    file = path.join(
        args.output_directory, 'ipe_grid_' + type + '_params.' +
        new_timestamp(mdate, args.timestamp, minute_diff))
    print('writing out ' + file)
    # scipy FortranFile is nicer code than manually writing headers with numpy
    fout = FortranFile(file, 'w')
    for var in range(len(var_ext)):
        fout.write_record(output[var, :])
Esempio n. 10
0
def getdata(filenumber, variable, DIR=' '):

    if DIR == ' ':
        filename = 'Data/' + variable + '{:03d}'.format(filenumber) + '.dat'
    else:
        filename = DIR + variable + '{:03d}'.format(filenumber) + '.dat'

    file = FortranFile(filename, 'r')
    nx = file.read_ints(np.int32)[0]
    ny = file.read_ints(np.int32)[0]
    nz = file.read_ints(np.int32)[0]
    return file.read_reals(float).reshape((nz, ny, nx), order="C")
Esempio n. 11
0
def test_fortran_eof_broken_record(tmpdir):
    filename = path.join(str(tmpdir), "scratch")
    np.random.seed(1)
    with FortranFile(filename, 'w') as f:
        f.write_record(np.random.randn(5))
        f.write_record(np.random.randn(3))
    with open(filename, "ab") as f:
        f.truncate(path.getsize(filename) - 20)
    with FortranFile(filename, 'r') as f:
        assert len(f.read_reals()) == 5
        with pytest.raises(FortranFormattingError):
            f.read_reals()
Esempio n. 12
0
    def __read_phiaver(self, datadir, variables, aver_file_name,
                       n_vars, var_index, iter_list, l_h5=False):
        """
        Read the PHIAVG file
        Return the time, cylindrical r and z and raw data.
        """

        import os
        import numpy as np
        from scipy.io import FortranFile
        from .. import read

        # Read the data
        if l_h5:
            import h5py
            #
            # Not implemented
            #
        else:
            glob_dim = read.dim(datadir)
            nu=glob_dim.nx/2
            nv=glob_dim.nz

            dim = read.dim(datadir)
            if dim.precision == 'S':
                dtype = np.float32
            if dim.precision == 'D':
                dtype = np.float64

            # Prepare the raw data.
            raw_data = []
            t = []

            # Read records
            #path=os.path.join(datadir, aver_file_name)
            #print(path)
            file_id = FortranFile(os.path.join(datadir, aver_file_name))

            data1 = file_id.read_record(dtype='i4')
            nr_phiavg = data1[0]
            nz_phiavg = data1[1]
            nvars = data1[2]
            nprocz = data1[3]

            data2 = file_id.read_record(dtype=np.float64)
            t = data2[0]
            r_cyl = data2[1:nr_phiavg+1]
            z_cyl = data2[nr_phiavg+1:nr_phiavg+nz_phiavg+1]

            data3 = file_id.read_record(dtype=np.float64)
            raw_data = data3.reshape(nvars,nz_phiavg,nr_phiavg)

            return t, r_cyl, z_cyl, raw_data
Esempio n. 13
0
def read_fortran_FFTfield(infile):
    """
    Read a Half-Field with FFTW indexing from
    a Fortran Unformatted Binary file. The first
    entry is a single integer.
    """
    f=FortranFile(infile,'r')
    Ngrid=f.read_ints(dtype=np.int32)[0]
    print('Fortran file Ngrid='+str(Ngrid))
    dcr=f.read_reals(dtype=np.complex64)
    dcr=np.reshape(dcr,(Ngrid//2+1,Ngrid,Ngrid),order='F')
    dcr.dump(infile+'.pickle') # Save infile as a pickle
    return dcr
Esempio n. 14
0
def read_fbin(filename):
    ''' this reads each written binary instance itteratively'''
    f = FortranFile(filename, 'r')
    array = []
    while True:
        try:
            array.append(f.read_reals(dtype=np.float_))
        except TypeError:
            break
    #array = np.reshape(array, (nspecs,-1))

    f.close()
    return array
 def read_fortran_FFTfield(self):
       """
       Read a fortran binary file from FFTW assert all Nyquist
       entries to be real.
       """
       f=FortranFile(self.infile,'r')
       Ng=f.read_ints(dtype=np.int32)[0]
       print('Fortran file Ngrid='+str(Ng))
       if (Ng != self.Ngrid):
             print('Ngrid values are not equal!')
       dcr=f.read_reals(dtype=np.complex64)
       dcr=np.reshape(dcr,(Ng//2+1,Ng,Ng),order='F')
       return dcr
Esempio n. 16
0
def read_fbin(filename):
    ''' this reads each written binary instance itteratively'''
    f = FortranFile(filename, 'r')
    array = []
    while True:
        try: 
            array.append(f.read_reals(dtype=np.float_))
        except TypeError: 
            break
    #array = np.reshape(array, (nspecs,-1))
    
    f.close()
    return array
Esempio n. 17
0
 def read_patch(self, file, verbose=0):
     ''' Read particles in a patch '''
     name = file + '.peb'
     f = FortranFile(name, 'rb')
     fmt = f.readInts()
     if verbose > 2:
         print('ioformat:', fmt[0])
     dim = f.readInts()
     dim = dim[0:2]
     a = f.readReals()
     a = a.reshape(dim[1], dim[0]).transpose()
     idv = f.readInts()
     idx = idv[0:dim[0]]
     f.close()
     dict = {}
     for i in range(size(idx)):
         id = idx[i]
         dict[id] = {
             'p': a[i, 0:3],
             'v': a[i, 0:3],
             't': a[i, 6],
             's': a[i, 7],
             'w': a[i, 8]
         }
     return idx, dict
Esempio n. 18
0
def progenitor_probability(density=None, sfr=None, mass=None, redshift=None):
    """
    Return the progenitor fraction for input values.
    >>> progenitor_probability(redshift=0.4, mass=10.8)
    0.266751184855
    """
    density = np.nan if density is None else density
    sfr = np.nan if sfr is None else sfr
    mass = np.nan if mass is None else mass
    redshift = np.nan if redshift is None else redshift

    values = [density, sfr, mass, redshift]

    if values.count(np.nan) > 3:
        raise ValueError('Incorrect number of arguments')

    # Read datacube
    f = FortranFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fractions.dat'))
    dims = f.read_record(dtype=np.int32)
    data = f.read_record(dtype=np.float32)
    data_size = np.product(dims)
    n_galaxies = np.reshape(data[0:data_size], dims, order='F')
    n_spiral_progenitors = np.reshape(data[data_size:2*data_size], dims, order='F')
    bins = np.stack([np.reshape(f.read_record(dtype=np.float32), dims, order='F') for _ in range(dims.size)], axis=0)

    # Marginalise over dimensions that are not specified
    while np.nan in values:
        i = values.index(np.nan)
        dims = np.delete(dims, i)
        values.pop(i)
        weights = n_galaxies
        n_galaxies = np.sum(n_galaxies, axis=i)
        n_spiral_progenitors = np.sum(n_spiral_progenitors, axis=i)
        bins = np.delete(np.nanmean(bins, axis=i+1), i, axis=0)
    data_size = np.product(dims)

    n_galaxies = np.reshape(n_galaxies, data_size)
    n_spiral_progenitors = np.reshape(n_spiral_progenitors, data_size)

    # Only use bins where there are at least 4 galaxies
    pick = n_galaxies > 3

    # Calculate progenitor fractions
    with np.errstate(divide='ignore', invalid='ignore'):
        frac = np.true_divide(n_spiral_progenitors[pick], n_galaxies[pick])

    bins = np.array([np.reshape(bins[i], data_size)[pick] for i in range(len(values))]).transpose()

    # Do n-D linear interpolation to get progenitor fraction for input values
    return griddata(bins, frac, values, method='linear')
Esempio n. 19
0
    def export_unk(self, grid=[50, 50, 50]):
        '''
        Export the periodic part of BF in a real space grid for plotting with wannier90
        '''

        from scipy.io import FortranFile
        grids_coor, weights = periodic_grid(self.cell, grid, order='F')

        for k_id in range(self.num_kpts_loc):
            spin = '.1'
            if self.spin_up != None and self.spin_up == False: spin = '.2'
            kpt = self.cell.get_abs_kpts(self.kpt_latt_loc[k_id])
            ao = numint.eval_ao(self.cell, grids_coor, kpt=kpt)
            u_ao = np.einsum('x,xi->xi',
                             np.exp(-1j * np.dot(grids_coor, kpt)),
                             ao,
                             optimize=True)
            unk_file = FortranFile('UNK' + "%05d" % (k_id + 1) + spin, 'w')
            unk_file.write_record(
                np.asarray(
                    [grid[0], grid[1], grid[2], k_id + 1, self.num_bands_loc],
                    dtype=np.int32))
            mo_included = self.mo_coeff_kpts[k_id][:, self.band_included_list]
            u_mo = np.einsum('xi,in->xn', u_ao, mo_included, optimize=True)
            for band in range(len(self.band_included_list)):
                unk_file.write_record(
                    np.asarray(u_mo[:, band], dtype=np.complex128))
            unk_file.close()
Esempio n. 20
0
def revolver_to_unformatted(input_filename,
                            output_filename,
                            cosmology,
                            equal_weights=False,
                            zrange=None):
    # open numpy file
    cat = np.load(input_filename)

    if zrange is not None:
        zmin, zmax = zrange
        ind = (cat[:, 2] > zmin) & (cat[:, 2] < zmax)
        cat = cat[ind]

    # convert redshifts to distances
    dist = cosmology.ComovingDistance(cat[:, 2])
    x = dist * np.cos(cat[:, 1] * np.pi / 180) * np.cos(
        cat[:, 0] * np.pi / 180)
    y = dist * np.cos(cat[:, 1] * np.pi / 180) * np.sin(
        cat[:, 0] * np.pi / 180)
    z = dist * np.sin(cat[:, 1] * np.pi / 180)

    if not equal_weights:
        weight = cat[:, 3]
    else:
        weight = np.ones(len(cat))

    #write result to output file
    cout = np.c_[x, y, z, weight]
    nrows, ncols = np.shape(cout)
    f = FortranFile(output_filename, 'w')
    nrows, ncols = np.shape(cout)
    f.write_record(nrows)
    f.write_record(ncols)
    f.write_record(cout)
    f.close()
Esempio n. 21
0
    def export_unk(self, spin=0, ngrid=None):
        '''
        Export the periodic part of BF in a real space grid for plotting with wannier90
        '''

        from scipy.io import FortranFile
        if spin == 0:
            spin_str = '.1'
        else:
            spin_str = '.2'

        if ngrid is None:
            ngrid = self.ngrid.copy()
        else:
            ngrid = np.array(ngrid, dtype=np.int64)
            assert ngrid.shape[0] == 3, 'Wrong syntax for ngrid'
            assert np.alltrue(ngrid >= self.ngrid), "Minium FT grid size: (%d, %d, %d)" % \
                    (self.ngrid[0], self.ngrid[1], self.ngrid[2])

        for kpt in range(self.nkpts):
            unk_file = FortranFile('UNK' + "%05d" % (kpt + 1) + spin_str, 'w')
            unk_file.write_record(
                np.asarray(
                    [ngrid[0], ngrid[1], ngrid[2], kpt + 1, self.nbands],
                    dtype=np.int32))
            for band in range(self.nbands):
                unk = self.get_unk(spin=spin,
                                   kpt=kpt + 1,
                                   band=band + 1,
                                   ngrid=ngrid,
                                   norm=False,
                                   norm_c=False)
                unk = unk.T.flatten()
                unk_file.write_record(unk)
            unk_file.close()
Esempio n. 22
0
    def export_unk(self, grid=[50, 50, 50]):
        '''
		Export the periodic part of BF in a real space grid for plotting with wannier90
		'''

        from scipy.io import FortranFile
        grids_coor = general_grid(self.cell, grid)

        for k_id in range(self.num_kpts_loc):
            kpt = self.cell.get_abs_kpts(self.kpt_latt_loc[k_id])
            ao = numint.eval_ao(self.cell, grids_coor, kpt=kpt)
            u_ao = np.einsum('x,xi->xi',
                             np.exp(-1j * np.dot(grids_coor, kpt)),
                             ao,
                             optimize=True)
            unk_file = FortranFile('UNK0000' + str(k_id + 1) + '.1', 'w')
            unk_file.write_record(
                np.asarray(
                    [grid[0], grid[1], grid[2], k_id + 1, self.num_bands_loc],
                    dtype=np.int32))
            mo_included = np.dot(
                self.U[k_id],
                self.mo_coeff_kpts[k_id])[:, self.band_included_list]
            u_mo = np.einsum('xi,in->xn', u_ao, mo_included, optimize=True)
            for band in range(len(self.band_included_list)):
                unk_file.write_record(
                    np.asarray(u_mo[:, band], dtype=np.complex))
            unk_file.close()
Esempio n. 23
0
def fits_to_unformatted(
  input_filename, output_filename, omega_m
):
  # define cosmology
  cosmo = Cosmology(omega_m=omega_m)

  # open fits file
  with fits.open(input_filename) as hdul:
    cat = hdul[1].data

  # convert redshifts to distances
  dist = cosmo.ComovingDistance(cat['Z'])
  x = dist * np.sin(cat['DEC'] * np.pi / 180) * np.cos(cat['RA'] * np.pi / 180)
  y = dist * np.sin(cat['DEC'] * np.pi / 180) * np.sin(cat['RA'] * np.pi / 180)
  z = dist * np.cos(cat['DEC'] * np.pi / 180)

  # write result to output file
  cout = np.c_[x, y, z]
  nrows, ncols = np.shape(cout)
  f = FortranFile(output_filename, 'w')
  nrows, ncols = np.shape(cout)
  f.write_record(nrows)
  f.write_record(ncols)
  f.write_record(cout)
  f.close()
Esempio n. 24
0
    def get_circumcentres(self, radius_limit=1000):
        '''
        Find the centre of the circumspheres
        associated to an input catalogue of
        tetrahedra.
        '''

        print('Finding circumcentres of tetrahedra...')
        vertices = self.delaunay_triangulation()
        cenx, ceny, cenz, r = [], [], [], []

        sing = 0
        for tetra in vertices:
            x0, x1, x2, x3 = tetra
            A = []
            B = []
            A.append((x1 - x0).T)
            A.append((x2 - x0).T)
            A.append((x3 - x0).T)
            A = np.asarray(A)
            B = np.sum(A**2, axis=1)
            B = np.asarray(B)
            try:
                C = np.linalg.inv(A).dot(B)
            except:
                sing += 1
                continue
            centre = x0 + 0.5 * C
            radius = 0.5 * np.sqrt(np.sum(C**2))
            if radius < radius_limit:
                cenx.append(centre[0])
                ceny.append(centre[1])
                cenz.append(centre[2])
                r.append(radius)

        print('{} singular matrices found.'.format(sing))

        cenx = np.asarray(cenx)
        ceny = np.asarray(ceny)
        cenz = np.asarray(cenz)

        cenx = cenx.reshape(len(cenx), 1)
        ceny = ceny.reshape(len(ceny), 1)
        cenz = cenz.reshape(len(cenz), 1)

        cout = np.hstack([cenx, ceny, cenz])

        print('{} centres found.'.format(len(cout)))

        f = FortranFile(self.centres_file, 'w')
        nrows, ncols = np.shape(cout)
        f.write_record(nrows)
        f.write_record(ncols)
        f.write_record(cout)
        f.close()

        self.centres = cout

        return
Esempio n. 25
0
def read_galaxy_list(listfile):
    galFile = FortranFile(listfile, 'r')
    print(listfile)
    ngal, columns = galFile.read_ints()
    _tmp = (galFile.read_reals(dtype=np.float32)).reshape((columns, ngal)).transpose()
    galaxies = pd.DataFrame(_tmp,
                            columns=['id', 'vt', 'dvz', 'dvr', 'dvtheta', 'mass', 'x', 'y', 'z'])
    galaxies.id.astype(int)

    galaxies['sigma'] = 1/3.*np.sqrt(galaxies.dvz**2 + galaxies.dvtheta**2 + galaxies.dvr**2)
    galaxies['sigmaoverv'] = galaxies.sigma / galaxies.vt
    galaxies['elliptic'] = galaxies.sigmaoverv > 1.5
    galaxies['spiral'] = galaxies.sigmaoverv < 0.8

    return galaxies
Esempio n. 26
0
def read_unformatted(filename):
    '''
    Reads an unformatted Fortran 90 files as
    a numpy array.

    Parameters:  filename: str
                 Name of the unformatted file.
    '''

    fin = FortranFile(filename, 'r')
    nrows = fin.read_ints()[0]
    ncols = fin.read_ints()[0]
    data = fin.read_reals(dtype=np.float64).reshape(nrows, ncols)

    return data, nrows, ncols
Esempio n. 27
0
    def get_inverse_covmat(self):
        #read full covmat
        f = FortranFile(self.cov_file, 'r')
        covmat = f.read_reals(dtype=float).reshape(
            (self.nbin_hi, self.nbin_hi))
        for i in range(self.nbin_hi):
            for j in range(i, self.nbin_hi):
                covmat[i, j] = covmat[j, i]

        #select relevant covmat
        if self.use_tt and not (self.use_ee) and not (self.use_te):
            #just tt
            bin_no = self.nbintt_hi
            start = 0
            end = start + bin_no
            cov = covmat[start:end, start:end]
        elif not (self.use_tt) and not (self.use_ee) and self.use_te:
            #just te
            bin_no = self.nbinte
            start = self.nbintt_hi
            end = start + bin_no
            cov = covmat[start:end, start:end]
        elif not (self.use_tt) and self.use_ee and not (self.use_te):
            #just ee
            bin_no = self.nbinee
            start = self.nbintt_hi + self.nbinte
            end = start + bin_no
            cov = covmat[start:end, start:end]
        elif self.use_tt and self.use_ee and self.use_te:
            #use all
            bin_no = self.nbin_hi
            cov = covmat
        else:
            print("not implemented")

        #invert high ell covariance matrix (cholesky decomposition should be faster)
        fisher = scipy.linalg.cho_solve(scipy.linalg.cho_factor(cov),
                                        np.identity(bin_no))
        fisher = fisher.transpose()

        if self.use_low_ell_bins:
            bin_no += self.nbintt_low_ell
            inv_covmat_with_lo = np.zeros(shape=(bin_no, bin_no))
            inv_covmat_with_lo[0:2, 0:2] = np.diag(1. / self.X_sig_low_ell**2)
            inv_covmat_with_lo[2:, 2:] = fisher
            fisher = inv_covmat_with_lo

        return fisher
Esempio n. 28
0
    def from_file(filename: str) -> object:
        """
        Reads the UNK data from file.

        Args:
            filename (str): path to UNK file to read

        Returns:
            Unk object
        """
        input_data = []
        with FortranFile(filename, "r") as f:
            *ng, ik, nbnd = f.read_ints()
            for _ in range(nbnd):
                input_data.append(
                    # when reshaping need to specify ordering as fortran
                    f.read_record(np.complex128).reshape(ng, order="F"))
            try:
                for _ in range(nbnd):
                    input_data.append(
                        f.read_record(np.complex128).reshape(ng, order="F"))
                is_noncollinear = True
            except FortranEOFError:
                is_noncollinear = False

        # mypy made me create an extra variable here >:(
        data = np.array(input_data, dtype=np.complex128)

        # spinors are interwoven, need to separate them
        if is_noncollinear:
            temp_data = np.empty((nbnd, 2, *ng), dtype=np.complex128)
            temp_data[:, 0, :, :, :] = data[::2, :, :, :]
            temp_data[:, 1, :, :, :] = data[1::2, :, :, :]
            return Unk(ik, temp_data)
        return Unk(ik, data)
Esempio n. 29
0
def test_fortranfiles_read():
    for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
        m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
        if not m:
            raise RuntimeError("Couldn't match %s filename to regex" % filename)

        dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))

        dtype = m.group(1).replace('s', '<')

        f = FortranFile(filename, 'r', '<u4')
        data = f.read_record(dtype=dtype).reshape(dims, order='F')
        f.close()

        expected = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
        assert_equal(data, expected)
Esempio n. 30
0
    def __init__(self, resroot, filename="LUM_SF.bin", NT_TOT=107, NBMU=51):
        """ Filename defined by -OSOAA.ResFile.vsZ:
            This optional file provides the radiance field (I,Q,U) versus
            the altitude or depth, for the given relative azimuth angle
            (-OSOAA.View.Phi) and for the given viewing zenith angle
            (-OSOAA.View.VZA).
            resroot     OSOAA results root directory.
            filename    Filename to look for the results.
            I           I Stokes parameters Fourier components. The array
                        is I(LEVEL;WAVE_NUMBER)
            Q           Q Stokes parameters Fourier components. The array
                        is Q(LEVEL;WAVE_NUMBER)
            U           U Stokes parameters Fourier components. The array
                        is U(LEVEL;WAVE_NUMBER)
            """
        # We read the fortran binary file
        with FortranFile(resroot + "/Advanced_outputs/" + filename,
                         "r") as file:
            tmp = file.read_reals()

        # Reshape the array into 3 array for I, Q and U
        tmp = tmp.reshape(3, int(tmp.size / 3))
        Itmp = tmp[0, :]
        Qtmp = tmp[1, :]
        Utmp = tmp[2, :]

        # We reshape the array for the number of levels
        Itmp = Itmp.reshape(int(Itmp.size / (NT_TOT + 1)), NT_TOT + 1)
        Qtmp = Qtmp.reshape(int(Qtmp.size / (NT_TOT + 1)), NT_TOT + 1)
        Utmp = Utmp.reshape(int(Utmp.size / (NT_TOT + 1)), NT_TOT + 1)

        # We transpose the result to get and array of ARRAY[LEVEL:ANGLE]
        self.I = Itmp.transpose()
        self.Q = Qtmp.transpose()
        self.U = Utmp.transpose()
Esempio n. 31
0
def get_nframes(dcdfilename):
    """
    Get the number of frames in the dcd file
    """
    with FortranFile(dcdfilename, 'r') as f:
        nframes = f.read_ints()[1]
    return nframes
Esempio n. 32
0
def test_fortran_eof_multidimensional(tmpdir):
    filename = path.join(str(tmpdir), "scratch")
    n, m, q = 3, 5, 7
    dt = np.dtype([("field", np.float64, (n, m))])
    a = np.zeros(q, dtype=dt)
    with FortranFile(filename, 'w') as f:
        f.write_record(a[0])
        f.write_record(a)
        f.write_record(a)
    with open(filename, "ab") as f:
        f.truncate(path.getsize(filename) - 20)
    with FortranFile(filename, 'r') as f:
        assert len(f.read_record(dtype=dt)) == 1
        assert len(f.read_record(dtype=dt)) == q
        with pytest.raises(FortranFormattingError):
            f.read_record(dtype=dt)
Esempio n. 33
0
def read_fortran_file(filename: Union[str, Path]) -> np.ndarray:
    with FortranFile(str(filename), 'r') as f:
        [t, gamma] = f.read_reals('f4')
        [nx, ny, nvar, nstep] = f.read_ints('i')
        dat = f.read_reals('f4')
    dat = np.array(dat)
    return dat.reshape(nvar, ny, nx)
Esempio n. 34
0
        def readfun(filename):
            '''
            reads unformatted fortran files 
            ''' 
            f = FortranFile('Outputs/'+filename, 'r')
            names = ''.join(f.read_reals('c'))
            data = []
            while True:
                    try:
                        data.append(f.read_reals(dtype=np.float_))
                    except TypeError:
                        break
                #array = np.reshape(array, (nspecs,-1))

            f.close()
            return [names.replace(' ',''),np.array(data)]
Esempio n. 35
0
    def from_file(self, fname):
        with FortranFile(fname, 'r') as f:
            self.header = ''.join(map(str, f.read_record('c')))
            self.nbnds = f.read_ints()[0]
            self.nbnds_excl = f.read_ints()[0]
            self.bands_excl = f.read_ints()
            self.dlv = f.read_reals().reshape((3, 3), order='F')
            self.rlv = f.read_reals().reshape((3, 3), order='F')
            self.nkpts = f.read_ints()[0]
            self.grid_dims = f.read_ints()
            self.kpoints = f.read_reals().reshape((-1, 3))
            self.nntot = f.read_ints()[0]
            self.nwann = f.read_ints()[0]
            self.chkpt = f.read_record('c')
            self.disentanglement = bool(f.read_ints()[0])

            if self.disentanglement:
                self.omega_invariant = f.read_reals()[0]
                self.windows = f.read_ints().reshape((self.nbnds, self.nkpts),
                                                     order='F').transpose
                f.read_ints()
                self.umat_opt = np.transpose(
                    f.read_reals().view(complex).reshape(
                        (self.nbnds, self.nwann, self.nkpts), order='F'),
                    axes=(2, 0, 1))

            self.umat = np.transpose(f.read_reals().view(complex).reshape(
                (self.nwann, self.nwann, self.nkpts), order='F'),
                                     axes=(2, 0, 1))
            self.mmat = np.transpose(f.read_reals().view(complex).reshape(
                (self.nwann, self.nwann, self.nntot, self.nkpts), order='F'),
                                     axes=(3, 2, 0, 1))
            self.wannier_centers = f.read_reals().reshape((-1, 3))
            self.wannier_spreads = f.read_reals()
Esempio n. 36
0
def test_fortranfiles_read():
    for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
        m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
        if not m:
            raise RuntimeError("Couldn't match %s filename to regex" % filename)

        dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))

        dtype = m.group(1).replace('s', '<')

        f = FortranFile(filename, 'r', '<u4')
        data = f.read_record(dtype=dtype).reshape(dims, order='F')
        f.close()

        expected = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
        assert_equal(data, expected)
Esempio n. 37
0
class SIMRA3DMeshReader(SIMRAMeshReader):

    reader_name = "SIMRA-3D"

    filename: Path
    mesh: FortranFile
    nodeshape: Shape

    @classmethod
    def applicable(cls, filename: Path) -> bool:
        _, u4_type = dtypes(config.input_endianness)
        try:
            with FortranFile(filename, 'r', header_dtype=u4_type) as f:
                assert f._read_size() == 6 * 4
            return True
        except:
            return False

    def __init__(self, filename: Path):
        super().__init__()
        self.filename = filename

    def __enter__(self):
        self.mesh = FortranFile(self.filename, 'r', header_dtype=self.u4_type).__enter__()
        with save_excursion(self.mesh._fp):
            _, _, imax, jmax, kmax, _ = self.mesh.read_ints(self.u4_type)
        if config.fix_orientation:
            self.nodeshape = (jmax, imax, kmax)
        else:
            self.nodeshape = (imax, jmax, kmax)
        return self

    def __exit__(self, *args):
        self.mesh.__exit__(*args)

    @cache(1)
    def patch(self) -> Patch:
        i, j, k = self.nodeshape
        return Patch(('geometry',), StructuredTopology((j-1, i-1, k-1), celltype=Hex()))

    @cache(1)
    def nodes(self) -> Array2D:
        with save_excursion(self.mesh._fp):
            fortran_skip_record(self.mesh)
            nodes = transpose(self.mesh.read_reals(self.f4_type), self.nodeshape)
        nodes = ensure_native(nodes)
        return translate(self.filename.parent, nodes)
Esempio n. 38
0
def read_cproj_NormalCar(inf='NormalCAR', save_cproj=True):
    '''
    Read NormalCAR of VASP output, which contains the coefficients of the PAW
    projector functions.

    Data stored in NormalCAR:

        WRITE(IU) LMDIM,WDES%NIONS,WDES%NRSPINORS
        WRITE(IU) CQIJ(1:LMDIM,1:LMDIM,1:WDES%NIONS,1:WDES%NRSPINORS) 
        WRITE(IU) WDES%NPROD, WDES%NPRO, WDES%NTYP
        DO NT = 1,  WDES%NTYP
          WRITE(IU) WDES%LMMAX(NT), WDES%NITYP(NT) 
        END DO
        DO ISPIN=1,WDES%ISPIN
          DO NK=1,WDES%NKPTS
            DO N=1,WDES%NB_TOT 
              WRITE(IU) CPROJ(1:WDES1%NPRO_TOT)
            END DO 
          END DO 
        END DO 
    '''
    from scipy.io import FortranFile

    ncr = FortranFile(inf, 'r')

    # rec1 = ncr.read_record(dtype=np.int32)
    rec1 = ncr.read_ints()
    lmdim, nions, nrspinors = rec1

    # rec2 = ncr.read_record(dtype=np.complex)
    cqij = np.array(ncr.read_reals()).reshape((lmdim, lmdim, nions, nrspinors),
                                              order='F')

    rec3 = ncr.read_ints()
    nprod, npro, ntyp = rec3
    # lmmax, natoms for each type of elements
    lmmax_per_typ = np.array([ncr.read_ints() for ii in range(ntyp)])

    cproj = []
    while True:
        try:
            rec = ncr.read_record(dtype=np.complex)
            cproj.append(rec)
        except:
            break
    cproj = np.array(cproj)
    if save_cproj:
        np.save('cproj', cproj)

    ncr.close()

    return cproj
Esempio n. 39
0
    def __init__(self,seedname):
        seedname=seedname.strip()
        FIN=FortranFile(seedname+'.chk','r')
        readint   = lambda : FIN.read_record('i4')
        readfloat = lambda : FIN.read_record('f8')
        def readcomplex():
            a=readfloat()
            return a[::2]+1j*a[1::2]

        print ( 'Reading restart information from file '+seedname+'.chk :')
        self.comment=readstr(FIN) 
        self.num_bands          = readint()[0]
        num_exclude_bands       = readint()[0]
        self.exclude_bands      = readint()
        assert  len(self.exclude_bands)==num_exclude_bands
        self.real_lattice=readfloat().reshape( (3 ,3),order='F')
        self.recip_lattice=readfloat().reshape( (3 ,3),order='F')
        assert np.linalg.norm(self.real_lattice.dot(self.recip_lattice.T)/(2*np.pi)-np.eye(3)) < 1e-14
        self.num_kpts = readint()[0]
        self.mp_grid  = readint()
        assert len(self.mp_grid)==3
        assert self.num_kpts==np.prod(self.mp_grid)
        self.kpt_latt=readfloat().reshape( (self.num_kpts,3))
        self.nntot    = readint()[0]
        self.num_wann = readint()[0]
        self.checkpoint=readstr(FIN)
        self.have_disentangled=bool(readint()[0])
        if self.have_disentangled:
            self.omega_invariant=readfloat()[0]
            lwindow=np.array( readint().reshape( (self.num_kpts,self.num_bands)),dtype=bool )
            ndimwin=readint()
            u_matrix_opt=readcomplex().reshape( (self.num_kpts,self.num_wann,self.num_bands) )
            self.win_min = np.array( [np.where(lwin)[0].min() for lwin in lwindow] )
            self.win_max = np.array( [wm+nd for wm,nd in zip(self.win_min,ndimwin)]) 
        else:
            self.win_min = np.array( [0]*self.num_kpts )
            self.win_max = np.array( [self.num_wann]*self.num_kpts) 
            
        u_matrix=readcomplex().reshape( (self.num_kpts,self.num_wann,self.num_wann) )
        m_matrix=readcomplex().reshape( (self.num_kpts,self.nntot,self.num_wann,self.num_wann) )
        if self.have_disentangled:
            self.v_matrix=[u.dot(u_opt[:,:nd]) for u,u_opt,nd in 
                                    zip(u_matrix,u_matrix_opt,ndimwin)]
        else:
            self.v_matrix=[u  for u in u_matrix ] 
        self.wannier_centres=readfloat().reshape((self.num_wann,3))
        self.wannier_spreads=readfloat().reshape((self.num_wann))
Esempio n. 40
0
def test_fortranfiles_read():
    for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
        m = re.search('fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
        if not m:
            raise RuntimeError("Couldn't match %s filename to regex" % filename)
        dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))

        f = FortranFile(filename, 'r', '<u4')
        data = f.read_record(dtype=m.group(1)).reshape(dims)
        f.close()

        counter = 0
        for k in range(dims[2]):
            for j in range(dims[1]):
                for i in range(dims[0]):
                    assert_equal(counter, data[i,j,k])
                    counter += 1
Esempio n. 41
0
 def __init__(self, filename, itype, newResults=[]):
     self.mesh = Mesh()
     self.newResults = newResults
     #    print(self.newResults)
     self.filename = filename
     self.itype = itype
     self.intType = 'i4'
     self.f = FortranFile(filename)
     try:
         self.defGeneralVars()
     except ValueError:
         self.f.close()
         self.f = FortranFile(filename)
         self.intType = 'i8'
         self.defGeneralVars()
     self.readGeom()
     self.getTypeAndNnodePerElement()
Esempio n. 42
0
def readBinaryArray(path, ncols) -> np.ndarray:
    """Get array from Fortran binary file.

    Parameters
    ----------

    path : Path
        Path to Fortran binary array.
    ncols : uint
        Number of columns in the binary file.
    """
    array = FortranFile(path, 'r')
    array = array.read_reals()
    nrows = int(array.shape[0] / ncols)
    array = np.reshape(array, (nrows, ncols))

    return array
Esempio n. 43
0
	def read_stalker_data(self,
		datadir='/data',
		procdir='proc0'):
			
		f = FortranFile(datadir+'/'+procdir+'/particles_stalker.dat','r')
		file_not_ended=True
		firstline = f.read_record([('a','f8'),('b','i4')])
		firstline = f.read_record([('a','f8'),('b','i4')])
		snaptime = []
		nrshot = []
		partdata = []
		iddata = []
		while (True):
			try:
				temp = f.read_record([('snaptime','f8'),('nrshot','i4')])
				snaptime.append(temp['snaptime'])
				nrshot.append(temp['nrshot'])
			except TypeError:
				break
			except ValueError:
				break
			if (nrshot[-1] > 0):
				temp = f.read_record([('iddata','i4')])
				iddata.append(temp['iddata'])
				temp = f.read_record([('partdata','f8')])
				temp = np.reshape(temp,(-1,self.ndims_stalk))
				partdata.append(temp['partdata'])
			else:
				iddata.append([])
				partdata.append([])
		
		partdata=np.asarray(partdata)
		iddata = np.asarray(iddata)
		snaptime = np.asarray(snaptime)
		nrshot = np.array(nrshot)
		oldshape = np.shape(partdata)
		
		partdata = np.reshape(partdata,oldshape)
		
		setattr(self,procdir+'_snaptime',snaptime)
		setattr(self,procdir+'_iddata',iddata)
		setattr(self,procdir+'_partdata',partdata)
		setattr(self,procdir+'_nrshot',nrshot)
Esempio n. 44
0
def read_fbin(filename):
    ''' this reads each written binary instance itteratively'''
    f = FortranFile(filename, 'r')
    array = []
    while True:
        try:
            array.append(f.read_reals(dtype=np.float_))
        except TypeError:
            break
    #array = np.reshape(array, (nspecs,-1))

    f.close()
    
    
    #newdata = np.array(array)[:,selected_index]
    
    #indices = xrange(0,len(array)-sets_of,sets_of)
    
    
    #newdata = newdata[indices,:]
    #return newdata
    return array
Esempio n. 45
0
def test_fortranfiles_write():
    for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
        m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
        if not m:
            raise RuntimeError("Couldn't match %s filename to regex" % filename)
        dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))

        dtype = m.group(1).replace('s', '<')
        data = np.arange(np.prod(dims)).reshape(dims).astype(dtype)

        tmpdir = tempfile.mkdtemp()
        try:
            testFile = path.join(tmpdir,path.basename(filename))
            f = FortranFile(testFile, 'w','<u4')
            f.write_record(data.T)
            f.close()
            originalfile = open(filename, 'rb')
            newfile = open(testFile, 'rb')
            assert_equal(originalfile.read(), newfile.read(),
                         err_msg=filename)
            originalfile.close()
            newfile.close()
        finally:
            shutil.rmtree(tmpdir)
Esempio n. 46
0
def export_FFT_field(phi,outfile):
    """
    export_FFT_field(phi,outfile)

    outfile: string
    phi[:,:,:] : np.array, rank 3

    This function takes in a 3-D field, of dimension
    Ngrid x Ngrid x Ngrid, and exports a half-field
    Ngrid/2+1 x Ngrid x Ngrid in unformatted
    Fortran record. Equivalent to a write(field) statement.
    """
    f=FortranFile(outfile,'w')
    n=phi.shape[2]
    f.write_record(np.array([n],dtype=np.int32)) # write integer record
    f.write_record(phi[:n//2+1].ravel(order='F')) # write half-field
    f.close()
Esempio n. 47
0
 def export_unk(self, grid = [50,50,50]):
     '''
     Export the periodic part of BF in a real space grid for plotting with wannier90
     '''    
     
     from scipy.io import FortranFile
     grids_coor, weights = periodic_grid(self.cell, grid, order = 'F')        
     
     for k_id in range(self.num_kpts_loc):
         spin = '.1'
         if self.spin_up != None and self.spin_up == False : spin = '.2'
         kpt = self.cell.get_abs_kpts(self.kpt_latt_loc[k_id])    
         ao = numint.eval_ao(self.cell, grids_coor, kpt = kpt)
         u_ao = np.einsum('x,xi->xi', np.exp(-1j*np.dot(grids_coor, kpt)), ao, optimize = True)
         unk_file = FortranFile('UNK' + "%05d" % (k_id + 1) + spin, 'w')
         unk_file.write_record(np.asarray([grid[0], grid[1], grid[2], k_id + 1, self.num_bands_loc], dtype = np.int32))    
         mo_included = self.mo_coeff_kpts[k_id][:,self.band_included_list]        
         u_mo = np.einsum('xi,in->xn', u_ao, mo_included, optimize = True)
         for band in range(len(self.band_included_list)):    
             unk_file.write_record(np.asarray(u_mo[:,band], dtype = np.complex128))                    
         unk_file.close()
Esempio n. 48
0
if calcUIU: reorder_uXu("uIu",UIUformatted)


if calcSPN:
  try:
    print "----------\n SPN  \n---------\n"

    if SPNformatted:
	f_spn_in =  open(seedname+".spn", 'r')
	f_spn_out = open(seednameGW+".spn", 'w')
	header=f_spn_in.readline()
        f_spn_out.write(header)
        nbnd,NK=np.array(f_spn_in.readline().split(),dtype=np.int32)
	f_spn_out.write("  ".join(str(x) for x in (NBND,NKPT) ) )
    else:
	f_spn_in = FortranFile(seedname+".spn", 'r')
	f_spn_out = FortranFile(seednameGW+".spn", 'w')
	header=f_spn_in.read_record(dtype='c')
        f_spn_out.write_record(header)
        nbnd,NK=f_spn_in.read_record(dtype=np.int32)
	f_spn_out.write_record(np.array([NBND,NKPT],dtype=np.int32))

    print "".join(header)
    assert nbnd==nbndDFT


    indm,indn=np.tril_indices(nbnd)
    indmQP,indnQP=np.tril_indices(NBND)

    if SPNformatted:
	SPN=np.loadtxt(f_spn_in).reshape(-1)
Esempio n. 49
0
def main(sourcedir, sourcefile, savedir, saveheader, moving=0, incr=1, imin=0, imax=-1 ):
    """
    imin --> minimum iteration above which files will be written
    incr --> iteration interval to write files at
    """

    # ogdir = os.getcwd()
    # os.chdir(savedir)

    MakeOutputDir(savedir)

    #link in source file
    if not os.path.isfile('{}/{}'.format(savedir, sourcefile)):
        # cmd('ln -s {}/{} {}'.format(sourcedir, sourcefile, sourcefile))
        cmd('ln -s {}/{} {}/{}'.format(sourcedir, sourcefile, savedir, sourcefile))


    #GET GRID/FILE DIMENSIONS
    f = FortranFile('{}/{}'.format(savedir, sourcefile), 'r')
    dims = f.read_ints('uint32')[2:6]
    #Number of iterations, Number of points in each direction, number of parameters, needed to read whole file
    nj, nk, nl, nq = dims

    #FORMATS IN BC201 FILE
    bc201formats = [
                        ('ints', '<i4', (1,7)), #IGRID,ISTEP,NJ,NK,NL,NQ,NQC
                        ('tvr', 'float64'),     #TVREF
                        ('dtr', 'float64'),     #DTVREF
                        ('xyz', 'float64', (3,nj,nk,nl)), #XYZ points
                        ('q', 'float64', (nq,nj,nk,nl)),  #Q variables (nq variables, njXnkXnl values for each)
                        ('iblank', 'int32', (nj,nk,nl)),  #IBLANK status of each grid point
                    ]

    #########################
    #Save pressure coeff history at certian locations
    #! I want to write out a subset through time (every iteration!)
    ports = [8,13,30,45]
    out = open(savedir+"/cp_history.dat","w")
    out.write("ITER")
    for p in ports:
        out.write(" {}_x {}_cp".format(p,p))
    out.write("\n")
    #############################


    #RESTART FILE AND READ EACH RECORD
    f = FortranFile('{}/{}'.format(savedir, sourcefile), 'r')
    keep_reading = True
    irecord = -1
    while (keep_reading is True):

        irecord += 1

        #READ CURRENT RECORD
        b = f.read_record(bc201formats)



        #GET CURRENT ITERATION
        istep = b['ints'][0][0][1]

        # print(istep)


        #DONT WRITE UNDESIRED FILES (SKIP)
        #only write files at specified interval
        if (istep % incr != 0):
            continue
        #Don't write files below start iter
        if (istep < imin):
            continue
        #Stop reading when max desired iteration is reached
        if istep > imax - incr:
            keep_reading = False




        #WRITE GRID POINTS TO PLOT3D FORMATTED FILE

        if moving:
            #Grid is moving, write to file at each iteration
            savename = '{}/{}.x.{}'.format(savedir, saveheader, istep)
            WriteGrid(savename, b, nj, nk, nl)
        elif irecord == 0:
           #Grid is stationary, write to file only once
            savename = '{}/{}.x'.format(savedir, saveheader)
            WriteGrid(savename, b, nj, nk, nl)


        #CALCULATE CP FOR EACH POINT
        cps = np.zeros((nj,nk,nl))
        # print(cps.shape)
        for j in range(nj):
            for k in range(nk):
                for l in range(nl):
                    #print(j,k,l,b['q'].shape)
                    q = np.zeros(nq)
                    for iq in range(nq):
                        q[iq] = b['q'][0][iq][j][k][l]

                    #print(q)
                    u = q[1] / q[0]
                    v = q[2] / q[0]
                    w = q[3] / q[0]
                    v2 = (u*u + v*v + w*w)
                    dp    = 0.5 * q[0] * v2           # Dynamic Pressure
                    dpinf = 0.5 * 1.0  * 0.107**2.0   # DP_oo  0.107=Mach
                    p     = (q[5] - 1.0)*(q[4] - dp)
                    cp    = (p - 1.0/1.4)/dpinf    # 1.4 = gamma_oo

                    cps[j][k][l] = cp


        #WRITE CP TO PLOT3D FILE

        # #If horizontal line, the pressure at the gap behind the ramp is at this location to use a reference pressure
        # vent_cp = cps[45][0][0]

        #get biggest pressure aft of gap to use as reference pressure
        vent_cps = []
        for j in range(15):
            vent_cps.append(cps[49+j][0][0])
        vent_cp = max(vent_cps)


        ofile = open('{}/cp.{}.{}'.format(savedir, saveheader, istep), 'w')
        #write header line with grid dimensions
        ofile.write('X Y Z cp cp_ref\n')
        #for the three dimensions, x,y,z...
        for j in range(nj):
            for k in range(nk):
                for l in range(nl):
                    for x in range(3):
                        ofile.write( ' {}'.format(b['xyz'][0][x][j][k][l]) )
                    ofile.write( ' {}'.format(cps[j][k][l]) )
                    #if horizontal line, also write gap cp for reference
                    ofile.write( ' {}'.format(vent_cp))
                    ofile.write( '\n')
        # ofile.close()



        #####################
        #Save pressure coeff history at certian locations
        out.write("{:10d}".format(istep))
        for p in ports:
            out.write(" {:12g} {:12g}".format(b['xyz'][0][0][p][0][0],cps[p][0][0]))
        out.write("\n")
Esempio n. 50
0
def reorder_uXu(ext,formatted=False):
  try:
    print "----------\n  {0}  \n----------".format(ext)
    
    if formatted:
	f_uXu_in = open(seedname+"."+ext, 'r')
	f_uXu_out = open(seednameGW+"."+ext, 'w')
	header=f_uXu_in.readline()
	f_uXu_out.write(header)
	nbnd,NK,nnb=np.array(f_uXu_in.readline().split(),dtype=int)
	f_uXu_out.write("  ".join(str(x) for x in [NBND,NK,nnb])+"\n")
    else:
	f_uXu_in = FortranFile(seedname+"."+ext, 'r')
	f_uXu_out = FortranFile(seednameGW+"."+ext, 'w')
	header=f_uXu_in.read_record(dtype='c')
        f_uXu_out.write_record(header)
	nbnd,NK,nnb=np.array(f_uXu_in.read_record(dtype=np.int32))
	f_uXu_out.write_record(np.array([NBND,NK,nnb],dtype=np.int32))
    
    assert nbnd==nbndDFT
    print nbnd,NK,nnb
    
    if formatted:
	uXu=np.loadtxt(f_uXu_in).reshape(-1)
	start=0
	length=nbnd*nbnd

    for ik in xrange(NKPT):
	for ib2 in xrange(nnb):
	    for ib1 in xrange(nnb):
		if formatted:
		    A=uXu[start:start+length]
		    start+=length
		else:
		    A=f_uXu_in.read_record(dtype=np.complex)
		A=(A.reshape(nbnd,nbnd,order='F')[BANDSORT[KPNB[ik][ib2]],:][:,BANDSORT[KPNB[ik][ib1]]]+
			np.einsum('ln,lm,l->nm',MMN[ik][ib2].conj(),MMN[ik][ib1],eigenDE[ik]) ).reshape(-1,order='F')
		if formatted:
		    f_uXu_out.write("".join("{0:26.16e}  {1:26.16f}\n".format(x.real,x.imag) for x in A))
		else:
	    	    f_uXu_out.write_record(A)
    f_uXu_out.close()
    f_uXu_in.close()
    print "----------\n {0} OK  \n----------\n".format(ext)
  except IOError as err:
    print "WARNING: {0}.{1} not written : ".format(seednameGW,ext),err 
Esempio n. 51
0
parser = argparse.ArgumentParser(description='Compute the smoothing tree of a halo.')
parser.add_argument('--in', dest='infile', type=str, help='Path to the output file of compute_extrema (in hdf format)', required=True)
parser.add_argument('--out', '-o', type=str, help='Prefix of the outputs')
parser.add_argument('--infofile', type=str, help='Path to the information file (the one containing units of RAMSES, …)')

args = parser.parse_args()

# read the info file
infos = dict()
infos['headers'] = pd.read_csv(args.infofile, sep=' *= *', nrows=19, names=['key', 'value'], index_col='key').T
infos['domain'] = pd.read_csv(args.infofile, delim_whitespace=True, skiprows=20)


# read the center
from scipy.io import FortranFile
ff = FortranFile('data/halo_536-centers.bin')
ff.read_ints() # don't care
outputs = ff.read_ints()
centers = ff.read_reals().reshape(len(outputs), 3)
mins    = ff.read_reals().reshape(len(outputs), 3)
span    = ff.read_reals().reshape(len(outputs), 3)
maxs    = ff.read_reals().reshape(len(outputs), 3)

# create the output dir if required
# if not os.path.isdir(args.out):
#     os.mkdir(args.out)


HDF = pd.HDFStore(args.infile)
# read the data
df    = HDF['extremas']
Esempio n. 52
0
def particles_in_halo(tree_brick, start=0, end=None, fun_filter=lambda x: True):
    ''' Open a tree bricks file and associate to each halo the corresponding particles.
    '''
    # Open file
    f = FortranFile(tree_brick, 'r')

    # Give a value to end, by default start + 1
    if end == None:
        end = start + 1

    # Read headers
    nbodies = f.read_ints()[0]
    f.read_reals(dtype=np.float32)
    aexp = f.read_reals(dtype=np.float32)
    f.read_reals(dtype=np.float32)
    age = f.read_reals(dtype=np.float32)
    nhalo, nsubhalo = f.read_ints()
    halo_tot = nhalo + nsubhalo

    halos = {}
    for i in tqdm(range(halo_tot)):
        parts = f.read_ints()[0]
        members = f.read_ints()
        this_id = f.read_ints()[0]
        if (start <= this_id and this_id < end and fun_filter(this_id)):
            for dm_particle_id in members:
                if not halos.has_key(this_id):
                    halos[this_id] = []

                halos[this_id].append(dm_particle_id)
        elif this_id >= end:
            break
        f.read_ints()

        # Irrelevant
        level, hosthalo, hostsub, nbsub, nextsub = f.read_ints()
        mstar = 1e11 * f.read_reals(dtype=np.float32)
        px, py, pz = f.read_reals(dtype=np.float32)
        f.read_reals(dtype=np.float32)
        f.read_reals(dtype=np.float32)
        rad = f.read_reals(dtype=np.float32)[0]
        f.read_reals(dtype=np.float32)
        f.read_reals(dtype=np.float32)
        rvir, mvir, tvir, cvel = f.read_reals(dtype=np.float32)
        f.read_reals(dtype=np.float32)

    f.close()
    return halos
Esempio n. 53
0
import numpy as np
from scipy.io import FortranFile
from PyFuncemeClimateTools import CreateNetCDF as cn
from decimal import *

lons = [ -55.637 + (0.54 * i) for i in range(109)]
lons = [ float(Decimal("%.2f" % elem)) for elem in lons]

lats = [-21.397, -20.893, -20.387, -19.880, -19.371, -18.861, -18.349, -17.835, -17.320, -16.803,
-16.285, -15.766, -15.246, -14.724, -14.200, -13.676, -13.150, -12.624, -12.096, -11.567,
-11.037, -10.506,  -9.975,  -9.442,  -8.909,  -8.375,  -7.840,  -7.304,  -6.768,  -6.231,
  -5.694,  -5.156,  -4.617,  -4.079,  -3.539,  -3.000,  -2.460,  -1.920,  -1.380,  -0.840,
  -0.300,  0.241,   0.781,   1.321,   1.861,   2.401,   2.941,   3.480,   4.019,   4.558,
   5.097, 5.635,   6.172,   6.709,   7.245,   7.781,   8.316,   8.850,   9.384,   9.916,
  10.448,  10.979,  11.509,  12.038,  12.566,  13.093,  13.618,  14.143,  14.666,  15.188,
  15.709,  16.229]

f = FortranFile('./plev.198811.DAILY.PER11.51', 'r')

myvar = np.full((30, 72, 109), np.nan)

for i in range(30):
    var = (f.read_reals(dtype='float32')).reshape(72, 109)
    myvar[i, ...] = var
    dummy = (f.read_reals(dtype='float32'))

cn.create_netcdf(myvar, lats, lons, ntime=30)

#
Esempio n. 54
0
from __future__ import print_function
import numpy as np
from scipy.io import FortranFile

#------------------------------------------------------------------------------
# open file and read
#------------------------------------------------------------------------------
# Parameters
path_eigentb = '/Users/ccai/Works/Project/Ionization_calc/Code_develop/\
ionization_code_reu2016/python_script/chianti_8/'
element = 'o'
file_name = element+'eigen.dat'

# Open file
file_eigentb = path_eigentb + file_name
f = FortranFile(file_eigentb, 'r')

# Read file
[nte, natom]=f.read_ints(dtype=np.int32)
te_arr = f.read_reals(dtype=np.float64)
eqistate = f.read_reals(dtype=np.float64).reshape((natom+1, nte), order='F')
eigenvals = f.read_reals(dtype=np.float64).reshape((natom+1, nte), order='F')
eigenvector = f.read_reals(dtype=np.float64).reshape((natom+1, natom+1, nte), order='F')
eigenvector_invers = f.read_reals(dtype=np.float64).reshape((natom+1, natom+1, nte), order='F')

# Close file
f.close()


# Note from Nick: I copied the next three routines to time_advance.py
# but am leaving these also here for now.
Esempio n. 55
0
    def read(self, datadir='data', proc=-1, quiet=False,
             trim=False):
        """
        Read the grid data from the pencil code simulation.
        If proc < 0, then load all data and assemble.
        Otherwise, load grid from specified processor.

        call signature:

        read(self, file_name='time_series.dat', datadir='data',
             double=0, quiet=0, comment_char='#')

        Keyword arguments:

        *datadir*:
          Directory where the data is stored.

        *proc*
          Processor to be read. If proc is -1, then read the 'global'
          grid. If proc is >=0, then read the grid.dat in the
          corresponding processor directory.

        *quiet*
          Flag for switching of output.

        *trim*
          Cuts off the ghost points.
        """

        import numpy as np
        import os
        from scipy.io import FortranFile
        import pencilnew.read as read

        datadir = os.path.expanduser(datadir)
        dim = read.dim(datadir, proc)
        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if proc < 0:
            proc_dirs = list(filter(lambda string: string.startswith('proc'), os.listdir(datadir)))
        else:
            proc_dirs = ['proc' + str(proc)]

        # Define the global arrays.
        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)
        dx_1 = np.zeros(dim.mx, dtype=precision)
        dy_1 = np.zeros(dim.my, dtype=precision)
        dz_1 = np.zeros(dim.mz, dtype=precision)
        dx_tilde = np.zeros(dim.mx, dtype=precision)
        dy_tilde = np.zeros(dim.my, dtype=precision)
        dz_tilde = np.zeros(dim.mz, dtype=precision)

        for directory in proc_dirs:
            proc = int(directory[4:])
            procdim = read.dim(datadir, proc)
            if not quiet:
                print("reading grid data from processor {0} of {1} ...".format(proc, len(proc_dirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            # Read the grid data.
            file_name = os.path.join(datadir, directory, 'grid.dat')
            infile = FortranFile(file_name, 'r')
            grid_raw = infile.read_record(dtype=precision)
            dx, dy, dz = tuple(infile.read_record(dtype=precision))
            Lx, Ly, Lz = tuple(infile.read_record(dtype=precision))
            dx_1_raw = infile.read_record(dtype=precision)
            dx_tilde_raw = infile.read_record(dtype=precision)
            infile.close()

            # Reshape the arrays.
            t = grid_raw[0]
            x_loc = grid_raw[1:mxloc+1]
            y_loc = grid_raw[mxloc+1:mxloc+myloc+1]
            z_loc = grid_raw[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            dx_1_loc = dx_1_raw[0:mxloc]
            dy_1_loc = dx_1_raw[mxloc:mxloc+myloc]
            dz_1_loc = dx_1_raw[mxloc+myloc:mxloc+myloc+mzloc]
            dx_tilde_loc = dx_tilde_raw[0:mxloc]
            dy_tilde_loc = dx_tilde_raw[mxloc:mxloc+myloc]
            dz_tilde_loc = dx_tilde_raw[mxloc+myloc:mxloc+myloc+mzloc]

            if len(proc_dirs) > 1:
                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0x_loc = 0
                    i1x_loc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0x_loc = procdim.nghostx
                    i1x_loc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0y_loc = 0
                    i1y_loc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0y_loc = procdim.nghosty
                    i1y_loc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z + procdim.mz
                    i0z_loc = 0
                    i1z_loc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0z_loc = procdim.nghostz
                    i1z_loc = procdim.mz

                x[i0x:i1x] = x_loc[i0x_loc:i1x_loc]
                y[i0y:i1y] = y_loc[i0y_loc:i1y_loc]
                z[i0z:i1z] = z_loc[i0z_loc:i1z_loc]
                dx_1[i0x:i1x] = dx_1_loc[i0x_loc:i1x_loc]
                dy_1[i0y:i1y] = dy_1_loc[i0y_loc:i1y_loc]
                dz_1[i0z:i1z] = dz_1_loc[i0z_loc:i1z_loc]
                dx_tilde[i0x:i1x] = dx_tilde_loc[i0x_loc:i1x_loc]
                dy_tilde[i0y:i1y] = dy_tilde_loc[i0y_loc:i1y_loc]
                dz_tilde[i0z:i1z] = dz_tilde_loc[i0z_loc:i1z_loc]

            else:
                x = x_loc
                y = y_loc
                z = z_loc
                dx_1 = dx_1_loc
                dy_1 = dy_1_loc
                dz_1 = dz_1_loc
                dx_tilde = dx_tilde_loc
                dy_tilde = dy_tilde_loc
                dz_tilde = dz_tilde_loc

        if trim:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            self.dx_1 = dx_1[dim.l1:dim.l2+1]
            self.dy_1 = dy_1[dim.m1:dim.m2+1]
            self.dz_1 = dz_1[dim.n1:dim.n2+1]
            self.dx_tilde = dx_tilde[dim.l1:dim.l2+1]
            self.dy_tilde = dy_tilde[dim.m1:dim.m2+1]
            self.dz_tilde = dz_tilde[dim.n1:dim.n2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.dx_1 = dx_1
            self.dy_1 = dy_1
            self.dz_1 = dz_1
            self.dx_tilde = dx_tilde
            self.dy_tilde = dy_tilde
            self.dz_tilde = dz_tilde

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        self.Lx = Lx
        self.Ly = Ly
        self.Lz = Lz
from matplotlib import use
use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from scipy.io import FortranFile

#
# Problem parameters
#
datafile = sys.argv[1]

#
# Read binary file
#
try:
  file = FortranFile(datafile, 'r')
except:
  print("Usage: python example-plot.py <datefile>")
  raise SystemExit

# Read the line number
nline = file.read_reals(dtype=np.int32)

# Set pdf pages
with PdfPages('./nei_example-plot.pdf') as pdf:

  for iline in range(nline[0]):
    nei_conce = file.read_reals(dtype=np.float64).reshape(30, 30)
    ei_conce = file.read_reals(dtype=np.float64).reshape(30, 30)
    
    plt.figure(figsize=(11/1.5, 8.5/1.5))
Esempio n. 57
0
    def read(self, var_file='', datadir='data', proc=-1, ivar=-1, quiet=True,
             trimall=False, magic=None, sim=None, precision='f'):
        """
        Read VAR files from Pencil Code. If proc < 0, then load all data
        and assemble, otherwise load VAR file from specified processor.

        The file format written by output() (and used, e.g. in var.dat)
        consists of the followinig Fortran records:
        1. data(mx, my, mz, nvar)
        2. t(1), x(mx), y(my), z(mz), dx(1), dy(1), dz(1), deltay(1)
        Here nvar denotes the number of slots, i.e. 1 for one scalar field, 3
        for one vector field, 8 for var.dat in the case of MHD with entropy.
        but, deltay(1) is only there if lshear is on! need to know parameters.

        call signature:

        var(var_file='', datadir='data', proc=-1, ivar=-1, quiet=True,
            trimall=False, magic=None, sim=None, precision='f')

        Keyword arguments:
            var_file:   Name of the VAR file.
            datadir:    Directory where the data is stored.
            proc:       Processor to be read. If -1 read all and assemble to one array.
            ivar:       Index of the VAR file, if var_file is not specified.
            quiet:      Flag for switching off output.
            trimall:    Trim the data cube to exclude ghost zones.
            magic:      Values to be computed from the data, e.g. B = curl(A).
            sim:        Simulation sim object.
            precision:  Float (f) or double (d).
        """

        import numpy as np
        import os
        from scipy.io import FortranFile
        from ..math.derivatives import curl, curl2
        from .. import read
        from ..sim import __Simulation__

        dim = None
        param = None
        index = None

        if isinstance(sim, __Simulation__):
            datadir = os.path.expanduser(sim.datadir)
            dim = sim.dim
            param = read.param(datadir=sim.datadir, quiet=True)
            index = read.index(datadir=sim.datadir)
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                if var_file[0:2].lower() == 'og':
                    dim = read.ogdim(datadir, proc)
                else:
                    dim = read.dim(datadir, proc)
            if param is None:
                param = read.param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read.index(datadir=datadir)

        run2D = param.lwrite_2d

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            total_vars = dim.mvar + dim.maux
        else:
            total_vars = dim.mvar

        if not var_file:
            if ivar < 0:
                var_file = 'var.dat'
            else:
                var_file = 'VAR' + str(ivar)

        if proc < 0:
            proc_dirs = self.__natural_sort(filter(lambda s: s.startswith('proc'),
                                                   os.listdir(datadir)))
        else:
            proc_dirs = ['proc' + str(proc)]

        # Set up the global array.
        if not run2D:
            f = np.zeros((total_vars, dim.mz, dim.my, dim.mx),
                         dtype=precision)
        else:
            if dim.ny == 1:
                f = np.zeros((total_vars, dim.mz, dim.mx), dtype=precision)
            else:
                f = np.zeros((total_vars, dim.my, dim.mx), dtype=precision)

        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)

        for directory in proc_dirs:
            proc = int(directory[4:])
            if var_file[0:2].lower() == 'og':
                procdim = read.ogdim(datadir, proc)
            else:
                procdim = read.dim(datadir, proc)
            if not quiet:
                print("Reading data from processor {0} of {1} ...".format( \
                      proc, len(proc_dirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            # Read the data.
            file_name = os.path.join(datadir, directory, var_file)
            infile = FortranFile(file_name)
            if not run2D:
                f_loc = infile.read_record(dtype=precision)
                f_loc = f_loc.reshape((-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.read_record(dtype=precision)
                    f_loc = f_loc.reshape((-1, mzloc, mxloc))
                else:
                    f_loc = infile.read_record(dtype=precision)
                    f_loc = f_loc.reshape((-1, myloc, mxloc))
            raw_etc = infile.read_record(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc+1]
            y_loc = raw_etc[mxloc+1:mxloc+myloc+1]
            z_loc = raw_etc[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            if param.lshear:
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3-shear_offset]
            dy = raw_etc[-2-shear_offset]
            dz = raw_etc[-1-shear_offset]

            if len(proc_dirs) > 1:
                # Calculate where the local processor will go in
                # the global array.
                #
                # Don't overwrite ghost zones of processor to the left (and
                # accordingly in y and z direction -- makes a difference on the
                # diagonals)
                #
                # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if not run2D:
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                            f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                            f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc

        if magic is not None:
            if 'bb' in magic:
                # Compute the magnetic field before doing trimall.
                aa = f[index.ax-1:index.az, ...]
                # TODO: Specify coordinate system.
                self.bb = curl(aa, dx, dy, dz, run2D=run2D)
                if trimall:
                    self.bb = self.bb[:, dim.n1:dim.n2+1,
                                      dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if 'jj' in magic:
                # Compute the electric current field before doing trimall.
                aa = f[index.ax-1:index.az, ...]
                # TODO: Specify coordinate system.
                self.jj = curl2(aa, dx, dy, dz)
                if trimall:
                    self.jj = self.jj[:, dim.n1:dim.n2+1,
                                      dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if 'vort' in magic:
                # Compute the vorticity field before doing trimall.
                uu = f[index.ux-1:index.uz, ...]
                # TODO: Specify coordinate system.
                # WL: The curl subroutine should take care of it. 
                self.vort = curl(uu, dx, dy, dz, run2D=run2D)
                if trimall:
                    if run2D:
                        if dim.nz == 1:
                            self.vort = self.vort[:, dim.m1:dim.m2+1,
                                                  dim.l1:dim.l2+1]
                        else:
                            self.vort = self.vort[:, dim.n1:dim.n2+1,
                                                  dim.l1:dim.l2+1]
                    else:
                        self.vort = self.vort[:, dim.n1:dim.n2+1,
                                              dim.m1:dim.m2+1,
                                              dim.l1:dim.l2+1]

        # Trim the ghost zones of the global f-array if asked.
        if trimall:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            if not run2D:
                self.f = f[:, dim.n1:dim.n2+1, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            else:
                if dim.ny == 1:
                    self.f = f[:, dim.n1:dim.n2+1, dim.l1:dim.l2+1]
                else:
                    self.f = f[:, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2 + 1
            self.m1 = dim.m1
            self.m2 = dim.m2 + 1
            self.n1 = dim.n1
            self.n2 = dim.n2 + 1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity
        for key in index.__dict__.keys():
            if key != 'global_gg' and key != 'keys':
                value = index.__dict__[key]
                setattr(self, key, self.f[value-1, ...])
        # Special treatment for vector quantities.
        if hasattr(index, 'uu'):
            self.uu = self.f[index.ux-1:index.uz, ...]
        if hasattr(index, 'aa'):
            self.aa = self.f[index.ax-1:index.az, ...]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # Do the rest of magic after the trimall (i.e. no additional curl...).
        self.magic = magic
        if self.magic is not None:
            self.magic_attributes(param)
Esempio n. 58
0
dY3Right = Y3[:, -1] - Y3[:, -2]

# Write mean tau
WuResMeanValid = np.zeros((nlat * nlon,))
WuResMeanValid[~mask] = WuResMean
WuResMeanValid = WuResMeanValid.reshape(nlat, nlon)
tau = np.tile(np.expand_dims(WuResMeanValid, 0), (T, 1, 1)) * ampMean
dtaudx = np.empty(tau.shape)
dtaudx[:, :, 1:-1] = (tau[:, :, 2:] - tau[:, :, :-2]) / dX3Center
dtaudx[:, :, 0] = (tau[:, :, 1] - tau[:, :, 0]) / dX3Left
dtaudx[:, :, -1] = (tau[:, :, -1] - tau[:, :, -2]) / dX3Right
dtaudy = np.empty(tau.shape)
dtaudy[:, 1:-1] = (tau[:, 2:] - tau[:, :-2]) / dY3Center
dtaudy[:, 0] = (tau[:, 1] - tau[:, 0]) / dY3Left
dtaudy[:, -1] = (tau[:, -1] - tau[:, -2]) / dY3Right
f = FortranFile('%s/tau_mean_ampMean%02d%s.bin' % (initDir, int(ampMean * 10), postfix), 'w')
for t in np.arange(T):
    f.write_record(tau[t])
f.close()
f = FortranFile('%s/dtaudx_mean_ampMean%02d%s.bin' % (initDir, int(ampMean * 10), postfix), 'w')
for t in np.arange(T):
    f.write_record(dtaudx[t])
f.close()
f = FortranFile('%s/dtaudy_mean_ampMean%02d%s.bin' % (initDir, int(ampMean * 10), postfix), 'w')
for t in np.arange(T):
    f.write_record(dtaudy[t])
f.close()

# Write tau with variations
for k in np.arange(nEOF):
    print 'Writing tau with %d eofs...' % (k+1,)
Esempio n. 59
0
def read_atomic_data(elements=['H', 'He', 'C',     # twelve most abundant elements
                               'N', 'O', 'Ne',
                               'Mg', 'Si', 'S', 
                               'Ar', 'Ca', 'Fe', ] , 
                     data_directory= 'sunnei/AtomicData',   # not robust!  Works when calling from the directory that sunnei is in
                     screen_output=False):

    '''
    This routine reads in the atomic data to be used for the
    non-equilibrium ionization calculations.
 
    Instructions for generating atomic data files
    =============================================
    
    The atomic data files are generated from the routines described by
    Shen et al. (2015) and are available at:
    
    https://github.com/ionizationcalc/time_dependent_fortran
    
    First, run the IDL routine 'pro_write_ionizrecomb_rate.pro' in the
    subdirectory sswidl_read_chianti with optional parameters: nte
    (number of temperature bins, default=501), te_low (low log
    temperature, default=4.0), and te_high (high log temperature,
    default=9.0) to get an ionization rate table.  The routine outputs
    the file "ionrecomb_rate.dat" which is a text file containing the
    ionization and recombination rates as a function of temperature.
    This routine requires the atomic database Chianti to be installed
    in IDL.

    Second, compile the Fortran routine 'create_eigenvmatrix.f90'.
    With the Intel mkl libraries it is compiled as: "ifort -mkl
    create_eigenvmatrix.f90 -o create.out" which can then be run with
    the command "./create.out".  This routine outputs all the
    eigenvalue tables for the first 28 elements (H to Ni).

    As of 2016 April 7, data from Chianti 8 is included in the
    CMEheat/AtomicData subdirectory.
    '''

    if screen_output:
        print('read_atomic_data: beginning program')
    
    from scipy.io import FortranFile

    '''
    Begin a loop to read in the atomic data files needed for the
    non-equilibrium ionization modeling.  The information will be
    stored in the atomic_data dictionary.

    For the first element in the loop, the information that should be
    the same for each element will be stored at the top level of the
    dictionary.  This includes the temperature grid, the number of
    temperatures, and the number of elements.

    For all elements, read in and store the arrays containing the
    equilibrium state, the eigenvalues, the eigenvectors, and the
    eigenvector inverses.
    '''

    atomic_data = {}
    
    first_element_in_loop = True

    for element in elements:

        if screen_output:
            print('read_atomic_data: '+element)

        AtomicNumber = AtomicNumbers[element]
        nstates = AtomicNumber + 1

        filename = data_directory + '/' + element.lower() + 'eigen.dat'
        H = FortranFile(filename, 'r')

        nte, nelems = H.read_ints(np.int32)
        temperatures = H.read_reals(np.float64)
        equistate = H.read_reals(np.float64).reshape((nte,nstates))
        eigenvalues = H.read_reals(np.float64).reshape((nte,nstates))
        eigenvector = H.read_reals(np.float64).reshape((nte,nstates,nstates))
        eigenvector_inv = H.read_reals(np.float64).reshape((nte,nstates,nstates))
        c_rate = H.read_reals(np.float64).reshape((nte,nstates))
        r_rate = H.read_reals(np.float64).reshape((nte,nstates))      
        
        if first_element_in_loop:
            atomic_data['nte'] = nte
            atomic_data['nelems'] = nelems  # Probably not used but store anyway
            atomic_data['temperatures'] = temperatures
            first_element_in_loop = False
        else: 
            assert nte == atomic_data['nte'], 'Atomic data files have different number of temperature levels: '+element
            assert nelems == atomic_data['nelems'], 'Atomic data files have different number of elements: '+element
            assert np.allclose(atomic_data['temperatures'],temperatures), 'Atomic data files have different temperature bins'

        atomic_data[element] = {'element':element,
                                'AtomicNumber':AtomicNumber,
                                'nstates':nstates,
                                'equistate':equistate,
                                'eigenvalues':eigenvalues,
                                'eigenvector':eigenvector,
                                'eigenvector_inv':eigenvector_inv,
                                'ionization_rate':c_rate,
                                'recombination_rate':r_rate,
                                }
        
    if screen_output:
        print('read_atomic_data: '+str(len(elements))+' elements read in')
        print('read_atomic_data: complete')

    return atomic_data
Esempio n. 60
-1
def read():
	f=FortranFile(BinaryData,'r')
	n_vals=f.read_record('i4,i4,i4,i4,i4,(12,)i4')
	d_vals=f.read_reals('f4')
	xyz_vals=f.read_record('(500,)f4,(500,)f4,(500,)f4')
	a_vals=f.read_reals('i8')
	ndr_vals=f.read_ints('i4')
	ag_vals=f.read_record('(3,)i8')
	f.close()
	return n_vals,d_vals,xyz_vals,a_vals,ndr_vals,ag_vals