- 积分
- 41
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-7-24
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2013-1-30 13:34:00
|
显示全部楼层
请高人指点,目前我想要的效果已经基本可以实现了,即:
1)求历年12个月数据各个月的平均值,求完放在 mdatas数组中(mdatas 为 GridData[12])。
2)创建result.nc文件,希望将计算结果(即历年12个月数据各个月的平均值mdatas)保存到新的文件中。nc的格式需要同原来的数据格式相同。
注:目前我将每个月的数据都存为一个nc文件没有问题。可是不知道如何将12个月的数据都存在一个文件中。
先贴代码再解释我的做法。
-
- NetCDFDataInfo aDataInfo = new NetCDFDataInfo();
- string aFile = Application.StartupPath + "\\Sample\\Hadisst_obs_1949-2010_T106_BCC_AGCM2.1.nc";
- //读取nc文件
- aDataInfo.ReadDataInfo(aFile);
- var t = aDataInfo.times;
- List<GridData>[] datas = new List<GridData>[12];
- int sYear = aDataInfo.times[0].Year;
- int eYear = aDataInfo.times[aDataInfo.times.Count - 1].Year;
- int yIndex = 0;
- GridData gData = new GridData();
- //循环年份
- for (int startYear = 1961; startYear < eYear; startYear++)
- {
- yIndex = startYear - sYear;
- //将每年12个月的数据都摆在相应月份的数组里
- for (int mIndex = 0; mIndex < 12; mIndex++)
- {
- List<GridData> mList = datas[mIndex];
- if (mList == null)
- {
- mList = new List<GridData>();
- }
- mList.Add(aDataInfo.GetGridData_LonLat(yIndex * 12 + mIndex, 4, 0));
- datas[mIndex] = mList;
- }
- }
- GridData[] mdatas = new GridData[12];
- //对每个月份的数据计算平均值
- for (int mIndex = 0; mIndex < 12; mIndex++)
- {
- List<GridData> mList = datas[mIndex];
- mdatas[mIndex] = DataMath.Average(mList, false);
- }
- NetCDFDataInfo gridFile = new NetCDFDataInfo();
- string outFilePath = Application.StartupPath + "\\Sample\\result.nc";
- gridFile.fileName = outFilePath;
- gridFile.UNDEF = aDataInfo.UNDEF;
- gridFile.unlimdimid = aDataInfo.unlimdimid;
- // Add dimensions:
- foreach (var dimension in aDataInfo.dimList)
- {
- gridFile.AddDimension(dimension);
- }
- object[] lonDatas = new object[160];
- object[] latDatas = new object[320];
- // Add variables
- foreach (Variable variable in aDataInfo.varList)
- {
- gridFile.AddVariable((Variable)variable.Clone());
- //Add variable attributes
- foreach (var attribute in variable.Attributes)
- {
- gridFile.AddVariableAttribute(variable.Name, attribute.attName, attribute.attValue.ToString());
- }
- if (variable.Name == "lon")
- {
- aDataInfo.GetVarData(variable, ref lonDatas);
- }
- if (variable.Name == "lat")
- {
- aDataInfo.GetVarData(variable, ref latDatas);
- }
- }
- //Add global attributes
- foreach (var globalAttribute in aDataInfo.gAttList)
- {
- gridFile.AddGlobalAttribute(globalAttribute.attName, globalAttribute.attValue.ToString());
- }
- //Create netCDF file
- gridFile.CreateNCFile(outFilePath);
- gridFile.WriteVar("lon", lonDatas);
- gridFile.WriteVar("lat", latDatas);
- //Add Time
- int[] startArray = new int[1];
- int[] countArray = new int[1];
- double[] dataArray = new double[12];
- for (int mIndex = 0; mIndex < 12; mIndex++)
- {
- dataArray[mIndex] = mIndex;
- }
- startArray[0] = 0;
- countArray[0] = 1;
- gridFile.WriteVara("time", startArray, countArray, dataArray);
- //Add Data
- int[] startdataArray = new int[3];
- int[] countdataArray = new int[3];
- startdataArray[0] = 0;
- startdataArray[1] = 0;
- startdataArray[2] = 0;
- countdataArray[0] = 12;
- countdataArray[1] = aDataInfo.Y.Length;
- countdataArray[2] = aDataInfo.X.Length;
-
- //创建一个可以盛放12个月数据的数组(从这个地方开始就不知道对不对了)
- double[] dataResult = new double[aDataInfo.X.Length * aDataInfo.Y.Length * 12];
- int dataIndex = 0;
- //循环12个月的数据
- for (int mIndex = 0; mIndex < 12; mIndex++)
- {
- //获取每个月的数据
- var ddt = mdatas[mIndex].Data;
- //将每个月的数据转换为数组赋值
- for (int y = 0; y < aDataInfo.Y.Length; y++)
- {
- for (int x = 0; x < aDataInfo.X.Length; x++)
- {
- dataResult[dataIndex] = ddt[y, x];
- dataIndex++;
- }
- }
- }
- // gridFile.w
- gridFile.WriteVara("SST_cpl", startdataArray, countdataArray, dataResult);
- gridFile.CloseNCFile();
各位看官请看源码的最下面,我开始测试,如果我这么写:
-
- //Add Data
- int[] startdataArray = new int[3];
- int[] countdataArray = new int[3];
- startdataArray[0] = 0;
- startdataArray[1] = 0;
- startdataArray[2] = 0;
- countdataArray[0] = 12;
- countdataArray[1] = aDataInfo.Y.Length;
- countdataArray[2] = aDataInfo.X.Length;
- //创建一个可以盛放数据的数组
- double[] dataResult = new double[aDataInfo.X.Length * aDataInfo.Y.Length ];
- int dataIndex = 0;
- //获取1月份的数据
- var ddt = mdatas[0].Data;
- //将每个月的数据转换为数组赋值
- for (int y = 0; y < aDataInfo.Y.Length; y++)
- {
- for (int x = 0; x < aDataInfo.X.Length; x++)
- {
- dataResult[dataIndex] = ddt[y, x];
- dataIndex++;
- }
- }
- // 写变量数据
- gridFile.WriteVara("SST_cpl", startdataArray, countdataArray, dataResult);
目前最大的问题就是多个时间尺度的数据如何保存。
startdataArray, countdataArray, dataResult三个变量都是怎么设置的?
说实话我都是依葫芦画瓢,希望王老师或其他高手能再给点指导。
|
|