简体   繁体   English

通过Python读取.nc文件

[英]Reading .nc files by Python

I want to read .nc files by netCDF4 (using conda) but my files look like this: something.3520_nc 我想通过netCDF4(使用conda)读取.nc文件,但我的文件如下所示: something.3520_nc

  1. What to do with this, it's still .nc file right? 该怎么办,它仍然是.nc文件吗? Should I rename it? 我应该重命名吗? Or after I downloaded it, is it important do save it on some special place? 或者在下载后,将其保存在某个特殊的位置上很重要吗?

  2. Can anyone show me, how to begin that code? 谁能告诉我如何开始该代码? I found some pages about this problem but it didn't work for me. 我找到了一些有关此问题的页面,但对我而言不起作用。 I have Windows 10, conda using my_root environment and I have the netCDF4 package. 我有使用my_root环境的Windows 10 conda,并且有netCDF4软件包。

I tried to write: 我试图写:

from netCDF4 import Dataset
import numpy as np

f = Dataset('test.nc')

I was suggested to use instead of numpy pylab, but I don't have that package and I don't know, how to install it. 建议我使用它而不是numpy pylab,但是我没有该软件包,我也不知道如何安装它。 I struggle with the third line the most, because every page I have visited wrote it differently. 我最不愿意与第三行打交道,因为我访问过的每一页写的都不一样。

My question is maybe quite complex and I admit I have a little experience with the Python. 我的问题可能很复杂,我承认我对Python有一点经验。

If you go and look at the documentation for your package, you will find a useful page here . 如果您去查看软件包的文档 ,则会在此处找到有用的页面。

So there you find 所以你在那里找到

Static methods 静态方法

def __init__( self, filename, mode="r", clobber=True, diskless=False, persist=False, keepweakref=False, format='NETCDF4') Dataset constructor. def __init __(self,filename,mode =“ r”,clobber = True,diskless = False,persist = False,keepweakref = False,format ='NETCDF4')数据集构造函数。

it takes these arguments. 需要这些参数。 I've only kept the important ones here. 我只将重要的信息保留在这里。 Go read the page for the rest 去阅读其余的页面

filename: Name of netCDF file to hold dataset. filename:用于保存数据集的netCDF文件的名称。 Can also be a python 3 pathlib instance or the URL of an OpenDAP dataset. 也可以是python 3 pathlib实例或OpenDAP数据集的URL。

format : underlying file format (one of 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_CLASSIC', 'NETCDF3_64BIT_OFFSET' or 'NETCDF3_64BIT_DATA'. Only relevant if mode = 'w' (if mode = 'r','a' or 'r+' the file format is automatically detected). Default 'NETCDF4', which means the data is stored in an HDF5 file, using netCDF 4 API features. Setting format='NETCDF4_CLASSIC' will create an HDF5 file, using only netCDF 3 compatible API features. netCDF 3 clients must be recompiled and linked against the netCDF 4 library to read files in NETCDF4_CLASSIC format. 'NETCDF3_CLASSIC' is the classic netCDF 3 file format that does not handle 2+ Gb files. 'NETCDF3_64BIT_OFFSET' is the 64-bit offset version of the netCDF 3 file format, which fully supports 2+ GB files, but is only compatible with clients linked against netCDF version 3.6.0 or later. 'NETCDF3_64BIT_DATA' is the 64-bit data version of the netCDF 3 file format, which supports 64-bit dimension sizes plus unsigned and 64 bit integer data types, bu 格式 :基础文件格式(“ NETCDF4”,“ NETCDF4_CLASSIC”,“ NETCDF3_CLASSIC”,“ NETCDF3_64BIT_OFFSET”或“ NETCDF3_64BIT_DATA”之一。仅当mode ='w'时才相关(如果mode ='r','a'或'r + (自动检测到文件格式)。默认为“ NETCDF4”,即使用netCDF 4 API功能将数据存储在HDF5文件中。设置format ='NETCDF4_CLASSIC'将仅使用与netCDF 3兼容的API功能创建HDF5文件。 .netCDF 3客户端必须重新编译并链接到netCDF 4库,以读取NETCDF4_CLASSIC格式的文件。“ NETCDF3_CLASSIC”是经典的netCDF 3文件格式,不能处理2+ Gb文件。“ NETCDF3_64BIT_OFFSET”是64位偏移版本netCDF 3文件格式的一种,完全支持2 GB以上的文件,但仅与链接到netCDF 3.6.0或更高版本的客户端兼容。“ NETCDF3_64BIT_DATA”是netCDF 3文件格式的64位数据版本64位尺寸大小加上无符号和64位整数数据类型bu t is only compatible with clients linked against netCDF version 4.4.0 or later. t仅与链接到netCDF 4.4.0或更高版本的客户端兼容。

So, what does this mean. 那么这是什么意思。

  1. You need to determine what format you are using. 您需要确定所使用的格式。 If you are using NETCDF4 then you can ignore this, but if not you need to specify your format. 如果使用的是NETCDF4则可以忽略它,但是如果没有,则需要指定格式。

  2. You need to make certain that your file path that you pass works. 您需要确保传递的文件路径有效。 To do this, try this test by making a file in the same directory as your existing code and hopefully your data 为此,请通过在与现有代码相同的目录中创建一个文件来尝试进行此测试,并希望您的数据

test_file.py test_file.py

import os

# this gets your current working directory, that is from the perspective of the module that you are running.
where_am_i = os.getcwd()

print(where_am_i)


my_file = "something.3520_nc"

if os.path.exists(my_file):
    print("Yep, I can read that file!")
else:
    print("Nope, the path doesn't reach your file. Go research filepath in python")

my_new_path = os.path.join('/the/absolute/path/to/file', my_file)

if os.path.exists(my_new_path):
    print("Yep, I can read that file!")
else:
    print("Nope, the path doesn't reach your file. Go research filepath in python")

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

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