Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
readhdf.cpp
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /*
54 Reads a CSAR rocket .hdf input file, an HDF4 format,
55 and passes it to a blockConsumer. Returns an
56 error string on errors; returns NULL on sucess.
57 
58 Orion Sky Lawlor, olawlor@acm.org, 6/8/2001
59 */
60 #include <stdio.h>
61 #include "gridutil.h"
62 
63 /*HDF4 include file*/
64 #include "mfhdf.h"
65 
66 #define chk(x) checkHDFerr(x,__FILE__,__LINE__)
67 static int checkHDFerr(int errCode,const char *fName,int lineNo) {
68  if (errCode==-1) {
69  fprintf(stderr,"HDF I/O Error in (%s:%d)\n",
70  fName,lineNo);
71  exit(23);
72  }
73  return errCode;
74 }
75 
76 const char *read_hdf(const char *filename,blockConsumer &dest)
77 {
78  int32 i, d;
79  //Open the input file
80  int32 sd_id = SDstart(filename, DFACC_READ);
81  if (sd_id==-1) return "Couldn't open HDF input file!";
82 
83  //Select the X, Y, and Z components
84  int32 sds_id[3];
85  for (i=0;i<3;i++) sds_id[i]=chk(SDselect(sd_id,5+4*i));
86 
87  //Determine the ni, nj, and nk dimensions
88  blockDim dim;
89  /* This doesn't work.
90  for (d=0;d<3;d++) {
91  int32 dim_id = chk(SDgetdimid(sds_id[0],d));
92  char dim_name[200];
93  int32 dim_size, data_type, n_attrs;
94  chk(SDdiminfo(dim_id, dim_name, &dim_size, &data_type, &n_attrs));
95  dim[2-d]=dim_size;
96  }
97  */
98  char name[200];
99  int32 rank, size[3], data_type, n_attrs;
100  chk(SDgetinfo(sds_id[0], name, &rank, size, &data_type, &n_attrs));
101  dim[0]=size[2];
102  dim[1]=size[1];
103  dim[2]=size[0];
104 
105  //Allocate storage for the X, Y, and Z output
106  vector3d *locs=dest.allocateBlock(dim);
107 
108  //Read each of X, Y, and Z
109  int nLocs=dim.getSize();
110  double *coord=new double[nLocs];
111  for (i=0;i<3;i++)
112  {
113  int32 start[3]={0,0,0};
114  int32 edges[3];
115  for (d=0;d<3;d++) edges[d]=dim[2-d];
116  chk(SDreaddata(sds_id[i], start, NULL, edges, coord));
117  //Copy this coordinate into locs
118  for (int l=0;l<nLocs;l++) locs[l][i]=coord[l];
119  }
120  delete[] coord;
121 
122  //Pass locations to consumer
123  dest.consume(dim,locs);
124  dest.freeBlock(locs);
125 
126  //Close the file and return
127  for (i=0;i<3;i++) chk(SDendaccess(sds_id[i]));
128  chk(SDend(sd_id));
129  return NULL;
130 }
131 
132 
const NT & d
int coord[NPANE][NROW *NCOL][3]
Definition: blastest.C:86
const char * read_hdf(const char *hdfName, blockConsumer &dest)
Definition: readhdf.cpp:76
void int int int REAL REAL REAL const char * fName
Definition: write.cpp:76
#define chk(x)
Definition: readhdf.cpp:66
virtual vector3d * allocateBlock(blockDim &dim)
Definition: read.cpp:146
virtual void freeBlock(vector3d *blk)
Definition: read.cpp:150
blockLoc i
Definition: read.cpp:79
static int rank
Definition: advectest.C:66
int getSize(void) const
Definition: gridutil.h:126
virtual const char * consume(const blockDim &dim, vector3d *locs)=0
static int checkHDFerr(int errCode, const char *fName, int lineNo)
Definition: readhdf.cpp:67