Re: Retrieving values at select coordinates from HDF Swath data

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Sat Apr 07 2012 - 14:34:48 MDT

The following was modified from an old script. It is the 2nd method.
It has not been tested in its current form:

undef("nearSwathVal")
function nearSwathVal(x[*][*], lat[*][*], lon[*][*], LAT[1], LON[1])
;
; find the location of the swath that is closest to LAT, LON
; return the value, the swath lat/lon, subscripts (indices) of grid
;
local x1d, lat1d, lon1d, gcdist, gcdmin, i1d, ni1d, ji, n, xval
begin
   x1d = ndtooned(x)
   lat1d = ndtooned(lat)
   lon1d = ndtooned(lon)
                                         ; calculate great circle distances
   gcdist = gc_latlon(LAT,LON,lat1d,lon1d,2,4)
   gcdmin = min(gcdist)

   i1d = ind(gcdist.eq.gcdmin) ; could be more than one value
   ji = ind_resolve(i1d, dimsizes(lat))

  ;print(i1d)
  ;print(lat1d(i1d)+" "+lon1d(i1d)+" "+x1d(i1d))
  ;print(ji)

   ni1d = dimsizes(i1d)
   do n=0,ni1d-1 ; loop
      if (.not.ismissing( x1d(i1d(n)) )) ; return the 1st valid value
          xval = x1d(i1d(n)) ; value
          xval@latval = lat1d(i1d(n)) ; pixel nearest location
          xval@lonval = lon1d(i1d(n))
          xval@jy = ji(n,0) ; indices of nearest pixel
          xval@ix = ji(n,1)
          break ; exit loop
      end if
   end do

   return(xval)
end

;================> MAIN <==============

    f = addfile("...", "r")
    x = f->Variable
    lat2d = f->Latitude
    lon2d = f->Longitude

    lat_pt= ...
    lon_pt= ...

    z = nearSwathVal(x,lat2d,lon2d,lat_pt,lon_pt)
    print(z)

Good Luck

On 4/6/12 4:35 PM, Farren L. Herron-Thorpe wrote:
> I would like to build a function that reports the value of an HDF swath
> data variable at a specific coordinate location (over and over again for
> different files). Since swath data has different pixel locations (and
> sizes) for every file, so I cannot simply report the value of a
> predefined grid location.
>
> I have 2 methods in mind that would both work: 1) I can grid the data
> first, and then report the value from known (i,j) location, or 2) I can
> index the lat2d and lon2d attributes, calculate the distance of each
> pixel from my coordinate of interest, and then pick the value from the
> pixel that is closest.
>
> Option 1 can lead to slight variance in data values, and seems too
> intensive for such a simple need. Option 2 would be less intensive, and
> more accurate, but I'm hoping someone might know of a simpler method?
>
> In other words, is there a way for NCL to find a grid point location in
> swath data that falls over a specific coordinate so that a single value
> can be reported?
>
> Thank you for your insight!
> -Farren
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sat Apr 7 14:34:57 2012

This archive was generated by hypermail 2.1.8 : Mon Apr 09 2012 - 13:43:03 MDT