1

I am using a list of integers corresponding to an x,y index of a gridded NetCDF array to extract specific values, the initial code was derived from here. My NetCDF file has a single dimension at a single timestep, which is named TMAX2M. My code written to execute this is as follows (please note that I have not shown the call of netCDF4 at the top of the script):

# grid point lists
lat = [914]
lon = [2141]

# Open netCDF File
fh = Dataset('/pathtofile/temperaturedataset.nc', mode='r')

# Variable Extraction
point_list = zip(lat,lon)
dataset_list = []
for i, j in point_list:
    dataset_list.append(fh.variables['TMAX2M'][i,j])

print(dataset_list)

The code executes, and the result is as follows:

masked_array(data=73,mask=False,fill_value=999999,dtype=int16]

The data value here is correct, however I would like the output to only contain the integer contained in "data". The goal is to pass a number of x,y points as seen in the example linked above and join them into a single list.

Any suggestions on what to add to the code to make this achievable would be great.

2 Answers 2

1

The solution to calling the particular value from the x,y list on single step within the dataset can be done as follows:

dataset_list = []
for i, j in point_list:
    dataset_list.append(fh.variables['TMAX2M'][:][i,j])

The previous linked example contained [0,16] for the indexed variables, [:] can be used in this case.

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest converting to NumPy array like this:

for i, j in point_list:
    dataset_list.append(np.array(fh.variables['TMAX2M'][i,j]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.