您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > swmm out结果二进制解析(C#)
publicpartialclassForm1:Form{//[DllImport(swmmh5.dll,EntryPoint=swmm_open)]//privatestaticexternintswmm_open(stringinpFile,stringrptFile,stringoutFile);//[DllImport(swmmh5.dll,EntryPoint=swmm_start)]//privatestaticexternintswmm_start(intnum);//[DllImport(swmmh5.dll,EntryPoint=GetSwmmResult)]//privatestaticexternintGetSwmmResult(intiType,intiIndex,intvIndex,intperiod,floatvalue);//[DllImport(swmmh5.dll,EntryPoint=CloseSwmmOutFile)]//privatestaticexternvoidCloseSwmmOutFile();//读取思路://1)判断out文件是否为完整结构,判断原则:magic1的值和magic2的值不等,或者错误码不为0,或者时间点总数为0//2)读取各要素的IDnames(idname的个数、idname的字符)//3)读取各要素输入的部分属性变量,subcatchment:area,node:nodetypecode、nodeinvertelevation、nodemax.depth,link:linktypecode、upstreaminvertoffset、downstreaminvertoffset、linkmax.depth、(linklength//4)读取计算的结果值,每个步长为一个片段,片段由两部分组成:报告输出的时间(年月日时分秒),各要素各属性的值constintRECORDSIZE=4;intversion;intNflowUnits;intNsubcatch;intNnodes;intNlinks;intNpolluts;publicForm1(){InitializeComponent();}//读取out文件结果privateintOpenSwmmOutFile(stringfile){intmagic1,magic2,version;interr;intstartPos;//数据开始位置intnPeriods;//报告阶段总数(时间点总数)interrCode;//错误码intIDpos;//id开始的位置intpropertyPos;//属性开始的位置stringpollutantUnit;//污染物单位//定义各要素id数组int[]subcatchmentIDArray;int[]nodeIDArray;int[]linkIDArray;int[]pollutantIDArray;//打开output文件,小于14个字节,返回常数BinaryReaderbr=newBinaryReader(newFileStream(file,FileMode.Open));if(br==null||br.BaseStream.LengthRECORDSIZE*14){br.Close();err=1;}//从文件末尾读取参数,依次为:数据开始位置、时间点总数、错误码、magic2br.BaseStream.Seek(-RECORDSIZE*6,SeekOrigin.End);IDpos=br.ReadInt32();propertyPos=br.ReadInt32();startPos=br.ReadInt32();nPeriods=br.ReadInt32();errCode=br.ReadInt32();magic2=br.ReadInt32();//开始的4个字节为magic1的值br.BaseStream.Seek(0,SeekOrigin.Begin);magic1=br.ReadInt32();//如果magic1的值和magic2的值不等,或者错误码不为0,或者时间点总数为0,则关闭退出。if(magic1!=magic2||errCode!=0||nPeriods==0){err=1;}else{err=0;}if(err==1){br.Close();returnerr;}//接下来读取额外的参数,依次为:版本号、单位、汇水区总数、节点总数、管线总数、污染物总数version=br.ReadInt32();NflowUnits=br.ReadInt32();Nsubcatch=br.ReadInt32();Nnodes=br.ReadInt32();Nlinks=br.ReadInt32();Npolluts=br.ReadInt32();//**********-----------读取ID列表,id开始的位置为idPos------------**********//IDnames由两部分组成:1)该ID所包含的字符数量,以4字节存储,2)组成该ID的每一个字符,以1字节存储br.BaseStream.Seek(IDpos,SeekOrigin.Begin);subcatchmentIDArray=newint[Nsubcatch];nodeIDArray=newint[Nnodes];linkIDArray=newint[Nlinks];pollutantIDArray=newint[Npolluts];for(inti=0;iNsubcatch;i++){//读取subcatchmentIDNames包含多少个字符intnumSubIdNames=br.ReadInt32();//subcatchmentIDArray[i]=br.ReadInt32();//读取组成ID的name字符串,NumSubIdNames个charbyte[]subcatchByte=newbyte[numSubIdNames];for(intcount=0;countnumSubIdNames;count++){subcatchByte[count]=br.ReadByte();}stringsubcatchName=System.Text.Encoding.Default.GetString(subcatchByte);txtSubcatchID.Text+=subcatchName+\r\n;}for(inti=0;iNnodes;i++){intnumNodeIdNames=br.ReadInt32();byte[]nodeByte=newbyte[numNodeIdNames];for(intcount=0;countnumNodeIdNames;count++){nodeByte[count]=br.ReadByte();}stringnodeName=System.Text.Encoding.Default.GetString(nodeByte);//txtSubcatchID.Text+=nodeName+\r\n;}for(inti=0;iNlinks;i++){intnumLinkIdNames=br.ReadInt32();byte[]linkByte=newbyte[numLinkIdNames];for(intcount=0;countnumLinkIdNames;count++){linkByte[count]=br.ReadByte();}stringlinkName=System.Text.Encoding.Default.GetString(linkByte);//txtSubcatchID.Text+=linkName+\r\n;}for(inti=0;iNpolluts;i++){intnumPollutsNames=br.ReadInt32();byte[]pollutsByte=newbyte[numPollutsNames];for(intcount=0;countnumPollutsNames;count++){pollutsByte[count]=br.ReadByte();}//stringpollutsName=System.Text.Encoding.Default.GetString(pollutsByte);}//再往下读的内容为污染物单位switch(br.ReadInt32()){case0:pollutantUnit=mg/L;break;case1:pollutantUnit=ug/L;break;case2:pollutantUnit=counts/L;break;}///**********------------读取各要素property-----------**********br.BaseStream.Seek(propertyPos,SeekOrigin.Begin);//获取subcatch属性的个数intnumSubcatProperty=br.ReadInt32();//获取node属性的个数intoffsetTemp1=numSubcatProperty*Nsubcatch;br.BaseStream.Seek((offsetTemp1+1)*4,SeekOrigin.Current);intnumNodeProperty=br.ReadInt32();//获取link属性的个数intoffsetTemp2=numNodeProperty*Nnodes;br.BaseStream.Seek((offsetTemp2+3)*4,SeekOrigin.Current);intnumLinkProperty=br.ReadInt32();ListintsubcatchProNameList=newListint();ListfloatsubcatchProValueList=newListfloat();ListintnodeProNameList=newListint();ListfloatnodeProValueList=newListfloat();ListintlinkProNameList=newListint();ListfloatlinkProValueList=newListfloat();//获取subcatch属性br.BaseStream.Seek(propertyPos+4,SeekOrigin.Begin);subcatchProNameList.Add(br.ReadInt32());for(inti=0;iNsubcatch*1;i++){subcatchProValueList.Add(br.ReadSingle());txtSubcatchPro.Text+=subcatchProValueList[i].ToString()+\r\n;}//获取node属性br.ReadInt32();nodeProNameList.Add(br.ReadInt32());nodeProNameList.Add(br.ReadInt32());nodeProNameList.Add(br.ReadInt32());for(inti=0;iNnodes*3;i++){nodeProValueList.Add(br.ReadSingle());}//获取link属性br.ReadInt32();linkProNameList.Add(br.ReadInt32());linkProNameList.Add(br.ReadInt32());linkProNameList.Add(br.ReadInt32());linkProNameList.Add(br.ReadInt32());lin
本文标题:swmm out结果二进制解析(C#)
链接地址:https://www.777doc.com/doc-5985571 .html