user8060881
user8060881

Reputation:

Reading .nc files by Python

I want to read .nc files by netCDF4 (using conda) but my files look like this: something.3520_nc

  1. What to do with this, it's still .nc file right? 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.

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. 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.

Upvotes: 1

Views: 13732

Answers (1)

Shawn Mehan
Shawn Mehan

Reputation: 4568

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.

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. Can also be a python 3 pathlib instance or the URL of an OpenDAP dataset.

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, but is only compatible with clients linked against netCDF version 4.4.0 or later.

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.

  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

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")

Upvotes: 1

Related Questions