superobs3d.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* File: superobs3.c
  2. *
  3. * Created: 17 Nov 2009
  4. *
  5. * Last modified: 17 Nov 2009
  6. * Author: Pavel Sakov
  7. * NERSC
  8. *
  9. * Purpose: Sorting of observations according to model grid cells.
  10. *
  11. * Description: This file is an extension of the superobs.c for the 3D case.
  12. *
  13. * Modifications: none
  14. */
  15. #include <math.h>
  16. #include "cfortran.h"
  17. #define IMAX 4096
  18. #define JMAX 4096
  19. #define KMAX 150
  20. typedef struct {
  21. int i;
  22. int j;
  23. int k;
  24. int index;
  25. } indexedvalue;
  26. static int comp(const void* p1, const void* p2)
  27. {
  28. indexedvalue* v1 = (indexedvalue*) p1;
  29. indexedvalue* v2 = (indexedvalue*) p2;
  30. if (v1->i > v2->i)
  31. return 1;
  32. else if (v1->i < v2->i)
  33. return -1;
  34. else if (v1->j > v2->j)
  35. return 1;
  36. else if (v1->j < v2->j)
  37. return -1;
  38. else if (v1->k > v2->k)
  39. return 1;
  40. else if (v1->k < v2->k)
  41. return -1;
  42. return 0;
  43. }
  44. void sortgriddedobs3d(double pn, int ipiv[], int jpiv[], int kpiv[], int sorted[])
  45. {
  46. int n = (int) pn;
  47. indexedvalue* iv = malloc(n * sizeof(indexedvalue));
  48. int i;
  49. for (i = 0; i < n; ++i) {
  50. int ii = ipiv[i];
  51. int jj = jpiv[i];
  52. int kk = kpiv[i];
  53. if (ii <= 0 || ii > IMAX || jj <= 0 || jj > JMAX || kk < 0 || kk > KMAX) {
  54. fprintf(stderr, "ERROR: superobs.c: sortgriddedobs(): ipiv(%d) = %d or jpiv(%d) = %d or kpiv(%d) = %d out of bounds\n", i, ii, i, jj, i, kk);
  55. exit(1);
  56. }
  57. iv[i].i = ii;
  58. iv[i].j = jj;
  59. iv[i].k = kk;
  60. iv[i].index = i;
  61. }
  62. qsort(iv, n, sizeof(indexedvalue), comp);
  63. for (i = 0; i < n; ++i)
  64. sorted[i] = iv[i].index + 1;
  65. free(iv);
  66. }
  67. FCALLSCSUB5(sortgriddedobs3d, SORTGRIDDEDOBS3D, sortgriddedobs3d, DOUBLE, PINT, PINT, PINT, PINT)