ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/JSOC/proj/sharp/apps/sw_functions.c
(Generate patch)

Comparing proj/sharp/apps/sw_functions.c (file contents):
Revision 1.3 by mbobra, Tue Oct 23 18:42:56 2012 UTC vs.
Revision 1.21 by mbobra, Mon Nov 11 23:18:40 2013 UTC

# Line 1 | Line 1
1   /*===========================================
2  
3 <   The following 13 functions calculate the following spaceweather indices:
3 >   The following 14 functions calculate the following spaceweather indices:
4  
5      USFLUX Total unsigned flux in Maxwells
6      MEANGAM Mean inclination angle, gamma, in degrees
# Line 20 | Line 20
20  
21     The indices are calculated on the pixels in which the conf_disambig segment is greater than 70 and
22     pixels in which the bitmap segment is greater than 30. These ranges are selected because the CCD
23 <   coordinate bitmaps are interpolated.
23 >   coordinate bitmaps are interpolated for certain data (at the time of this CVS submit, all data
24 >   prior to 2013.08.21_17:24:00_TAI contain interpolated bitmaps; data post-2013.08.21_17:24:00_TAI
25 >   contain nearest-neighbor bitmaps).
26  
27     In the CCD coordinates, this means that we are selecting the pixels that equal 90 in conf_disambig
28 <   and the pixels that equal 33 or 44 in bitmap. Here are the definitions of the pixel values:
28 >   and the pixels that equal 33 or 34 in bitmap. Here are the definitions of the pixel values:
29  
30     For conf_disambig:
31     50 : not all solutions agree (weak field method applied)
# Line 37 | Line 39
39     33 : weak field inside smooth bounding curve
40     34 : strong field inside smooth bounding curve
41  
42 <   Written by Monica Bobra 15 August 2012
42 >   Written by Monica Bobra 15 August 2012
43     Potential Field code (appended) written by Keiji Hayashi
44 +   Error analysis modification 21 October 2013
45  
46   ===========================================*/
47   #include <math.h>
48 + #include <mkl.h>
49  
50 < #define PI              (M_PI)
50 > #define PI       (M_PI)
51   #define MUNAUGHT (0.0000012566370614) /* magnetic constant */
52  
53   /*===========================================*/
# Line 59 | Line 63
63   //  To convert G to G*cm^2, simply multiply by the number of square centimeters per pixel.
64   //  As an order of magnitude estimate, we can assign 0.5 to CDELT1 and 722500m/arcsec to (RSUN_REF/RSUN_OBS).
65   //  (Gauss/pix^2)(CDELT1)^2(RSUN_REF/RSUN_OBS)^2(100.cm/m)^2
66 < //  =(Gauss/pix^2)(0.5 arcsec/pix)^2(722500m/arcsec)^2(100cm/m)^2
63 < //  =(1.30501e15)Gauss*cm^2
66 > //  =Gauss*cm^2
67  
68 < //  The disambig mask value selects only the pixels with values of 5 or 7 -- that is,
69 < //  5: pixels for which the radial acute disambiguation solution was chosen
70 < //  7: pixels for which the radial acute and NRWA disambiguation agree
68 <
69 < int computeAbsFlux(float *bz, int *dims, float *absFlux,
70 <                                   float *mean_vf_ptr, int *mask,  int *bitmask,
71 <                                   float cdelt1, double rsun_ref, double rsun_obs)
68 > int computeAbsFlux(float *bz_err, float *bz, int *dims, float *absFlux,
69 >                   float *mean_vf_ptr, float *mean_vf_err_ptr, float *count_mask_ptr, int *mask,  
70 >                   int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
71  
72   {
73  
74 <    int nx = dims[0], ny = dims[1];
75 <    int i, j, count_mask=0;
76 <    double sum=0.0;
77 <    
78 <    if (nx <= 0 || ny <= 0) return 1;
79 <        
74 >    int nx = dims[0];
75 >    int ny = dims[1];
76 >    int i = 0;
77 >    int j = 0;
78 >    int count_mask = 0;
79 >    double sum = 0.0;
80 >    double err = 0.0;
81      *absFlux = 0.0;
82 <    *mean_vf_ptr =0.0;
82 >    *mean_vf_ptr = 0.0;
83 >
84 >
85 >    if (nx <= 0 || ny <= 0) return 1;
86  
87 <        for (j = 0; j < ny; j++)
87 >        for (i = 0; i < nx; i++)
88          {
89 <                for (i = 0; i < nx; i++)
89 >                for (j = 0; j < ny; j++)
90                  {
91                    if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
92 +                  if isnan(bz[j * nx + i]) continue;
93                    sum += (fabs(bz[j * nx + i]));
94 +                  err += bz_err[j * nx + i]*bz_err[j * nx + i];
95                    count_mask++;
96                  }      
97          }
98  
99 <     printf("nx=%d,ny=%d,count_mask=%d,sum=%f\n",nx,ny,count_mask,sum);
100 <     printf("cdelt1=%f,rsun_ref=%f,rsun_obs=%f\n",cdelt1,rsun_ref,rsun_obs);
101 <     *mean_vf_ptr = sum*cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0;
99 >     *mean_vf_ptr     = sum*cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0;
100 >     *mean_vf_err_ptr = (sqrt(err))*fabs(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0); // error in the unsigned flux
101 >     *count_mask_ptr  = count_mask;  
102       return 0;
103   }
104  
105   /*===========================================*/
106 < /* Example function 2: Calculate Bh in units of Gauss */
106 > /* Example function 2: Calculate Bh, the horizontal field, in units of Gauss */
107   // Native units of Bh are Gauss
108  
109 < int computeBh(float *bx, float *by, float *bz, float *bh, int *dims,
109 > int computeBh(float *bx_err, float *by_err, float *bh_err, float *bx, float *by, float *bz, float *bh, int *dims,
110                            float *mean_hf_ptr, int *mask, int *bitmask)
111  
112   {
113  
114 <    int nx = dims[0], ny = dims[1];
115 <    int i, j, count_mask=0;
116 <    float sum=0.0;      
117 <    *mean_hf_ptr =0.0;
114 >    int nx = dims[0];
115 >    int ny = dims[1];
116 >    int i = 0;
117 >    int j = 0;
118 >    int count_mask = 0;
119 >    double sum = 0.0;  
120 >    *mean_hf_ptr = 0.0;
121  
122      if (nx <= 0 || ny <= 0) return 1;
123  
124 <        for (j = 0; j < ny; j++)
124 >        for (i = 0; i < nx; i++)
125            {
126 <            for (i = 0; i < nx; i++)
127 <              {
128 <                bh[j * nx + i] = sqrt( bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] );
129 <                sum += bh[j * nx + i];
130 <                count_mask++;
126 >            for (j = 0; j < ny; j++)
127 >              {  
128 >                 if isnan(bx[j * nx + i])
129 >                 {  
130 >                    bh[j * nx + i] = NAN;
131 >                    bh_err[j * nx + i] = NAN;
132 >                    continue;
133 >                 }
134 >                 if isnan(by[j * nx + i])
135 >                 {  
136 >                    bh[j * nx + i] = NAN;
137 >                    bh_err[j * nx + i] = NAN;
138 >                    continue;
139 >                 }
140 >                 bh[j * nx + i] = sqrt( bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] );
141 >                 sum += bh[j * nx + i];
142 >                 bh_err[j * nx + i]=sqrt( bx[j * nx + i]*bx[j * nx + i]*bx_err[j * nx + i]*bx_err[j * nx + i] + by[j * nx + i]*by[j * nx + i]*by_err[j * nx + i]*by_err[j * nx + i])/ bh[j * nx + i];
143 >                 count_mask++;
144                }
145            }
146      
147      *mean_hf_ptr = sum/(count_mask); // would be divided by nx*ny if shape of count_mask = shape of magnetogram
148 <    printf("*mean_hf_ptr=%f\n",*mean_hf_ptr);
148 >
149      return 0;
150   }
151  
152   /*===========================================*/
153   /* Example function 3: Calculate Gamma in units of degrees */
154   // Native units of atan(x) are in radians; to convert from radians to degrees, multiply by (180./PI)
155 + //
156 + // Error analysis calculations are done in radians (since derivatives are only true in units of radians),
157 + // and multiplied by (180./PI) at the end for consistency in units.
158  
159 <
160 < int computeGamma(float *bx, float *by, float *bz, float *bh, int *dims,
137 <                                 float *mean_gamma_ptr, int *mask, int *bitmask)
159 > int computeGamma(float *bz_err, float *bh_err, float *bx, float *by, float *bz, float *bh, int *dims,
160 >                 float *mean_gamma_ptr, float *mean_gamma_err_ptr, int *mask, int *bitmask)
161   {
162 <    int nx = dims[0], ny = dims[1];
163 <    int i, j, count_mask=0;
162 >    int nx = dims[0];
163 >    int ny = dims[1];
164 >    int i = 0;
165 >    int j = 0;
166 >    int count_mask = 0;
167 >    double sum = 0.0;
168 >    double err = 0.0;
169 >    *mean_gamma_ptr = 0.0;
170  
171      if (nx <= 0 || ny <= 0) return 1;
143        
144    *mean_gamma_ptr=0.0;
145    float sum=0.0;
146    int count=0;
172  
173          for (i = 0; i < nx; i++)
174            {
# Line 152 | Line 177 | int computeGamma(float *bx, float *by, f
177                  if (bh[j * nx + i] > 100)
178                    {
179                      if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
180 <                    sum += (atan (fabs( bz[j * nx + i] / bh[j * nx + i] ))* (180./PI));
181 <                    count_mask++;
180 >                    if isnan(bz[j * nx + i]) continue;
181 >                    if isnan(bz_err[j * nx + i]) continue;
182 >                    if isnan(bh_err[j * nx + i]) continue;                    
183 >                    if isnan(bh[j * nx + i]) continue;
184 >                    if (bz[j * nx + i] == 0) continue;
185 >                    sum += fabs(atan(bh[j * nx + i]/fabs(bz[j * nx + i])))*(180./PI);
186 >                    err += (1/(1+((bh[j * nx + i]*bh[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i]))))*(1/(1+((bh[j * nx + i]*bh[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i])))) *
187 >                           ( ((bh_err[j * nx + i]*bh_err[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i])) +
188 >                             ((bh[j * nx + i]*bh[j * nx + i]*bz_err[j * nx + i]*bz_err[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i]*bz[j * nx + i]*bz[j * nx + i])) );
189 >                    count_mask++;
190                    }
191                }
192            }
193  
194       *mean_gamma_ptr = sum/count_mask;
195 <     printf("*mean_gamma_ptr=%f\n",*mean_gamma_ptr);
195 >     *mean_gamma_err_ptr = (sqrt(err)/(count_mask))*(180./PI);
196 >     //printf("MEANGAM=%f\n",*mean_gamma_ptr);
197 >     //printf("MEANGAM_err=%f\n",*mean_gamma_err_ptr);
198       return 0;
199   }
200  
# Line 167 | Line 202 | int computeGamma(float *bx, float *by, f
202   /* Example function 4: Calculate B_Total*/
203   // Native units of B_Total are in gauss
204  
205 < int computeB_total(float *bx, float *by, float *bz, float *bt, int *dims, int *mask, int *bitmask)
205 > int computeB_total(float *bx_err, float *by_err, float *bz_err, float *bt_err, float *bx, float *by, float *bz, float *bt, int *dims, int *mask, int *bitmask)
206   {
207  
208 <    int nx = dims[0], ny = dims[1];
209 <    int i, j, count_mask=0;
208 >    int nx = dims[0];
209 >    int ny = dims[1];
210 >    int i = 0;
211 >    int j = 0;
212 >    int count_mask = 0;
213          
214      if (nx <= 0 || ny <= 0) return 1;
215  
# Line 179 | Line 217 | int computeB_total(float *bx, float *by,
217            {
218              for (j = 0; j < ny; j++)
219                {
220 <                bt[j * nx + i] = sqrt( bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] + bz[j * nx + i]*bz[j * nx + i]);
220 >                 if isnan(bx[j * nx + i])
221 >                 {  
222 >                    bt[j * nx + i] = NAN;
223 >                    bt_err[j * nx + i] = NAN;
224 >                    continue;
225 >                 }
226 >                 if isnan(by[j * nx + i])
227 >                 {  
228 >                    bt[j * nx + i] = NAN;
229 >                    bt_err[j * nx + i] = NAN;
230 >                    continue;
231 >                 }
232 >                 if isnan(bz[j * nx + i])
233 >                 {
234 >                    bt[j * nx + i] = NAN;
235 >                    bt_err[j * nx + i] = NAN;
236 >                    continue;
237 >                 }
238 >                 bt[j * nx + i] = sqrt( bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] + bz[j * nx + i]*bz[j * nx + i]);
239 >                 bt_err[j * nx + i]=sqrt(bx[j * nx + i]*bx[j * nx + i]*bx_err[j * nx + i]*bx_err[j * nx + i] + by[j * nx + i]*by[j * nx + i]*by_err[j * nx + i]*by_err[j * nx + i] +  bz[j * nx + i]*bz[j * nx + i]*bz_err[j * nx + i]*bz_err[j * nx + i] ) / bt[j * nx + i];
240                }
241            }
242       return 0;
# Line 188 | Line 245 | int computeB_total(float *bx, float *by,
245   /*===========================================*/
246   /* Example function 5:  Derivative of B_Total SQRT( (dBt/dx)^2 + (dBt/dy)^2 ) */
247  
248 < int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask, int *bitmask, float *derx_bt, float *dery_bt)
248 > int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask, int *bitmask, float *derx_bt, float *dery_bt, float *bt_err, float *mean_derivative_btotal_err_ptr)
249   {
250  
251 <    int nx = dims[0], ny = dims[1];
252 <    int i, j, count_mask=0;
253 <
254 <    if (nx <= 0 || ny <= 0) return 1;
255 <
251 >    int nx = dims[0];
252 >    int ny = dims[1];
253 >    int i = 0;
254 >    int j = 0;
255 >    int count_mask = 0;
256 >    double sum = 0.0;
257 >    double err = 0.0;
258      *mean_derivative_btotal_ptr = 0.0;
200    float sum = 0.0;
259  
260 +    if (nx <= 0 || ny <= 0) return 1;
261  
262          /* brute force method of calculating the derivative (no consideration for edges) */
263          for (i = 1; i <= nx-2; i++)
# Line 245 | Line 304 | int computeBtotalderivative(float *bt, i
304            }
305  
306  
307 <        /* Just some print statements
249 <        for (i = 0; i < nx; i++)
250 <          {
251 <             for (j = 0; j < ny; j++)
252 <              {
253 <              printf("j=%d\n",j);
254 <              printf("i=%d\n",i);
255 <              printf("dery_bt[j*nx+i]=%f\n",dery_bt[j*nx+i]);
256 <              printf("derx_bt[j*nx+i]=%f\n",derx_bt[j*nx+i]);
257 <              printf("bt[j*nx+i]=%f\n",bt[j*nx+i]);
258 <              }
259 <          }
260 <        */
261 <
262 <        for (i = 0; i <= nx-1; i++)
307 >        for (i = 1; i <= nx-2; i++)
308            {
309 <            for (j = 0; j <= ny-1; j++)
309 >            for (j = 1; j <= ny-2; j++)
310              {
266               // if ( (derx_bt[j * nx + i]-dery_bt[j * nx + i]) == 0) continue;
311                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
312 +               if ( (derx_bt[j * nx + i] + dery_bt[j * nx + i]) == 0) continue;
313 +               if isnan(bt[j * nx + i])      continue;
314 +               if isnan(bt[(j+1) * nx + i])  continue;
315 +               if isnan(bt[(j-1) * nx + i])  continue;
316 +               if isnan(bt[j * nx + i-1])    continue;
317 +               if isnan(bt[j * nx + i+1])    continue;
318 +               if isnan(bt_err[j * nx + i])  continue;
319 +               if isnan(derx_bt[j * nx + i]) continue;
320 +               if isnan(dery_bt[j * nx + i]) continue;
321                 sum += sqrt( derx_bt[j * nx + i]*derx_bt[j * nx + i]  + dery_bt[j * nx + i]*dery_bt[j * nx + i]  ); /* Units of Gauss */
322 +               err += (((bt[(j+1) * nx + i]-bt[(j-1) * nx + i])*(bt[(j+1) * nx + i]-bt[(j-1) * nx + i])) * (bt_err[(j+1) * nx + i]*bt_err[(j+1) * nx + i] + bt_err[(j-1) * nx + i]*bt_err[(j-1) * nx + i])) / (16.0*( derx_bt[j * nx + i]*derx_bt[j * nx + i]  + dery_bt[j * nx + i]*dery_bt[j * nx + i]  ))+
323 +                      (((bt[j * nx + (i+1)]-bt[j * nx + (i-1)])*(bt[j * nx + (i+1)]-bt[j * nx + (i-1)])) * (bt_err[j * nx + (i+1)]*bt_err[j * nx + (i+1)] + bt_err[j * nx + (i-1)]*bt_err[j * nx + (i-1)])) / (16.0*( derx_bt[j * nx + i]*derx_bt[j * nx + i]  + dery_bt[j * nx + i]*dery_bt[j * nx + i]  )) ;
324                 count_mask++;
325              }  
326            }
327  
328 <        *mean_derivative_btotal_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
329 <        printf("*mean_derivative_btotal_ptr=%f\n",*mean_derivative_btotal_ptr);
328 >        *mean_derivative_btotal_ptr     = (sum)/(count_mask);
329 >        *mean_derivative_btotal_err_ptr = (sqrt(err))/(count_mask);
330 >        //printf("MEANGBT=%f\n",*mean_derivative_btotal_ptr);
331 >        //printf("MEANGBT_err=%f\n",*mean_derivative_btotal_err_ptr);
332 >
333          return 0;
334   }
335  
# Line 279 | Line 337 | int computeBtotalderivative(float *bt, i
337   /*===========================================*/
338   /* Example function 6:  Derivative of Bh SQRT( (dBh/dx)^2 + (dBh/dy)^2 ) */
339  
340 < int computeBhderivative(float *bh, int *dims, float *mean_derivative_bh_ptr, int *mask, int *bitmask, float *derx_bh, float *dery_bh)
340 > int computeBhderivative(float *bh, float *bh_err, int *dims, float *mean_derivative_bh_ptr, float *mean_derivative_bh_err_ptr, int *mask, int *bitmask, float *derx_bh, float *dery_bh)
341   {
342  
343 <        int nx = dims[0], ny = dims[1];
344 <        int i, j, count_mask=0;
343 >     int nx = dims[0];
344 >     int ny = dims[1];
345 >     int i = 0;
346 >     int j = 0;
347 >     int count_mask = 0;
348 >     double sum= 0.0;
349 >     double err =0.0;    
350 >     *mean_derivative_bh_ptr = 0.0;
351  
352          if (nx <= 0 || ny <= 0) return 1;
353  
290        *mean_derivative_bh_ptr = 0.0;
291        float sum = 0.0;
292
354          /* brute force method of calculating the derivative (no consideration for edges) */
355          for (i = 1; i <= nx-2; i++)
356            {
# Line 335 | Line 396 | int computeBhderivative(float *bh, int *
396            }
397  
398  
338        /*Just some print statements
339        for (i = 0; i < nx; i++)
340          {
341             for (j = 0; j < ny; j++)
342              {
343              printf("j=%d\n",j);
344              printf("i=%d\n",i);
345              printf("dery_bh[j*nx+i]=%f\n",dery_bh[j*nx+i]);
346              printf("derx_bh[j*nx+i]=%f\n",derx_bh[j*nx+i]);
347              printf("bh[j*nx+i]=%f\n",bh[j*nx+i]);
348              }
349          }
350        */
351
399          for (i = 0; i <= nx-1; i++)
400            {
401              for (j = 0; j <= ny-1; j++)
402              {
356               // if ( (derx_bh[j * nx + i]-dery_bh[j * nx + i]) == 0) continue;
403                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
404 +               if ( (derx_bh[j * nx + i] + dery_bh[j * nx + i]) == 0) continue;
405 +               if isnan(bh[j * nx + i])      continue;
406 +               if isnan(bh[(j+1) * nx + i])  continue;
407 +               if isnan(bh[(j-1) * nx + i])  continue;
408 +               if isnan(bh[j * nx + i-1])    continue;
409 +               if isnan(bh[j * nx + i+1])    continue;
410 +               if isnan(bh_err[j * nx + i])  continue;
411 +               if isnan(derx_bh[j * nx + i]) continue;
412 +               if isnan(dery_bh[j * nx + i]) continue;
413                 sum += sqrt( derx_bh[j * nx + i]*derx_bh[j * nx + i]  + dery_bh[j * nx + i]*dery_bh[j * nx + i]  ); /* Units of Gauss */
414 +               err += (((bh[(j+1) * nx + i]-bh[(j-1) * nx + i])*(bh[(j+1) * nx + i]-bh[(j-1) * nx + i])) * (bh_err[(j+1) * nx + i]*bh_err[(j+1) * nx + i] + bh_err[(j-1) * nx + i]*bh_err[(j-1) * nx + i])) / (16.0*( derx_bh[j * nx + i]*derx_bh[j * nx + i]  + dery_bh[j * nx + i]*dery_bh[j * nx + i]  ))+
415 +                      (((bh[j * nx + (i+1)]-bh[j * nx + (i-1)])*(bh[j * nx + (i+1)]-bh[j * nx + (i-1)])) * (bh_err[j * nx + (i+1)]*bh_err[j * nx + (i+1)] + bh_err[j * nx + (i-1)]*bh_err[j * nx + (i-1)])) / (16.0*( derx_bh[j * nx + i]*derx_bh[j * nx + i]  + dery_bh[j * nx + i]*dery_bh[j * nx + i]  )) ;
416                 count_mask++;
417              }  
418            }
419  
420 <        *mean_derivative_bh_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
420 >        *mean_derivative_bh_ptr     = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
421 >        *mean_derivative_bh_err_ptr = (sqrt(err))/(count_mask); // error in the quantity (sum)/(count_mask)
422 >        //printf("MEANGBH=%f\n",*mean_derivative_bh_ptr);
423 >        //printf("MEANGBH_err=%f\n",*mean_derivative_bh_err_ptr);
424 >
425          return 0;
426   }
427  
428   /*===========================================*/
429   /* Example function 7:  Derivative of B_vertical SQRT( (dBz/dx)^2 + (dBz/dy)^2 ) */
430  
431 < int computeBzderivative(float *bz, int *dims, float *mean_derivative_bz_ptr, int *mask, int *bitmask, float *derx_bz, float *dery_bz)
431 > int computeBzderivative(float *bz, float *bz_err, int *dims, float *mean_derivative_bz_ptr, float *mean_derivative_bz_err_ptr, int *mask, int *bitmask, float *derx_bz, float *dery_bz)
432   {
433  
434 <        int nx = dims[0], ny = dims[1];
435 <        int i, j, count_mask=0;
434 >        int nx = dims[0];
435 >        int ny = dims[1];
436 >        int i = 0;
437 >        int j = 0;
438 >        int count_mask = 0;
439 >        double sum = 0.0;
440 >        double err = 0.0;
441 >        *mean_derivative_bz_ptr = 0.0;
442  
443          if (nx <= 0 || ny <= 0) return 1;
444  
378        *mean_derivative_bz_ptr = 0.0;
379        float sum = 0.0;
380
445          /* brute force method of calculating the derivative (no consideration for edges) */
446          for (i = 1; i <= nx-2; i++)
447            {
448              for (j = 0; j <= ny-1; j++)
449                {
450 +                if isnan(bz[j * nx + i]) continue;
451                  derx_bz[j * nx + i] = (bz[j * nx + i+1] - bz[j * nx + i-1])*0.5;
452                }
453            }
# Line 392 | Line 457 | int computeBzderivative(float *bz, int *
457            {
458              for (j = 1; j <= ny-2; j++)
459                {
460 +                if isnan(bz[j * nx + i]) continue;
461                  dery_bz[j * nx + i] = (bz[(j+1) * nx + i] - bz[(j-1) * nx + i])*0.5;
462                }
463            }
# Line 401 | Line 467 | int computeBzderivative(float *bz, int *
467          i=0;
468          for (j = 0; j <= ny-1; j++)
469            {
470 +             if isnan(bz[j * nx + i]) continue;
471               derx_bz[j * nx + i] = ( (-3*bz[j * nx + i]) + (4*bz[j * nx + (i+1)]) - (bz[j * nx + (i+2)]) )*0.5;
472            }
473  
474          i=nx-1;
475          for (j = 0; j <= ny-1; j++)
476            {
477 +             if isnan(bz[j * nx + i]) continue;
478               derx_bz[j * nx + i] = ( (3*bz[j * nx + i]) + (-4*bz[j * nx + (i-1)]) - (-bz[j * nx + (i-2)]) )*0.5;
479            }
480  
481          j=0;
482          for (i = 0; i <= nx-1; i++)
483            {
484 +             if isnan(bz[j * nx + i]) continue;
485               dery_bz[j * nx + i] = ( (-3*bz[j*nx + i]) + (4*bz[(j+1) * nx + i]) - (bz[(j+2) * nx + i]) )*0.5;
486            }
487  
488          j=ny-1;
489          for (i = 0; i <= nx-1; i++)
490            {
491 +             if isnan(bz[j * nx + i]) continue;
492               dery_bz[j * nx + i] = ( (3*bz[j * nx + i]) + (-4*bz[(j-1) * nx + i]) - (-bz[(j-2) * nx + i]) )*0.5;
493            }
494  
495  
426        /*Just some print statements
427        for (i = 0; i < nx; i++)
428          {
429             for (j = 0; j < ny; j++)
430              {
431              printf("j=%d\n",j);
432              printf("i=%d\n",i);
433              printf("dery_bz[j*nx+i]=%f\n",dery_bz[j*nx+i]);
434              printf("derx_bz[j*nx+i]=%f\n",derx_bz[j*nx+i]);
435              printf("bz[j*nx+i]=%f\n",bz[j*nx+i]);
436              }
437          }
438        */
439
496          for (i = 0; i <= nx-1; i++)
497            {
498              for (j = 0; j <= ny-1; j++)
499              {
444               // if ( (derx_bz[j * nx + i]-dery_bz[j * nx + i]) == 0) continue;
500                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
501 +               if ( (derx_bz[j * nx + i] + dery_bz[j * nx + i]) == 0) continue;
502 +               if isnan(bz[j * nx + i])      continue;
503 +               if isnan(bz[(j+1) * nx + i])  continue;
504 +               if isnan(bz[(j-1) * nx + i])  continue;
505 +               if isnan(bz[j * nx + i-1])    continue;
506 +               if isnan(bz[j * nx + i+1])    continue;
507 +               if isnan(bz_err[j * nx + i])  continue;
508 +               if isnan(derx_bz[j * nx + i]) continue;
509 +               if isnan(dery_bz[j * nx + i]) continue;
510                 sum += sqrt( derx_bz[j * nx + i]*derx_bz[j * nx + i]  + dery_bz[j * nx + i]*dery_bz[j * nx + i]  ); /* Units of Gauss */
511 +               err += (((bz[(j+1) * nx + i]-bz[(j-1) * nx + i])*(bz[(j+1) * nx + i]-bz[(j-1) * nx + i])) * (bz_err[(j+1) * nx + i]*bz_err[(j+1) * nx + i] + bz_err[(j-1) * nx + i]*bz_err[(j-1) * nx + i])) /
512 +                        (16.0*( derx_bz[j * nx + i]*derx_bz[j * nx + i]  + dery_bz[j * nx + i]*dery_bz[j * nx + i]  )) +
513 +                      (((bz[j * nx + (i+1)]-bz[j * nx + (i-1)])*(bz[j * nx + (i+1)]-bz[j * nx + (i-1)])) * (bz_err[j * nx + (i+1)]*bz_err[j * nx + (i+1)] + bz_err[j * nx + (i-1)]*bz_err[j * nx + (i-1)])) /
514 +                        (16.0*( derx_bz[j * nx + i]*derx_bz[j * nx + i]  + dery_bz[j * nx + i]*dery_bz[j * nx + i]  )) ;
515                 count_mask++;
516              }  
517            }
518  
519          *mean_derivative_bz_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
520 +        *mean_derivative_bz_err_ptr = (sqrt(err))/(count_mask); // error in the quantity (sum)/(count_mask)
521 +        //printf("MEANGBZ=%f\n",*mean_derivative_bz_ptr);
522 +        //printf("MEANGBZ_err=%f\n",*mean_derivative_bz_err_ptr);
523 +
524          return 0;
525   }
526  
527   /*===========================================*/
456
528   /* Example function 8:  Current Jz = (dBy/dx) - (dBx/dy) */
529  
530   //  In discretized space like data pixels,
# Line 470 | Line 541 | int computeBzderivative(float *bz, int *
541   //  
542   //  To change units from Gauss/pixel to mA/m^2 (the units for Jz in Leka and Barnes, 2003),
543   //  one must perform the following unit conversions:
544 < //  (Gauss/pix)(pix/arcsec)(arcsec/meter)(Newton/Gauss*Ampere*meter)(Ampere^2/Newton)(milliAmpere/Ampere), or
545 < //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(1 T / 10^4 Gauss)(1 / 4*PI*10^-7)( 10^3 milliAmpere/Ampere),
544 > //  (Gauss)(1/arcsec)(arcsec/meter)(Newton/Gauss*Ampere*meter)(Ampere^2/Newton)(milliAmpere/Ampere), or
545 > //  (Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(1 T / 10^4 Gauss)(1 / 4*PI*10^-7)( 10^3 milliAmpere/Ampere), or
546 > //  (Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(1000.),
547   //  where a Tesla is represented as a Newton/Ampere*meter.
548 + //
549   //  As an order of magnitude estimate, we can assign 0.5 to CDELT1 and 722500m/arcsec to (RSUN_REF/RSUN_OBS).
550   //  In that case, we would have the following:
551   //  (Gauss/pix)(1/0.5)(1/722500)(10^-4)(4*PI*10^7)(10^3), or
552   //  jz * (35.0)
553   //
554   //  The units of total unsigned vertical current (us_i) are simply in A. In this case, we would have the following:
555 < //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)(1000.)
556 < //  =(Gauss/pix)(1/CDELT1)(0.0010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(1000.)
484 < //  =(Gauss/pix)(1/0.5)(10^-4)(4*PI*10^7)(722500)(1000.)
485 < //  =(Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(1000.)
555 > //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(CDELT1)(CDELT1)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)
556 > //  = (Gauss/pix)(0.00010)(1/MUNAUGHT)(CDELT1)(RSUN_REF/RSUN_OBS)
557  
487 int computeJz(float *bx, float *by, int *dims, float *jz,
488                          float *mean_jz_ptr, float *us_i_ptr, int *mask, int *bitmask,
489                          float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery)
558  
559 + // Comment out random number generator, which can only run on solar3
560 + //int computeJz(float *bx_err, float *by_err, float *bx, float *by, int *dims, float *jz, float *jz_err, float *jz_err_squared,
561 + //            int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery, float *noisebx,
562 + //              float *noiseby, float *noisebz)
563  
564 + int computeJz(float *bx_err, float *by_err, float *bx, float *by, int *dims, float *jz, float *jz_err, float *jz_err_squared,
565 +              int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery)
566  
493 {
567  
568 <        int nx = dims[0], ny = dims[1];
569 <        int i, j, count_mask=0;
568 > {
569 >        int nx = dims[0];
570 >        int ny = dims[1];
571 >        int i = 0;
572 >        int j = 0;
573 >        int count_mask = 0;
574  
575 <        if (nx <= 0 || ny <= 0) return 1;
499 <
500 <        *mean_jz_ptr = 0.0;
501 <        float curl=0.0, us_i=0.0,test_perimeter=0.0,mean_curl=0.0;
502 <
575 >        if (nx <= 0 || ny <= 0) return 1;
576  
577 +        /* Calculate the derivative*/
578          /* brute force method of calculating the derivative (no consideration for edges) */
579 +
580          for (i = 1; i <= nx-2; i++)
581            {
582              for (j = 0; j <= ny-1; j++)
583                {
584 +                 if isnan(by[j * nx + i]) continue;
585                   derx[j * nx + i] = (by[j * nx + i+1] - by[j * nx + i-1])*0.5;
586                }
587            }
588  
513        /* brute force method of calculating the derivative (no consideration for edges) */
589          for (i = 0; i <= nx-1; i++)
590            {
591              for (j = 1; j <= ny-2; j++)
592                {
593 +                 if isnan(bx[j * nx + i]) continue;
594                   dery[j * nx + i] = (bx[(j+1) * nx + i] - bx[(j-1) * nx + i])*0.5;
595                }
596            }
597  
598 <
523 <        /* consider the edges */
598 >        // consider the edges
599          i=0;
600          for (j = 0; j <= ny-1; j++)
601            {
602 +             if isnan(by[j * nx + i]) continue;
603               derx[j * nx + i] = ( (-3*by[j * nx + i]) + (4*by[j * nx + (i+1)]) - (by[j * nx + (i+2)]) )*0.5;
604            }
605  
606          i=nx-1;
607          for (j = 0; j <= ny-1; j++)
608            {
609 +             if isnan(by[j * nx + i]) continue;
610               derx[j * nx + i] = ( (3*by[j * nx + i]) + (-4*by[j * nx + (i-1)]) - (-by[j * nx + (i-2)]) )*0.5;
611 <          }
611 >          }
612  
613          j=0;
614          for (i = 0; i <= nx-1; i++)
615            {
616 +             if isnan(bx[j * nx + i]) continue;
617               dery[j * nx + i] = ( (-3*bx[j*nx + i]) + (4*bx[(j+1) * nx + i]) - (bx[(j+2) * nx + i]) )*0.5;
618            }
619  
620          j=ny-1;
621 <        for (i = 0; i <= nx-1; i++)
621 >        for (i = 0; i <= nx-1; i++)  
622            {
623 +             if isnan(bx[j * nx + i]) continue;
624               dery[j * nx + i] = ( (3*bx[j * nx + i]) + (-4*bx[(j-1) * nx + i]) - (-bx[(j-2) * nx + i]) )*0.5;
625            }
626  
627 <        /* Just some print statements
628 <        for (i = 0; i < nx; i++)
629 <          {
630 <             for (j = 0; j < ny; j++)
631 <              {
632 <              printf("j=%d\n",j);
633 <              printf("i=%d\n",i);
634 <              printf("dery[j*nx+i]=%f\n",dery[j*nx+i]);
635 <              printf("derx[j*nx+i]=%f\n",derx[j*nx+i]);
636 <              printf("bx[j*nx+i]=%f\n",bx[j*nx+i]);
637 <              printf("by[j*nx+i]=%f\n",by[j*nx+i]);
638 <              }
639 <          }
640 <        */
627 >        for (i = 1; i <= nx-2; i++)
628 >          {
629 >            for (j = 1; j <= ny-2; j++)
630 >            {
631 >               // calculate jz at all points
632 >
633 >               jz[j * nx + i]            = (derx[j * nx + i]-dery[j * nx + i]);       // jz is in units of Gauss/pix
634 >               jz_err[j * nx + i]        = 0.5*sqrt( (bx_err[(j+1) * nx + i]*bx_err[(j+1) * nx + i]) + (bx_err[(j-1) * nx + i]*bx_err[(j-1) * nx + i]) +
635 >                                                     (by_err[j * nx + (i+1)]*by_err[j * nx + (i+1)]) + (by_err[j * nx + (i-1)]*by_err[j * nx + (i-1)]) ) ;
636 >               jz_err_squared[j * nx + i]= (jz_err[j * nx + i]*jz_err[j * nx + i]);
637 >               count_mask++;
638 >
639 >            }  
640 >          }          
641 >        return 0;
642 > }
643 >
644 > /*===========================================*/
645 >
646 > /* Example function 9:  Compute quantities on Jz array */
647 > // Compute mean and total current on Jz array.
648 >
649 > int computeJzsmooth(float *bx, float *by, int *dims, float *jz, float *jz_smooth, float *jz_err, float *jz_rms_err, float *jz_err_squared_smooth,
650 >                    float *mean_jz_ptr, float *mean_jz_err_ptr, float *us_i_ptr, float *us_i_err_ptr, int *mask, int *bitmask,
651 >                    float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery)
652 >
653 > {
654  
655 +        int nx = dims[0];
656 +        int ny = dims[1];
657 +        int i = 0;
658 +        int j = 0;
659 +        int count_mask = 0;
660 +        double curl = 0.0;
661 +        double us_i = 0.0;
662 +        double err = 0.0;
663  
664 +        if (nx <= 0 || ny <= 0) return 1;
665 +
666 +        /* At this point, use the smoothed Jz array with a Gaussian (FWHM of 4 pix and truncation width of 12 pixels) but keep the original array dimensions*/
667          for (i = 0; i <= nx-1; i++)
668            {
669              for (j = 0; j <= ny-1; j++)
670              {
568               // if ( (derx[j * nx + i]-dery[j * nx + i]) == 0) continue;
671                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
672 <               curl +=     (derx[j * nx + i]-dery[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref)*(0.00010)*(1/MUNAUGHT)*(1000.); /* curl is in units of mA / m^2 */
673 <               us_i += fabs(derx[j * nx + i]-dery[j * nx + i])*(1/cdelt1)*(rsun_ref/rsun_obs)*(0.00010)*(1/MUNAUGHT);         /* us_i is in units of A  / m^2 */
674 <               jz[j * nx + i] = (derx[j * nx + i]-dery[j * nx + i]);                                                          /* jz is in units of Gauss/pix */                                    
675 <
672 >               if isnan(derx[j * nx + i]) continue;
673 >               if isnan(dery[j * nx + i]) continue;
674 >               if isnan(jz[j * nx + i]) continue;
675 >               curl +=     (jz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref)*(0.00010)*(1/MUNAUGHT)*(1000.); /* curl is in units of mA / m^2 */
676 >               us_i += fabs(jz[j * nx + i])*(cdelt1/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/MUNAUGHT);         /* us_i is in units of A */
677 >               err  += (jz_err[j * nx + i]*jz_err[j * nx + i]);
678                 count_mask++;
679 <            }  
679 >            }
680            }
681  
682 <        mean_curl        = (curl/count_mask);
579 <        printf("mean_curl=%f\n",mean_curl);
580 <        printf("cdelt1, what is it?=%f\n",cdelt1);
682 >        /* Calculate mean vertical current density (mean_jz) and total unsigned vertical current (us_i) using smoothed Jz array and continue conditions above */
683          *mean_jz_ptr     = curl/(count_mask);        /* mean_jz gets populated as MEANJZD */
684 <        printf("count_mask=%d\n",count_mask);
685 <        *us_i_ptr        = (us_i);                   /* us_i gets populated as MEANJZD */
684 >        *mean_jz_err_ptr = (sqrt(err)/count_mask)*((1/cdelt1)*(rsun_obs/rsun_ref)*(0.00010)*(1/MUNAUGHT)*(1000.)); // error in the quantity MEANJZD
685 >
686 >        *us_i_ptr        = (us_i);                   /* us_i gets populated as TOTUSJZ */
687 >        *us_i_err_ptr    = (sqrt(err))*((cdelt1/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/MUNAUGHT)); // error in the quantity TOTUSJZ
688 >
689 >        //printf("MEANJZD=%f\n",*mean_jz_ptr);
690 >        //printf("MEANJZD_err=%f\n",*mean_jz_err_ptr);
691 >
692 >        //printf("TOTUSJZ=%g\n",*us_i_ptr);
693 >        //printf("TOTUSJZ_err=%g\n",*us_i_err_ptr);
694 >
695          return 0;
696  
697   }
698  
588
699   /*===========================================*/
590 /* Example function 9:  Twist Parameter, alpha */
700  
701 < // The twist parameter, alpha, is defined as alpha = Jz/Bz and the units are in 1/Mm
701 > /* Example function 10:  Twist Parameter, alpha */
702 >
703 > // The twist parameter, alpha, is defined as alpha = Jz/Bz. In this case, the calculation
704 > // for alpha is weighted by Bz (following Hagino et al., http://adsabs.harvard.edu/abs/2004PASJ...56..831H):
705 >  
706 >       // numerator   = sum of all Jz*Bz
707 >       // denominator = sum of Bz*Bz
708 >       // alpha       = numerator/denominator
709 >
710 > // The units of alpha are in 1/Mm
711   // The units of Jz are in Gauss/pix; the units of Bz are in Gauss.
712   //
713   // Therefore, the units of Jz/Bz = (Gauss/pix)(1/Gauss)(pix/arcsec)(arsec/meter)(meter/Mm), or
714   //                               = (Gauss/pix)(1/Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(10^6)
715   //                               = 1/Mm
716  
717 < int computeAlpha(float *bz, int *dims, float *jz, float *mean_alpha_ptr, int *mask, int *bitmask,
718 <                 float cdelt1, double rsun_ref, double rsun_obs)
717 > int computeAlpha(float *jz_err, float *bz_err, float *bz, int *dims, float *jz, float *jz_smooth, float *mean_alpha_ptr, float *mean_alpha_err_ptr, int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
718 >
719   {
720 <        int nx = dims[0], ny = dims[1];
721 <        int i, j, count_mask=0;
720 >        int nx                     = dims[0];
721 >        int ny                     = dims[1];
722 >        int i                      = 0;
723 >        int j                      = 0;
724 >        double alpha_total         = 0.0;
725 >        double C                   = ((1/cdelt1)*(rsun_obs/rsun_ref)*(1000000.));
726 >        double total               = 0.0;
727 >        double A                   = 0.0;
728 >        double B                   = 0.0;
729  
730          if (nx <= 0 || ny <= 0) return 1;
731 <
732 <        *mean_alpha_ptr = 0.0;
733 <        float aa, bb, cc, bznew, alpha2, sum=0.0;
731 >        for (i = 1; i < nx-1; i++)
732 >          {
733 >            for (j = 1; j < ny-1; j++)
734 >              {
735 >                if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
736 >                if isnan(jz[j * nx + i])   continue;
737 >                if isnan(bz[j * nx + i])   continue;
738 >                if (jz[j * nx + i] == 0.0) continue;
739 >                if (bz[j * nx + i] == 0.0) continue;
740 >                A += jz[j*nx+i]*bz[j*nx+i];
741 >                B += bz[j*nx+i]*bz[j*nx+i];
742 >              }
743 >            }
744  
745          for (i = 1; i < nx-1; i++)
746            {
747              for (j = 1; j < ny-1; j++)
748                {
749                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
750 <                if isnan(jz[j * nx + i]) continue;
751 <                if isnan(bz[j * nx + i]) continue;
750 >                if isnan(jz[j * nx + i])   continue;
751 >                if isnan(bz[j * nx + i])   continue;
752 >                if (jz[j * nx + i] == 0.0) continue;
753                  if (bz[j * nx + i] == 0.0) continue;
754 <                sum += (jz[j * nx + i] / bz[j * nx + i])*((1/cdelt1)*(rsun_obs/rsun_ref)*(1000000.)) ; /* the units for (jz/bz) are 1/Mm */
755 <                count_mask++;
620 <              }
754 >                total += bz[j*nx+i]*bz[j*nx+i]*jz_err[j*nx+i]*jz_err[j*nx+i] + (jz[j*nx+i]-2*bz[j*nx+i]*A/B)*(jz[j*nx+i]-2*bz[j*nx+i]*A/B)*bz_err[j*nx+i]*bz_err[j*nx+i];
755 >              }
756            }
757  
758 <        printf("cdelt1=%f,rsun_ref=%f,rsun_obs=%f\n",cdelt1,rsun_ref,rsun_obs);
759 <        printf("count_mask=%d\n",count_mask);
760 <        printf("sum=%f\n",sum);
761 <        *mean_alpha_ptr = sum/count_mask; /* Units are 1/Mm */
758 >        /* Determine the absolute value of alpha. The units for alpha are 1/Mm */
759 >        alpha_total              = ((A/B)*C);
760 >        *mean_alpha_ptr          = alpha_total;
761 >        *mean_alpha_err_ptr      = (C/B)*(sqrt(total));
762 >
763          return 0;
764   }
765  
766   /*===========================================*/
767 < /* Example function 10:  Helicity (mean current helicty, mean unsigned current helicity, and mean absolute current helicity) */
767 > /* Example function 11:  Helicity (mean current helicty, total unsigned current helicity, absolute value of net current helicity) */
768  
769   //  The current helicity is defined as Bz*Jz and the units are G^2 / m
770   //  The units of Jz are in G/pix; the units of Bz are in G.
771 < //  Therefore, the units of Bz*Jz = (Gauss)*(Gauss/pix) = (Gauss^2/pix)(pix/arcsec)(arcsec/m)
771 > //  Therefore, the units of Bz*Jz = (Gauss)*(Gauss/pix) = (Gauss^2/pix)(pix/arcsec)(arcsec/meter)
772   //                                                      = (Gauss^2/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)
773 < //                                                      = G^2 / m.
773 > //                                                      =  G^2 / m.
774  
775 <
776 < int computeHelicity(float *bz, int *dims, float *jz, float *mean_ih_ptr, float *total_us_ih_ptr,
777 <                                        float *total_abs_ih_ptr, int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
775 > int computeHelicity(float *jz_err, float *jz_rms_err, float *bz_err, float *bz, int *dims, float *jz, float *mean_ih_ptr,
776 >                    float *mean_ih_err_ptr, float *total_us_ih_ptr, float *total_abs_ih_ptr,
777 >                    float *total_us_ih_err_ptr, float *total_abs_ih_err_ptr, int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
778  
779   {
780  
781 <        int nx = dims[0], ny = dims[1];
782 <        int i, j, count_mask=0;
781 >        int nx         = dims[0];
782 >        int ny         = dims[1];
783 >        int i          = 0;
784 >        int j          = 0;
785 >        int count_mask = 0;
786 >        double sum     = 0.0;
787 >        double sum2    = 0.0;
788 >        double err     = 0.0;
789          
790          if (nx <= 0 || ny <= 0) return 1;
791  
792 <        *mean_ih_ptr = 0.0;
651 <        float sum=0.0, sum2=0.0;
652 <
653 <        for (j = 0; j < ny; j++)
792 >        for (i = 0; i < nx; i++)
793          {
794 <                for (i = 0; i < nx; i++)
794 >                for (j = 0; j < ny; j++)
795                  {
796 <                if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
797 <                if isnan(jz[j * nx + i]) continue;
798 <                if isnan(bz[j * nx + i]) continue;
799 <                if (bz[j * nx + i] == 0.0) continue;
800 <                if (jz[j * nx + i] == 0.0) continue;
801 <                sum  +=     (jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref);
802 <                sum2 += fabs(jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref);
803 <                count_mask++;
796 >                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
797 >                  if isnan(jz[j * nx + i])     continue;
798 >                  if isnan(bz[j * nx + i])     continue;
799 >                  if isnan(jz_err[j * nx + i]) continue;
800 >                  if isnan(bz_err[j * nx + i]) continue;
801 >                  if (bz[j * nx + i] == 0.0)   continue;
802 >                  if (jz[j * nx + i] == 0.0)   continue;
803 >                  sum     +=     (jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref); // contributes to MEANJZH and ABSNJZH
804 >                  sum2    += fabs(jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref); // contributes to TOTUSJH
805 >                  err     += (jz_err[j * nx + i]*jz_err[j * nx + i]*bz[j * nx + i]*bz[j * nx + i]) + (bz_err[j * nx + i]*bz_err[j * nx + i]*jz[j * nx + i]*jz[j * nx + i]);
806 >                  count_mask++;
807                  }      
808           }
809  
810 <            printf("sum/count_mask=%f\n",sum/count_mask);
811 <            printf("(1/cdelt1)*(rsun_obs/rsun_ref)=%f\n",(1/cdelt1)*(rsun_obs/rsun_ref));
812 <            *mean_ih_ptr     = sum/count_mask; /* Units are G^2 / m ; keyword is MEANJZH */
813 <            *total_us_ih_ptr = sum2;           /* Units are G^2 / m */
814 <            *total_abs_ih_ptr= fabs(sum);      /* Units are G^2 / m */
810 >        *mean_ih_ptr          = sum/count_mask ; /* Units are G^2 / m ; keyword is MEANJZH */
811 >        *total_us_ih_ptr      = sum2           ; /* Units are G^2 / m ; keyword is TOTUSJH */
812 >        *total_abs_ih_ptr     = fabs(sum)      ; /* Units are G^2 / m ; keyword is ABSNJZH */
813 >
814 >        *mean_ih_err_ptr      = (sqrt(err)/count_mask)*(1/cdelt1)*(rsun_obs/rsun_ref) ; // error in the quantity MEANJZH
815 >        *total_us_ih_err_ptr  = (sqrt(err))*(1/cdelt1)*(rsun_obs/rsun_ref) ;            // error in the quantity TOTUSJH
816 >        *total_abs_ih_err_ptr = (sqrt(err))*(1/cdelt1)*(rsun_obs/rsun_ref) ;            // error in the quantity ABSNJZH
817 >
818 >        //printf("MEANJZH=%f\n",*mean_ih_ptr);
819 >        //printf("MEANJZH_err=%f\n",*mean_ih_err_ptr);
820 >
821 >        //printf("TOTUSJH=%f\n",*total_us_ih_ptr);
822 >        //printf("TOTUSJH_err=%f\n",*total_us_ih_err_ptr);
823 >
824 >        //printf("ABSNJZH=%f\n",*total_abs_ih_ptr);
825 >        //printf("ABSNJZH_err=%f\n",*total_abs_ih_err_ptr);
826  
827          return 0;
828   }
829  
830   /*===========================================*/
831 < /* Example function 11:  Sum of Absolute Value per polarity  */
831 > /* Example function 12:  Sum of Absolute Value per polarity  */
832  
833   //  The Sum of the Absolute Value per polarity is defined as the following:
834   //  fabs(sum(jz gt 0)) + fabs(sum(jz lt 0)) and the units are in Amperes.
835   //  The units of jz are in G/pix. In this case, we would have the following:
836   //  Jz = (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)(RSUN_OBS/RSUN_REF),
837   //     = (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)
838 + //
839 + //  The error in this quantity is the same as the error in the mean vertical current (mean_jz_err).
840  
841 < int computeSumAbsPerPolarity(float *bz, float *jz, int *dims, float *totaljzptr,
841 > int computeSumAbsPerPolarity(float *jz_err, float *bz_err, float *bz, float *jz, int *dims, float *totaljzptr, float *totaljz_err_ptr,
842                                                           int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
843  
844   {      
845 <        int nx = dims[0], ny = dims[1];
846 <        int i, j, count_mask=0;
845 >        int nx = dims[0];
846 >        int ny = dims[1];
847 >        int i=0;
848 >        int j=0;
849 >        int count_mask=0;
850 >        double sum1=0.0;
851 >        double sum2=0.0;
852 >        double err=0.0;
853 >        *totaljzptr=0.0;
854  
855          if (nx <= 0 || ny <= 0) return 1;
694        
695        *totaljzptr=0.0;
696        float sum1=0.0, sum2=0.0;
856      
857          for (i = 0; i < nx; i++)
858            {
859              for (j = 0; j < ny; j++)
860                {
861                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
862 +                if isnan(bz[j * nx + i]) continue;
863 +                if isnan(jz[j * nx + i]) continue;
864                  if (bz[j * nx + i] >  0) sum1 += ( jz[j * nx + i])*(1/cdelt1)*(0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs);
865                  if (bz[j * nx + i] <= 0) sum2 += ( jz[j * nx + i])*(1/cdelt1)*(0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs);
866 +                err += (jz_err[j * nx + i]*jz_err[j * nx + i]);
867 +                count_mask++;
868                }
869            }
870          
871 <        *totaljzptr = fabs(sum1) + fabs(sum2);  /* Units are A */
871 >        *totaljzptr    = fabs(sum1) + fabs(sum2);  /* Units are A */
872 >        *totaljz_err_ptr = sqrt(err)*(1/cdelt1)*fabs((0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs));
873 >        //printf("SAVNCPP=%g\n",*totaljzptr);
874 >        //printf("SAVNCPP_err=%g\n",*totaljz_err_ptr);
875 >
876          return 0;
877   }
878  
879   /*===========================================*/
880 < /* Example function 12:  Mean photospheric excess magnetic energy and total photospheric excess magnetic energy density */
880 > /* Example function 13:  Mean photospheric excess magnetic energy and total photospheric excess magnetic energy density */
881   // The units for magnetic energy density in cgs are ergs per cubic centimeter. The formula B^2/8*PI integrated over all space, dV
882 < // automatically yields erg per cubic centimeter for an input B in Gauss.
882 > // automatically yields erg per cubic centimeter for an input B in Gauss. Note that the 8*PI can come out of the integral; thus,
883 > // the integral is over B^2 dV and the 8*PI is divided at the end.
884   //
885   // Total magnetic energy is the magnetic energy density times dA, or the area, and the units are thus ergs/cm. To convert
886   // ergs per centimeter cubed to ergs per centimeter, simply multiply by the area per pixel in cm:
887 < // erg/cm^3(CDELT1)^2(RSUN_REF/RSUN_OBS)^2(100.)^2
888 < // = erg/cm^3(0.5 arcsec/pix)^2(722500m/arcsec)^2(100cm/m)^2
889 < // = erg/cm^3(1.30501e15)
887 > //   erg/cm^3*(CDELT1^2)*(RSUN_REF/RSUN_OBS ^2)*(100.^2)
888 > // = erg/cm^3*(0.5 arcsec/pix)^2(722500m/arcsec)^2(100cm/m)^2
889 > // = erg/cm^3*(1.30501e15)
890   // = erg/cm(1/pix^2)
891  
892 < int computeFreeEnergy(float *bx, float *by, float *bpx, float *bpy, int *dims,
893 <                                          float *meanpotptr, float *totpotptr, int *mask, int *bitmask,
892 > int computeFreeEnergy(float *bx_err, float *by_err, float *bx, float *by, float *bpx, float *bpy, int *dims,
893 >                      float *meanpotptr, float *meanpot_err_ptr, float *totpotptr, float *totpot_err_ptr, int *mask, int *bitmask,
894                                            float cdelt1, double rsun_ref, double rsun_obs)
895  
896   {
897 <        int nx = dims[0], ny = dims[1];
898 <        int i, j, count_mask=0;
899 <        
897 >        int nx = dims[0];
898 >        int ny = dims[1];
899 >        int i = 0;
900 >        int j = 0;
901 >        int count_mask = 0;
902 >        double sum = 0.0;
903 >        double sum1 = 0.0;
904 >        double err = 0.0;
905 >        *totpotptr = 0.0;
906 >        *meanpotptr = 0.0;
907 >
908          if (nx <= 0 || ny <= 0) return 1;
733        
734        *totpotptr=0.0;
735        *meanpotptr=0.0;
736        float sum=0.0;
909  
910          for (i = 0; i < nx; i++)
911            {
912              for (j = 0; j < ny; j++)
913                {
914                   if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
915 <                 sum += ((    ((bx[j * nx + i])*(bx[j * nx + i]) + (by[j * nx + i])*(by[j * nx + i]) ) -  ((bpx[j * nx + i])*(bpx[j * nx + i]) + (bpy[j * nx + i])*(bpy[j * nx + i]))  )/8.*PI);
915 >                 if isnan(bx[j * nx + i]) continue;
916 >                 if isnan(by[j * nx + i]) continue;
917 >                 sum  += ( ((bx[j * nx + i] - bpx[j * nx + i])*(bx[j * nx + i] - bpx[j * nx + i])) + ((by[j * nx + i] - bpy[j * nx + i])*(by[j * nx + i] - bpy[j * nx + i])) )*(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0);
918 >                 sum1 += (  ((bx[j * nx + i] - bpx[j * nx + i])*(bx[j * nx + i] - bpx[j * nx + i])) + ((by[j * nx + i] - bpy[j * nx + i])*(by[j * nx + i] - bpy[j * nx + i])) );
919 >                 err  += 4.0*(bx[j * nx + i] - bpx[j * nx + i])*(bx[j * nx + i] - bpx[j * nx + i])*(bx_err[j * nx + i]*bx_err[j * nx + i]) +
920 >                         4.0*(by[j * nx + i] - bpy[j * nx + i])*(by[j * nx + i] - bpy[j * nx + i])*(by_err[j * nx + i]*by_err[j * nx + i]);
921                   count_mask++;
922                }
923            }
924  
925 <        *meanpotptr = (sum)/(count_mask);              /* Units are ergs per cubic centimeter */
926 <        *totpotptr  = sum*(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0)*(count_mask);   /* Units of sum are ergs/cm^3, units of factor are cm^2/pix^2, units of count_mask are pix^2; therefore, units of totpotptr are ergs per centimeter */
925 >        /* Units of meanpotptr are ergs per centimeter */
926 >        *meanpotptr      = (sum1) / (count_mask*8.*PI) ;     /* Units are ergs per cubic centimeter */
927 >        *meanpot_err_ptr = (sqrt(err)) / (count_mask*8.*PI); // error in the quantity (sum)/(count_mask)
928 >
929 >        /* Units of sum are ergs/cm^3, units of factor are cm^2/pix^2; therefore, units of totpotptr are ergs per centimeter */
930 >        *totpotptr       = (sum)/(8.*PI);
931 >        *totpot_err_ptr  = (sqrt(err))*fabs(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0*(1/(8.*PI)));
932 >
933 >        //printf("MEANPOT=%g\n",*meanpotptr);
934 >        //printf("MEANPOT_err=%g\n",*meanpot_err_ptr);
935 >
936 >        //printf("TOTPOT=%g\n",*totpotptr);
937 >        //printf("TOTPOT_err=%g\n",*totpot_err_ptr);
938 >
939          return 0;
940   }
941  
942   /*===========================================*/
943 < /* Example function 13:  Mean 3D shear angle, area with shear greater than 45, mean horizontal shear angle, area with horizontal shear angle greater than 45 */
943 > /* Example function 14:  Mean 3D shear angle, area with shear greater than 45, mean horizontal shear angle, area with horizontal shear angle greater than 45 */
944 >
945 > int computeShearAngle(float *bx_err, float *by_err, float *bz_err, float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims,
946 >                      float *meanshear_angleptr, float *meanshear_angle_err_ptr, float *area_w_shear_gt_45ptr, int *mask, int *bitmask)
947 >
948  
756 int computeShearAngle(float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims,
757                                          float *meanshear_angleptr, float *area_w_shear_gt_45ptr,
758                                          float *meanshear_anglehptr, float *area_w_shear_gt_45hptr,
759                                          int *mask, int *bitmask)
949   {      
950 <        int nx = dims[0], ny = dims[1];
951 <        int i, j;
950 >        int nx = dims[0];
951 >        int ny = dims[1];
952 >        int i = 0;
953 >        int j = 0;
954 >        float count_mask = 0;
955 >        float count = 0;
956 >        double dotproduct = 0.0;
957 >        double magnitude_potential = 0.0;
958 >        double magnitude_vector = 0.0;
959 >        double shear_angle = 0.0;
960 >        double denominator = 0.0;
961 >        double term1 = 0.0;
962 >        double term2 = 0.0;
963 >        double term3 = 0.0;
964 >        double sumsum = 0.0;        
965 >        double err = 0.0;
966 >        double part1 = 0.0;
967 >        double part2 = 0.0;
968 >        double part3 = 0.0;
969 >        *area_w_shear_gt_45ptr = 0.0;
970 >        *meanshear_angleptr = 0.0;
971          
972          if (nx <= 0 || ny <= 0) return 1;
765        
766        *area_w_shear_gt_45ptr=0.0;
767        *meanshear_angleptr=0.0;
768        float dotproduct, magnitude_potential, magnitude_vector, shear_angle=0.0, sum = 0.0, count=0.0, count_mask=0.0;
769        float dotproducth, magnitude_potentialh, magnitude_vectorh, shear_angleh=0.0, sum1 = 0.0, counth = 0.0;
973  
974          for (i = 0; i < nx; i++)
975            {
# Line 777 | Line 980 | int computeShearAngle(float *bx, float *
980                   if isnan(bpy[j * nx + i]) continue;                
981                   if isnan(bpz[j * nx + i]) continue;
982                   if isnan(bz[j * nx + i]) continue;
983 <                 /* For mean 3D shear angle, area with shear greater than 45*/
983 >                 if isnan(bx[j * nx + i]) continue;
984 >                 if isnan(by[j * nx + i]) continue;
985 >                 if isnan(bx_err[j * nx + i]) continue;                
986 >                 if isnan(by_err[j * nx + i]) continue;                
987 >                 if isnan(bz_err[j * nx + i]) continue;
988 >
989 >                 /* For mean 3D shear angle, percentage with shear greater than 45*/
990                   dotproduct            = (bpx[j * nx + i])*(bx[j * nx + i]) + (bpy[j * nx + i])*(by[j * nx + i]) + (bpz[j * nx + i])*(bz[j * nx + i]);
991 <                 magnitude_potential   = sqrt((bpx[j * nx + i]*bpx[j * nx + i]) + (bpy[j * nx + i]*bpy[j * nx + i]) + (bpz[j * nx + i]*bpz[j * nx + i]));
992 <                 magnitude_vector      = sqrt( (bx[j * nx + i]*bx[j * nx + i]) + (by[j * nx + i]*by[j * nx + i]) + (bz[j * nx + i]*bz[j * nx + i]) );
993 <                 shear_angle           = acos(dotproduct/(magnitude_potential*magnitude_vector))*(180./PI);
991 >                 magnitude_potential   = sqrt( (bpx[j * nx + i]*bpx[j * nx + i]) + (bpy[j * nx + i]*bpy[j * nx + i]) + (bpz[j * nx + i]*bpz[j * nx + i]));
992 >                 magnitude_vector      = sqrt( (bx[j * nx + i]*bx[j * nx + i])   + (by[j * nx + i]*by[j * nx + i])   + (bz[j * nx + i]*bz[j * nx + i]) );
993 >                 //printf("dotproduct=%f\n",dotproduct);
994 >                 //printf("magnitude_potential=%f\n",magnitude_potential);
995 >                 //printf("magnitude_vector=%f\n",magnitude_vector);
996 >
997 >                 shear_angle            = acos(dotproduct/(magnitude_potential*magnitude_vector))*(180./PI);
998 >                 sumsum                  += shear_angle;
999 >                 //printf("shear_angle=%f\n",shear_angle);
1000                   count ++;
1001 <                 sum += shear_angle ;
1001 >
1002                   if (shear_angle > 45) count_mask ++;
1003 <              }
1004 <          }
1005 <        
1003 >
1004 >                 // For the error analysis
1005 >                
1006 >                 term1 = bx[j * nx + i]*by[j * nx + i]*bpy[j * nx + i] - by[j * nx + i]*by[j * nx + i]*bpx[j * nx + i] + bz[j * nx + i]*bx[j * nx + i]*bpz[j * nx + i] - bz[j * nx + i]*bz[j * nx + i]*bpx[j * nx + i];
1007 >                 term2 = bx[j * nx + i]*bx[j * nx + i]*bpy[j * nx + i] - bx[j * nx + i]*by[j * nx + i]*bpx[j * nx + i] + bx[j * nx + i]*bz[j * nx + i]*bpy[j * nx + i] - bz[j * nx + i]*by[j * nx + i]*bpz[j * nx + i];
1008 >                 term3 = bx[j * nx + i]*bx[j * nx + i]*bpz[j * nx + i] - bx[j * nx + i]*bz[j * nx + i]*bpx[j * nx + i] + by[j * nx + i]*by[j * nx + i]*bpz[j * nx + i] - by[j * nx + i]*bz[j * nx + i]*bpy[j * nx + i];
1009 >                
1010 >                 part1 = bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] + bz[j * nx + i]*bz[j * nx + i];
1011 >                 part2 = bpx[j * nx + i]*bpx[j * nx + i] + bpy[j * nx + i]*bpy[j * nx + i] + bpz[j * nx + i]*bpz[j * nx + i];
1012 >                 part3 = bx[j * nx + i]*bpx[j * nx + i] + by[j * nx + i]*bpy[j * nx + i] + bz[j * nx + i]*bpz[j * nx + i];
1013 >                
1014 >                 // denominator is squared
1015 >                 denominator = part1*part1*part1*part2*(1.0-((part3*part3)/(part1*part2)));
1016 >                
1017 >                 err = (term1*term1*bx_err[j * nx + i]*bx_err[j * nx + i])/(denominator) +
1018 >                       (term1*term1*bx_err[j * nx + i]*bx_err[j * nx + i])/(denominator) +
1019 >                       (term1*term1*bx_err[j * nx + i]*bx_err[j * nx + i])/(denominator) ;
1020 >
1021 >              }
1022 >          }  
1023          /* For mean 3D shear angle, area with shear greater than 45*/
1024 <        *meanshear_angleptr = (sum)/(count);              /* Units are degrees */
1025 <        printf("count_mask=%f\n",count_mask);
1026 <        printf("count=%f\n",count);
1027 <        *area_w_shear_gt_45ptr = (count_mask/(count))*(100.);  /* The area here is a fractional area -- the % of the total area */
1024 >            *meanshear_angleptr = (sumsum)/(count);                 /* Units are degrees */
1025 >        *meanshear_angle_err_ptr = (sqrt(err)/count_mask)*(180./PI);  
1026 >
1027 >        /* The area here is a fractional area -- the % of the total area. This has no error associated with it. */
1028 >        *area_w_shear_gt_45ptr   = (count_mask/(count))*(100.0);
1029 >
1030 >        printf("MEANSHR=%f\n",*meanshear_angleptr);
1031 >        printf("MEANSHR_err=%f\n",*meanshear_angle_err_ptr);
1032 >        printf("SHRGT45=%f\n",*area_w_shear_gt_45ptr);
1033  
1034          return 0;
1035   }
# Line 913 | Line 1150 | void greenpot(float *bx, float *by, floa
1150  
1151  
1152   /*===========END OF KEIJI'S CODE =========================*/
1153 +
1154 + char *sw_functions_version() // Returns CVS version of sw_functions.c
1155 + {
1156 +  return strdup("$Id$");
1157 + }
1158 +
1159   /* ---------------- end of this file ----------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines