/// <summary> /// 写入seg-y文件 /// </summary> /// <param name="sgy">要写入的sgy文件结构体</param> /// <param name="filename">要保存的绝对路径名</param> /// <returns>写入成功返回true,否则返回false</returns> public static bool Write(SGY_data sgy, string filename) { FileStream ws; try { ws = new FileStream(filename, FileMode.CreateNew); } catch (IOException) { DialogResult result = MessageBox.Show("文件已存在,是否创建?", "警告!", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { ws = new FileStream(filename, FileMode.Create); } else { return(false); } } ws.Write(Encoding.ASCII.GetBytes(sgy.Head_f), 0, 3200); ws.Write(StructToBytes(sgy.head_se), 0, 400); for (int i = 0; i < sgy.head_se.ntrpr; i++) { ws.Write(StructToBytes(sgy.daoju[i].daotou), 0, 240); ws.Write(ArrayToBytes(sgy.daoju[i].sgynum), 0, sgy.head_se.hns * 4); } ws.Close(); return(true); }
/// <summary> /// 打开seg-y文件 /// </summary> /// <param name="file_name">要打开sgy文件的绝对路径</param> /// <param name="read">seg-y文件结构体,保存打开后的sgy文件所有信息,引用类型(前加“ref”)</param> /// <returns>打开成功返回true,否则返回false</returns> public static bool Open(string file_name, ref SGY_data read) { try { if (File.Exists(file_name) == false) { MessageBox.Show("文件不存在!!"); return(false); } FileStream rs = new FileStream(file_name, FileMode.Open); byte[] read_tmp; read_tmp = new byte[3200]; rs.Read(read_tmp, 0, 3200); read.Head_f = System.Text.Encoding.ASCII.GetString(read_tmp); read.head_se = new Struct_400(); read.head_se.hunass = new short[170]; read_tmp = new byte[400]; IntPtr tmp = Marshal.AllocHGlobal(400); rs.Read(read_tmp, 0, 400); Marshal.Copy(read_tmp, 0, tmp, 400); read.head_se = (Struct_400)Marshal.PtrToStructure(tmp, typeof(Struct_400)); Marshal.FreeHGlobal(tmp); read.daoju = new SU_data[read.head_se.ntrpr]; for (int i = 0; i < read.head_se.ntrpr; i++) { read_tmp = new byte[240]; tmp = Marshal.AllocHGlobal(240); rs.Read(read_tmp, 0, 240); Marshal.Copy(read_tmp, 0, tmp, 240); read.daoju[i].daotou = (Struct_240)Marshal.PtrToStructure(tmp, typeof(Struct_240)); Marshal.FreeHGlobal(tmp); read_tmp = new byte[read.head_se.hns * 4]; tmp = Marshal.AllocHGlobal(read.head_se.hns * 4); read.daoju[i].sgynum = new float[read.head_se.hns]; rs.Read(read_tmp, 0, read.head_se.hns * 4); Marshal.Copy(read_tmp, 0, tmp, read.head_se.hns * 4); Marshal.Copy(tmp, read.daoju[i].sgynum, 0, read.head_se.hns); Marshal.FreeHGlobal(tmp); } rs.Close(); return(true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(false); } }
/// <summary> /// 合并文件函数 /// </summary> /// <param name="heb">seg-y文件结构体,引用类型(前加“ref”)</param> /// <param name="pianzi">sgy数据偏移量,从每道数据的第pianzi个数据开始合并</param> /// <param name="filname">要合并的sgy文件的绝对路径,变长参数</param> /// <returns>合并成功返回true,否则返回false</returns> public static bool hebing(ref SGY_data heb, int pianzi, params string[] filname) { short j = 0, ntrp_num = 0, ntrpr_tmp = 0; ushort hns_max; try { SGY_data[] fil = new SGY_data[filname.Length]; foreach (string i in filname) { if (Open(i, ref fil[j]) == true) { ++j; } } foreach (SGY_data i in fil) { ntrp_num += (short)i.head_se.ntrpr; } hns_max = fil[0].head_se.hns; foreach (SGY_data i in fil) { hns_max = (hns_max > i.head_se.hns) ? hns_max : i.head_se.hns; } heb.Head_f = fil[0].Head_f; heb.head_se = fil[0].head_se; heb.daoju = new SU_data[ntrp_num]; heb.head_se.ntrpr = heb.head_se.nart = ntrp_num; heb.head_se.hns = heb.head_se.nso = hns_max; heb.head_se.format = 5; for (int i = 0, f = 0; i < heb.head_se.ntrpr; ++i) { heb.daoju[i].sgynum = new float[heb.head_se.hns]; for (int k = 0; k < heb.head_se.hns; ++k) { do { if (i < (fil[f].head_se.ntrpr + ntrpr_tmp)) { if ((k + pianzi) < fil[f].head_se.hns) { heb.daoju[i].sgynum[k] = fil[f].daoju[i - ntrpr_tmp].sgynum[k + pianzi]; } break; } else { ntrpr_tmp += fil[f].head_se.ntrpr; ++f; } } while (true); } heb.daoju[i].daotou = fil[f].daoju[i - ntrpr_tmp].daotou; heb.daoju[i].daotou.tracl = i; heb.daoju[i].daotou.tracr = i + 1; } return(true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(false); } }