forked from mbobra/SHARPs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_swx_fits.py
1271 lines (1020 loc) · 51.8 KB
/
calculate_swx_fits.py
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
"""
Purpose: To calculate the following spaceweather parameters and their errors using SDO/HMI vector magnetic field data:
USFLUX Total unsigned flux in Maxwells
ERRVF Error in the total unsigned flux
CMASK Number of pixels used in the USFLUX calculation
MEANGAM Mean inclination angle, gamma, in degrees
ERRGAM Error in the mean inclination angle
MEANGBT Mean value of the total field gradient, in Gauss/Mm
ERRBT Error in the mean value of the total field gradient
MEANGBZ Mean value of the vertical field gradient, in Gauss/Mm
ERRBZ Error in the mean value of the vertical field gradient
MEANGBH Mean value of the horizontal field gradient, in Gauss/Mm
ERRBH Error in the mean value of the horizontal field gradient
MEANJZD Mean vertical current density, in mA/m2
ERRJZ Error in the mean vertical current density
TOTUSJZ Total unsigned vertical current, in Amperes
ERRUSI Error in the total unsigned vertical current
MEANALP Mean twist parameter, alpha, in 1/Mm
ERRALP Error in the mean twist parameter
MEANJZH Mean current helicity in G2/m
ERRMIH Error in the mean current helicity
TOTUSJH Total unsigned current helicity in G2/m
ERRTUI Error in the total unsigned current helicity
ABSNJZH Absolute value of the net current helicity in G2/m
ERRTAI Error in the absolute value of the net current helicity
SAVNCPP Sum of the absolute value of the net current per polarity in Amperes
ERRJHT Error in the sum of the absolute value of the net current per polarity
MEANPOT Mean photospheric excess magnetic energy density in ergs per cubic centimeter
ERRMPOT Error in the mean photospheric excess magnetic energy density
TOTPOT Total photospheric magnetic energy density in ergs per centimeter
ERRTPOT Error in the total photospheric magnetic energy density
MEANSHR Mean shear angle (measured using Btotal) in degrees
ERRMSHA Error in the mean shear angle
SHRGT45 Area with shear angle greater than 45 degrees (as a percent of total area)
R_VALUE Flux along gradient-weighted neutral-line length in Maxwells
Derivations of the analytical functions for the error in each parameter can be found here:
http://jsoc.stanford.edu/doc/data/hmi/sharp/error_analysis.pdf
Inputs: All SDO/HMI data is stored in a pSQL database; the web interface is here:
http://jsoc.stanford.edu/ajax/lookdata.html.
The data used for this code is available in the DRMS series hmi.sharp_cea_720s,
which is documented extensively in Bobra et al., Solar Physics, 2014, an open-access publication:
http://link.springer.com/article/10.1007%2Fs11207-014-0529-3.
We use the following segments:
[example filename] --> [description]
hmi.sharp_cea_*.Br.fits --> radial component of the magnetic field vector
hmi.sharp_cea_*.Bt.fits --> theta-component of the magnetic field vector
hmi.sharp_cea_*.Bp.fits --> phi-component of the magnetic field vector
hmi.sharp_cea_*.Br_err.fits --> error in radial component of the magnetic field vector
hmi.sharp_cea_*.Bt_err.fits --> error in theta-component of the magnetic field vector
hmi.sharp_cea_*.Bp_err.fits --> error in phi-component of the magnetic field vector
hmi.sharp_cea_*.conf_disambig.fits --> bits indicate confidence levels in disambiguation result
hmi.sharp_cea_*.bitmap.fits --> bits indicate result of automatic detection algorithm
hmi.sharp_cea_*.magnetogram.fits --> line-of-sight component of the magnetic field
Usage: This code depends on the numpy, scipy, and sunpy libraries.
Examples: ipython:
> %run calculate_swx_fits.py --file_bz=hmi.sharp_cea_720s.377.20110215_020000_TAI.Br.fits --file_by=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bt.fits --file_bx=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bp.fits --file_bz_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Br_err.fits --file_by_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bt_err.fits --file_bx_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bp_err.fits --file_conf_disambig=hmi.sharp_cea_720s.377.20110215_020000_TAI.conf_disambig.fits --file_bitmap=hmi.sharp_cea_720s.377.20110215_020000_TAI.bitmap.fits --file_los=hmi.sharp_cea_720s.377.20110215_020000_TAI.magnetogram.fits
command line:
> python calculate_swx_fits.py --help
> python calculate_swx_fits.py --file_bz=hmi.sharp_cea_720s.377.20110215_020000_TAI.Br.fits --file_by=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bt.fits --file_bx=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bp.fits --file_bz_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Br_err.fits --file_by_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bt_err.fits --file_bx_err=hmi.sharp_cea_720s.377.20110215_020000_TAI.Bp_err.fits --file_conf_disambig=hmi.sharp_cea_720s.377.20110215_020000_TAI.conf_disambig.fits --file_bitmap=hmi.sharp_cea_720s.377.20110215_020000_TAI.bitmap.fits --file_los=hmi.sharp_cea_720s.377.20110215_020000_TAI.magnetogram.fits
Written: Monica Bobra
1 May 2015
23 October 2017 Updated to Python 3.5
18 October 2019 Updated cdelt1 to cdelt1_arcsec + changed header key & array handling
"""
# import some modules
import sunpy, sunpy.map, scipy, numpy as np, sys, math, argparse, pdb
# define some constants
radsindeg = np.pi/180.
munaught = 0.0000012566370614
#===========================================
def main():
file_bz = ''
file_by = ''
file_bx = ''
file_bz_err = ''
file_by_err = ''
file_bx_err = ''
file_conf_disambig = ''
file_bitmap = ''
file_los = ''
parser = argparse.ArgumentParser(description='calculate spaceweather keywords from vector magnetic field data')
parser.add_argument('-a', '--file_bz', type=str, help='FITS file containing Bz-component of magnetic field vector', required=True)
parser.add_argument('-b', '--file_by', type=str, help='FITS file containing By-component of magnetic field vector', required=True)
parser.add_argument('-c', '--file_bx', type=str, help='FITS file containing Bx-component of magnetic field vector', required=True)
parser.add_argument('-d', '--file_bz_err', type=str, help='FITS file containing error in Bz-component of magnetic field vector', required=True)
parser.add_argument('-e', '--file_by_err', type=str, help='FITS file containing error in By-component of magnetic field vector', required=True)
parser.add_argument('-f', '--file_bx_err', type=str, help='FITS file containing error in Bx-component of magnetic field vector', required=True)
parser.add_argument('-g', '--file_conf_disambig', type=str, help='FITS file with bits identifying high-confidence in disambiguation result', required=True)
parser.add_argument('-i', '--file_bitmap', type=str, help='FITS file with bits identifying the active region', required=True)
parser.add_argument('-j', '--file_los', type=str, help='FITS file containing line-of-sight component of magnetic field', required=True)
parser._optionals.title = "flag arguments"
args = parser.parse_args()
file_bz = args.file_bz
file_by = args.file_by
file_bx = args.file_bx
file_bz_err = args.file_bz_err
file_by_err = args.file_by_err
file_bx_err = args.file_bx_err
file_conf_disambig = args.file_conf_disambig
file_bitmap = args.file_bitmap
file_los = args.file_los
print('')
print('These are the files:')
print('file_bz is', file_bz)
print('file_by is', file_by)
print('file_bx is', file_bx)
print('file_bz_err is', file_bz_err)
print('file_by_err is', file_by_err)
print('file_bx_err is', file_bx_err)
print('file_conf_disambig is', file_conf_disambig)
print('file_bitmap is', file_bitmap)
print('file_los is', file_los)
print('')
# get the data
print('Getting the data.')
bz, by, bx, bz_err, by_err, bx_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, los = get_data(file_bz, file_by, file_bx, file_bz_err, file_by_err, file_bx_err, file_conf_disambig, file_bitmap, file_los)
print('These are the keyword values:')
# compute the total unsigned flux and associated errors
mean_vf, mean_vf_err, count_mask = compute_abs_flux(bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec)
print('USFLUX ',mean_vf,'Mx')
print('ERRVF', mean_vf_err,'Mx')
print('CMASK', count_mask,'pixels')
# compute the horizontal component of the magnetic field and associated errors
horiz = compute_bh(bx, by, bz, bx_err, by_err, bz_err, conf_disambig, bitmap, nx, ny)
bh, bh_err = horiz[0], horiz[1]
# compute the shear angle and associated errors
mean_gamma, mean_gamma_err = compute_gamma(bx, by, bz, bh, bz_err, bh_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec)
print('MEANGAM ', mean_gamma,'degree')
print('ERRGAM ', mean_gamma_err,'degree')
# compute the total magnetic field vector and associated errors
total = compute_bt(bx, by, bz, bx_err, by_err, bz_err, conf_disambig, bitmap, nx, ny)
bt, bt_err = total[0], total[1]
# compute the field gradients and associated errors
mean_derivative_bt, mean_derivative_bt_err = computeBtderivative(bt, bt_err, nx, ny, conf_disambig, bitmap)
print('MEANGBT ',mean_derivative_bt,'G * Mm^(-1)')
print('ERRBT ',mean_derivative_bt_err,'G * Mm^(-1)')
mean_derivative_bh, mean_derivative_bh_err = computeBhderivative(bh, bh_err, nx, ny, conf_disambig, bitmap)
print('MEANGBH ',mean_derivative_bt,'G * Mm^(-1)')
print('ERRBH ',mean_derivative_bt_err,'G * Mm^(-1)')
mean_derivative_bz, mean_derivative_bz_err = computeBzderivative(bz, bz_err, nx, ny, conf_disambig, bitmap)
print('MEANGBZ ',mean_derivative_bt,'G * Mm^(-1)')
print('ERRBZ ',mean_derivative_bt_err,'G * Mm^(-1)')
# compute the vertical current and associated errors
current = computeJz(bx, by, bx_err, by_err, conf_disambig, bitmap, nx, ny)
jz, jz_err, derx, dery = current[0], current[1], current[2], current[3]
# compute the moments of the vertical current density and associated errors
mean_jz, mean_jz_err, us_i, us_i_err = computeJzmoments(jz, jz_err, derx, dery, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, munaught)
print('MEANJZD ', mean_jz,'mA * m^(−2)')
print('ERRJZ ', mean_jz_err,'mA * m^(−2)')
print('TOTUSJZ ', us_i,'A')
print('ERRUSI', us_i_err,'A')
# compute the twist parameter, alpha, and associated errors
mean_alpha, mean_alpha_err = computeAlpha(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec)
print('MEANALP ', mean_alpha,'Mm^(-1)')
print('ERRALP ', mean_alpha_err,'Mm^(-1)')
# compute the moments of the current helicity and associated errors
mean_ih, mean_ih_err, total_us_ih, total_us_ih_err, total_abs_ih, total_abs_ih_err = computeHelicity(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec)
print('MEANJZH ', mean_ih,'G2 * m^(−1)')
print('ERRMIH ', mean_ih_err,'G2 * m^(−1)')
print('TOTUSJH ', total_us_ih,'G2 * m^(−1)')
print('ERRTUI ', total_us_ih_err,'G2 * m^(−1)')
print('ABSNJZH ', total_abs_ih,'G2 * m^(−1)')
print('ERRTAI ', total_abs_ih_err,'G2 * m^(−1)')
# compute the sum of the absolute value per polarity and associated errors
totaljz, totaljz_err = computeSumAbsPerPolarity(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, munaught)
print('SAVNCPP ', totaljz,'A')
print('ERRJHT ', totaljz_err,'A')
# compute a numerical model of the potential field (it has no errors, as the theoretical values are exact)
potential = greenpot(bz, nx, ny)
bpx, bpy = potential[0], potential[1]
# compute the energy stored in the magnetic field and its associated errors
meanpot, meanpot_err, totpot, totpot_err = computeFreeEnergy(bx_err, by_err, bx, by, bpx, bpy, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, conf_disambig, bitmap)
print('MEANPOT ',meanpot,'erg * cm^(−3)')
print('ERRMPOT ',meanpot_err,'erg * cm^(−3)')
print('TOTPOT ',totpot,'erg * cm^(−1)')
print('ERRTPOT ',totpot_err,'erg * cm^(−1)')
# compute the degree to which the observed field is sheared and its associated errors
meanshear_angle, meanshear_angle_err, area_w_shear_gt_45 = computeShearAngle(bx_err, by_err, bz_err, bx, by, bz, bpx, bpy, nx, ny, conf_disambig, bitmap)
print('MEANSHR ',meanshear_angle,'degree')
print('ERRMSHA ',meanshear_angle_err,'degree')
print('SHRGT45 ',area_w_shear_gt_45,'as a percentage')
# compute the gradient-weighted neutral line length
Rparam = computeR(los, nx, ny, cdelt1_arcsec)
print('R_VALUE ', Rparam[0],'Mx')
def get_data(file_bz, file_by, file_bx, file_bz_err, file_by_err, file_bx_err, file_conf_disambig, file_bitmap, file_los):
"""function: get_data
This function reads the appropriate data and metadata.
"""
try:
bz_map = sunpy.map.Map(file_bz)
except:
print("Could not open the bz fits file")
sys.exit(1)
try:
by_map = sunpy.map.Map(file_by)
except:
print("Could not open the by fits file")
sys.exit(1)
try:
bx_map = sunpy.map.Map(file_bx)
except:
print("Could not open the bx fits file")
sys.exit(1)
try:
bz_err_map = sunpy.map.Map(file_bz_err)
except:
print("Could not open the bz_err fits file")
sys.exit(1)
try:
by_err_map = sunpy.map.Map(file_by_err)
except:
print("Could not open the by_err fits file")
sys.exit(1)
try:
bx_err_map = sunpy.map.Map(file_bx_err)
except:
print("Could not open the bx_err fits file")
sys.exit(1)
try:
conf_disambig_map = sunpy.map.Map(file_conf_disambig)
except:
print("Could not open the conf_disambig fits file")
sys.exit(1)
try:
bitmap_map = sunpy.map.Map(file_bitmap)
except:
print("Could not open the bitmap fits file")
sys.exit(1)
try:
los_map = sunpy.map.Map(file_los)
except:
print("Could not open the LoS fits file")
sys.exit(1)
# get metadata
header = bz.meta
# get array data
bz = bz_map.data
by = by_map.data
bx_map = bx_map.data
bz_err_map = bz_err_map.data
by_err_map = by_err_map.data
bx_err_map = bx_err_map.data
conf_disambig_map = conf_disambig_map.data
bitmap_map = bitmap_map.data
los_map = los_map.data
# get fits header key information
rsun_ref = header['rsun_ref']
dsun_obs = header['dsun_obs']
rsun_obs = header['rsun_obs']
cdelt1 = header['cdelt1']
# convert cdelt1 from degrees to arcsec
cdelt1_arcsec = (math.atan((rsun_ref*cdelt1*radsindeg)/(dsun_obs)))*(1/radsindeg)*(3600.)
# get dimensions
nx = bz.shape[1]
ny = bz.shape[0]
# flip the sign of by
by_flipped = -1.0*(np.array(by))
return [bz, by_flipped, bx, bz_err, by_err, bx_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, los]
#===========================================
def compute_abs_flux(bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec):
"""function: compute_abs_flux
This function computes the total unsigned flux in units of G/cm^2.
It also returns the number of pixels used in this calculation in the keyword CMASK.
To compute the unsigned flux, we simply calculate
flux = surface integral [(vector Bz) dot (normal vector)],
= surface integral [(magnitude Bz)*(magnitude normal)*(cos theta)].
However, since the field is radial, we will assume cos theta = 1.
Therefore, the pixels only need to be corrected for the projection.
To convert G to G*cm^2, simply multiply by the number of square centimeters per pixel:
(Gauss/pix^2)(CDELT1)^2(RSUN_REF/RSUN_OBS)^2(100.cm/m)^2
=Gauss*cm^2
"""
count_mask = 0
sum = 0.0
err = 0.0
for j in range(ny):
for i in range(nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(bz[j,i]):
continue
sum += abs(bz[j,i])
err += bz_err[j,i]*bz_err[j,i];
count_mask += 1
mean_vf = sum*cdelt1_arcsec*cdelt1_arcsec*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0
mean_vf_err = (np.sqrt(err))*abs(cdelt1_arcsec*cdelt1_arcsec*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0)
return [mean_vf, mean_vf_err, count_mask]
#===========================================
def compute_bh(bx, by, bz, bx_err, by_err, bz_err, conf_disambig, bitmap, nx, ny):
"""function: compute_bh
This function calculates B_h, the horizontal field, in units of Gauss.
(The magnetic field has native units of Gauss since the filling factor = 1).
"""
bh = np.zeros([ny,nx])
bh_err = np.zeros([ny,nx])
for j in range(ny):
for i in range(nx):
if (np.isnan(bx[j,i]) or np.isnan(by[j,i])):
bh[j,i] = np.nan
bh_err[j,i] = np.nan
continue
bh[j,i] = np.sqrt(bx[j,i]*bx[j,i] + by[j,i]*by[j,i])
bh_err[j,i] = np.sqrt(bx[j,i]*bx[j,i]*bx_err[j,i]*bx_err[j,i] + by[j,i]*by[j,i]*by_err[j,i]*by_err[j,i])/ bh[j,i]
return [bh, bh_err]
#===========================================
def compute_gamma(bx, by, bz, bh, bz_err, bh_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec):
"""function: compute_gamma
This function computes the inclination of the horizontal field (relative to the radial field).
Error analysis calculations are done in radians (since derivatives are only true in units of radians),
and multiplied by (180./PI) at the end for consistency in units.
"""
count_mask = 0
sum = 0.0
err = 0.0
for j in range(ny):
for i in range(nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if ( np.isnan(bz[j,i]) or np.isnan(bz_err[j,i]) or np.isnan(bh[j,i]) or np.isnan(bh_err[j,i]) or bz[j,i] == 0 ):
continue
if ( bh[j,i] < 100 ):
continue
sum += abs(math.atan(bh[j,i]/abs(bz[j,i])))*(180./np.pi)
err += (1/(1+((bh[j,i]*bh[j,i])/(bz[j,i]*bz[j,i]))))*(1/(1+((bh[j,i]*bh[j,i])/(bz[j,i]*bz[j,i]))))*( ((bh_err[j,i]*bh_err[j,i])/(bz[j,i]*bz[j,i])) + ((bh[j,i]*bh[j,i]*bz_err[j,i]*bz_err[j,i])/(bz[j,i]*bz[j,i]*bz[j,i]*bz[j,i])) )
count_mask += 1
mean_gamma = sum/count_mask
mean_gamma_err = (np.sqrt(err)/(count_mask))*(180./np.pi)
return [mean_gamma, mean_gamma_err]
#===========================================
def compute_bt(bx, by, bz, bx_err, by_err, bz_err, conf_disambig, bitmap, nx, ny):
"""function: compute_bt
This function calculates B_t, the total field, in units of Gauss.
(The magnetic field has native units of Gauss since the filling factor = 1).
"""
bt = np.zeros([ny,nx])
bt_err = np.zeros([ny,nx])
for j in range(ny):
for i in range(nx):
if (np.isnan(bx[j,i]) or np.isnan(by[j,i]) or np.isnan(bz[j,i])):
bt[j,i] = np.nan
bt_err[j,i] = np.nan
continue
bt[j,i] = np.sqrt(bx[j,i]*bx[j,i] + by[j,i]*by[j,i] + bz[j,i]*bz[j,i])
bt_err[j,i] = np.sqrt(bx[j,i]*bx[j,i]*bx_err[j,i]*bx_err[j,i] + by[j,i]*by[j,i]*by_err[j,i]*by_err[j,i] + bz[j,i]*bz[j,i]*bz_err[j,i]*bz_err[j,i])/ bt[j,i]
return [bt, bt_err]
#===========================================
def computeBtderivative(bt, bt_err, nx, ny, conf_disambig, bitmap):
"""function: computeBtderivative
This function computes the derivative of the total field.
"""
count_mask = 0
sum = 0.0
err = 0.0
derx_bt = np.zeros([ny,nx])
dery_bt = np.zeros([ny,nx])
err_term1 = np.zeros([ny,nx])
err_term2 = np.zeros([ny,nx])
# brute force method of calculating the derivative d/dx (no consideration for edges)
for i in range(1,nx-1):
for j in range(0,ny):
derx_bt[j,i] = (bt[j,i+1] - bt[j,i-1])*0.5
err_term1[j,i] = ( ((bt[j,i+1]-bt[j,i-1])*(bt[j,i+1]-bt[j,i-1])) * (bt_err[j,i+1]*bt_err[j,i+1] + bt_err[j,i-1]*bt_err[j,i-1]) )
#brute force method of calculating the derivative d/dy (no consideration for edges) */
for i in range(0,nx):
for j in range(1,ny-1):
dery_bt[j,i] = (bt[j+1,i] - bt[j-1,i])*0.5
err_term2[j,i] = ( ((bt[j+1,i]-bt[j-1,i])*(bt[j+1,i]-bt[j-1,i])) * (bt_err[j+1,i]*bt_err[j+1,i] + bt_err[j-1,i]*bt_err[j-1,i]) )
# consider the edges for the arrays that contribute to the variable "sum" in the computation below.
# ignore the edges for the error terms as those arrays have been initialized to zero.
# this is okay because the error term will ultimately not include the edge pixels as they are selected out by the conf_disambig and bitmap arrays.
i=0
for j in range(ny):
derx_bt[j,i] = ( (-3*bt[j,i]) + (4*bt[j,i+1]) - (bt[j,i+2]) )*0.5
i=nx-1
for j in range(ny):
derx_bt[j,i] = ( (3*bt[j,i]) + (-4*bt[j,i-1]) - (-bt[j,i-2]) )*0.5
j=0
for i in range(nx):
dery_bt[j,i] = ( (-3*bt[j,i]) + (4*bt[j+1,i]) - (bt[(j+2),i]) )*0.5
j=ny-1
for i in range(nx):
dery_bt[j,i] = ( (3*bt[j,i]) + (-4*bt[j-1,i]) - (-bt[j-2,i]) )*0.5
# Calculate the sum only
for j in range(1,ny-1):
for i in range (1,nx-1):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if ( (derx_bt[j,i] + dery_bt[j,i]) == 0):
continue
if np.isnan(bt[j,i]):
continue
if np.isnan(bt[j+1,i]):
continue
if np.isnan(bt[j-1,i]):
continue
if np.isnan(bt[j,i-1]):
continue
if np.isnan(bt[j,i+1]):
continue
if np.isnan(bt_err[j,i]):
continue
if np.isnan(derx_bt[j,i]):
continue
if np.isnan(dery_bt[j,i]):
continue
sum += np.sqrt( derx_bt[j,i]*derx_bt[j,i] + dery_bt[j,i]*dery_bt[j,i] )
err += err_term2[j,i] / (16.0*( derx_bt[j,i]*derx_bt[j,i] + dery_bt[j,i]*dery_bt[j,i] )) + err_term1[j,i] / (16.0*( derx_bt[j,i]*derx_bt[j,i] + dery_bt[j,i]*dery_bt[j,i] ))
count_mask += 1
mean_derivative_bt = (sum)/(count_mask)
mean_derivative_bt_err = (np.sqrt(err))/(count_mask)
return [mean_derivative_bt, mean_derivative_bt_err]
#===========================================
def computeBhderivative(bh, bh_err, nx, ny, conf_disambig, bitmap):
"""function: computeBhderivative
This function computes the derivative of the horizontal field.
"""
count_mask = 0
sum = 0.0
err = 0.0
derx_bh = np.zeros([ny,nx])
dery_bh = np.zeros([ny,nx])
err_term1 = np.zeros([ny,nx])
err_term2 = np.zeros([ny,nx])
# brute force method of calculating the derivative d/dx (no consideration for edges)
for i in range(1,nx-1):
for j in range(0,ny):
derx_bh[j,i] = (bh[j,i+1] - bh[j,i-1])*0.5
err_term1[j,i] = ( ((bh[j,i+1]-bh[j,i-1])*(bh[j,i+1]-bh[j,i-1])) * (bh_err[j,i+1]*bh_err[j,i+1] + bh_err[j,i-1]*bh_err[j,i-1]) )
#brute force method of calculating the derivative d/dy (no consideration for edges) */
for i in range(0,nx):
for j in range(1,ny-1):
dery_bh[j,i] = (bh[j+1,i] - bh[j-1,i])*0.5
err_term2[j,i] = ( ((bh[j+1,i]-bh[j-1,i])*(bh[j+1,i]-bh[j-1,i])) * (bh_err[j+1,i]*bh_err[j+1,i] + bh_err[j-1,i]*bh_err[j-1,i]) )
# consider the edges for the arrays that contribute to the variable "sum" in the computation below.
# ignore the edges for the error terms as those arrays have been initialized to zero.
# this is okay because the error term will ultimately not include the edge pixels as they are selected out by the conf_disambig and bitmap arrays.
i=0
for j in range(ny):
derx_bh[j,i] = ( (-3*bh[j,i]) + (4*bh[j,i+1]) - (bh[j,i+2]) )*0.5
i=nx-1
for j in range(ny):
derx_bh[j,i] = ( (3*bh[j,i]) + (-4*bh[j,i-1]) - (-bh[j,i-2]) )*0.5
j=0
for i in range(nx):
dery_bh[j,i] = ( (-3*bh[j,i]) + (4*bh[j+1,i]) - (bh[(j+2),i]) )*0.5
j=ny-1
for i in range(nx):
dery_bh[j,i] = ( (3*bh[j,i]) + (-4*bh[j-1,i]) - (-bh[j-2,i]) )*0.5
# Calculate the sum only
for j in range(1,ny-1):
for i in range (1,nx-1):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if ( (derx_bh[j,i] + dery_bh[j,i]) == 0):
continue
if np.isnan(bh[j,i]):
continue
if np.isnan(bh[j+1,i]):
continue
if np.isnan(bh[j-1,i]):
continue
if np.isnan(bh[j,i-1]):
continue
if np.isnan(bh[j,i+1]):
continue
if np.isnan(bh_err[j,i]):
continue
if np.isnan(derx_bh[j,i]):
continue
if np.isnan(dery_bh[j,i]):
continue
sum += np.sqrt( derx_bh[j,i]*derx_bh[j,i] + dery_bh[j,i]*dery_bh[j,i] )
err += err_term2[j,i] / (16.0*( derx_bh[j,i]*derx_bh[j,i] + dery_bh[j,i]*dery_bh[j,i] )) + err_term1[j,i] / (16.0*( derx_bh[j,i]*derx_bh[j,i] + dery_bh[j,i]*dery_bh[j,i] ))
count_mask += 1
mean_derivative_bh = (sum)/(count_mask)
mean_derivative_bh_err = (np.sqrt(err))/(count_mask)
return [mean_derivative_bh, mean_derivative_bh_err]
#===========================================
def computeBzderivative(bz, bz_err, nx, ny, conf_disambig, bitmap):
"""function: computeBzderivative
This function computes the derivative of the vertical field.
"""
count_mask = 0
sum = 0.0
err = 0.0
derx_bz = np.zeros([ny,nx])
dery_bz = np.zeros([ny,nx])
err_term1 = np.zeros([ny,nx])
err_term2 = np.zeros([ny,nx])
# brute force method of calculating the derivative d/dx (no consideration for edges)
for i in range(1,nx-1):
for j in range(0,ny):
derx_bz[j,i] = (bz[j,i+1] - bz[j,i-1])*0.5
err_term1[j,i] = ( ((bz[j,i+1]-bz[j,i-1])*(bz[j,i+1]-bz[j,i-1])) * (bz_err[j,i+1]*bz_err[j,i+1] + bz_err[j,i-1]*bz_err[j,i-1]) )
#brute force method of calculating the derivative d/dy (no consideration for edges) */
for i in range(0,nx):
for j in range(1,ny-1):
dery_bz[j,i] = (bz[j+1,i] - bz[j-1,i])*0.5
err_term2[j,i] = ( ((bz[j+1,i]-bz[j-1,i])*(bz[j+1,i]-bz[j-1,i])) * (bz_err[j+1,i]*bz_err[j+1,i] + bz_err[j-1,i]*bz_err[j-1,i]) )
# consider the edges for the arrays that contribute to the variable "sum" in the computation below.
# ignore the edges for the error terms as those arrays have been initialized to zero.
# this is okay because the error term will ultimately not include the edge pixels as they are selected out by the conf_disambig and bitmap arrays.
i=0
for j in range(ny):
derx_bz[j,i] = ( (-3*bz[j,i]) + (4*bz[j,i+1]) - (bz[j,i+2]) )*0.5
i=nx-1
for j in range(ny):
derx_bz[j,i] = ( (3*bz[j,i]) + (-4*bz[j,i-1]) - (-bz[j,i-2]) )*0.5
j=0
for i in range(nx):
dery_bz[j,i] = ( (-3*bz[j,i]) + (4*bz[j+1,i]) - (bz[(j+2),i]) )*0.5
j=ny-1
for i in range(nx):
dery_bz[j,i] = ( (3*bz[j,i]) + (-4*bz[j-1,i]) - (-bz[j-2,i]) )*0.5
# Calculate the sum only
for j in range(1,ny-1):
for i in range (1,nx-1):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if ( (derx_bz[j,i] + dery_bz[j,i]) == 0):
continue
if np.isnan(bz[j,i]):
continue
if np.isnan(bz[j+1,i]):
continue
if np.isnan(bz[j-1,i]):
continue
if np.isnan(bz[j,i-1]):
continue
if np.isnan(bz[j,i+1]):
continue
if np.isnan(bz_err[j,i]):
continue
if np.isnan(derx_bz[j,i]):
continue
if np.isnan(dery_bz[j,i]):
continue
sum += np.sqrt( derx_bz[j,i]*derx_bz[j,i] + dery_bz[j,i]*dery_bz[j,i] )
err += err_term2[j,i] / (16.0*( derx_bz[j,i]*derx_bz[j,i] + dery_bz[j,i]*dery_bz[j,i] )) + err_term1[j,i] / (16.0*( derx_bz[j,i]*derx_bz[j,i] + dery_bz[j,i]*dery_bz[j,i] ))
count_mask += 1
mean_derivative_bz = (sum)/(count_mask)
mean_derivative_bz_err = (np.sqrt(err))/(count_mask)
return [mean_derivative_bz, mean_derivative_bz_err]
#===========================================
def computeJz(bx, by, bx_err, by_err, conf_disambig, bitmap, nx, ny):
"""function: computeJz
This function computes the z-component of the current.
In discretized space like data pixels, the current (or curl of B) is calculated as the integration
of the field Bx and By along the circumference of the data pixel divided by the area of the pixel.
One form of differencing the curl is expressed as:
(dx * (Bx(i,j-1)+Bx(i,j)) / 2
+dy * (By(i+1,j)+By(i,j)) / 2
-dx * (Bx(i,j+1)+Bx(i,j)) / 2
-dy * (By(i-1,j)+By(i,j)) / 2) / (dx * dy)
To change units from Gauss/pixel to mA/m^2 (the units for Jz in Leka and Barnes, 2003),
one must perform the following unit conversions:
(Gauss)(1/arcsec)(arcsec/meter)(Newton/Gauss*Ampere*meter)(Ampere^2/Newton)(milliAmpere/Ampere), or
(Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(1 T / 10^4 Gauss)(1 / 4*PI*10^-7)( 10^3 milliAmpere/Ampere), or
(Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(1000.),
where a Tesla is represented as a Newton/Ampere*meter.
The units of total unsigned vertical current (us_i) are simply in A. In this case, we would have the following:
(Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(CDELT1)(CDELT1)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)
= (Gauss/pix)(0.00010)(1/MUNAUGHT)(CDELT1)(RSUN_REF/RSUN_OBS)
"""
count_mask = 0
sum = 0.0
err = 0.0
derx = np.zeros([ny,nx])
dery = np.zeros([ny,nx])
err_term1 = np.zeros([ny,nx])
err_term2 = np.zeros([ny,nx])
jz = np.zeros([ny,nx])
jz_err = np.zeros([ny,nx])
# brute force method of calculating the derivative d/dx (no consideration for edges)
for i in range(1,nx-1):
for j in range(0,ny):
derx[j,i] = (by[j,i+1] - by[j,i-1])*0.5
err_term1[j,i] = ((by_err[j,i+1]*by_err[j,i+1]) + (by_err[j,i-1]*by_err[j,i-1]))
#brute force method of calculating the derivative d/dy (no consideration for edges) */
for i in range(0,nx):
for j in range(1,ny-1):
dery[j,i] = (bx[j+1,i] - bx[j-1,i])*0.5
err_term2[j,i] = ((bx_err[j+1,i]*bx_err[j+1,i]) + (bx_err[j-1,i]*bx_err[j-1,i]))
# consider the edges for the arrays that contribute to the variable "sum" in the computation below.
# ignore the edges for the error terms as those arrays have been initialized to zero.
# this is okay because the error term will ultimately not include the edge pixels as they are selected out by the conf_disambig and bitmap arrays.
i=0
for j in range(ny):
derx[j,i] = ( (-3*by[j,i]) + (4*by[j,i+1]) - (by[j,i+2]) )*0.5
i=nx-1
for j in range(ny):
derx[j,i] = ( (3*by[j,i]) + (-4*by[j,i-1]) - (-by[j,i-2]) )*0.5
j=0
for i in range(nx):
dery[j,i] = ( (-3*bx[j,i]) + (4*bx[j+1,i]) - (bx[(j+2),i]) )*0.5
j=ny-1
for i in range(nx):
dery[j,i] = ( (3*bx[j,i]) + (-4*bx[j-1,i]) - (-bx[j-2,i]) )*0.5
# Calculate the sum only
for j in range(1,ny-1):
for i in range (1,nx-1):
jz[j,i] = (derx[j,i] - dery[j,i])
jz_err[j,i] = 0.5*np.sqrt(err_term1[j,i] + err_term2[j,i])
return [jz, jz_err, derx, dery]
#===========================================
def computeJzmoments(jz, jz_err, derx, dery, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, munaught):
"""function: computeJzmoments
This function computes moments of the vertical current.
The mean vertical current density is in units of mA/m^2.
The total unsigned vertical current is in units of Amperes.
"""
count_mask = 0
curl = 0.0
err = 0.0
us_i = 0.0
# Calculate the sum only
for j in range(ny):
for i in range(nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(jz[j,i]):
continue
if np.isnan(derx[j,i]):
continue
if np.isnan(dery[j,i]):
continue
curl += (jz[j,i])*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref)*(0.00010)*(1/munaught )*(1000.)
us_i += abs(jz[j,i])*(cdelt1_arcsec/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/munaught)
err += (jz_err[j,i]*jz_err[j,i])
count_mask += 1
mean_jz = curl/(count_mask)
mean_jz_err = (np.sqrt(err)/count_mask)*((1/cdelt1_arcsec)*(rsun_obs/rsun_ref)*(0.00010)*(1/munaught)*(1000.))
us_i = (us_i)
us_i_err = (np.sqrt(err))*((cdelt1_arcsec/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/munaught ))
return [mean_jz, mean_jz_err, us_i, us_i_err]
#===========================================
def computeAlpha(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec):
"""function: computeAlpha
This function computes the twist parameter.
The twist parameter, alpha, is defined as alpha = Jz/Bz. In this case, the calculation for alpha is weighted by Bz:
numerator = sum of all Jz*Bz
denominator = sum of Bz*Bz
alpha = numerator/denominator
The units of alpha are in 1/Mm
The units of Jz are in Gauss/pix; the units of Bz are in Gauss.
Therefore, the units of Jz/Bz = (Gauss/pix)(1/Gauss)(pix/arcsec)(arsec/meter)(meter/Mm), or
= (Gauss/pix)(1/Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(10^6)
= 1/Mm
"""
alpha_total = 0.0
C = ((1/cdelt1_arcsec)*(rsun_obs/rsun_ref)*(1000000.))
total = 0.0
A = 0.0
B = 0.0
for j in range(ny):
for i in range(nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(jz[j,i]):
continue
if np.isnan(bz[j,i]):
continue
if (jz[j,i] == 0):
continue
if (bz[j,i] == 0):
continue
A += jz[j,i]*bz[j,i]
B += bz[j,i]*bz[j,i]
for j in range(ny):
for i in range(nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(jz[j,i]):
continue
if np.isnan(bz[j,i]):
continue
if (jz[j,i] == 0):
continue
if (bz[j,i] == 0):
continue
total += bz[j,i]*bz[j,i]*jz_err[j,i]*jz_err[j,i] + (jz[j,i]-2*bz[j,i]*A/B)*(jz[j,i]-2*bz[j,i]*A/B)*bz_err[j,i]*bz_err[j,i]
#Determine the absolute value of alpha. The units for alpha are 1/Mm
alpha_total = ((A/B)*C)
mean_alpha = alpha_total
mean_alpha_err = (C/B)*(np.sqrt(total))
return [mean_alpha, mean_alpha_err]
#===========================================
def computeHelicity(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec):
"""function: computeHelicity
This function computes a proxy for the current helicity and various moments.
The current helicity is defined as Bz*Jz and the units are G^2 / m
The units of Jz are in G/pix; the units of Bz are in G.
Therefore, the units of Bz*Jz = (Gauss)*(Gauss/pix) = (Gauss^2/pix)(pix/arcsec)(arcsec/meter)
= (Gauss^2/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)
= G^2 / m.
"""
count_mask = 0.0
sum = 0.0
sum2 = 0.0
err = 0.0
for j in range(ny):
for i in range (nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(jz[j,i]):
continue
if np.isnan(bz[j,i]):
continue
if (jz[j,i] == 0):
continue
if (bz[j,i] == 0):
continue
if np.isnan(jz_err[j,i]):
continue
if np.isnan(bz_err[j,i]):
continue
sum += (jz[j,i]*bz[j,i])*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref) #contributes to MEANJZH and ABSNJZH
sum2 += abs(jz[j,i]*bz[j,i])*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref) # contributes to TOTUSJH
err += (jz_err[j,i]*jz_err[j,i]*bz[j,i]*bz[j,i]) + (bz_err[j,i]*bz_err[j,i]*jz[j,i]*jz[j,i])
count_mask += 1
mean_ih = sum/count_mask # Units are G^2 / m ; keyword is MEANJZH
total_us_ih = sum2 # Units are G^2 / m ; keyword is TOTUSJH
total_abs_ih = abs(sum) # Units are G^2 / m ; keyword is ABSNJZH
mean_ih_err = (np.sqrt(err)/count_mask)*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref) # error in the quantity MEANJZH
total_us_ih_err = (np.sqrt(err))*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref) # error in the quantity TOTUSJH
total_abs_ih_err = (np.sqrt(err))*(1/cdelt1_arcsec)*(rsun_obs/rsun_ref) # error in the quantity ABSNJZH
return [mean_ih, mean_ih_err, total_us_ih, total_us_ih_err, total_abs_ih, total_abs_ih_err]
#===========================================
def computeSumAbsPerPolarity(jz, jz_err, bz, bz_err, conf_disambig, bitmap, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, munaught):
"""function: computeSumAbsPerPolarity
This function computes the sum of the absolute value of the current per polarity. It is defined as follows:
The sum of the absolute value per polarity is defined as the following:
abs(sum(jz gt 0)) + abs(sum(jz lt 0)) and the units are in Amperes per arcsecond.
The units of jz are in G/pix. In this case, we would have the following:
Jz = (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)(RSUN_OBS/RSUN_REF),
= (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)
The error in this quantity is the same as the error in the mean vertical current.
"""
count_mask = 0.0
sum1 = 0.0
sum2 = 0.0
err = 0.0
for j in range(ny):
for i in range (nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(jz[j,i]):
continue
if np.isnan(bz[j,i]):
continue
if (bz[j,i] > 0):
sum1 += ( jz[j,i])*(1/cdelt1_arcsec)*(0.00010)*(1/munaught)*(rsun_ref/rsun_obs)
if (bz[j,i] <= 0):
sum2 += ( jz[j,i])*(1/cdelt1_arcsec)*(0.00010)*(1/munaught)*(rsun_ref/rsun_obs)
err += (jz_err[j,i]*jz_err[j,i]);
count_mask += 1
totaljz = abs(sum1) + abs(sum2)
totaljz_err = np.sqrt(err)*(1/cdelt1_arcsec)*abs((0.00010)*(1/munaught)*(rsun_ref/rsun_obs))
return [totaljz, totaljz_err]
#===========================================
def computeFreeEnergy(bx_err, by_err, bx, by, bpx, bpy, nx, ny, rsun_ref, rsun_obs, cdelt1_arcsec, conf_disambig, bitmap):
"""
function: computeFreeEnergy
This function computes the mean photospheric excess magnetic energy and total photospheric excess magnetic energy density.
The units for magnetic energy density in cgs are ergs per cubic centimeter. The formula B^2/8*PI integrated over all space, dV
automatically yields erg per cubic centimeter for an input B in Gauss. Note that the 8*PI can come out of the integral; thus,
the integral is over B^2 dV and the 8*PI is divided at the end.
Total magnetic energy is the magnetic energy density times dA, or the area, and the units are thus ergs/cm. To convert
ergs per centimeter cubed to ergs per centimeter, simply multiply by the area per pixel in cm:
erg/cm^3*(CDELT1^2)*(RSUN_REF/RSUN_OBS ^2)*(100.^2)
= erg/cm(1/pix^2)
"""
count_mask = 0.0
sum = 0.0
sum1 = 0.0
err = 0.0
for j in range(ny):
for i in range (nx):
if ( conf_disambig[j,i] < 70 or bitmap[j,i] < 30 ):
continue
if np.isnan(bx[j,i]):
continue
if np.isnan(by[j,i]):
continue
sum += ( ((bx[j,i] - bpx[j,i])*(bx[j,i] - bpx[j,i])) + ((by[j,i] - bpy[j,i])*(by[j,i] - bpy[j,i])) )*(cdelt1_arcsec*cdelt1_arcsec*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0)
sum1 += ( ((bx[j,i] - bpx[j,i])*(bx[j,i] - bpx[j,i])) + ((by[j,i] - bpy[j,i])*(by[j,i] - bpy[j,i])) )
err += 4.0*(bx[j,i] - bpx[j,i])*(bx[j,i] - bpx[j,i])*(bx_err[j,i]*bx_err[j,i]) + 4.0*(by[j,i] - bpy[j,i])*(by[j,i] - bpy[j,i])*(by_err[j,i]*by_err[j,i])
count_mask += 1
# Units of meanpotptr are ergs per centimeter
meanpot = (sum1) / (count_mask*8.*np.pi)
meanpot_err = (np.sqrt(err)) / (count_mask*8.*np.pi)
# Units of sum are ergs/cm^3, units of factor are cm^2/pix^2; therefore, units of totpotptr are ergs per centimeter
totpot = (sum)/(8.*np.pi)
totpot_err = (np.sqrt(err))*abs(cdelt1_arcsec*cdelt1_arcsec*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0*(1/(8.*np.pi)))
return [meanpot, meanpot_err, totpot, totpot_err]