简体   繁体   English

在netCDF文件中创建和存储多维数组

[英]Creating and Storing Multi-Dimensional Array in a netCDF File

This question has potentially two parts but maybe only one if the first part can be encapsulated by the second. 这个问题可能有两个部分,但如果第一部分可以由第二部分封装,则可能只有一个部分。 I am using python with numpy and netCDF4 我在numpy和netCDF4中使用python

First: 第一:

I have four lists of different variable values (hereafter referred to elevation values) each of which has a length of 28. These four lists are one set of 5 different latitude values of which are one set of the 24 different time values. 我有四个不同变量值(以下称为高程值)的列表,每个变量的长度为28。这四个列表是一组5种不同的纬度值,其中一组是24种不同的时间值。

So 24 times...each time with 5 latitudes...each latitude with four lists...each list with 28 values. 因此,有24次...每次使用5个纬度...每个纬度有四个列表...每个列表具有28个值。

I want to create an array with the following dimensions (elevation, latitude, time, variable) 我想创建一个具有以下尺寸(高度,纬度,时间,变量)的数组

In words, I want to be able to specify which of the four lists I access,which index in the list, and specify a specific time and latitude. 换句话说,我希望能够指定要访问的四个列表中的哪个,列表中的哪个索引,并指定特定的时间和纬度。 So an index into this array would look like this: 因此,对该数组的索引如下所示:

array(0,1,2,3) where 0 specifies the first index of the the 4th list specified by the 3. 1 specifies the 2nd latitude, and 2 specifies the 3rd time and the output is the value at that point. array(0,1,2,3),其中0指定由3指定的第4个列表的第一个索引。1指定第二个纬度,而2指定第3个时间,输出是该点的值。

I won't include my code for this part since literally the only things of mention are the lists 我不会在这部分中包含我的代码,因为从字面上仅提及的是列表

list1=[...] list1 = [...]

list2=[...] list2 = [...]

list3=[...] list3 = [...]

list4=[...] list4 = [...]

How can I do this, is there an easier structure of the array, or is there anything else I a missing? 我该怎么做?是否有更简单的数组结构?或者我还缺少其他什么吗?

Second: 第二:

I have created a netCDF file with variables with these four dimensions. 我用这四个维度的变量创建了一个netCDF文件。 I need to set those variables to the array structure made above. 我需要将这些变量设置为上述数组结构。 I have no idea how to do this and the netCDF4 documentation does a 1-d array in a fairly cryptic way. 我不知道如何执行此操作,netCDF4文档以相当隐秘的方式完成了一维数组。 If the arrays can be made directly into the netCDF file bypassing the need to use numpy first, by all means show me how. 如果可以将数组直接放入netCDF文件中,而无需先使用numpy,则请一定向我展示如何操作。

Thanks! 谢谢!

After talking to a few people where I work we came up with this solution: 与我工作的几个人交谈之后,我们提出了以下解决方案:

First we made an array of zeroes using the following argument: 首先,我们使用以下参数创建一个零数组:

array1=np.zeros((28,5,24,4)) array1 = np.zeros((28,5,24,4))

Then appended this array by specifying where in the array we wanted to change: 然后通过指定要在数组中更改的位置来附加此数组:

array1[:,0,0,0]=list1 array1 [:,0,0,0] = list1

This inserted the values of the list into the first entry in the array. 这会将列表的值插入到数组的第一个条目中。

Next to write the array to a netCDF file, I created a netCDF in the same program I made the array, made a single variable and gave it values like this: 接下来,将数组写入netCDF文件,我在制作数组的同一程序中创建了netCDF,制作了一个变量,并为其提供了如下值:

netcdfvariable[:]=array1 netcdfvariable [:] = array1

Hope that helps anyone who finds this. 希望对发现这个问题的任何人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM