home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / examples.lha / examples / sds / slices / putslices.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  2.9 KB  |  116 lines

  1. /************************************************************ 
  2. *
  3. * Example program: Illustrates storing slices in a file
  4. *
  5. * Input file:  none
  6. * Output file: named by user on command line
  7. *
  8. ************************************************************ */
  9.  
  10. #include <stdio.h>
  11. #include "df.h"
  12. #define ROWS 10
  13. #define COLS 12 
  14. #define TRUE   1
  15. #define FALSE  0
  16.  
  17. main(argc,argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     int i, j, count, ret;
  22.     int dimsizes[2], windims[2];
  23.     float data[ROWS][COLS], indata[ROWS][COLS];
  24.     int data_ok = TRUE;
  25.     
  26.     if (argc != 2) {
  27.         printf("Usage: %s outfile\n", argv[0]);
  28.         exit(1);
  29.     }
  30.  
  31.     printf("\nWrites out 10x12 data set in slices, then reads it back in.\n");
  32.  
  33.     count = 0;
  34.     for (i=0; i<ROWS; i++)
  35.        for (j=0; j<COLS; j++) 
  36.            data[i][j] = (float) count++;
  37.  
  38.     printf("\nHere is the data set:\n");   
  39.     for (i=0; i<ROWS; i++) {
  40.         printf("\n %2d ", i+1);
  41.         for (j=0; j<COLS; j++)
  42.             printf("%5.0f", data[i][j]);
  43.     }
  44.     printf("\n");
  45.  
  46.     dimsizes[0]=ROWS;
  47.     dimsizes[1]=COLS;
  48.  
  49.     DFSDsetdims(2,dimsizes);
  50.  
  51.     /* write out scientific data set in slices */
  52.     DFSDstartslice(argv[1]);
  53.  
  54.     printf("\nNow writing out the slices.\n\n");
  55.     printf("First slice: (1,1) to (2,12)\n");
  56.     windims[0]=2; windims[1]=12;        /* {(1,1) to (2,12)} */
  57.     ret = DFSDputslice(windims, &data[0][0], dimsizes);
  58.     checkerror(ret, "DFSDputslice");
  59.  
  60.     printf(" Next slice: (3,1) to (6,12)\n");
  61.     windims[0]=4; windims[1]=12;        /* {(3,1) to (6,12)} */
  62.     ret = DFSDputslice(windims, &data[2][0], dimsizes);
  63.     checkerror(ret, "DFSDputslice");
  64.  
  65.     printf("  Next slice: (7,1) to (7,4)\n");
  66.     windims[0]=1; windims[1]=4;        /* {(7,1) to (7,4)} */
  67.     ret = DFSDputslice(windims, &data[6][0], dimsizes);
  68.     checkerror(ret, "DFSDputslice");
  69.  
  70.     printf("   Next slice: (7,5) to (7,12)\n");
  71.     windims[0]=1; windims[1]=8;        /* {(7,5) to (7,12)} */
  72.     ret = DFSDputslice(windims, &data[6][4], dimsizes);
  73.     checkerror(ret, "DFSDputslice");
  74.  
  75.     printf("    Final slice: (8,1) to (10,12)\n");
  76.     windims[0]=3; windims[1]=12;        /* {(8,1) to (10,12)} */
  77.     ret = DFSDputslice(windims, &data[7][0], dimsizes);
  78.     checkerror(ret, "DFSDputslice");
  79.  
  80.     ret = DFSDendslice();
  81.     checkerror(ret, "DFSDendslice");
  82.  
  83.     printf("\nNow reading the data set back in.\nHere it is:\n");
  84.     ret = DFSDgetdata(argv[1], 2, dimsizes, indata);
  85.     for (i=0; i<ROWS; i++) {
  86.         printf("\n %2d ", i+1);
  87.         for (j=0; j<COLS; j++) {
  88.         if (indata[i][j] != data[i][j])
  89.         data_ok = FALSE;
  90.             printf("%5.0f", indata[i][j]);
  91.     }
  92.     }
  93.  
  94.     printf("\n\n");
  95.     if (data_ok == TRUE)
  96.     printf("Test of putslice successful!\n");
  97.     else
  98.     printf("Test unsuccessful, at least one number did not match.\n");
  99.     printf("\n");
  100. }
  101.  
  102.  
  103. checkerror(ret, name)
  104. int ret;
  105. char *name;
  106. {
  107.     if ( ret != 0) {
  108.         printf("\nError calling routine %s\n", name);
  109.         printf("ret = %d;   DFerror = %d\n\n", ret, DFerror);
  110.     }
  111. }
  112.  
  113.  
  114.  
  115.  
  116.