Re: [Fwd: multi band tiff from netCDF]

_From owner-netcdf-java@xxxxxxxxxxxxxxxx Tue Apr 19 07:21:32 2005
Received: (from majordo@localhost)
        by unidata.ucar.edu (UCAR/Unidata) id j3JDK9Ht017708
        for netcdf-java-out; Tue, 19 Apr 2005 07:20:09 -0600 (MDT)
Received: from crow.rsinc.com (crow.rsinc.com [192.5.156.11])
        by unidata.ucar.edu (UCAR/Unidata) with ESMTP id j3JDJwv2017652;
        Tue, 19 Apr 2005 07:19:58 -0600 (MDT)
Organization: UCAR/Unidata
Keywords: 200504191319.j3JDJwv2017652
Received: from mack.corp.rsinc.com (nis1.bldr.rsinc.com [10.17.10.11])
        by crow.rsinc.com (8.12.11/8.12.10) with ESMTP id j3JDJvvA005658;
        Tue, 19 Apr 2005 07:19:57 -0600 (MDT)
Received: from apollo.rsinc.com (apollo.rsinc.com [10.17.10.67])
        by mack.corp.rsinc.com (8.12.11/8.12.10) with ESMTP id j3JDJvpH006670;
        Tue, 19 Apr 2005 07:19:57 -0600 (MDT)
Received: from bath.uk.rsinc.com ([192.168.202.5]) by apollo.rsinc.com with 
Microsoft SMTPSVC(6.0.3790.211);
         Tue, 19 Apr 2005 07:20:11 -0600
X-MimeOLE: Produced By Microsoft Exchange V6.5.7226.0
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain;
        charset="iso-8859-1"
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
Thread-Topic: [Fwd: multi band tiff from netCDF]
Thread-Index: AcVEM2fSM52BTOM2S3yzs2ybegkFjwArqrgA
Cc: <netcdf-java@xxxxxxxxxxxxxxxx>, <support-netcdf-java@xxxxxxxxxxxxxxxx>
X-OriginalArrivalTime: 19 Apr 2005 13:20:11.0527 (UTC) 
FILETIME=[83D92170:01C544E2]
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by unidata.ucar.edu id 
j3JDJwv2017653
Sender: owner-netcdf-java@xxxxxxxxxxxxxxxx
Precedence: bulk

Thanks for the reply, I will top post (apologies) since the file is long.

I wrote some test code before I integrated into my app, and using netcdf-java 
and
JAI the following creates a multiband tif file. (there are some redundant 
variables in
there, apologies again).  JAI documentation is a bit sparse, so hope this helps.

Norman

        public static void main(String[] args) {
                String testNC = "/temp/test.nc";
                String outGTif = "/temp/out.tif";
                String gridName = "ta";
                try {
                        NetcdfDataset ncDataset = 
NetcdfDataset.openDataset(testNC);
                        
                        GridDataset gSet = new GridDataset(ncDataset);
                        GeoGrid grid = gSet.findGridByName(gridName);
            GridCoordSys gcs = grid.getCoordinateSystem();
                        
                    //latlon coord does not need to be scaled
                    double scaler = (gcs.isLatLon()) ? 1.0 : 1000.0;

                    CoordinateAxis1D xaxis = (CoordinateAxis1D) 
gcs.getXHorizAxis();
                    CoordinateAxis1D yaxis = (CoordinateAxis1D) 
gcs.getYHorizAxis();
                    double xStart = xaxis.getCoordValue(0) * scaler;
                    double yStart = yaxis.getCoordValue(0) * scaler;
                        double xEnd = 
xaxis.getCoordValue(grid.getXDimension().getLength()-1) * scaler;
                        double yEnd = 
yaxis.getCoordValue(grid.getYDimension().getLength()-1) * scaler;
                        double xInc = (xEnd - xStart)/xaxis.getElementSize();
                        double yInc = (yEnd - yStart)/yaxis.getElementSize();
                        
                        
                        
                        List list = ncDataset.getCoordinateAxes();
                        Iterator itr = list.iterator();

                        while (itr.hasNext()) {
                                CoordinateAxis1D dim = (CoordinateAxis1D) 
itr.next();
                                String dimName = dim.getName();

                                if (dimName.equals(IngestFile.DIM_NAME)) {
                                        int nBands = (int) dim.getSize();
                                        if (dim.isUnlimited()) {
                                                // we can't do anything, log
                                                return;
                                        }
                                        
                                        int width = 
grid.getXDimension().getLength();
                                        int height = 
grid.getYDimension().getLength();
                
                                        ParameterBlockJAI pbjai = new 
ParameterBlockJAI("bandmerge");
                                        
                                        for (int i = 0; i < nBands; i++)
                                        {
                                                Array arr = 
grid.readDataSlice(-1, i, -1, -1);
                                                float[] data = (float[]) 
arr.getStorage();
                                                DataBufferFloat dbuffer = new 
DataBufferFloat(data, width*height);
                                                
                                                // create a float data sample 
model
                                                SampleModel sampleModel = 
RasterFactory.createBandedSampleModel(DataBuffer.TYPE_FLOAT, width, height, 1);
                                                
                                                // create a compatible colour 
model
                                                ColorModel colorModel = 
PlanarImage.createColorModel(sampleModel);
                                                
                                                // create a writable raster
                                                Raster raster = 
RasterFactory.createWritableRaster(sampleModel, dbuffer, null);
                                                
                                                // create a tiled image using 
the float sample model
                                                TiledImage tiledImage = new 
TiledImage(0, 0, width, height, 0, 0, sampleModel, colorModel);
                                                
                                                // set the data of the tiled 
image to be the raster
                                                tiledImage.setData(raster);
                                                
                                                pbjai.setSource(tiledImage, i);
                                        }
                                        // save the image on a file
                                        PlanarImage result = 
JAI.create("bandmerge", pbjai, null);
                                        JAI.create("filestore", result, 
outGTif, "TIFF");
                                        break;
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }

        }

-----Original Message-----
Sent: Monday, April 18, 2005 5:27 PM
Cc: netcdf-java@xxxxxxxxxxxxxxxx; support-netcdf-java@xxxxxxxxxxxxxxxx


John Caron wrote:

> can you answer this?
> cc to netcdf-java@xxxxxxxxxxxxxxxx and 
> support-netcdf-java@xxxxxxxxxxxxxxxx
>
> -------- Original Message --------
> Subject:     multi band tiff from netCDF
> Date:     Mon, 18 Apr 2005 16:01:21 +0100
> From:     Norman Barker <nbarker@xxxxxxxxx>
> Organization:     UCAR/Unidata
> To:     <netcdf-java@xxxxxxxxxxxxxxxx>
>
>
>
> Hi,
>
> I am using the java classes  to convert a netcdf file for one time 
> instance, with 27 levels in Z (atmospheric pressure levels).
>
> I have created a loop with variable i, and attempting to write a band 
> in the tif file
> for each iteration. What seems to happen is the first band gets 
> written ok, but then after that
> the page number gets messed up?
>
> Array data = grid.readDataSlice(0, i, -1, -1);
> writer.writeGrid(grid, data, false, xStart, yStart, xInc, yInc, i);
>
> Has anyone got any pseudo code for writing multiband tif files from 
> netCDF?
>
> Many thanks,
>
> Norman Barker

Norman,
               The code was not designed for multiband tiff files. We 
originally believed that multipage was better to store multi variables 
and/or multi vertical levels.
We just realized that multiband was probably better embraced because of 
the existing tiff readers and the concept of tiff community. This will 
be my next task to change the
software to use multiband to store tiff files, it will be released 
sometime in the Summer.

Yuan