flag_definition.py 3.55 KB
Newer Older
Feng Lu's avatar
Feng Lu committed
1
2
3
4
5
6
7
8
9
10
11
12
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def flag(original_flag, isQualified=None, \
                        isBadPixel=None, \
                        isHotPixel=None, \
                        isSaturationContaminated=None, \
                        isCosmicRayContaminated=None,\
                        isSpacecraftContaminated=None,\
                        isGhostContaminated=None,\
                        isScatterLightContaminated=None):
# 合格像元/坏像元	0	可以用于有效科学研究的像元
# 热像元	1	因为宇宙线或其它外部原因造成的不可用于有效科学研究的像元
# 流量饱和溢出像元	2	饱和像元及流量溢出污染的像元
# 宇宙线像元	3	宇宙线污染的像元
# 卫星拖尾像元	4	卫星或者人造移动天体轨迹污染的像元
# 鬼像像元	5	鬼像污染的像元
# 散射光像元	6	散射光污染的像元(包括dragon breath)

# if a pixel is qualified, then, all other flags will be ignored

    bad_pixel_bit_pos = 0
    hot_pixel_bit_pos = 1
    sat_pixel_bit_pos = 2
    csr_pixel_bit_pos = 3
    spc_pixel_bit_pos = 4
    gst_pixel_bit_pos = 5
    sct_pixel_bit_pos = 6
    
    new_flag = original_flag
    
    # # COMMENTED: Is "isQualified" flag exclusive with all other flags?
    # 
    if isQualified==True:
        new_flag = 0
        return  new_flag

    bad_pixel_flag = False

    if isHotPixel!=None:
        new_flag = setBitValue(new_flag, hot_pixel_bit_pos, isHotPixel)
        bad_pixel_flag = True
    if isSaturationContaminated!=None:
        new_flag = setBitValue(new_flag, sat_pixel_bit_pos, isSaturationContaminated)
        bad_pixel_flag = True
    if isCosmicRayContaminated!=None:
        new_flag = setBitValue(new_flag, csr_pixel_bit_pos, isCosmicRayContaminated)
        bad_pixel_flag = True
    if isSpacecraftContaminated!=None:
        new_flag = setBitValue(new_flag, spc_pixel_bit_pos, isSpacecraftContaminated)
        bad_pixel_flag = True
    if isGhostContaminated!=None:
        new_flag = setBitValue(new_flag, gst_pixel_bit_pos, isGhostContaminated)
        bad_pixel_flag = True
    if isScatterLightContaminated!=None:
        new_flag = setBitValue(new_flag, sct_pixel_bit_pos, isScatterLightContaminated)
        bad_pixel_flag = True
    if isBadPixel==True or bad_pixel_flag==True: 
        # if the pixel is defined as bad, or is defined as other non-ideal cases, the pixel will be marked as bad as well 
        new_flag = setBitValue(new_flag, bad_pixel_bit_pos, True)

    return new_flag

def setBitValue(dataIn, bitPosition, bitValue):
# set a bit at bitPosition to be bitValue in dataIn and return the new value
    if bitPosition<0:
        raise Exception('bit position should always be a non negative integer')
   
    if (bitValue!=True)&(bitValue!=False):
        raise Exception('bit value should always be either 0 or 1, or, True or False')

    dataOut = dataIn
    if bitValue == True:
        dataOut = dataOut | (bitValue<<bitPosition)
    else:
        dataOut = dataIn>>(bitPosition+1)<<(bitPosition+1)
        dataOut = dataOut + (dataIn&((1<<bitPosition)-1))    
    return dataOut

def getBitValue(dataIn, bitPosition):
    # get the bit value at bitPosition in dataIn and return the bit value
    if bitPosition<0:
        raise Exception('bit position should always be a non negative integer')
    
    return (dataIn >> bitPosition) & 1

if __name__ == "__main__":
    original_flag = 0
    print(f'{bin(original_flag)}')
    print(f'{bin(flag(original_flag, isHotPixel=True, isCosmicRayContaminated=True, isSpacecraftContaminated=True, isScatterLightContaminated=True))}')
    print('should be 0b10110101')