Revision 444 org.gvsig.raster.netcdf/trunk/org.gvsig.raster.netcdf/org.gvsig.raster.netcdf.io/src/main/java/org/gvsig/raster/netcdf/io/NetCDFProvider.java

View differences:

NetCDFProvider.java
180 180
	public void init (AbstractRasterDataParameters params,
181 181
			DataStoreProviderServices storeServices) throws NotSupportedExtensionException, OpenException {
182 182
		
183
		//TODO:
184
		
185 183
		if(((RasterFileStoreParameters)params).getFile().exists()) {
186 184
            try { 
187 185
            	gridNetCDF = GridDataset.open(((RasterFileStoreParameters)params).getFile().getAbsolutePath());
......
225 223
		//CrsWkt crs = new CrsWkt(wktProjection);
226 224
		//IProjection proj = CRSFactory.getCRS("EPSG:23030");
227 225
		noDataEnabled = true;
226
		
227
		/*LatLonRect gcs = selectedGridDataType.getCoordinateSystem().getLatLonBoundingBox();
228
		getColorInterpretation();
229
		double scaleX = gcs.getWidth() / selectedGridDataType.getXDimension().getLength();
230
		double scaleY = gcs.getHeight() / selectedGridDataType.getYDimension().getLength();
231
		ownTransformation = new AffineTransform(
232
				scaleX, 0, 
233
				0, -scaleY, 
234
				gcs.getLonMin(), 
235
				gcs.getLatMax());*/
236
		
228 237
		ProjectionRect pRect = selectedGridDataType.getCoordinateSystem().getBoundingBox();
229
		getColorInterpretation();
230 238
		double scaleX = pRect.getWidth() / selectedGridDataType.getXDimension().getLength();
231 239
		double scaleY = pRect.getHeight() / selectedGridDataType.getYDimension().getLength();
232 240
		ownTransformation = new AffineTransform(
......
373 381
		if(x < 0 || y < 0 || x >= getWidth() || y >= getHeight())
374 382
			throw new InvalidSetViewException("Request out of grid");
375 383
		
376
		//TODO
377 384
		return null;
378 385
	}
379 386

  
......
383 390
	 */
384 391
	public void getWindow(Extent ex, int bufWidth, int bufHeight, 
385 392
			BandList bandList, TileListener listener) throws ProcessInterruptedException, RasterDriverException {
386
		//TODO
387 393
	}
388 394

  
389 395
	/*
......
408 414
		try {
409 415
			int strideX = 1;
410 416
			int strideY = 1;
411
			Range rangeY = new Range((int)ul.getY(), (int)(lr.getY() - 1), strideY);
417
			//Range rangeY = new Range((int)ul.getY(), (int)(lr.getY() - 1), strideY);
418
			Range rangeY = new Range((int)(getHeight() - lr.getY()), (int)(getHeight() - ul.getY() - 1), strideY);
412 419
			Range rangeX = new Range((int)ul.getX(), (int)(lr.getX() - 1), strideX);
413 420
			
414
			selectVariable();
415
			
416
			int level = -1;
417
			int time = -1;
418

  
419
			if(param.hasDynValue(NetCDFDataParameters.FIELD_LEVEL))
420
				level = ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue();
421
			if(param.hasDynValue(NetCDFDataParameters.FIELD_TIME))
422
				time = ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue();
423
			
424
			Range rangeZ = null;
425
			if(level != -1)
426
				rangeZ = new Range(level, level + 1, strideX);
427
			Range rangeT = null;
428
			if(time != -1)
429
				rangeT = new Range(time, time + 1, strideX);
430
				
431
			GridDatatype dt = selectedGridDataType.makeSubset(null, null, rangeT, rangeZ, rangeY, rangeX);
432
			Array values = dt.readDataSlice(0, 0, -1, -1);
421
			selectSubdataset();
422
			int time = param.hasDynValue(NetCDFDataParameters.FIELD_TIME) ? ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue() : 0;
423
			int level = param.hasDynValue(NetCDFDataParameters.FIELD_LEVEL) ? ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue() : 0;
424
			GridDatatype dt = selectedGridDataType.makeSubset(null, null, getTime(strideX), getLevel(strideX), rangeY, rangeX);
425
			Array values = dt.readDataSlice(time, level, -1, -1);
433 426
			rasterBuf = arrayValuesToBuffer(values, rasterBuf, rangeX.length(), rangeY.length(), bandList);
434 427
		} catch (IOException e) {
435 428
			throw new RasterDriverException("Error reading a slice", e);
......
440 433
		return rasterBuf;
441 434
	}
442 435
	
443
	private void selectVariable() {
436
	/**
437
	 * Gets the Range of the selected time or null if there is not a selected time
438
	 * @param strideX
439
	 * @return
440
	 * @throws InvalidRangeException
441
	 */
442
	public Range getTime(int strideX) throws InvalidRangeException {
443
		int time = -1;
444
		if(param.hasDynValue(NetCDFDataParameters.FIELD_TIME))
445
			time = ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue();
446
		if(time != -1) {
447
			return new Range(time, time, strideX);
448
		}
449
		return null;
450
	}
451
	
452
	
453
	/**
454
	 * Gets the Range of the selected level or null if there is not a selected level
455
	 * @param strideX
456
	 * @return
457
	 * @throws InvalidRangeException
458
	 */
459
	public Range getLevel(int strideX) throws InvalidRangeException {
460
		int level = -1;
461
		if(param.hasDynValue(NetCDFDataParameters.FIELD_LEVEL))
462
			level = ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue();
463
		if(level != -1) {
464
			return new Range(level, level, strideX);
465
		}
466
		return null;
467
	}
468
	
469
	/**
470
	 * Selects the GridDataType using the selected variable in the parameters
471
	 */
472
	protected void selectSubdataset() {
444 473
		if(param.hasDynValue(NetCDFDataParameters.FIELD_VARIABLE)) {
445 474
			String variable = (String)param.getDynValue(NetCDFDataParameters.FIELD_VARIABLE);
446 475
			if(variable != null) {
447 476
				for (int j = 0; j < gridList.size(); j++) {
448
					if(gridList.get(j).getName().compareTo(variable) == 0)
477
					if(gridList.get(j).getName().compareTo(variable) == 0) {
449 478
						selectedGridDataType = gridList.get(j);
479
						reloadMetadataFromGrid();
480
						super.selectSubdataset(j);
481
					}
450 482
				}
451 483
			}
452 484
		}
......
461 493
	 * @return
462 494
	 */
463 495
	private Buffer arrayValuesToBuffer(Array values, Buffer rasterBuf, int w, int h, BandList bandList) {
464
		int col = 0;
465
		int row = 0;
466 496
		Buffer buf = null;
467 497
		boolean resampling = false;
468 498
		
......
478 508
		
479 509
		if(getDataType()[0] == Buffer.TYPE_BYTE) {
480 510
			for (int i = 0; i < values.getSize(); i++) {
481
				row = (int)(i / buf.getWidth());
482
				col = i - (row * buf.getWidth());
511
				int[] rc = getColAndRow(i, buf);
483 512
				for (int iBands = 0; iBands < drawableBands.length; iBands++) 
484
					buf.setElem(row, col, drawableBands[iBands], values.getByte(i));
513
					buf.setElem(rc[0], rc[1], drawableBands[iBands], values.getByte(i));
485 514
			}
486 515
		}
487 516

  
488 517
		if(getDataType()[0] == Buffer.TYPE_SHORT) {
489 518
			for (int i = 0; i < values.getSize(); i++) {
490
				row = (int)(i / buf.getWidth());
491
				col = i - (row * buf.getWidth());
519
				int[] rc = getColAndRow(i, buf);
492 520
				for (int iBands = 0; iBands < drawableBands.length; iBands++) 
493
					buf.setElem(row, col, drawableBands[iBands], values.getShort(i));
521
					buf.setElem(rc[0], rc[1], drawableBands[iBands], values.getShort(i));
494 522
			}
495 523
		}
496 524

  
497 525
		if(getDataType()[0] == Buffer.TYPE_INT) {
498 526
			for (int i = 0; i < values.getSize(); i++) {
499
				row = (int)(i / buf.getWidth());
500
				col = i - (row * buf.getWidth());
527
				int[] rc = getColAndRow(i, buf);
501 528
				for (int iBands = 0; iBands < drawableBands.length; iBands++) 
502
					buf.setElem(row, col, drawableBands[iBands], values.getInt(i));
529
					buf.setElem(rc[0], rc[1], drawableBands[iBands], values.getInt(i));
503 530
			}
504 531
		}
505 532

  
506 533
		if(getDataType()[0] == Buffer.TYPE_FLOAT) {
507 534
			for (int i = 0; i < values.getSize(); i++) {
508
				row = (int)(i / buf.getWidth());
509
				col = i - (row * buf.getWidth());
535
				int[] rc = getColAndRow(i, buf);
510 536
				for (int iBands = 0; iBands < drawableBands.length; iBands++) 
511
					buf.setElem(row, col, drawableBands[iBands], values.getFloat(i));
537
					buf.setElem(rc[0], rc[1], drawableBands[iBands], values.getFloat(i));
512 538
			}
513 539
		}
514 540

  
515 541
		if(getDataType()[0] == Buffer.TYPE_DOUBLE) {
516 542
			for (int i = 0; i < values.getSize(); i++) {
517
				row = (int)(i / buf.getWidth());
518
				col = i - (row * buf.getWidth());
543
				int[] rc = getColAndRow(i, buf);
519 544
				for (int iBands = 0; iBands < drawableBands.length; iBands++) 
520
					buf.setElem(row, col, drawableBands[iBands], values.getDouble(i));
545
					buf.setElem(rc[0], rc[1], drawableBands[iBands], values.getDouble(i));
521 546
			}
522 547
		}
523 548
		if(resampling) {
......
529 554
		}
530 555
		return buf;
531 556
	}
557
	
558
	/**
559
	 * Calculates the row and column number for the position i in the
560
	 * array of data
561
	 * @param i
562
	 * @param buf
563
	 * @return
564
	 */
565
	private int[] getColAndRow(int i, Buffer buf) {
566
		int auxRow = (int)(i / buf.getWidth());
567
		return new int[]{ 
568
				(int)buf.getHeight() - auxRow - 1,
569
				i - (auxRow * buf.getWidth())
570
				};
571
	}
532 572

  
533 573
	/*
534 574
	 * (non-Javadoc)
......
536 576
	 */
537 577
	public Buffer getWindow(double ulx, double uly, double w, double h, 
538 578
			BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
539
		//TODO
540

  
541 579
		return rasterBuf;
542 580
	}
543 581

  
......
564 602
		try {
565 603
			int strideX = width / rasterBuf.getWidth();
566 604
			int strideY = height / rasterBuf.getHeight();
567
			Range rangeY = new Range((int)ul.getY(), (int)(lr.getY() - 1), strideY);
605
			//Range rangeY = new Range((int)ul.getY(), (int)(lr.getY() - 1), strideY);
606
			Range rangeY = new Range((int)(getHeight() - lr.getY()), (int)(getHeight() - ul.getY() - 1), strideY);
568 607
			Range rangeX = new Range((int)ul.getX(), (int)(lr.getX() - 1), strideX);
569
			GridDatatype dt = selectedGridDataType.makeSubset(null, null, null, null, rangeY, rangeX);
570
			Array values = dt.readDataSlice(0, 0, -1, -1);
608
			
609
			selectSubdataset();
610
			int time = param.hasDynValue(NetCDFDataParameters.FIELD_TIME) ? ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_TIME)).intValue() : 0;
611
			int level = param.hasDynValue(NetCDFDataParameters.FIELD_LEVEL) ? ((Integer)param.getDynValue(NetCDFDataParameters.FIELD_LEVEL)).intValue() : 0;
612
			GridDatatype dt = selectedGridDataType.makeSubset(null, null, getTime(strideX), getLevel(strideX), rangeY, rangeX);
613
			Array values = dt.readDataSlice(time, level, -1, -1);
571 614
			rasterBuf = arrayValuesToBuffer(values, rasterBuf, rangeX.length(), rangeY.length(), bandList);
572 615
		} catch (IOException e) {
573 616
			throw new RasterDriverException("Error reading a slice", e);
......
593 636
	 */
594 637
	public Buffer getWindow(int x, int y, int w, int h, 
595 638
			BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
596
		//TODO
597
		
598 639
		return rasterBuf;
599 640
	}
600 641

  
......
604 645
	 */
605 646
	public Buffer getWindow(int x, int y, int w, int h, 
606 647
			int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
607
		//TODO
608
		
609 648
		return rasterBuf;
610 649
	}
611 650

  

Also available in: Unified diff