PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wrfv2_fire/external/io_grib1/WGRIB/gds_grid.c

http://github.com/jbeezley/wrf-fire
C | 162 lines | 127 code | 19 blank | 16 comment | 51 complexity | ec6a717236ae72d62df97b5f26905525 MD5 | raw file
Possible License(s): AGPL-1.0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "grib.h"
  4. #include "bds.h"
  5. #include "gds.h"
  6. /*
  7. * get grid size from GDS
  8. *
  9. * added calculation of nxny of spectral data and clean up of triangular
  10. * grid nnxny calculation l. kornblueh
  11. * 7/25/03 wind fix Dusan Jovic
  12. * 9/17/03 fix scan mode
  13. */
  14. int GDS_grid(unsigned char *gds, unsigned char *bds, int *nx, int *ny,
  15. long int *nxny) {
  16. int i, d, ix, iy, pl;
  17. long int isum;
  18. *nx = ix = GDS_LatLon_nx(gds);
  19. *ny = iy = GDS_LatLon_ny(gds);
  20. *nxny = ix * iy;
  21. /* thin grid */
  22. if (GDS_Gaussian(gds) || GDS_LatLon(gds)) {
  23. if (ix == 65535) {
  24. *nx = -1;
  25. /* reduced grid */
  26. isum = 0;
  27. pl = GDS_PL(gds);
  28. for (i = 0; i < iy; i++) {
  29. isum += gds[pl+i*2]*256 + gds[pl+i*2+1];
  30. }
  31. *nxny = isum;
  32. }
  33. return 0;
  34. }
  35. if (GDS_Triangular(gds)) {
  36. i = GDS_Triangular_ni(gds);
  37. d = GDS_Triangular_nd(gds);
  38. *nx = *nxny = d * (i + 1) * (i + 1);
  39. *ny = 1;
  40. return 0;
  41. }
  42. if (GDS_Harmonic(gds)) {
  43. /* this code assumes j, k, m are consistent with bds */
  44. *nx = *nxny = (8*(BDS_LEN(bds)-15)-BDS_UnusedBits(bds))/
  45. BDS_NumBits(bds)+1;
  46. if ((8*(BDS_LEN(bds)-15)-BDS_UnusedBits(bds)) % BDS_NumBits(bds)) {
  47. fprintf(stderr,"inconsistent harmonic BDS\n");
  48. }
  49. *ny = 1;
  50. }
  51. return 0;
  52. }
  53. #define NCOL 15
  54. void GDS_prt_thin_lon(unsigned char *gds) {
  55. int iy, i, col, pl;
  56. iy = GDS_LatLon_ny(gds);
  57. iy = (iy + 1) / 2;
  58. iy = GDS_LatLon_ny(gds);
  59. if ((pl = GDS_PL(gds)) == -1) {
  60. fprintf(stderr,"\nprogram error: GDS_prt_thin\n");
  61. return;
  62. }
  63. for (col = i = 0; i < iy; i++) {
  64. if (col == 0) printf(" ");
  65. printf("%5d", (gds[pl+i*2] << 8) + gds[pl+i*2+1]);
  66. col++;
  67. if (col == NCOL) {
  68. col = 0;
  69. printf("\n");
  70. }
  71. }
  72. if (col != 0) printf("\n");
  73. }
  74. /*
  75. * prints out wind rel to grid or earth
  76. */
  77. static char *scan_mode[8] = {
  78. "WE:NS",
  79. "NS:WE",
  80. "WE:SN",
  81. "SN:WE",
  82. "EW:NS",
  83. "NS:EW",
  84. "EW:SN",
  85. "SN:EW" };
  86. void GDS_winds(unsigned char *gds, int verbose) {
  87. int scan = -1, mode = -1;
  88. if (gds != NULL) {
  89. if (GDS_LatLon(gds)) {
  90. scan = GDS_LatLon_scan(gds);
  91. mode = GDS_LatLon_mode(gds);
  92. }
  93. else if (GDS_Mercator(gds)) {
  94. scan =GDS_Merc_scan(gds);
  95. mode =GDS_Merc_mode(gds);
  96. }
  97. /* else if (GDS_Gnomonic(gds)) { */
  98. else if (GDS_Lambert(gds)) {
  99. scan = GDS_Lambert_scan(gds);
  100. mode = GDS_Lambert_mode(gds);
  101. }
  102. else if (GDS_Gaussian(gds)) {
  103. scan = GDS_LatLon_scan(gds);
  104. mode = GDS_LatLon_mode(gds);
  105. }
  106. else if (GDS_Polar(gds)) {
  107. scan = GDS_Polar_scan(gds);
  108. mode = GDS_Polar_mode(gds);
  109. }
  110. else if (GDS_RotLL(gds)) {
  111. scan = GDS_RotLL_scan(gds);
  112. mode = GDS_RotLL_mode(gds);
  113. }
  114. /* else if (GDS_Triangular(gds)) { */
  115. else if (GDS_ssEgrid(gds)) {
  116. scan = GDS_ssEgrid_scan(gds);
  117. mode = GDS_ssEgrid_mode(gds);
  118. }
  119. else if (GDS_fEgrid(gds)) {
  120. scan = GDS_fEgrid_scan(gds);
  121. mode = GDS_fEgrid_mode(gds);
  122. }
  123. else if (GDS_ss2dEgrid(gds)) {
  124. scan = GDS_ss2dEgrid_scan(gds);
  125. mode = GDS_ss2dEgrid_mode(gds);
  126. }
  127. }
  128. if (verbose == 1) {
  129. if (mode != -1) {
  130. if (mode & 8) printf("winds in grid direction:");
  131. else printf("winds are N/S:");
  132. }
  133. }
  134. else if (verbose == 2) {
  135. if (scan != -1) {
  136. printf(" scan: %s", scan_mode[(scan >> 5) & 7]);
  137. }
  138. if (mode != -1) {
  139. if (mode & 8) printf(" winds(grid) ");
  140. else printf(" winds(N/S) ");
  141. }
  142. }
  143. }