📄 xsilfield.cc
字号:
if(DEBUG) { printf("xsilField::writeAsFormat\n"); } if (streamFormat == "Text") { // dump data as ascii file char datFileName[64]; sprintf(datFileName,"%s%li.dat",datFileNameBase,iD); FILE *tempfile=fopen(datFileName,"w"); // write header row (this doesn't work for matlab!) if(format != FORMAT_MATLAB) { for(list<XMLString>::const_iterator pXMLString = variableNamesList.begin(); pXMLString != variableNamesList.end(); pXMLString++) { fprintf(tempfile," %s ",pXMLString->c_str()); } } // write data fprintf(tempfile,"%s",data.c_str()); fclose(tempfile); // initialise variables list<XMLString>::iterator pXMLString = variableNamesList.begin(); for(unsigned long i=0;i<variableNamesList.size();i++) { // need to format variable names to remove any non alpha-numeric characters pXMLString->goLatinAlphaNumeric(); char tempString[64]; sprintf(tempString,"_%li",iD); *pXMLString += tempString; if(nIndependentVariables==0) { fprintf(outfile,"%s = zeros(1,1);\n",pXMLString->c_str()); } else if(nIndependentVariables==1) { if(i==0) { fprintf(outfile,"temp_d1 = zeros(1,%li);\n",lattice(0)); } else { fprintf(outfile,"%s = zeros(1,%li);\n",pXMLString->c_str(),lattice(0)); } } else { if(i<nIndependentVariables) { fprintf(outfile,"temp_d%li = zeros(%li",i+1,lattice(nIndependentVariables-1)); } else { fprintf(outfile,"%s = zeros(%li",pXMLString->c_str(),lattice(nIndependentVariables-1)); } for(unsigned long j=nIndependentVariables-1;j>0;j--) { fprintf(outfile,",%li",lattice(j-1)); } fprintf(outfile,");\n"); } if(i<nIndependentVariables) { fprintf(outfile,"%s = zeros(1,%li);\n",pXMLString->c_str(),lattice(i)); } pXMLString++; } fprintf(outfile,"\n"); // load in temp file if(format==FORMAT_SCILAB) { fprintf(outfile,"%s%li = fscanfMat('%s');\n",datFileNameBase,iD,datFileName); } else { fprintf(outfile,"load %s -ascii\n",datFileName); } for(unsigned long i=0;i<nIndependentVariables;i++) { fprintf(outfile,"temp_d%li(:) = %s%li(:,%li);\n",i+1,datFileNameBase,iD,i+1); } for(unsigned long i=nIndependentVariables;i<variableNamesList.size();i++) { fprintf(outfile,"%s(:) = %s%li(:,%li);\n",variableName(i)->c_str(),datFileNameBase,iD,i+1); } // work out coordinates for(unsigned long i=0;i<nIndependentVariables;i++) { fprintf(outfile,"%s(:) = temp_d%li(",variableName(i)->c_str(),i+1); if(i==(nIndependentVariables-1)) { fprintf(outfile,":"); } else { fprintf(outfile,"1"); } for(unsigned long j=nIndependentVariables-1;j>0;j--) { if((j-1)==i) { fprintf(outfile,",:"); } else { fprintf(outfile,",1"); } } fprintf(outfile,");\n"); } fprintf(outfile,"\n"); // clear excess variables fprintf(outfile,"clear %s%li",datFileNameBase,iD); for(unsigned long i=0;i<nIndependentVariables;i++) { fprintf(outfile," temp_d%li",i+1); } fprintf(outfile,"\n"); fprintf(outfile,"\n"); } else if (streamFormat == "Binary") { if (format == FORMAT_MATLAB) { // work out how matlab will interpret endian-ness std::string machineFormat; if (binEncoding == "BigEndian") { machineFormat = "ieee-be"; } else if (binEncoding == "LittleEndian") { machineFormat = "ieee-le"; } else { machineFormat = "native"; } fprintf(outfile,"fpDat = fopen('%s','r','%s');\n",binDatFname.c_str(),machineFormat.c_str()); fprintf(outfile,"if (fpDat < 0)\n"); fprintf(outfile," disp('Cannot open binary data file: ',%s)\n",binDatFname.c_str()); fprintf(outfile," return\n"); fprintf(outfile,"end\n"); unsigned long int k = 0; for(list<XMLString>::iterator pXMLString = variableNamesList.begin(); pXMLString != variableNamesList.end(); pXMLString++) { char tempString[64]; sprintf(tempString,"_%li",iD); *pXMLString += tempString; if (k < nIndependentVariables) { fprintf(outfile,"%sLen = fread(fpDat,1,'ulong');\n",pXMLString->c_str()); fprintf(outfile,"%s = fread(fpDat,%sLen,'%s');\n",pXMLString->c_str(),pXMLString->c_str(),binPrecision.c_str()); } else if (k >= nIndependentVariables) { fprintf(outfile,"%sLen = fread(fpDat,1,'ulong');\n",pXMLString->c_str()); if (nIndependentVariables == 1) { fprintf(outfile,"%s = fread(fpDat,%sLen,'%s');\n",pXMLString->c_str(), variableNamesList.begin()->c_str(),binPrecision.c_str()); } else if (nIndependentVariables == 2) { fprintf(outfile,"%s = fread(fpDat,[%sLen,%sLen],'%s');\n",pXMLString->c_str(), (++variableNamesList.begin())->c_str(),variableNamesList.begin()->c_str(),binPrecision.c_str()); } else if (nIndependentVariables > 2) { // now we need to create a multi-dimensional matrix, and this is harder to do... // we need to read in a matrix-sized (ie 2D) block at a time, and append this to the other dimensions // the number of independent variables determines the dimensions of the N-D matrix to produce // construct the for loop to loop over the third and subsequent dimensions list<XMLString>::iterator pIndepVars = variableNamesList.begin(); for (unsigned long int inumIndepVars=2; inumIndepVars<nIndependentVariables; inumIndepVars++) { fprintf(outfile, "for index%li = 1:%sLen\n",inumIndepVars-2,pIndepVars->c_str()); pIndepVars++; } // generate the first part of the string, which is the array to be assigned into fprintf(outfile, "%s(:,:,",pXMLString->c_str()); pIndepVars = variableNamesList.begin(); for (unsigned long int inumIndepVars=2; inumIndepVars<nIndependentVariables; inumIndepVars++) { fprintf(outfile, "index%li",inumIndepVars-2); // need to append a comma if not the last index to append if (inumIndepVars+3 != nIndependentVariables) { fprintf(outfile,","); } } // generate the fread statement // to do this, I have to work out what the last and second-to-last independent variable names are // this is because, for some reason, one can't inspect a given element of a list // first the last one pIndepVars = variableNamesList.begin(); XMLString lastIndepVar; for (unsigned long int inumIndepVars=0; inumIndepVars<nIndependentVariables; inumIndepVars++) { lastIndepVar = *pIndepVars; pIndepVars++; } // now the second to last one XMLString secondLastIndepVar; pIndepVars = variableNamesList.begin(); for (unsigned long int inumIndepVars=1; inumIndepVars<nIndependentVariables; inumIndepVars++) { secondLastIndepVar = *pIndepVars; pIndepVars++; } fprintf(outfile, ") = fread(fpDat,[%sLen,%sLen],'%s');\n",lastIndepVar.c_str(), secondLastIndepVar.c_str(),binPrecision.c_str()); // finish off the for loop for (unsigned long int inumIndepVars=2; inumIndepVars<nIndependentVariables; inumIndepVars++) { fprintf(outfile,"end\n"); } } } k++; } // clean up a bit fprintf(outfile,"clear fpDat "); for(list<XMLString>::iterator pXMLString = variableNamesList.begin(); pXMLString != variableNamesList.end(); pXMLString++) { fprintf(outfile, "%sLen ",pXMLString->c_str()); } for (unsigned long int inumIndepVars=2; inumIndepVars<nIndependentVariables; inumIndepVars++) { fprintf(outfile, "index%li ",inumIndepVars-2); } fprintf(outfile, "\n"); } else if (format == FORMAT_SCILAB) { // as far as I can tell, scilab can't handle binary data input, // so barf, and tell why, and give some alternative. printf("\nFatal error: Sorry, but at the time of this version of xmds,\n" "scilab cannot handle binary input. To be able to use scilab,\n" "please change your output format to ascii (in the <output> tag).\n" "Exiting...\n"); exit(254); } else { throw(xmdsException("Unknown format. I only accept Matlab or Scilab at present\n")); } } else { throw(xmdsException("Stream format is neither Text or Binary, something has seriously gone wrong!\n")); }};// ******************************************************************************// ******************************************************************************// xsilField private// ******************************************************************************// ******************************************************************************// ******************************************************************************unsigned long xsilField::lattice( const unsigned long& index) const { if(DEBUG) { printf("xsilField::lattice\n"); } if(index>=latticeList.size()) { throw xmdsException("Internal range error in xsilField::lattice"); } list<unsigned long>::const_iterator pULong = latticeList.begin(); for(unsigned long i=0; i<index; i++) { pULong++; } return *pULong;};// ******************************************************************************const XMLString* xsilField::variableName( const unsigned long& index) const { if(DEBUG) { printf("xsilField::varaibleName\n"); } if(index>=variableNamesList.size()) { throw xmdsException("Internal range error in xsilField::variableName"); } list<XMLString>::const_iterator pXMLString = variableNamesList.begin(); for(unsigned long i=0; i<index; i++) { pXMLString++; } return &*pXMLString;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -