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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
180
181
182
183
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Ocean Optics spectrometer data analyzer
Program for controlling of the Ocean optics spectrometers and extractin the most important information from spectra
## oospecanalyz is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
## oospecanalyz is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with oospecanalyz. If not, see <http://www.gnu.org/licenses/>.
f
"""
from numpy import *
import matplotlib
#matplotlib.use('agg')
from matplotlib.pyplot import *
import os
from time import mktime, strptime,sleep,asctime, localtime,struct_time, time
import re
from scipy.optimize import leastsq
from scipy.interpolate import interpolate
from scipy import stats
import scipy.stats
from scipy.special import erf
import ConfigParser
import zipfile
import socket
import shutil
import locale
import subprocess
import urllib2
from matplotlib.ticker import NullFormatter,ScalarFormatter
set_printoptions(precision=4,linewidth=400)
#rcParams['backend'] = 'Agg'
debugging = False
def MAD(x):
"""
Median absolute deviation
"""
return median(abs(x-median(x)))*1.4826
def MovingAverage(data, subset_size):
"""
Simple moving average
"""
if subset_size < 1 and len(data) < subset_size:
raise ValueError('subset_size must be > 1 and less then len(data)')
mvavg = zeros(size(data))
for i in range(subset_size):
mvavg[subset_size/2:-subset_size/2]+= data[i: (+i-subset_size)]
mvavg/= float(subset_size)
mvavg[:subset_size/2] = mean(data[:subset_size/2+1])
mvavg[-subset_size/2:] = mean(data[-subset_size/2-1:])
return mvavg
def UniversalLineShape( p,x):
"""
The most general line shape. xc is position, A amplitude, w width, a skewness, n voitig profile
Line profile is integrated over single pixels
"""
x_interp = interpolate.interp1d(arange(len(x)),x)
x0 = x
m = 10
x = linspace(x[0],x[-1]+(x[-1]-x[-2]),size(x)*m)-mean(diff(x0))/2#increase resolution
xc =p[2]
A = p[0]
w = abs(p[1])
a = p[3]
n = p[4]
# "skew voitig profile"
#http://en.wikipedia.org/wiki/Skew_normal_distribution
y = abs(A)*(1/sqrt(2*pi*w**2)*exp(-((x-xc)/w)**2/2)*(1+erf(a*(x-xc)/sqrt(2)/w))*(1-n)+ n*2/pi*w/(4*(x-xc)**2+w**2))
y = reshape(y, (size(y)/m,m) )
y = sum(y,1)/m #integrate over pixel
return y
def lineCharacteristics(p):
"""
Calculate real properties of the line shape from function UniversalLineShape.
pc[0] - surface under line, pc[1] - FWHM, pc[2] - center of mass, pc[3] - skewness, pc[4] - voitig profile
"""
pc = zeros(5)
a = p[3]
n = p[4]
p[1] = abs(p[1])
#http://en.wikipedia.org/w/index.php?title=Skew_normal_distribution
d = a/sqrt(1+a**2)
pc[0] = p[0]
pc[2] = p[2]+(1-n)*p[1]*d*sqrt(2/pi)
f_L = p[1]/2*n
f_G = (1-n)*p[1]*sqrt(1-2*d**2/pi)*2*sqrt(2*log(2))
pc[1] = 0.5*f_L+sqrt(0.22*f_L**2+f_G**2)#http://en.wikipedia.org/wiki/Voigt_profile
pc[3] = (1-n)*(4-pi)/2*(d*sqrt(2/pi))**3/(1-2*d**2/pi)**(1.5)
pc[4] = n
return pc
class Tokamak:
"Tokamak properties and methods container class "
def __init__(self, name):
self.name = name.upper()
self.loadConfig()
self.shotNumber = -1
self.uploadServerIp = '0.0.0.0'
self.elements = list()
for name in self.ElementsList:
self.elements.append([loadtxt('SpectralLines/'+name+'.txt'),name])
def loadConfig(self):
"""
Load tokamak properties from config file
"""
if not os.path.isfile('tokamak_'+self.name+'.cfg'):
raise Exception('Tokamak config file does not exists.')
config = ConfigParser.RawConfigParser()
config.read('tokamak_'+self.name+'.cfg')
name = config.get('DEFAULT', 'Name')
if name != self.name:
raise Exception('WrongConfig')
self.dataAcqusitionTime = config.getfloat('Basic parameters', 'Plasma length')
self.trigger_shift = config.getfloat('Basic parameters', 'trigger shift')
self.database = config.get('Database', 'remote database folder')
self.trigger_port = config.getint('Database', 'trigger port')
self.login = config.get('Database', 'database login')
elements_str = config.get('Elements', 'ions') # list of the element - originaly comes from NIST database
self.ElementsList = elements_str.replace("', '", ' ').strip("'[]").split()
def saveConfig(self):
"""
Save tokamak properties to config file
"""
config = ConfigParser.RawConfigParser()
config.set('DEFAULT', 'Name', self.name)
config.add_section('Basic parameters')
config.set('Basic parameters', 'Plasma length', self.dataAcqusitionTime)
config.set('Basic parameters', 'trigger shift', self.trigger_shift)
config.add_section('Database')
config.set('Database', 'remote database folder', self.database)
config.set('Database', 'trigger port', self.trigger_port)
config.set('Database', 'database login', self.login)
config.add_section('Elements')
config.set('Elements', 'ions', self.ElementsList)
with open('tokamak_'+self.name +'.cfg', 'wb') as configfile:
config.write(configfile)
##GOLEM
def storeData(self, data_folder):
"""
Upload measured data and graphs to database
"""
if self.shotNumber is None:
print 'missing shot number'
return
if self.uploadServerIp == '0.0.0.0' or self.shotNumber == -1:
raise Exception('can not access to database')
if os.path.isfile(data_folder+'spectra.txt_LinesEvolution_.png'):
os.system('convert -resize 150 %sspectra.txt_LinesEvolution_.png %sicon.png' %((data_folder,)*2))
for i in range(10):# nemÄla by tou ta smyÅ¡by být
try:
if os.system(('scp -r %s %s:%s' % (data_folder,self.login+'@'+self.uploadServerIp,self.database ))%(self.shotNumber)):
raise Exception('can not copy data')
if os.path.isfile(data_folder+'icon.png'):
if os.system(('scp -r %s %s:%s' % (data_folder+'icon.png',self.login+'@'+self.uploadServerIp,self.database+'icon.png'))%(self.shotNumber)):
print 'can not copy icon'
if os.path.isfile(data_folder+'status'):
if os.system(('scp -r %s %s:%s' % (data_folder+'status' ,self.login+'@'+self.uploadServerIp,self.database+'status' ))%(self.shotNumber)):
print 'can not copy'
break
except Exception as inst:
print 'problem with database access:', inst
sleep(5)
def actualShotNumber(self):
"""
Return number of the actual shot
"""
return self.shotNumber
def waitOnSoftwareTrigger(self):
"""
Waits on the signal from the main server, that database is ready. Also shot number of actual shot is reclieved.
"""
def netcat(hostname, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((hostname, port))
s.listen(1)
conn, addr = s.accept()
data = conn.recv(1024)
conn.close()
return addr,data
HOST = '' # Symbolic name meaning all available interfaces
PORT = self.trigger_port # Arbitrary non-privileged port
[addr,data] = netcat(HOST, PORT)
if data == '':
self.shotNumber = None
print 'wrong shot number reclieved'
else:
self.shotNumber = int(data) # je to integer
self.uploadServerIp = addr[0]
def waitOnHardwareTrigger(self, filename, t ):
"""
Detect that spectrometer has reclieved hardware trigger
"""
#detect the hardware trigger only by change of the file with the data
t0 = time()
if not os.path.isfile(filename):
creat_time = 0
else:
creat_time = os.stat(filename).st_mtime
change_time = creat_time
while creat_time == change_time and time()-t0 < t :
if not os.path.isfile(filename):
change_time = 0
else:
change_time = os.stat(filename).st_mtime
sleep(0.1)
if creat_time != change_time:
print 'hw trigger ', str(time()-t0 ), 's'
return True
else:
print 'timeout'
return False
#class Tokamak:
#def __init__(self, name):
#self.name = name.upper()
#self.loadConfig()
#self.shotNumber = -1
#self.uploadServerIp = '0.0.0.0'
#def loadConfig(self):
#if not os.path.isfile('tokamak_'+self.name+'.cfg'):
#raise Exception('Tokamak config file does not exists.')
#config = ConfigParser.RawConfigParser()
#config.read('tokamak_'+self.name+'.cfg')
#name = config.get('DEFAULT', 'Name')
#if name != self.name:
#raise Exception('WrongConfig')
#self.dataAcqusitionTime = config.getfloat('Basic parameters', 'Plasma length')
#self.database = config.get('Database', 'remote database folder')
#self.trigger_port = config.getint('Database', 'trigger port')
#self.login = config.get('Database', 'database login')
#TODO sezna oÄekávaných prvků! v golemovi nenà B, i v poÅadà v jakém se najà hledat
#COMPASS
#def storeData(self, data_folder):
"""
Upload measured data and graphs to database
"""
#newpath = '../'+ str(self.shotNumber)+'/'
#if os.path.exists(newpath):
#print 'file '+newpath+'exists, will be deleted'
#shutil.rmtree(newpath)
#shutil.copytree(data_folder,newpath)
#def actualShotNumber(self):
"""
Return number of the actual shot
"""
#self.shotNumber = -1
#if os.path.exists('index.php'):
#os.remove('index.php')
#if os.system("wget --post-data='"+self.login.decode('base64')+ "' " +self.database) or not os.path.exists('index.php'):
#print 'Problems with logbook access'
#return self.shotNumber
#f = open('index.php', 'r')
#search_str = "name=shot_no class=input_text_s value="
#for i,line in enumerate(f):
#if line.find(search_str) != -1:
#ind = line.find(search_str)
#self.shotNumber = int(line[ind+len(search_str): ind+4+len(search_str)])
#break
#if self.shotNumber == -1:
#raise Exception('Problems with logbook access')
#return self.shotNumber
#def waitOnSoftwareTrigger(self):
"""
Waits on the signal from the main server, that database is ready. Also shot number of actual shot is reclieved.
"""
#pass
#def waitOnHardwareTrigger(self, filename):
"""
Detect that spectrometer has reclieved hardware trigger
"""
##detect the hardware trigger only by change of the file with the data
#if not os.path.isfile(filename):
#creat_time = 0
#else:
#creat_time = os.stat(filename).st_mtime
#change_time = creat_time
#while(creat_time == change_time):
#if not os.path.isfile(filename):
#change_time = 0
#else:
#change_time = os.stat(filename).st_mtime
#sleep(1)
class Spectrometer():
"Spectrometer properties and methods container class "
def __init__(self, SerialNumber,tokamak):
self.SerialNumber = SerialNumber
self.Tokamak = tokamak
#def fittingFunction( p,x): # ordinary gaussian
#if len(p) != 3:
#raise Exception('WrongNumberOfParameters')
#print 'spec fit fun'
#p2 = empty(5)
#p2[:3] = p
#p2[3:] = 0
#shape = UniversalLineShape( p2,x)
#return shape
#def lineCharacteristics(p): # do nothing
#return p
def elementIdentification(self,wavelength,sigma):
"""
Find the closest line to the "wavelength" from lines in tokamak database. If any line was found, amed "unknown" is returned.
"""
dispersion = (self.wavelength_range[1]-self.wavelength_range[0])/self.resolution
if not isnan(sigma):
presicion = min(sqrt(self.wavelength_presicion**2+sigma**2),dispersion*3)
else:
presicion = min(sqrt(self.wavelength_presicion**2),dispersion*3)
close_line_list = list()
closest_line = inf
element_name = ""
for i in self.Tokamak.elements:
index = abs(i[0]-wavelength) < presicion
if any(index):
close_line_list.append([i[0][index], i[1]])
print '\t\t\t',i[1],i[0][index], (i[0]-wavelength)[index]
close_line_i = argmin( abs(i[0]-wavelength))
close_line = i[0][close_line_i]
if abs(closest_line-wavelength)>abs(close_line-wavelength) and abs(close_line-wavelength) < presicion:
closest_line = close_line
element_name = i[1]
if isnan(closest_line):
print('\t\t\tclosest line: %4.3f , difference: %2.3f' % (wavelength, sigma))
if closest_line == inf:
closest_line = wavelength
element_name = 'unknown'
return element_name,closest_line,close_line_list
def convertCountToPhotons(self,wavelength,intensity,integ_time, make_plot = False):
"""
Estimate the number of photons necessery to achieve number of counts in "intensity" on the "wavelength".
This estimation is not based on real measurement of effectivity, but only as multiplying of effectivity of CCD,
gratting and optical fiber.
"""
if self.absolute_calibration == None and self.relative_calibration == None:
f1 = interpolate.interp1d(self.sensor_efficiency[:,0],self.sensor_efficiency[:,1], bounds_error=True, fill_value=0.3, kind = 'quadratic')
f2 = interpolate.interp1d(self.gratting_efficiency[:,0],self.gratting_efficiency[:,1], bounds_error=True, fill_value=0.1, kind = 'quadratic')
fiber_effectivity = 10**(-self.fiber_attenuation[:,1]/10*self.fiber_length)
f3 = interpolate.interp1d(self.fiber_attenuation[:,0],fiber_effectivity, bounds_error=True, fill_value=0.1, kind = 'quadratic')
intensity/= (f1(wavelength)*f2(wavelength)*f3(wavelength))*self.photonsPerCount
if make_plot:
plot(wavelength,self.photonsPerCount/(f1(wavelength)*f2(wavelength*f3(wavelength))))
else:
if self.absolute_calibration:
f = interpolate.interp1d( self.absolute_calibration[:,0], self.absolute_calibration[:,1], kind = 'slinear')
intensity/= f(wavelength)
else:
f = interpolate.interp1d( self.relative_calibration[:,0], self.relative_calibration[:,1], kind = 'slinear')
fiber_effectivity = 10**(-self.fiber_attenuation[:,1]/10*self.fiber_length)
f3 = interpolate.interp1d(self.fiber_attenuation[:,0],fiber_effectivity, bounds_error=True, fill_value=0.1, kind = 'quadratic')
intensity/= f(wavelength)*f3(wavelength)
#intensity/= integ_time/1000.0
if make_plot:
plot(wavelength,f(wavelength))
if make_plot:
xlabel('wavelength [nm]')
ylabel('unit/count')
show()
return intensity
def peakFitting(self,ix0,wavelength,intensity, plot_fit = False):
"""
Identify the peak around the middle in index ix0. The peak is fitted by NLLSQ and
centre of mass of line is compared with lines from database. Option plot_fit will show
the plot with data, their best fit and spectral lines near of this line.
"""
yn = intensity[ix0]
ixr = ix0+1
n = len(wavelength)
x0 = wavelength[ix0]
while ixr < n and yn > intensity[ixr]-sqrt(2):
yn = intensity[ixr]
ixr+=1
yn = intensity[ix0]
ixl = ix0
while ixl > 0 and yn > intensity[ixl-1]-sqrt(2):
yn = intensity[ixl-1]
ixl-=1
if ixr-ixl-3 <= 0: #probably false peak, too much narrow
ixr = min(n, ixr+3)
ixl = max(0, ixl-3)
a = intensity[ix0]*sqrt(2*pi*mean(self.inst_width[:,1])**2)
p0 = (a,mean(self.inst_width[:,1]),x0)
fun = lambda p,x: intensity[ixl:ixr]-self.fittingFunction(p,x)
popt,pcov,info,mesg,success = leastsq(fun, p0, args = wavelength[ixl:ixr], full_output=1)
if any(pcov == None):
pcov = ones((3,3))*nan
# calculate real paramethers of this line
popt_corr = self.lineCharacteristics(popt)
#print popt_corr
chisq=sum(info["fvec"]*info["fvec"])
doF=ixr-ixl-3
print('\t\twavelength %4.3f +/- %1.3f; chi^2/doF: %4.2f'%(popt_corr[2],sqrt(pcov[2,2]),chisq/max(doF,1)))
if chisq > scipy.stats.chi2.isf(0.1,doF): # estimate uncorrect fit
pcov*=chisq/doF
# find closest lines
line_name, line ,close_line_list= self.elementIdentification(popt_corr[2],sqrt(pcov[2,2]))
#if line == inf:
#plot_fit = True
#print line
#if abs(line - 657.805 )<0.01:
#plot_fit = True
if popt_corr[1] > 5:
popt_corr[0] = nan
#plot_fit = True
#plot graph for easier debugging
if plot_fit:
clf()
interv = arange(max(ixl-3,0),min(ixr+3,n))
w_interp = interpolate.interp1d(interv,wavelength[interv])
crop_w = wavelength[interv]
step(crop_w,intensity[interv],'r',where='mid')
step(wavelength[ixl:ixr+1]-(wavelength[ixl+1]-wavelength[ixl])/2, intensity[ixl:ixr+1],'b',where='post') # steps over the integrating area of the pixel
errorbar(wavelength[interv],intensity[interv], yerr=ones(len(interv)),fmt='r.') #intensity with errorbars
errorbar(wavelength[ixl:ixr], intensity[ixl:ixr], yerr=ones(ixr-ixl),fmt='b.') #intensity used for fitting with errorbars
crop_w_100 = w_interp(linspace(amin(interv), amax(interv), len(interv)*100))
crop_w_1 = w_interp(linspace(amin(interv), amax(interv), len(interv)*1))
plot(crop_w_100,self.fittingFunction(popt,crop_w_100), 'b--')
plot(wavelength[ixl:ixr],self.fittingFunction(popt,wavelength[ixl:ixr]), '.')
axhline(y=4, ls= '--',label = 'threshold')
print('wavelength %4.3f +/- %1.3f' % (popt_corr[2], sqrt(pcov[2,2])))
axvline(x = popt_corr[2], ls = '-.',c = 'g', label = "Center of mass")
#CM = sum(wavelength[ixl:ixr]*self.fittingFunction(popt,wavelength[ixl:ixr]))/sum(self.fittingFunction(popt,wavelength[ixl:ixr]))
#axvline(x = CM, ls = '-.',c = 'y', label = "Center of mass 2")
colour = ( 'g','r','c', 'm', 'y', 'k' )
for i, element in enumerate(close_line_list):
for j,line in enumerate(element[0]):
axvline(x = line , label = element[1], c = colour[(i%len(colour))])
axis('tight')
xlabel('$\lambda$ [nm]')
ylabel('SNR[-]')
legend(loc = 'best')
title('wavelength %4.3f +/- %1.3f; chi^2/doF: %4.2f, A: %4.1f'%(popt_corr[2],sqrt(pcov[2,2]),chisq/doF,popt_corr[0] ))
show()
#savefig(str(self.Tokamak.shotNumber)+'.png')
#close()
d = diff(wavelength)[max(0,ix0-1)]
ixl = max(int(ix0-popt_corr[1]/d*3), ixl)
ixr = min(int(ix0+popt_corr[1]/d*3), ixr)
return popt_corr,sqrt(diag(pcov)),ixl,ixr,line_name, line
def identifyLines(self,wavelength, intensity,readoutNoiseRMS, debug = True):
"""
Detect line in the spectra given by field "intensity". Detection is based on chi2 statistics criterium.
The detection threshold is set so that false detect of line in the spectrum with purely gaussian noise
with sigma equal to readoutNoiseRMS is 1%. This method is more sensitive to weak lines than the naive detection of pixels higher than some threshold.
Lines are detected in infinite cycle until any line is detected. The peak is fitted by function "peakFitting"
and at the and of the cycle the peaks are removed.
There are two ways how to remove peak. The peak can be substracted from the spectrum or
the peak can be replaced by gaussian noise. Substrating can be used only of the instumental
function is measured very precisely and therefore chi2 of the fit is close to 1. Because this is not fulfiled, tzhe second way is now used.
"""
print '\t identifing lines'
spectraParametres = list()
line_fit = list()
length = len(intensity)
derivation = zeros(length)
mean_peak_FWHM = 6 #[px] #TODO je to nesystematické, dát do vlastnostà spektrometru
lim = scipy.stats.chi2.isf(0.01/length,mean_peak_FWHM/2)*2 #using only positive part (50% of data), misscorrect detect probability 1%
#intensity-= median(intensity[self.black_pixels]) # dávat to sem?
#readoutNoiseRMS = MAD(intensity)
intensity/=readoutNoiseRMS
step = 0
while True:
step+= 1
if step >200:
print('infinitely cycle')
break
cumSumSpectra = cumsum((intensity*(intensity>0))**2)*2 - arange(length) #using only positive part (50% of data)
derivation[2:length-2] = cumSumSpectra[4:]-cumSumSpectra[:length-4]+mean_peak_FWHM
if all(derivation < lim):
break
#find maximum on the neighbourhood
ix0 = argmax(derivation)
ix0 = argmax(intensity[max(0,ix0-mean_peak_FWHM):min(ix0+mean_peak_FWHM, length)])+max(0,ix0-mean_peak_FWHM)
x0 = wavelength[ix0]
##if ix0 == self.resolution-1:
#plot(derivation, label= 'derivation')
##plot((0,length), (lim,lim), label = 'threshold')
#axhline( y = lim, c = 'r', ls = '--',label = 'threshold')
#axvline( x = ix0, c = 'g',label = 'max')
#axvline( x = argmax(derivation), c = 'b', ls = '--',label = 'max deriv')
##yscale('log', nonposy='clip')
#axis('tight')
#legend()
#show()
#plot(intensity, label= 'intensity')
#axvline( x = ix0, c = 'g',label = 'max')
#axvline( x = argmax(derivation), c = 'b', ls = '--',label = 'max deriv')
#axis('tight')
#legend()
#show()
if debug:
plot(wavelength, cumSumSpectra, label = 'culumative sum of squars')
axvline( x = x0, c = 'r', ls = '--')
title('cumulative sum of squars of data')
xlim(amin(wavelength),amax(wavelength))
show()
popt,sig,ixl,ixr,line_name,line = self.peakFitting(ix0,wavelength,intensity,debug)
#renormalize
popt[0] = self.convertCountToPhotons(x0,readoutNoiseRMS*popt[0],1 )
sig[0] = self.convertCountToPhotons(x0,readoutNoiseRMS*sig[0] ,1)
sig*=stats.t.isf(0.16,ixr- ixl-3) #"1 sigma" probability (68%)
# wavelength, name, area,error,FWHM,error, Delta lambda, error
line_fit.append((line,line_name, popt[0],sig[0], popt[1],sig[1],(popt[2]-line ), sig[2]))
savetxt('current_peak.txt',(wavelength[ixl:ixr],intensity[ixl:ixr]))
# remove the fitted peak (in ideal case the peak should be substracted
#plot(wavelength[ixl+1:ixr-1], intensity[ixl+1:ixr-1],'o')
#plot(wavelength[ixl+1-5:ixr-1+5], intensity[ixl+1-5:ixr+5-1],'.r')
#show()
peakInterv = range(max(ixl+1,0),min(ixr, self.resolution-1)-1)
intensity[peakInterv] = random.randn(len(peakInterv))
#print line_fit
#exit()
return line_fit
class OceanOptics_Spec(Spectrometer):
"child of class Spectrometer with the Ocean Optics specific functions"
def __init__(self, SerialNumber,tokamak):
Spectrometer.__init__(self, SerialNumber,tokamak)
#self.SerialNumber
#self.Tokamak
#self.calibrationPolynomeCorrection = zeros((4,1))
#self.wavelength_presicion =0.1#nm
#self.black_pixels = nan
#self.resolution = 3648
#self.min_integ_time = 3.8
#self.detector = 'TCD1304AP'
#self.gratting_efficiency = array(((0,1),(1200,1)))
#self.sensor_efficiency = loadtxt('./HR4000/citlivost')
#self.photonsPerCount = 60
#self.hotPixels = None
#self.inst_width = array(((0,0),(1200,0)))
#self.inst_skewness = array(((0,0),(1200,0)))
#self.voitig_parameter = 0
self.NonLinCoeff = zeros(7) # implementovat to vůbec??
self.NonLinCoeff[0] = 1
database = os.listdir('SpectralLines')
#load everything from config file
self.loadConfig()
def start(self):
"""
It will start the java constrol program of the spectrometer.
"""
def isThisRunning( process_name ):
ps = subprocess.Popen("ps -eaf", shell=True, stdout=subprocess.PIPE)
output = ps.stdout.read()
output = output.find(process_name)
ps.stdout.close()
ps.wait()
if output == -1:
return False
else:
return True
java_library = '/opt/OmniDriver-/OOI_HOME/HighResTiming.jar:/opt/OmniDriver-/OOI_HOME/OmniDriver.jar'
if isThisRunning('HighSpeedAcquisitionSample'): #TODO dodÄlat
print 'spectrometer is already running'
return
try:
os.remove('./data/nohup_java.out')
os.remove('highspeedacquisitionsample/HighSpeedAcquisitionSample.class')
except:
print 'file can not be removed'
os.system('javac -classpath '+java_library+' HighSpeedAcquisitionSample.java')
shutil.copyfile('HighSpeedAcquisitionSample.class', 'highspeedacquisitionsample/HighSpeedAcquisitionSample.class')
# need admin rights to start with so high priority
run_options = (self.SerialNumber,int(self.Tokamak.dataAcqusitionTime*1000), int(1000*self.integTime))
os.system('nohup chrt -f 99 java -Djava.library.path=. -classpath '+java_library+':. highspeedacquisitionsample.HighSpeedAcquisitionSample %s %i %i > ./data/nohup_java.out &' %run_options)
#os.system('nohup chrt -f 99 java -Djava.library.path=. -classpath '+java_library+':. highspeedacquisitionsample.HighSpeedAcquisitionSample %s %i %i &' %run_options)
#os.system('tail -f nohup.out &')
def wavelengthCorrection(self, old_wavelength):
"""
Apply the wavelength correction polynome
"""
new_wavelegth = copy(old_wavelength)
for i,a in enumerate(self.calibrationPolynomeCorrection):
new_wavelegth+= a*old_wavelength**i
return new_wavelegth
def loadConfig(self):
"""
Load the configuration file of the spectrometer
"""
def parseStringOfField(s):
s = s.splitlines()
field = list()
for line in s:
row = (line.strip('[]')).split()
field.append(list())
for col in row:
field[-1].append(float(col))
return array(field)
if not os.path.isfile(self.SerialNumber+'.cfg'):
raise Exception('Spectrometer config file does not exists.')
config = ConfigParser.RawConfigParser()
config.read(self.SerialNumber+'.cfg')
SerialNumber = config.get('Basic parameters', 'Serial Number')
if SerialNumber != self.SerialNumber:
raise Exception('WrongSerialNumber')
self.detector = config.get('Basic parameters', 'Detector')
self.GratingTyp = config.get('Basic parameters', 'Optical grating')
self.wavelength_presicion = config.getfloat('Basic parameters', 'Wavelength presicion [nm]')
self.min_integ_time = config.getfloat('Basic parameters', 'Minimum integration time [ms]')
tmpstr = config.get('Basic parameters', 'Opticaly black pixels')
self.black_pixels = int_(parseStringOfField(tmpstr))
self.resolution = config.getint('Basic parameters', 'Resolution [px]' )
self.photonsPerCount = config.getfloat('Basic parameters', 'min photons/count')
tmpstr = config.get('Efficiencies', 'Gratting efficiency')
self.gratting_efficiency = parseStringOfField(tmpstr)
tmpstr = config.get('Efficiencies', 'Sensor efficiency') # pro HR4000 to velmi výraznÄ nesedà s údaji na webu
self.sensor_efficiency = parseStringOfField(tmpstr)
try:
tmpstr = config.get('Efficiencies', 'Absolute calibration') # counts/(W*m-2*nm)
self.absolute_calibration = parseStringOfField(tmpstr)
except ConfigParser.NoOptionError as (err):
self.absolute_calibration = None
try:
tmpstr = config.get('Efficiencies', 'Overall effectivity')
self.relative_calibration = parseStringOfField(tmpstr)
except ConfigParser.NoOptionError as (err):
self.relative_calibration = None
tmpstr = config.get('Efficiencies', 'optical fibers attenuation (dB/m)')
self.fiber_attenuation = parseStringOfField(tmpstr)
self.fiber_length = config.getfloat('Efficiencies', 'fiber length')
tmpstr = config.get('Other properties', 'Calibration polynome correction')
self.calibrationPolynomeCorrection = parseStringOfField(tmpstr)[0]
tmpstr = config.get('Other properties', 'Linearity polynome correction')
self.linearityPolynomeCorrection = parseStringOfField(tmpstr)[0]
tmpstr = config.get('Other properties', 'Hot pixels')
self.hotPixels = parseStringOfField(tmpstr)[0]
tmpstr = config.get('Other properties', 'wavelength range')
self.wavelength_range = parseStringOfField(tmpstr)[0]
tmpstr = config.get('Instrumental broadening function', 'width(wavelength)')
self.inst_width = parseStringOfField(tmpstr)
self.F_inst_width = interpolate.interp1d(self.inst_width[:,0],self.inst_width[:,1],bounds_error=False,fill_value=0)
tmpstr = config.get('Instrumental broadening function', 'skewness(wavelength)')
self.inst_skewness = parseStringOfField(tmpstr)
self.F_inst_skewness = interpolate.interp1d(self.inst_skewness[:,0],self.inst_skewness[:,1],bounds_error=False,fill_value=0)
self.voitig_parameter = config.getfloat('Instrumental broadening function', 'voitig parameter')
print 'Loaded data of ', SerialNumber
def saveConfig(self):
"""
Save the configuration file of the spectrometer
"""
config = ConfigParser.RawConfigParser()
config.add_section('Basic parameters')
config.set('Basic parameters', 'Type', 'Ocean Optics Spectrometer')
config.set('Basic parameters', 'Serial Number', self.SerialNumber )
config.set('Basic parameters', 'Detector', self.detector )
config.set('Basic parameters', 'Optical grating', self.GratingTyp )
config.set('Basic parameters', 'Date', asctime())
config.set('Basic parameters', 'Wavelength presicion [nm]', self.wavelength_presicion)
config.set('Basic parameters', 'Opticaly black pixels', self.black_pixels )
config.set('Basic parameters', 'Resolution [px]', self.resolution )
config.set('Basic parameters', 'Minimum integration time [ms]', self.min_integ_time)
config.set('Basic parameters', 'min photons/count', self.photonsPerCount)
config.add_section('Efficiencies')
config.set('Efficiencies', 'Gratting efficiency', self.gratting_efficiency )
config.set('Efficiencies', 'Sensor efficiency', self.sensor_efficiency)
config.set('Efficiencies', 'Optical fibers attenuation (dB/m)', self.fiber_attenuation)
config.set('Efficiencies', 'fiber length', self.fiber_length)
config.add_section('Other properties')
config.set('Other properties', 'Calibration polynome correction', self.calibrationPolynomeCorrection)
config.set('Other properties', 'linearity polynome correction' , self.linearityPolynomeCorrection)
config.set('Other properties', 'Hot pixels', self.hotPixels)
config.set('Other properties', 'wavelength range',self.wavelength_range )
config.add_section('Instrumental broadening function')
config.set('Instrumental broadening function', 'width(wavelength)', self.inst_width ) # Å¡ÃÅka kalibraÄnÃch Äar v nÄkolika bodech
config.set('Instrumental broadening function', 'skewness(wavelength)', self.inst_skewness ) # Å¡ikmost kalibraÄnÃch Äar v nÄkolika bodech
config.set('Instrumental broadening function', 'voitig parameter', self.voitig_parameter ) # negausovkost základny
with open(self.SerialNumber+'.cfg', 'wb') as configfile:
config.write(configfile)
def checkCalibration(self): # vykresà to graf a urÄà chi^2 = suma(delta labda ku sigma)^2
#zapolá to makeCalibration
pass
def makeCalibration(self, dryRun = True):
pass
#naÄte list kalibraÄnÃch Äar
#100x spustà sbÄr data a zprůmÄruje
#projde kalibraÄnà Äáry a najde nejbližšà peak
#aktualizuje (pro dryRun = false) to self.inst_width, self.inst_skewness, calibrationPolynomeCorrection,
# vykreslà to souÄasné a nové hodnoty (i errorbary)
#TODO nastavit width(wavelength),skewness, voitig parameter, Calibration polynome correction
def calcParamsForFitting(self,p):
"""
The lines are not fitted by full set of paramathes of the general peak function UniversalShape. Only a few paramethers are used, other are calculated here.
"""
p_full = empty(5)
p_full[:3] = p[:3]
#print 'int_w ', self.F_inst_width(p[2]), self.F_inst_skewness(p[2]), self.voitig_parameter
#p_full[1] = p[1]
p_full[1] = sqrt(p[1]**2+self.F_inst_width(p[2])**2) #add instrumental broadening
p_full[3] = self.F_inst_skewness(p[2])
#p_full[3] = 10
p_full[4] = self.voitig_parameter
#p_full[4] = 0.00
return p_full
def fittingFunction(self,p,x): # využÃvat informace o znalosti instrumentálnÃho rozÅ¡ÃÅenÃ
p_full = self.calcParamsForFitting(p)
return UniversalLineShape( p_full,x)
def lineCharacteristics(self,p):
"""
The lines are not fitted by full set of paramathes of the general peak function UniversalShape.
Therefore the real properties of the peak are calcutelad here from the limited set of the paramethers.
"""
p_full = self.calcParamsForFitting(p)
p_corrected = lineCharacteristics(p_full)
p_corrected[1] = sqrt(p_corrected[1] **2-self.F_inst_width(p_corrected[2])**2)
return lineCharacteristics(p_full)
def loadData(self,path,typ,name = 'spectra'):
"""
Load data from file and create object "shot" containig all necessary informations andou the tokamak shot. It supports many different types of files.
Java_Data_file - file experted from java control program HighSpeedAcquisitionSample.java
Python_Data_file - file exported from this python program
SpectraSuite_xml - file exported from OceanOptics SpectralSuite
SpectraSuite_txt - acsii file exported from OceanOptics SpectralSuite
SpectraSuite_HighSpeed - ascii file exported from OceanOptics SpectralSuite after high speed acquisition
"""
shot = Shot(self.Tokamak.shotNumber, self)
if typ == 'Java_Data_file': # acqurired by HighSpeedAcquisitionSample.java
#extract data about spectra
shot.DataFile = path+name+'.txt'
if not os.path.isfile(shot.DataFile):
raise Exception('Data file '+shot.DataFile+' does not exists.')
f = open(shot.DataFile, 'r')
for i,line in enumerate(f):
if line[:14] == 'Date and time:':
shot.time = mktime(strptime(line[15:-1], "%Y.%m.%d %H:%M:%S"))
if line[:18] == 'Number of spectra:':
shot.n_spectra = int(line[20:-1])
if line[:25] == 'Number pixels in spectra:':
shot.n_pixels = int(line[27:-1])
if line[:22] == 'Integration time [us]:':
shot.integ_time = int(line[24:-1])/1000.0
if line[:22] == 'Board temperature [C]:':
shot.temperature = float(line[24:-1])
if line[:17] == 'Spectrometer S/N:':
SN = line[18:-1]
if line[:18] == 'Exact time stamps:':
break
if SN != self.SerialNumber:
raise Exception('wrong serial number, '+SN+' != '+self.SerialNumber)
shot.time_stamps = zeros(shot.n_spectra)
for i,line in enumerate(f):
if i >= shot.n_spectra:
break
m = re.search('\s[0-9]+\.[0-9E]+',line)
s = m.group(0)
shot.time_stamps[i] = float(s)
shot.time_stamps/= 1e6 #convert ns -> ms
shot.time_stamps+= -shot.time_stamps[0]+self.Tokamak.trigger_shift +self.min_integ_time#shift ->0
clf()
plot(diff(shot.time_stamps),'.', label = 'exposure times from timestamps')
axhline(y=self.integTime, ls= '--',label = ' exposure time ')
ylim(0,None)
legend(loc = 'best')
savefig(shot.DataFile+'_diff_timestamps.png',bbox_inches='tight')
clf()
print 'read out time', mean(diff(shot.time_stamps)), ' ; ', diff(shot.time_stamps)
# I think, that timestams returned by HighSpeedAcqusion are not the real timestamps of the spectra
shot.time_stamps = self.Tokamak.trigger_shift+self.min_integ_time*2+(arange(shot.n_spectra)+0.5)*shot.integ_time
data = genfromtxt(f)
f.close()
shot.wavelength = data[:shot.n_pixels]
shot.readoutNoiseRMS = 0
for i in range(shot.n_spectra):
intensity = data[(i+1)*shot.n_pixels: (i+2)*shot.n_pixels]
#intensity -= median(intensity[self.black_pixels])
shot.spectra.append(Spectrum(shot,shot.wavelength,intensity,shot.time_stamps[i] ))
shot.readoutNoiseRMS += std(intensity[self.black_pixels])**2
shot.readoutNoiseRMS = sqrt(shot.readoutNoiseRMS/shot.n_spectra) # Zkontrolovat stabilitu odhadu
#extract data about background and dark current
if os.path.isfile(path+'background.txt'):
imax = 0
f = open(path+'background.txt', 'r')
for i,line in enumerate(f):
if line[:-1].replace('.','').isalnum():
imax = i
break
if line[0:22] == 'Integration time [us]:':
integ_time_background = int(line[24:])/1000
f.close()
shot.darkNoise = genfromtxt(path+'background.txt', skip_header=imax)
shot.darkNoise -= median(shot.darkNoise[self.black_pixels])
shot.darkNoise*=shot.integ_time/float(integ_time_background)
#extract data about readout patterns
if os.path.isfile(path+'readoutpatterns.txt'):
imax = 0
f = open(path+'readoutpatterns.txt', 'r')
for i,line in enumerate(f):
if line[:-1].replace('.','').isalnum():
imax = i
break
if line[0:22] == 'Integration time [us]:':
integ_time_readoutpatterns = int(line[24:-1])/1000
if line[0:16] == 'RMS^2 of signal:':
shot.readoutNoiseRMS = sqrt(float(line[17:]))
print 'RMS', shot.readoutNoiseRMS
f.close()
shot.readOutPatterns = genfromtxt(path+'readoutpatterns.txt', skip_header=imax)
if abs(mean(shot.readOutPatterns[:1023]) -mean(shot.readOutPatterns[1023:])) > 5: # SPECTROMETER FAILURE
shot.readOutPatterns[1023:] -= shot.readOutPatterns[1024]-shot.readOutPatterns[1023]
shot.readoutNoiseRMS = 10
shot.readOutPatterns-= (shot.darkNoise/shot.integ_time)*integ_time_readoutpatterns
#plot(shot.readOutPatterns)
#show()
else:
shot.readOutPatterns = mean(intensity[self.black_pixels])
if typ == 'Python_Data_file':
shot.DataFile = path+name+'.txt'
if not os.path.isfile(shot.DataFile):
raise Exception('Data file '+shot.DataFile+' does not exists.')
f = open(shot.DataFile, 'r')
line_header = 0
for i,line in enumerate(f):
line_header +=1
ind = line.find('Serial Number')
if ind != -1:
SN = line[ind+15:-1]
ind = line.find('Date and time (GMT)')
if ind != -1:
#print (line[ind+21:-1])
#print strptime("Thu Apr 5 21:46:30 2012", "%a %b %d %H:%M:%S %Y") #BUG!!!!
#print strptime((line[ind+21:-1]))
shot.time = mktime(strptime(line[ind+21:-1]))
ind = line.find('Number of spectra')
if ind != -1:
shot.n_spectra = int(line[ind+19:-1])
ind = line.find('Resolution')
if ind != -1:
shot.n_pixels = int( line[ind+16:-1])
ind = line.find('Integration time [ms]')
if ind != -1:
shot.integ_time = float(line[ind+22:-1])
ind = line.find('Board temperature [C]')
if ind != -1:
shot.temperature = float(line[ind+22:-1])
ind = line.find('Time stamps [ms]')
if ind != -1:
s = line[ind+18:-1]
shot.time_stamps = float_(s.strip('[]').split())
ind = line.find('Noise RMS')
if ind != -1:
shot.readoutNoiseRMS = float(line[ind+20:-1])
if line.find('***************') != -1:
break
if SN != self.SerialNumber:
raise Exception('wrong serial number, '+SN+' != '+self.SerialNumber)
f.close()
if shot.readoutNoiseRMS > 10:#probably failura in the calculation
shot.readoutNoiseRMS = 10
f = open(shot.DataFile, 'r')
data = genfromtxt(f,skip_header=line_header)
f.close()
shot.wavelength = data[:,0]
#print shot.n_spectra
#print shape(data[:,i+1])
for i in range(shot.n_spectra):
shot.spectra.append(Spectrum(shot,shot.wavelength, data[:,i+1], shot.time_stamps[i]))
shot.darkNoise = zeros(shot.n_pixels)
shot.readOutPatterns = zeros(shot.n_pixels)
if typ == 'VW_java': #acqurid by V.W. java program
shot.temperature = nan
shot.DataFile = path
if not os.path.isfile(shot.DataFile+'wavelength_axis.txt'):
raise Exception('Data file '+shot.DataFile+'wavelength_axis.txt'+' does not exists.')
shot.wavelength = genfromtxt(shot.DataFile+'wavelength_axis.txt')
if max(abs(nanmin(shot.wavelength)-self.wavelength_range[0]), abs(nanmax(shot.wavelength)-self.wavelength_range[1]))>10:
raise Exception('bad wavelength range %d - %d != %d - %d' %(nanmin(shot.wavelength),nanmax(shot.wavelength),self.wavelength_range[0],self.wavelength_range[1]))
if not os.path.isfile(shot.DataFile+'settings.txt'):
raise Exception('Data file '+shot.DataFile+'settings.txt'+' does not exists.')
f = open(shot.DataFile+'settings.txt', 'r')
for i,line in enumerate(f):
#Number of measured spectra:
if i == 1:
shot.n_spectra = int(line)
#Requested integration time [microseconds]:
if i == 3:
shot.integ_time = float(line)
f.close()
shot.time = os.stat(shot.DataFile+'settings.txt').st_mtime
shot.n_pixels = 2048
if not os.path.isfile(shot.DataFile+'timing.txt'):
raise Exception('Data file '+shot.DataFile+'timing.txt'+' does not exists.')
times = genfromtxt(shot.DataFile+'timing.txt',skip_header=2)
shot.time_stamps = times[:,0]-times[0,0] +self.Tokamak.trigger_shift #[ms]
if os.path.isfile(shot.DataFile+'temperature.txt'):
shot.temperature = genfromtxt(shot.DataFile+'temperature.txt')
for i in arange(shot.n_spectra):
if not os.path.isfile(shot.DataFile+'spectrum_'+str(i+1)+'.txt'):
raise Exception('Data file '+shot.DataFile+'spectrum_'+str(i+1)+'.txt'+' does not exists.')
intensity = loadtxt(shot.DataFile+'spectrum_'+str(i+1)+'.txt')
intensity -= median(intensity)
shot.spectra.append(Spectrum(shot,shot.wavelength,intensity, shot.time_stamps[i]))
shot.readoutNoiseRMS = MAD(intensity)
if typ == 'SpectraSuite_xml': #acqurid by Data Studio
shot.temperature = nan
shot.DataFile = path+name+'.ProcSpec'
if not os.path.isfile(shot.DataFile):
raise Exception('Data file '+shot.DataFile+' does not exists.')
with zipfile.ZipFile(shot.DataFile, 'r') as myzip:
shot.n_spectra = len(myzip.namelist())-2
for data in myzip.namelist():
if data != 'OOISignatures.xml' and data != 'OOIVersion.txt':
#print directory+'/'+data_file+'/'+data
t = myzip.getinfo(data).date_time
shot.time = mktime((t[0],t[1],t[2],t[3],t[4],t[5],0, 0,0))
lines = myzip.open(data)
for j,line in enumerate(lines):
if line.find('<numberOfPixels>') != -1:
ind = line.find('<numberOfPixels>')
shot.n_pixels = int(line[ind+16:-18])
if line.find('<integrationTime>') != -1:
ind = line.find('<integrationTime>')
shot.integ_time = int(line[ind+17:-19])/1000.0#convert to ms
if line.find('<spectrometerSerialNumber>') != -1:
ind = line.find('<spectrometerSerialNumber>')
SN = line[26+ind:-28]
if SN != self.SerialNumber:
raise Exception('wrong serial number, '+SN+' != '+self.SerialNumber)
lines.close()
lines = myzip.open(data)
j = 0
intensity = zeros(shot.n_pixels)
shot.wavelength = zeros(shot.n_pixels)
channelWavelengths = False
sourceSpectra = False
pixelValues = False
for i,line in enumerate(lines):
index = line.find('<double>')
if line.find('<com.oceanoptics.omnidriver.spectra.OmniSpectrum>') != -1:
sourceSpectra = True
j = 0
if line.find('</com.oceanoptics.omnidriver.spectra.OmniSpectrum>') != -1:
sourceSpectra = False
if line.find('<channelWavelengths>') != -1:
channelWavelengths = True
j = 0
if line.find('</channelWavelengths>') != -1:
channelWavelengths = False
if line.find('<pixelValues>') != -1:
pixelValues = True
j = 0
if line.find('</pixelValues>') != -1:
pixelValues = False
if index != -1:
if sourceSpectra and pixelValues :
intensity[j] = float(line[index+8:-10])
if sourceSpectra and channelWavelengths:
shot.wavelength[j] = float(line[index+8:-10])
j+=1
lines.close()
intensity -= median(intensity)
shot.spectra.append(Spectrum(shot,shot.wavelength,intensity))
shot.time_stamps = arange(shot.n_spectra)*shot.integ_time+self.Tokamak.trigger_shift
shot.readoutNoiseRMS = MAD(intensity)
if typ == 'SpectraSuite_txt': #text acqurid by Data Studio
shot.temperature = nan
shot.DataFile = path+name+'.txt'
if not os.path.isfile(shot.DataFile):
raise Exception('Data file '+shot.DataFile+' does not exists.')
replaceComma = lambda s:float(s.replace(',','.'))
if os.path.isfile(path+'readoutpatterns.txt'):
readOutPatterns = genfromtxt(path+'readoutpatterns.txt',skip_header=17,skip_footer = 1,converters = {0:replaceComma, 1:replaceComma })
shot.readOutPatterns = readOutPatterns[:,1]
shot.readOutPatterns -= median(shot.readOutPatterns[self.black_pixels])
f = open(shot.DataFile, 'r')
line = f.readline()
if line != 'SpectraSuite Data File\n':
raise Exception('wrong file type')
n_aver = 1
while True:
line = f.readline()
if not line:
if not line: raise Exception('corrupted header of file')
if line[:5] == 'Date:':
try:
shot.time = mktime(strptime(line[6:-1], "%a %b %d %H:%M:%S %Z %Y")) # sometimes it can make mysterious errors connected with figure()
except:
shot.time = os.path.getctime(shot.DataFile)
if line[:36] == 'Number of Sampled Component Spectra:':
shot.n_spectra = int(line[37:-1])
if line[:16] == 'Number of Pixels':
shot.n_pixels = int(line[-5:-1])
if line[:24] == 'Integration Time (usec):':
if line[-11:-1] == '('+self.SerialNumber+')':
shot.integ_time = int(line[25:-11])/1000.0 #convert to ms
else:
shot.integ_time = int(line[25:-1])/1000.0 #convert to ms
if line[:14] == 'Spectrometers:' or line[:27] == 'Spectrometer Serial Number:': #TODO opravit od do
SN = line[-9:-1]
if SN != self.SerialNumber:
raise Exception('wrong serial number, '+SN+' != '+self.SerialNumber)
if line[:16] == 'Spectra Averaged':
if line[-11:-1] == '('+self.SerialNumber+')':
n_aver = int(line[17:-11])
else:
n_aver = int(line[17:-1])
if line[:5] == '>>>>>':
break
if shot.n_spectra == 0: # missing information in file
shot.n_spectra = 1
data = genfromtxt(f, skip_footer = 1, converters = {0:replaceComma, 1:replaceComma })
f.close()
shot.wavelength = data[:,0]
intensity = data[:,1]
intensity -= median(intensity)
shot.spectra.append(Spectrum(shot,shot.wavelength,intensity))
if n_aver == 1:
shot.readoutNoiseRMS = MAD(intensity)
else:
shot.readoutNoiseRMS = 10/sqrt(n_aver)# std(intensity[self.black_pixels])
intensity -= median(intensity[self.black_pixels])
#shot.readoutNoiseRMS = std(intensity[self.black_pixels])
if typ == 'SpectraSuite_HighSpeed':
shot.temperature = nan
shot.DataFile = path+name+'.txt'
if not os.path.isfile(shot.DataFile):
raise Exception('Data file '+shot.DataFile+' does not exists.')
f = open(shot.DataFile, 'r')
first_line = f.readline()
if first_line == 'SpectraSuite Data File\n':
raise Exception('wrong file type')
data = loadtxt(f)
f.close()
shot.time_stamps = float_(first_line.split())+self.Tokamak.trigger_shift
shot.time = os.path.getctime(shot.DataFile) # it can be wrong time!
shot.integ_time = shot.time_stamps[-1]/(len(shot.time_stamps)-1) # only estimation!
shot.n_spectra = size(data,1)-1
shot.n_pixels = size(data,0)
shot.wavelength = data[:,0]
for i in range(shot.n_spectra):
intensity = data[:,i+1]
intensity -= median(intensity)
shot.spectra.append(Spectrum(shot,shot.wavelength,intensity,shot.time_stamps[i] ))
shot.readoutNoiseRMS = MAD(data[self.black_pixels,1:])
if max(abs(nanmin(shot.wavelength)-self.wavelength_range[0]), abs(nanmax(shot.wavelength)-self.wavelength_range[1]))>10:
raise Exception('bad wavelength range %d - %d != %d - %d' %(nanmin(shot.wavelength),nanmax(shot.wavelength),self.wavelength_range[0],self.wavelength_range[1]))
if shot.n_pixels != self.resolution:
raise Exception('wrong number of pixels, '+str(shot.n_pixels)+' != '+str(self.resolution))
return shot
##TODO ve fitovacà funkci zahrout i to , že je to pÅenormované Å¡umem. (prostÄ vypoÄÃtávat Å¡um i pro tu teoretickou funkci)
#class CIII_HR_Spec(Spectrometer):
##TODO různé konfigy prop různá nastaven�
#def __init__(self):
#Spectrometer.__init__(self, SerialNumber,tokamak)
##TODO musà si to pamatovat všechny nastavitelné parametry
#def convertCountToPhotons(self,wavelength, intensity, make_plot = False):
##TODO tady by si to mohlo pamatovat ten Å¡um
##TODO založit to na vzorci z výzkumáku
#if len(wavelength) != len(intensity):
#wavelength = ones(len(intensity))*mean(wavelength)
#if make_plot:
#plot(wavelength,f1(wavelength)*f2(wavelength)*self.photonsPerCount)
#xlabel('wavelength [nm]')
#ylabel('photons/count')
#show()
##TODO stÅeva
#return corr_intensity # Å¡um i intenzita pÅepoÄtené na fotony
#def estimateNoiseInCounts(self,wavelength, intensity):
#if len(wavelength) != len(intensity):
#wavelength = ones(len(intensity))*mean(wavelength)
##TODO dodÄlat stÅeva
#return noise #Å¡um v poÄtu countů
#def PeakFitting:
#def fittingFunction:# buÄ
## pÅidá navÃc paramer základna, využije se tu maxumum dosavadnÃch znalostÃ.
#def identifyLines(self,wavelength, intensity):# return table of lines, wavelngths, +other parametres
class Shot():
"""Discharge container class
used for plotting and obtaining discharge data
"""
def __init__(self, shot_num, Spectrometer):
self.Spectrometer = Spectrometer
self.shot_num=shot_num
self.spectra = list()
self.wavelength = nan # udÄlat korekci pÅi naÄÃtánÃ
self.readOutPatterns = zeros(Spectrometer.resolution) #odeÄÃst mediaán!!!
self.darkNoise = zeros(Spectrometer.resolution) #odeÄÃst mediaán!!!, normované na integraÄnà dobu
self.readoutNoiseRMS = 13 # TODO co to je? chovám se k tomu jak o k poli
self.maxSNR = 0 #TODO co stÃm?
self.n_spectra = 0
self.time_stamps = zeros(1)
self.DataFile = ''
def plasmaShot(self):
"""
Detect if there was any plasma during the shot.
"""
plasma = zeros(self.n_spectra, dtype = 'bool')
for i in arange(self.n_spectra):
plasma[i] = self.spectra[i].plasma()
return any(plasma)
def getData(self,sensitivity_correction = False,noiseCorrected = True):
"""
Return the field containig spectra from the all acqusitions during the shot.
Option sensitivity_correction set that the correction on the not constant sensitivity of the
whole device at different wavelength shold by applied. Option noiseCorrected set that the readout
noise patterns, dark noise and hot pixels should be removed.
"""
spectra = empty((self.Spectrometer.resolution, self.n_spectra))
for i in arange(self.n_spectra):
spectra[:,i] = self.spectra[i].getData(sensitivity_correction , noiseCorrected )
return copy(self.wavelength), spectra
def saveData(self,filename = None, noiseCorrected = True):
"""
Export data from the shot in well arranged shape. At the bigining of the file is the header and than coloumns with wavelengths and intensities.
"""
print 'saving data '
if filename == None:
filename = self.DataFile+'_Data_.txt'
# make a header
header = ''
header += 'Spectrometer:\t' + 'Ocean Optics Spectrometer\n'
header += 'Serial Number:\t' + self.Spectrometer.SerialNumber+'\n'
header += 'Date and time (GMT):\t' + asctime(localtime(self.time))+'\n'
header += 'Number of spectra:\t'+ str(self.n_spectra)+'\n'
header += 'Resolution [px]:\t' + str(self.Spectrometer.resolution)+'\n'
header += 'Integration time [ms]:\t'+ '%2.2f' %(self.integ_time)+'\n'
header += 'Acquisition time [ms]:\t'+ '%2.2f' %((self.time_stamps[-1]-self.time_stamps[0])/(len(self.time_stamps)-1))+'\n'
header += 'Board temperature [C]:\t'+ '%2.3f' %self.temperature +'\n'
header += 'Time stamps [ms]:\t' + str(self.time_stamps)+'\n'
header += 'Noise RMS [counts]:\t' + '%2.1f' %self.readoutNoiseRMS+'\n'
header += '*'*100+'\n'
#save spectra
spectra = empty((self.Spectrometer.resolution, self.n_spectra+1))
spectra[:,0] = self.wavelength
for i in arange(self.n_spectra):
spectra[:,i+1] = self.spectra[i].getData(noiseCorrected = noiseCorrected, linearityCorrected = False)
f = open(filename, 'w')
f.write(header)
savetxt(f, spectra,fmt=['%1.3f']+['%4i']* self.n_spectra)
f.close()
def plot(self, file_type = 'pdf', log_scale = True ,sensitivity_correction = True, w_interval = None):
"""
Plot spectra in separate files.
Options:
file_type - file type of exported graph
log_scale - set log scale
sensitivity_correction - correction on the not constant sensitivity of the whole device at different wavelength
w_interval - interval if the plotted wavelengths
"""
print 'plotting separate graphs'
for i,spectrum in enumerate(self.spectra):
self.spectra[i].plot(sensitivity_correction, log_scale =log_scale, noiseCorrected = True, w_interval = w_interval)
if sensitivity_correction:
ylabel('units [-]')
else:
ylabel('counts [-]')
if file_type == None:
show()
else:
savefig(self.DataFile+'_Graph_%d.'%(i)+file_type,bbox_inches='tight')
close()
clf()
def plot_all(self, file_type = 'pdf', log_scale = True ,sensitivity_correction = True, w_interval = None, plasma = True):
"""
Plot spectra in to one plot
Options:
file_type - file type of exported graph
log_scale - set log scale
sensitivity_correction - correction on the not constant sensitivity of the whole device at different wavelength
w_interval - interval if the plotted wavelengths
plasma - only spectra with some lines
"""
print 'plotting all together'
class MyFormatter(ScalarFormatter):
def __call__(self, x, pos=None):
if pos==0:
return ''
else: return ScalarFormatter.__call__(self, x, pos)
fig = figure(num=None, figsize=(12, 12), dpi=80, facecolor='w', edgecolor='k')
subplots_adjust(wspace=0,hspace=0)
if not self.plasmaShot():
plasma = False
n_plots = self.n_spectra
for i,spectrum in enumerate(self.spectra):
if not spectrum.plasma() and plasma:
n_plots-= 1
i_plot = 0
for i,spectrum in enumerate(self.spectra):
if (not spectrum.plasma()) and plasma:
continue
i_plot+= 1
ax = fig.add_subplot(n_plots,1,i_plot)
ax.xaxis.set_major_formatter( NullFormatter() )
ax.yaxis.set_major_formatter( MyFormatter() )
#subplot( self.n_spectra ,1, i+1)
self.spectra[i].plot(sensitivity_correction, log_scale =log_scale, noiseCorrected = True, w_interval = w_interval)
if sensitivity_correction:
ylabel('photons [-]', rotation='horizontal')
else:
ylabel('counts [-]', rotation='horizontal')
ax = fig.add_subplot(n_plots ,1,n_plots)
ax.xaxis.set_major_formatter( ScalarFormatter() )
if file_type == None:
show()
else:
savefig(self.DataFile+'_Graph_.'+file_type,bbox_inches='tight')
close()
#clf()
def processShot(self, details = False):
"""
prepare table of identified lines and their properties for another functions
"""
LinesTable = []
def addNewLine(wavelength,name):
LinesTable.append(list((wavelength,name)))
if details:
emptyCell = list((0,nan,nan,nan,nan,nan))
else:
emptyCell = list((0,))
for i in range(self.n_spectra):
LinesTable[-1].append(emptyCell)
def setValue(wavelength,time_slice, data):
for row in LinesTable:
if row[0] == wavelength:
# every detect except the firts, is false
if isinstance(row[2+time_slice],float) and row[2+time_slice] != 0:
break
if isinstance(row[2+time_slice],(list,tuple)) and row[2+time_slice][0] != 0:
break
if details:
row[2+time_slice] = data
else:
row[2+time_slice] = data[0]
break
for i,spectrum in enumerate(self.spectra):
Table = spectrum.processSpectrum()
#print 'processing '+str(i+1)+'-th spectrum'
for line in Table: # lines are sorted from most intense
exist = False
for row in LinesTable:
if row[0] == line[0]:
exist = True
setValue(line[0],i,line[2:] )
break
if not exist:
addNewLine(line[0],line[1])
setValue(line[0],i,line[2:] )
ColoumnNames = list(('wavelength','line'))
if details:
OtherColoumns = list(('area','err','FWHM','err', 'Delta lambda','err'))
else:
OtherColoumns = list(('area',))
for i in range(self.n_spectra):
for name in OtherColoumns:
ColoumnNames.append(name)
return ColoumnNames,LinesTable,self.time_stamps
def plotLinesTimeEvolution(self,file_type = None, only_identified = True, n_lines = 7, logscale = False):
"""
Identify annd plot the most intensive lines and plot their time evolution during the shot
Options:
file_type - file type of exported graph
only_identified - extract only identified lines
n_lines - number of plotted lines
"""
print 'lines evolution plotting'
IntensitiesTable = []
ErrorsTable = []
NamesTable = []
ColoumnNames,LinesTable,time_stamps = self.processShot(True)
for line in LinesTable:
if only_identified == True and line[1] == 'unknown':
continue
NamesTable.append(line[1]+' %3.1f' %line[0]+'nm')
intensity = zeros(self.n_spectra)
errorbars= zeros(self.n_spectra)
for j, time_slice in enumerate(line[2:]):
if not self.spectra[j].plasma():
continue
intensity[j] = time_slice[0]
errorbars[j] = time_slice[1]
IntensitiesTable.append(intensity)
ErrorsTable.append(errorbars)
if len(IntensitiesTable) == 0:
return
plasma_end = self.n_spectra
for i in arange(self.n_spectra,0,-1):
if self.spectra[i-1].plasma():
plasma_end = i
break
plasma_end = min(plasma_end,self.n_spectra-1)
plasma_start = 0
for i in arange(self.n_spectra):
if self.spectra[i].plasma():
plasma_start = i-1
break
plasma_start = max(0,plasma_start)
t_start = time_stamps[plasma_start]
t_end = time_stamps[plasma_end]
IntensMax = nanmax(IntensitiesTable, axis = 1)
sort_ind = argsort(IntensMax, order=None)[::-1]
for i,index in enumerate(sort_ind):
if i >= n_lines:
break
points= [IntensitiesTable[index] != 0]
if not logscale:
points[0][plasma_end] = True
points[0][plasma_start] = True
errorbar(time_stamps[points],IntensitiesTable[index][points],yerr=ErrorsTable[index][points],label=NamesTable[index])
leg = legend(loc='best', fancybox=True)
leg.get_frame().set_alpha(0.5)
axis( [t_start,t_end,0, nanmax(IntensitiesTable) *1.1])
xlabel('t [ms]')
ylabel('Intensity [a.u.]')
title('The most intense lines')
if logscale:
yscale('log', nonposy='clip')
ylim([nanmin(IntensitiesTable)*0.8, nanmax(IntensitiesTable)*1.1])
if file_type == None:
show()
else:
savefig(self.DataFile+'_LinesEvolution_.'+file_type,bbox_inches='tight')
close()
clf()
def positionHistorogram(self):
"""
plot the hytorogram of positions of identified lines. It is useful for false line identifications.
"""
ColoumnNames,LinesTable,time_stamps = self.processShot(True)
hist_list = list()
for line in LinesTable:
for cell in line[2:]:
if not isnan(cell[4]):
hist_list.append(( line[0] ,cell[4]))
hist_list = array(hist_list)
plot(hist_list[:,0], hist_list[:,1], '.')
show()
def tableOfResultes(self,sufix = '', sort = False, details = False, only_identified = True):
"""
Export table with lines and their properties
"""
ColoumnNames,LinesTable,time_stamps = self.processShot(details)
f = open(self.DataFile+'_Resultes_'+sufix+'.csv', 'w')
for name in ColoumnNames:
f.write(name)
f.write(' ;')
wavelengths = []
for i,row in enumerate(LinesTable):
wavelengths.append(row[0])
if sort:
wavelengths.sort()
for pos in wavelengths: # complicate way to sort the lines
for row in LinesTable:
if only_identified and row[1] == 'unknown':
continue
if row[0] == pos:
f.write('\n')
for cells in row:
col = 0
if isinstance(cells,list) or isinstance(cells,tuple):
for num in cells:
f.write(locale.format("%5.3f",num)+' ;')
col += 1
else:
if isinstance(cells, double):
f.write(locale.format('%3.3f', cells)+' ;')
else:
f.write(str(cells)+' ;')
col += 1
f.closed
class Spectrum():
"""
Spectrum container class used for plotting and preparing of single spectrum
"""
def __init__(self,Shot,wavelength,intensity, time_stamp = 0):
self.Shot = Shot
self.intensity = intensity #odeÄÃst mediaán!!!, kontrolovat jeszli to neovlivÅujà dalÅ¡Ã funkce
self.wavelength = Shot.Spectrometer.wavelengthCorrection(wavelength)
self.time_stamp = time_stamp
self.lineFitList = []
def plasma(self):
"""
Detect if in the data are some lines
"""
if any(self.getData() > self.Shot.readoutNoiseRMS*5):
return True
return False
def plot(self,sensitivity_correction = True, log_scale = True, noiseCorrected = True, w_interval = None):
"""
Plot a single spectrum
log_scale - set log scale
sensitivity_correction - correction on the not constant sensitivity of the whole device at different wavelength
w_interval - interval if the plotted wavelengths
"""
if not w_interval:
w_interval = [nanmin(self.wavelength), nanmax(self.wavelength)]
w_interval_bool = (self.wavelength >= w_interval[0]) * (self.wavelength <= w_interval[1] )
intensity = self.getData(sensitivity_correction,noiseCorrected)*w_interval_bool
noise = self.Shot.readoutNoiseRMS
threshold = scipy.stats.norm.isf(0.05/self.Shot.Spectrometer.resolution) # 5% probability of mistake
if sensitivity_correction:
noise = self.Shot.Spectrometer.convertCountToPhotons(self.wavelength, noise,self.Shot.integ_time)
else:
noise *= ones(self.Shot.Spectrometer.resolution)
min_threshold = nanmin(threshold*noise)
noise *= w_interval_bool
max_threshold = nanmax(threshold*noise)
plot(self.wavelength, intensity,'b', label= '%2.1f ms' %self.time_stamp, linewidth=0.3)
plot(self.wavelength,threshold*noise, 'g', linewidth=0.6)
xlabel('$\lambda$ [nm]',fontdict={'fontsize':12})
if log_scale:
yscale('log', nonposy='clip')
axis([w_interval[0],w_interval[1],min_threshold/8, max(max_threshold,nanmax(intensity))])
else:
axis([w_interval[0],w_interval[1],nanmin(-noise) , max(max_threshold,nanmax(intensity))])
axis([w_interval[0],w_interval[1],0, max(max_threshold,nanmax(intensity))])
leg = legend(loc='center right', fancybox=True)
leg.get_frame().set_alpha(0.7)
def getData(self,sensitivity_correction = False,noiseCorrected = True, linearityCorrected = True):
"""
prepare intensities for following processing.
In the fist step nonlinearity is removed, than if noiseCorrected is allowed the readout patterns,
dark noise and hot pixels are removed and finally if sensitivity_correction is allowed
the different sentivity of whole device for different wavelengths is corrected.
"""
#plot(self.intensity)
#plot( self.Shot.readOutPatterns)
#plot(self.Shot.darkNoise)
#show()
intensity = copy(self.intensity)
if noiseCorrected:
intensity -= self.Shot.readOutPatterns+self.Shot.darkNoise
intensity[int_(self.Shot.Spectrometer.hotPixels)] = median(intensity)
if linearityCorrected:
correction = zeros(size(intensity))
for i,a in enumerate(self.Shot.Spectrometer.linearityPolynomeCorrection[::-1]):
correction*= intensity
correction += a
intensity = intensity/correction
if sensitivity_correction:
intensity = self.Shot.Spectrometer.convertCountToPhotons(self.wavelength, intensity,self.Shot.integ_time)
return intensity
def processSpectrum(self):
if not self.plasma():
return list()
if len(self.lineFitList) != 0:
return self.lineFitList
corr_intensity = self.getData(False,True)
#print 'identifyLines'#, self.Shot.readoutNoiseRMS
self.lineFitList = self.Shot.Spectrometer.identifyLines(self.wavelength,corr_intensity,self.Shot.readoutNoiseRMS, debugging ) # return table of lines, wavelength, +other parametres
return self.lineFitList
def Example0():
# the most primitive example of accesing data from GOLEM - store them to the same folder as this program, name of file with data is spectra.txt_Data_.txt
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
shot_num = 8420
GOLEM.shotNumber = shot_num
import urllib2
url = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txt'
u = urllib2.urlopen(url)
localFile = open('data_'+str(shot_num)+'.txt', 'w')
localFile.write(u.read())
localFile.close()
shot = HR2000.loadData('','Python_Data_file','data_'+str(shot_num))
print shot.wavelength
for spectrum in shot.spectra:
print spectrum.intensity
shot.plot(None, log_scale = False, sensitivity_correction = True)
shot.plot_all(None, log_scale = False, sensitivity_correction = True)
shot.plotLinesTimeEvolution(None,only_identified = True)
def Example1():
#run acqusition on Golem
path = './data/'
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
HR2000.integTime = 1.67#[ms] it doesn't work waster on this computer
#os.system('killall java')
#sleep(0.5)
while True:
try:
dir_list = os.listdir(path)
for i in dir_list:
os.remove(path+i)
except OSError:
os.mkdir(path)
print 'can not remove old files'
os.system('killall java')
sleep(0.5)
#software trigger - cca 20s before discharge
print 'waiting on software trigger'
GOLEM.waitOnSoftwareTrigger()
print 'software trigger accepted'
#prepare spectrometer (starting and measuring of the bacground take about 10s)
HR2000.start()
if not GOLEM.waitOnHardwareTrigger('prepared', 3): # very problematics step!!!!!
print os.system('killall java')
print 'java killed'
sleep(0.5)
HR2000.start()
#hardware trigger - watche if the drivers hase stored the date
print 'waiting on hardware trigger'
if not GOLEM.waitOnHardwareTrigger('ready',60):
print 'hardware trigger timeout'
continue
print 'hardware trigger accepted'
shot = HR2000.loadData(path,'Java_Data_file','spectra')
shot.saveData(filename = None, noiseCorrected = True)
#ploting data
shot.plot_all('svgz', log_scale = True, sensitivity_correction = True)
shot.plot('svgz', log_scale = False, sensitivity_correction = True, w_interval = [200, 900])
shot.plot_all('png', log_scale = True, sensitivity_correction = True)
shot.plot('png', log_scale = False, sensitivity_correction = True, w_interval = [200, 900])
GOLEM.storeData(path)
plot(shot.readOutPatterns)
savefig(shot.DataFile+'_readoutnoise.png',bbox_inches='tight')
clf()
plot(shot.darkNoise)
savefig(shot.DataFile+'_darkNoise.png',bbox_inches='tight')
#processing spectra
shot.tableOfResultes(details = False, sort = True, only_identified = True)
shot.tableOfResultes(details = True,sufix = 'full', sort = True, only_identified = False)
shot.plotLinesTimeEvolution('png',only_identified = True)
shot.plotLinesTimeEvolution('svgz',only_identified = True)
#create status file
File = open('./data/status', 'w')
File.write('0') #status READY
File.close()
GOLEM.storeData(path)
print 'data stored '+asctime(localtime())
sleep(10)
def Example2():
# load file "spectra.txt_Data_" (file from internet, where was extracted all information about spectra) and horizontal lines at the position of the spectral lines at database
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
shot = HR2000.loadData('./','Python_Data_file','spectra.txt_Data_')
shot.plasmaShot()
shot.plot_all(None, log_scale = False, sensitivity_correction = True, plasma = True)
shot.tableOfResultes(details = True, sort = True, only_identified = False)
shot.plotLinesTimeEvolution('None',only_identified = True)
colour = ( 'g','r','c', 'm', 'y', 'k' )
#plot positions of all ions at to the spectra together
shot.spectra[1].plot( log_scale = False, sensitivity_correction = True)
for i, element in enumerate(GOLEM.elements):
for j,line in enumerate(element[0]):
axvline(x = line , c = colour[(i%len(colour))])
show()
#plot positions of all ions separately
for i, element in enumerate(GOLEM.elements):
shot.spectra[1].plot( log_scale = False, sensitivity_correction = True)
for j,line in enumerate(element[0]):
axvline(x = line , c = colour[(i%len(colour))])
title(element[1])
show()
def Example3():
# explore primitive database of COMPASS spectra, data must be stored in folder './data_wladaserv/'
path = '../../data_wladaserv/'
COMPASS = Tokamak('COMPASS')
#HR2000 = OceanOptics_Spec('HR+C0732',COMPASS) #VIS
HR2000 = OceanOptics_Spec('HR+C1834',COMPASS) #H alpha
#HR2000 = OceanOptics_Spec('HR+C1103',COMPASS) #UV
spectrometer_dirs = os.listdir(path)
print spectrometer_dirs
def ReadFile(directory):
# V. weinzettell format
try:
shot = HR2000.loadData(directory+'/','VW_java','')
shot.plot_all('png', log_scale = True,sensitivity_correction = False)
#shot.plot_all('png', log_scale = True,sensitivity_correction = False)
shot.plot(None, log_scale = False,sensitivity_correction = True)
#shot.tableOfResultes(details = False, sort = True, only_identified = False)
shot.plotLinesTimeEvolution('pdf',only_identified = True, logscale = True)
shot.plotLinesTimeEvolution('png',only_identified = True, logscale = True)
except Exception as inst:
print 'problem', inst
for s_dir in spectrometer_dirs:
date_dirs= os.listdir(path+s_dir)
for d_dir in date_dirs:
shots_dir= os.listdir(path+s_dir+'/'+d_dir)
for data_file in shots_dir:
dir_path = path+s_dir+'/'+d_dir+'/'+data_file
print dir_path
#if os.path.isfile(dir_path+'/_Graph_.png'):
#continue
ReadFile(dir_path)
def Example4():
#explore on COMPASS "database", process data in ProcSpec files
path = './data_nova/'
GOLEM = Tokamak('GOLEM')
#HR2000 = OceanOptics_Spec('HR+C0732',GOLEM)
HR2000 = OceanOptics_Spec('HR+C1834',GOLEM)
#HR2000 = OceanOptics_Spec('HR+C1103',GOLEM)
working_dirs = os.listdir(path)
database = list()
for directory in working_dirs:
shots = os.listdir(path+directory)
for data_file in shots:
if data_file[-8:] == '.ProcSpec':
data_file = data_file[:-9]
name = path+directory+'/','.ProcSpec',data_file
print name
try:
shot = HR2000.loadData(path+directory+'/','SpectraSuite_xml',data_file)
shot.tableOfResultes(details = True, sort = True, only_identified = True)
shot.plot_all(None, log_scale = False,sensitivity_correction = False)
shot.saveData(filename = None, noiseCorrected = False)
except Exception as inst:
print 'problem', inst
def Example5():
#run the acqusition on the COMPASS
path = './data/'
COMPASS = Tokamak('COMPASS')
HR2000 = OceanOptics_Spec('HR+C1103',COMPASS)
HR2000.integTime = 15#[ms]
print COMPASS.actualShotNumber()
HR2000.start()
try:
dir_list = os.listdir(path)
for i in dir_list:
os.remove(path+i)
except OSError:
os.mkdir(path)
print 'can not remove old files'
while True:
print 'waiting on software trigger'
#COMPASS.waitOnSoftwareTrigger() #condensators charge???
print 'software trigger accepted'
print 'waiting on hardware trigger'
COMPASS.waitOnHardwareTrigger(path+'spectra.txt')
print 'hardware trigger accepted'
shot = HR2000.loadData(path,'Java_Data_file','spectra')
shot.saveData(filename = None, noiseCorrected = True)
shot.plot_all('png', log_scale = True, sensitivity_correction = True)
shot.plot('png', log_scale = False, sensitivity_correction = True)
shot.tableOfResultes(details = False, sort = True, only_identified = False)
shot.plotLinesTimeEvolution('png',only_identified = True)
COMPASS.storeData(path)
def Example6():
#calibration data
path = './calibration_8-3-2012/'
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
shot = HR2000.loadData(path,'SpectraSuite_txt','Hg2')
shot.plot(None, log_scale = False)
w, s = shot.getData(sensitivity_correction = True)
print sum(s)
for i, element in enumerate(GOLEM.elements):
shot.spectra[0].plot( log_scale = False, sensitivity_correction = True)
for j,line in enumerate(element[0]):
axvline(x = line , ls = '--', c = 'r')
title(element[1])
ylabel('counts [a.u.]')
show()
def Example7():
#plot spectra and positions of the lines, it can be useful for identification of the lines
shots = [7818,]+range(8059,8075)
He_gas = arange(8059,8070)
H_gas = arange(8070,8075)
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
for shot_num in shots:
gas = ''
if any(He_gas == shot_num) :
gas = 'He'
if any(H_gas == shot_num) :
gas = 'H'
print 'shot number '+str(shot_num)
GOLEM.shotNumber = shot_num
#download data
url = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txt'
try:
u = urllib2.urlopen(url)
except:
print 'shot number '+str(shot_num)+ ' does not exist'
continue
localFile = open('data_'+str(shot_num)+'.txt', 'w')
localFile.write(u.read())
localFile.close()
#read data
shot = HR2000.loadData('','Python_Data_file','data_'+str(shot_num))
integ_spectrum = zeros(HR2000.resolution)
for spectrum in shot.spectra:
if spectrum.plasma():
integ_spectrum += spectrum.intensity
#plot positions of the ions lins
colour = ( 'g','r','c', 'm', 'y', 'k' )
for i, element in enumerate(GOLEM.elements):
for j,line in enumerate(element[0]):
axvline(x = line , c = colour[(i%len(colour))])
title('ion: '+element[1]+'; shot #'+str(shot_num)+'; fill gas: ' + gas)
plot(shot.wavelength,integ_spectrum)
show()
def Example8(shots, names, ext_data = None):
#Assisted line identifications
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
#download data
if ext_data == None:
for shot_num in shots:
if os.path.isfile('data_'+str(shot_num)+'.txt'):
continue
print 'shot number '+str(shot_num)
GOLEM.shotNumber = shot_num
url = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txt'
try:
u = urllib2.urlopen(url)
except:
print 'shot number '+str(shot_num)+ ' does not contain spectrometer data'
continue
localFile = open('data_'+str(shot_num)+'.txt', 'w')
localFile.write(u.read())
localFile.close()
#plot spectra
while(True):
print 'set the wavelength interval for the spectral lines comparison'
l = raw_input('from [nm] (200 default): ')
if l == '':
l = 200
l = double(l)
r = raw_input('to [nm] (1200 default): ')
if r == '':
r = 1200
r = double(r)
shots_list = list()
for shot_num in shots:
shots_list.append(HR2000.loadData('','Python_Data_file','data_'+str(shot_num)))
integ_spectrum = list()
for shot in shots_list:
integ_spectrum.append(zeros(HR2000.resolution))
for spectrum in shot.spectra:
if spectrum.plasma():
integ_spectrum[-1] += spectrum.getData()
wavelength = shots_list[0].wavelength
interval = where((wavelength> l) * (wavelength < r))
colour = ( 'g','r','k', 'm', 'b', 'c' )
style = ('-', '--', '-.', ':')
shift = 0.4
for i,shot_spectrum in enumerate(zip(integ_spectrum,names)):
plot(wavelength[interval],shot_spectrum[0][interval],style[i%4],linewidth=2, label = shot_spectrum[1])
#plot lines from database
for i, element in enumerate(GOLEM.elements):
axvline(x = 0 , label = element[1], c = colour[(i/len(style))], ls = style[i%len(style)])
for j,line in enumerate(element[0]):
if line> r or line < l:
continue
axvline(x = line+shift , c = colour[(i/len(style))], ls = style[i%len(style)])
text(line+shift , 0, element[1])
leg = legend(loc='center right', fancybox=True)
leg.get_frame().set_alpha(0.7)
xlim(l, r)
show()
def Example9():
#massive data procesing
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
#i = %
#download data
for shot_num in arange(9000,9200):
if not os.path.isfile('data_'+str(shot_num)+'.txt'):
#continue
print 'shot number '+str(shot_num)
GOLEM.shotNumber = shot_num
url = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txt'
try:
u = urllib2.urlopen(url)
except:
#print 'shot number '+str(shot_num)+ ' does not contain spectrometer data'
continue
localFile = open('data_'+str(shot_num)+'.txt', 'w')
localFile.write(u.read())
localFile.close()
shot = HR2000.loadData('','Python_Data_file','data_'+str(shot_num))
#print shot.spectra[1])
i+=1
#if i%10!= 0:
for spectrum in shot.spectra:
if spectrum.plasma():
ha = spectrum.getData()[1008:1013]
plot( ha/mean(ha))
ylim(0,None)
show()
#plot(shot.spectra[1])
#show()
#exit()
##plot spectra
#while(True):
##print 'set the wavelength interval for the spectral lines comparison'
##l = raw_input('from [nm] (200 default): ')
##if l == '':
##l = 200
##l = double(l)
##r = raw_input('to [nm] (1200 default): ')
##if r == '':
##r = 1200
##r = double(r)
##shots_list = list()
##for shot_num in shots:
##shots_list.append(
#shot = HR2000.loadData('','Python_Data_file','data_'+str(shot_num))#)
#shot.
##integ_spectrum = list()
##for shot in shots_list:
##integ_spectrum.append(zeros(HR2000.resolution))
##for spectrum in shot.spectra:
##if spectrum.plasma():
##integ_spectrum[-1] += spectrum.getData()
##wavelength = shots_list[0].wavelength
##interval = where((wavelength> l) * (wavelength < r))
##colour = ( 'g','r','k', 'm', 'b', 'c' )
##style = ('-', '--', '-.', ':')
##shift = 0.4
##for i,shot_spectrum in enumerate(zip(integ_spectrum,names)):
##plot(wavelength[interval],shot_spectrum[0][interval],style[i%4],linewidth=2, label = shot_spectrum[1])
###plot lines from database
##for i, element in enumerate(GOLEM.elements):
##axvline(x = 0 , label = element[1], c = colour[(i/len(style))], ls = style[i%len(style)])
##for j,line in enumerate(element[0]):
##if line> r or line < l:
##continue
##axvline(x = line+shift , c = colour[(i/len(style))], ls = style[i%len(style)])
##text(line+shift , 0, element[1])
##leg = legend(loc='center right', fancybox=True)
##leg.get_frame().set_alpha(0.7)
##xlim(l, r)
##show()
# MAIN PROGRAM
def Example10():
#Assisted line identifications
#exit()
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
#i = %
N = list()
center = list()
data = list()
data_full = list()
pressure = list()
temperature = list()
dates = list()
#download data
sesion_lines = zeros((0,28))
shot_names = list()
PlasmaLengths = list()
path = './net_data/'
wrong_shot = [7342,7732,7737,7312,7400,7580,7582,7583]
for shot_num in arange(5000,10300):
print shot_num
if shot_num in wrong_shot:
print 'wrong'
continue
#print os.path.isfile(path+'data_'+str(shot_num)+'.txt')
#print os.path.isfile(path+'p_'+str(shot_num)+'.txt')
#exit()
if not os.path.isfile(path+'data_'+str(shot_num)+'.txt') or not os.path.isfile(path+'p_'+str(shot_num)+'.txt') or not os.path.isfile(path+'s_'+str(shot_num)+'.txt') or not os.path.isfile(path+'e_'+str(shot_num)+'.txt') :
#continue
if os.path.isfile(path+'no_data_'+str(shot_num)):
print 'neni'
continue
#print 'loadim'
#shot_num = 9685
url = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/diagnostics/Radiation/1111Spectrometer.ON/data/spectra.txt_Data_.txt'
url_p = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/Initial_PfeifferMerkaVakua'
url_s = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/basicdiagn/PlasmaStart'
url_e = 'http://golem.fjfi.cvut.cz/operation/shots/'+str(shot_num)+'/basicdiagn/PlasmaEnd'
try:
u = urllib2.urlopen(url)
u_p = urllib2.urlopen(url_p)
u_s = urllib2.urlopen(url_s)
u_e = urllib2.urlopen(url_e)
except:
localFile = open(path+'no_data_'+str(shot_num), 'w')
localFile.close()
print 'neni'
continue
localFile = open(path+'data_'+str(shot_num)+'.txt', 'w')
localFile.write(u.read())
localFile.close()
localFile = open(path+'p_'+str(shot_num)+'.txt', 'w')
localFile.write(u_p.read())
localFile.close()
localFile = open(path+'s_'+str(shot_num)+'.txt', 'w')
localFile.write(u_s.read())
localFile.close()
localFile = open(path+'e_'+str(shot_num)+'.txt', 'w')
localFile.write(u_e.read())
localFile.close()
print 'download'
#exit()
shot = HR2000.loadData('','Python_Data_file',path+'data_'+str(shot_num))
if not shot.plasmaShot():
continue
#print shot.spectra[1])
#i+=1
#if i%10!= 0:
print 'shot number '+str(shot_num)
GOLEM.shotNumber = shot_num
wavelength, intensities = shot.getData()
#print shape(intensities)
for spectrum in shot.spectra:
if spectrum.plasma():
#if any(spectrum.getData()[-1] >5000):
#plot(spectrum.getData())
#show()
data.append(single(copy(spectrum.getData())))
temperature.append(shot.temperature)
pressure.append(loadtxt(path+'p_'+str(shot_num)+'.txt'))
#print shot.time
dates.append(shot.time)
#plot(spectrum.getData())
#show()
#shot.plot(None, log_scale = False, sensitivity_correction = True, w_interval = [200, 900])
#show()
#intens = sum(intensities, 1)
#data_full.append(intens)
#print spectra
#CII = [284.0,427.1,658.5,678,712]
#CIII = [230.1]
#HeI = [389.4,587.5,668,706.7,729.6]
#HeII = [542.7]
#H = [486.1,656.4]
#NII = [399.6,489.9,500.2,568.2,617,648.4]
#OII = [375.6,435,441.7,539.2,664.2,672.4,777.6,845]
#ions = [CII, CIII, H, HeI, HeII, NII,OII]
#shot_lines = zeros(0)
shot_names.append(shot_num)
#pressure.append(loadtxt(path+'p_'+str(shot_num)+'.txt'))
length = float(loadtxt(path+'e_'+str(shot_num)+'.txt'))-float(loadtxt(path+'s_'+str(shot_num)+'.txt'))
PlasmaLengths.append(length)
#print length
#exit()
#TODO to heII kecá!!
#TODO dÄlit délkou plazmatu
#for i,ion in enumerate(ions):
#lines = zeros(len(ion))
#for j,line in enumerate(ion):
#interval = where(abs(wavelength- line)<0.7)
#lines[j] = sum(intens[interval])
##shot_lines = hstack((shot_lines,lines))
#ion = H
#lines = zeros(len(ion))
#for j,line in enumerate(ion):
#interval = where(abs(wavelength- line)<0.7)
#lines[j] = sum(array(intens[interval]) )
#N.append(sum(lines)/length)
#interval = where(abs(wavelength- 468.1)<0.7)[0]
#center.append( sum(interval* intens[interval]/sum(intens[interval]) ))
##axvline(x = line-0.5)
##axvline(x = line+0.5)
##plot(wavelength[interval], intens[interval])
##title(str(i)+ ' '+str(line) )
##show()
##for spectrum in shot.spectra:
##if spectrum.plasma():
##wavelength =
##ha = spectrum.getData()[1008:1013]
##plot( ha/mean(ha))
##ylim(0,None)
##show()
##print shape(sesion_lines), shape(shot_lines)
##sesion_lines = vstack((sesion_lines, shot_lines))
#center = array(center)
#shot_names = array(shot_names)
#pressure = array( pressure)
#temperature = array(temperature)
##print shape(array( pressure))
#N = (array(N))
##plot(shot_names, temperature, '.')
##show()
##TODO shift/teplota
#data = array(data)
#print shape(data)
###print data
#data_full = array(data_full)
##exit()
#ix0 = argmin(abs(778-wavelength))
#ix0 = 1011
###plot()
#inter = arange(198,206)
#inter = arange(198,300)
##for i in arange(size(data_full,0)):
##plot( data_full[i,:])
##show()
#Hbeta = zeros((size(data_full,0)))
#Herror = zeros((size(data_full,0)))
#for i in arange(size(data_full,0)):
##Tokamak.shotNumber
#GOLEM.shotNumber = shot_names[i]
#ix0 = 1011
##ix0 = argmax(data_full[i,(ix0-5):(ix0+5)])
##plot(wavelength[(ix0-50):(ix0+50)], data_full[i,(ix0-50):(ix0+50)])
##show()
#ix0 = argmax(data_full[i,(ix0-3):(ix0+3)])+ix0-3
#popt,sig,ixl,ixr,line_name,line = HR2000.peakFitting(ix0,wavelength,data_full[i,:], plot_fit = False)
#Hbeta[i] = popt[2]
#Herror[i] = sig[2]
##print popt[2], sig[2]
##Hbeta[i,:] = [popt[2], sig[2]]
#print shape(arange(size(data_full,0))), shape(Hbeta)
##plot(arange(size(data,0)), Hbeta[:,0])
##print type(Hbeta[:,0])
##print type(Hbeta[:,1])
##print shape(Hbeta)
##print
##plot(arange(size(data,0)), Hbeta[:,1],'.')
#plot(shot_names,-Hbeta,'.')
##errorbar( Hbeta, Herror,'.')
#show()
#plot(temperature,Hbeta)
##errorbar( Hbeta, Herror,'.')
#show()
#plot(shot_names,temperature)
##errorbar( Hbeta, Herror,'.')
#show()
#plot(data.T)
#show()
save('data',data)
save('wavelength',wavelength)
save('temperature', temperature)
save('pressure', pressure)
save('shot_names', shot_names)
save('dates', dates)
#exit()
##print (N), (shot_names)
##plot(shot_names, N, '.')
#plot(pressure, N, '.')
#xlabel('$p_{init}$ [mPa]')
#ylabel('intensity H I')
#ylim(0,None)
#show()
#lengths = array(lengths)
#plot(pressure, lengths, '.')
#show()
#plot(shot_names, lengths, '.')
#show()
#plot(shot_names, center)
#show()
#print shape(shot_names)
#plot(shot_names, pressure, '.')
#ylabel('$p_{init}$ [mPa]')
#xlabel('shot number')
#ylim(0,None)
#show()
#sesion_lines = sesion_lines.T
##print shape(sesion_linessesion_lines)
#savetxt('fyztyd_lines',sesion_lines, fmt='%.2e' )
#print sesion_lines
#print array(shot_names)
#savetxt('fyztyd_lines_names',shot_names )
def Example11():
GOLEM = Tokamak('GOLEM')
HR2000 = OceanOptics_Spec('HR+C1886',GOLEM)
data = load('components_sparser_manual2.npy')
#data = load('components_full3.npy')
wavelength = load('wavelength_cropped.npy')
#wavelength = load('wavelength.npy')
colour = ( 'b','r','y','k', 'm', 'g', 'c' )
style = ('-', '--', '-.', ':')
figure(figsize=(8,8))
ax = axes([0.1, 0.1, 0.8, 0.8])
shift = 0.7 - (wavelength-206)/183*0.1
for i,shot_spectrum in enumerate((data)):
#if i != 2:
#continue
print shape(wavelength), shape(shot_spectrum)
if all(shot_spectrum == 0):
continue
plot(wavelength-shift,shot_spectrum,colour[(i/len(style))]+style[i%4],linewidth=2, label = str(i))
#show()
#plot lines from database
for i, element in enumerate(GOLEM.elements):
axvline(x = 0 , label = element[1], c = colour[(i/len(style))], ls = style[i%len(style)])
for j,line in enumerate(element[0]):
#if line> r or line < l:
#continue
if line< amin(wavelength) or line> amax(wavelength):
continue
axvline(x = line , c = colour[(i/len(style))], ls = style[i%len(style)])
text(line , i+j, element[1])
leg = legend(loc='center right', fancybox=True)
leg.get_frame().set_alpha(0.7)
show()
def main():
Example11()
#Example10()
#exit()
#shots = raw_input("Enter discharge numbers seperated by a comma (default 7818,8059,8074): ")
#if shots == '':
#shots = [7818, 8059, 8074]
#names = ('H, baked chamber','He, dirty chamber','H, dirty chamber')
#else:
#names = [ (i) for i in shots.replace(' ', '').split(',') ]
#shots = int_(names) #strip the string of spaces and split numbers based on comma and pack into a list of ints
###names = str_(shots)
###shots = [ int(i) for i in shots.replace(' ', '').split(',') ]
##print names, size(names), shots, size(shots)
##exit()
#Example8(shots, names)
if __name__ == "__main__":
main()
|