aodn / imos-toolbox

Graphical tool for QC'ing and NetCDF'ing oceanographic datasets
GNU General Public License v3.0
46 stars 31 forks source link

(Feat) Add failed QC tests information into new ancillary variable [*]_failed_tests #797

Closed lbesnard closed 1 year ago

lbesnard commented 1 year ago

Some test files generated with this draft PR: https://cloudstor.aarnet.edu.au/plus/s/RKMb19mu8O6GSQx

a python function to split a number into the sum of powers of 2

import xarray as xr
import math

def split_into_powers_of_2(number):
    number = int(number) 
    powers = []
    power = 0

    while number > 0:
        if number & 1:
            powers.append(2**power)
        power += 1
        number >>= 1

    return powers
ncfile='/tmp/IMOS_ANMN-NSW_AETVZ_20110620T135500Z_PH100_FV01_PH100-1106-Sentinel-or-Monitor-Workhorse-ADCP-109.5_END-20110831T135500Z_C-20230629T062412Z.nc'

ds = xr.open_dataset(ncfile)

value = ds.VCUR_failed_tests.data[0][1]  # just a random point we want to see which QC routine failed

# Split the value into powers of 2
flag_masks = split_into_powers_of_2(value)

# Get the corresponding flag meanings
flag_meanings = ds.VCUR_failed_tests.flag_meanings.split(' ')

# Match flag_masks with flag_meanings
matched_flags = []
for mask in flag_masks:
    index = int(math.log2(mask))  # Calculate the index corresponding to the mask
    matched_flags.append((mask, flag_meanings[index]))

# Print the matched flags
for mask, meaning in matched_flags:
    print(f"Flag Mask: {mask}, Meaning: {meaning}")

@mhidas, @evacougnon FYI

mhidas commented 1 year ago

:+1: Thanks @lbesnard Here's how I did the same thing...

def map_failed_tests(var):
    """Convert a _failed_tests variable to a list of strings.
    var can be an xarray.DataArray or a netCDF4.Variable
    """

    flag_map = dict(zip(var.flag_masks, var.flag_meanings.split()))
    def map_one(value):    
        ms = [flag_map[i] for i in flag_map if value & i]
        return ", ".join(ms)

    return list(map_one(v) for v in var.data)