Home Installation Walkthrough Pipeline modules Pipeline configuration Plotting tools Community guidelines

FAOM API documentation

foam.functions_for_gyre

Extract frequencies from a grid of GYRE output, and generate period spacing series.

 1"""Extract frequencies from a grid of GYRE output, and generate period spacing series."""
 2
 3import glob
 4import logging
 5import multiprocessing
 6from functools import partial
 7from pathlib import Path
 8
 9import pandas as pd
10
11from foam import support_functions as sf
12
13logger = logging.getLogger("logger.ffg")
14
15
16################################################################################
17def extract_frequency_grid(
18    gyre_files, output_file="pulsationGrid.hdf", parameters=["rot", "Z", "M", "logD", "aov", "fov", "Xc"], nr_cpu=None
19):
20    """
21    Extract frequencies from each globbed GYRE file and write them to 1 large file.
22
23    Parameters
24    ----------
25    gyre_files: string
26        String to glob to find all the relevant GYRE summary files.
27    output_file: string
28        Name (can include a path) for the file containing all the pulsation frequencies of the grid.
29    parameters: list of strings
30        List of parameters varied in the computed grid, so these are taken from the
31        name of the summary files, and included in the 1 file containing all the info of the whole grid.
32    nr_cpu: int
33        Number of worker processes to use in multiprocessing.
34        The default 'None' will use the number returned by os.cpu_count().
35    """
36    # make empty MultiProcessing listProxy
37    mp_list = multiprocessing.Manager().list()
38
39    # Glob all the files, then iteratively send them to a pool of processors
40    summary_files = glob.iglob(gyre_files)
41    with multiprocessing.Pool(nr_cpu) as p:
42        extract_func = partial(all_freqs_from_summary, parameters=parameters)
43        dictionaries = p.imap(extract_func, summary_files)
44        for new_row in dictionaries:
45            # Fill the listProxy with dictionaries for each read file
46            mp_list.append(new_row)
47
48        df = pd.DataFrame(data=list(mp_list))
49    # Sort the columns with frequencies by their radial order
50    column_list = list(df.columns[: len(parameters)])
51    column_list.extend(sorted(df.columns[len(parameters) :]))
52    df = df.reindex(column_list, axis=1)
53
54    # Generate the directory for the output file and write the file afterwards
55    Path(Path(output_file).parent).mkdir(parents=True, exist_ok=True)
56    df.to_hdf(path_or_buf=output_file, key="pulsation_grid", format="table", mode="w")
57
58
59################################################################################
60def all_freqs_from_summary(gyre_summary_file, parameters):
61    """
62    Extract model parameters and pulsation frequencies from a GYRE summary file
63
64    Parameters
65    ----------
66    gyre_summary_file: string
67        path to the GYRE summary file
68    parameters: list of strings
69        List of input parameters varied in the computed grid,
70        so these are read from the filename and included in returned line.
71
72    Returns
73    ----------
74    param_dict: dict
75        Dictionary containing all the model parameters and pulsation frequencies of the GYRE summary file.
76    """
77
78    _, data = sf.read_hdf5(gyre_summary_file)
79    param_dict = sf.get_param_from_filename(gyre_summary_file, parameters, values_as_float=True)
80
81    # Arrange increasing in radial order
82    for j in range(len(data["freq"]) - 1, -1, -1):
83        n_pg = data["n_pg"][j]
84        if abs(n_pg) < 10:
85            n_pg = f"{sf.sign(n_pg)}00{abs(n_pg)}"
86        elif abs(n_pg) < 100:
87            n_pg = f"{sf.sign(n_pg)}0{abs(n_pg)}"
88        param_dict.update({f"n_pg{n_pg}": data["freq"][j][0]})
89
90    return param_dict
def extract_frequency_grid( gyre_files, output_file='pulsationGrid.hdf', parameters=['rot', 'Z', 'M', 'logD', 'aov', 'fov', 'Xc'], nr_cpu=None):
18def extract_frequency_grid(
19    gyre_files, output_file="pulsationGrid.hdf", parameters=["rot", "Z", "M", "logD", "aov", "fov", "Xc"], nr_cpu=None
20):
21    """
22    Extract frequencies from each globbed GYRE file and write them to 1 large file.
23
24    Parameters
25    ----------
26    gyre_files: string
27        String to glob to find all the relevant GYRE summary files.
28    output_file: string
29        Name (can include a path) for the file containing all the pulsation frequencies of the grid.
30    parameters: list of strings
31        List of parameters varied in the computed grid, so these are taken from the
32        name of the summary files, and included in the 1 file containing all the info of the whole grid.
33    nr_cpu: int
34        Number of worker processes to use in multiprocessing.
35        The default 'None' will use the number returned by os.cpu_count().
36    """
37    # make empty MultiProcessing listProxy
38    mp_list = multiprocessing.Manager().list()
39
40    # Glob all the files, then iteratively send them to a pool of processors
41    summary_files = glob.iglob(gyre_files)
42    with multiprocessing.Pool(nr_cpu) as p:
43        extract_func = partial(all_freqs_from_summary, parameters=parameters)
44        dictionaries = p.imap(extract_func, summary_files)
45        for new_row in dictionaries:
46            # Fill the listProxy with dictionaries for each read file
47            mp_list.append(new_row)
48
49        df = pd.DataFrame(data=list(mp_list))
50    # Sort the columns with frequencies by their radial order
51    column_list = list(df.columns[: len(parameters)])
52    column_list.extend(sorted(df.columns[len(parameters) :]))
53    df = df.reindex(column_list, axis=1)
54
55    # Generate the directory for the output file and write the file afterwards
56    Path(Path(output_file).parent).mkdir(parents=True, exist_ok=True)
57    df.to_hdf(path_or_buf=output_file, key="pulsation_grid", format="table", mode="w")

Extract frequencies from each globbed GYRE file and write them to 1 large file.

Parameters
  • gyre_files (string): String to glob to find all the relevant GYRE summary files.
  • output_file (string): Name (can include a path) for the file containing all the pulsation frequencies of the grid.
  • parameters (list of strings): List of parameters varied in the computed grid, so these are taken from the name of the summary files, and included in the 1 file containing all the info of the whole grid.
  • nr_cpu (int): Number of worker processes to use in multiprocessing. The default 'None' will use the number returned by os.cpu_count().
def all_freqs_from_summary(gyre_summary_file, parameters):
61def all_freqs_from_summary(gyre_summary_file, parameters):
62    """
63    Extract model parameters and pulsation frequencies from a GYRE summary file
64
65    Parameters
66    ----------
67    gyre_summary_file: string
68        path to the GYRE summary file
69    parameters: list of strings
70        List of input parameters varied in the computed grid,
71        so these are read from the filename and included in returned line.
72
73    Returns
74    ----------
75    param_dict: dict
76        Dictionary containing all the model parameters and pulsation frequencies of the GYRE summary file.
77    """
78
79    _, data = sf.read_hdf5(gyre_summary_file)
80    param_dict = sf.get_param_from_filename(gyre_summary_file, parameters, values_as_float=True)
81
82    # Arrange increasing in radial order
83    for j in range(len(data["freq"]) - 1, -1, -1):
84        n_pg = data["n_pg"][j]
85        if abs(n_pg) < 10:
86            n_pg = f"{sf.sign(n_pg)}00{abs(n_pg)}"
87        elif abs(n_pg) < 100:
88            n_pg = f"{sf.sign(n_pg)}0{abs(n_pg)}"
89        param_dict.update({f"n_pg{n_pg}": data["freq"][j][0]})
90
91    return param_dict

Extract model parameters and pulsation frequencies from a GYRE summary file

Parameters
  • gyre_summary_file (string): path to the GYRE summary file
  • parameters (list of strings): List of input parameters varied in the computed grid, so these are read from the filename and included in returned line.
Returns
  • param_dict (dict): Dictionary containing all the model parameters and pulsation frequencies of the GYRE summary file.