Coverage for tcvx21/record_c/template_writer_m.py: 92%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

39 statements  

1""" 

2We want to make sure that each data source -- the experiment (TCV) and the different simulations --  

3all use the same data structure. This helps to ensure that no data is missing from the data source 

4 

5By using a data template, we can simplify the post-processing routines since all the data sources 

6will be structured the same way 

7 

8We do this by first mapping a data-source onto a standard dictionary, and then writing that standard 

9dictionary into a NetCDF file. 

10 

11The standard dictionary is defined via the observables.json file. This is a simple .json file,  

12which is defined to match the dictionary in this file 

13 

14The structure of the dictionary is as follows 

15 

16Top-level keys give observable positions/diagnostics 

17 Each key of diagnostics gives a different diagnostic 

18 Each diagnostic has 

19 name 

20 R_sample_points and Z_sample_points which give sample points for the reference shot. 

21 N.b. These aren't necessarily the comparison points, they're just points where you can 

22 take the data from, and then the routines will interpolate to the observable positions 

23 observables from that diagnostic 

24 Each observable in observables has 

25 name 

26 dimensionality (i.e. 0D, 1D or 2D) 

27 units as a pint-compatible string 

28 experimental_hierarchy primacy hierarchy for the experiment 

29 simulation_hierarchy (set to an error-flag of -1) which should be set by each code 

30 values giving the magnitude of the values in units of units 

31 errors giving the magnitude of the uncertainty in units of units 

32 For 1D data, 

33 Ru giving the upstream-mapped radial distance to the separatrix 

34 Ru_units giving the units of the upstream-mapped radial distance to the separatrix 

35 For 1D and 2D data, 

36 Zx giving the vertical position relative to the X-point 

37 Zx_units giving the units of the vertical position relative to the X-point 

38""" 

39from pathlib import Path 

40from scipy.io import savemat 

41from tcvx21 import write_to_json 

42 

43 

44observables = { 

45 "LFS-LP": { 

46 "name": "Low-field-side target Langmuir probes", 

47 "observables": { 

48 "density": { 

49 "name": "Plasma density", 

50 "units": "1/m^3", 

51 "experimental_hierarchy": 2, 

52 }, 

53 "electron_temp": { 

54 "name": "Electron temperature", 

55 "units": "eV", 

56 "experimental_hierarchy": 2, 

57 }, 

58 "ion_temp": { 

59 "name": "Ion temperature", 

60 "units": "eV", 

61 "experimental_hierarchy": -1, 

62 }, 

63 "potential": { 

64 "name": "Plasma potential", 

65 "units": "V", 

66 "experimental_hierarchy": 2, 

67 }, 

68 "current": { 

69 "name": "Parallel current", 

70 "units": "A/m^2", 

71 "experimental_hierarchy": 1, 

72 }, 

73 "current_std": { 

74 "name": "Standard deviation of the parallel current", 

75 "units": "A/m^2", 

76 "experimental_hierarchy": 1, 

77 }, 

78 "jsat": { 

79 "name": "Ion saturation current", 

80 "units": "A/m^2", 

81 "experimental_hierarchy": 1, 

82 }, 

83 "jsat_std": { 

84 "name": "Standard deviation of the ion saturation current", 

85 "units": "A/m^2", 

86 "experimental_hierarchy": 1, 

87 }, 

88 "jsat_skew": { 

89 "name": "Skew of the ion saturation current", 

90 "units": "", 

91 "experimental_hierarchy": 1, 

92 }, 

93 "jsat_kurtosis": { 

94 "name": "Pearson kurtosis of the ion saturation current", 

95 "units": "", 

96 "experimental_hierarchy": 1, 

97 }, 

98 "vfloat": { 

99 "name": "Floating potential", 

100 "units": "V", 

101 "experimental_hierarchy": 1, 

102 }, 

103 "vfloat_std": { 

104 "name": "Standard deviation of the floating potential", 

105 "units": "V", 

106 "experimental_hierarchy": 1, 

107 }, 

108 }, 

109 }, 

110 "LFS-IR": { 

111 "name": "Low-field-side target infrared camera", 

112 "observables": { 

113 "q_parallel": { 

114 "name": "Parallel heat flux", 

115 "units": "W/m^2", 

116 "experimental_hierarchy": 2, 

117 } 

118 } 

119 }, 

120 "HFS-LP": { 

121 "name": "High-field-side target Langmuir probes", 

122 "observables": { 

123 "density": { 

124 "name": "Plasma density", 

125 "units": "1/m^3", 

126 "experimental_hierarchy": 2, 

127 }, 

128 "electron_temp": { 

129 "name": "Electron temperature", 

130 "units": "eV", 

131 "experimental_hierarchy": 2, 

132 }, 

133 "ion_temp": { 

134 "name": "Ion temperature", 

135 "units": "eV", 

136 "experimental_hierarchy": -1, 

137 }, 

138 "potential": { 

139 "name": "Plasma potential", 

140 "units": "V", 

141 "experimental_hierarchy": 2, 

142 }, 

143 "current": { 

144 "name": "Parallel current", 

145 "units": "A/m^2", 

146 "experimental_hierarchy": 1, 

147 }, 

148 "current_std": { 

149 "name": "Standard deviation of the parallel current", 

150 "units": "A/m^2", 

151 "experimental_hierarchy": 1, 

152 }, 

153 "jsat": { 

154 "name": "Ion saturation current", 

155 "units": "A/m^2", 

156 "experimental_hierarchy": 1, 

157 }, 

158 "jsat_std": { 

159 "name": "Standard deviation of the ion saturation current", 

160 "units": "A/m^2", 

161 "experimental_hierarchy": 1, 

162 }, 

163 "jsat_skew": { 

164 "name": "Skew of the ion saturation current", 

165 "units": "", 

166 "experimental_hierarchy": 1, 

167 }, 

168 "jsat_kurtosis": { 

169 "name": "Pearson kurtosis of the ion saturation current", 

170 "units": "", 

171 "experimental_hierarchy": 1, 

172 }, 

173 "vfloat": { 

174 "name": "Floating potential", 

175 "units": "V", 

176 "experimental_hierarchy": 1, 

177 }, 

178 "vfloat_std": { 

179 "name": "Standard deviation of the floating potential", 

180 "units": "V", 

181 "experimental_hierarchy": 1, 

182 } 

183 } 

184 }, 

185 "FHRP": { 

186 "name": "Outboard midplane reciprocating probe", 

187 "observables": { 

188 "density": { 

189 "name": "Plasma density", 

190 "units": "1/m^3", 

191 "experimental_hierarchy": 2, 

192 }, 

193 "electron_temp": { 

194 "name": "Electron temperature", 

195 "units": "eV", 

196 "experimental_hierarchy": 2, 

197 }, 

198 "ion_temp": { 

199 "name": "Ion temperature", 

200 "units": "eV", 

201 "experimental_hierarchy": -1, 

202 }, 

203 "potential": { 

204 "name": "Plasma potential", 

205 "units": "V", 

206 "experimental_hierarchy": 2, 

207 }, 

208 "jsat": { 

209 "name": "Ion saturation current", 

210 "units": "A/m^2", 

211 "experimental_hierarchy": 1, 

212 }, 

213 "jsat_std": { 

214 "name": "Standard deviation of the ion saturation current", 

215 "units": "A/m^2", 

216 "experimental_hierarchy": 1, 

217 }, 

218 "jsat_skew": { 

219 "name": "Skew of the ion saturation current", 

220 "units": "", 

221 "experimental_hierarchy": 1, 

222 }, 

223 "jsat_kurtosis": { 

224 "name": "Pearson kurtosis of the ion saturation current", 

225 "units": "", 

226 "experimental_hierarchy": 1, 

227 }, 

228 "vfloat": { 

229 "name": "Floating potential", 

230 "units": "V", 

231 "experimental_hierarchy": 1, 

232 }, 

233 "vfloat_std": { 

234 "name": "Standard deviation of the floating potential", 

235 "units": "V", 

236 "experimental_hierarchy": 1, 

237 }, 

238 "mach_number": { 

239 "name": "Plasma velocity normalised to local sound speed", 

240 "units": "", 

241 "experimental_hierarchy": 2, 

242 } 

243 } 

244 }, 

245 "TS": { 

246 "name": "Thomson scattering at divertor entrance (Z<0)", 

247 "observables": { 

248 "density": { 

249 "name": "Plasma density", 

250 "units": "1/m^3", 

251 "experimental_hierarchy": 2, 

252 }, 

253 "electron_temp": { 

254 "name": "Electron temperature", 

255 "units": "eV", 

256 "experimental_hierarchy": 2, 

257 }, 

258 "ion_temp": { 

259 "name": "Ion temperature", 

260 "units": "eV", 

261 "experimental_hierarchy": -1, 

262 }, 

263 } 

264 }, 

265 "RDPA": { 

266 "name": "Reciprocating divertor probe array", 

267 "observables": { 

268 "density": { 

269 "name": "Plasma density", 

270 "units": "1/m^3", 

271 "experimental_hierarchy": 2, 

272 }, 

273 "electron_temp": { 

274 "name": "Electron temperature", 

275 "units": "eV", 

276 "experimental_hierarchy": 2, 

277 }, 

278 "ion_temp": { 

279 "name": "Ion temperature", 

280 "units": "eV", 

281 "experimental_hierarchy": -1, 

282 }, 

283 "potential": { 

284 "name": "Plasma potential", 

285 "units": "V", 

286 "experimental_hierarchy": 2, 

287 }, 

288 "jsat": { 

289 "name": "Ion saturation current", 

290 "units": "A/m^2", 

291 "experimental_hierarchy": 1, 

292 }, 

293 "jsat_std": { 

294 "name": "Standard deviation of the ion saturation current", 

295 "units": "A/m^2", 

296 "experimental_hierarchy": 1, 

297 }, 

298 "jsat_skew": { 

299 "name": "Skew of the ion saturation current", 

300 "units": "", 

301 "experimental_hierarchy": 1, 

302 }, 

303 "jsat_kurtosis": { 

304 "name": "Pearson kurtosis of the ion saturation current", 

305 "units": "", 

306 "experimental_hierarchy": 1, 

307 }, 

308 "vfloat": { 

309 "name": "Floating potential", 

310 "units": "V", 

311 "experimental_hierarchy": 1, 

312 }, 

313 "vfloat_std": { 

314 "name": "Standard deviation of the floating potential", 

315 "units": "V", 

316 "experimental_hierarchy": 1, 

317 }, 

318 "mach_number": { 

319 "name": "Plasma velocity normalised to local sound speed", 

320 "units": "", 

321 "experimental_hierarchy": 2, 

322 }, 

323 } 

324 } 

325} 

326 

327diagnostics_1d = 'LFS-LP', 'HFS-LP', 'LFS-IR', 'FHRP', 'TS' 

328diagnostics_2d = 'RDPA' 

329 

330def observables_template(): 

331 """Fills in entries in the dictionary that are repeated and therefore left out in the definition""" 

332 observables_filled = observables.copy() 

333 

334 for diagnostic_key, diagnostic in observables_filled.items(): 

335 for observable in diagnostic['observables'].values(): 

336 

337 if diagnostic_key in diagnostics_1d: 

338 observable['dimensionality'] = 1 

339 

340 elif diagnostic_key in diagnostics_2d: 

341 observable['dimensionality'] = 2 

342 

343 else: 

344 raise NotImplementedError(f"Diagnostics key {diagnostic_key} not recognised") 

345 

346 observable['simulation_hierarchy'] = -1 

347 observable['values'] = [] 

348 observable['errors'] = [] 

349 

350 observable['Ru'] = [] 

351 observable['Ru_units'] = 'cm' 

352 

353 if observable['dimensionality'] == 2: 

354 observable['Zx'] = [] 

355 observable['Zx_units'] = 'm' 

356 

357 return observables_filled 

358 

359def write_template_files(output_directory = Path(__file__).parent): 

360 """Writes template files for data analysis""" 

361 

362 observables_filled = observables_template() 

363 

364 # Write to JSON 

365 write_to_json(observables_filled, Path(output_directory)/'observables.json', allow_overwrite=True) 

366 

367 # Also write as a MATLAB struct 

368 # Since '-' is an illegal key in a MATLAB struct, replace it with '_' 

369 observables_for_mat = observables_filled.copy() 

370 for diagnostic_key in observables_filled.keys(): 

371 

372 if '-' in diagnostic_key: 

373 observables_for_mat[diagnostic_key.replace('-', '_')] = observables_for_mat.pop(diagnostic_key) 

374 diagnostic_key = diagnostic_key.replace('-', '_') 

375 

376 observables_group = observables_for_mat[diagnostic_key]['observables'] 

377 for observable_key in observables_group.keys(): 

378 

379 if '-' in observable_key: 

380 observables_group[observable_key.replace('-', '_')] = observables_group.pop(observable_key) 

381 

382 savemat(Path(output_directory)/'observables.mat', {'CODE_NAME_field_direction': observables_for_mat}) 

383 

384if __name__=='__main__': 

385 write_template_files()