Elsri
Elsri

Reputation: 49

OS.stat on array dataset returns ValueError: stat: embedded null character in path

I have a big netCDF4 array dataset of which I want to get the basic information (type, attributes and dimensions sizes). In the original NCAR Command Level code (which I am rewriting to Python3) the function printVarSummary() was used to extract this information. I understood os.stat() should be able to do the same.

However, when I try it I get the error:

return os.stat(filename).st_size
ValueError: stat: embedded null character in path

My python code looks like this:

T2_input = np.ma.core.MaskedArray()
with Dataset(dir + 'filename.nc') as file_T2:
    T2_raw = file_T2.variables['T2MEAN'][strt * 8 : (strt + doy) * 8 - 1, :, :] - 273.15
    dimsizes_T2 = T2_raw.shape
    T2_input = T2_raw[4 : (dimsizes_T2[0]-4) : 8, :, :]  
print(os.stat(T2_input))

The original ncl code looks like this:

f_T2=addfile(dir + "filename.nc","r")
T2_raw = f_T2->T2MEAN(strt*8:(strt+doy)*8-1,:,:)-273.15
dsizes_T2 = dimsizes(T2_raw)
T2_input = T2_raw(4:(dsizes_T2(0)-4):8,:,:)
delete(T2_raw)
printVarSummary(T2_input)

What causes the embedded null characters in python?

Upvotes: 0

Views: 92

Answers (1)

Elsri
Elsri

Reputation: 49

Solved the issue. Since the datafile is a masked array, the numpy.ma attributes need to be used to get the information. Following code works:

T2_raw = np.ma.core.MaskedArray()
T2_input = np.ma.core.MaskedArray()
with Dataset(dir + 'filename.nc') as file_T2:
                                                                                                                                                                
    T2_raw = file_T2.variables['T2MEAN'][strt * 8 : (strt + doy) * 8 - 1, :, :] - 273.15
    dimsizes_T2 = T2_raw.shape
    T2_input = T2_raw[4 : (dimsizes_T2[0]-4) : 8, :, :]                                                                                               
del T2_raw

print("Data type:", end=" "), print(T2_input.dtype)
print("Number of array dimensions:", end=" "), print(temp_at_2m_input.ndim)
print("Number of array elements:", end=" "), print(temp_at_2m_input.size)

Upvotes: 0

Related Questions