Sama K
Sama K

Reputation: 19

extracting data from nc files using scipy

I am trying to extract values of some parameters from an nc file, but my code doesn't work for the slp parameter, though it works for the other parameters. The code is showing me negative values for slp which means the importing of the data is not correct.

I used this script:

import numpy as np
import matplotlib as mpl
mpl.use('agg')
from mpl_toolkits.basemap import Basemap
from scipy.io import netcdf_file as nc
from pylab import *

from datetime import datetime
from datetime import timedelta
from glob import glob


files1 = glob('n1*nc')
files1.sort()
MVAL1 = -32767
for item in files1:
  clf()
  f1 = nc(item)
  times=f1.variables['time'] 
  lon = f1.variables['longitude'].data
  lat = f1.variables['latitude'].data
  slp = f1.variables['msl'].data   
  # to convert to hpa
  slp = slp*0.01
  dims = slp.shape                   
  print slp.shape, np.max(slp), np.min(slp)

The results are

(73, 121) 327.67 -327.66

while when I check it in Panoply the min and max values are different.

How do I solve this problem?

Upvotes: 1

Views: 1087

Answers (1)

Deditos
Deditos

Reputation: 1415

I suspect that Panoply is automatically applying the scale_factor and add_offset attributes to the short data and presenting the results to you as floats in the physical units (Pa). By default scipi.io does not apply those attributes, so you'll need to do it manually:

slp = f1.variables['msl']
fslp = slp[:]*slp.scale_factor + slp.add_offset

In your case the possible range of the data will be approximately -32766*scale + offset = 99081. Pa to 32767*scale + offset = 101718. Pa.

It's quite common for higher level, convention aware packages (e.g., Panoply, Ferret) to apply these packing attributes automatically and for lower level interfaces (e.g., Scipy.IO) to leave it to the user. You might consider using the NetCDF4 Python package instead, which handles packing and fill values automatically.

Upvotes: 2

Related Questions