common.py 6.9 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
import copy
from dataclasses import dataclass, field
Wei Shoulin's avatar
Wei Shoulin committed
3
import json
Wei Shoulin's avatar
Wei Shoulin committed
4
5
6

def from_proto_model_list(clazz, records):
    return [clazz().from_proto_model(r) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
7

Wei Shoulin's avatar
mc3    
Wei Shoulin committed
8
9
def from_dict_list(clazz, records):
    return [clazz().from_dict(r) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
10

Wei Shoulin's avatar
Wei Shoulin committed
11
12
def to_proto_model_list(protoObjClazz, records):
    return [r.to_proto_model(protoObjClazz) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
13
14

def default_field(obj):
Wei Shoulin's avatar
Wei Shoulin committed
15
16
17
18
    return field(default_factory=lambda: copy.copy(obj))

class Dict(dict):
    __setattr__ = dict.__setitem__
Wei Shoulin's avatar
Wei Shoulin committed
19
20
21
22
    __getattr__ = dict.__getitem__    
    def __init__(self, **args):
        super(Dict, self).__init__()
        self.update(args)
Wei Shoulin's avatar
Wei Shoulin committed
23
24
25
26
27
28
29
30
31
 
def dict_to_object(dictObj):
    if not isinstance(dictObj, dict):
        return dictObj
    inst=Dict()
    for k,v in dictObj.items():
        inst[k] = dict_to_object(v)
    return inst

Wei Shoulin's avatar
Wei Shoulin committed
32
@dataclass
Wei Shoulin's avatar
Wei Shoulin committed
33
34
35
36
37
38
39
class BaseModel:
    def from_dict(self, data = {}):
        for k in self.__dataclass_fields__.keys():
            self.__setattr__(k, data.get(k, None))
        return self

    def from_proto_model(self, record):
Wei Shoulin's avatar
new id    
Wei Shoulin committed
40
41
        if record is None:
            return None
Wei Shoulin's avatar
Wei Shoulin committed
42
        for k in self.__dataclass_fields__.keys():
Wei Shoulin's avatar
Wei Shoulin committed
43
            if k == 'header':
Wei Shoulin's avatar
bug    
Wei Shoulin committed
44
45
                if record.__getattribute__(k):
                    self.__setattr__(k, json.loads(record.__getattribute__(k)))
Wei Shoulin's avatar
Wei Shoulin committed
46
47
            else:
                self.__setattr__(k, record.__getattribute__(k))
Wei Shoulin's avatar
Wei Shoulin committed
48
        return self
Wei Shoulin's avatar
Wei Shoulin committed
49
50
51
52
53
54
    
    def to_proto_model(self, protoObjClazz):
        obj = protoObjClazz()
        for k in self.__dataclass_fields__.keys():
            v = self.__getattribute__(k)
            if v is not None:
Wei Shoulin's avatar
Wei Shoulin committed
55
                if isinstance(v, list) or isinstance(v, tuple):
Wei Shoulin's avatar
Wei Shoulin committed
56
                    obj.__getattribute__(k).extend(v)
Wei Shoulin's avatar
Wei Shoulin committed
57
                elif isinstance(v, dict):
Wei Shoulin's avatar
Wei Shoulin committed
58
59
60
61
                    if k == 'header':
                        obj.__setattr__(k, json.dumps(v))
                    else:
                        obj.__getattribute__(k).update(v)                    
Wei Shoulin's avatar
Wei Shoulin committed
62
63
64
                else:
                    obj.__setattr__(k, v)
        return obj
Wei Shoulin's avatar
Wei Shoulin committed
65

Wei Shoulin's avatar
Wei Shoulin committed
66
@dataclass
Wei Shoulin's avatar
Wei Shoulin committed
67
class Gaia3Record(BaseModel):
Wei Shoulin's avatar
Wei Shoulin committed
68
69
70
71
    SolutionId: int = -1
    Designation: str = ''
    SourceId: int = -1
    RandomIndex: int = -1
Wei Shoulin's avatar
Wei Shoulin committed
72
73
74
    RefEpoch: float = -1
    Ra: float = -1
    RaError: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
75
    Dec: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
76
77
78
79
80
81
82
83
84
    DecError: float = -1
    Parallax: float = -1
    ParallaxError: float = -1
    ParallaxOverError: float = -1
    Pm: float = -1
    Pmra: float = -1
    PmraError: float = -1
    Pmdec: float = -1
    PmdecError: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
85
    RaDecCorr: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    RaParallaxCorr: float = -1
    RaPmraCorr: float = -1
    RaPmdecCorr: float = -1
    DecParallaxCorr: float = -1
    DecPmraCorr: float = -1
    DecPmdecCorr: float = -1
    ParallaxPmraCorr: float = -1
    ParallaxPmdecCorr: float = -1
    PmraPmdecCorr: float = -1
    AstrometricNObsAl: int = -1
    AstrometricNObsAc: int = -1
    AstrometricNGoodObsAl: int = -1
    AstrometricNBadObsAl: int = -1
    AstrometricGofAl: float = -1
    AstrometricChi2Al: float = -1
    AstrometricExcessNoise: float = -1
Wei Shoulin's avatar
bug    
Wei Shoulin committed
102
    AstrometricExcessNoiseSig: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
103
    AstrometricParamsSolved: int = -1
Wei Shoulin's avatar
Wei Shoulin committed
104
    AstrometricPrimaryFlag: int = 0
Wei Shoulin's avatar
Wei Shoulin committed
105
106
107
108
109
110
    NuEffUsedInAstrometry: float = -1
    Pseudocolour: float = -1
    PseudocolourError: float = -1
    RaPseudocolourCorr: float = -1
    DecPseudocolourCorr: float = -1
    ParallaxPseudocolourCorr: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
111
    PmraPseudocolourCorr: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
112
113
114
    PmdecPseudocolourCorr: float = -1
    AstrometricMatchedTransits: int = -1
    VisibilityPeriodsUsed: int = -1
Wei Shoulin's avatar
Wei Shoulin committed
115
    AstrometricSigma5dMax: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    MatchedTransits: int = -1
    NewMatchedTransits: int = -1
    MatchedTransitsRemoved: int = -1
    IpdGofHarmonicAmplitude: float = -1
    IpdGofHarmonicPhase: float = -1
    IpdFracMultiPeak: int = -1
    IpdFracOddWin: int = -1
    Ruwe: float = -1
    ScanDirectionStrengthK1: float = -1
    ScanDirectionStrengthK2: float = -1
    ScanDirectionStrengthK3: float = -1
    ScanDirectionStrengthK4: float = -1
    ScanDirectionMeanK1: float = -1
    ScanDirectionMeanK2: float = -1
    ScanDirectionMeanK3: float = -1
    ScanDirectionMeanK4: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
132
    DuplicatedSource: int = 0
Wei Shoulin's avatar
Wei Shoulin committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    PhotGNObs: int = -1
    PhotGMeanFlux: float = -1
    PhotGMeanFluxError: float = -1
    PhotGMeanFluxOverError: float = -1
    PhotGMeanMag: float = -1
    PhotBpNObs: int = -1
    PhotBpMeanFlux: float = -1
    PhotBpMeanFluxError: float = -1
    PhotBpMeanFluxOverError: float = -1
    PhotBpMeanMag: float = -1
    PhotRpNObs: int = -1
    PhotRpMeanFlux: float = -1
    PhotRpMeanFluxError: float = -1
    PhotRpMeanFluxOverError: float = -1
    PhotRpMeanMag: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
148
    PhotBpRpExcessFactor: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
149
150
151
152
    PhotBpNContaminatedTransits: float = -1
    PhotBpNBlendedTransits: float = -1
    PhotRpNContaminatedTransits: float = -1
    PhotRpNBlendedTransits: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
153
    PhotProcMode: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
154
    BpRp: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    BpG: float = -1
    GRp: float = -1
    RadialVelocity: float = -1
    RadialVelocityError: float = -1
    RvMethodUsed: float = -1
    RvNbTransits: float = -1
    RvNbDeblendedTransits: float = -1
    RvVisibilityPeriodsUsed: float = -1
    RvExpectedSigToNoise: float = -1
    RvRenormalisedGof: float = -1
    RvChisqPvalue: float = -1
    RvTimeDuration: float = -1
    RvAmplitudeRobust: float = -1
    RvTemplateTeff: float = -1
    RvTemplateLogg: float = -1
    RvTemplateFeH: float = -1
    RvAtmParamOrigin: float = -1
    Vbroad: float = -1
    VbroadError: float = -1
    VbroadNbTransits: float = -1
    GrvsMag: float = -1
    GrvsMagError: float = -1
    GrvsMagNbTransits: float = -1
    RvsSpecSigToNoise: float = -1
    PhotVariableFlag: str = ''
Wei Shoulin's avatar
Wei Shoulin committed
180
181
182
183
    L: float = -1
    B: float = -1
    EclLon: float = -1
    EclLat: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    InQsoCandidates: int = 0
    InGalaxyCandidates: int = 0
    NonSingleStar: int = -1
    HasXpContinuous: int = 0
    HasXpSampled: int = 0
    HasRvs: int = 0
    HasEpochPhotometry: int = 0
    HasEpochRv: int = 0
    HasMcmcGspphot: int = 0
    HasMcmcMsc: int = 0
    InAndromedaSurvey: int = 0
    ClassprobDscCombmodQuasar: float = -1
    ClassprobDscCombmodGalaxy: float = -1
    ClassprobDscCombmodStar: float = -1
    TeffGspphot: float = -1
    TeffGspphotLower: float = -1
    TeffGspphotUpper: float = -1
    LoggGspphot: float = -1
    LoggGspphotLower: float = -1
    LoggGspphotUpper: float = -1
    MhGspphot: float = -1
    MhGspphotLower: float = -1
    MhGspphotUpper: float = -1
    DistanceGspphot: float = -1
    DistanceGspphotLower: float = -1
    DistanceGspphotUpper: float = -1
    AzeroGspphot: float = -1
    AzeroGspphotLower: float = -1
    AzeroGspphotUpper: float = -1
    AgGspphot: float = -1
    AgGspphotLower: float = -1
    AgGspphotUpper: float = -1
    EbpminrpGspphot: float = -1
    EbpminrpGspphotLower: float = -1
    EbpminrpGspphotUpper: float = -1
    LibnameGspphot: str = ''
Wei Shoulin's avatar
Wei Shoulin committed
220
221
222
223
    NS8HIdx: int = -1
    NS16HIdx: int = -1
    NS32HIdx: int = -1
    NS64HIdx: int = -1
Wei Shoulin's avatar
Wei Shoulin committed
224
225
    FileIdx: int = -1
    
Wei Shoulin's avatar
Wei Shoulin committed
226
227
    def __init__(self):
        self.AstrometricNObsAc = 0