示例#1
0
 def test_user_missing_values(self):
     data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
     basekwargs = dict(dtype=None, delimiter=",", names=True)
     mdtype = [("A", int), ("B", float), ("C", complex)]
     #
     test = np.mafromtxt(StringIO(data), missing_values="N/A", **basekwargs)
     control = ma.array(
         [(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)],
         mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
         dtype=mdtype,
     )
     assert_equal(test, control)
     #
     basekwargs["dtype"] = mdtype
     test = np.mafromtxt(StringIO(data), missing_values={0: -9, 1: -99, 2: -999j}, **basekwargs)
     control = ma.array(
         [(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)],
         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
         dtype=mdtype,
     )
     assert_equal(test, control)
     #
     test = np.mafromtxt(StringIO(data), missing_values={0: -9, "B": -99, "C": -999j}, **basekwargs)
     control = ma.array(
         [(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j), (3, -99, 3j)],
         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
         dtype=mdtype,
     )
     assert_equal(test, control)
示例#2
0
 def test_user_missing_values(self):
     datastr ="A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
     data = StringIO.StringIO(datastr)
     basekwargs = dict(dtype=None, delimiter=',', names=True, missing='N/A')
     mdtype = [('A', int), ('B', float), ('C', complex)]
     #
     test = np.mafromtxt(data, **basekwargs)
     control = ma.array([(   0, 0.0,    0j), (1, -999, 1j),
                         (  -9, 2.2, -999j), (3,  -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     data.seek(0)
     test = np.mafromtxt(data,
                         missing_values={0:-9, 1:-99, 2:-999j}, **basekwargs)
     control = ma.array([(   0, 0.0,    0j), (1, -999, 1j),
                         (  -9, 2.2, -999j), (3,  -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     data.seek(0)
     test = np.mafromtxt(data,
                         missing_values={0:-9, 'B':-99, 'C':-999j},
                         **basekwargs)
     control = ma.array([(   0, 0.0,    0j), (1, -999, 1j),
                         (  -9, 2.2, -999j), (3,  -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
示例#3
0
 def test_user_missing_values(self):
     data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
     basekwargs = dict(dtype=None, delimiter=",", names=True,)
     mdtype = [('A', int), ('B', float), ('C', complex)]
     #
     test = np.mafromtxt(StringIO(data), missing_values="N/A",
                         **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     basekwargs['dtype'] = mdtype
     test = np.mafromtxt(StringIO(data),
                         missing_values={0:-9, 1:-99, 2:-999j}, **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     test = np.mafromtxt(StringIO(data),
                         missing_values={0:-9, 'B':-99, 'C':-999j},
                         **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
示例#4
0
 def test_withmissing(self):
     data = StringIO('A,B\n0,1\n2,N/A')
     kwargs = dict(delimiter=",", missing_values="N/A", names=True)
     err = np.seterr(invalid='ignore')
     try:
         test = np.mafromtxt(data, dtype=None, **kwargs)
     finally:
         np.seterr(**err)
     control = ma.array([(0, 1), (2, -1)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.int), ('B', np.int)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     #
     data.seek(0)
     err = np.seterr(invalid='ignore')
     try:
         test = np.mafromtxt(data, **kwargs)
     finally:
         np.seterr(**err)
     control = ma.array([(0, 1), (2, -1)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.float), ('B', np.float)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#5
0
 def test_withmissing(self):
     data = StringIO.StringIO('A,B\n0,1\n2,N/A')
     test = np.mafromtxt(data, dtype=None, delimiter=',', missing='N/A',
                         names=True)
     control = ma.array([(0, 1), (2, -1)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.int), ('B', np.int)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     #
     data.seek(0)
     test = np.mafromtxt(data, delimiter=',', missing='N/A', names=True)
     control = ma.array([(0, 1), (2, -1)],
                        mask=[[False, False], [False, True]],)
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#6
0
 def test_fancy_dtype_alt(self):
     "Check that a nested dtype isn't MIA"
     data = StringIO.StringIO('1,2,3.0\n4,5,6.0\n')
     fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
     test = np.mafromtxt(data, dtype=fancydtype, delimiter=',')
     control = ma.array([(1,(2,3.0)),(4,(5,6.0))], dtype=fancydtype)
     assert_equal(test, control)
示例#7
0
 def test_fancy_dtype_alt(self):
     "Check that a nested dtype isn't MIA"
     data = StringIO.StringIO('1,2,3.0\n4,5,6.0\n')
     fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
     test = np.mafromtxt(data, dtype=fancydtype, delimiter=',')
     control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
     assert_equal(test, control)
示例#8
0
 def test_fancy_dtype_alt(self):
     "Check that a nested dtype isn't MIA"
     data = StringIO("1,2,3.0\n4,5,6.0\n")
     fancydtype = np.dtype([("x", int), ("y", [("t", int), ("s", float)])])
     test = np.mafromtxt(data, dtype=fancydtype, delimiter=",")
     control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
     assert_equal(test, control)
示例#9
0
 def test_withmissing_float(self):
     data = StringIO("A,B\n0,1.5\n2,-999.00")
     test = np.mafromtxt(data, dtype=None, delimiter=",", missing_values="-999.0", names=True)
     control = ma.array(
         [(0, 1.5), (2, -1.0)], mask=[(False, False), (False, True)], dtype=[("A", np.int), ("B", np.float)]
     )
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#10
0
 def test_withmissing(self):
     data = StringIO("A,B\n0,1\n2,N/A")
     kwargs = dict(delimiter=",", missing_values="N/A", names=True)
     test = np.mafromtxt(data, dtype=None, **kwargs)
     control = ma.array(
         [(0, 1), (2, -1)], mask=[(False, False), (False, True)], dtype=[("A", np.int), ("B", np.int)]
     )
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     #
     data.seek(0)
     test = np.mafromtxt(data, **kwargs)
     control = ma.array(
         [(0, 1), (2, -1)], mask=[(False, False), (False, True)], dtype=[("A", np.float), ("B", np.float)]
     )
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#11
0
 def test_withmissing_float(self):
     data = StringIO.StringIO('A,B\n0,1.5\n2,-999.00')
     test = np.mafromtxt(data, dtype=None, delimiter=',', missing='-999.0',
                         names=True,)
     control = ma.array([(0, 1.5), (2, -1.)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.int), ('B', np.float)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#12
0
 def test_withmissing_float(self):
     data = StringIO('A,B\n0,1.5\n2,-999.00')
     test = np.mafromtxt(data, dtype=None, delimiter=',',
                         missing_values='-999.0', names=True,)
     control = ma.array([(0, 1.5), (2, -1.)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.int), ('B', np.float)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#13
0
 def test_user_missing_values(self):
     data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
     basekwargs = dict(dtype=None, delimiter=",", names=True,)
     mdtype = [('A', int), ('B', float), ('C', complex)]
     #
     err = np.seterr(invalid='ignore')
     try:
         test = np.mafromtxt(StringIO(data), missing_values="N/A",
                             **basekwargs)
     finally:
         np.seterr(**err)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     basekwargs['dtype'] = mdtype
     err = np.seterr(invalid='ignore')
     try:
         test = np.mafromtxt(StringIO(data),
                             missing_values={0:-9, 1:-99, 2:-999j},
                             **basekwargs)
     finally:
         np.seterr(**err)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
     #
     err = np.seterr(invalid='ignore')
     try:
         test = np.mafromtxt(StringIO(data),
                             missing_values={0:-9, 'B':-99, 'C':-999j},
                             **basekwargs)
     finally:
         np.seterr(**err)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
                         (-9, 2.2, -999j), (3, -99, 3j)],
                         mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                         dtype=mdtype)
     assert_equal(test, control)
示例#14
0
 def test_withmissing(self):
     data = StringIO.StringIO('A,B\n0,1\n2,N/A')
     test = np.mafromtxt(data,
                         dtype=None,
                         delimiter=',',
                         missing='N/A',
                         names=True)
     control = ma.array([(0, 1), (2, -1)],
                        mask=[(False, False), (False, True)],
                        dtype=[('A', np.int), ('B', np.int)])
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     #
     data.seek(0)
     test = np.mafromtxt(data, delimiter=',', missing='N/A', names=True)
     control = ma.array(
         [(0, 1), (2, -1)],
         mask=[[False, False], [False, True]],
     )
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
示例#15
0
 def test_user_missing_values(self):
     datastr = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
     data = StringIO.StringIO(datastr)
     basekwargs = dict(dtype=None, delimiter=',', names=True, missing='N/A')
     mdtype = [('A', int), ('B', float), ('C', complex)]
     #
     test = np.mafromtxt(data, **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j),
                         (3, -99, 3j)],
                        mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
                        dtype=mdtype)
     assert_equal(test, control)
     #
     data.seek(0)
     test = np.mafromtxt(data,
                         missing_values={
                             0: -9,
                             1: -99,
                             2: -999j
                         },
                         **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j),
                         (3, -99, 3j)],
                        mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                        dtype=mdtype)
     assert_equal(test, control)
     #
     data.seek(0)
     test = np.mafromtxt(data,
                         missing_values={
                             0: -9,
                             'B': -99,
                             'C': -999j
                         },
                         **basekwargs)
     control = ma.array([(0, 0.0, 0j), (1, -999, 1j), (-9, 2.2, -999j),
                         (3, -99, 3j)],
                        mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
                        dtype=mdtype)
     assert_equal(test, control)
ll_corner =  arcpy.Point(-180., -90.0)  # to position the bottom left corner
dx_dy = 0.5
nodata = '-9999'
#
# ---- create the basic workspace parameters, create masked arrays ----
#
out_file = r'c:\Data\ascii_samples\avg_yr.tif'
folder = r'C:\Data\ascii_samples'
arcpy.env.workspace = folder
ascii_files = arcpy.ListFiles("*.asc")
a_s = [folder + '\{}'.format(i) for i in ascii_files]
arrays = []

for arr in a_s[:1]:
    a = np.mafromtxt(arr, dtype='int32', comments='#',
                     delimiter=' ', skip_header=6,
                     missing_values=nodata, usemask=True)
    value_to_nodata = int(a.get_fill_value())
    out = a.view(np.ndarray, fill_value=value_to_nodata)
    r = arcpy.NumPyArrayToRaster(out, ll_corner, dx_dy, dx_dy)
    out_file = arr.replace(".asc", ".tif")
    if save_output:
        r.save(out_file)
    del r

# ----------------------------------------------------------------------
# __main__ .... code section
if __name__ == "__main__":
    """Optionally...
    : - print the script source name.
    : - run the _demo