Skip to content

Aerosol PPE utility functions

get_namlist_string

get_namlist_string(
    look_for,
    fincl_n,
    filename_output_vars,
    operation,
    category_exclude=None,
    category_include=None,
)

Create a namelist string for the specified frequency and operation based on the output variables defined in a CSV file.

Source code in tinkertool/utils/write_out_namelist_opt_fincl.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def get_namlist_string(look_for, 
                       fincl_n, 
                       filename_output_vars, 
                       operation,
                       category_exclude=None,
                       category_include=None):
    """
    Create a namelist string for the specified frequency and operation based on the output variables defined in a CSV file.
    """
    df_output = pd.read_csv(filename_output_vars, index_col=0, header=1)
    df_output = df_output.rename(columns={
        'category': 'category',
       'Frequency (mon, 3-h) and spatial (e.g. 3h-global, 3h-station, mon-global, mon-region)':'Freq',
       'Operation (A,I,or max)': 'opflag',
       'Variable name:': 'varname',
       'Keep/reject: mon-global': 'mon-global(k/r)',
       'Keep/reject: 3-h-station':'3-h-station(k/r)', 
       'Dimensions (2D,3D)': '2D_3D', 
       'comment': 'comment',
       'nl history flagg': 'nl_history_flagg'
    }
    )

    df_output_mon_glob = df_output[
        df_output['Freq'].apply(lambda x: look_for in str(x))
    ]
    temp = df_output_mon_glob["opflag"].str.contains("A", na=False).copy()
    df_output_mon_glob['A'] = temp
    temp = df_output_mon_glob["opflag"].str.contains("I", na=False).copy()
    df_output_mon_glob['I'] = temp
    df_output_mon_glob = df_output_mon_glob[
        df_output_mon_glob[f"{look_for}(k/r)"].apply(
            lambda x: (("K" in str(x))) or ("k" in str(x))
        )
    ]
    if category_include is not None:
        df_output_mon_glob = df_output_mon_glob.loc[df_output_mon_glob['category'].apply(lambda x: x in category_include)]
    if category_exclude is not None:
        df_output_mon_glob = df_output_mon_glob.loc[~df_output_mon_glob['category'].apply(lambda x: x in category_exclude)]
    # select where "Operation ('A','I',or max)" is equal to operation
    operation_filter = df_output_mon_glob[operation] == True
    df_output_mon_glob = df_output_mon_glob[operation_filter]
    df_output_mon_glob.loc[:, "namelist_name"] = df_output_mon_glob.index
    namelist_name = df_output_mon_glob["namelist_name"].to_list()
    namelist_str = "fincl" + str(fincl_n) + " = "
    for i, name in enumerate(namelist_name):
        namelist_str += f"{name}\n"
    namelist_str = namelist_str[:-2]
    return namelist_str

write_out_station_nm_string

write_out_station_nm_string(
    path_station_file, history_field=2
)

Write out a string of station names for use in a namelist.

:param path_station_file: :return:

Source code in tinkertool/utils/write_out_station_nl_string.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def write_out_station_nm_string(path_station_file, history_field=2):
    """
    Write out a string of station names for use in a namelist.

    :param path_station_file:
    :return:
    """
    # %%
    if path_station_file is None:
        path_station_file = "input_files/stations_combined.csv"
    # %%
    combined_df = pd.read_csv(path_station_file, index_col=0)

    dft = combined_df.copy()
    dft["lon"] = dft["lon"].astype(float)
    dft["lat"] = dft["lat"].astype(float)

    dft["lon_str"] = dft["lon"].apply(lambda x: lon_str(x))
    dft["lat_str"] = dft["lat"].apply(lambda x: lat_str(x))

    dft["fincl2lonlat"] = dft.T.apply(lambda x: "%s_%s" % (x["lon_str"], x["lat_str"]))
    str_namelist = dft["fincl2lonlat"].to_list()
    str_namelist = "\n".join(str_namelist)
    nl_string = f"fincl{history_field:d}lonlat={str_namelist}"
    # %%
    return nl_string