#!/usr/bin/env python

import os
from copy import deepcopy
import numpy as np
from numpy.testing import assert_array_equal
import gippy as gp
import unittest
import gippy.test as gpt


class GeoImageTests(unittest.TestCase):

    prefix = 'test-'

    def setUp(self):
        """ Configure options """
        gp.Options.set_verbose(1)
        gp.Options.set_chunksize(4.0)

    def test0_open(self):
        """ Open existing image """
        geoimg = gpt.get_test_image()
        self.assertEqual(geoimg.xsize(), 627)
        self.assertEqual(geoimg.ysize(), 603)

    def test1_create(self):
        """ Create single band image """
        fout = 'test.tif'
        geoimg = gp.GeoImage.create(fout, xsz=1000, ysz=1000)
        self.assertTrue(geoimg.xsize() == 1000)
        self.assertTrue(geoimg.ysize() == 1000)
        self.assertTrue(os.path.exists(fout))
        # test resolution
        res = geoimg.resolution()
        self.assertEqual(res.x(), 1.0/geoimg.xsize())
        self.assertEqual(res.y(), -1.0/geoimg.ysize())
        os.remove(fout)

    def test_read(self):
        """ Read multiband image """
        geoimg = gpt.get_test_image()
        arr = geoimg.read()
        self.assertEqual(geoimg.nbands(), arr.shape[0])
        # make sure x, y dimensions are same when reading single bands
        self.assertEqual(arr.shape[1:3], geoimg[0].read().shape)

    def test_read_random_pixels(self):
        """ Read random pixels """
        geoimg = gpt.get_test_image()
        arr = geoimg.read_random_pixels(1000)

    def test_uint16_read(self):