00001
00002 #define PI (3.141592653589793)
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 int computeAbsFlux(float *bz, int *dims, float *absFlux, float *mean_vf_ptr, int *mask)
00015 {
00016
00017 int nx = dims[0], ny = dims[1];
00018 int i, j, count_mask=0;
00019 double sum=0.0;
00020
00021 if (nx <= 0 || ny <= 0) return 1;
00022
00023 *absFlux = 0.0;
00024 *mean_vf_ptr =0.0;
00025
00026 for (j = 0; j < ny; j++)
00027 {
00028 for (i = 0; i < nx; i++)
00029 {
00030 if (mask[j * nx + i] <= 1) continue;
00031 sum += (fabs(bz[j * nx + i]));
00032 count_mask++;
00033 }
00034 }
00035
00036 printf("nx=%d,ny=%d,count_mask=%d,sum=%f\n",nx,ny,count_mask,sum);
00037 *mean_vf_ptr = sum*(1.30501e15);
00038 return 0;
00039 }
00040
00041
00042
00043
00044
00045 int computeBh(float *bx, float *by, float *bz, float *bh, int *dims, float *mean_hf_ptr, int *mask)
00046 {
00047
00048 int nx = dims[0], ny = dims[1];
00049 int i, j, count_mask=0;
00050 float sum=0.0;
00051 *mean_hf_ptr =0.0;
00052
00053 if (nx <= 0 || ny <= 0) return 1;
00054
00055 for (j = 0; j < ny; j++)
00056 {
00057 for (i = 0; i < nx; i++)
00058 {
00059 bh[j * nx + i] = sqrt( bx[j * nx + i]*bx[j * nx + i] + by[j * nx + i]*by[j * nx + i] );
00060 sum += bh[j * nx + i];
00061 count_mask++;
00062 }
00063 }
00064
00065 *mean_hf_ptr = sum/(count_mask);
00066 printf("*mean_hf_ptr=%f\n",*mean_hf_ptr);
00067 return 0;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077 int computeGamma(float *bx, float *by, float *bz, float *bh, int *dims, float *mean_gamma_ptr, int *mask)
00078 {
00079 int nx = dims[0], ny = dims[1];
00080 int i, j, count_mask=0;
00081
00082 if (nx <= 0 || ny <= 0) return 1;
00083
00084 *mean_gamma_ptr=0.0;
00085 float sum=0.0;
00086 int count=0;
00087
00088 for (i = 0; i < nx; i++)
00089 {
00090 for (j = 0; j < ny; j++)
00091 {
00092 if (bh[j * nx + i] > 100)
00093 {
00094 if (mask[j * nx + i] <= 1) continue;
00095 sum += (atan (fabs( bz[j * nx + i] / bh[j * nx + i] ))* (180./PI));
00096 count_mask++;
00097 }
00098 }
00099 }
00100
00101 *mean_gamma_ptr = sum/count_mask;
00102 printf("*mean_gamma_ptr=%f\n",*mean_gamma_ptr);
00103 return 0;
00104 }
00105
00106
00107
00108
00109
00110
00111 int computeB_total(float *bx, float *by, float *bz, float *bt, int *dims, int *mask)
00112 {
00113
00114 int nx = dims[0], ny = dims[1];
00115 int i, j, count_mask=0;
00116
00117 if (nx <= 0 || ny <= 0) return 1;
00118
00119 for (i = 0; i < nx; i++)
00120 {
00121 for (j = 0; j < ny; j++)
00122 {
00123 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]);
00124 }
00125 }
00126 return 0;
00127 }
00128
00129
00130
00131
00132
00133 int computeBtotalderivative(float *bt, int *dims, float *mean_derivative_btotal_ptr, int *mask)
00134 {
00135
00136 int nx = dims[0], ny = dims[1];
00137 int i, j, count_mask=0;
00138
00139 if (nx <= 0 || ny <= 0) return 1;
00140
00141 *mean_derivative_btotal_ptr = 0.0;
00142 float derx, dery, sum = 0.0;
00143
00144 for (i = 1; i < nx-1; i++)
00145 {
00146 for (j = 1; j < ny-1; j++)
00147 {
00148 if (mask[j * nx + i] <= 1) continue;
00149
00150 derx = bt[j * nx + i+1] - bt[j * nx + i-1];
00151 dery = bt[(j+1) * nx + i] - bt[(j-1) * nx + i];
00152 sum += sqrt(derx*derx + dery*dery);
00153 count_mask++;
00154 }
00155 }
00156
00157 *mean_derivative_btotal_ptr = (sum)/(count_mask);
00158 printf("*mean_derivative_btotal_ptr=%f\n",*mean_derivative_btotal_ptr);
00159 return 0;
00160 }
00161
00162
00163
00164
00165 int computeBhderivative(float *bh, int *dims, float *mean_derivative_bh_ptr, int *mask)
00166 {
00167
00168 int nx = dims[0], ny = dims[1];
00169 int i, j, count_mask=0;
00170
00171 if (nx <= 0 || ny <= 0) return 1;
00172
00173 *mean_derivative_bh_ptr = 0.0;
00174 float derx, dery, sum = 0.0;
00175
00176 for (i = 1; i < nx-1; i++)
00177 {
00178 for (j = 1; j < ny-1; j++)
00179 {
00180 if (mask[j * nx + i] <= 1) continue;
00181
00182 derx = bh[j * nx + i+1] - bh[j * nx + i-1];
00183
00184 dery = bh[(j+1) * nx + i] - bh[(j-1) * nx + i];
00185
00186 sum += sqrt(derx*derx + dery*dery);
00187
00188 count_mask++;
00189
00190 }
00191 }
00192
00193 *mean_derivative_bh_ptr = (sum)/(count_mask);
00194 printf("*mean_derivative_bh_ptr=%f,nx=%d,ny=%d,sum=%f\n",*mean_derivative_bh_ptr,nx,ny,sum);
00195 return 0;
00196 }
00197
00198
00199
00200
00201 int computeBzderivative(float *bz, int *dims, float *mean_derivative_bz_ptr, int *mask)
00202 {
00203
00204 int nx = dims[0], ny = dims[1];
00205 int i, j, count_mask=0;
00206
00207 if (nx <= 0 || ny <= 0) return 1;
00208
00209 *mean_derivative_bz_ptr = 0.0;
00210 float derx, dery, sum = 0.0;
00211
00212 for (i = 1; i < nx-1; i++)
00213 {
00214 for (j = 1; j < ny-1; j++)
00215 {
00216 if (mask[j * nx + i] <= 1) continue;
00217
00218 derx = bz[j * nx + i+1] - bz[j * nx + i-1];
00219 dery = bz[(j+1) * nx + i] - bz[(j-1) * nx + i];
00220 sum += sqrt(derx*derx + dery*dery);
00221 count_mask++;
00222 }
00223 }
00224
00225 *mean_derivative_bz_ptr = (sum)/(count_mask);
00226
00227 return 0;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 int computeJz(float *bx, float *by, int *dims, float *jz, float *mean_jz_ptr, float *us_i_ptr, int *mask)
00261 {
00262
00263 int nx = dims[0], ny = dims[1];
00264 int i, j, count_mask=0;
00265
00266 if (nx <= 0 || ny <= 0) return 1;
00267
00268 *mean_jz_ptr = 0.0;
00269 float derx, dery, curl=0.0, us_i=0.0,test_perimeter=0.0,mean_curl=0.0;
00270
00271 for (i = 1; i < nx-1; i++)
00272 {
00273 for (j = 1; j < ny-1; j++)
00274 {
00275 if (mask[j * nx + i] <= 1) continue;
00276 derx = (by[j * nx + i+1] - by[j * nx + i-1])*0.5;
00277 dery = (bx[(j+1) * nx + i] - bx[(j-1) * nx + i])*0.5;
00278 curl += (derx-dery)*(35.0);
00279 jz[j * nx + i] = (derx-dery);
00280 us_i += fabs(derx-dery)*(18100000000.) ;
00281 count_mask++;
00282 }
00283 }
00284
00285 mean_curl = (curl/count_mask);
00286 printf("mean_curl=%f\n",mean_curl);
00287 *mean_jz_ptr = curl/(count_mask);
00288 printf("count_mask=%d\n",count_mask);
00289 *us_i_ptr = (us_i);
00290 return 0;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 int computeAlpha(float *bz, int *dims, float *jz, float *mean_alpha_ptr, int *mask)
00308 {
00309 int nx = dims[0], ny = dims[1];
00310 int i, j, count_mask=0;
00311
00312 if (nx <= 0 || ny <= 0) return 1;
00313
00314 *mean_alpha_ptr = 0.0;
00315 float aa, bb, cc, bznew, alpha2, sum=0.0;
00316
00317 for (i = 1; i < nx-1; i++)
00318 {
00319 for (j = 1; j < ny-1; j++)
00320 {
00321 if (mask[j * nx + i] <= 1) continue;
00322 if isnan(jz[j * nx + i]) continue;
00323 if isnan(bz[j * nx + i]) continue;
00324 if (bz[j * nx + i] == 0.0) continue;
00325 sum += (jz[j * nx + i] / bz[j * nx + i])*2.7 ;
00326
00327
00328
00329
00330 count_mask++;
00331
00332 }
00333 }
00334
00335 printf("count_mask=%d\n",count_mask);
00336 printf("sum=%f\n",sum);
00337 *mean_alpha_ptr = sum/count_mask;
00338 return 0;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 int computeHelicity(float *bz, int *dims, float *jz, float *mean_ih_ptr, float *total_us_ih_ptr, float *total_abs_ih_ptr, int *mask)
00354 {
00355
00356
00357 int nx = dims[0], ny = dims[1];
00358 int i, j, count_mask=0;
00359
00360 if (nx <= 0 || ny <= 0) return 1;
00361
00362 *mean_ih_ptr = 0.0;
00363 float sum=0.0, sum2=0.0;
00364
00365 for (j = 0; j < ny; j++)
00366 {
00367 for (i = 0; i < nx; i++)
00368 {
00369 if (mask[j * nx + i] <= 1) continue;
00370 if isnan(jz[j * nx + i]) continue;
00371 if isnan(bz[j * nx + i]) continue;
00372 if (bz[j * nx + i] == 0.0) continue;
00373 if (jz[j * nx + i] == 0.0) continue;
00374 sum += (jz[j * nx + i])*(bz[j * nx + i])*(0.000002768166);
00375 sum2 += fabs(jz[j * nx + i]*(bz[j * nx + i]))*(0.000002768166);
00376 count_mask++;
00377 printf("(jz[j * nx + i])=%f\n",(jz[j * nx + i]));
00378 printf("(bz[j * nx + i])=%f\n",(bz[j * nx + i]));
00379 printf("(jz[j * nx + i])*(bz[j * nx + i])*(0.000002768166)=%f\n",(jz[j * nx + i])*(bz[j * nx + i])*(0.000002768166));
00380 printf("sum=%f\n",sum);
00381 printf("sum2=%f\n",sum2);
00382 printf("count_mask=%d\n",count_mask);
00383 }
00384 }
00385
00386 printf("sum/count_mask=%f\n",sum/count_mask);
00387 *mean_ih_ptr = sum/count_mask;
00388 *total_us_ih_ptr = sum2;
00389 *total_abs_ih_ptr= fabs(sum);
00390
00391 return 0;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 int computeSumAbsPerPolarity(float *bz, float *jz, int *dims, float *totaljzptr, int *mask)
00406 {
00407
00408
00409 int nx = dims[0], ny = dims[1];
00410 int i, j, count_mask=0;
00411
00412 if (nx <= 0 || ny <= 0) return 1;
00413
00414 *totaljzptr=0.0;
00415 float sum1=0.0, sum2=0.0;
00416
00417 for (i = 0; i < nx; i++)
00418 {
00419 for (j = 0; j < ny; j++)
00420 {
00421 if (mask[j * nx + i] <= 1) continue;
00422 if (bz[j * nx + i] > 0) sum1 += ( jz[j * nx + i]*18100000000.);
00423 if (bz[j * nx + i] <= 0) sum2 += ( jz[j * nx + i]*18100000000.);
00424 }
00425 }
00426
00427 *totaljzptr = fabs(sum1)+fabs(sum2);
00428 return 0;
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 int computeFreeEnergy(float *bx, float *by, float *bpx, float *bpy, int *dims, float *meanpotptr, float *totpotptr, int *mask)
00445 {
00446 int nx = dims[0], ny = dims[1];
00447 int i, j, count_mask=0;
00448
00449 if (nx <= 0 || ny <= 0) return 1;
00450
00451 *totpotptr=0.0;
00452 *meanpotptr=0.0;
00453 float sum=0.0;
00454
00455 for (i = 0; i < nx; i++)
00456 {
00457 for (j = 0; j < ny; j++)
00458 {
00459 if (mask[j * nx + i] <= 1) continue;
00460 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);
00461 count_mask++;
00462 }
00463 }
00464
00465 *meanpotptr = (sum)/(count_mask);
00466 *totpotptr = sum*(1.30501e15)*(count_mask);
00467 return 0;
00468 }
00469
00470
00471
00472
00473 int computeShearAngle(float *bx, float *by, float *bz, float *bpx, float *bpy, float *bpz, int *dims, float *meanshear_angleptr, float *area_w_shear_gt_45ptr, float *meanshear_anglehptr, float *area_w_shear_gt_45hptr, int *mask)
00474 {
00475 int nx = dims[0], ny = dims[1];
00476 int i, j, count_mask=0;
00477
00478 if (nx <= 0 || ny <= 0) return 1;
00479
00480 *area_w_shear_gt_45ptr=0.0;
00481 *meanshear_angleptr=0.0;
00482 float dotproduct, magnitude_potential, magnitude_vector, shear_angle=0.0, sum = 0.0, count=0.0;
00483 float dotproducth, magnitude_potentialh, magnitude_vectorh, shear_angleh=0.0, sum1 = 0.0, counth = 0.0;
00484
00485 for (i = 0; i < nx; i++)
00486 {
00487 for (j = 0; j < ny; j++)
00488 {
00489 if (mask[j * nx + i] <= 1) continue;
00490
00491 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]);
00492 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]));
00493 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]) );
00494 shear_angle = acos(dotproduct/(magnitude_potential*magnitude_vector))*(180./PI);
00495 sum += shear_angle ;
00496 if (shear_angle > 45) count++;
00497
00498
00499 dotproducth = (bpx[j * nx + i])*(bx[j * nx + i]) + (bpy[j * nx + i])*(by[j * nx + i]);
00500 magnitude_potentialh = sqrt((bpx[j * nx + i]*bpx[j * nx + i]) + (bpy[j * nx + i]*bpy[j * nx + i]));
00501 magnitude_vectorh = sqrt( (bx[j * nx + i]*bx[j * nx + i]) + (by[j * nx + i]*by[j * nx + i]) );
00502 shear_angleh = acos(dotproduct/(magnitude_potential*magnitude_vector))*(180./PI);
00503 sum1 += shear_angleh ;
00504 if (shear_angleh > 45) counth++;
00505 count_mask++;
00506 }
00507 }
00508
00509
00510 *meanshear_angleptr = (sum)/(count_mask);
00511 *area_w_shear_gt_45ptr = (count/(count_mask))*(100.);
00512
00513
00514
00515
00516 return 0;
00517 }
00518
00519
00520
00521
00522 #include <math.h>
00523
00524 void greenpot(float *bx, float *by, float *bz, int nnx, int nny)
00525 {
00526
00527 int inx, iny, i, j, n;
00528
00529 float *pfpot, *rdist;
00530 pfpot=(float *)malloc(sizeof(float) *nnx*nny);
00531 rdist=(float *)malloc(sizeof(float) *nnx*nny);
00532
00533 unsigned long long llnan = 0x7ff0000000000000;
00534
00535
00536
00537 for (iny=0; iny < nny; iny++){for (inx=0; inx < nnx; inx++){pfpot[nnx*iny+inx] = 0.0;}}
00538
00539 for (iny=0; iny < nny; iny++){for (inx=0; inx < nnx; inx++){rdist[nnx*iny+inx] = 0.0;}}
00540
00541 for (iny=0; iny < nny; iny++){for (inx=0; inx < nnx; inx++){bx[nnx*iny+inx] = 0.0;}}
00542
00543 for (iny=0; iny < nny; iny++){for (inx=0; inx < nnx; inx++){by[nnx*iny+inx] = 0.0;}}
00544
00545 float dz;
00546 dz = params_get_float(&cmdparams, "dzvalue");
00547
00548
00549
00550
00551 for (iny=0; iny < nny; iny++){for (inx=0; inx < nnx; inx++)
00552 {
00553 float rdd, rdd1, rdd2;
00554 float r;
00555 rdd1 = (float)(inx);
00556 rdd2 = (float)(iny);
00557 rdd = rdd1 * rdd1 + rdd2 * rdd2 + dz * dz;
00558 rdist[nnx*iny+inx] = 1.0/sqrt(rdd);
00559 }}
00560
00561 int iwindow;
00562 if (nnx > nny) {iwindow = nnx;} else {iwindow = nny;}
00563 float rwindow;
00564 rwindow = (float)(iwindow);
00565 rwindow = rwindow * rwindow + 0.01;
00566
00567 for (iny=0;iny<nny;iny++){for (inx=0;inx<nnx;inx++)
00568 {
00569 float val0 = bz[nnx*iny + inx];
00570 if (isnan(val0))
00571 {
00572 pfpot[nnx*iny + inx] = NAN;
00573 }
00574 else
00575 {
00576 float sum;
00577 sum = 0.0;
00578 int j2, i2;
00579 for (j2=0;j2<nny;j2++){for (i2=0;i2<nnx;i2++)
00580 {
00581 float val1 = bz[nnx*j2 + i2];
00582 float rr, r1, r2;
00583 r1 = (float)(i2 - inx);
00584 r2 = (float)(j2 - iny);
00585 rr = r1*r1 + r2*r2;
00586 if ((!isnan(val1)) && (rr < rwindow))
00587 {
00588 int di, dj;
00589 di = abs(i2 - inx);
00590 dj = abs(j2 - iny);
00591 sum = sum + val1 * rdist[nnx * dj + di] * dz;
00592 }
00593 } }
00594 pfpot[nnx*iny + inx] = sum;
00595 }
00596 } }
00597
00598
00599 for (iny=1; iny < nny - 1; iny++){for (inx=1; inx < nnx - 1; inx++)
00600 {
00601 bx[nnx*iny + inx] = -(pfpot[nnx*iny + (inx+1)]-pfpot[nnx*iny + (inx-1)]) / 2.0;
00602 by[nnx*iny + inx] = -(pfpot[nnx*(iny+1) + inx]-pfpot[nnx*(iny-1) + inx]) / 2.0;
00603 } }
00604 free(pfpot);
00605 }
00606
00607
00608
00609