write2d.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*-
  2. * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Fall 1993
  3. * University of Illinois
  4. * US Army Construction Engineering Research Lab
  5. * Copyright 1993, H. Mitasova (University of Illinois),
  6. * I. Kosinovsky, (USA-CERL), and D.Gerdes (USA-CERL)
  7. *
  8. * modified by McCauley in August 1995
  9. * modified by Mitasova in August 1995
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <unistd.h>
  15. #include <grass/gis.h>
  16. #include <grass/interpf.h>
  17. int IL_write_temp_2d(struct interp_params *params, int ngstc, int nszc, int offset2 /* begin. and end. column, offset */
  18. )
  19. /*
  20. * Writes az,adx,...,adxy into appropriate place (depending on ngstc, nszc
  21. * and offset) in corresponding temp file
  22. */
  23. {
  24. int j;
  25. static FCELL *array_cell = NULL;
  26. if (!array_cell) {
  27. if (!
  28. (array_cell =
  29. (FCELL *) G_malloc(sizeof(FCELL) * params->nsizc + 1))) {
  30. fprintf(stderr, "Cannot allocate memory for array_cell\n");
  31. return -1;
  32. }
  33. }
  34. if (params->Tmp_fd_z != NULL) {
  35. for (j = ngstc; j <= nszc; j++)
  36. array_cell[j - 1] = (FCELL) params->az[j];
  37. if (fseek(params->Tmp_fd_z, (long)offset2, 0) == -1) {
  38. fprintf(stderr, "Cannot fseek elev offset2=%d\n", (int)offset2);
  39. return -1;
  40. }
  41. if (!
  42. (fwrite
  43. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  44. params->Tmp_fd_z))) {
  45. fprintf(stderr, "Not enough disk space--cannot write files\n");
  46. return -1;
  47. }
  48. }
  49. if (params->Tmp_fd_dx != NULL) {
  50. for (j = ngstc; j <= nszc; j++)
  51. if (!params->deriv)
  52. array_cell[j - 1] = (FCELL) params->adx[j];
  53. else
  54. array_cell[j - 1] = (FCELL) (params->adx[j] * params->scik1);
  55. if (fseek(params->Tmp_fd_dx, (long)offset2, 0) == -1) {
  56. fprintf(stderr, "Cannot fseek slope\n");
  57. return -1;
  58. }
  59. if (!
  60. (fwrite
  61. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  62. params->Tmp_fd_dx))) {
  63. fprintf(stderr, "Not enough disk space--cannot write files\n");
  64. return -1;
  65. }
  66. }
  67. if (params->Tmp_fd_dy != NULL) {
  68. for (j = ngstc; j <= nszc; j++) {
  69. if (!params->deriv) {
  70. if (params->ady[j] > 0. && params->ady[j] < 0.5)
  71. params->ady[j] = 360.;
  72. array_cell[j - 1] = (FCELL) params->ady[j];
  73. }
  74. else
  75. array_cell[j - 1] = (FCELL) (params->ady[j] * params->scik1);
  76. }
  77. if (fseek(params->Tmp_fd_dy, (long)offset2, 0) == -1) {
  78. fprintf(stderr, "Cannot fseek aspect\n");
  79. return -1;
  80. }
  81. if (!
  82. (fwrite
  83. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  84. params->Tmp_fd_dy))) {
  85. fprintf(stderr, "Not enough disk space--cannot write files\n");
  86. return -1;
  87. }
  88. }
  89. if (params->Tmp_fd_xx != NULL) {
  90. for (j = ngstc; j <= nszc; j++) {
  91. array_cell[j - 1] = (FCELL) (params->adxx[j] * params->scik1);
  92. }
  93. if (fseek(params->Tmp_fd_xx, (long)offset2, 0) == -1) {
  94. fprintf(stderr, "Cannot fseek pcurv\n");
  95. return -1;
  96. }
  97. if (!
  98. (fwrite
  99. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  100. params->Tmp_fd_xx))) {
  101. fprintf(stderr, "Not enough disk space--cannot write files\n");
  102. return -1;
  103. }
  104. }
  105. if (params->Tmp_fd_yy != NULL) {
  106. for (j = ngstc; j <= nszc; j++)
  107. array_cell[j - 1] = (FCELL) (params->adyy[j] * params->scik2);
  108. if (fseek(params->Tmp_fd_yy, (long)offset2, 0) == -1) {
  109. fprintf(stderr, "Cannot fseek tcurv\n");
  110. return -1;
  111. }
  112. if (!
  113. (fwrite
  114. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  115. params->Tmp_fd_yy))) {
  116. fprintf(stderr, "Not enough disk space--cannot write files\n");
  117. return -1;
  118. }
  119. }
  120. if (params->Tmp_fd_xy != NULL) {
  121. for (j = ngstc; j <= nszc; j++)
  122. array_cell[j - 1] = (FCELL) (params->adxy[j] * params->scik3);
  123. if (fseek(params->Tmp_fd_xy, (long)offset2, 0) == -1) {
  124. fprintf(stderr, "Cannot fseek mcurv\n");
  125. return -1;
  126. }
  127. if (!
  128. (fwrite
  129. (array_cell + ngstc - 1, sizeof(FCELL), nszc - ngstc + 1,
  130. params->Tmp_fd_xy))) {
  131. fprintf(stderr, "Not enough disk space--cannot write files\n");
  132. return -1;
  133. }
  134. }
  135. return 1;
  136. }