Commit 832e133f authored by Andrea Giannetti's avatar Andrea Giannetti
Browse files

Added docstring; included refactoring to save the dust temperature grid.

parent 99293880
Loading
Loading
Loading
Loading
+60 −15
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ def write_radmc_main_input_file(config_mdl: dict,
                                config_lines: dict,
                                path: Union[None, str] = None):
    """
    Creates the main input file for RADMC, which is later used for execution
    Creates the main input file for RADMC, which can be used to reprocess the input
    :param config_mdl: the model configuration
    :param config_lines: te line configuration, from the STG layer
    :param path: the path where to put the input file
@@ -113,6 +113,11 @@ def write_radmc_main_input_file(config_mdl: dict,


def get_solid_body_rotation_y(grid_metadata):
    """
    Generate the velocity field for solid body rotation around the y-axis according to the configuration file
    :param grid_metadata: the grid metadata
    :return: a tuple with the velocity along the x- and y-axes
    """
    distance_xz = grid_metadata['distance_matrix'][:, grid_metadata['grid_refpix'][1], :]
    gradient = float(grid_metadata['velocity_gradient']) * u.Unit(grid_metadata['velocity_gradient_unit'])
    radial_velocity = (distance_xz * u.cm * gradient).to(u.cm / u.second)
@@ -275,6 +280,12 @@ def write_molecular_number_density_profiles(profiles: dict,
def save_fits_grid_profile(quantity: np.array,
                           filename: str,
                           path: str = None):
    """
    Save the model grid to a fits file
    :param quantity: the array to be stored in the fits file
    :param filename: the name of the file to be created
    :param path: the path where the file will be stored
    """
    _path = validate_parameter(path, default=os.path.join('prs', 'fits', 'grids'))
    if not os.path.isfile(os.path.join(_path, filename)):
        fits.writeto(os.path.join(_path, filename), quantity)
@@ -390,13 +401,21 @@ def populate_grid_table(config: dict,
    return output_filename


def populate_grid_files(quantity: str,
def populate_grid_files(quantity_name: str,
                        engine: sqla_engine,
                        zip_filename: str,
                        filename: str,
                        run_id: str):
    """
    Stores into the DB the grid filename for later reference
    :param quantity_name: the name of the quantity to store
    :param engine: the SQLAlchemy engine
    :param zip_filename: the name of the grid archive containing the radmc3d input files
    :param filename: the fits filename where the grid is stored
    :param run_id: the ID of the run
    """
    raw_insert_entry = {'zipped_grid_name': zip_filename,
                        'quantity': quantity,
                        'quantity': quantity_name,
                        'fits_grid_name': filename,
                        'created_on': datetime.now(),
                        'run_id': run_id}
@@ -437,6 +456,13 @@ def write_stellar_input_file(stars_metadata: dict,
                             grid_metadata: dict,
                             path: str,
                             wavelengths_micron: np.array):
    """
    Create the input file to insert stars in the computation
    :param stars_metadata: the metadata about the stars, as specified in the configuration file
    :param grid_metadata: the grid metadata
    :param path: the path where the files will be created
    :param wavelengths_micron: the wavelength to consider
    """
    star_properties = [' '.join([str(rstar), str(mstar), str(pos[0]), str(pos[1]), str(pos[2])]) for rstar, mstar, pos
                       in zip(stars_metadata['rstars'], stars_metadata['mstars'], stars_metadata['star_positions'])]
    override_defaults = {
@@ -457,6 +483,14 @@ def write_stellar_input_file(stars_metadata: dict,
def get_grid_name(method: Union[str, None] = None,
                  zip_filename: Union[str, None] = None,
                  quantity_name: Union[str, None] = None):
    """
    Compute the name of the grid file
    :param method: method used to compute the file name; uuid generates a random unique identifier, composite_grid
        appends the quantity name to the zipped archive name
    :param zip_filename: the zipped archive name
    :param quantity_name: the name of the quantity that should be processed
    :return: the name of the file
    """
    _method = validate_parameter(method, default='uuid')
    allowed_methods = ('uuid', 'composite_grid')
    if method == 'uuid':
@@ -533,18 +567,6 @@ def main(run_id: str,
                                       grid_metadata=grid_metadata,
                                       run_id=run_id)

    grid_file_name = get_grid_name(method='composite_grid',
                                   zip_filename=zip_filename,
                                   quantity_name='gas_number_density')
    save_fits_grid_profile(quantity=profiles['gas_number_density'],
                           filename=grid_file_name)

    populate_grid_files(quantity='gas_number_density',
                        engine=engine,
                        zip_filename=zip_filename,
                        filename=grid_file_name,
                        run_id=run_id)

    write_radmc_main_input_file(config_mdl=config_mdl,
                                config_lines=config_lines,
                                path=input_files_dir)
@@ -569,11 +591,34 @@ def main(run_id: str,
    engine.dispose()
    os.chdir(execution_dir)

    for quantity_name in ('gas_number_density', 'dust_temperature'):
        save_and_persist_grid(engine=engine,
                              profiles=profiles,
                              run_id=run_id,
                              zip_filename=zip_filename,
                              quantity_name=quantity_name)

    make_archive(output_filename=zip_filename,
                 source_dir=input_files_dir,
                 archive_path=os.path.join('stg', 'archive'))
    return zip_filename


def save_and_persist_grid(engine: sqla_engine,
                          profiles: dict,
                          run_id: str,
                          zip_filename: str,
                          quantity_name: str,
                          method: Union[str, None] = None):
    _method = validate_parameter(method, default='composite_grid')
    grid_file_name = get_grid_name(method=_method,
                                   zip_filename=zip_filename,
                                   quantity_name=quantity_name)
    save_fits_grid_profile(quantity=profiles[quantity_name],
                           filename=grid_file_name)
    populate_grid_files(quantity_name=quantity_name, engine=engine, zip_filename=zip_filename, filename=grid_file_name,
                        run_id=run_id)


if __name__ == '__main__':
    main(run_id='test_run')