gsl_histogram__file2d.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* histogram/file2d.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdio.h>
  21. #include "gsl_errno.h"
  22. #include "gsl_block.h"
  23. #include "gsl_histogram2d.h"
  24. int
  25. gsl_histogram2d_fread (FILE * stream, gsl_histogram2d * h)
  26. {
  27. int status = gsl_block_raw_fread (stream, h->xrange, h->nx + 1, 1);
  28. if (status)
  29. return status;
  30. status = gsl_block_raw_fread (stream, h->yrange, h->ny + 1, 1);
  31. if (status)
  32. return status;
  33. status = gsl_block_raw_fread (stream, h->bin, h->nx * h->ny, 1);
  34. return status;
  35. }
  36. int
  37. gsl_histogram2d_fwrite (FILE * stream, const gsl_histogram2d * h)
  38. {
  39. int status = gsl_block_raw_fwrite (stream, h->xrange, h->nx + 1, 1);
  40. if (status)
  41. return status;
  42. status = gsl_block_raw_fwrite (stream, h->yrange, h->ny + 1, 1);
  43. if (status)
  44. return status;
  45. status = gsl_block_raw_fwrite (stream, h->bin, h->nx * h->ny, 1);
  46. return status;
  47. }
  48. int
  49. gsl_histogram2d_fprintf (FILE * stream, const gsl_histogram2d * h,
  50. const char *range_format, const char *bin_format)
  51. {
  52. size_t i, j;
  53. const size_t nx = h->nx;
  54. const size_t ny = h->ny;
  55. int status;
  56. for (i = 0; i < nx; i++)
  57. {
  58. for (j = 0; j < ny; j++)
  59. {
  60. status = fprintf (stream, range_format, h->xrange[i]);
  61. if (status < 0)
  62. {
  63. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  64. }
  65. status = putc (' ', stream);
  66. if (status == EOF)
  67. {
  68. GSL_ERROR ("putc failed", GSL_EFAILED);
  69. }
  70. status = fprintf (stream, range_format, h->xrange[i + 1]);
  71. if (status < 0)
  72. {
  73. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  74. }
  75. status = putc (' ', stream);
  76. if (status == EOF)
  77. {
  78. GSL_ERROR ("putc failed", GSL_EFAILED);
  79. }
  80. status = fprintf (stream, range_format, h->yrange[j]);
  81. if (status < 0)
  82. {
  83. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  84. }
  85. status = putc (' ', stream);
  86. if (status == EOF)
  87. {
  88. GSL_ERROR ("putc failed", GSL_EFAILED);
  89. }
  90. status = fprintf (stream, range_format, h->yrange[j + 1]);
  91. if (status < 0)
  92. {
  93. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  94. }
  95. status = putc (' ', stream);
  96. if (status == EOF)
  97. {
  98. GSL_ERROR ("putc failed", GSL_EFAILED);
  99. }
  100. status = fprintf (stream, bin_format, h->bin[i * ny + j]);
  101. if (status < 0)
  102. {
  103. GSL_ERROR ("fprintf failed", GSL_EFAILED);
  104. }
  105. status = putc ('\n', stream);
  106. if (status == EOF)
  107. {
  108. GSL_ERROR ("putc failed", GSL_EFAILED);
  109. }
  110. }
  111. status = putc ('\n', stream);
  112. if (status == EOF)
  113. {
  114. GSL_ERROR ("putc failed", GSL_EFAILED);
  115. }
  116. }
  117. return GSL_SUCCESS;
  118. }
  119. int
  120. gsl_histogram2d_fscanf (FILE * stream, gsl_histogram2d * h)
  121. {
  122. size_t i, j;
  123. const size_t nx = h->nx;
  124. const size_t ny = h->ny;
  125. double xupper, yupper;
  126. for (i = 0; i < nx; i++)
  127. {
  128. for (j = 0; j < ny; j++)
  129. {
  130. int status = fscanf (stream,
  131. "%lg %lg %lg %lg %lg",
  132. h->xrange + i, &xupper,
  133. h->yrange + j, &yupper,
  134. h->bin + i * ny + j);
  135. if (status != 5)
  136. {
  137. GSL_ERROR ("fscanf failed", GSL_EFAILED);
  138. }
  139. }
  140. h->yrange[ny] = yupper;
  141. }
  142. h->xrange[nx] = xupper;
  143. return GSL_SUCCESS;
  144. }