Search code examples
pythonnetcdfnetcdf4

netcdf (.nc) file only has -999 values?


I'm trying to analyze rainfall data from https://imdpune.gov.in/Clim_Pred_LRF_New/Grided_Data_Download.html, specifically the 'Gridded Rainfall (0.25 x 0.25) NetCDF' Files.

I used netCDF4 to load the data but I get this peculiar result:

from netCDF4 import Dataset
import numpy as np 

path = '2013.nc' 
f = Dataset(path ,'r')
rain = np.array(f.variables['RAINFALL'][:,:,:])

print(rain[0][0])

output:

[-999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999. -999.
 -999. -999. -999.]

NOTE: There isn't a problem with this specific file, as I get the same result with the data for every year. You can try it out yourself.

Am I loading the data in python wrong is there something up with the data that I downloaded? The latitude and longitude variables don't give me this problem though


Solution

  • from pathlib import Path
    import numpy as np
    import matplotlib.pyplot as plt
    
    # file import 
    path = Path('../../Downloads/')
    file = 'Clim_Pred_LRF_New_RF25_IMD0p252013.nc' 
    f = Dataset(path / file, 'r')
    
    # select variable "Rainfall"
    rain = np.array(f.variables['RAINFALL'][:,:,:]) 
    # assign NaN value
    rain[rain==-999.] = np.nan  
    
    # plot dataset for selected date (2013-01-18)    
    plt.pcolormesh(rain[17])
    plt.colorbar()
    

    The data looks fine.

    But check out the shape of rain:

    print(rain.shape)
    (365, 129, 135) 
    # --> (days, latitude, longitude).
    

    With rain[0][0] you're selecting the first time step (Jan-01) and the first latitude (6.5), which will return a 1D array with NaN values only because all pixels are outside of India. Selecting the second and third dimension with 0 will always return arrays with -999 exclusively. You can try for example rain[17][100]!

    print(rain[17][100]
    array([        nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,  1.2962954 ,  0.99620295,  2.705826  ,
            9.772656  , 11.187045  ,  0.        ,  3.0871496 ,  5.298688  ,
           18.108587  , 34.280876  , 35.034836  , 41.41929   , 38.952507  ,
           63.49905   , 92.398125  , 92.92495   , 80.57031   , 63.660515  ,
           45.413662  ,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan,
                   nan,         nan,         nan,         nan,         nan],
          dtype=float32)