Tuple related error

Would anyone please help me with this Tuple related error?

Here is my code:

if A == 1
   DL = 11;           
elseif A == 2
   DL = 21;                  
end
ds3 = NCDataset("$(P0)/Input/TSO2/xxx.nc");
T = ds3["abc"][:,:,DL]; 
close(ds3);

The variable abc is a 3-dimensional array with the size of 360x180x33.

Here is the error message:

**ERROR:** LoadError: BoundsError: attempt to access Tuple{Colon, Colon, Int64} at index [4]
Stacktrace:
[1] **getindex**
@ ./tuple.jl:29 [inlined]
[2] **ncsub2**
@ ~/.julia/packages/NCDatasets/st9Jz/src/variable.jl:436 [inlined]
[3] **getindex(**::NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, ::Colon, ::Colon, ::Int64**)**
@ NCDatasets ~/.julia/packages/NCDatasets/st9Jz/src/variable.jl:484
[4] **getindex(**::CommonDataModel.CFVariable{Union{Missing, Float32}, 4, NCDatasets.Variable{Float32, 4, NCDataset{Nothing}}, NCDatasets.Attributes{NCDataset{Nothing}}, NamedTuple{(:fillvalue, :missing_values, :scale_factor, :add_offset, :calendar, :time_origin, :time_factor), Tuple{Float32, Tuple{}, Vararg{Nothing, 5}}}}, ::Colon, ::Colon, ::Int64**)**
@ CommonDataModel ~/.julia/packages/CommonDataModel/pO4st/src/cfvariable.jl:390
[5] top-level scope

Many thanks.

Can you post a working example for testing? (see this post on making it easier to get help)

Just looking at the error, I would wager a guess that the issue is your code is trying to access some element in the NETCDF file that doesn’t exist.

1 Like

Many thanks for the reply.

I’m able to make it work by changing the code to below:

ds3 = NCDataset("$(P0)/Input/TSO2/xxx.nc");
T = ds3["abc"][:,:,:]; 
close(ds3);
T = T[:,:,DL];

This is so weird. Do you know why Julia is so picky here?

1 Like

First I’ll just note that this doesn’t seem to be a “Julia” thing, meaning its not the language itself but rather the specifics of the package providing NCDataset.

I really don’t know the answer, but if I had to guess, maybe in the initial code you were trying to access the data before it was read, whereas in the solution the data has been read into variable T making it possible to index? :man_shrugging:

It would probably be worth looking in the NCDataset documentation to see if there is a recommended way to do accomplish this task.

1 Like

The stack-traces indicates that the variable is actually 4D dimensional:

The exact output of size(ds3["abc"]) would be useful. In the case it its size is (360,180,33,1), you can also use ds3["abc"][:,:,DL,1].