0

I'm using a NASA GISS netcdf file with gridded monthly temperature values. According to the readme file "Missing data is flagged with a value of 9999.f" I am trying to plot the data but keep getting blank maps. I think its because this 9999.f value is throwing off my scale. How do I replace it with Nan? I tried:

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

data2 = Dataset(r'GriddedAir250.nc')

lats=data2.variables['lat'][:]
lons=data2.variables['lon'][:]
time=data2.variables['time'][:]
air=data2.variables['air'][:]

air=air.astype('float')
air[air==9999]=np.nan

But it looks like this gives me an array of boolean values: enter image description here

Megan Martin
  • 221
  • 1
  • 9
  • It will be easier for someone to help you if you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). What library are you using to read the file? Don't make us guess; include all the relevant `import` statements that you are using. – Warren Weckesser Feb 14 '22 at 19:21
  • 1
    Thanks, I just went back and added my import statements to the question – Megan Martin Feb 14 '22 at 19:30

1 Answers1

0

netCDF4 creates masked arrays, and automatically masks the value 9999.0. In your code, this means the result of air = data2.variables['air'][:] is a masked array. So I suspect the problem is that the plotting code that you are trying to use does not handle masked arrays. If you think the plotting code can handle nan, you could try

air = air.filled(fill_value=np.nan)

This will convert air to a regular NumPy array, with the masked values (i.e. the values that were originaly 9999.0 in the .nc file) converted to nan.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214