<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Dec 15 17:56:58 2014

@author: sm1fg
"""
import numpy as np
import pysac.io.gdf_writer as gdf
import h5py
import astropy.units as u
#import h5py as h5

##============================================================================
## Save a file!!!
##============================================================================
""" For large data arrays this has been producing overfull memory so moved to
dedicated serial function, which should be parallel and moved ahead of gather
for plotting -- maybe handle plotting from hdf5 also

This file is potentially large - recommended to mkdir hdf5 in /data/${USER}
and add symlink to ${HOME} to avoid exceeding quota.
"""
def save_SACvariables(
              filename,
              rho,
              Bx,
              By,
              Bz,
              energy,
              option_pars,
              physical_constants,
              coords,
              Nxyz
             ):

    """ Save the background variables for a SAC model in hdf5 (gdf default)
    format after collating the data from mpi sub processes if necessary.
    """
    rank = 0
    if option_pars['l_mpi']:
        from mpi4py import MPI
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        gather_vars = [
                       rho.to(u.Unit('kg m-3')),
                       Bx.to(u.Unit('Tesla')),
                       By.to(u.Unit('Tesla')),
                       Bz.to(u.Unit('Tesla')),
                       energy.to(u.Unit('kg m-2 s-2'))
                      ]
        concat_vars = []
        for var in gather_vars:
            concat_vars.append(comm.gather(var, root=0))

        if rank == 0:
            out_vars = []
            for cvar in concat_vars:
                out_vars.append(np.concatenate(cvar, axis=0))

            rho,Bx,By,Bz,energy = out_vars
    if rank == 0:
        print'writing',filename
        print'SAC background atmosphere'

        grid_dimensions = [Nxyz[0], Nxyz[1], Nxyz[2]]
        left_edge =  u.Quantity([coords['xmin'],
                                 coords['ymin'],
                                 coords['zmin']]).to(u.m)
        right_edge = u.Quantity([coords['xmax'],
                                 coords['ymax'],
                                 coords['zmax']]).to(u.m)
        g0 = physical_constants['gravity']
        gamma = 5./3.
        dummy = np.zeros(rho.shape)
        simulation_parameters = gdf.SimulationParameters([
                            ['boundary_conditions', np.zeros(6) + 2],
                            ['cosmological_simulation', 0          ],
                            ['current_iteration', 0                ],
                            ['current_time', 0.0                   ],
                            ['dimensionality', 3                   ],
                            ['domain_dimensions', grid_dimensions  ],
                            ['domain_left_edge', left_edge         ],
                            ['domain_right_edge', right_edge       ],
                            ['eta', 0.0                            ],
                            ['field_ordering', 0                   ],
                            ['gamma', gamma                   ],
                            ['gravity0', 0.0                       ],
                            ['gravity1', 0.0                       ],
                            ['gravity2', g0                        ],
                            ['nu', 0.0                             ],
                            ['num_ghost_zones', 0                  ],
                            ['refine_by', 0                        ],
                            ['unique_identifier', 'sacgdf2014'     ]
                            ])

        gdf_file = gdf.create_file(h5py.File(filename,'w'), simulation_parameters, grid_dimensions)

        gdf.write_field(gdf_file, rho,
                              'density_bg',
                              'Background Density'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('kg/m^3'),
                              'density_pert',
                              'Perturbation Density'
                              )
        gdf.write_field(gdf_file,
                              energy,
                              'internal_energy_bg',
                              'Background Internal Energy'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('kg m-1 s-2'),
                              'internal_energy_pert',
                              'Perturbation Internal Energy'
                              )
        gdf.write_field(gdf_file, Bx,
                              'mag_field_x_bg',
                              'x Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('Tesla'),
                              'mag_field_x_pert',
                              'x Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, By,
                              'mag_field_y_bg',
                              'y Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('Tesla'),
                              'mag_field_y_pert',
                              'y Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, Bz,
                              'mag_field_z_bg',
                              'z Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('Tesla'),
                              'mag_field_z_pert',
                              'z Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                              'velocity_x',
                              'x Component of Velocity'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                              'velocity_y',
                              'y Component of Velocity'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                                'velocity_z',
                                'z Component of Velocity'
                              )

        gdf_file.close()


#============================================================================

def save_SACsources(
                    sourcesfile,<commit_msg>Replaced Tesla with T<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Dec 15 17:56:58 2014

@author: sm1fg
"""
import numpy as np
import pysac.io.gdf_writer as gdf
import h5py
import astropy.units as u
#import h5py as h5

##============================================================================
## Save a file!!!
##============================================================================
""" For large data arrays this has been producing overfull memory so moved to
dedicated serial function, which should be parallel and moved ahead of gather
for plotting -- maybe handle plotting from hdf5 also

This file is potentially large - recommended to mkdir hdf5 in /data/${USER}
and add symlink to ${HOME} to avoid exceeding quota.
"""
def save_SACvariables(
              filename,
              rho,
              Bx,
              By,
              Bz,
              energy,
              option_pars,
              physical_constants,
              coords,
              Nxyz
             ):

    """ Save the background variables for a SAC model in hdf5 (gdf default)
    format after collating the data from mpi sub processes if necessary.
    """
    rank = 0
    if option_pars['l_mpi']:
        from mpi4py import MPI
        comm = MPI.COMM_WORLD
        rank = comm.Get_rank()
        gather_vars = [
                       rho.to(u.Unit('kg m-3')),
                       Bx.to(u.Unit('T')),
                       By.to(u.Unit('T')),
                       Bz.to(u.Unit('T')),
                       energy.to(u.Unit('kg m-2 s-2'))
                      ]
        concat_vars = []
        for var in gather_vars:
            concat_vars.append(comm.gather(var, root=0))

        if rank == 0:
            out_vars = []
            for cvar in concat_vars:
                out_vars.append(np.concatenate(cvar, axis=0))

            rho,Bx,By,Bz,energy = out_vars
    if rank == 0:
        print'writing',filename
        print'SAC background atmosphere'

        grid_dimensions = [Nxyz[0], Nxyz[1], Nxyz[2]]
        left_edge =  u.Quantity([coords['xmin'],
                                 coords['ymin'],
                                 coords['zmin']]).to(u.m)
        right_edge = u.Quantity([coords['xmax'],
                                 coords['ymax'],
                                 coords['zmax']]).to(u.m)
        g0 = physical_constants['gravity']
        gamma = 5./3.
        dummy = np.zeros(rho.shape)
        simulation_parameters = gdf.SimulationParameters([
                            ['boundary_conditions', np.zeros(6) + 2],
                            ['cosmological_simulation', 0          ],
                            ['current_iteration', 0                ],
                            ['current_time', 0.0                   ],
                            ['dimensionality', 3                   ],
                            ['domain_dimensions', grid_dimensions  ],
                            ['domain_left_edge', left_edge         ],
                            ['domain_right_edge', right_edge       ],
                            ['eta', 0.0                            ],
                            ['field_ordering', 0                   ],
                            ['gamma', gamma                   ],
                            ['gravity0', 0.0                       ],
                            ['gravity1', 0.0                       ],
                            ['gravity2', g0                        ],
                            ['nu', 0.0                             ],
                            ['num_ghost_zones', 0                  ],
                            ['refine_by', 0                        ],
                            ['unique_identifier', 'sacgdf2014'     ]
                            ])

        gdf_file = gdf.create_file(h5py.File(filename,'w'), simulation_parameters, grid_dimensions)

        gdf.write_field(gdf_file, rho,
                              'density_bg',
                              'Background Density'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('kg/m^3'),
                              'density_pert',
                              'Perturbation Density'
                              )
        gdf.write_field(gdf_file,
                              energy,
                              'internal_energy_bg',
                              'Background Internal Energy'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('kg m-1 s-2'),
                              'internal_energy_pert',
                              'Perturbation Internal Energy'
                              )
        gdf.write_field(gdf_file, Bx,
                              'mag_field_x_bg',
                              'x Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('T'),
                              'mag_field_x_pert',
                              'x Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, By,
                              'mag_field_y_bg',
                              'y Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('T'),
                              'mag_field_y_pert',
                              'y Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, Bz,
                              'mag_field_z_bg',
                              'z Component of Background Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('T'),
                              'mag_field_z_pert',
                              'z Component of Pertubation Magnetic Field'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                              'velocity_x',
                              'x Component of Velocity'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                              'velocity_y',
                              'y Component of Velocity'
                              )
        gdf.write_field(gdf_file, dummy*u.Unit('m/s'),
                                'velocity_z',
                                'z Component of Velocity'
                              )

        gdf_file.close()


#============================================================================

def save_SACsources(
                    sourcesfile,
{'fraction_non_alphanumeric': 0.12187736563209689, 'fraction_numerical': 0.00984102952308857, 'mean_word_length': 1.0089720194647203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3130547', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3192, 'n_words': 666}