Recipe: IDL program to read data from downloaded file: Difference between revisions

Pchakrab (talk | contribs)
Pchakrab (talk | contribs)
Line 62: Line 62:
==== Modifications to read a subset of the data ====
==== Modifications to read a subset of the data ====


The above python code can be easily modified to read a subset of the data instead. For example, to read temperature data inside the box bounded by latitudes 25<sup>o</sup>N, 50<sup>o</sup>N and longitudes -130<sup>o</sup>N, -65<sup>o</sup>N, we will replace line 15 in the code above by
The above IDL script can be easily modified to read a subset of the data instead. The modified code, to read temperature data inside the box bounded by latitudes 25<sup>o</sup>N, 50<sup>o</sup>N and longitudes -130<sup>o</sup>W, -65<sup>o</sup>W, is


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
T = rootgrp.variables['T'][0,:,229:280,99:230]
; file
file = 'c1440_NR.inst01hr_3d_T_Cv.20060918_0900z.nc4'
 
; read data from file
ncid = ncdf_open(file)
; bounding box
;  lons = -130:0.5:-65
;  lats =  25:0.5:50
imin = round((-130. + 180.)/0.5) - 1
imax = round(( -65. + 180.)/0.5) - 1
jmin = round((  25 +  90.)/0.5) - 1
jmax = round((  50 +  90.)/0.5) - 1
; corresponding array sizes
im = imax-imin+1
jm = jmax-jmin+1
lm = 72 ; read all 72 levels
; start and count vectors
offset = [imin, jmin, 0, 0]
count = [im, jm, lm, 1]
ncdf_varget, ncid, 'T', T, count=count, offset=offset
ncdf_close, ncid
 
; max/min of T
print, 'max(T):', max(T)
print, 'min(T):', min(T)
 
; plot T at the surface
lons = findgen(im)*0.5-130.
lats = findgen(jm)*0.5+25.
basemap = map('Cylindrical Equal Area', limit=[25., -130., 50., -65.])
cntrplot = contour( $
          T[*,*,71], $ ;; level=71 => surface
          lons, $
          lats, $
          grid_units=2, $ ;; degrees
          n_levels=20, $
          rgb_table=34, $
          ;; /fill, $
          /overplot, $
          title='surface air temperature' $
                  )
m1 = mapcontinents(/countries)
 
END
</syntaxhighlight>
</syntaxhighlight>


The conversion of lat/lon co-ordinates to indices is left as an exercise for the user.
Running the modified script, produces the text output
max(T):      305.605
min(T):      191.696


Running the modified script, produces the text output
and the plot
rootgrp.variables['T'].shape (1, 72, 361, 720)
Reading T... done.
T.shape: (72, 51, 131)
min(T): 191.6956
max(T): 305.6048


== See Also ==
== See Also ==