def flag_criteria(flags):
    """
    Flag Criteria for Filtering Elements
    ====================================

    In data processing and analysis, flag criteria are essential for selecting and managing elements based on specific conditions or quality assessments. Pikobs uses a **bitmask system** to filter data effectively, focusing on elements that meet predefined standards or have undergone particular processing stages.

    Standard Flag Criteria
    ----------------------

    When using the ``--flags_criteria`` parameter in Pikobs, the system checks specific bits to determine if an observation should be included.

    ``all``
        **No restrictions:** Selects all available elements. No bitmask filters are applied.

    ``assimilee``
        **Assimilated elements:** 
        
        * **Required Active Bits:** **BIT12** (4096) must be active (1).
        * Indicates that the observation has actively influenced the analysis. By default, this includes both clear and cloudy sky conditions.

    ``bgckalt``
        **Approved by Background Check:** 
        
        * **Required Inactive Bits:** **BIT9** (512), **BIT11** (2048), and **BIT8** (256) must be inactive (0).
        * Removes observations rejected due to AO quality control, thinning/canal selection processes, and those on the blacklist.

    ``bgckalt_qc``
        **Approved by Background Check and QC-Var:** 
        
        * **Required Inactive Bits:** **BIT9** (512) and **BIT11** (2048) must be inactive (0).

    ``monitoring``
        **Monitoring filter:** 
        
        * **Required Inactive Bits:** **BIT9** (512) and **BIT7** (128) must be inactive (0).
        * Filters out observations rejected by AO quality control or satellite QC.

    ``postalt``
        **Post-Analysis filter:** 
        
        * **Required Inactive Bits:** **BIT17** (131072), **BIT9** (512), **BIT11** (2048), and **BIT8** (256) must be inactive (0).
        * Removes data rejected by QC-Var, AO quality control, thinning/canal processes, and blacklisted observations.

    Sky Condition Filters (Radiance Data)
    -------------------------------------

    For satellite radiances, specific flags can be used to filter assimilated observations based on cloud conditions using **BIT23** (Cloud-affected radiance).

    ``clear_sky``
        **Clear Sky Only:**
        
        * **Required Active Bits:** **BIT12** (4096) must be active.
        * **Required Inactive Bits:** **BIT23** (8388608) must be strictly inactive (0).
        * Filters assimilated radiances that are completely free of cloud contamination.

    ``cloudy_sky``
        **Cloudy Sky Only:**
        
        * **Required Active Bits:** **BIT23** (8388608) must be active (1).
        * Filters radiances specifically flagged as being affected by clouds.

    ``clear_cloud_sky``
        **All-Sky (Clear + Cloudy):**
        
        * **Required Active Bits:** **BIT12** (4096) must be active.
        * This is the standard behavior for all-sky assimilation, accepting elements regardless of whether BIT23 is active or inactive.

    Active Bits Description Reference
    ---------------------------------

    The following table defines all active bits, their decimal values, and their meaning within the quality control (QC) and assimilation processes:

    .. list-table:: Bitmask Definitions and Values
       :widths: 15 15 70
       :header-rows: 1
       :align: center

       * - Bit Index
         - Decimal Value
         - Description / Rejection Reason
       * - **BIT0**
         - 1
         - Modified or generated by ADE.
       * - **BIT1**
         - 2
         - Element that exceeds a climatological extreme or fails consistency test.
       * - **BIT2**
         - 4
         - Erroneous element.
       * - **BIT3**
         - 8
         - Potentially erroneous element.
       * - **BIT4**
         - 16
         - Dubious element.
       * - **BIT5**
         - 32
         - Interpolated element, generated by DERIVATE.
       * - **BIT6**
         - 64
         - Corrected by DERIVATE sequence or bias correction.
       * - **BIT7**
         - 128
         - Rejected by satellite QC.
       * - **BIT8**
         - 256
         - Element rejected because it is on a blacklist.
       * - **BIT9**
         - 512
         - Element rejected by AO quality control (Background Check or QC-Var).
       * - **BIT10**
         - 1024
         - Generated by AO.
       * - **BIT11**
         - 2048
         - Rejected by a selection process (thinning or canal).
       * - **BIT12**
         - 4096
         - Assimilated (successfully used in analysis).
       * - **BIT13**
         - 8192
         - Comparison against test field, level 1.
       * - **BIT14**
         - 16384
         - Comparison against test field, level 2.
       * - **BIT15**
         - 32768
         - Comparison against test field, level 3.
       * - **BIT16**
         - 65536
         - Rejected by comparison against test field (Background Check).
       * - **BIT17**
         - 131072
         - Rejected by QC-Var.
       * - **BIT18**
         - 262144
         - Not used due to orography.
       * - **BIT19**
         - 524288
         - Not used due to land-sea mask.
       * - **BIT20**
         - 1048576
         - Aircraft position error detected by TrackQC.
       * - **BIT21**
         - 2097152
         - Inconsistency detected by a CQ process.
       * - **BIT22**
         - 4194304
         - Rejected due to transmittance above model top.
       * - **BIT23**
         - 8388608
         - Cloud-affected radiance.
    """ 

    # Define bit masks for flag criteria
    BIT6_BIASCORR = 64  
    BIT7_REJ = 128  
    BIT8_BLACKLIST = 256
    BIT9_REJ = 512
    BIT11_SELCOR = 2048  
    BIT12_VUE = 4096 
    BIT16_REJBGCK = 65536
    BIT17_QCVAR = 131072
    BIT23_CLOUD = 8388608  # Added BIT23 for sky conditions

    # Generate filtering criteria based on the selected flag
    if flags == "all":
        FLAG = "flag >= 0"
    elif flags == "assimilee":
        FLAG = f"and (flag & {BIT12_VUE}) = {BIT12_VUE}"
    elif flags == "bgckalt":
        FLAG = f"and (flag & {BIT9_REJ}) = 0 and (flag & {BIT11_SELCOR}) = 0 and (flag & {BIT8_BLACKLIST}) = 0"
    elif flags == "bgckalt_qc":
        FLAG = f"and (flag & {BIT9_REJ}) = 0 and (flag & {BIT11_SELCOR}) = 0"
    elif flags == "monitoring":
        FLAG = f"and (flag & {BIT9_REJ}) = 0 and (flag & {BIT7_REJ}) = 0"
    elif flags == "postalt":
        FLAG = f"and (flag & {BIT17_QCVAR}) = 0 and (flag & {BIT9_REJ}) = 0 and (flag & {BIT11_SELCOR}) = 0 and (flag & {BIT8_BLACKLIST}) = 0"
    elif flags == "rejets_qc":
        FLAG = f"and (flag & {BIT9_REJ}) = {BIT9_REJ}"
    elif flags == "rejets_bgck":
        FLAG = f"and (flag & {BIT16_REJBGCK}) = {BIT16_REJBGCK}"
    elif flags == "bias_corr":
        FLAG = f"and (flag & {BIT6_BIASCORR}) = {BIT6_BIASCORR}"
    elif flags == "qc":
        FLAG = f"and (flag & {BIT9_REJ}) = 0"
        
    # --- New Sky Condition Filters ---
    elif flags == "clear_sky":
        FLAG = f"and (flag & {BIT12_VUE}) = {BIT12_VUE} and (flag & {BIT23_CLOUD}) = 0"
    elif flags == "cloudy_sky":
        FLAG = f"and (flag & {BIT23_CLOUD}) = {BIT23_CLOUD}"
    elif flags == "clear_cloud_sky":
        FLAG = f"and (flag & {BIT12_VUE}) = {BIT12_VUE}" # Same logical operation as 'assimilee'
    
    else:
        raise ValueError(f'Invalid flag option: {flags}')

    return FLAG
