00001 #include "FlowData.h"
00002 #include <math.h>
00003 #include "reverseBytes.h"
00004
00005 FlowData::FlowData()
00006 {
00007
00008 for(int i = 0; i < max_channels; i++)
00009 {
00010 channels[i] = NULL;
00011 freeChannel[i] = true;
00012 }
00013 }
00014
00015 FlowData::~FlowData()
00016 {
00017
00018 for(int i = 0; i < max_channels; i++)
00019 if (!freeChannel[i])
00020 deleteChannel(i);
00021 }
00022
00023 bool FlowData::loadDataset(string filename, bool bigEndian)
00024 {
00025 FILE* griFile = NULL;
00026 FILE* datFile = NULL;
00027 char header[40];
00028
00029
00030 int lastdot = filename.find_last_of(".",filename.length()-1);
00031 if (lastdot != string::npos)
00032
00033 filename = filename.substr(0,lastdot);
00034
00035
00036
00037
00038 string griName = filename+".gri";
00039
00040 std::cout << "- Loading grid file '" << griName << "' ... " << std::endl;
00041
00042 griFile = fopen(griName.c_str(),"rb");
00043
00044 if (!griFile)
00045 {
00046 std::cerr << "+ Error loading grid file:" << griName << std::endl << std::endl;
00047 return false;
00048 }
00049
00050 fread(header,40,1,griFile);
00051
00052 if (!geometry.readFromFile(header,griFile,bigEndian))
00053 return false;
00054
00055 fclose(griFile);
00056
00057 int dimX,dimY,dimZ,numChannels;
00058 float DT;
00059
00060 sscanf(header,"SN4DB %d %d %d %d %d %f",&dimX,&dimY,&dimZ,&numChannels,×teps,&DT);
00061 printf("Channels: %d\nTimesteps: %d\n",numChannels,timesteps);
00062
00063
00064
00065
00066
00067 char suffix[16];
00068 sprintf(suffix,".%.5u.dat",0);
00069 string datName = filename.append(suffix);
00070 std::cout << "- Loading data file '" << datName << "' ... " << std::endl;
00071
00072 datFile = fopen(datName.c_str(),"rb");
00073 if (!datFile)
00074 {
00075 std::cerr << "+ Error loading dat file:" << datName << std::endl << std::endl;
00076 return false;
00077 }
00078
00079 numChannels += 3;
00080 int* ch = new int[numChannels];
00081
00082
00083
00084 float* tmpArray = new float[numChannels*geometry.getDimX()*geometry.getDimY()];
00085 int result = fread(tmpArray,sizeof(float),numChannels*geometry.getDimX()*geometry.getDimY(),datFile);
00086
00087 if (result != numChannels*geometry.getDimX()*geometry.getDimY())
00088 {
00089 std::cerr << "+ Error reading dat file:" << datName << std::endl << std::endl;
00090 return false;
00091 }
00092
00093 fclose(datFile);
00094
00095
00096 if (bigEndian)
00097 for(int j = 0; j < numChannels*geometry.getDimX()*geometry.getDimY(); j++)
00098 tmpArray[j] = reverseBytes<float>(tmpArray[j]);
00099
00100 for (int j = 0; j < (numChannels); j++)
00101 {
00102
00103 ch[j] = createChannel();
00104
00105 channels[ch[j]]->copyValues(tmpArray,(numChannels),j);
00106 }
00107 delete[] ch;
00108 delete[] tmpArray;
00109 return true;
00110 }
00111
00112 int FlowData::createChannel()
00113 {
00114
00115 int i = 0;
00116 while ((!freeChannel[i])&&(i < max_channels)) i++;
00117
00118 if (i < max_channels)
00119 {
00120 std::cout << "Creating channel at " << i << " ... ";
00121
00122 channels[i] = new FlowChannel(&geometry);
00123
00124 freeChannel[i] = false;
00125
00126 return i;
00127 }
00128
00129 else
00130 {
00131 return -1;
00132 std::cerr << "There is no free channel slot!" << std::endl;
00133 }
00134 }
00135
00136 void FlowData::deleteChannel(int i)
00137 {
00138
00139 if (!freeChannel[i])
00140 {
00141 std::cout << "Deleting channel at " << i << " ... ";
00142
00143 delete channels[i];
00144 channels[i] = NULL;
00145
00146 freeChannel[i] = true;
00147 }
00148 else std::cout << "Tried to delete a non-existing channel at " << i << "." << std::endl;
00149 }
00150
00151 FlowChannel* FlowData::getChannel(int i)
00152 {
00153 return channels[i];
00154 }
00155
00156 int FlowData::createChannelGeometry(int dimension)
00157 {
00158 int result = createChannel();
00159
00160 channels[result]->copyValues((float*)geometry.geometryData, 3, dimension);
00161 return result;
00162 }
00163
00164 int FlowData::createChannelVectorLength(FlowChannel* chX, FlowChannel* chY, FlowChannel* chZ)
00165 {
00166 int result = createChannel();
00167
00168 if (chZ)
00169 for (int i = 0; i < geometry.getDimX()*geometry.getDimY(); i++)
00170
00171 channels[result]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i) + chZ->getValue(i)*chZ->getValue(i)));
00172 else
00173 for (int i = 0; i < geometry.getDimX()*geometry.getDimY(); i++)
00174 channels[result]->setValue(i,sqrt(chX->getValue(i)*chX->getValue(i) + chY->getValue(i)*chY->getValue(i)));
00175
00176 return result;
00177 }
00178
00179 int FlowData::createChannelVectorLength(int chX, int chY, int chZ)
00180 {
00181
00182 if (chZ >= 0)
00183 return createChannelVectorLength(getChannel(chX),getChannel(chY),getChannel(chZ));
00184 else return createChannelVectorLength(getChannel(chX),getChannel(chY));
00185 }
00186
00187 int FlowData::getNumTimesteps()
00188 {
00189 return timesteps;
00190 }