home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / vis5d / code / sample.c
Encoding:
C/C++ Source or Header  |  1994-06-21  |  13.5 KB  |  451 lines

  1. /* sample.c */
  2.  
  3. /* VIS-5D version 3.3 */
  4.  
  5. /* This file is a skeleton program for converting your data file format to
  6.    McIDAS format.  Basically, you set a number of #defines to describe the
  7.    size of your data set, fill in some arrays, and write the code to read
  8.    your data into a 5-D array.  The 5-D array is then written out in the
  9.    McIDAS format for you.
  10.  
  11.    The file "sample.c.m" is a an example of a Makefile for sample.c
  12.  
  13.    Typically, you will have to edit and recompile this file whenever the
  14.    file you are converting from has a different size, number of variables,
  15.    or timesteps, etc than the last one you converted.  The original copy
  16.    of this file has example values for all the #defines and arrays to
  17.    give you an idea of what's expected.
  18.  
  19.    Also, you should probably rename this file from "sample.c" to a more
  20.    appropriate/descriptive name such as "myformat_to_mcidas.c".
  21.  
  22.    Usage:  To run this program you must give it two arguments:
  23.       (1) the name of the file to read (convert from).
  24.       (2) the number of the McIDAS file to write (convert to).
  25.    If you run the program without any arguments, it will print a usage
  26.    summary.
  27.  
  28. */
  29.  
  30.  
  31. #include <stdio.h>
  32. #include <math.h>
  33. #include "binaryio.h"
  34.  
  35.  
  36. /*** SET THE FOLLOWING CONSTANTS AND VARIABLES TO YOUR VALUES ***/
  37.  
  38.  
  39. /* Optional identification string */
  40. char identification[33] = "This is a test grid";
  41.  
  42.  
  43. /* Dimensions of grid: */
  44. #define NR 30       /* Number of rows */
  45. #define NC 30       /* Number of columns */
  46. #define NL 30       /* Number of levels */
  47. #define NTIMES 10   /* Number of time steps */
  48. #define NPARAMS 2   /* Number of physical vars or parameters */
  49.  
  50.  
  51. /* Geographics boundaries of the grid domain: */
  52. #define NORTH_LATITUDE  45.0
  53. #define SOUTH_LATITUDE  10.0
  54. #define WEST_LONGITUDE 120.0
  55. #define EAST_LONGITUDE  50.0
  56. #define BOTTOM_ALTITUDE  0.0
  57. #define TOP_ALTITUDE    18.0
  58.  
  59.  
  60. /* Each physical variable/parameter has a 4-letter name and units label: */
  61. char  param_name[NPARAMS][5] = { "THET", "T   " };  /* theta and temperature */
  62. char  param_unit[NPARAMS][5] = { "GPCM", "K   " };  /* g/cm3 and Kelvin */
  63.  
  64.  
  65. /* Each time step has a time (in HHMMSS format) and date (in YYDDD format): */
  66. int  grid_time[NTIMES] = { 120000, 120500, 121000, 121500, 122000, 122500,
  67.    123000, 123500, 124000, 124500 };
  68. int  grid_date[NTIMES] = { 92027, 92027, 92027, 92027, 92027, 92027, 92027,
  69.    92027, 92027, 92027 };
  70.  
  71.  
  72. /* The 5-D grid of data:
  73.    The first dimension is time.  The second dimension is the physical
  74.    variable.  The third, fourth, and fifth dimensions correspond to
  75.    levels, columns, and rows, respectively.
  76.  
  77.    Note that grid[t][p][0][0][0] corresponds to the north-west-bottom corner
  78.    and grid[t][p][NL-1][NC-1][NR-1] corresponds to the south-east-top corner.
  79.    A 'missing' data value should be stored as 1.0e35.
  80. */
  81. float grid[NTIMES][NPARAMS][NL][NC][NR];
  82.  
  83.  
  84.  
  85. /* You must write the body of this function.  Its purpose is to open a
  86.    data file (whatever format you use) and read the data set into the
  87.    5-Dimensional grid array.
  88. */
  89. int read_grid( filename )
  90. char *filename;
  91. {
  92.    FILE *f;
  93.    int ip, it, ir, ic, il;
  94.    float dl, dc, dr;
  95.  
  96.    f = fopen( filename, "r" );
  97.    if (!f) {
  98.       printf("Error: Unable to open file %s for reading\n", filename);
  99.       exit(0);
  100.    }
  101.  
  102.  
  103.    /* READ YOUR DATA HERE: */
  104.    /* You'll probably need to use fread if your file is in a binary format */
  105.    /* or use fscanf if your file is in an ASCII format. */
  106.  
  107.  
  108.  
  109.    /* THIS IS JUST AN EXAMPLE OF PUTTING SOME DATA INTO THE grid ARRAY. */
  110.    /* (YOU SHOULD DELETE THIS) */
  111.    /* In this case, we put values in such that isosurfaces will look like */
  112.    /* spheres and contour lines will look like circles in VIS-5D. */
  113.    for (it=0;it<NTIMES;it++) {
  114.       for (ip=0;ip<NPARAMS;ip++) {
  115.  for (il=0;il<NL;il++) {
  116.     dl = (il-NL/2);
  117.     for (ic=0;ic<NC;ic++) {
  118.        dc = (ic-NC/2);
  119.        for (ir=0;ir<NR;ir++) {
  120.   dr = (ir-NR/2);
  121.   grid[it][ip][il][ic][ir] = sqrt(dl*dl + dc*dc + dr*dr) +it+ip;
  122.        }
  123.     }
  124.  }
  125.       }
  126.    }
  127.    /* END OF EXAMPLE OF PUTTING DATA INTO grid ARRAY. (DELETE THIS) */
  128.  
  129.  
  130.    fclose(f);
  131. }
  132.  
  133.  
  134.  
  135.  
  136. /**********************************************************************/
  137. /**********************************************************************/
  138. /***    You shouldn't have to change anything beyond this point.    ***/
  139. /**********************************************************************/
  140. /**********************************************************************/
  141.  
  142.  
  143. main( argc, argv )
  144. int argc;
  145. char *argv[];
  146. {
  147.    int i, n;
  148.    char mcname[1000];
  149.  
  150.    if (argc!=3) {
  151.       printf("Usage:\n");
  152.       printf("   sample yourfile nnnn\n");
  153.       printf("Where:\n");
  154.       printf("   yourfile is the filename of your data set to read.\n");
  155.       printf("   nnnn is the number of the McIDAS file to write.\n");
  156.       printf("Example:\n");
  157.       printf("   sample MODEL.DATA 104\n");
  158.       printf("       This will convert your file MODEL.DATA to the McIDAS\n");
  159.       printf("       grid file named GR3D0104.\n");
  160.       exit(0);
  161.    }
  162.  
  163.    /* fill the grid array with data: */
  164.    read_grid( argv[1] );
  165.  
  166.    /* write the file: */
  167.    n = atoi( argv[2] );
  168.    if (n<1 || n>9999) {
  169.       printf("Error: The McIDAS grid file number must be between 1 and 9999\n");
  170.       exit(0);
  171.    }
  172.  
  173.    sprintf(mcname, "GR3D%04d", n );
  174.  
  175.    write_gridfile( mcname );
  176.  
  177.  
  178.    /* verify it by printing the header... */
  179.    print_header( mcname );
  180.  
  181.    /* ...and printing the info describing the first 4 grids: */
  182.    for (i=1;i<5;i++) {
  183.       printf("\ngrid %d:\n", i);
  184.       print_gridinfo( mcname, i );
  185.    }
  186.  
  187. }
  188.  
  189.  
  190.  
  191. /*
  192.   NOTES ON FILE FORMAT:
  193.    char must be 1 byte.
  194.    int must be 4 bytes.
  195.    float must be 4 bytes.
  196.  
  197.    Overall structure:
  198.       Let nt = number of time steps
  199.       Let np = number of physical variables or parameters
  200.  
  201.       part 1:  A 5-D header (256 bytes)
  202.       part 2:  nt*np 3-D grid info headers (256 bytes each)
  203.       part 3:  extra space for future headers
  204.       part 4:  nt*np 3-D grids of data (each data point is a 4-byte float)
  205. */
  206.  
  207.  
  208. /* McIDAS 5-D grid file header:  this is the very first structure in the file.
  209.    (size is 64 words or 256 bytes)
  210. */
  211.  
  212. struct header {
  213. /*  1 */   char ident[32];       /* file description/identification */
  214. /*  9 */   int  nproj;           /* Project number? */
  215. /* 10 */   int  creation_date;   /* Date created in YYDDD format */
  216. /* 11 */   int  maxsize;         /* number of data points in largest 3D grid */
  217. /* 12 */   int  numgrid;         /* number of 3D grids in this file */
  218. /* 13 */   int  firstgrid;       /* location of first grid's data as offset */
  219.                          /*   in 4-byte words from start of file */
  220. /* 14 */   int  pad[51];
  221. };
  222.  
  223.  
  224.  
  225. /* McIDAS 3-D grid info block:  one of these is associated with each
  226.    3-D sub-grid in the 5-D file.  (size is 64 words or 256 bytes)
  227. */
  228.  
  229. struct gridinfo {
  230. /*  1 */   int  size;            /* number of data points / words of data */
  231.                          /*   size should be equal to nr * nc * nl */
  232. /*  2 */   int  nr;              /* number of rows */
  233. /*  3 */   int  nc;              /* number of columns */
  234. /*  4 */   int  nl;              /* number of levels */
  235. /*  5 */   int  iword;           /* location of data as offset in 4-byte words
  236.                                         from start of file */
  237. /*  6 */   int  date;            /* grid date stamp in YYDDD format */
  238. /*  7 */   int  time;            /* grid time stamp in HHMMSS format */
  239. /*  8 */   int  pad1;
  240. /*  9 */   char name[4];         /* 4-character variable or parameter name */
  241. /* 10 */   char units[4];        /* 4-character units description */
  242. /* 11 */   int  pad2[11];
  243. /* 22 */   int  itype;           /* always 4 */
  244. /* 23 */   int  latn;            /* north latitude * 10000 */
  245. /* 24 */   int  lonw;            /* west longitude * 10000 */
  246. /* 25 */   int  latinc;          /* latitude increment * 10000 */
  247. /* 26 */   int  loninc;          /* longitude increment * 10000 */
  248. /* 27 */   int  pad3[4];
  249. /* 31 */   int  ihtype;          /* always 1 */
  250. /* 32 */   int  top;             /* top altitude * 1000 */
  251. /* 33 */   int  topinc;          /* altitude increment * 1000 */
  252. /* 34 */   int  pad4[31];
  253. };
  254.  
  255.  
  256.  
  257.  
  258. /* This function writes a 5-D grid file given the info in the above
  259.    global variables and constants.
  260. */
  261. write_gridfile( filename )
  262. char filename[];
  263. {
  264.    struct header head;
  265.    struct gridinfo info;
  266.    FILE *f;
  267.    int it, ip, n, bytes, c;
  268.    float inc;
  269.  
  270.    /* initialize grid header */
  271.    strncpy( head.ident, identification, 31 );
  272.    head.nproj = 0;
  273.    head.creation_date = 1;
  274.    head.maxsize = NR * NC * NL;
  275.    head.numgrid = NTIMES * NPARAMS;
  276.    head.firstgrid = (NTIMES * NPARAMS + 1) * 64;
  277.  
  278.    /* Open file */
  279.    f = fopen( filename, "w" );
  280.    if (f) {
  281.       /* Write grid file header */
  282. #ifdef LITTLE
  283.       flip4( &head, &head, 32/4 );  /* if little endian flip char string */
  284. #endif
  285.       if (write_int4_array( f, &head, 64 )!=64) {  /* not really integers */
  286.  fprintf( stderr, "Error while writing to grid file.\n" );
  287.  fclose(f);
  288.  exit(-1);
  289.       }
  290.  
  291.       /* Write grid info blocks */
  292.       for (it=0;it<NTIMES;it++) {
  293.  for (ip=0;ip<NPARAMS;ip++) {
  294.     info.size = NR * NC * NL;
  295.     info.nr = NR;
  296.     info.nc = NC;
  297.     info.nl = NL;
  298.     info.iword = sizeof(struct header) / 4
  299.                  + NTIMES * NPARAMS * sizeof(struct gridinfo) / 4
  300.  + it * NPARAMS * NR * NC * NL
  301.  + ip * NR * NC * NL;
  302.     info.date = grid_date[it];
  303.     info.time = grid_time[it];
  304.     /* pad name and unit strings with spaces if needed */
  305.     for (c=0;c<4;c++) {
  306.        if (param_name[ip][c]==0)
  307.   param_name[ip][c] = ' ';
  308.        if (param_unit[ip][c]==0)
  309.   param_unit[ip][c] = ' ';
  310.     }
  311.     strncpy( info.name, param_name[ip], 4 );
  312.     strncpy( info.units, param_unit[ip], 4 );
  313.     info.itype = 4;
  314.     info.ihtype = 1;
  315.     info.latn = NORTH_LATITUDE * 10000;
  316.     info.lonw = WEST_LONGITUDE * 10000;
  317.     info.top = TOP_ALTITUDE * 1000;
  318.     inc = (NORTH_LATITUDE-SOUTH_LATITUDE) / (NR-1);
  319.     info.latinc = (int) (inc * 10000.0);
  320.     inc = (WEST_LONGITUDE-EAST_LONGITUDE) / (NC-1);
  321.     info.loninc = (int) (inc * 10000.0);
  322.     inc = (TOP_ALTITUDE-BOTTOM_ALTITUDE) / (NL-1);
  323.     info.topinc = (int) (inc * 1000.0);
  324.  
  325.     /* Write gridinfo struct */
  326. #ifdef LITTLE
  327.     /* flip char strings */
  328.     flip4( &info.name, &info.name, 1 );
  329.     flip4( &info.units, &info.units, 1 );
  330. #endif
  331.     if (write_int4_array( f, &info, 64 )!=64) {
  332. /*    if (n!=1) {*/
  333.        fprintf(stderr,"Error while writing grid info.\n");
  334.        fclose(f);
  335.        exit(-1);
  336.     }
  337.  }
  338.       }
  339.  
  340.       /* Write grid data */
  341.       bytes = NR * NC * NL * 4;
  342.       for (it=0;it<NTIMES;it++) {
  343.  for (ip=0;ip<NPARAMS;ip++) {
  344.     if (write_bytes( f, grid[it][ip], bytes)!=bytes) {
  345.        fprintf( stderr, "Error while writing grid data.\n" );
  346.        fclose(f);
  347.        exit(-1);
  348.     }
  349.  }
  350.       }
  351.  
  352.       /* all done */
  353.       fclose(f);
  354.    }
  355.    else {
  356.       fprintf( stderr, "Unable to open output file: %s\n", filename );
  357.       exit(-1);
  358.    }
  359.  
  360. }
  361.  
  362.  
  363.  
  364.  
  365. /* This function prints the header information in a 5-D grid file given
  366.    a filename.
  367. */
  368. print_header( filename )
  369. char *filename;
  370. {
  371.    FILE *f;
  372.    struct header  head;
  373.    int b;
  374.  
  375.    f = fopen(filename, "r");
  376.    if (f) {
  377.       if (read_int4_array( f, &head, 64 )==64) {
  378.  /* flip character strings if on little endian */
  379. #ifdef LITTLE
  380.  flip4( &head, &head, 32/4 );  /* if little endian flip char string */
  381. #endif
  382.          printf("       identifier: %32s\n", head.ident);
  383.          printf("       project id: %d\n", head.nproj);
  384.          printf("    creation date: %d\n", head.creation_date);
  385.          printf("max 3-D grid size: %d\n", head.maxsize);
  386.          printf("     no. of grids: %d\n", head.numgrid);
  387.          printf("first grid offset: %d\n", head.firstgrid);
  388.       }
  389.       else {
  390.          printf("error reading header.\n");
  391.       }
  392.       fclose(f);
  393.    }
  394.    else {
  395.       printf("Unable to open %s\n", filename );
  396.    }
  397. }
  398.  
  399.  
  400.  
  401. /* This function prints the grid information corresponding to grid 'gridno'
  402.    in a 5-D grid file.
  403. */
  404. print_gridinfo( filename, gridno )
  405. char *filename;
  406. int gridno;
  407. {
  408.    FILE *f;
  409.    struct gridinfo info;
  410.    int b, bytes, offset;
  411.  
  412.    f = fopen(filename, "r");
  413.    if (f) {
  414.       bytes = sizeof( struct gridinfo );
  415.       offset = gridno * bytes;
  416.       if (fseek( f, offset, 0)==0) {
  417.  b = fread( &info, bytes, 1, f);
  418.  if (b==1) {
  419.     printf("   size: %d\n", info.size );
  420.     printf("     nr: %d\n", info.nr );
  421.     printf("     nc: %d\n", info.nc );
  422.     printf("     nl: %d\n", info.nl );
  423.     printf("  iword: %d\n", info.iword );
  424.     printf("   date: %d\n", info.date);
  425.     printf("   time: %d\n", info.time);
  426.             printf("   name: %c%c%c%c\n",
  427.    info.name[0], info.name[1], info.name[2], info.name[3]);
  428.             printf("  units: %c%c%c%c\n",
  429.    info.units[0], info.units[1], info.units[2], info.units[3]);
  430.     printf("   LatN: %f\n", (float) info.latn / 10000.0 );
  431.     printf(" LatInc: %f\n", (float) info.latinc / 10000.0  );
  432.     printf("   LonW: %f\n", (float) info.lonw / 10000.0  );
  433.     printf(" LonInc: %f\n", (float) info.loninc / 10000.0 );
  434.     printf("    Top: %f\n", (float) info.top / 1000.0 );
  435.     printf(" TopInc: %f\n", (float) info.topinc / 1000.0 );
  436.  }
  437.  else {
  438.     printf("error reading info.\n");
  439.  }
  440.       }
  441.       else {
  442.  printf("error seeking.\n");
  443.       }
  444.       fclose(f);
  445.    }
  446.    else {
  447.       printf("Unable to open %s\n", filename );
  448.    }
  449. }
  450.  
  451.