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.17 by mbobra, Wed Jul 24 02:35:08 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 42 | Line 42
42  
43   ===========================================*/
44   #include <math.h>
45 + #include <mkl.h>
46  
47 < #define PI              (M_PI)
47 > #define PI       (M_PI)
48   #define MUNAUGHT (0.0000012566370614) /* magnetic constant */
49  
50   /*===========================================*/
# Line 66 | Line 67
67   //  5: pixels for which the radial acute disambiguation solution was chosen
68   //  7: pixels for which the radial acute and NRWA disambiguation agree
69  
70 < int computeAbsFlux(float *bz, int *dims, float *absFlux,
71 <                                   float *mean_vf_ptr, int *mask,  int *bitmask,
72 <                                   float cdelt1, double rsun_ref, double rsun_obs)
70 > int computeAbsFlux(float *bz_err, float *bz, int *dims, float *absFlux,
71 >                   float *mean_vf_ptr, float *mean_vf_err_ptr, float *count_mask_ptr, int *mask,  
72 >                   int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
73  
74   {
75  
76 <    int nx = dims[0], ny = dims[1];
77 <    int i, j, count_mask=0;
78 <    double sum=0.0;
79 <    
80 <    if (nx <= 0 || ny <= 0) return 1;
81 <        
76 >    int nx = dims[0];
77 >    int ny = dims[1];
78 >    int i = 0;
79 >    int j = 0;
80 >    int count_mask = 0;
81 >    double sum = 0.0;
82 >    double err = 0.0;
83      *absFlux = 0.0;
84 <    *mean_vf_ptr =0.0;
84 >    *mean_vf_ptr = 0.0;
85 >
86 >
87 >    if (nx <= 0 || ny <= 0) return 1;
88  
89 <        for (j = 0; j < ny; j++)
89 >        for (i = 0; i < nx; i++)
90          {
91 <                for (i = 0; i < nx; i++)
91 >                for (j = 0; j < ny; j++)
92                  {
93                    if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
94 +                  if isnan(bz[j * nx + i]) continue;
95                    sum += (fabs(bz[j * nx + i]));
96 +                  //printf("i,j,bz[j * nx + i]=%d,%d,%f\n",i,j,bz[j * nx + i]);
97 +                  err += bz_err[j * nx + i]*bz_err[j * nx + i];
98                    count_mask++;
99                  }      
100          }
101  
102 <     printf("nx=%d,ny=%d,count_mask=%d,sum=%f\n",nx,ny,count_mask,sum);
103 <     printf("cdelt1=%f,rsun_ref=%f,rsun_obs=%f\n",cdelt1,rsun_ref,rsun_obs);
104 <     *mean_vf_ptr = sum*cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0;
102 >     *mean_vf_ptr     = sum*cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0;
103 >     *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
104 >     *count_mask_ptr  = count_mask;  
105 >     //printf("cdelt1=%f\n",cdelt1);        
106 >     //printf("rsun_obs=%f\n",rsun_obs);
107 >     //printf("rsun_ref=%f\n",rsun_ref);
108 >     //printf("CMASK=%g\n",*count_mask_ptr);
109 >     //printf("USFLUX=%g\n",*mean_vf_ptr);
110 >     //printf("sum=%f\n",sum);
111 >     //printf("USFLUX_err=%g\n",*mean_vf_err_ptr);
112       return 0;
113   }
114  
115   /*===========================================*/
116 < /* Example function 2: Calculate Bh in units of Gauss */
116 > /* Example function 2: Calculate Bh, the horizontal field, in units of Gauss */
117   // Native units of Bh are Gauss
118  
119 < int computeBh(float *bx, float *by, float *bz, float *bh, int *dims,
119 > int computeBh(float *bx_err, float *by_err, float *bh_err, float *bx, float *by, float *bz, float *bh, int *dims,
120                            float *mean_hf_ptr, int *mask, int *bitmask)
121  
122   {
123  
124 <    int nx = dims[0], ny = dims[1];
125 <    int i, j, count_mask=0;
126 <    float sum=0.0;      
127 <    *mean_hf_ptr =0.0;
124 >    int nx = dims[0];
125 >    int ny = dims[1];
126 >    int i = 0;
127 >    int j = 0;
128 >    int count_mask = 0;
129 >    double sum = 0.0;  
130 >    *mean_hf_ptr = 0.0;
131  
132      if (nx <= 0 || ny <= 0) return 1;
133  
134 <        for (j = 0; j < ny; j++)
134 >        for (i = 0; i < nx; i++)
135            {
136 <            for (i = 0; i < nx; i++)
136 >            for (j = 0; j < ny; j++)
137                {
138 +                if isnan(bx[j * nx + i]) continue;
139 +                if isnan(by[j * nx + i]) continue;
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 + // Redo calculation in radians for error analysis (since derivatives are only true in units of radians).
156  
157 <
158 < int computeGamma(float *bx, float *by, float *bz, float *bh, int *dims,
137 <                                 float *mean_gamma_ptr, int *mask, int *bitmask)
157 > int computeGamma(float *bz_err, float *bh_err, float *bx, float *by, float *bz, float *bh, int *dims,
158 >                 float *mean_gamma_ptr, float *mean_gamma_err_ptr, int *mask, int *bitmask)
159   {
160 <    int nx = dims[0], ny = dims[1];
161 <    int i, j, count_mask=0;
160 >    int nx = dims[0];
161 >    int ny = dims[1];
162 >    int i = 0;
163 >    int j = 0;
164 >    int count_mask = 0;
165 >    double sum = 0.0;
166 >    double err = 0.0;
167 >    *mean_gamma_ptr = 0.0;
168  
169      if (nx <= 0 || ny <= 0) return 1;
143        
144    *mean_gamma_ptr=0.0;
145    float sum=0.0;
146    int count=0;
170  
171          for (i = 0; i < nx; i++)
172            {
# Line 152 | Line 175 | int computeGamma(float *bx, float *by, f
175                  if (bh[j * nx + i] > 100)
176                    {
177                      if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
178 <                    sum += (atan (fabs( bz[j * nx + i] / bh[j * nx + i] ))* (180./PI));
179 <                    count_mask++;
178 >                    if isnan(bz[j * nx + i]) continue;
179 >                    if isnan(bz_err[j * nx + i]) continue;
180 >                    if isnan(bh_err[j * nx + i]) continue;
181 >                    if (bz[j * nx + i] == 0) continue;
182 >                    sum += (atan(fabs(bz[j * nx + i]/bh[j * nx + i] )))*(180./PI);
183 >                    err += (( sqrt ( ((bz_err[j * nx + i]*bz_err[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i])) + ((bh_err[j * nx + i]*bh_err[j * nx + i])/(bh[j * nx + i]*bh[j * nx + i])))  * fabs(bz[j * nx + i]/bh[j * nx + i]) ) / (1 + (bz[j * nx + i]/bh[j * nx + i])*(bz[j * nx + i]/bh[j * nx + i]))) *(180./PI);
184 >                    count_mask++;
185                    }
186                }
187            }
188  
189       *mean_gamma_ptr = sum/count_mask;
190 <     printf("*mean_gamma_ptr=%f\n",*mean_gamma_ptr);
190 >     *mean_gamma_err_ptr = (sqrt(err*err))/(count_mask*100.0); // error in the quantity (sum)/(count_mask)
191 >     //printf("MEANGAM=%f\n",*mean_gamma_ptr);
192 >     //printf("MEANGAM_err=%f\n",*mean_gamma_err_ptr);
193       return 0;
194   }
195  
# Line 167 | Line 197 | int computeGamma(float *bx, float *by, f
197   /* Example function 4: Calculate B_Total*/
198   // Native units of B_Total are in gauss
199  
200 < int computeB_total(float *bx, float *by, float *bz, float *bt, int *dims, int *mask, int *bitmask)
200 > 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)
201   {
202  
203 <    int nx = dims[0], ny = dims[1];
204 <    int i, j, count_mask=0;
203 >    int nx = dims[0];
204 >    int ny = dims[1];
205 >    int i = 0;
206 >    int j = 0;
207 >    int count_mask = 0;
208          
209      if (nx <= 0 || ny <= 0) return 1;
210  
# Line 179 | Line 212 | int computeB_total(float *bx, float *by,
212            {
213              for (j = 0; j < ny; j++)
214                {
215 +                if isnan(bx[j * nx + i]) continue;
216 +                if isnan(by[j * nx + i]) continue;
217 +                if isnan(bz[j * nx + i]) continue;
218                  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]);
219 +                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];
220                }
221            }
222       return 0;
# Line 188 | Line 225 | int computeB_total(float *bx, float *by,
225   /*===========================================*/
226   /* Example function 5:  Derivative of B_Total SQRT( (dBt/dx)^2 + (dBt/dy)^2 ) */
227  
228 < int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask, int *bitmask, float *derx_bt, float *dery_bt)
228 > 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)
229   {
230  
231 <    int nx = dims[0], ny = dims[1];
232 <    int i, j, count_mask=0;
233 <
234 <    if (nx <= 0 || ny <= 0) return 1;
235 <
231 >    int nx = dims[0];
232 >    int ny = dims[1];
233 >    int i = 0;
234 >    int j = 0;
235 >    int count_mask = 0;
236 >    double sum = 0.0;
237 >    double err = 0.0;
238      *mean_derivative_btotal_ptr = 0.0;
200    float sum = 0.0;
239  
240 +    if (nx <= 0 || ny <= 0) return 1;
241  
242          /* brute force method of calculating the derivative (no consideration for edges) */
243          for (i = 1; i <= nx-2; i++)
# Line 245 | Line 284 | int computeBtotalderivative(float *bt, i
284            }
285  
286  
248        /* 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
287          for (i = 0; i <= nx-1; i++)
288            {
289              for (j = 0; j <= ny-1; j++)
290              {
266               // if ( (derx_bt[j * nx + i]-dery_bt[j * nx + i]) == 0) continue;
291                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
292 +               if isnan(derx_bt[j * nx + i]) continue;
293 +               if isnan(dery_bt[j * nx + i]) continue;
294                 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 */
295 +               err += (2.0)*bt_err[j * nx + i]*bt_err[j * nx + i];
296                 count_mask++;
297              }  
298            }
299  
300 <        *mean_derivative_btotal_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
301 <        printf("*mean_derivative_btotal_ptr=%f\n",*mean_derivative_btotal_ptr);
300 >        *mean_derivative_btotal_ptr     = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
301 >        *mean_derivative_btotal_err_ptr = (sqrt(err))/(count_mask); // error in the quantity (sum)/(count_mask)
302 >        //printf("MEANGBT=%f\n",*mean_derivative_btotal_ptr);
303 >        //printf("MEANGBT_err=%f\n",*mean_derivative_btotal_err_ptr);
304          return 0;
305   }
306  
# Line 279 | Line 308 | int computeBtotalderivative(float *bt, i
308   /*===========================================*/
309   /* Example function 6:  Derivative of Bh SQRT( (dBh/dx)^2 + (dBh/dy)^2 ) */
310  
311 < int computeBhderivative(float *bh, int *dims, float *mean_derivative_bh_ptr, int *mask, int *bitmask, float *derx_bh, float *dery_bh)
311 > 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)
312   {
313  
314 <        int nx = dims[0], ny = dims[1];
315 <        int i, j, count_mask=0;
314 >     int nx = dims[0];
315 >     int ny = dims[1];
316 >     int i = 0;
317 >     int j = 0;
318 >     int count_mask = 0;
319 >     double sum= 0.0;
320 >     double err =0.0;    
321 >     *mean_derivative_bh_ptr = 0.0;
322  
323          if (nx <= 0 || ny <= 0) return 1;
324  
290        *mean_derivative_bh_ptr = 0.0;
291        float sum = 0.0;
292
325          /* brute force method of calculating the derivative (no consideration for edges) */
326          for (i = 1; i <= nx-2; i++)
327            {
# Line 335 | Line 367 | int computeBhderivative(float *bh, int *
367            }
368  
369  
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
370          for (i = 0; i <= nx-1; i++)
371            {
372              for (j = 0; j <= ny-1; j++)
373              {
356               // if ( (derx_bh[j * nx + i]-dery_bh[j * nx + i]) == 0) continue;
374                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
375 +               if isnan(derx_bh[j * nx + i]) continue;
376 +               if isnan(dery_bh[j * nx + i]) continue;
377                 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 */
378 +               err += (2.0)*bh_err[j * nx + i]*bh_err[j * nx + i];
379                 count_mask++;
380              }  
381            }
382  
383 <        *mean_derivative_bh_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
383 >        *mean_derivative_bh_ptr     = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
384 >        *mean_derivative_bh_err_ptr = (sqrt(err))/(count_mask); // error in the quantity (sum)/(count_mask)
385 >        //printf("MEANGBH=%f\n",*mean_derivative_bh_ptr);
386 >        //printf("MEANGBH_err=%f\n",*mean_derivative_bh_err_ptr);
387 >
388          return 0;
389   }
390  
391   /*===========================================*/
392   /* Example function 7:  Derivative of B_vertical SQRT( (dBz/dx)^2 + (dBz/dy)^2 ) */
393  
394 < int computeBzderivative(float *bz, int *dims, float *mean_derivative_bz_ptr, int *mask, int *bitmask, float *derx_bz, float *dery_bz)
394 > 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)
395   {
396  
397 <        int nx = dims[0], ny = dims[1];
398 <        int i, j, count_mask=0;
397 >        int nx = dims[0];
398 >        int ny = dims[1];
399 >        int i = 0;
400 >        int j = 0;
401 >        int count_mask = 0;
402 >        double sum = 0.0;
403 >        double err = 0.0;
404 >        *mean_derivative_bz_ptr = 0.0;
405  
406          if (nx <= 0 || ny <= 0) return 1;
407  
378        *mean_derivative_bz_ptr = 0.0;
379        float sum = 0.0;
380
408          /* brute force method of calculating the derivative (no consideration for edges) */
409          for (i = 1; i <= nx-2; i++)
410            {
411              for (j = 0; j <= ny-1; j++)
412                {
413 +                if isnan(bz[j * nx + i]) continue;
414                  derx_bz[j * nx + i] = (bz[j * nx + i+1] - bz[j * nx + i-1])*0.5;
415                }
416            }
# Line 392 | Line 420 | int computeBzderivative(float *bz, int *
420            {
421              for (j = 1; j <= ny-2; j++)
422                {
423 +                if isnan(bz[j * nx + i]) continue;
424                  dery_bz[j * nx + i] = (bz[(j+1) * nx + i] - bz[(j-1) * nx + i])*0.5;
425                }
426            }
# Line 401 | Line 430 | int computeBzderivative(float *bz, int *
430          i=0;
431          for (j = 0; j <= ny-1; j++)
432            {
433 +             if isnan(bz[j * nx + i]) continue;
434               derx_bz[j * nx + i] = ( (-3*bz[j * nx + i]) + (4*bz[j * nx + (i+1)]) - (bz[j * nx + (i+2)]) )*0.5;
435            }
436  
437          i=nx-1;
438          for (j = 0; j <= ny-1; j++)
439            {
440 +             if isnan(bz[j * nx + i]) continue;
441               derx_bz[j * nx + i] = ( (3*bz[j * nx + i]) + (-4*bz[j * nx + (i-1)]) - (-bz[j * nx + (i-2)]) )*0.5;
442            }
443  
444          j=0;
445          for (i = 0; i <= nx-1; i++)
446            {
447 +             if isnan(bz[j * nx + i]) continue;
448               dery_bz[j * nx + i] = ( (-3*bz[j*nx + i]) + (4*bz[(j+1) * nx + i]) - (bz[(j+2) * nx + i]) )*0.5;
449            }
450  
451          j=ny-1;
452          for (i = 0; i <= nx-1; i++)
453            {
454 +             if isnan(bz[j * nx + i]) continue;
455               dery_bz[j * nx + i] = ( (3*bz[j * nx + i]) + (-4*bz[(j-1) * nx + i]) - (-bz[(j-2) * nx + i]) )*0.5;
456            }
457  
458  
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
459          for (i = 0; i <= nx-1; i++)
460            {
461              for (j = 0; j <= ny-1; j++)
462              {
463                 // if ( (derx_bz[j * nx + i]-dery_bz[j * nx + i]) == 0) continue;
464                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
465 +               if isnan(bz[j * nx + i]) continue;
466 +               //if isnan(bz_err[j * nx + i]) continue;
467 +               if isnan(derx_bz[j * nx + i]) continue;
468 +               if isnan(dery_bz[j * nx + i]) continue;
469                 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 */
470 +               err += 2.0*bz_err[j * nx + i]*bz_err[j * nx + i];
471                 count_mask++;
472              }  
473            }
474  
475          *mean_derivative_bz_ptr = (sum)/(count_mask); // would be divided by ((nx-2)*(ny-2)) if shape of count_mask = shape of magnetogram
476 +        *mean_derivative_bz_err_ptr = (sqrt(err))/(count_mask); // error in the quantity (sum)/(count_mask)
477 +        //printf("MEANGBZ=%f\n",*mean_derivative_bz_ptr);
478 +        //printf("MEANGBZ_err=%f\n",*mean_derivative_bz_err_ptr);
479 +
480          return 0;
481   }
482  
483   /*===========================================*/
456
484   /* Example function 8:  Current Jz = (dBy/dx) - (dBx/dy) */
485  
486   //  In discretized space like data pixels,
# Line 470 | Line 497 | int computeBzderivative(float *bz, int *
497   //  
498   //  To change units from Gauss/pixel to mA/m^2 (the units for Jz in Leka and Barnes, 2003),
499   //  one must perform the following unit conversions:
500 < //  (Gauss/pix)(pix/arcsec)(arcsec/meter)(Newton/Gauss*Ampere*meter)(Ampere^2/Newton)(milliAmpere/Ampere), or
501 < //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(1 T / 10^4 Gauss)(1 / 4*PI*10^-7)( 10^3 milliAmpere/Ampere),
500 > //  (Gauss)(1/arcsec)(arcsec/meter)(Newton/Gauss*Ampere*meter)(Ampere^2/Newton)(milliAmpere/Ampere), or
501 > //  (Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(1 T / 10^4 Gauss)(1 / 4*PI*10^-7)( 10^3 milliAmpere/Ampere), or
502 > //  (Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(1000.),
503   //  where a Tesla is represented as a Newton/Ampere*meter.
504 + //
505   //  As an order of magnitude estimate, we can assign 0.5 to CDELT1 and 722500m/arcsec to (RSUN_REF/RSUN_OBS).
506   //  In that case, we would have the following:
507   //  (Gauss/pix)(1/0.5)(1/722500)(10^-4)(4*PI*10^7)(10^3), or
508   //  jz * (35.0)
509   //
510   //  The units of total unsigned vertical current (us_i) are simply in A. In this case, we would have the following:
511 < //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)(1000.)
512 < //  =(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.)
511 > //  (Gauss/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)(0.00010)(1/MUNAUGHT)(CDELT1)(CDELT1)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)
512 > //  = (Gauss/pix)(0.00010)(1/MUNAUGHT)(CDELT1)(RSUN_REF/RSUN_OBS)
513  
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)
514  
515 + // Comment out random number generator, which can only run on solar3
516 + //int computeJz(float *bx_err, float *by_err, float *bx, float *by, int *dims, float *jz, float *jz_err, float *jz_err_squared,
517 + //            int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery, float *noisebx,
518 + //              float *noiseby, float *noisebz)
519  
520 + int computeJz(float *bx_err, float *by_err, float *bx, float *by, int *dims, float *jz, float *jz_err, float *jz_err_squared,
521 +              int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery)
522  
493 {
523  
524 <        int nx = dims[0], ny = dims[1];
525 <        int i, j, count_mask=0;
524 > {
525 >        int nx = dims[0];
526 >        int ny = dims[1];
527 >        int i = 0;
528 >        int j = 0;
529 >        int count_mask = 0;
530  
531 <        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 <
531 >        if (nx <= 0 || ny <= 0) return 1;
532  
533 +        /* Calculate the derivative*/
534          /* brute force method of calculating the derivative (no consideration for edges) */
535 +
536          for (i = 1; i <= nx-2; i++)
537            {
538              for (j = 0; j <= ny-1; j++)
539                {
540 +                 if isnan(by[j * nx + i]) continue;
541                   derx[j * nx + i] = (by[j * nx + i+1] - by[j * nx + i-1])*0.5;
542                }
543            }
544  
513        /* brute force method of calculating the derivative (no consideration for edges) */
545          for (i = 0; i <= nx-1; i++)
546            {
547              for (j = 1; j <= ny-2; j++)
548                {
549 +                 if isnan(bx[j * nx + i]) continue;
550                   dery[j * nx + i] = (bx[(j+1) * nx + i] - bx[(j-1) * nx + i])*0.5;
551                }
552            }
553  
554 <
523 <        /* consider the edges */
554 >        // consider the edges
555          i=0;
556          for (j = 0; j <= ny-1; j++)
557            {
558 +             if isnan(by[j * nx + i]) continue;
559               derx[j * nx + i] = ( (-3*by[j * nx + i]) + (4*by[j * nx + (i+1)]) - (by[j * nx + (i+2)]) )*0.5;
560            }
561  
562          i=nx-1;
563          for (j = 0; j <= ny-1; j++)
564            {
565 +             if isnan(by[j * nx + i]) continue;
566               derx[j * nx + i] = ( (3*by[j * nx + i]) + (-4*by[j * nx + (i-1)]) - (-by[j * nx + (i-2)]) )*0.5;
567 <          }
567 >          }
568  
569          j=0;
570          for (i = 0; i <= nx-1; i++)
571            {
572 +             if isnan(bx[j * nx + i]) continue;
573               dery[j * nx + i] = ( (-3*bx[j*nx + i]) + (4*bx[(j+1) * nx + i]) - (bx[(j+2) * nx + i]) )*0.5;
574            }
575  
576          j=ny-1;
577 <        for (i = 0; i <= nx-1; i++)
577 >        for (i = 0; i <= nx-1; i++)  
578            {
579 +             if isnan(bx[j * nx + i]) continue;
580               dery[j * nx + i] = ( (3*bx[j * nx + i]) + (-4*bx[(j-1) * nx + i]) - (-bx[(j-2) * nx + i]) )*0.5;
581            }
582  
583 <        /* Just some print statements
584 <        for (i = 0; i < nx; i++)
585 <          {
586 <             for (j = 0; j < ny; j++)
587 <              {
588 <              printf("j=%d\n",j);
589 <              printf("i=%d\n",i);
555 <              printf("dery[j*nx+i]=%f\n",dery[j*nx+i]);
556 <              printf("derx[j*nx+i]=%f\n",derx[j*nx+i]);
557 <              printf("bx[j*nx+i]=%f\n",bx[j*nx+i]);
558 <              printf("by[j*nx+i]=%f\n",by[j*nx+i]);
559 <              }
560 <          }
561 <        */
583 >        for (i = 1; i <= nx-2; i++)
584 >          {
585 >            for (j = 1; j <= ny-2; j++)
586 >            {
587 >               // calculate jz at all points
588 >
589 >               jz[j * nx + i]            = (derx[j * nx + i]-dery[j * nx + i]);       // jz is in units of Gauss/pix
590  
591 +               // the next 7 lines can be used with a for loop that goes from i=0;i<=nx-1 and j=0;j<=ny-1.
592 +               //int i1, j1,i2, j2;
593 +               //i1 = i + 1 ; if (i1 >nx-1){i1=nx-1;}
594 +               //j1 = j + 1 ; if (j1 >ny-1){j1=ny-1;}
595 +               //i2 = i - 1; if (i2 < 0){i2 = 0;}
596 +               //j2 = j - 1; if (j2 < 0){i2 = 0;}
597 +               //jz_err[j * nx + i]        = 0.5*sqrt( (bx_err[j1 * nx + i]*bx_err[j1 * nx + i]) + (bx_err[j2 * nx + i]*bx_err[j2 * nx + i]) +
598 +               //                                     (by_err[j * nx + i1]*by_err[j * nx + i1]) + (by_err[j * nx + i2]*by_err[j * nx + i2]) ) ;
599 +
600 +               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]) +
601 +                                                    (by_err[j * nx + (i+1)]*by_err[j * nx + (i+1)]) + (by_err[j * nx + (i-1)]*by_err[j * nx + (i-1)]) ) ;
602 +               jz_err_squared[j * nx + i]= (jz_err[j * nx + i]*jz_err[j * nx + i]);
603 +               count_mask++;
604  
605 +            }  
606 +          }          
607 +        return 0;
608 + }
609 +
610 + /*===========================================*/
611 +
612 +
613 + /* Example function 9:  Compute quantities on Jz array */
614 + // Compute mean and total current on Jz array.
615 +
616 + 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,
617 +                    float *mean_jz_ptr, float *mean_jz_err_ptr, float *us_i_ptr, float *us_i_err_ptr, int *mask, int *bitmask,
618 +                    float cdelt1, double rsun_ref, double rsun_obs,float *derx, float *dery)
619 +
620 + {
621 +
622 +        int nx = dims[0];
623 +        int ny = dims[1];
624 +        int i = 0;
625 +        int j = 0;
626 +        int count_mask = 0;
627 +        double curl = 0.0;
628 +        double us_i = 0.0;
629 +        double err = 0.0;
630 +
631 +        if (nx <= 0 || ny <= 0) return 1;
632 +
633 +        /* 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*/
634          for (i = 0; i <= nx-1; i++)
635            {
636              for (j = 0; j <= ny-1; j++)
637              {
568               // if ( (derx[j * nx + i]-dery[j * nx + i]) == 0) continue;
638                 if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
639 <               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 */
640 <               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 */
641 <               jz[j * nx + i] = (derx[j * nx + i]-dery[j * nx + i]);                                                          /* jz is in units of Gauss/pix */                                    
642 <
639 >               if isnan(derx[j * nx + i]) continue;
640 >               if isnan(dery[j * nx + i]) continue;
641 >               if isnan(jz[j * nx + i]) continue;
642 >               curl +=     (jz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref)*(0.00010)*(1/MUNAUGHT)*(1000.); /* curl is in units of mA / m^2 */
643 >               us_i += fabs(jz[j * nx + i])*(cdelt1/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/MUNAUGHT);         /* us_i is in units of A */
644 >               err  += (jz_err[j * nx + i]*jz_err[j * nx + i]);
645                 count_mask++;
646 <            }  
646 >            }
647            }
648  
649 <        mean_curl        = (curl/count_mask);
579 <        printf("mean_curl=%f\n",mean_curl);
580 <        printf("cdelt1, what is it?=%f\n",cdelt1);
649 >        /* Calculate mean vertical current density (mean_jz) and total unsigned vertical current (us_i) using smoothed Jz array and continue conditions above */
650          *mean_jz_ptr     = curl/(count_mask);        /* mean_jz gets populated as MEANJZD */
651 <        printf("count_mask=%d\n",count_mask);
652 <        *us_i_ptr        = (us_i);                   /* us_i gets populated as MEANJZD */
651 >        *mean_jz_err_ptr = (sqrt(err))*fabs(((rsun_obs/rsun_ref)*(0.00010)*(1/MUNAUGHT)*(1000.))/(count_mask)); // error in the quantity MEANJZD
652 >
653 >        *us_i_ptr        = (us_i);                   /* us_i gets populated as TOTUSJZ */
654 >        *us_i_err_ptr    = (sqrt(err))*fabs((cdelt1/1)*(rsun_ref/rsun_obs)*(0.00010)*(1/MUNAUGHT)); // error in the quantity TOTUSJZ
655 >
656 >        //printf("MEANJZD=%f\n",*mean_jz_ptr);
657 >        //printf("MEANJZD_err=%f\n",*mean_jz_err_ptr);
658 >
659 >        //printf("TOTUSJZ=%g\n",*us_i_ptr);
660 >        //printf("TOTUSJZ_err=%g\n",*us_i_err_ptr);
661 >
662          return 0;
663  
664   }
665  
588
666   /*===========================================*/
590 /* Example function 9:  Twist Parameter, alpha */
667  
668 < // The twist parameter, alpha, is defined as alpha = Jz/Bz and the units are in 1/Mm
668 > /* Example function 10:  Twist Parameter, alpha */
669 >
670 > // The twist parameter, alpha, is defined as alpha = Jz/Bz. In this case, the calculation
671 > // for alpha is calculated in the following way (different from Leka and Barnes' approach):
672 >  
673 >       // (sum of all positive Bz + abs(sum of all negative Bz)) = avg Bz
674 >       // (abs(sum of all Jz at positive Bz) + abs(sum of all Jz at negative Bz)) = avg Jz
675 >       // avg alpha = avg Jz / avg Bz
676 >
677 > // The sign is assigned as follows:
678 > // If the sum of all Bz is greater than 0, then evaluate the sum of Jz at the positive Bz pixels.
679 > // If this value is > 0, then alpha is > 0.
680 > // If this value is < 0, then alpha is <0.
681 > //
682 > // If the sum of all Bz is less than 0, then evaluate the sum of Jz at the negative Bz pixels.
683 > // If this value is > 0, then alpha is < 0.
684 > // If this value is < 0, then alpha is > 0.
685 >
686 > // The units of alpha are in 1/Mm
687   // The units of Jz are in Gauss/pix; the units of Bz are in Gauss.
688   //
689   // Therefore, the units of Jz/Bz = (Gauss/pix)(1/Gauss)(pix/arcsec)(arsec/meter)(meter/Mm), or
690   //                               = (Gauss/pix)(1/Gauss)(1/CDELT1)(RSUN_OBS/RSUN_REF)(10^6)
691   //                               = 1/Mm
692  
693 < int computeAlpha(float *bz, int *dims, float *jz, float *mean_alpha_ptr, int *mask, int *bitmask,
694 <                 float cdelt1, double rsun_ref, double rsun_obs)
693 > 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)
694 >
695   {
696 <        int nx = dims[0], ny = dims[1];
697 <        int i, j, count_mask=0;
696 >        int nx = dims[0];
697 >        int ny = dims[1];
698 >        int i = 0;
699 >        int j = 0;
700 >        int count_mask = 0;
701 >        double a = 0.0;
702 >        double b = 0.0;
703 >        double c = 0.0;
704 >        double d = 0.0;
705 >        double sum1 = 0.0;
706 >        double sum2 = 0.0;
707 >        double sum3 = 0.0;
708 >        double sum4 = 0.0;
709 >        double sum = 0.0;
710 >        double sum5 = 0.0;
711 >        double sum6 = 0.0;
712 >        double sum_err = 0.0;
713  
714          if (nx <= 0 || ny <= 0) return 1;
715  
607        *mean_alpha_ptr = 0.0;
608        float aa, bb, cc, bznew, alpha2, sum=0.0;
609
716          for (i = 1; i < nx-1; i++)
717            {
718              for (j = 1; j < ny-1; j++)
# Line 614 | Line 720 | int computeAlpha(float *bz, int *dims, f
720                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
721                  if isnan(jz[j * nx + i]) continue;
722                  if isnan(bz[j * nx + i]) continue;
723 <                if (bz[j * nx + i] == 0.0) continue;
724 <                sum += (jz[j * nx + i] / bz[j * nx + i])*((1/cdelt1)*(rsun_obs/rsun_ref)*(1000000.)) ; /* the units for (jz/bz) are 1/Mm */
723 >                if (jz[j * nx + i]     == 0.0) continue;
724 >                if (bz_err[j * nx + i] == 0.0) continue;
725 >                if (bz[j * nx + i]     == 0.0) continue;
726 >                if (bz[j * nx + i] >  0) sum1 += ( bz[j * nx + i] ); a++;
727 >                if (bz[j * nx + i] <= 0) sum2 += ( bz[j * nx + i] ); b++;
728 >                if (bz[j * nx + i] >  0) sum3 += ( jz[j * nx + i] ); c++;
729 >                if (bz[j * nx + i] <= 0) sum4 += ( jz[j * nx + i] ); d++;
730 >                sum5    += bz[j * nx + i];
731 >                /* sum_err is a fractional uncertainty */
732 >                sum_err += sqrt(((jz_err[j * nx + i]*jz_err[j * nx + i])/(jz[j * nx + i]*jz[j * nx + i])) + ((bz_err[j * nx + i]*bz_err[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i]))) * fabs( ( (jz[j * nx + i]) / (bz[j * nx + i]) ) *(1/cdelt1)*(rsun_obs/rsun_ref)*(1000000.));
733                  count_mask++;
734                }
735            }
736 +        
737 +        sum     = (((fabs(sum3))+(fabs(sum4)))/((fabs(sum2))+sum1))*((1/cdelt1)*(rsun_obs/rsun_ref)*(1000000.)); /* the units for (jz/bz) are 1/Mm */
738 +        
739 +        /* Determine the sign of alpha */
740 +        if ((sum5 > 0) && (sum3 >  0)) sum=sum;
741 +        if ((sum5 > 0) && (sum3 <= 0)) sum=-sum;
742 +        if ((sum5 < 0) && (sum4 <= 0)) sum=sum;
743 +        if ((sum5 < 0) && (sum4 >  0)) sum=-sum;
744 +
745 +        *mean_alpha_ptr = sum; /* Units are 1/Mm */
746 +        *mean_alpha_err_ptr    = (sqrt(sum_err*sum_err)) / ((a+b+c+d)*100.0); // error in the quantity (sum)/(count_mask); factor of 100 comes from converting percent
747 +
748 +        //printf("MEANALP=%f\n",*mean_alpha_ptr);
749 +        //printf("MEANALP_err=%f\n",*mean_alpha_err_ptr);
750  
623        printf("cdelt1=%f,rsun_ref=%f,rsun_obs=%f\n",cdelt1,rsun_ref,rsun_obs);
624        printf("count_mask=%d\n",count_mask);
625        printf("sum=%f\n",sum);
626        *mean_alpha_ptr = sum/count_mask; /* Units are 1/Mm */
751          return 0;
752   }
753  
754   /*===========================================*/
755 < /* Example function 10:  Helicity (mean current helicty, mean unsigned current helicity, and mean absolute current helicity) */
755 > /* Example function 11:  Helicity (mean current helicty, total unsigned current helicity, absolute value of net current helicity) */
756  
757   //  The current helicity is defined as Bz*Jz and the units are G^2 / m
758   //  The units of Jz are in G/pix; the units of Bz are in G.
759 < //  Therefore, the units of Bz*Jz = (Gauss)*(Gauss/pix) = (Gauss^2/pix)(pix/arcsec)(arcsec/m)
759 > //  Therefore, the units of Bz*Jz = (Gauss)*(Gauss/pix) = (Gauss^2/pix)(pix/arcsec)(arcsec/meter)
760   //                                                      = (Gauss^2/pix)(1/CDELT1)(RSUN_OBS/RSUN_REF)
761 < //                                                      = G^2 / m.
761 > //                                                      =  G^2 / m.
762  
763 <
764 < int computeHelicity(float *bz, int *dims, float *jz, float *mean_ih_ptr, float *total_us_ih_ptr,
765 <                                        float *total_abs_ih_ptr, int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
763 > int computeHelicity(float *jz_err, float *jz_rms_err, float *bz_err, float *bz, int *dims, float *jz, float *mean_ih_ptr,
764 >                    float *mean_ih_err_ptr, float *total_us_ih_ptr, float *total_abs_ih_ptr,
765 >                    float *total_us_ih_err_ptr, float *total_abs_ih_err_ptr, int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
766  
767   {
768  
769 <        int nx = dims[0], ny = dims[1];
770 <        int i, j, count_mask=0;
769 >        int nx = dims[0];
770 >        int ny = dims[1];
771 >        int i = 0;
772 >        int j = 0;
773 >        int count_mask = 0;
774 >        double sum = 0.0;
775 >        double sum2 = 0.0;
776 >        double sum_err = 0.0;
777          
778          if (nx <= 0 || ny <= 0) return 1;
779  
780 <        *mean_ih_ptr = 0.0;
651 <        float sum=0.0, sum2=0.0;
652 <
653 <        for (j = 0; j < ny; j++)
780 >        for (i = 0; i < nx; i++)
781          {
782 <                for (i = 0; i < nx; i++)
782 >                for (j = 0; j < ny; j++)
783                  {
784 <                if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
785 <                if isnan(jz[j * nx + i]) continue;
786 <                if isnan(bz[j * nx + i]) continue;
787 <                if (bz[j * nx + i] == 0.0) continue;
788 <                if (jz[j * nx + i] == 0.0) continue;
789 <                sum  +=     (jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref);
790 <                sum2 += fabs(jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref);
791 <                count_mask++;
784 >                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
785 >                  if isnan(jz[j * nx + i]) continue;
786 >                  if isnan(bz[j * nx + i]) continue;
787 >                  if (bz[j * nx + i] == 0.0) continue;
788 >                  if (jz[j * nx + i] == 0.0) continue;
789 >                  sum     +=     (jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref); // contributes to MEANJZH and ABSNJZH
790 >                  sum2    += fabs(jz[j * nx + i]*bz[j * nx + i])*(1/cdelt1)*(rsun_obs/rsun_ref); // contributes to TOTUSJH
791 >                  sum_err += sqrt(((jz_err[j * nx + i]*jz_err[j * nx + i])/(jz[j * nx + i]*jz[j * nx + i])) + ((bz_err[j * nx + i]*bz_err[j * nx + i])/(bz[j * nx + i]*bz[j * nx + i]))) * fabs(jz[j * nx + i]*bz[j * nx + i]*(1/cdelt1)*(rsun_obs/rsun_ref));
792 >                  count_mask++;
793                  }      
794           }
795  
796 <            printf("sum/count_mask=%f\n",sum/count_mask);
797 <            printf("(1/cdelt1)*(rsun_obs/rsun_ref)=%f\n",(1/cdelt1)*(rsun_obs/rsun_ref));
798 <            *mean_ih_ptr     = sum/count_mask; /* Units are G^2 / m ; keyword is MEANJZH */
799 <            *total_us_ih_ptr = sum2;           /* Units are G^2 / m */
800 <            *total_abs_ih_ptr= fabs(sum);      /* Units are G^2 / m */
796 >        *mean_ih_ptr          = sum/count_mask ; /* Units are G^2 / m ; keyword is MEANJZH */
797 >        *total_us_ih_ptr      = sum2           ; /* Units are G^2 / m ; keyword is TOTUSJH */
798 >        *total_abs_ih_ptr     = fabs(sum)      ; /* Units are G^2 / m ; keyword is ABSNJZH */
799 >
800 >        *mean_ih_err_ptr      = (sqrt(sum_err*sum_err)) / (count_mask*100.0)    ;  // error in the quantity MEANJZH
801 >        *total_us_ih_err_ptr  = (sqrt(sum_err*sum_err)) / (100.0)               ;  // error in the quantity TOTUSJH
802 >        *total_abs_ih_err_ptr = (sqrt(sum_err*sum_err)) / (100.0)               ;  // error in the quantity ABSNJZH
803 >
804 >        //printf("MEANJZH=%f\n",*mean_ih_ptr);
805 >        //printf("MEANJZH_err=%f\n",*mean_ih_err_ptr);
806 >
807 >        //printf("TOTUSJH=%f\n",*total_us_ih_ptr);
808 >        //printf("TOTUSJH_err=%f\n",*total_us_ih_err_ptr);
809 >
810 >        //printf("ABSNJZH=%f\n",*total_abs_ih_ptr);
811 >        //printf("ABSNJZH_err=%f\n",*total_abs_ih_err_ptr);
812  
813          return 0;
814   }
815  
816   /*===========================================*/
817 < /* Example function 11:  Sum of Absolute Value per polarity  */
817 > /* Example function 12:  Sum of Absolute Value per polarity  */
818  
819   //  The Sum of the Absolute Value per polarity is defined as the following:
820   //  fabs(sum(jz gt 0)) + fabs(sum(jz lt 0)) and the units are in Amperes.
821   //  The units of jz are in G/pix. In this case, we would have the following:
822   //  Jz = (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)(RSUN_REF/RSUN_OBS)(RSUN_OBS/RSUN_REF),
823   //     = (Gauss/pix)(1/CDELT1)(0.00010)(1/MUNAUGHT)(RSUN_REF/RSUN_OBS)
824 + //
825 + //  The error in this quantity is the same as the error in the mean vertical current (mean_jz_err).
826  
827 < int computeSumAbsPerPolarity(float *bz, float *jz, int *dims, float *totaljzptr,
827 > int computeSumAbsPerPolarity(float *jz_err, float *bz_err, float *bz, float *jz, int *dims, float *totaljzptr, float *totaljz_err_ptr,
828                                                           int *mask, int *bitmask, float cdelt1, double rsun_ref, double rsun_obs)
829  
830   {      
831 <        int nx = dims[0], ny = dims[1];
832 <        int i, j, count_mask=0;
831 >        int nx = dims[0];
832 >        int ny = dims[1];
833 >        int i=0;
834 >        int j=0;
835 >        int count_mask=0;
836 >        double sum1=0.0;
837 >        double sum2=0.0;
838 >        double err=0.0;
839 >        *totaljzptr=0.0;
840  
841          if (nx <= 0 || ny <= 0) return 1;
694        
695        *totaljzptr=0.0;
696        float sum1=0.0, sum2=0.0;
842      
843          for (i = 0; i < nx; i++)
844            {
845              for (j = 0; j < ny; j++)
846                {
847                  if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
848 +                if isnan(bz[j * nx + i]) continue;
849                  if (bz[j * nx + i] >  0) sum1 += ( jz[j * nx + i])*(1/cdelt1)*(0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs);
850                  if (bz[j * nx + i] <= 0) sum2 += ( jz[j * nx + i])*(1/cdelt1)*(0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs);
851 +                err += (jz_err[j * nx + i]*jz_err[j * nx + i]);
852 +                count_mask++;
853                }
854            }
855          
856 <        *totaljzptr = fabs(sum1) + fabs(sum2);  /* Units are A */
856 >        *totaljzptr    = fabs(sum1) + fabs(sum2);  /* Units are A */
857 >        *totaljz_err_ptr = sqrt(err)*(1/cdelt1)*fabs((0.00010)*(1/MUNAUGHT)*(rsun_ref/rsun_obs));
858 >        //printf("SAVNCPP=%g\n",*totaljzptr);
859 >        //printf("SAVNCPP_err=%g\n",*totaljz_err_ptr);
860 >
861          return 0;
862   }
863  
864   /*===========================================*/
865 < /* Example function 12:  Mean photospheric excess magnetic energy and total photospheric excess magnetic energy density */
865 > /* Example function 13:  Mean photospheric excess magnetic energy and total photospheric excess magnetic energy density */
866   // The units for magnetic energy density in cgs are ergs per cubic centimeter. The formula B^2/8*PI integrated over all space, dV
867 < // automatically yields erg per cubic centimeter for an input B in Gauss.
867 > // automatically yields erg per cubic centimeter for an input B in Gauss. Note that the 8*PI can come out of the integral; thus,
868 > // the integral is over B^2 dV and the 8*PI is divided at the end.
869   //
870   // Total magnetic energy is the magnetic energy density times dA, or the area, and the units are thus ergs/cm. To convert
871   // ergs per centimeter cubed to ergs per centimeter, simply multiply by the area per pixel in cm:
872 < // erg/cm^3(CDELT1)^2(RSUN_REF/RSUN_OBS)^2(100.)^2
873 < // = erg/cm^3(0.5 arcsec/pix)^2(722500m/arcsec)^2(100cm/m)^2
874 < // = erg/cm^3(1.30501e15)
872 > //   erg/cm^3*(CDELT1^2)*(RSUN_REF/RSUN_OBS ^2)*(100.^2)
873 > // = erg/cm^3*(0.5 arcsec/pix)^2(722500m/arcsec)^2(100cm/m)^2
874 > // = erg/cm^3*(1.30501e15)
875   // = erg/cm(1/pix^2)
876  
877 < int computeFreeEnergy(float *bx, float *by, float *bpx, float *bpy, int *dims,
878 <                                          float *meanpotptr, float *totpotptr, int *mask, int *bitmask,
877 > int computeFreeEnergy(float *bx_err, float *by_err, float *bx, float *by, float *bpx, float *bpy, int *dims,
878 >                      float *meanpotptr, float *meanpot_err_ptr, float *totpotptr, float *totpot_err_ptr, int *mask, int *bitmask,
879                                            float cdelt1, double rsun_ref, double rsun_obs)
880  
881   {
882 <        int nx = dims[0], ny = dims[1];
883 <        int i, j, count_mask=0;
884 <        
882 >        int nx = dims[0];
883 >        int ny = dims[1];
884 >        int i = 0;
885 >        int j = 0;
886 >        int count_mask = 0;
887 >        double sum = 0.0;
888 >        double sum1 = 0.0;
889 >        double err = 0.0;
890 >        *totpotptr = 0.0;
891 >        *meanpotptr = 0.0;
892 >
893          if (nx <= 0 || ny <= 0) return 1;
733        
734        *totpotptr=0.0;
735        *meanpotptr=0.0;
736        float sum=0.0;
894  
895          for (i = 0; i < nx; i++)
896            {
897              for (j = 0; j < ny; j++)
898                {
899                   if ( mask[j * nx + i] < 70 || bitmask[j * nx + i] < 30 ) continue;
900 <                 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);
900 >                 if isnan(bx[j * nx + i]) continue;
901 >                 if isnan(by[j * nx + i]) continue;
902 >                 sum  += ( ((bpx[j * nx + i] - bx[j * nx + i])*(bpx[j * nx + i] - bx[j * nx + i])) + ((bpy[j * nx + i] - by[j * nx + i])*(bpy[j * nx + i] - by[j * nx + i])) )*(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0);
903 >                 sum1 += ( ((bpx[j * nx + i] - bx[j * nx + i])*(bpx[j * nx + i] - bx[j * nx + i])) + ((bpy[j * nx + i] - by[j * nx + i])*(bpy[j * nx + i] - by[j * nx + i])) );
904 >                 err  += (4.0*bx[j * nx + i]*bx[j * nx + i]*bx_err[j * nx + i]*bx_err[j * nx + i]) + (4.0*by[j * nx + i]*by[j * nx + i]*by_err[j * nx + i]*by_err[j * nx + i]);
905                   count_mask++;
906                }
907            }
908  
909 <        *meanpotptr = (sum)/(count_mask);              /* Units are ergs per cubic centimeter */
910 <        *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 */
909 >        *meanpotptr      = (sum1/(8.*PI)) / (count_mask);     /* Units are ergs per cubic centimeter */
910 >        *meanpot_err_ptr = (sqrt(err))*fabs(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0) / (count_mask*8.*PI); // error in the quantity (sum)/(count_mask)
911 >
912 >        /* Units of sum are ergs/cm^3, units of factor are cm^2/pix^2; therefore, units of totpotptr are ergs per centimeter */
913 >        *totpotptr       = (sum)/(8.*PI);
914 >        *totpot_err_ptr  = (sqrt(err))*fabs(cdelt1*cdelt1*(rsun_ref/rsun_obs)*(rsun_ref/rsun_obs)*100.0*100.0*(1/(8.*PI)));
915 >
916 >        //printf("MEANPOT=%g\n",*meanpotptr);
917 >        //printf("MEANPOT_err=%g\n",*meanpot_err_ptr);
918 >
919 >        //printf("TOTPOT=%g\n",*totpotptr);
920 >        //printf("TOTPOT_err=%g\n",*totpot_err_ptr);
921 >
922          return 0;
923   }
924  
925   /*===========================================*/
926 < /* 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 */
926 > /* 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 */
927  
928 < int computeShearAngle(float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims,
929 <                                          float *meanshear_angleptr, float *area_w_shear_gt_45ptr,
758 <                                          float *meanshear_anglehptr, float *area_w_shear_gt_45hptr,
759 <                                          int *mask, int *bitmask)
928 > int computeShearAngle(float *bx_err, float *by_err, float *bh_err, float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims,
929 >                      float *meanshear_angleptr, float *meanshear_angle_err_ptr, float *area_w_shear_gt_45ptr, int *mask, int *bitmask)
930   {      
931 <        int nx = dims[0], ny = dims[1];
932 <        int i, j;
931 >        int nx = dims[0];
932 >        int ny = dims[1];
933 >        int i = 0;
934 >        int j = 0;
935 >        int count_mask = 0;
936 >        double dotproduct = 0.0;
937 >        double magnitude_potential = 0.0;
938 >        double magnitude_vector = 0.0;
939 >        double shear_angle = 0.0;
940 >        double err = 0.0;
941 >        double sum = 0.0;
942 >        double count = 0.0;
943 >        *area_w_shear_gt_45ptr = 0.0;
944 >        *meanshear_angleptr = 0.0;
945          
946          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;
947  
948          for (i = 0; i < nx; i++)
949            {
# Line 777 | Line 954 | int computeShearAngle(float *bx, float *
954                   if isnan(bpy[j * nx + i]) continue;                
955                   if isnan(bpz[j * nx + i]) continue;
956                   if isnan(bz[j * nx + i]) continue;
957 +                 if isnan(bx[j * nx + i]) continue;
958 +                 if isnan(by[j * nx + i]) continue;
959                   /* For mean 3D shear angle, area with shear greater than 45*/
960                   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]);
961 <                 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]));
962 <                 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]) );
961 >                 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]));
962 >                 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]) );
963                   shear_angle           = acos(dotproduct/(magnitude_potential*magnitude_vector))*(180./PI);
964                   count ++;
965                   sum += shear_angle ;
966 +                 err += -(1./(1.- sqrt(bx_err[j * nx + i]*bx_err[j * nx + i]+by_err[j * nx + i]*by_err[j * nx + i]+bh_err[j * nx + i]*bh_err[j * nx + i])));            
967                   if (shear_angle > 45) count_mask ++;
968                }
969            }
970          
971          /* For mean 3D shear angle, area with shear greater than 45*/
972 <        *meanshear_angleptr = (sum)/(count);              /* Units are degrees */
973 <        printf("count_mask=%f\n",count_mask);
974 <        printf("count=%f\n",count);
975 <        *area_w_shear_gt_45ptr = (count_mask/(count))*(100.);  /* The area here is a fractional area -- the % of the total area */
972 >        *meanshear_angleptr = (sum)/(count);                 /* Units are degrees */
973 >        *meanshear_angle_err_ptr = (sqrt(err*err))/(count);  // error in the quantity (sum)/(count_mask)
974 >        *area_w_shear_gt_45ptr   = (count_mask/(count))*(100.0);/* The area here is a fractional area -- the % of the total area */
975 >
976 >        //printf("MEANSHR=%f\n",*meanshear_angleptr);
977 >        //printf("MEANSHR_err=%f\n",*meanshear_angle_err_ptr);
978  
979          return 0;
980   }
# Line 913 | Line 1095 | void greenpot(float *bx, float *by, floa
1095  
1096  
1097   /*===========END OF KEIJI'S CODE =========================*/
1098 +
1099 + char *sw_functions_version() // Returns CVS version of sw_functions.c
1100 + {
1101 +  return strdup("$Id$");
1102 + }
1103 +
1104   /* ---------------- end of this file ----------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines