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 #include <jsoc_main.h>
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 #include <math.h>
00053 #include <sys/time.h>
00054 #include <sys/resource.h>
00055
00056 #define DTOR (M_PI / 180.)
00057
00058 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
00059 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00060 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00061
00062
00063 #include "timing.c"
00064
00065
00066 #include "fitimage.c"
00067
00068
00069 #include "fstats.c"
00070
00071
00072
00073
00074 #define PROJNM STEREOGRAPHIC
00075 #define INTPNM BILINEAR
00076 #define IMGDIM 600
00077
00078
00079 struct fil_opt {
00080 double lat0;
00081 double latfil;
00082 int nflag;
00083 int sflag;
00084 };
00085
00086
00087 enum methodnums {
00088 SPATIAL_2D
00089 };
00090 char *methodlist[] = {"SPATIAL_2D"};
00091
00092
00093 enum intp {
00094 BILINEAR
00095 };
00096 char *intplist[] = {"BILINEAR"};
00097
00098
00099 enum proj {
00100 STEREOGRAPHIC
00101 };
00102 char *projlist[] = {"STEREOGRAPHIC"};
00103
00104
00105
00106 struct fil_opt option;
00107 enum methodnums methodnum;
00108 enum intp intpname;
00109 enum proj projname;
00110 int verbflag;
00111
00112
00113
00114 void spatial_2d(float *synop, double *lons, double *lats,
00115 int nlons, int nlats, int sinlat, int order, int imgDims[]);
00116
00117
00118 double get_index(int sinlat, int n, double x[], double x0);
00119 double linintd(double *f, int nx, int ny, double x, double y);
00120
00121 void stereo_fwd(int south, double lon, double lat, double *x, double *y,
00122 double xc, double yc, double rx, double ry, int nx, int ny);
00123 void stereo_bwd(int south, double *lon, double *lat, double x, double y,
00124 double xc, double yc, double rx, double ry, int nx, int ny);
00125
00126 void polar_proj_fwd(int south, int sinlat, double *image, int nx, int ny,
00127 double *map, int nlons, int nlats, double lons[], double lats[], double lat_lim);
00128 void polar_proj_bwd(int south, int sinlat, double *image, int nx, int ny,
00129 double *map, int nlons, int nlats, double lons[], double lats[], double lat_lim);
00130
00131
00132
00133
00134
00135
00136
00137 char *module_name = "jpolfil";
00138
00139 ModuleArgs_t module_args[] =
00140 {
00141 {ARG_STRING, "in", NULL, "Input data series."},
00142 {ARG_STRING, "out", NULL, "Output data series."},
00143 {ARG_STRING, "METHOD", "SPATIAL_2D", "Filling method."},
00144 {ARG_STRING, "INTERP", "BILINEAR", "Interpolation method."},
00145 {ARG_STRING, "PROJECT", "STEREOGRAPHIC", "Map projection method."},
00146 {ARG_DOUBLE, "LAT0", "65.", "Starting latitude used for fitting."},
00147 {ARG_DOUBLE, "LATFIL", "75.", "Starting latitude for filling in."},
00148 {ARG_INT, "NFLAG", "1", "Filling for north pole: 1 for yes, 0 for no."},
00149 {ARG_INT, "SFLAG", "1", "Filling for south pole: 1 for yes, 0 for no."},
00150 {ARG_INT, "VERB", "1", "Level of verbosity: 0=errors/warnings; 1=all messages"},
00151 {ARG_INT, "ORDER", "7", "Highest power of polynomials."},
00152 {ARG_END}
00153 };
00154
00155
00156 char *synop_keys[] = {"TELESCOP", "INSTRUME", "WAVELNTH", "BUNIT", "CONTENT", "HISTORY", "COMMENT",
00157 "CTYPE1", "CTYPE2", "CRPIX1", "CRPIX2", "CRVAL1", "CRVAL2", "CDELT1", "CDELT2",
00158 "CUNIT1", "CUNIT2", "CRDER1", "CRDER2", "CSYSER1", "CSYSER2", "WCSNAME",
00159 "T_REC", "T_REC_epoch", "T_REC_step", "T_REC_unit", "CADENCE", "DATASIGN", "T_OBS", "T_START", "T_STOP", "T_ROT", "T_EARTH",
00160 "CARRTIME", "B0_ROT", "B0_FRST", "B0_LAST", "EARTH_B0", "LON_FRST", "LON_LAST", "LON_STEP", "W_OFFSET", "W_WEIGHT",
00161 "MAG_NUM", "MAG_FRST", "MAG_LAST", "MAG_ROT", "HWNWIDTH", "EQPOINTS", "NSIGMA", "CARSTRCH", "DIFROT_A", "DIFROT_B", "DIFROT_C",
00162 "DSUN_OBS", "CRLN_OBS", "CRLT_OBS", "OBS_VR", "OBS_VN", "OBS_VW", "QUALITY", "MAPMMAX", "MAPLGMAX", "MAPLGMIN", "SINBDIVS",
00163 "MAPBMAX", "MAPRMAX", "LGSHIFT", "INTERPO", "MCORLEV", "MOFFSET", "FRTIMWDN", "FRAVEPNT", "FRWINDOW", "SYNDRORA",
00164 "SYNDRO_A", "SYNDRO_B", "SYNDRO_C", "OSYNDR_A", "OSYNDR_B", "OSYNDR_C"};
00165
00166 int DoIt(void)
00167 {
00168 int status = DRMS_SUCCESS;
00169 char *inQuery, *outQuery;
00170 DRMS_RecordSet_t *inRS, *outRS;
00171 int irec, nrecs;
00172 DRMS_Record_t *inRec, *outRec;
00173 DRMS_Segment_t *inSeg, *outSeg;
00174 DRMS_Array_t *inArray, *outArray;
00175 DRMS_Link_t *srcLink;
00176 float *inData, *outData;
00177
00178 char *inchecklist[] = {"CAR_ROT", "CTYPE2"};
00179 char *outchecklist[] = {"CAR_ROT", "METHOD", "LAT0", "LATFIL", "NFLAG", "SFLAG"};
00180 DRMS_Record_t *inRec_tmp, *outRec_tmp;
00181 DRMS_Keyword_t *inkeytest, *outkeytest;
00182
00183 int outDims[2];
00184 char *method, *interp, *project;
00185 int methodflag = 0, intpflag = 0, projflag = 0;
00186 int imgDims[3];
00187
00188 char *ctype2;
00189 int sinlat, order;
00190 double lon0;
00191
00192 double *lons, *lats;
00193
00194 int i, j, itest;
00195 int n0, n1, mapsz;
00196 int img_n, img_s, imgsz;
00197
00198
00199 double wt0, wt1, wt;
00200 double ut0, ut1, ut;
00201 double st0, st1, st;
00202 double ct0, ct1, ct;
00203 wt0 = getwalltime();
00204 ct0 = getcputime(&ut0, &st0);
00205
00206
00207 double dat_min, dat_max, dat_medn, dat_mean, dat_sig, dat_skew, dat_kurt;
00208 int dat_ngood;
00209
00210
00211 inQuery = (char *)params_get_str(&cmdparams, "in");
00212 outQuery = (char *)params_get_str(&cmdparams, "out");
00213 method = (char *)params_get_str(&cmdparams, "METHOD");
00214 for (itest = 0; itest < ARRLENGTH(methodlist) && !methodflag; itest++) {
00215 if (!strcmp(method, methodlist[itest])) {
00216 methodflag = 1;
00217 methodnum = (enum methodnums)(itest);
00218 }
00219 }
00220 if (!methodflag) DIE("Unknow method");
00221
00222 interp = (char *)params_get_str(&cmdparams, "INTERP");
00223 for (itest = 0; itest < ARRLENGTH(intplist) && !intpflag; itest++) {
00224 if (!strcmp(interp, intplist[itest])) {
00225 intpflag = 1;
00226 intpname = (enum intp)(itest);
00227 }
00228 }
00229 if (!intpflag) DIE("Unknow interpolation method");
00230
00231 project = (char *)params_get_str(&cmdparams, "PROJECT");
00232 for (itest = 0; itest < ARRLENGTH(projlist) && !projflag; itest++) {
00233 if (!strcmp(project, projlist[itest])) {
00234 projflag = 1;
00235 projname = (enum proj)(itest);
00236 }
00237 }
00238 if (!projflag) DIE("Unknow projection method");
00239
00240
00241 option.lat0 = params_get_double(&cmdparams, "LAT0");
00242 option.latfil = params_get_double(&cmdparams, "LATFIL");
00243 option.nflag = params_get_int(&cmdparams, "NFLAG");
00244 option.sflag = params_get_int(&cmdparams, "SFLAG");
00245 verbflag = params_get_int(&cmdparams, "VERB");
00246 order = params_get_int(&cmdparams, "ORDER");
00247
00248
00249 inRS = drms_open_records(drms_env, inQuery, &status);
00250 if (status || inRS->n == 0) DIE("No input data found");
00251 nrecs = inRS->n;
00252
00253
00254 inRec_tmp = inRS->records[0];
00255 for (itest = 0; itest < ARRLENGTH(inchecklist); itest++) {
00256 inkeytest = hcon_lookup_lower(&inRec_tmp->keywords, inchecklist[itest]);
00257 if (!inkeytest) {
00258 fprintf(stderr, "ERROR: required input keyword %s is missing\n", inchecklist[itest]);
00259 return 0;
00260 }
00261 }
00262
00263
00264 outRec_tmp = drms_create_record(drms_env, outQuery, DRMS_TRANSIENT, &status);
00265 if (status) DIE("Unable to open record in output series");
00266 for (itest = 0; itest < ARRLENGTH(outchecklist); itest++) {
00267 outkeytest = hcon_lookup_lower(&outRec_tmp->keywords, outchecklist[itest]);
00268 if (!outkeytest || outkeytest->info->islink || outkeytest->info->recscope == 1) {
00269 fprintf(stderr, "Output keyword %s is missing/constant/a link.\n", outchecklist[itest]);
00270 return 0;
00271 }
00272 }
00273 drms_close_record(outRec_tmp, DRMS_FREE_RECORD);
00274
00275
00276 outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00277 if (status) DIE("Output recordset not created");
00278
00279
00280 for (irec = 0; irec < nrecs; irec++)
00281 {
00282 if (verbflag) {
00283 wt1 = getwalltime();
00284 ct1 = getcputime(&ut1, &st1);
00285 printf("processing record %d...\n", irec);
00286 }
00287
00288
00289 inRec = inRS->records[irec];
00290 inSeg = drms_segment_lookupnum(inRec, 0);
00291 inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00292 if (status) {
00293 printf("No data file found. \n"); fflush(stdout);
00294 drms_free_array(inArray);
00295 continue;
00296 }
00297 inData = (float *)inArray->data;
00298
00299
00300 n0 = inArray->axis[0]; n1 = inArray->axis[1];
00301 mapsz = n0 * n1;
00302 ctype2 = drms_getkey_string(inRec, "CTYPE2", &status);
00303 sinlat = (strcmp(ctype2, "Sine Latitude") == 0 || strcmp(ctype2, "CRLT-CEA") == 0) ? 1 : 0;
00304
00305
00306 outDims[0] = n0; outDims[1] = n1;
00307 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, NULL, &status);
00308 outData = (float *)outArray->data;
00309 for (i = 0; i < mapsz; i++) {
00310 outData[i] = inData[i];
00311 }
00312 drms_free_array(inArray);
00313
00314
00315 imgDims[0] = IMGDIM; imgDims[1] = IMGDIM; imgDims[2] = 2;
00316
00317
00318 lons = (double *)malloc(n0 * sizeof(double));
00319 lats = (double *)malloc(n1 * sizeof(double));
00320 for (i = 0; i < n0; i++) lons[i] = (i - n0 / 2.0) / n0 * 360. * DTOR;
00321 for (j = 0; j < n1; j++)
00322 lats[j] = (j + 0.5 - n1 / 2.0) / (n1 / 2.0);
00323 if (sinlat) {
00324 for (j = 0; j < n1; j++) lats[j] = asin(lats[j]);
00325 } else {
00326 for (j = 0; j < n1; j++) lats[j] = lats[j] * M_PI;
00327 }
00328
00329
00330 switch (methodnum) {
00331 case ((enum methodnums)(0)):
00332 spatial_2d(outData, lons, lats, n0, n1, sinlat, order, imgDims);
00333 break;
00334 default:
00335 spatial_2d(outData, lons, lats, n0, n1, sinlat, order, imgDims);
00336 break;
00337 }
00338
00339
00340 outRec = outRS->records[irec];
00341 outSeg = drms_segment_lookupnum(outRec, 0);
00342 outSeg->axis[0] = outArray->axis[0];
00343 outSeg->axis[1] = outArray->axis[1];
00344 outArray->parent_segment = outSeg;
00345
00346
00347 srcLink = hcon_lookup_lower(&outRec->links, "SYNOPMR_ORIG");
00348 if (srcLink) {
00349 drms_setlink_static(outRec, "SYNOPMR_ORIG", inRec->recnum);
00350 }
00351
00352
00353
00354 drms_copykey(outRec, inRec, "CAR_ROT");
00355
00356 drms_setkey_string(outRec, "METHOD", method);
00357 drms_setkey_double(outRec, "LAT0", option.lat0);
00358 drms_setkey_double(outRec, "LATFIL", option.latfil);
00359 drms_setkey_int(outRec, "NFLAG", option.nflag);
00360 drms_setkey_int(outRec, "SFLAG", option.sflag);
00361
00362
00363 status = fstats(mapsz, outData, &dat_min, &dat_max, &dat_medn, &dat_mean,
00364 &dat_sig, &dat_skew, &dat_kurt, &dat_ngood);
00365
00366
00367 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
00368 drms_setkey_time(outRec, "DATE", CURRENT_SYSTEM_TIME);
00369 drms_setkey_int(outRec, "TOTVALS", mapsz);
00370 if (!status) {
00371 drms_setkey_int(outRec, "DATAVALS", dat_ngood);
00372 drms_setkey_int(outRec, "MISSVALS", mapsz - dat_ngood);
00373 drms_setkey_double(outRec, "DATAMIN", dat_min);
00374 drms_setkey_double(outRec, "DATAMAX", dat_max);
00375 drms_setkey_double(outRec, "DATAMEDN", dat_medn);
00376 drms_setkey_double(outRec, "DATAMEAN", dat_mean);
00377 drms_setkey_double(outRec, "DATARMS", dat_sig);
00378 drms_setkey_double(outRec, "DATASKEW", dat_skew);
00379 drms_setkey_double(outRec, "DATAKURT", dat_kurt);
00380 }
00381 drms_copykey(outRec, inRec, "T_REC");
00382
00383
00384 status = drms_segment_write(outSeg, outArray, 0);
00385 if (status) DIE("problem writing data");
00386 drms_free_array(outArray);
00387 free(lons); free(lats);
00388
00389
00390 for (itest = 0; itest < ARRLENGTH(synop_keys); itest++) {
00391 drms_copykey(outRec, inRec, synop_keys[itest]);
00392 }
00393
00394
00395 if (verbflag) {
00396 wt = getwalltime();
00397 ct = getcputime(&ut, &st);
00398 printf("record %d done, %.2f ms wall time, %.2f ms cpu time\n",
00399 irec, wt - wt1, ct - ct1);
00400 }
00401 }
00402
00403 drms_close_records(inRS, DRMS_FREE_RECORD);
00404 drms_close_records(outRS, DRMS_INSERT_RECORD);
00405
00406 if (verbflag) {
00407 wt = getwalltime();
00408 ct = getcputime(&ut, &st);
00409 printf("total time spent: %.2f ms wall time, %.2f ms cpu time\n",
00410 wt - wt0, ct - ct0);
00411 }
00412
00413 return(DRMS_SUCCESS);
00414
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 void spatial_2d(synop, lons, lats, nlons, nlats, sinlat, order, imgDims)
00429 float *synop;
00430 double *lons, *lats;
00431 int nlons, nlats, sinlat, order;
00432 int imgDims[];
00433 {
00434 int i, j;
00435 int dj;
00436 int mapsz = nlons * nlats;
00437 int imgsz = imgDims[0] * imgDims[1];
00438 double *lats_n, *lats_s;
00439 double *map;
00440 double *image_n, *image_s;
00441
00442
00443 lats_n = (double *)malloc(nlats * sizeof(double));
00444 lats_s = (double *)malloc(nlats * sizeof(double));
00445 for (j = 0; j < nlats; j++) {
00446 lats_n[j] = (j + 1. - nlats / 2.0) / (nlats / 2.0);
00447 lats_s[j] = (j - nlats / 2.0) / (nlats / 2.0);
00448 }
00449 if (sinlat) {
00450 for (j = 0; j < nlats; j++) {
00451 lats_n[j] = asin(lats_n[j]); lats_s[j] = asin(lats_s[j]);
00452 }
00453 } else {
00454 for (j = 0; j < nlats; j++) {
00455 lats_n[j] = lats_n[j] * M_PI; lats_s[j] = lats_s[j] * M_PI;
00456 }
00457 }
00458
00459
00460 map = (double *)malloc(mapsz * sizeof(double));
00461 for (i = 0; i < mapsz; i++) map[i] = synop[i];
00462 image_n = (double *)malloc(imgsz * sizeof(double));
00463 image_s = (double *)malloc(imgsz * sizeof(double));
00464
00465
00466 if (verbflag) SHOW("remapping... ");
00467 polar_proj_fwd(0, sinlat, image_n, imgDims[0], imgDims[1],
00468 map, nlons, nlats, lons, lats_n, (option.lat0) * DTOR);
00469 polar_proj_fwd(1, sinlat, image_s, imgDims[0], imgDims[1],
00470 map, nlons, nlats, lons, lats_n, (option.lat0) * DTOR);
00471 if (verbflag) SHOW("done.\n");
00472
00473
00474 if (verbflag) SHOW("fitting... ");
00475 fitimage(image_n, imgDims[0], imgDims[1], order, 1);
00476 fitimage(image_s, imgDims[0], imgDims[1], order, 1);
00477 if (verbflag) SHOW("done.\n")
00478
00479
00480 if (verbflag) SHOW("mapping back... ");
00481 polar_proj_bwd(0, sinlat, image_n, imgDims[0], imgDims[1],
00482 map, nlons, nlats, lons, lats_n, (option.lat0) * DTOR);
00483 polar_proj_bwd(1, sinlat, image_s, imgDims[0], imgDims[1],
00484 map, nlons, nlats, lons, lats_n, (option.lat0) * DTOR);
00485 if (verbflag) SHOW("done.\n");
00486
00487
00488 if (sinlat) {
00489 dj = floor((1. - sin(fabs(option.latfil))) / (2. / nlats));
00490 } else {
00491 dj = floor((90. - fabs(option.latfil)) / (360. / nlats));
00492 }
00493 if (option.nflag) {
00494 for (j = nlats - dj; j < nlats; j++)
00495 for (i = 0; i < nlons; i++)
00496 synop[j * nlons + i] = map[j * nlons + i];
00497 }
00498 if (option.sflag) {
00499 for (j = 0; j < dj; j++)
00500 for (i = 0; i < nlons; i++)
00501 synop[j * nlons + i] = map[j * nlons + i];
00502 }
00503
00504
00505 free(lats_n); free(lats_s);
00506 free(map); free(image_n); free(image_s);
00507 }
00508
00509
00510
00511
00512
00513
00514
00515 double get_index(sinlat, n, x, x0)
00516 int sinlat, n;
00517 double x[], x0;
00518 {
00519 int i, i0, i1;
00520 double ind;
00521
00522 if (x0 <= x[0]) {
00523 i0 = 0; i1 = 1;
00524 } else if (x0 > x[n - 1]) {
00525 i0 = n - 2; i1 = n - 1;
00526 } else {
00527 for (int i = 0; i < n - 1; i++) {
00528 if (x0 > x[i] && x0 <= x[i + 1]) {
00529 i0 = i; i1 = i + 1;
00530 break;
00531 }
00532 }
00533 }
00534
00535 if (sinlat) {
00536 ind = i0 + (sin(x0) - sin(x[i0])) / (sin(x[i1]) - sin(x[i0]));
00537 } else {
00538 ind = i0 + (x0 - x[i0]) / (x[i1] - x[i0]);
00539 }
00540 return ind;
00541 }
00542
00543
00544
00545
00546
00547 double linintd(f, nx, ny, x, y)
00548 double *f;
00549 int nx, ny;
00550 double x, y;
00551 {
00552 double p0, p1, q0, q1;
00553 int ilow, jlow;
00554 double *fptr;
00555 double u;
00556
00557 ilow = (int)floor(x);
00558 jlow = (int)floor(y);
00559 if (ilow < 0) ilow = 0;
00560 if (ilow + 1 >= nx) ilow -= 1;
00561 if (jlow < 0) jlow = 0;
00562 if (jlow + 1 >= ny) jlow -= 1;
00563 p1 = x - ilow;
00564 p0 = 1.0 - p1;
00565 q1 = y - jlow;
00566 q0 = 1.0 - q1;
00567
00568
00569 u = 0.0;
00570 fptr = f + jlow * nx + ilow;
00571 u += p0 * q0 * (*fptr++);
00572 u += p1 * q0 * (*fptr);
00573 fptr += nx - 1;
00574 u += p0 * q1 * (*fptr++);
00575 u += p1 * q1 * (*fptr);
00576 return u;
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 void stereo_fwd(south, lon, lat,
00596 x, y, xc, yc, rx, ry, nx, ny)
00597 int south;
00598 double lon, lat;
00599 double *x, *y, xc, yc, rx, ry;
00600 int nx, ny;
00601 {
00602 double x0, y0;
00603 double r, th;
00604
00605 r = cos(lat) / (1 + sin(fabs(lat)));
00606 th = lon;
00607 x0 = r * cos(th);
00608 y0 = r * sin(th);
00609 *x = x0 * rx + xc;
00610 *y = y0 * ry + yc;
00611 }
00612
00613
00614
00615 void stereo_bwd(south, lon, lat,
00616 x, y, xc, yc, rx, ry, nx, ny)
00617 int south;
00618 double *lon, *lat;
00619 double x, y, xc, yc, rx, ry;
00620 int nx, ny;
00621 {
00622 double x0, y0;
00623 double r, th;
00624
00625 x0 = (x - xc) / rx;
00626 y0 = (y - yc) / ry;
00627 r = sqrt(x0 * x0 + y0 * y0);
00628 th = atan2(y0, x0);
00629 *lon = th;
00630 *lat = M_PI / 2.0 - atan(r) * 2.0;
00631 if (south) *lat = -(*lat);
00632 }
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 void polar_proj_fwd(south, sinlat, image, nx, ny,
00650 map, nlons, nlats, lons, lats, lat_lim)
00651 int south, sinlat;
00652 double *image;
00653 int nx, ny;
00654 double *map;
00655 int nlons, nlats;
00656 double lons[], lats[], lat_lim;
00657 {
00658 int i, j;
00659 double lon, lat, ind_lon, ind_lat;
00660 double x, y, xc, yc, r_lim, rx, ry;
00661 void (*proj_bwd)();
00662 double (*interpolate)();
00663
00664 switch (projname) {
00665 case ((enum proj)(0)):
00666 proj_bwd = stereo_bwd;
00667 break;
00668 default:
00669 proj_bwd = stereo_bwd;
00670 break;
00671 }
00672 switch (intpname) {
00673 case ((enum intp)(0)):
00674 interpolate = linintd;
00675 break;
00676 default:
00677 interpolate = linintd;
00678 break;
00679 }
00680
00681
00682 xc = (nx - 1) / 2.0;
00683 yc = (ny - 1) / 2.0;
00684
00685 r_lim = cos(lat_lim) / (1 + sin(fabs(lat_lim)));
00686 rx = xc / r_lim;
00687 ry = yc / r_lim;
00688
00689 for (j = 0; j < ny; j++) {
00690 for (i = 0; i < nx; i++) {
00691
00692 (*proj_bwd)(south, &lon, &lat,
00693 (double)i, (double)j, xc, yc, rx, ry, nx, ny);
00694 if (fabs(lat) < fabs(lat_lim)) {
00695 image[j * nx + i] = NAN;
00696 continue;
00697 }
00698
00699 ind_lon = get_index(0, nlons, lons, lon);
00700 ind_lat = get_index(sinlat, nlats, lats, lat);
00701
00702 image[j * nx + i] =
00703 (*interpolate)(map, nlons, nlats, ind_lon, ind_lat);
00704 }
00705 }
00706 }
00707
00708
00709
00710
00711
00712
00713 void polar_proj_bwd(south, sinlat, image, nx, ny,
00714 map, nlons, nlats, lons, lats, lat_lim)
00715 int south, sinlat;
00716 double *image;
00717 int nx, ny;
00718 double *map;
00719 int nlons, nlats;
00720 double lons[], lats[], lat_lim;
00721 {
00722 int i, j, j0, j1;
00723 double x, y, xc, yc, r_lim, rx, ry;
00724 void (*proj_fwd)();
00725 double (*interpolate)();
00726
00727 switch (projname) {
00728 case ((enum proj)(0)):
00729 proj_fwd = stereo_fwd;
00730 break;
00731 default:
00732 proj_fwd = stereo_fwd;
00733 break;
00734 }
00735 switch (intpname) {
00736 case ((enum intp)(0)):
00737 interpolate = linintd;
00738 break;
00739 default:
00740 interpolate = linintd;
00741 break;
00742 }
00743
00744
00745 xc = (nx - 1) / 2.0;
00746 yc = (ny - 1) / 2.0;
00747
00748 r_lim = cos(lat_lim) / (1 + sin(fabs(lat_lim)));
00749 rx = xc / r_lim;
00750 ry = yc / r_lim;
00751
00752 if (south) {
00753 j0 = 0;
00754 j1 = ceil(get_index(sinlat, nlats, lats, -fabs(lat_lim)));
00755 } else {
00756 j0 = floor(get_index(sinlat, nlats, lats, fabs(lat_lim)));
00757 j1 = nlats - 1;
00758 }
00759
00760 for (j = j0; j <= j1; j++) {
00761 for (i = 0; i < nlons; i++) {
00762
00763 (*proj_fwd)(south, lons[i], lats[j],
00764 &x, &y, xc, yc, rx, ry, nx, ny);
00765
00766 if (x < 0 || x >= nx || y < 0 || y >= ny) {
00767 continue;
00768 } else {
00769 map[j * nlons + i] =
00770 (*interpolate)(image, nx, ny, x, y);
00771 }
00772 }
00773 }
00774 }
00775