comet-toolkit / obsarray

Measurement uncertainty handling in Python
GNU Lesser General Public License v3.0
7 stars 1 forks source link

Method to safely rename uncertainty variable #10

Closed pdevis closed 9 months ago

pdevis commented 1 year ago

A function to rename a variable, or a dimension would be useful, which then also renames all the uncertainty variables, and associated attributes etc in the relevant uncertainty and err_corr variables. Something along the line of:

def rename_var(self, dataset, varname, varname_new, wavvar, wavvar_new):
    replace_dict = {wavvar: wavvar_new}
    for var in dataset.variables:
        if varname in var:
            replace_dict[var] = var.replace(varname, varname_new)
    dataset_temp = dataset.rename(replace_dict)
    if "unc_comps" in dataset_temp[varname_new].attrs.keys():
        dataset_temp[varname_new].attrs["unc_comps"] = [
            comp.replace(varname, varname_new)
            for comp in dataset_temp[varname_new].attrs["unc_comps"]
        ]
        for comp in dataset_temp[varname_new].attrs["unc_comps"]:
            for i in range(2):
                if wavvar == dataset_temp[comp].attrs["err_corr_%s_dim"%(i+1)]:
                    dataset_temp[comp].attrs["err_corr_%s_dim"%(i+1)]=wavvar_new
                if len(dataset_temp[comp].attrs["err_corr_%s_params"%(i+1)]) > 0:
                    if "err_corr" in dataset_temp[comp].attrs["err_corr_%s_params"%(i+1)][0]:
                        dataset_temp[comp].attrs["err_corr_%s_params"%(i+1)][0] = \
                        dataset_temp[comp].attrs["err_corr_%s_params"%(i+1)][0].replace(varname, varname_new)
    return dataset_temp 
shunt16 commented 9 months ago

Separated the dimension point into #21