00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #include <jsoc_main.h>
00081 #include "fstats.h"
00082 #include "cartography.h"
00083 #include "magutils.h"
00084
00085
00086 #define RECTANGULAR (0)
00087 #define CASSINI (1)
00088 #define MERCATOR (2)
00089 #define CYLEQA (3)
00090 #define SINEQA (4)
00091 #define GNOMONIC (5)
00092 #define POSTEL (6)
00093 #define STEREOGRAPHIC (7)
00094 #define ORTHOGRAPHIC (8)
00095 #define LAMBERT (9)
00096
00097 #define PI (M_PI)
00098 #define RADSINDEG (PI/180)
00099 #define ARCSECSINRAD (3600*180/PI)
00100 #define RAD2ARCSEC (648000. / M_PI)
00101 #define RSUNM (6.96e8)
00102 #define INTERP_NEAREST_NEIGHBOR (1)
00103 #define INTERP_BILINEAR (2)
00104
00105
00106 extern int synop_solar_image_info(DRMS_Record_t *img, double *xscl, double *yscl, double *ctrx, double *ctry, double *apsd, const char *rsun_key, const char *apsd_key, double *pang, double *ellipse_e, double *ellipse_pa, int *x_invrt, int *y_invrt, int *need_ephem, int AIPS_convention);
00107
00108
00109 extern int synop_plane2sphere (double x, double y, double latc, double lonc, double *lat, double *lon, int projection);
00110 extern int synop_img2sphere(double x, double y, double ang_r, double latc, double lonc, double pa, double *rho, double *lat, double *lon, double *sinlat, double *coslat, double *sig, double *mu, double *chi);
00111 extern int synop_sphere2img(double lat, double lon, double latc, double lonc, double *x, double *y, double xcenter, double ycenter, double rsun, double peff, double ecc, double chi, int xinvrt, int yinvrt);
00112 extern int synop_sphere2plane(double lat, double lon, double latc, double lonc, double *x, double *y, int projection);
00113
00114
00115 char *module_name = "maproj3comperror";
00116 char *module_desc = "mapping from solar images";
00117 char *version_id = "1.0";
00118
00119 ModuleArgs_t module_args[] = {
00120 {ARG_DATASET, "in", "", "input data set"},
00121 {ARG_DATASERIES, "out", "", "output data series"},
00122 {ARG_DOUBLE, "clat", "0.0", "heliographic latitude of map center [deg]"},
00123 {ARG_DOUBLE, "clon", "0.0", "Carrington longitude of map center [deg]"},
00124 {ARG_DOUBLE, "scale", "Not specified", "map scale at center [deg/pxl]"},
00125 {ARG_NUME, "map", "orthographic", "map projection",
00126 "carree, Cassini, Mercator, cyleqa, sineqa, gnomonic, Postel, stereographic, orthographic, Lambert"},
00127 {ARG_NUME, "interp", "cubiconv", "interpolation option",
00128 "cubiconv, nearest, bilinear"},
00129 {ARG_FLOAT, "grid", "Not Specified",
00130 "if specified, spacing of grid overlay [deg]"},
00131 {ARG_INT, "cols", "0", "columns in output map"},
00132 {ARG_INT, "rows", "0", "rows in output map"},
00133 {ARG_FLOAT, "map_pa", "0.0", "position angle of north on output map [deg]"},
00134 {ARG_FLOAT, "bscale", "0.0", "output scaling factor"},
00135 {ARG_FLOAT, "bzero", "Default", "output offset"},
00136 {ARG_STRING, "clon_key", "CRLN_OBS", "keyname for image central longitude"},
00137 {ARG_STRING, "clat_key", "CRLT_OBS", "keyname for image central latitude"},
00138 {ARG_STRING, "rsun_key", "R_SUN", "keyname for image semi-diameter (pixel)"},
00139 {ARG_STRING, "apsd_key", "RSUN_OBS", "keyname for apparent solar semi-diameter (arcsec)"},
00140 {ARG_STRING, "dsun_key", "DSUN_OBS", "keyname for observer distance"},
00141 {ARG_FLAG, "c", "", "center map at center of image"},
00142 {ARG_FLAG, "s", "", "clon is Stonyhurst longitude"},
00143 {ARG_FLAG, "v", "", "verbose mode"},
00144 {}
00145 };
00146
00147
00148 float missing_val;
00149
00150
00151 void ccker (double *u, double s) {
00152 double s2, s3;
00153
00154 s2= s * s;
00155 s3= s2 * s;
00156 u[0] = s2 - 0.5 * (s3 + s);
00157 u[1] = 1.5*s3 - 2.5*s2 + 1.0;
00158 u[2] = -1.5*s3 + 2.0*s2 + 0.5*s;
00159 u[3] = 0.5 * (s3 - s2);
00160 }
00161
00162 float ccint2 (float *f, int nx, int ny, double x, double y) {
00163 double ux[4], uy[4];
00164 double sum;
00165 int ix, iy, ix1, iy1, i, j;
00166
00167 if (x < 1.0 || x >= (float)(nx-2) || y < 1.0 || y >= (float)(ny-2))
00168 return missing_val;
00169
00170 ix = (int)x;
00171 ccker (ux, x - (double)ix);
00172 iy = (int)y;
00173 ccker (uy, y - (double)iy);
00174
00175 ix1 = ix - 1;
00176 iy1 = iy - 1;
00177 sum = 0.;
00178 for (i = 0; i < 4; i++)
00179 for (j = 0; j < 4; j++)
00180 sum = sum + f[(iy1+i) * nx + ix1 + j] * uy[i] * ux[j];
00181 return (float)sum;
00182 }
00183
00184
00185 float ccint2_error (float *f, int nx, int ny, double x, double y) {
00186 double ux[4], uy[4];
00187 double sum, squareValue;
00188 int ix, iy, ix1, iy1, i, j;
00189
00190 if (x < 1.0 || x >= (float)(nx-2) || y < 1.0 || y >= (float)(ny-2))
00191 return missing_val;
00192
00193 ix = (int)x;
00194 ccker (ux, x - (double)ix);
00195 iy = (int)y;
00196 ccker (uy, y - (double)iy);
00197
00198 ix1 = ix - 1;
00199 iy1 = iy - 1;
00200 sum = 0.;
00201 for (i = 0; i < 4; i++){
00202 for (j = 0; j < 4; j++){
00203 squareValue = f[(iy1+i) * nx + ix1 + j] * uy[i] * ux[j];
00204 sum = sum + squareValue * squareValue;
00205 }
00206 }
00207 return (float)sqrt(sum);
00208 }
00209
00210 float linint2 (float *f, int cols, int rows, double x, double y) {
00211 double p, q, val;
00212 int col = (int)x, row = (int)y;
00213 int onerow = cols * row;
00214 int colp1 = col + 1, onerowp1 = onerow + cols;
00215
00216 if (x < 0.0 || x > cols || y < 0.0 || y >= rows)
00217 return missing_val;
00218 p = x - col;
00219 q = y - row;
00220 val = (1 - p) * (1 - q) * f[col + onerow]
00221 + p * (1 - q) * f[colp1 + onerow]
00222 + (1 - p) * q * f[col + onerowp1]
00223 + p * q * f[colp1 + onerowp1];
00224 return val;
00225 }
00226
00227
00228 float linint2_error (float *f, int cols, int rows, double x, double y) {
00229 double p, q, val;
00230 int col = (int)x, row = (int)y;
00231 int onerow = cols * row;
00232 int colp1 = col + 1, onerowp1 = onerow + cols;
00233
00234 if (x < 0.0 || x > cols || y < 0.0 || y >= rows)
00235 return missing_val;
00236 p = x - col;
00237 q = y - row;
00238 val = (1 - p) * (1 - q) * f[col + onerow] * (1 - p) * (1 - q) * f[col + onerow]
00239 + p * (1 - q) * f[colp1 + onerow] * p * (1 - q) * f[colp1 + onerow]
00240 + (1 - p) * q * f[col + onerowp1] * (1 - p) * q * f[col + onerowp1]
00241 + p * q * f[colp1 + onerowp1] * p * q * f[colp1 + onerowp1];
00242 return sqrt(val);
00243 }
00244
00245
00246 float nearest_val (float *f, int cols, int rows, double x, double y) {
00247 int col, row;
00248 if (x < -0.5 || x >= (cols - 0.5) || y < -0.5 || y >= (rows - 0.5))
00249 return missing_val;
00250 col = x + 0.5;
00251 row = y + 0.5;
00252 return f[col + row * cols];
00253 }
00254
00255
00256 float array_imaginterp (DRMS_Array_t *img, double x, double y,
00257 int schema) {
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 double xs, ys;
00283 int cols, rows, mdim;
00284
00285 cols = img->axis[0];
00286 rows = img->axis[1];
00287 mdim = (cols > rows) ? cols : rows;
00288 xs = 0.5 * (x + 1.0) * mdim - 0.5;
00289 ys = 0.5 * (y + 1.0) * mdim - 0.5;
00290 if (schema == INTERP_NEAREST_NEIGHBOR)
00291 return nearest_val (img->data, cols, rows, xs, ys);
00292 else if (schema == INTERP_BILINEAR)
00293 return linint2 (img->data, cols, rows, xs, ys);
00294 else return ccint2 (img->data, cols, rows, xs, ys);
00295 }
00296
00297 float array_imaginterp_error (DRMS_Array_t *img, double x, double y,
00298 int schema) {
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 double xs, ys;
00325 int cols, rows, mdim;
00326
00327 cols = img->axis[0];
00328 rows = img->axis[1];
00329 mdim = (cols > rows) ? cols : rows;
00330 xs = 0.5 * (x + 1.0) * mdim - 0.5;
00331 ys = 0.5 * (y + 1.0) * mdim - 0.5;
00332 if (schema == INTERP_NEAREST_NEIGHBOR)
00333 return nearest_val (img->data, cols, rows, xs, ys);
00334 else if (schema == INTERP_BILINEAR)
00335 return linint2_error (img->data, cols, rows, xs, ys);
00336 else return ccint2_error (img->data, cols, rows, xs, ys);
00337 }
00338
00339 void perform_mapping (DRMS_Array_t *img, float *map,
00340 double *maplat, double *maplon, double *map_coslat, double *map_sinlat,
00341 int pixct, unsigned char *offsun, double latc, double lonc,
00342 double xc, double yc, double radius, double pa, double ellipse_e,
00343 double ellipse_pa, int x_invrt, int y_invrt, int interpolator,
00344 int MDI_correct_distort) {
00345
00346
00347
00348
00349
00350 static double sin_asd = 0.004660, cos_asd = 0.99998914;
00351
00352 double r, cos_cang, xr, yr;
00353 double cos_lat, sin_lat, lon, cos_lat_lon;
00354 double xx, yy;
00355 float interpval;
00356 int n;
00357
00358 double cos_pa = cos (pa);
00359 double sin_pa = sin (pa);
00360 double cos_latc = cos (latc);
00361 double sin_latc = sin (latc);
00362 int plate_cols = img->axis[0];
00363 int plate_rows = img->axis[1];
00364 double plate_width = (plate_cols > plate_rows) ? plate_cols : plate_rows;
00365
00366 xc *= 2.0 / plate_width;
00367 yc *= 2.0 / plate_width;
00368 radius *= 2.0 / plate_width;
00369
00370 for (n = 0; n < pixct; n++) {
00371
00372 if (offsun[n]) {
00373 map[n] = missing_val;
00374 continue;
00375 }
00376 sin_lat = map_sinlat[n];
00377 cos_lat = map_coslat[n];
00378 lon = maplon[n];
00379 cos_lat_lon = cos_lat * cos (lon - lonc);
00380 cos_cang = sin_lat * sin_latc + cos_latc * cos_lat_lon;
00381 if (cos_cang < 0.0) {
00382 map[n] = missing_val;
00383 continue;
00384 }
00385 r = radius * cos_asd / (1.0 - cos_cang * sin_asd);
00386 xr = r * cos_lat * sin (lon - lonc);
00387 yr = r * (sin_lat * cos_latc - sin_latc * cos_lat_lon);
00388 xx = xr * cos_pa - yr * sin_pa;
00389 yy = xr * sin_pa + yr * cos_pa;
00390 yy += yc;
00391 xx += xc;
00392
00393 if (plate_cols > plate_rows) yy -= 1.0 - plate_rows / plate_width;
00394 if (plate_rows > plate_cols) xx -= 1.0 - plate_cols / plate_width;
00395 interpval = array_imaginterp (img, xx, yy, interpolator);
00396
00397
00398 if (MDI_correct_distort) {
00399 mtrack_MDI_image_tip (&xx, &yy, 1, 1);
00400 mtrack_MDI_image_stretch (&xx, &yy, 1, 1);
00401 }
00402 map[n] = (isnan (interpval)) ? missing_val : interpval;
00403 }
00404 }
00405
00406 void perform_mapping_error (DRMS_Array_t *img, float *map,
00407 double *maplat, double *maplon, double *map_coslat, double *map_sinlat,
00408 int pixct, unsigned char *offsun, double latc, double lonc,
00409 double xc, double yc, double radius, double pa, double ellipse_e,
00410 double ellipse_pa, int x_invrt, int y_invrt, int interpolator,
00411 int MDI_correct_distort) {
00412
00413
00414
00415
00416
00417 static double sin_asd = 0.004660, cos_asd = 0.99998914;
00418
00419 double r, cos_cang, xr, yr;
00420 double cos_lat, sin_lat, lon, cos_lat_lon;
00421 double xx, yy;
00422 float interpval;
00423 int n;
00424
00425 double cos_pa = cos (pa);
00426 double sin_pa = sin (pa);
00427 double cos_latc = cos (latc);
00428 double sin_latc = sin (latc);
00429 int plate_cols = img->axis[0];
00430 int plate_rows = img->axis[1];
00431 double plate_width = (plate_cols > plate_rows) ? plate_cols : plate_rows;
00432
00433 xc *= 2.0 / plate_width;
00434 yc *= 2.0 / plate_width;
00435 radius *= 2.0 / plate_width;
00436
00437 for (n = 0; n < pixct; n++) {
00438
00439 if (offsun[n]) {
00440 map[n] = missing_val;
00441 continue;
00442 }
00443 sin_lat = map_sinlat[n];
00444 cos_lat = map_coslat[n];
00445 lon = maplon[n];
00446 cos_lat_lon = cos_lat * cos (lon - lonc);
00447 cos_cang = sin_lat * sin_latc + cos_latc * cos_lat_lon;
00448 if (cos_cang < 0.0) {
00449 map[n] = missing_val;
00450 continue;
00451 }
00452 r = radius * cos_asd / (1.0 - cos_cang * sin_asd);
00453 xr = r * cos_lat * sin (lon - lonc);
00454 yr = r * (sin_lat * cos_latc - sin_latc * cos_lat_lon);
00455 xx = xr * cos_pa - yr * sin_pa;
00456 yy = xr * sin_pa + yr * cos_pa;
00457 yy += yc;
00458 xx += xc;
00459
00460 if (plate_cols > plate_rows) yy -= 1.0 - plate_rows / plate_width;
00461 if (plate_rows > plate_cols) xx -= 1.0 - plate_cols / plate_width;
00462 interpval = array_imaginterp_error (img, xx, yy, interpolator);
00463
00464
00465 if (MDI_correct_distort) {
00466 mtrack_MDI_image_tip (&xx, &yy, 1, 1);
00467 mtrack_MDI_image_stretch (&xx, &yy, 1, 1);
00468 }
00469 map[n] = (isnan (interpval)) ? missing_val : interpval;
00470 }
00471 }
00472
00473 int near_grid_line (double lon, double lat, double grid, double near) {
00474
00475
00476
00477
00478 static double degrad = 180.0 / M_PI;
00479 double g2 = 0.5 * grid;
00480
00481 lon *= degrad;
00482 lat *= degrad;
00483
00484 while (lon < 0.0) lon += grid;
00485 while (lon > g2) lon -= grid;
00486 if (fabs (lon) < near) return 1;
00487 while (lat < 0.0) lat += grid;
00488 while (lat > g2) lat -= grid;
00489 if (fabs (lat) < near) return 1;
00490 return 0;
00491 }
00492
00493 int DoIt (void) {
00494 CmdParams_t *params = &cmdparams;
00495 DRMS_RecordSet_t *ids, *ods;
00496 double *maplat, *maplon, *map_coslat, *map_sinlat;
00497 double x, y, x0, y0, xstp, ystp, xrot, yrot;
00498 double lat, lon, cos_phi, sin_phi;
00499 double img_lat, img_lon;
00500 double img_xscl, img_yscl, img_xc, img_yc, img_radius, img_pa;
00501 double grid_width;
00502 double ellipse_e, ellipse_pa;
00503 int axes[2];
00504 int img, imgct, pixct, segct;
00505 int isegnum, osegnum;
00506 int found, kstat, status;
00507 int need_ephem;
00508 int x_invrt, y_invrt;
00509 int n, col, row;
00510 int MDI_correct, MDI_correct_distort;
00511 unsigned char *offsun, *ongrid;
00512 char *input, *isegname, *osegname;
00513 char source[DRMS_MAXQUERYLEN], recid[DRMS_MAXQUERYLEN];
00514 char module_ident[64], key[64], tbuf[64];
00515
00516 double raddeg = M_PI / 180.0;
00517 double degrad = 1.0 / raddeg;
00518 int scaling_override = 0;
00519 char *mapname[] = {"PlateCarree", "Cassini-Soldner", "Mercator",
00520 "LambertCylindrical", "Sanson-Flamsteed", "gnomonic", "Postel",
00521 "stereographic", "orthographic", "LambertAzimuthal"};
00522 char *interpname[] = {"Cubic Convolution", "Nearest Neighbor", "Bilinear"};
00523 char *wcscode[] = {"CAR", "CAS", "MER", "CEA", "GLS", "TAN", "ARC", "STG",
00524 "SIN", "ZEA"};
00525 missing_val = 0.0 / 0.0;
00526 float bblank = -1.0 / 0.0;
00527 float wblank = 1.0 / 0.0;
00528
00529 char *inset = strdup (params_get_str (params, "in"));
00530 char *outser = strdup (params_get_str (params, "out"));
00531 double clat = params_get_double (params, "clat") * raddeg;
00532 double clon = params_get_double (params, "clon") * raddeg;
00533 double map_scale = params_get_double (params, "scale");
00534 double map_pa = params_get_double (params, "map_pa") * raddeg;
00535 float bscale = params_get_float (params, "bscale");
00536 float bzero = params_get_float (params, "bzero");
00537 float grid_spacing = params_get_float (params, "grid");
00538 int map_cols = params_get_int (params, "cols");
00539 int map_rows = params_get_int (params, "rows");
00540 int proj = params_get_int (params, "map");
00541 int intrpopt = params_get_int (params, "interp");
00542 char *clon_key = strdup (params_get_str (params, "clon_key"));
00543 char *clat_key = strdup (params_get_str (params, "clat_key"));
00544 char *rsun_key = strdup (params_get_str (params, "rsun_key"));
00545 char *apsd_key = strdup (params_get_str (params, "apsd_key"));
00546 char *dsun_key = strdup (params_get_str (params, "dsun_key"));
00547 int center = params_isflagset (params, "c");
00548 int stonyhurst = params_isflagset (params, "s");
00549 int verbose = params_isflagset (params, "v");
00550 int overlay = (isfinite (grid_spacing));
00551 int MDI_proc = params_isflagset (params, "M");
00552
00553 TIME trec, tnow, UNIX_epoch = -220924792.000;
00554
00555
00556 double vrcenter;
00557 float xcen, ycen, rsun;
00558 int sunSize, vrcent;
00559 char *tobsstring, ttemp[64], tstr[64];
00560 int maskid;
00561 int xMask = 4096, yMask = 4096;
00562
00563
00564 snprintf (module_ident, 64, "%s v %s", module_name, version_id);
00565 if (verbose) printf ("%s: JSOC version %s\n", module_ident, jsoc_version);
00566
00567 if (map_cols < 1) map_cols = map_rows;
00568 if (map_rows < 1) map_rows = map_cols;
00569 if (map_rows < 1) {
00570 fprintf (stderr, "Error: at least one of \"cols\" or \"rows\" must be set\n");
00571 return 1;
00572 }
00573 if (isnan (map_scale) || map_scale == 0.0) {
00574 fprintf (stderr,
00575 "Error: auto scaling from image resolution not implemented;\n");
00576 fprintf (stderr, " scale parameter must be set.\n");
00577 return 1;
00578 }
00579 MDI_correct = MDI_correct_distort = MDI_proc;
00580 cos_phi = cos (map_pa);
00581 sin_phi = sin (map_pa);
00582 xstp = ystp = map_scale * raddeg;
00583 x0 = 0.5 * (1.0 - map_cols) * xstp;
00584 y0 = 0.5 * (1.0 - map_rows) * ystp;
00585 grid_width = 0.01 * grid_spacing;
00586
00587 if (!(ids = drms_open_records (drms_env, inset, &status))) {
00588 fprintf (stderr, "Error: (%s) unable to open input data set \"%s\"\n",
00589 module_ident, inset);
00590 fprintf (stderr, " status = %d\n", status);
00591 return 1;
00592 }
00593 if ((imgct = ids->n) < 1) {
00594 fprintf (stderr, "Error: (%s) no records in selected input set\n",
00595 module_ident);
00596 fprintf (stderr, " %s\n", inset);
00597 drms_close_records (ids, DRMS_FREE_RECORD);
00598 return 1;
00599 }
00600 input = strdup (inset);
00601
00602 if (!(ods = drms_create_records (drms_env, imgct, outser, DRMS_PERMANENT,
00603 &status))) {
00604 fprintf (stderr, "Error: unable to create %d records in series %s\n",
00605 imgct, outser);
00606 fprintf (stderr, " drms_create_records() returned status %d\n",
00607 status);
00608 return 1;
00609 }
00610
00611
00612
00613 axes[0] = map_cols;
00614 axes[1] = map_rows;
00615 pixct = map_cols * map_rows;
00616 maplat = (double *)malloc (pixct * sizeof (double));
00617 maplon = (double *)malloc (pixct * sizeof (double));
00618 map_coslat = (double *)malloc (pixct * sizeof (double));
00619 map_sinlat = (double *)malloc (pixct * sizeof (double));
00620 offsun = (unsigned char *)malloc (pixct * sizeof (char));
00621 if (overlay) ongrid = (unsigned char *)malloc (pixct * sizeof (char));
00622
00623
00624
00625 for (img = 0; img < imgct; img++) {
00626
00627 DRMS_Record_t *irec, *orec;
00628 DRMS_Segment_t *iseg, *oseg;
00629 DRMS_Array_t *image = NULL, *outimage = NULL, *map = NULL;
00630 float *bTotal, *bIncl, *bAzim, *disamb;
00631 int length[2], outDim[2];
00632 double xcrpix1, ycrpix1, b0, obsl0, p, S, rsunobs, imagescale, rsun, dsunobs;
00633 int xDim, yDim, iData, ix, jy, yOff;
00634 float *data;
00635
00636
00637 double dSun, rSun_ref, asd, rSun;
00638 float *mask;
00639 float cdelt;
00640 mask = (float *)malloc(xMask * yMask * sizeof(float));
00641
00642
00643 irec = ids->records[img];
00644 drms_sprint_rec_query (source, irec);
00645 iseg = drms_segment_lookup(irec, "inclination");
00646 image = drms_segment_read (iseg, DRMS_TYPE_FLOAT, &status);
00647
00648
00649 img_lon = drms_getkey_double (irec, clon_key, &status);
00650 img_lat = drms_getkey_double (irec, clat_key, &status);
00651
00652 status = synop_solar_image_info (irec, &img_xscl, &img_yscl, &img_xc, &img_yc,
00653 &img_radius, rsun_key, apsd_key, &img_pa, &ellipse_e, &ellipse_pa,
00654 &x_invrt, &y_invrt, &need_ephem, 0);
00655 if (status & NO_SEMIDIAMETER) {
00656 int keystat = 0;
00657 double dsun_obs = drms_getkey_double (irec, dsun_key, &keystat);
00658 if (keystat) {
00659 fprintf (stderr, "Error: one or more essential keywords or values missing; skipped\n");
00660 fprintf (stderr, "solar_image_info() returned %08x\n", status);
00661 continue;
00662 }
00663
00664 img_radius = asin (RSUNM / dsun_obs);
00665 img_radius *= 3600.0 * degrad;
00666 img_radius /= (img_xscl <= img_yscl) ? img_xscl : img_yscl;
00667 status &= ~NO_SEMIDIAMETER;
00668 }
00669 if (status == KEYSCOPE_VARIABLE) {
00670 fprintf (stderr, "Warning: one or more keywords expected constant are variable\n");
00671 } else if (status) {
00672 fprintf (stderr, "Error: one or more essential keywords or values missing; skipped\n");
00673 fprintf (stderr, "solar_image_info() returned %08x\n", status);
00674 continue;
00675 }
00676 if (MDI_correct) {
00677 mtrack_MDI_correct_imgctr (&img_xc, &img_yc, img_radius);
00678 mtrack_MDI_correct_pa (&img_pa);
00679 }
00680
00681 img_xc -= 0.5 * (image->axis[0] - 1);
00682 img_yc -= 0.5 * (image->axis[1] - 1);
00683
00684 img_lon *= raddeg; clon = img_lon;
00685 img_lat *= raddeg;
00686
00687 bIncl = (float *)image->data;
00688
00689 iseg = drms_segment_lookup(irec, "field");
00690 image = drms_segment_read (iseg, DRMS_TYPE_FLOAT, &status);
00691
00692
00693 bTotal = (float *)image->data;
00694
00695 iseg = drms_segment_lookup(irec, "azimuth");
00696 image = drms_segment_read (iseg, DRMS_TYPE_FLOAT, &status);
00697
00698
00699 bAzim = (float *)image->data;
00700
00701 iseg = drms_segment_lookup(irec, "disambig");
00702 image = drms_segment_read (iseg, DRMS_TYPE_FLOAT, &status);
00703
00704
00705 disamb = (float *)image->data;
00706
00707
00708
00709 for (n=0, row=0, y=y0; row < map_rows; row++, y += ystp) {
00710 for (col=0, x=x0; col < map_cols; col++, x += xstp, n++) {
00711 xrot = x * cos_phi - y * sin_phi;
00712 yrot = y * cos_phi + x * sin_phi;
00713 offsun[n] = synop_plane2sphere (xrot, yrot, clat, clon, &lat, &lon, proj);
00714 maplat[n] = lat;
00715 maplon[n] = lon;
00716 map_coslat[n] = cos (lat);
00717 map_sinlat[n] = sin (lat);
00718 if (overlay) ongrid[n] =
00719 near_grid_line (maplon[n], maplat[n], grid_spacing, grid_width);
00720 }
00721 }
00722
00723
00724
00725
00726
00727 vrcenter = drms_getkey_double(irec, "OBS_VR", &status);
00728 dSun = drms_getkey_double(irec, "DSUN_OBS", &status);
00729 rSun_ref = drms_getkey_double(irec, "RSUN_REF", &status);
00730 if (status) rSun_ref = 6.96e8;
00731 cdelt = drms_getkey_float(irec, "CDELT1", &status);
00732 asd = asin(rSun_ref/dSun);
00733 rSun = asin(rSun_ref / dSun) * RAD2ARCSEC / cdelt;
00734 xcen = drms_getkey_float(irec, "CRPIX1", &status) - 1.0;
00735 ycen = drms_getkey_float(irec, "CRPIX2", &status) - 1.0;
00736 tobsstring = drms_getkey_string(irec, "INVPHMAP", &status);
00737 trec = drms_getkey_time(irec, "T_REC", &status);
00738 strcpy(ttemp, tobsstring);
00739 maskid = obstime2maskid(trec);
00740
00741 printf("vrcenter=%f, rSun=%f, xcen=%f, ycen=%f, INVPHMAP=%s\n", vrcenter, rSun, xcen, ycen, ttemp);
00742 noisemaskimag4twindow(xMask, yMask, xcen, ycen, rSun, vrcenter, maskid, mask);
00743
00744
00745
00746
00747
00748 xcrpix1 = drms_getkey_double(irec, "CRPIX1", &status) - 1.0;
00749 ycrpix1 = drms_getkey_double(irec, "CRPIX2", &status) - 1.0;
00750 b0 = drms_getkey_double(irec, "CRLT_OBS", &status);
00751 obsl0 = drms_getkey_double(irec, "CRLN_OBS", &status);
00752 p = drms_getkey_double(irec, "CROTA2", &status);
00753 rsunobs = drms_getkey_double(irec, "RSUN_OBS", &status);
00754 imagescale = drms_getkey_double(irec, "CDELT1", &status);
00755 dsunobs = drms_getkey_double(irec, "DSUN_OBS", &status);
00756 rsun = rsunobs/imagescale;
00757 S = RSUNM / dsunobs;
00758
00759 xDim = image->axis[0];
00760 yDim = image->axis[1];
00761 double bxHel, byHel, bzHel;
00762 float *bRadial, *bTheta, *bPhi;
00763 bRadial = (float *)malloc(xDim * yDim * sizeof(float));
00764 bTheta = (float *)malloc(xDim * yDim * sizeof(float));
00765 bPhi = (float *)malloc(xDim * yDim * sizeof(float));
00766
00767 double vxx, vyy, vlon, vlat, vcoslat, vsinlat;
00768 double vmu, vrho, vsig, vchi;
00769 jy = 0; iData = 0; yOff = 0;
00770 for (jy = 0; jy < yDim; jy++)
00771 {
00772 ix = 0;
00773 vyy = (double)jy - ycrpix1;
00774 vyy /= img_radius;
00775 yOff = jy * xDim;
00776
00777 for (ix = 0; ix < xDim; ix++)
00778 {
00779 iData = yOff + ix;
00780 vxx = (double)ix - xcrpix1;
00781 vxx /= img_radius;
00782 if (isnan(bTotal[iData]))
00783 {
00784 bRadial[iData] = DRMS_MISSING_FLOAT;
00785 bTheta[iData] = DRMS_MISSING_FLOAT;
00786 bPhi[iData] = DRMS_MISSING_FLOAT;
00787 continue;
00788 }
00789
00790 synop_img2sphere (vxx, vyy, asin(S), b0 * RADSINDEG, obsl0 * RADSINDEG, p * RADSINDEG, &vrho, &vlat, &vlon,
00791 &vsinlat, &vcoslat, &vsig, &vmu, &vchi);
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 double bx = 0.0, by = 0.0, bz = 0.0;
00803
00804 if ((int)(disamb[iData]/2)%2 == 1) bAzim[iData] += 180.0;
00805
00806 bx = -bTotal[iData] * sin(bIncl[iData] * RADSINDEG)
00807 * sin(bAzim[iData] * RADSINDEG);
00808 by = bTotal[iData] * sin(bIncl[iData] * RADSINDEG)
00809 * cos(bAzim[iData] * RADSINDEG);
00810 bz = bTotal[iData] * cos(bIncl[iData] * RADSINDEG);
00811
00812
00813
00814
00815 img2helioVector (bx, by, bz, &bxHel, &byHel, &bzHel, vlon, vlat, obsl0 * RADSINDEG, b0 * RADSINDEG, p * RADSINDEG);
00816 bPhi[iData] = bxHel;
00817 bTheta[iData] = -byHel;
00818 bRadial[iData] = bzHel;
00819 }
00820 }
00821
00822
00823
00824
00825 orec = ods->records[img];
00826 map = drms_array_create (DRMS_TYPE_FLOAT, 2, axes, NULL, &status);
00827 data = (float *)map->data;
00828
00829 oseg = drms_segment_lookup(orec, "Br");
00830 length[0] = xDim; length[1] = yDim;
00831 outimage = drms_array_create(DRMS_TYPE_FLOAT, 2, length, NULL, &status);
00832
00833 outimage = drms_array_create(DRMS_TYPE_FLOAT, 2, length, bRadial, &status);
00834
00835 perform_mapping (outimage, data, maplat, maplon, map_coslat, map_sinlat,
00836 pixct, offsun, img_lat, img_lon, img_xc, img_yc, img_radius, img_pa,
00837 ellipse_e, ellipse_pa, x_invrt, y_invrt, intrpopt, MDI_correct_distort);
00838 if (overlay) {
00839 for (n = 0; n < pixct; n++)
00840 if (ongrid[n]) data[n] = (isfinite (data[n])) ? bblank : wblank;
00841 }
00842
00843 status = drms_segment_write (oseg, map, 0);
00844 if (status) {
00845 drms_sprint_rec_query (recid, orec);
00846 fprintf (stderr, "Error writing data to record %s\n", recid);
00847 fprintf (stderr, " series may not have appropriate structure\n");
00848 return 1;
00849 }
00850
00851 double statMin, statMax, statMedn, statMean, statSig, statSkew, statKurt;
00852 int statNgood, ipixels;
00853 if (fstats(axes[0] * axes[1], data, &statMin, &statMax, &statMedn, &statMean, &statSig,
00854 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00855
00856 ipixels = axes[0] * axes[1];
00857 drms_setkey_int(orec, "TOTVALS_1", ipixels);
00858 drms_setkey_int(orec, "DATAVALS_1", statNgood);
00859 ipixels = axes[0] * axes[1]-statNgood;
00860 drms_setkey_int(orec, "MISSVALS_1", ipixels);
00861 drms_setkey_double(orec, "DATAMIN_1", statMin);
00862 drms_setkey_double(orec, "DATAMAX_1", statMax);
00863 drms_setkey_double(orec, "DATAMEDN_1", statMedn);
00864 drms_setkey_double(orec, "DATAMEAN_1", statMean);
00865 drms_setkey_double(orec, "DATARMS_1", statSig);
00866 drms_setkey_double(orec, "DATASKEW_1", statSkew);
00867 drms_setkey_double(orec, "DATAKURT_1", statKurt);
00868 drms_free_array(outimage);
00869
00870 oseg = drms_segment_lookup(orec, "Bt");
00871 outimage = drms_array_create(DRMS_TYPE_FLOAT, 2, length, bTheta, &status);
00872
00873
00874 perform_mapping (outimage, data, maplat, maplon, map_coslat, map_sinlat,
00875 pixct, offsun, img_lat, img_lon, img_xc, img_yc, img_radius, img_pa,
00876 ellipse_e, ellipse_pa, x_invrt, y_invrt, intrpopt, MDI_correct_distort);
00877 if (overlay) {
00878 for (n = 0; n < pixct; n++)
00879 if (ongrid[n]) data[n] = (isfinite (data[n])) ? bblank : wblank;
00880 }
00881
00882 status = drms_segment_write (oseg, map, 0);
00883 if (status) {
00884 drms_sprint_rec_query (recid, orec);
00885 fprintf (stderr, "Error writing data to record %s\n", recid);
00886 fprintf (stderr, " series may not have appropriate structure\n");
00887 return 1;
00888 }
00889
00890 if (fstats(axes[0] * axes[1], data, &statMin, &statMax, &statMedn, &statMean, &statSig,
00891 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00892
00893 ipixels = axes[0] * axes[1];
00894 drms_setkey_int(orec, "TOTVALS_2", ipixels);
00895 drms_setkey_int(orec, "DATAVALS_2", statNgood);
00896 ipixels = axes[0] * axes[1]-statNgood;
00897 drms_setkey_int(orec, "MISSVALS_2", ipixels);
00898 drms_setkey_double(orec, "DATAMIN_2", statMin);
00899 drms_setkey_double(orec, "DATAMAX_2", statMax);
00900 drms_setkey_double(orec, "DATAMEDN_2", statMedn);
00901 drms_setkey_double(orec, "DATAMEAN_2", statMean);
00902 drms_setkey_double(orec, "DATARMS_2", statSig);
00903 drms_setkey_double(orec, "DATASKEW_2", statSkew);
00904 drms_setkey_double(orec, "DATAKURT_2", statKurt);
00905 drms_free_array(outimage);
00906
00907 oseg = drms_segment_lookup(orec, "Bp");
00908 outimage = drms_array_create(DRMS_TYPE_FLOAT, 2, length, bPhi, &status);
00909
00910 perform_mapping (outimage, data, maplat, maplon, map_coslat, map_sinlat,
00911 pixct, offsun, img_lat, img_lon, img_xc, img_yc, img_radius, img_pa,
00912 ellipse_e, ellipse_pa, x_invrt, y_invrt, intrpopt, MDI_correct_distort);
00913 if (overlay) {
00914 for (n = 0; n < pixct; n++)
00915 if (ongrid[n]) data[n] = (isfinite (data[n])) ? bblank : wblank;
00916 }
00917
00918 status = drms_segment_write (oseg, map, 0);
00919 if (status) {
00920 drms_sprint_rec_query (recid, orec);
00921 fprintf (stderr, "Error writing data to record %s\n", recid);
00922 fprintf (stderr, " series may not have appropriate structure\n");
00923 return 1;
00924 }
00925
00926 if (fstats(axes[0] * axes[1], data, &statMin, &statMax, &statMedn, &statMean, &statSig,
00927 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00928
00929 ipixels = axes[0] * axes[1];
00930 drms_setkey_int(orec, "TOTVALS_3", ipixels);
00931 drms_setkey_int(orec, "DATAVALS_3", statNgood);
00932 ipixels = axes[0] * axes[1]-statNgood;
00933 drms_setkey_int(orec, "MISSVALS_3", ipixels);
00934 drms_setkey_double(orec, "DATAMIN_3", statMin);
00935 drms_setkey_double(orec, "DATAMAX_3", statMax);
00936 drms_setkey_double(orec, "DATAMEDN_3", statMedn);
00937 drms_setkey_double(orec, "DATAMEAN_3", statMean);
00938 drms_setkey_double(orec, "DATARMS_3", statSig);
00939 drms_setkey_double(orec, "DATASKEW_3", statSkew);
00940 drms_setkey_double(orec, "DATAKURT_3", statKurt);
00941 drms_free_array(outimage);
00942
00943
00944 oseg = drms_segment_lookup(orec, "B_error");
00945 outimage = drms_array_create(DRMS_TYPE_FLOAT, 2, length, mask, &status);
00946
00947
00948 perform_mapping (outimage, data, maplat, maplon, map_coslat, map_sinlat,
00949 pixct, offsun, img_lat, img_lon, img_xc, img_yc, img_radius, img_pa,
00950 ellipse_e, ellipse_pa, x_invrt, y_invrt, intrpopt, MDI_correct_distort);
00951 if (overlay) {
00952 for (n = 0; n < pixct; n++)
00953 if (ongrid[n]) data[n] = (isfinite (data[n])) ? bblank : wblank;
00954 }
00955
00956 status = drms_segment_write (oseg, map, 0);
00957 if (status) {
00958 drms_sprint_rec_query (recid, orec);
00959 fprintf (stderr, "Error writing data to record %s\n", recid);
00960 fprintf (stderr, " series may not have appropriate structure\n");
00961 return 1;
00962 }
00963
00964 if (fstats(axes[0] * axes[1], data, &statMin, &statMax, &statMedn, &statMean, &statSig,
00965 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00966
00967 ipixels = axes[0] * axes[1];
00968 drms_setkey_int(orec, "TOTVALS_4", ipixels);
00969 drms_setkey_int(orec, "DATAVALS_4", statNgood);
00970 ipixels = axes[0] * axes[1]-statNgood;
00971 drms_setkey_int(orec, "MISSVALS_4", ipixels);
00972 drms_setkey_double(orec, "DATAMIN_4", statMin);
00973 drms_setkey_double(orec, "DATAMAX_4", statMax);
00974 drms_setkey_double(orec, "DATAMEDN_4", statMedn);
00975 drms_setkey_double(orec, "DATAMEAN_4", statMean);
00976 drms_setkey_double(orec, "DATARMS_4", statSig);
00977 drms_setkey_double(orec, "DATASKEW_4", statSkew);
00978 drms_setkey_double(orec, "DATAKURT_4", statKurt);
00979 drms_free_array(outimage);
00980
00981
00982
00983 drms_copykey(orec, irec, "T_REC");
00984 drms_copykey(orec, irec, "T_OBS");
00985 drms_copykey(orec, irec, "CADENCE");
00986 drms_copykey(orec, irec, "DATASIGN");
00987 drms_copykey(orec, irec, "OBS_VR");
00988 drms_copykey(orec, irec, "OBS_VW");
00989 drms_copykey(orec, irec, "OBS_VN");
00990 drms_copykey(orec, irec, "DATE__OBS");
00991 drms_copykey(orec, irec, "QUALITY");
00992 drms_copykey(orec, irec, "CAMERA");
00993 drms_copykey(orec, irec, "CRLN_OBS");
00994 drms_copykey(orec, irec, "CRLT_OBS");
00995 drms_copykey(orec, irec, "CAR_ROT");
00996 drms_copykey(orec, irec, "DSUN_OBS");
00997
00998 kstat = 0;
00999 kstat += check_and_set_key_str (orec, "WCSNAME", "Carrington Heliographic");
01000 kstat += check_and_set_key_int (orec, "WCSAXES", 2);
01001 snprintf (key, 64, "CRLN-%s", wcscode[proj]);
01002 kstat += check_and_set_key_str (orec, "CTYPE1", key);
01003 snprintf (key, 64, "CRLT-%s", wcscode[proj]);
01004 kstat += check_and_set_key_str (orec, "CTYPE2", key);
01005 kstat += check_and_set_key_str (orec, "CUNIT1", "deg");
01006 kstat += check_and_set_key_str (orec, "CUNIT2", "deg");
01007 kstat += check_and_set_key_float (orec, "CRPIX1", 0.5 * map_cols + 0.5);
01008 kstat += check_and_set_key_float (orec, "CRPIX2", 0.5 * map_rows + 0.5);
01009 kstat += check_and_set_key_float (orec, "CRVAL1", clon * degrad);
01010 kstat += check_and_set_key_float (orec, "CRVAL2", clat * degrad);
01011 kstat += check_and_set_key_float (orec, "CDELT1", map_scale);
01012 kstat += check_and_set_key_float (orec, "CDELT2", map_scale);
01013 if (map_pa != 0.0) {
01014 kstat += check_and_set_key_float (orec, "PC1_1", cos (map_pa));
01015
01016 kstat += check_and_set_key_float (orec, "PC1_2", sin (map_pa));
01017
01018 kstat += check_and_set_key_float (orec, "PC2_1", sin (map_pa));
01019 kstat += check_and_set_key_float (orec, "PC2_2", cos (map_pa));
01020 }
01021
01022
01023
01024 kstat += check_and_set_key_str (orec, "MapProj", mapname[proj]);
01025 kstat += check_and_set_key_float (orec, "MapScale", map_scale);
01026 kstat += check_and_set_key_float (orec, "Width", map_cols * map_scale);
01027 kstat += check_and_set_key_float (orec, "Height", map_rows * map_scale);
01028 kstat += check_and_set_key_float (orec, "Size", sqrt (map_rows * map_cols) * map_scale);
01029 kstat += check_and_set_key_float (orec, "Map_PA", map_pa / raddeg);
01030 kstat += check_and_set_key_float (orec, "RSunRef", 1.0e-6 * RSUNM);
01031 kstat += check_and_set_key_str (orec, "Interp", interpname[intrpopt]);
01032 kstat += check_and_set_key_str (orec, "Module", module_ident);
01033 kstat += check_and_set_key_str (orec, "BLD_VERS", jsoc_version);
01034
01035 kstat += check_and_set_key_str (orec, "Source", source);
01036 kstat += check_and_set_key_str (orec, "Input", input);
01037
01038 if (kstat) {
01039 fprintf (stderr, "Error writing key value(s) to record %d in series %s\n",
01040 img, outser);
01041 fprintf (stderr, " series may not have appropriate structure\n");
01042 drms_close_records (ods, DRMS_FREE_RECORD);
01043 return 1;
01044 }
01045 tnow = (double)time(NULL);
01046 tnow += UNIX_epoch;
01047 drms_setkey_time(orec, "DATE", tnow);
01048
01049 free(bIncl); free(bTotal); free(bAzim);
01050 drms_free_array(image);
01051 drms_free_array (map);
01052 }
01053 drms_close_records(ids, DRMS_FREE_RECORD);
01054 drms_close_records (ods, DRMS_INSERT_RECORD);
01055 return 0;
01056 }
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078