Re: [netcdf-java] Reading grib2 with LambertConformal_Projection -> lat / lng


Harold,

I use my own projection code for most of the projected grids that Panoply 
handles, but I have also written versions for all of them that use the Unidata 
netCDF library's projection code. My method for getting the projection from the 
Unidata code is a bit different from yours, mostly because the various 
projections I have implemented extend from an abstract projection handler. That 
abstract class doesn't need to know what the specific projection is; it just 
works on a ProjectionImpl class, and LambertConformal extends from 
ProjectionImpl.



    private ProjectionImpl njProj;

    private void initUnidataProj ( )
    {
        final VariableDS njvarDS = ...;

        final List<CoordinateSystem> csList = njvarDS.getCoordinateSystems ( );

        if (csList == null || csList.isEmpty ( ))
        { throw new RuntimeException ("No coordinate systems found."); }

        final CoordinateSystem cs = csList.get (0);

        //  Get coordinate system projection. CS will return LatLonProjection 
if no projection detected but
    //  there is an auxiliary 2D grid in place.
        njProj = cs.getProjection ( );

    //  If projection is not Lambert Conformal Conic, that makes us unhappy.
        if (! (njProj instanceof LambertConformal))
        {
            throw new RuntimeException ("CS returned " + njProj.getClass ( ) + 
" instead of LambertConformal");
        }
        }

And then as you indicate, call njproj.projToLatLon to translate grid 
coordinates to lon-lat pairs.

But your final question is the opposite direction, given a lon-lat pair, to 
find the data value at that location.

When gridding such data, Panoply creates an x-y grid using the projected grid 
coordinates. It then creates an output lon-lat grid, and calls lonLatToProj on 
each of the points in that grid, takes each returned x-y pair, and finds the 
nearest data point in the x-y grid to that pair. Since it is doing this an 
array of locations rather than just looking up single points on demand, there's 
some optimization in place.

rbs







--
Robert B. Schmunk
Webmaster / Senior Systems Programmer
NASA Goddard Institute for Space Studies
2880 Broadway, New York, NY 10025
 

-----Original Message-----
From: <netcdf-java-bounces@xxxxxxxxxxxxxxxx> on behalf of Harold 
<harold@xxxxxxxxxxxxxx>
Date: Tuesday, June 18, 2019 at 12:38
To: "netcdf-java@xxxxxxxxxxxxxxxx" <netcdf-java@xxxxxxxxxxxxxxxx>
Subject: [netcdf-java] Reading grib2 with LambertConformal_Projection -> lat    
/ lng

    Hello,
    
    
    First, thanks for this lib; it is very helpful and I don't know where we'd 
be without it.
    
    
    I encountered my first netcdf (.grib2) files recently, and hope to access 
the data therein to verify the output of another system.
    
    
    Here's an example file, if that helps:
     - 
    https://s3.us-east-2.amazonaws.com/tech.public.data/grib2-test/3600.grib2 
<https://s3.us-east-2.amazonaws.com/tech.public.data/grib2-test/3600.grib2>
    
    
    I found PanoplyJ, which works great, and summarizes the file like this:
     - https://i.imgur.com/VJXnpWH.png
    
    
    
    I am using the java libs `edu.ucar/cdm` and `edu.ucar/grib`, both at 
version 4.6.13 - which also work great and open the file without trouble.
    
    
    My intention is to verify the contents of this file against a different 
dataset which expresses surface location with lat/lng, while this grib2 file 
has x/y (under a Lambert conformal mapping).
    
    
    The path I found through the lib yesterday is:
    1) NetcdfDataset/openDataset
    2) .getVariables and store a reference to the `LambertConformal_Projection` 
variable
    3) Construct a `LambertConformalConic` and call `.makeCoordinateTransform` 
with the dataset and the variable
    4) From that I can `.getProjection`
    5) Then I make a `float[][]` out of the separate x/y variables
    6) Then I can call `.projToLatLon` with the projection and the `float[][]`
    7) After this I go linear over the contents of the `Temperature_surface` 
variable associating each one with the correct lat/lng.
    
    
    This actually works, but I am wondering if there is a better way.
    
    
    I have a feeling that there is some higher level API that I missed that 
would do all this more automatically, and possibly more generally. My code 
feels brittle and over-specific to this particular file.
    
    
    Or, put another way, how best to (given a lat/lng) query the temp at that 
location in this file?
    
    
    With warm appreciation for your time,
    -Harold
    
    

  • 2019 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: