NSF Unidata

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Still struggling; MPE - GRIB2 decoders

This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.



Jelle,

attached is some code to print out the GDS and then the data. I think this was what you wanted.

java -Xmx256m ucar/grib/grib2/Grib2MPE <MPE data file> <output>

if you want you can add another parameter for an output file

robb...




On Mon, 10 Mar 2008, Jelle Ferwerda wrote:

Dear Rob,

Sorry to bug you once more.. I have now received the data and am trying to automate extraction of my data, for which I need to set up projections in IDL. Not a problem. however, I need to programmatically extract the projection parameters from the GRIB2 (MPE) files. And somehow I am doing something wrong, I think. I get almost only 0's as output, and an unknown earth shape. Even though the NETcdftoosl 2.2 can show me the information just fine. I think it might have to do with not selecting a product, but I cannot find out how to do this.

I hope you have time to pop me a line and explain where I am going wrong?

This is how I have been trying:

// Create a unidata specified RandomAccesFile for reading
ucar.unidata.io.RandomAccessFile InRA = new ucar.unidata.io.RandomAccessFile(InFile, "r"); // get the grid definitions Grib2GridDefinitionSection gds = new Grib2GridDefinitionSection(InRA, false);
  System.out.println("-------------------------");
  System.out.println(gds.getShapeName());
  System.out.println(gds.getGdtn());
  System.out.println(gds.getLa1());
  System.out.println(gds.getLo1());
  System.out.println(gds.getLa2());
  System.out.println(gds.getLo2());
  System.out.println(gds.getDx());
  System.out.println(gds.getDy());
  System.out.println(gds.getMajorAxis());
  System.out.println(gds.getMinorAxis());
  System.out.println(gds.getEarthRadius());
  System.out.println(gds.getResolution());
  System.out.println("-------------------------");


which returns:
-------------------------
Unknown Earth Shape
30
0.345602
0.026597999
0.0
0.0
805306.4
6825.5654
0.0
0.0
0.0
123
-------------------------

Thanks,

Jelle. -----------------------------------------------
Jelle Ferwerda
Postdoctoral Research Fellow

Animal Behavior Research Group
Department of Zoology / University of Oxford
South Parks Road / Oxford / OX13PS
United Kingdom

email:          address@hidden
Office Phone:   +44 18652 71214
WWW:            http://www.bio-vision.nl
                http://www.cookseasonal.com
-----------------------------------------------


===============================================================================
Robb Kambic                                Unidata Program Center
Software Engineer III                      Univ. Corp for Atmospheric Research
address@hidden             WWW: http://www.unidata.ucar.edu/
===============================================================================
/*
 * Copyright 1997-2004 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * address@hidden.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

// $Id: Grib2MPE.java,v 1.36 2006/08/18 20:22:10 rkambic Exp $


package ucar.grib.grib2;


import ucar.grib.Index;
import ucar.grib.NoValidGribException;
import ucar.grib.NotSupportedException;

/**
 * Grib2MPE.java
 * @author Robb Kambic  10/20/03
 *
 *
 */

// import statements
import ucar.unidata.io.RandomAccessFile;

import ucar.unidata.io.RandomAccessFile;

import java.io.BufferedOutputStream;  // Input/Output functions
import java.io.FileOutputStream;      // Input/Output functions
import java.io.IOException;
import java.io.PrintStream;           // Input/Output functions
import java.io.File;

import java.lang.*;                   // Standard java functions

import java.util.*;                   // Extra utilities from sun


/**
 * Prints GDS and the data for a MPE grib.
 * see <a href="../../../IndexFormat.txt"> IndexFormat.txt</a>
 *
 */
public class Grib2MPE {

    /**
     * Write a Grib file index; optionally create an in-memory index.
     * @param inputRaf GRIB file raf
     * @param ps write output to here
     * @param makeIndex  make an in-memory index if true
     * @throws IOException
     * @return Index if makeIndex is true, else null
     */
    public final Index writeFileIndex(RandomAccessFile inputRaf,
                                             PrintStream ps,
                                             boolean makeIndex)
            throws IOException {

       /**
        * date format "yyyy-MM-dd'T'HH:mm:ss'Z'".
        */
        final java.text.SimpleDateFormat dateFormat;
        dateFormat =
            new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        dateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));  //same 
as UTC

        Date now = Calendar.getInstance().getTime();
        //System.out.println(now.toString() + " ... Start of GribMPE");
        long  start = System.currentTimeMillis();
        int   count = 0;

        Index index = makeIndex
                      ? new Index()
                      : null;

        // Opening of grib data must be inside a try-catch block
        try {
            inputRaf.seek(0);
            // Create Grib2Input instance
            Grib2Input g2i = new Grib2Input(inputRaf);
            // params getProducts (implies  unique GDSs too), oneRecord
            g2i.scan(true, false);
            /*
            // Section 1 Global attributes
            // while needed here to process complete stream
            ps.println("index_version = " + Index.current_index_version);
            ps.println("grid_edition = " + 2);
            ps.println("location = " + inputRaf.getLocation());
            ps.println("length = " + inputRaf.length());
            ps.println("created = " + dateFormat.format(now));
            //ps.println("version = 1.0");
            if (makeIndex) {
                index.addGlobalAttribute("length",
                                         Long.toString(inputRaf.length()));
                index.addGlobalAttribute("location", inputRaf.getLocation());
                index.addGlobalAttribute("created", dateFormat.format(now));
                //index.addGlobalAttribute( "version", "1.0");
            }
            */
            // section 3: GDSs in this File
            HashMap gdsHM = g2i.getGDSs();
            for (Iterator it = gdsHM.keySet().iterator(); it.hasNext(); ) {
                ps.println(
                    
"--------------------------------------------------------------------");
                String key = (String) it.next();
                //ps.println("GDSkey = " + key);
                Grib2GridDefinitionSection gds =
                    (Grib2GridDefinitionSection) gdsHM.get(key);
                printGDS(gds, ps);
                if (makeIndex) {
                    index.addHorizCoordSys(makeGdsRecord(gds));
                }
                ps.println(
                    
"--------------------------------------------------------------------");
            }

            Grib2Data g2d    = new Grib2Data(inputRaf);
            // section 2 grib records
            ArrayList products = g2i.getProducts();
            for (int i = 0; i < products.size(); i++) {
                Grib2Product product = (Grib2Product) products.get(i);
                Grib2ProductDefinitionSection pds = product.getPDS();
                /*
                Grib2IdentificationSection    id  = product.getID();
                if (i == 0) {
                    ps.println("center = " + id.getCenter_id());
                    ps.println("sub_center = " + id.getSubcenter_id());
                    ps.println("table_version = "
                               + id.getLocal_table_version());
                    ps.println(
                        
"--------------------------------------------------------------------");
                }
                */
                /*
                ps.println(pds.getProductDefinition() + " "
                           + product.getDiscipline() + " "
                           + pds.getParameterCategory() + " "
                           + pds.getParameterNumber() + " "
                           + pds.getTypeGenProcess() + " "
                           + pds.getTypeFirstFixedSurface() + " "
                           + pds.getValueFirstFixedSurface() + " "
                           + pds.getTypeSecondFixedSurface() + " "
                           + pds.getValueSecondFixedSurface() + " "
                           + product.getReferenceTime() + " "
                           + pds.getForecastTime() + " "
                           + product.getGDSkey() + " "
                           + product.getGdsOffset() + " "
                           + product.getPdsOffset());
                */
                float     data[] = g2d.getData(product.getGdsOffset(), 
product.getPdsOffset());

                if (data != null) {
                    for (int j = 0; j < data.length; j++) {
                         ps.println("data[ " + j + " ]=" + data[j]);
                    }
                }

                count++;
            }
            // Catch thrown errors from GribFile
        } catch (NotSupportedException noSupport) {
            System.err.println("NotSupportedException : " + noSupport);
        }
        ps.close();

        //System.out.println(" "+count+" products took 
"+(System.currentTimeMillis() - start) + " msec");

        return index;
    }  // end writeFileIndex

    /**
     * creates a GribRecord from a Grib2Product.
     *
     * @param index _more_
     * @param product
     * @return Index.GribRecord
     */

    protected static final Index.GribRecord makeGribRecord(Index index,
            Grib2Product product) {
        Index.GribRecord              gr  = index.getGribRecord();

        Grib2ProductDefinitionSection pds = product.getPDS();

        gr.productType    = pds.getProductDefinition();
        gr.discipline     = product.getDiscipline();
        gr.category       = pds.getParameterCategory();
        gr.paramNumber    = pds.getParameterNumber();
        gr.typeGenProcess = pds.getTypeGenProcess();
        gr.levelType1     = pds.getTypeFirstFixedSurface();
        gr.levelValue1    = pds.getValueFirstFixedSurface();
        gr.levelType2     = pds.getTypeSecondFixedSurface();
        gr.levelValue2    = pds.getValueSecondFixedSurface();
        gr.refTime        = product.getReferenceTime().toString();
        gr.forecastTime   = pds.getForecastTime();
        gr.gdsKey         = product.getGDSkey().trim();
        gr.offset1        = product.getGdsOffset();
        gr.offset2        = product.getPdsOffset();

        return gr;
    }

    /**
     * Creates a Index.GdsRecord from a GDS
     * @param gds
     * @return Index.GdsRecord
     */
    protected static final Index.GdsRecord makeGdsRecord(
            Grib2GridDefinitionSection gds) {

        Index.GdsRecord igds = new Index.GdsRecord();

        igds.gdsKey = gds.getCheckSum();
        igds.addParam("GDSkey", gds.getCheckSum());
        //System.out.println( "igds.gdsKey ="+ igds.gdsKey );

        String winds = ((gds.getResolution() & 8) == 0)
                       ? "Relative"
                       : "True";

        if (((gds.getGdtn() < 50) || (gds.getGdtn() > 53))
                && (gds.getGdtn() != 100) && (gds.getGdtn() != 120)
                && (gds.getGdtn() != 1200)) {
            igds.grid_shape_code = gds.getShape();
            igds.addParam("grid_shape_code",
                          Integer.toString(gds.getShape()));
            igds.grid_type = gds.getGdtn();
            igds.addParam("grid_type", Integer.toString(gds.getGdtn()));
            igds.addParam("grid_shape", gds.getShapeName());

            if ((gds.getShape() < 2) || (gds.getShape() == 6)) {
                igds.radius_spherical_earth = gds.getEarthRadius();
                igds.addParam("radius_spherical_earth",
                              Double.toString(gds.getEarthRadius()));

            } else if ((gds.getShape() > 1) && (gds.getShape() < 5)) {
                igds.major_axis_earth = gds.getMajorAxis();
                igds.addParam("major_axis_earth",
                              Double.toString(gds.getMajorAxis()));
                igds.minor_axis_earth = gds.getMinorAxis();
                igds.addParam("minor_axis_earth",
                              Double.toString(gds.getMinorAxis()));
            }
        }
        switch (gds.getGdtn()) {                // Grid Definition Template 
Number

          case 0 :
          case 1 :
          case 2 :
          case 3 :                              // Latitude/Longitude Grid

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              igds.winds      = winds;
              igds.addParam("La2", Double.toString(gds.getLa2()));
              igds.addParam("Lo2", Double.toString(gds.getLo2()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));

              if (gds.getGdtn() == 1) {         //Rotated Latitude/longitude
                  igds.addParam("SpLat", Double.toString(gds.getSpLat()));
                  igds.addParam("SpLon", Double.toString(gds.getSpLon()));

              } else if (gds.getGdtn() == 2) {  //Stretched Latitude/longitude
                  igds.addParam("pLat", Double.toString(gds.getPoleLat()));
                  igds.addParam("pLon", Double.toString(gds.getPoleLon()));

              } else if (gds.getGdtn() == 3) {  //Stretched and Rotated
                  // Latitude/longitude
                  igds.addParam("SpLat", Double.toString(gds.getSpLat()));
                  igds.addParam("SpLon", Double.toString(gds.getSpLon()));

              }
              break;

          case 10 :                              // Mercator

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              igds.winds      = winds;
              igds.addParam("La2", Double.toString(gds.getLa2()));
              igds.addParam("Lo2", Double.toString(gds.getLo2()));
              igds.addParam("Angle", Double.toString(gds.getAngle()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));

              break;

          case 20 :  // Polar stereographic projection

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              String NpProj = ((gds.getProjectionCenter() & 128) == 0)
                       ? "true"
                       : "false";
              igds.addParam("NpProj", NpProj);
              //System.out.println( "makeGdsRec NpProj ="+ NpProj );
              igds.winds      = winds;
              igds.LaD        = gds.getLad();
              igds.addParam("Lad", Double.toString(gds.getLad()));
              igds.LoV = gds.getLov();
              igds.addParam("Lov", Double.toString(gds.getLov()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));

              break;

          case 30 :                              // Lambert Conformal

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              NpProj = ((gds.getProjectionCenter() & 128) == 0)
                       ? "true"
                       : "false";
              igds.addParam("NpProj", NpProj);
              //System.out.println( "makeGdsRec NpProj ="+ NpProj );
              igds.winds      = winds;
              igds.LaD        = gds.getLad();
              igds.addParam("Lad", Double.toString(gds.getLad()));
              igds.LoV = gds.getLov();
              igds.addParam("Lov", Double.toString(gds.getLov()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));
              igds.latin1 = gds.getLatin1();
              igds.addParam("Latin1", Double.toString(gds.getLatin1()));
              igds.latin2 = gds.getLatin2();
              igds.addParam("Latin2", Double.toString(gds.getLatin2()));
              igds.addParam("SpLat", Double.toString(gds.getSpLat()));
              igds.addParam("SpLon", Double.toString(gds.getSpLon()));

              break;

          case 31 :                              // Albers equal area

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              NpProj = ((gds.getProjectionCenter() & 128) == 0)
                       ? "true"
                       : "false";
              igds.addParam("NpProj", NpProj);
              //System.out.println( "makeGdsRec NpProj ="+ NpProj );
              igds.winds      = winds;
              igds.LaD        = gds.getLad();
              igds.addParam("Lad", Double.toString(gds.getLad()));
              igds.LoV = gds.getLov();
              igds.addParam("Lov", Double.toString(gds.getLov()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));
              igds.latin1 = gds.getLatin1();
              igds.addParam("Latin1", Double.toString(gds.getLatin1()));
              igds.latin2 = gds.getLatin2();
              igds.addParam("Latin2", Double.toString(gds.getLatin2()));
              igds.addParam("SpLat", Double.toString(gds.getSpLat()));
              igds.addParam("SpLon", Double.toString(gds.getSpLon()));

              break;

          case 40 :
          case 41 :
          case 42 :
          case 43 :  // Gaussian latitude/longitude

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              igds.winds      = winds;
              igds.addParam("La2", Double.toString(gds.getLa2()));
              igds.addParam("Lo2", Double.toString(gds.getLo2()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.addParam("NumberParallels", Double.toString(gds.getN()));

              if (gds.getGdtn() == 41) {         //Rotated Gaussian 
Latitude/longitude
                  igds.addParam("SpLat", Double.toString(gds.getSpLat()));
                  igds.addParam("SpLon", Double.toString(gds.getSpLon()));

              } else if (gds.getGdtn() == 42) {  //Stretched Gaussian
                  igds.addParam("pLat", Double.toString(gds.getPoleLat()));
                  igds.addParam("pLon", Double.toString(gds.getPoleLon()));
                  // Latitude/longitude
              } else if (gds.getGdtn() == 43) {  //Stretched and Rotated 
Gaussian
                  // Latitude/longitude
                  igds.addParam("SpLat", Double.toString(gds.getSpLat()));
                  igds.addParam("SpLon", Double.toString(gds.getSpLon()));
                  igds.addParam("Angle", Double.toString(gds.getAngle()));
                  igds.addParam("pLat", Double.toString(gds.getPoleLat()));
                  igds.addParam("pLon", Double.toString(gds.getPoleLon()));

              }

              break;

          case 50 :
          case 51 :
          case 52 :
          case 53 :   // Spherical harmonic coefficients

              break;

          case 90 :   // Space view perspective or orthographic

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.addParam("Lap", Double.toString(gds.getLap()));
              igds.addParam("Lop", Double.toString(gds.getLop()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              igds.winds      = winds;
              igds.dx         = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));
              igds.addParam("Xp", Double.toString(gds.getXp()));
              igds.addParam("Yp", Double.toString(gds.getYp()));
              igds.addParam("Angle", Double.toString(gds.getAngle()));
              igds.addParam("Nr", Double.toString(gds.getAltitude()));
              igds.addParam("Xo", Double.toString(gds.getXo()));
              igds.addParam("Yo", Double.toString(gds.getYo()));

              break;

          case 100 :  // Triangular grid based on an icosahedron

              igds.addParam("pLat", Double.toString(gds.getPoleLat()));
              igds.addParam("pLon", Double.toString(gds.getPoleLon()));

              break;

          case 110 :  // Equatorial azimuthal equidistant projection

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              NpProj = ((gds.getProjectionCenter() & 128) == 0)
                       ? "true"
                       : "false";
              igds.addParam("NpProj", NpProj);
              //System.out.println( "makeGdsRec NpProj ="+ NpProj );
              igds.winds      = winds;
              igds.dx         = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));
              igds.dy = gds.getDy();
              igds.addParam("Dy", Double.toString(gds.getDy()));

              break;


          case 120 :  // Azimuth-range Projection

              igds.La1 = gds.getLa1();
              igds.addParam("La1", Double.toString(gds.getLa1()));
              igds.Lo1 = gds.getLo1();
              igds.addParam("Lo1", Double.toString(gds.getLo1()));
              igds.dx = gds.getDx();
              igds.addParam("Dx", Double.toString(gds.getDx()));

              break;

          case 204 :   // Curvilinear orthographic

              igds.nx = gds.getNx();
              igds.addParam("Nx", Integer.toString(gds.getNx()));
              igds.ny = gds.getNy();
              igds.addParam("Ny", Integer.toString(gds.getNy()));
              igds.resolution = gds.getResolution();
              igds.addParam("ResCompFlag", Integer.toString(gds.getResolution() 
));
              igds.winds      = winds;
              //igds.dx         = gds.getDx();
              //igds.addParam("Dx", Double.toString(gds.getDx()));
              //igds.dy = gds.getDy();
              //igds.addParam("Dy", Double.toString(gds.getDy()));

              break;
          /*
                 case 1000: //  Cross-Section Grid with Points Equally Spaced 
on the Horizontal

                 igds.La1  = gds.getLa1();
                 igds.addParam( "La1", Double.toString( gds.getLa1() ) );
                 igds.Lo1  = gds.getLo1();
                 igds.addParam( "Lo1", Double.toString( gds.getLo1() ) );
                 igds.resolution  = gds.getResolution();
                 igds.addParam("ResCompFlag", 
Integer.toString(gds.getResolution() ));
                 igds.winds = winds;
                 igds.addParam( "La2", Double.toString( gds.getLa2() ) );
                 igds.addParam( "Lo2", Double.toString( gds.getLo2() ) );

                 break;
          */

        }  // end switch gdtn

        return igds;

    }


    /**
     * prints out a products GDS.
     * @param gds
     * @param ps printStream used to write Index
     */
    public void printGDS(Grib2GridDefinitionSection gds,
                                PrintStream ps) {

        ps.println("grid_type = " + gds.getGdtn());
        ps.println("grid_name = " + gds.getName());

        String winds = ((gds.getResolution() & 8) == 0)
                       ? "Relative"
                       : "True";

        if (((gds.getGdtn() < 50) || (gds.getGdtn() > 53))
                && (gds.getGdtn() != 100) && (gds.getGdtn() != 120)
                && (gds.getGdtn() != 1200)) {
            ps.println("grid_shape_code = " + gds.getShape());
            ps.println("grid_shape = " + gds.getShapeName());
            if ((gds.getShape() < 2) || (gds.getShape() == 6)) {
                ps.println("radius_spherical_earth = "
                           + gds.getEarthRadius());
            } else if ((gds.getShape() > 1) && (gds.getShape() < 5)) {
                ps.println("major_axis_earth = " + gds.getMajorAxis());
                ps.println("minor_axis_earth = " + gds.getMinorAxis());
            }
        }

        switch (gds.getGdtn()) {                // Grid Definition Template 
Number

          case 0 :
          case 1 :
          case 2 :
          case 3 :                              // Latitude/Longitude Grid

              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("La2 = " + gds.getLa2());
              ps.println("Lo2 = " + gds.getLo2());
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());
              //ps.println("ScanningMode = " + gds.getScanMode());

              if (gds.getGdtn() == 1) {         //Rotated Latitude/longitude
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());

              } else if (gds.getGdtn() == 2) {  //Stretched Latitude/longitude
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());

              } else if (gds.getGdtn() == 3) {  //Stretched and Rotated 
                  // Latitude/longitude
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());
              }
              break;

          case 10 :                              // Mercator
              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("La2 = " + gds.getLa2());
              ps.println("Lo2 = " + gds.getLo2());
              //ps.println("ScanningMode = " + gds.getScanMode());
              ps.println("BasicAngle = " + gds.getAngle());
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());

              break;

          case 20 :  // Polar stereographic projection

              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("LaD = " + gds.getLad());
              ps.println("LoV = " + gds.getLov());
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());
              ps.println("ProjFlag = " + gds.getProjectionCenter());
              ps.println("NpProj = " + ((gds.getProjectionCenter() & 128) == 
0));
              //ps.println("ScanningMode = " + gds.getScanMode());

              break;

          case 30 :                              // Lambert Conformal

              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("LaD = " + gds.getLad());
              ps.println("LoV = " + gds.getLov());
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());
              ps.println("ProjFlag = " + gds.getProjectionCenter());
              ps.println("NpProj = " + ((gds.getProjectionCenter() & 128) == 
0));
              //ps.println("ScanningMode = " + gds.getScanMode());
              ps.println("Latin1 = " + gds.getLatin1());
              ps.println("Latin2 = " + gds.getLatin2());
              ps.println("SpLat = " + gds.getSpLat());
              ps.println("SpLon = " + gds.getSpLon());

              break;

          case 40 :
          case 41 :
          case 42 :
          case 43 :  // Gaussian latitude/longitude
              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("La2 = " + gds.getLa2());
              ps.println("Lo2 = " + gds.getLo2());
              ps.println("Dx = " + gds.getDx());
              ps.println("StretchingFactor = " + gds.getFactor());
              ps.println("NumberParallels = " + gds.getN());
              //ps.println("ScanningMode = " + gds.getScanMode());

              if (gds.getGdtn() == 41) {         //Rotated Gaussian 
Latitude/longitude
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());

              } else if (gds.getGdtn() == 42) {  //Stretched Gaussian 
                  // Latitude/longitude
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());

              } else if (gds.getGdtn() == 43) {  //Stretched and Rotated 
Gaussian  
                  // Latitude/longitude
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());

              }
              break;

          case 50 :
          case 51 :
          case 52 :
          case 53 :                       // Spherical harmonic coefficients
              ps.println("J = " + gds.getJ());
              ps.println("K = " + gds.getK());
              ps.println("M = " + gds.getM());
              ps.println("MethodNorm = " + gds.getMethod());
              ps.println("ModeOrder = " + gds.getMode());
              if (gds.getGdtn() == 51) {  //Rotated Spherical harmonic 
coefficients
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());

              } else if (gds.getGdtn() == 52) {  //Stretched Spherical 
                  // harmonic coefficients
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());

              } else if (gds.getGdtn() == 53) {  //Stretched and Rotated 
                  // Spherical harmonic coefficients
                  ps.println("SpLat = " + gds.getSpLat());
                  ps.println("SpLon = " + gds.getSpLon());
                  ps.println("RotationAngle = " + gds.getRotationangle());
                  ps.println("pLat = " + gds.getPoleLat());
                  ps.println("pLon = " + gds.getPoleLon());
                  ps.println("StretchingFactor = " + gds.getFactor());

              }
              break;

          case 90 :  // Space view perspective or orthographic
              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("Lap = " + gds.getLap());
              ps.println("Lop = " + gds.getLop());
              //ps.println("NumberPointsParallel = " + gds.getNx());
              //ps.println("NumberPointsMeridian = " + gds.getNy());
              //ps.println("LatitudeSub-satellitePoint = " + gds.getLap());
              //ps.println("LongitudeSub-satellitePoint = " + gds.getLop());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());
              ps.println("Xp = " + gds.getXp());
              ps.println("Yp = " + gds.getYp());
              //ps.println("ScanningMode = " + gds.getScanMode());
              ps.println("Angle = " + gds.getAngle());
              ps.println("Nr = " + gds.getAltitude());
              ps.println("Xo = " + gds.getXo());
              ps.println("Yo = " + gds.getYo());

              break;

          case 100 :  // Triangular grid based on an icosahedron
              ps.println("Exponent2Intervals = " + gds.getN2());
              ps.println("Exponent3Intervals = " + gds.getN3());
              ps.println("NumberIntervals = " + gds.getNi());
              ps.println("NumberDiamonds = " + gds.getNd());
              ps.println("pLat = " + gds.getPoleLat());
              ps.println("pLon = " + gds.getPoleLon());
              ps.println("GridPointPosition = " + gds.getPosition());
              ps.println("NumberOrderDiamonds = " + gds.getOrder());
              //ps.println("ScanningMode = " + gds.getScanMode());
              ps.println("NumberParallels = " + gds.getN());

              break;

          case 110 :  // Equatorial azimuthal equidistant projection
              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("NpProj = " + ((gds.getProjectionCenter() & 128) == 
0));
              ps.println("Winds = " + winds);
              ps.println("Dx = " + gds.getDx());
              ps.println("Dy = " + gds.getDy());
              ps.println("ProjFlag = " + gds.getProjectionCenter());
              //ps.println("ScanningMode = " + gds.getScanMode());

              break;

          case 120 :  // Azimuth-range Projection
              ps.println("NumberDataBins = " + gds.getNb());
              ps.println("NumberRadials = " + gds.getNr());
              ps.println("NumberPointsParallel = " + gds.getNx());
              ps.println("La1 = " + gds.getLa1());
              ps.println("Lo1 = " + gds.getLo1());
              ps.println("Dx = " + gds.getDx());
              ps.println("OffsetFromOrigin = " + gds.getDstart());
              //ps.println( "need code to get azi and adelta" );

              break;

          case 204 :  // Curvilinear orthographic
              ps.println("Nx = " + gds.getNx());
              ps.println("Ny = " + gds.getNy());
              ps.println("ResCompFlag = " + gds.getResolution());
              ps.println("Winds = " + winds);
              //ps.println("Dx = " + gds.getDx());
              //ps.println("Dy = " + gds.getDy());
              //ps.println("ScanningMode = " + gds.getScanMode());

              break;

           default :
              ps.println("Unknown Grid Type" + gds.getGdtn());
        }             // end switch gdtn
        if (gds.getOlon() == 0) {
            ps.println("Quasi = false");
        } else {
            ps.println("Quasi = true");
        }
    }  // end printGDS

    /**
     *
     * Dumps usage of the class.
     * @param className Grib2MPE
     *
     */
    private static void usage(String className) {
        System.out.println();
        System.out.println("Usage of " + className + ":");
        System.out.println("Parameters:");
        System.out.println("<GribFileToRead> reads/scans for index");
        System.out.println(
            "<IndexFile.idx> where to write index, default STDOUT");
        System.out.println();
        System.out.println("java " + className
                           + " <GribFileToRead> <IndexFile>");
        System.exit(0);
    }

    /**
     * creates a Grib2 index for given Grib2 file.
     * @param args 2 if Grib file and index file name given
     * @throws IOException
     */
    public static void main(String args[]) throws IOException {

        // Function References
        Grib2MPE indexer = new Grib2MPE();

        // Test usage
        if (args.length < 1) {
            // Get class name as String
            Class cl = indexer.getClass();
            usage(cl.getName());
            System.exit(0);
        }

        RandomAccessFile raf    = null;
        PrintStream      ps     = System.out;
        String           infile = args[0];
        raf = new RandomAccessFile(infile, "r");
        raf.order(RandomAccessFile.BIG_ENDIAN);

        if (args.length == 2) {  // input file and index file name given
            String idxfile;
            if (args[1].endsWith(".dat")) {
                idxfile = args[1];
            } else {
                idxfile = args[1].concat(".dat");
            }
            File idx = new File( idxfile );
            // create tmp  index file
            idxfile = idxfile +".tmp";
            //System.out.println( "idxfile ="+ idxfile );
            ps = new PrintStream(
                new BufferedOutputStream(
                    new FileOutputStream(idxfile, false)));
            indexer.writeFileIndex(raf, ps, false);
            ps.close();
            File tidx = new File( idxfile );
            tidx.renameTo( idx );

        } else if (args.length == 1) {  // output to STDOUT
            ps = System.out;
            indexer.writeFileIndex(raf, ps, false);
        }
    }
}  // end Grib2MPE