/ncl_ncarg-6.0.0/ni/src/lib/nfp/shgridW.c

# · C · 697 lines · 525 code · 49 blank · 123 comment · 92 complexity · ca723bfe90273c6c3573c422e3b4f917 MD5 · raw file

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "wrapper.h"
  4. char shmsg[61];
  5. NhlErrorTypes shgrid_W(void)
  6. {
  7. int ier = 0;
  8. float *xi;
  9. ng_size_t dsizes_xi[NCL_MAX_DIMENSIONS];
  10. float *yi;
  11. ng_size_t dsizes_yi[NCL_MAX_DIMENSIONS];
  12. float *zi;
  13. ng_size_t dsizes_zi[NCL_MAX_DIMENSIONS];
  14. float *fval;
  15. ng_size_t dsizes_fval[NCL_MAX_DIMENSIONS];
  16. float *xo;
  17. ng_size_t dsizes_xo[NCL_MAX_DIMENSIONS];
  18. float *yo;
  19. ng_size_t dsizes_yo[NCL_MAX_DIMENSIONS];
  20. float *zo;
  21. ng_size_t dsizes_zo[NCL_MAX_DIMENSIONS];
  22. int has_missing_xi, has_missing_yi, has_missing_zi, has_missing_fval;
  23. int has_missing_xo, has_missing_yo, has_missing_zo;
  24. NclScalar missing_xi, missing_yi, missing_zi, missing_fval;
  25. NclScalar missing_xo, missing_yo, missing_zo;
  26. float *uo;
  27. int nxi, nxo, nyo, nzo, ndims_uo;
  28. ng_size_t i, j, dsizes_uo[3], num_points, num_missing;
  29. /*
  30. * Retrieve argument #0 (x coordinates).
  31. */
  32. xi = (float *) NclGetArgValue(
  33. 0,
  34. 7,
  35. NULL,
  36. dsizes_xi,
  37. &missing_xi,
  38. &has_missing_xi,
  39. NULL,
  40. DONT_CARE);
  41. /*
  42. * Retrieve argument #1 (y coordinates).
  43. */
  44. yi = (float *) NclGetArgValue(
  45. 1,
  46. 7,
  47. NULL,
  48. dsizes_yi,
  49. &missing_yi,
  50. &has_missing_yi,
  51. NULL,
  52. DONT_CARE);
  53. /*
  54. * Retrieve argument #2 (z coordinates).
  55. */
  56. zi = (float *) NclGetArgValue(
  57. 2,
  58. 7,
  59. NULL,
  60. dsizes_zi,
  61. &missing_zi,
  62. &has_missing_zi,
  63. NULL,
  64. DONT_CARE);
  65. /*
  66. * Retrieve argument #3 (input functional values)
  67. */
  68. fval = (float *) NclGetArgValue(
  69. 3,
  70. 7,
  71. NULL,
  72. dsizes_fval,
  73. &missing_fval,
  74. &has_missing_fval,
  75. NULL,
  76. DONT_CARE);
  77. /*
  78. * Retrieve argument #4 (output x coordinates)
  79. */
  80. xo = (float *) NclGetArgValue(
  81. 4,
  82. 7,
  83. NULL,
  84. dsizes_xo,
  85. &missing_xo,
  86. &has_missing_xo,
  87. NULL,
  88. DONT_CARE);
  89. /*
  90. * Retrieve argument #5 (output y coordinates)
  91. */
  92. yo = (float *) NclGetArgValue(
  93. 5,
  94. 7,
  95. NULL,
  96. dsizes_yo,
  97. &missing_yo,
  98. &has_missing_yo,
  99. NULL,
  100. DONT_CARE);
  101. /*
  102. * Retrieve argument #6 (output z coordinates)
  103. */
  104. zo = (float *) NclGetArgValue(
  105. 6,
  106. 7,
  107. NULL,
  108. dsizes_zo,
  109. &missing_zo,
  110. &has_missing_zo,
  111. NULL,
  112. DONT_CARE);
  113. /*
  114. * Check that all of the first four arguments are of the same size.
  115. */
  116. if( (dsizes_xi[0] != dsizes_yi[0]) ||
  117. (dsizes_yi[0] != dsizes_zi[0]) ||
  118. (dsizes_zi[0] != dsizes_fval[0]) ) {
  119. NhlPError(NhlFATAL,NhlEUNKNOWN,
  120. "shgrid: all arrays for the input functional values"
  121. " must be of the same size");
  122. return(NhlFATAL);
  123. }
  124. else {
  125. num_points = dsizes_xi[0];
  126. num_missing = 0;
  127. }
  128. /*
  129. * Test the dimension sizes.
  130. */
  131. if( (dsizes_xi[0] > INT_MAX) || (dsizes_xo[0] > INT_MAX) ||
  132. (dsizes_yo[0] > INT_MAX) || (dsizes_zo[0] > INT_MAX)) {
  133. NhlPError(NhlFATAL,NhlEUNKNOWN,"shgrid: the length of one or more input arrays is greater than INT_MAX");
  134. return(NhlFATAL);
  135. }
  136. nxi = (int) num_points;
  137. nxo = (int) dsizes_xo[0];
  138. nyo = (int) dsizes_yo[0];
  139. nzo = (int) dsizes_zo[0];
  140. /*
  141. * Check for missing values. Each argument xi, yi, zi, and fval must
  142. * be checked separately, since it may be that _FillValue may be
  143. * set for just one or two of the arguments.
  144. *
  145. * If a missing value is found, ignore that input point by collapsing
  146. * all of the input arrays.
  147. */
  148. if (has_missing_xi) {
  149. i = 0;
  150. lab0:
  151. if ((xi[i] == missing_xi.floatval) && (i < num_points)) {
  152. for (j = i; j < num_points - 1; j++) {
  153. xi[j] = xi[j+1];
  154. yi[j] = yi[j+1];
  155. zi[j] = zi[j+1];
  156. fval[j] = fval[j+1];
  157. }
  158. num_points--;
  159. if (num_points < 3) {
  160. NhlPError(NhlFATAL,NhlEUNKNOWN,
  161. "shgrid: missing values in the input data have reduced the number "
  162. "of valid points to less than 3.\n");
  163. return(NhlFATAL);
  164. }
  165. num_missing++;
  166. goto lab0;
  167. }
  168. i++;
  169. if (i < num_points) goto lab0;
  170. }
  171. if (has_missing_yi) {
  172. i = 0;
  173. lab1:
  174. if ((yi[i] == missing_yi.floatval) && (i < num_points)) {
  175. for (j = i; j < num_points - 1; j++) {
  176. xi[j] = xi[j+1];
  177. yi[j] = yi[j+1];
  178. zi[j] = zi[j+1];
  179. fval[j] = fval[j+1];
  180. }
  181. num_points--;
  182. if (num_points < 3) {
  183. NhlPError(NhlFATAL,NhlEUNKNOWN,
  184. "shgrid: missing values in the input data have reduced the number "
  185. "of valid points to less than 3.\n");
  186. return(NhlFATAL);
  187. }
  188. num_missing++;
  189. goto lab1;
  190. }
  191. i++;
  192. if (i < num_points) goto lab1;
  193. }
  194. if (has_missing_zi) {
  195. i = 0;
  196. lab2:
  197. if ((zi[i] == missing_zi.floatval) && (i < num_points)) {
  198. for (j = i; j < num_points - 1; j++) {
  199. xi[j] = xi[j+1];
  200. yi[j] = yi[j+1];
  201. zi[j] = zi[j+1];
  202. fval[j] = fval[j+1];
  203. }
  204. num_points--;
  205. if (num_points < 3) {
  206. NhlPError(NhlFATAL,NhlEUNKNOWN,
  207. "shgrid: missing values in the input data have reduced the number "
  208. "of valid points to less than 3.\n");
  209. return(NhlFATAL);
  210. }
  211. num_missing++;
  212. goto lab2;
  213. }
  214. i++;
  215. if (i < num_points) goto lab2;
  216. }
  217. if (has_missing_fval) {
  218. i = 0;
  219. lab3:
  220. if ((fval[i] == missing_fval.floatval) && (i < num_points)) {
  221. for (j = i; j < num_points - 1; j++) {
  222. xi[j] = xi[j+1];
  223. yi[j] = yi[j+1];
  224. zi[j] = zi[j+1];
  225. fval[j] = fval[j+1];
  226. }
  227. num_points--;
  228. if (num_points < 3) {
  229. NhlPError(NhlFATAL,NhlEUNKNOWN,
  230. "shgrid: missing values in the input data have reduced the number "
  231. "of valid points to less than 3.\n");
  232. return(NhlFATAL);
  233. }
  234. num_missing++;
  235. goto lab3;
  236. }
  237. i++;
  238. if (i < num_points) goto lab3;
  239. }
  240. /*
  241. * Issue warning if missing values have been detected.
  242. */
  243. if (num_missing > 0) {
  244. NhlPError(NhlWARNING,NhlEUNKNOWN,
  245. "shgrid: missing values in %d input points - those points ignored.",
  246. num_missing);
  247. }
  248. /*
  249. * Missing values not allowed in the output arrays.
  250. */
  251. if (has_missing_xo) {
  252. for (i = 0; i < dsizes_xo[0]; i++) {
  253. if (xo[i] == missing_xo.floatval) {
  254. NhlPError(NhlFATAL,NhlEUNKNOWN,
  255. "shgrid: output arrays not allowed to contain a missing value.");
  256. return(NhlFATAL);
  257. }
  258. }
  259. }
  260. if (has_missing_yo) {
  261. for (i = 0; i < dsizes_yo[0]; i++) {
  262. if (yo[i] == missing_yo.floatval) {
  263. NhlPError(NhlFATAL,NhlEUNKNOWN,
  264. "shgrid: output arrays not allowed to contain a missing value.");
  265. return(NhlFATAL);
  266. }
  267. }
  268. }
  269. if (has_missing_zo) {
  270. for (i = 0; i < dsizes_zo[0]; i++) {
  271. if (zo[i] == missing_zo.floatval) {
  272. NhlPError(NhlFATAL,NhlEUNKNOWN,
  273. "shgrid: output arrays not allowed to contain a missing value.");
  274. return(NhlFATAL);
  275. }
  276. }
  277. }
  278. /*
  279. * Call the C procedure.
  280. */
  281. uo = c_shgrid(nxi, xi, yi, zi, fval, nxo, nyo, nzo,
  282. xo, yo, zo, &ier);
  283. if (ier != 0) {
  284. sprintf(shmsg, "shgrid: Error number %d.", ier);
  285. NhlPError(NhlFATAL, NhlEUNKNOWN, shmsg);
  286. return(NhlFATAL);
  287. }
  288. ndims_uo = 3;
  289. dsizes_uo[0] = dsizes_xo[0];
  290. dsizes_uo[1] = dsizes_yo[0];
  291. dsizes_uo[2] = dsizes_zo[0];
  292. return(NclReturnValue( (void *) uo, ndims_uo, dsizes_uo, NULL, NCL_float, 0));
  293. }
  294. NhlErrorTypes shgetnp_W(void)
  295. {
  296. int ier = 0, nxi;
  297. ng_size_t i, j, num_points, num_missing;
  298. float *px;
  299. ng_size_t dsizes_px[NCL_MAX_DIMENSIONS];
  300. float *py;
  301. ng_size_t dsizes_py[NCL_MAX_DIMENSIONS];
  302. float *pz;
  303. ng_size_t dsizes_pz[NCL_MAX_DIMENSIONS];
  304. float *xi;
  305. ng_size_t dsizes_xi[NCL_MAX_DIMENSIONS];
  306. float *yi;
  307. ng_size_t dsizes_yi[NCL_MAX_DIMENSIONS];
  308. float *zi;
  309. ng_size_t dsizes_zi[NCL_MAX_DIMENSIONS];
  310. int *flag;
  311. ng_size_t dsizes_flag[NCL_MAX_DIMENSIONS];
  312. int has_missing_px, has_missing_py, has_missing_pz;
  313. NclScalar missing_px, missing_py, missing_pz;
  314. int has_missing_xi, has_missing_yi, has_missing_zi;
  315. NclScalar missing_xi, missing_yi, missing_zi;
  316. int *index;
  317. ng_size_t index_dims = 1;
  318. /*
  319. * Retrieve argument #0 (x coordinate of reference point).
  320. */
  321. px = (float *) NclGetArgValue(
  322. 0,
  323. 7,
  324. NULL,
  325. dsizes_px,
  326. &missing_px,
  327. &has_missing_px,
  328. NULL,
  329. DONT_CARE);
  330. /*
  331. * Retrieve argument #1 (y coordinate of reference point).
  332. */
  333. py = (float *) NclGetArgValue(
  334. 1,
  335. 7,
  336. NULL,
  337. dsizes_py,
  338. &missing_py,
  339. &has_missing_py,
  340. NULL,
  341. DONT_CARE);
  342. /*
  343. * Retrieve argument #2 (z coordinate of reference point).
  344. */
  345. pz = (float *) NclGetArgValue(
  346. 2,
  347. 7,
  348. NULL,
  349. dsizes_pz,
  350. &missing_pz,
  351. &has_missing_pz,
  352. NULL,
  353. DONT_CARE);
  354. /*
  355. * Retrieve argument #3 (x coordinates of input data).
  356. */
  357. xi = (float *) NclGetArgValue(
  358. 3,
  359. 7,
  360. NULL,
  361. dsizes_xi,
  362. &missing_xi,
  363. &has_missing_xi,
  364. NULL,
  365. DONT_CARE);
  366. /*
  367. * Retrieve argument #4 (y coordinates of input data).
  368. */
  369. yi = (float *) NclGetArgValue(
  370. 4,
  371. 7,
  372. NULL,
  373. dsizes_yi,
  374. &missing_yi,
  375. &has_missing_yi,
  376. NULL,
  377. DONT_CARE);
  378. /*
  379. * Retrieve argument #5 (z coordinates of input data).
  380. */
  381. zi = (float *) NclGetArgValue(
  382. 5,
  383. 7,
  384. NULL,
  385. dsizes_zi,
  386. &missing_zi,
  387. &has_missing_zi,
  388. NULL,
  389. DONT_CARE);
  390. /*
  391. * Retrieve argument #6 (call flag)
  392. */
  393. flag = (int *) NclGetArgValue(
  394. 6,
  395. 7,
  396. NULL,
  397. dsizes_flag,
  398. NULL,
  399. NULL,
  400. NULL,
  401. DONT_CARE);
  402. /*
  403. * Check that the input arrays are the same size.
  404. */
  405. if( (dsizes_xi[0] != dsizes_yi[0]) ||
  406. (dsizes_yi[0] != dsizes_zi[0])) {
  407. NhlPError(NhlFATAL,NhlEUNKNOWN,
  408. "shgetnp: all arrays for the input functional values"
  409. " must be of the same size");
  410. return(NhlFATAL);
  411. }
  412. else {
  413. num_points = dsizes_xi[0];
  414. num_missing = 0;
  415. }
  416. /*
  417. * Test the dimension sizes.
  418. */
  419. if(dsizes_xi[0] > INT_MAX) {
  420. NhlPError(NhlFATAL,NhlEUNKNOWN,"shgetnp: the length of xi is greater than INT_MAX");
  421. return(NhlFATAL);
  422. }
  423. nxi = (int) num_points;
  424. /*
  425. * Check for missing values. Each argument xi, yi, and zi must
  426. * be checked separately, since it may be that _FillValue may be
  427. * set for just one or two of the arguments.
  428. *
  429. * If a missing value is found, ignore that input point by collapsing
  430. * all of the input arrays.
  431. */
  432. if (has_missing_xi) {
  433. i = 0;
  434. lab0:
  435. if ((xi[i] == missing_xi.floatval) && (i < num_points)) {
  436. for (j = i; j < num_points - 1; j++) {
  437. xi[j] = xi[j+1];
  438. yi[j] = yi[j+1];
  439. zi[j] = zi[j+1];
  440. }
  441. num_points--;
  442. if (num_points < 3) {
  443. NhlPError(NhlFATAL,NhlEUNKNOWN,
  444. "shgrid: missing values in the input data have reduced the number "
  445. "of valid points to less than 3.\n");
  446. return(NhlFATAL);
  447. }
  448. num_missing++;
  449. goto lab0;
  450. }
  451. i++;
  452. if (i < num_points) goto lab0;
  453. }
  454. if (has_missing_yi) {
  455. i = 0;
  456. lab1:
  457. if ((yi[i] == missing_yi.floatval) && (i < num_points)) {
  458. for (j = i; j < num_points - 1; j++) {
  459. xi[j] = xi[j+1];
  460. yi[j] = yi[j+1];
  461. zi[j] = zi[j+1];
  462. }
  463. num_points--;
  464. if (num_points < 3) {
  465. NhlPError(NhlFATAL,NhlEUNKNOWN,
  466. "shgrid: missing values in the input data have reduced the number "
  467. "of valid points to less than 3.\n");
  468. return(NhlFATAL);
  469. }
  470. num_missing++;
  471. goto lab1;
  472. }
  473. i++;
  474. if (i < num_points) goto lab1;
  475. }
  476. if (has_missing_zi) {
  477. i = 0;
  478. lab2:
  479. if ((zi[i] == missing_zi.floatval) && (i < num_points)) {
  480. for (j = i; j < num_points - 1; j++) {
  481. xi[j] = xi[j+1];
  482. yi[j] = yi[j+1];
  483. zi[j] = zi[j+1];
  484. }
  485. num_points--;
  486. if (num_points < 3) {
  487. NhlPError(NhlFATAL,NhlEUNKNOWN,
  488. "shgrid: missing values in the input data have reduced the number "
  489. "of valid points to less than 3.\n");
  490. return(NhlFATAL);
  491. }
  492. num_missing++;
  493. goto lab2;
  494. }
  495. i++;
  496. if (i < num_points) goto lab2;
  497. }
  498. /*
  499. * Issue warning if missing values have been detected.
  500. */
  501. if (num_missing > 0) {
  502. NhlPError(NhlWARNING,NhlEUNKNOWN,
  503. "shgetnp: missing values in %d input points - those points ignored.",
  504. num_missing);
  505. }
  506. /*
  507. * Call the C procedure.
  508. */
  509. index = (int*)malloc(sizeof(int));
  510. *index = c_shgetnp(*px, *py, *pz, nxi, xi, yi, zi, *flag, &ier);
  511. if (ier != 0) {
  512. sprintf(shmsg, "shgetnp: Error number %d.", ier);
  513. NhlPError(NhlFATAL, NhlEUNKNOWN, shmsg);
  514. return(NhlFATAL);
  515. }
  516. return(NclReturnValue( (void *) index, 1, &index_dims, NULL, NCL_int, 0));
  517. }
  518. NhlErrorTypes shsetp_W(void)
  519. {
  520. char *arg1;
  521. int numpi, i;
  522. /*
  523. * List the integer parameter names. To add new ones,
  524. * all that needs to be done is add the names to this list.
  525. */
  526. char *params_i[] = {"nls", "NLS", "nfl", "NFL", "ncl", "NCL"};
  527. /*
  528. * Input array variables
  529. */
  530. string *pname;
  531. ng_size_t dsizes_pname[NCL_MAX_DIMENSIONS];
  532. void *pvalue;
  533. ng_size_t dsizes_pvalue[NCL_MAX_DIMENSIONS];
  534. NclBasicDataTypes type_pname, type_pvalue;
  535. /*
  536. * Retrieve argument #1
  537. */
  538. pname = (string *) NclGetArgValue(
  539. 0,
  540. 2,
  541. NULL,
  542. dsizes_pname,
  543. NULL,
  544. NULL,
  545. &type_pname,
  546. DONT_CARE);
  547. arg1 = NrmQuarkToString(*pname);
  548. /*
  549. * Check to see if the parameter name is valid.
  550. */
  551. numpi = sizeof(params_i)/sizeof(void *);
  552. for (i = 0; i < numpi; i++) {
  553. if (!strncmp(arg1, params_i[i], strlen(params_i[i]))) {
  554. goto OK_NAME;
  555. }
  556. }
  557. NhlPError(NhlFATAL, NhlEUNKNOWN, "shsetp: unrecognized parameter name");
  558. return(NhlFATAL);
  559. /*
  560. * Retrieve argument #2
  561. */
  562. OK_NAME: pvalue = (void *) NclGetArgValue(
  563. 1,
  564. 2,
  565. NULL,
  566. dsizes_pvalue,
  567. NULL,
  568. NULL,
  569. &type_pvalue,
  570. DONT_CARE);
  571. /*
  572. * Process the parameter if it has an integer value.
  573. */
  574. if (type_pvalue == NCL_int) {
  575. for (i = 0; i < numpi; i++) {
  576. if (!strncmp(arg1, params_i[i], strlen(params_i[i]))) {
  577. c_shseti(arg1, *((int *) pvalue));
  578. return(NhlNOERROR);
  579. }
  580. }
  581. NhlPError(NhlFATAL, NhlEUNKNOWN, "The specified value for the parameter "
  582. "has an invalid type");
  583. return(NhlFATAL);
  584. }
  585. else {
  586. NhlPError(NhlFATAL, NhlEUNKNOWN, "The specified value for the parameter "
  587. "has an invalid type");
  588. return(NhlFATAL);
  589. }
  590. }
  591. NhlErrorTypes shgetp_W(void)
  592. {
  593. /*
  594. * Get values for shgrid parameters.
  595. */
  596. char *arg1;
  597. int numpi, i;
  598. /*
  599. * List the integer parameter names. To add new ones,
  600. * all that needs to be done is add the names to this list.
  601. */
  602. char *params_i[] = {"nls", "NLS", "nfl", "NFL", "ncl", "NCL"};
  603. /*
  604. * Input array variable
  605. */
  606. string *pname;
  607. ng_size_t dsizes_pname[NCL_MAX_DIMENSIONS];
  608. NclBasicDataTypes type_pname;
  609. int *ival;
  610. ng_size_t ret_size = 1;
  611. /*
  612. * Retrieve argument #1
  613. */
  614. pname = (string *) NclGetArgValue(
  615. 0,
  616. 1,
  617. NULL,
  618. dsizes_pname,
  619. NULL,
  620. NULL,
  621. &type_pname,
  622. DONT_CARE);
  623. arg1 = NrmQuarkToString(*pname);
  624. /*
  625. * Check to see if the parameter name is valid.
  626. */
  627. numpi = sizeof(params_i)/sizeof(void *);
  628. for (i = 0; i < numpi; i++) {
  629. if (!strncmp(arg1, params_i[i], strlen(params_i[i]))) {
  630. goto OK_NAME;
  631. }
  632. }
  633. NhlPError(NhlFATAL, NhlEUNKNOWN, "shgetp: unrecognized parameter name");
  634. return(NhlFATAL);
  635. /*
  636. * Process the parameter if it has an integer value.
  637. */
  638. OK_NAME:
  639. for (i = 0; i < numpi; i++) {
  640. if (!strncmp(arg1, params_i[i], strlen(params_i[i]))) {
  641. ival = (int *) calloc(1,sizeof(int));
  642. *ival = c_shgeti(arg1);
  643. return(NclReturnValue( (void *) ival, 1, &ret_size, NULL, NCL_int, 0));
  644. }
  645. }
  646. NhlPError(NhlFATAL, NhlEUNKNOWN, "shgetp: impossible to get this message");
  647. return(NhlFATAL);
  648. }