1414
1515class ComplexNumbersTestCase (unittest .TestCase ):
1616 def setUp (self ):
17- self .tmp_path = pathlib .Path (tempfile .mkdtemp ())
17+ tmp_dir = tempfile .TemporaryDirectory ()
18+ self .addCleanup (tmp_dir .cleanup )
19+ self .tmp_path = pathlib .Path (tmp_dir .name )
1820
1921 def test_read_dim (self ):
2022 filename = self .tmp_path / "test_read_dim.nc"
@@ -34,6 +36,116 @@ def test_read_dim(self):
3436
3537 assert np .array_equal (data , complex_array )
3638
39+ def test_write_real_with_complex_dimension (self ):
40+ # A new variable must retain its requested dtype and dimensions, even
41+ # when its on-disk layout matches a complex-number convention (#1404).
42+ for format in ("NETCDF4" , "NETCDF4_CLASSIC" , "NETCDF3_CLASSIC" ):
43+ for dim_name in ("complex" , "ri" , "_pfnc_complex" ):
44+ for dtype in ("f4" , "f8" , "i4" ):
45+ with self .subTest (format = format , dim_name = dim_name , dtype = dtype ):
46+ filename = self .tmp_path / "test_write_real.nc"
47+ values = np .arange (12 , dtype = dtype ).reshape (6 , 2 )
48+ with netCDF4 .Dataset (
49+ filename , "w" , format = format , auto_complex = True
50+ ) as f :
51+ f .createDimension ("x" , 6 )
52+ f .createDimension (dim_name , 2 )
53+ var = f .createVariable ("data" , dtype , ("x" , dim_name ))
54+ self .assertEqual (var .dtype , np .dtype (dtype ))
55+ self .assertEqual (var .dimensions , ("x" , dim_name ))
56+ self .assertEqual (var .ndim , 2 )
57+ self .assertEqual (var .shape , values .shape )
58+ var [:] = values
59+ var [::2 , 1 ] = - 1
60+ values [::2 , 1 ] = - 1
61+ np .testing .assert_array_equal (var [:], values )
62+ np .testing .assert_array_equal (var [::- 2 , ::- 1 ], values [::- 2 , ::- 1 ])
63+
64+ with netCDF4 .Dataset (filename ) as f :
65+ self .assertEqual (f ["data" ].dimensions , ("x" , dim_name ))
66+ np .testing .assert_array_equal (f ["data" ][:], values )
67+
68+ # Existing floating-point layouts must still decode
69+ # as complex when reopening with auto_complex=True.
70+ if dtype != "i4" :
71+ with netCDF4 .Dataset (filename , auto_complex = True ) as f :
72+ var = f ["data" ]
73+ self .assertEqual (var .dimensions , ("x" ,))
74+ self .assertEqual (var .ndim , 1 )
75+ self .assertEqual (var .dtype , np .dtype ("c8" if dtype == "f4" else "c16" ))
76+ np .testing .assert_array_equal (
77+ var [:], values [:, 0 ] + 1j * values [:, 1 ]
78+ )
79+
80+ def test_write_real_component_axis (self ):
81+ # A component-like dimension can also be an ordinary leading axis,
82+ # or the only axis of a real variable.
83+ for dimensions in (("complex" , "x" ), ("complex" ,)):
84+ with self .subTest (dimensions = dimensions ):
85+ filename = self .tmp_path / "test_write_real_axis.nc"
86+ shape = (2 , 3 ) if len (dimensions ) == 2 else (2 ,)
87+ values = np .arange (np .prod (shape ), dtype = "f8" ).reshape (shape )
88+ with netCDF4 .Dataset (filename , "w" , auto_complex = True ) as f :
89+ f .createDimension ("complex" , 2 )
90+ f .createDimension ("x" , 3 )
91+ var = f .createVariable ("data" , "f8" , dimensions )
92+ self .assertEqual (var .dimensions , dimensions )
93+ self .assertEqual (var .ndim , len (dimensions ))
94+ var [:] = values
95+ np .testing .assert_array_equal (var [:], values )
96+ with netCDF4 .Dataset (filename ) as f :
97+ np .testing .assert_array_equal (f ["data" ][:], values )
98+
99+ def test_write_complex_casts_real_values (self ):
100+ # Both compound and dimension storage must cast real input to complex,
101+ # and reopened variables must keep the same complex I/O behavior.
102+ for format in ("NETCDF4" , "NETCDF3_CLASSIC" ):
103+ for dtype in ("c8" , "c16" ):
104+ with self .subTest (format = format , dtype = dtype ):
105+ filename = self .tmp_path / "test_write_complex_cast.nc"
106+ values = complex_array .astype (dtype )
107+ with netCDF4 .Dataset (
108+ filename , "w" , format = format , auto_complex = True
109+ ) as f :
110+ f .createDimension ("x" , len (values ))
111+ var = f .createVariable ("data" , dtype , ("x" ,))
112+ self .assertEqual (var .dimensions , ("x" ,))
113+ var [:] = values .real
114+ np .testing .assert_array_equal (var [:], values .real )
115+ var [::2 ] = values [::2 ]
116+ expected = values .real .astype (dtype )
117+ expected [::2 ] = values [::2 ]
118+ np .testing .assert_array_equal (var [:], expected )
119+ with netCDF4 .Dataset (filename , "r+" , auto_complex = True ) as f :
120+ var = f ["data" ]
121+ self .assertEqual (var .dtype , np .dtype (dtype ))
122+ np .testing .assert_array_equal (var [:], expected )
123+ var [:] = values
124+ with netCDF4 .Dataset (filename ) as f :
125+ raw = f ["data" ]
126+ if format == "NETCDF4" :
127+ self .assertEqual (raw .dimensions , ("x" ,))
128+ np .testing .assert_array_equal (raw [:]["r" ], values .real )
129+ np .testing .assert_array_equal (raw [:]["i" ], values .imag )
130+ else :
131+ self .assertEqual (len (raw .dimensions ), 2 )
132+ self .assertEqual (raw .shape , (len (values ), 2 ))
133+ np .testing .assert_array_equal (raw [:, 0 ], values .real )
134+ np .testing .assert_array_equal (raw [:, 1 ], values .imag )
135+ with netCDF4 .Dataset (filename , auto_complex = True ) as f :
136+ np .testing .assert_array_equal (f ["data" ][:], values )
137+
138+ def test_write_explicit_complex_struct (self ):
139+ filename = self .tmp_path / "test_write_explicit_struct.nc"
140+ with netCDF4 .Dataset (filename , "w" , auto_complex = True ) as f :
141+ f .createDimension ("x" , len (complex_array ))
142+ datatype = f .createCompoundType (np_dt , "nc_complex" )
143+ var = f .createVariable ("data" , datatype , ("x" ,))
144+ var [:] = complex_struct_array
145+ np .testing .assert_array_equal (var [:], complex_struct_array )
146+ with netCDF4 .Dataset (filename , auto_complex = True ) as f :
147+ np .testing .assert_array_equal (f ["data" ][:], complex_array )
148+
37149 def test_read_struct (self ):
38150 filename = self .tmp_path / "test_read_struct.nc"
39151
0 commit comments