File: test_netcdf.py

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (1309 lines) | stat: -rw-r--r-- 46,262 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import MDAnalysis as mda
import numpy as np
import sys

from scipy.io import netcdf_file

import pytest
from numpy.testing import assert_equal, assert_almost_equal
from MDAnalysis.coordinates.TRJ import NCDFReader, NCDFWriter

from MDAnalysisTests.datafiles import (
    PFncdf_Top,
    PFncdf_Trj,
    GRO,
    TRR,
    XYZ_mini,
    PRM_NCBOX,
    TRJ_NCBOX,
    DLP_CONFIG,
    CPPTRAJ_TRAJ_TOP,
    CPPTRAJ_TRAJ,
)
from MDAnalysisTests.coordinates.test_trj import _TRJReaderTest
from MDAnalysisTests.coordinates.reference import RefVGV, RefTZ2
from MDAnalysisTests import make_Universe
from MDAnalysisTests.util import block_import


class _NCDFReaderTest(_TRJReaderTest):
    prec = 3

    @pytest.fixture()
    def universe(self):
        return mda.Universe(self.topology, self.filename)

    def test_slice_iteration(self, universe):
        frames = [ts.frame for ts in universe.trajectory[4:-2:4]]
        assert_equal(
            frames,
            np.arange(universe.trajectory.n_frames)[4:-2:4],
            err_msg="slicing did not produce the expected frames",
        )

    def test_metadata(self, universe):
        data = universe.trajectory.trjfile
        assert_equal(data.Conventions.decode("utf-8"), "AMBER")
        assert_equal(data.ConventionVersion.decode("utf-8"), "1.0")

    def test_dt(self, universe):
        ref = 0.0
        assert_almost_equal(ref, universe.trajectory.dt, self.prec)
        assert_almost_equal(ref, universe.trajectory.ts.dt, self.prec)

    def test_get_writer(self, universe):
        with universe.trajectory.Writer("out.ncdf") as w:
            assert w.n_atoms == len(universe.atoms)
            assert w.remarks.startswith("AMBER NetCDF format")

    def test_get_writer_custom_n_atoms(self, universe):
        with universe.trajectory.Writer(
            "out.ncdf", n_atoms=42, remarks="Hi!"
        ) as w:
            assert w.n_atoms == 42
            assert w.remarks == "Hi!"

    def test_wrong_natoms(self):
        with pytest.raises(ValueError):
            mda.coordinates.TRJ.NCDFReader(self.filename, n_atoms=2)

    def test_read_on_closed(self, universe):
        universe.trajectory.close()

        with pytest.raises(IOError):
            universe.trajectory.__getitem__(2)

    def test_mmap_kwarg(self, universe):
        # default is None
        assert universe.trajectory._mmap == None


# Ugly way to create the tests for mmap


class _NCDFReaderTest_mmap_None(_NCDFReaderTest):
    @pytest.fixture()
    def universe(self):
        return mda.Universe(self.topology, self.filename, mmap=None)


class _NCDFReaderTest_mmap_True(_NCDFReaderTest):
    @pytest.fixture()
    def universe(self):
        return mda.Universe(self.topology, self.filename, mmap=True)

    def test_mmap_kwarg(self, universe):
        # default is None
        assert universe.trajectory._mmap == True


class _NCDFReaderTest_mmap_False(_NCDFReaderTest):
    @pytest.fixture()
    def universe(self):
        return mda.Universe(self.topology, self.filename, mmap=False)

    def test_mmap_kwarg(self, universe):
        assert universe.trajectory._mmap == False


class TestNCDFReader(_NCDFReaderTest, RefVGV):
    pass


class TestNCDFReader_mmap_None(_NCDFReaderTest_mmap_None, RefVGV):
    pass


class TestNCDFReader_mmap_True(_NCDFReaderTest_mmap_True, RefVGV):
    pass


class TestNCDFReader_mmap_False(_NCDFReaderTest_mmap_False, RefVGV):
    pass


class TestNCDFReaderTZ2(_NCDFReaderTest, RefTZ2):
    pass


class TestNCDFReader2(object):
    """NCDF Trajectory with positions and forces.

    Contributed by Albert Solernou
    """

    prec = 3

    @pytest.fixture(scope="class")
    def u(self):
        return mda.Universe(PFncdf_Top, PFncdf_Trj)

    def test_positions_1(self, u):
        """Check positions on first frame"""
        u.trajectory[0]
        ref_1 = np.array(
            [
                [-0.11980818, 18.70524979, 11.6477766],
                [-0.44717646, 18.61727142, 12.59919548],
                [-0.60952115, 19.47885513, 11.22137547],
            ],
            dtype=np.float32,
        )
        assert_almost_equal(ref_1, u.atoms.positions[:3], self.prec)

    def test_positions_2(self, u):
        """Check positions on second frame"""
        u.trajectory[1]
        ref_2 = np.array(
            [
                [-0.13042036, 18.6671524, 11.69647026],
                [-0.46643803, 18.60186768, 12.646698],
                [-0.46567637, 19.49173927, 11.21922874],
            ],
            dtype=np.float32,
        )
        assert_almost_equal(ref_2, u.atoms.positions[:3], self.prec)

    def test_forces_1(self, u):
        """Check forces on first frame"""
        u.trajectory[0]
        ref_1 = np.array(
            [
                [49.23017883, -97.05565643, -86.09863281],
                [2.97547197, 29.84169388, 11.12069607],
                [-15.93093777, 14.43616867, 30.25889015],
            ],
            dtype=np.float32,
        )
        assert_almost_equal(ref_1, u.atoms.forces[:3], self.prec)

    def test_forces_2(self, u):
        """Check forces on second frame"""
        u.trajectory[1]
        ref_2 = np.array(
            [
                [116.39096832, -145.44448853, -151.3155365],
                [-18.90058327, 27.20145798, 1.95245135],
                [-31.08556366, 14.95863628, 41.10367966],
            ],
            dtype=np.float32,
        )
        assert_almost_equal(ref_2, u.atoms.forces[:3], self.prec)

    def test_time_1(self, u):
        """Check time on first frame"""
        ref = 35.02
        assert_almost_equal(ref, u.trajectory[0].time, self.prec)

    def test_time_2(self, u):
        """Check time on second frame"""
        ref = 35.04
        assert_almost_equal(ref, u.trajectory[1].time, self.prec)

    def test_dt(self, u):
        ref = 0.02
        assert_almost_equal(ref, u.trajectory.dt, self.prec)
        assert_almost_equal(ref, u.trajectory.ts.dt, self.prec)

    def test_box(self, u):
        for ts in u.trajectory:
            assert ts.dimensions is None


class TestNCDFReader3(object):
    """NCDF trajectory with box, positions, forces and velocities

    Added to address Issue #2323
    """

    prec = 3

    # Expected coordinates as stored in Angstrom units
    coord_refs = np.array(
        [
            [
                [15.249873, 12.578178, 15.191731],
                [14.925511, 13.58888, 14.944009],
                [15.285703, 14.3409605, 15.645962],
            ],
            [
                [14.799454, 15.214347, 14.714555],
                [15.001984, 15.870884, 13.868363],
                [16.03358, 16.183628, 14.02995],
            ],
        ],
        dtype=np.float32,
    )

    # Expected forces as stored in kcal/(mol*Angstrom)
    frc_refs = np.array(
        [
            [
                [8.583388, 1.8023694, -15.0033455],
                [-21.594835, 39.09166, 6.567963],
                [4.363016, -12.135163, 4.4775457],
            ],
            [
                [-10.106646, -7.870829, -10.385734],
                [7.23599, -12.366022, -9.106191],
                [-4.637955, 11.597565, -6.463743],
            ],
        ],
        dtype=np.float32,
    )

    # Expected velocities as stored in Angstrom per AKMA time unit
    # These are usually associated with a scale_factor of 20.455
    vel_refs = np.array(
        [
            [
                [-0.5301689, -0.16311595, -0.31390688],
                [0.00188578, 0.02513031, -0.2687525],
                [0.84072256, 0.09402391, -0.7457009],
            ],
            [
                [-1.7773226, 1.2307, 0.50276583],
                [-0.13532305, 0.1355039, -0.05567304],
                [-0.6182481, 1.6396415, 0.46686798],
            ],
        ],
        dtype=np.float32,
    )

    # Expected box values as stored in cell_lengths ([:3]) of Angstrom
    # and cell_angles ([:3]) of degree
    box_refs = np.array(
        [
            [28.81876287, 28.27875261, 27.72616397, 90.0, 90.0, 90.0],
            [27.06266081, 26.55555665, 26.03664058, 90.0, 90.0, 90.0],
        ],
        dtype=np.float32,
    )

    @pytest.fixture(scope="class")
    def universe(self):
        return mda.Universe(PRM_NCBOX, TRJ_NCBOX)

    @pytest.mark.parametrize("index,expected", ((0, 0), (8, 1)))
    def test_positions(self, universe, index, expected):
        universe.trajectory[index]
        assert_almost_equal(
            self.coord_refs[expected], universe.atoms.positions[:3], self.prec
        )

    @pytest.mark.parametrize("index,expected", ((0, 0), (8, 1)))
    def test_forces(self, universe, index, expected):
        """Here we multiply the forces by 4.184 to convert from
        kcal to kj in order to verify that MDA has correctly read
        and converted the units from those stored in the NetCDF file.
        """
        universe.trajectory[index]
        assert_almost_equal(
            self.frc_refs[expected] * 4.184,
            universe.atoms.forces[:3],
            self.prec,
        )

    @pytest.mark.parametrize("index,expected", ((0, 0), (8, 1)))
    def test_velocities(self, universe, index, expected):
        """Here we multiply the velocities by 20.455 to match the value of
        `scale_factor` which has been declared in the NetCDF file, which
        should change the values from Angstrom/AKMA time unit to Angstrom/ps.
        """
        universe.trajectory[index]
        assert_almost_equal(
            self.vel_refs[expected] * 20.455,
            universe.atoms.velocities[:3],
            self.prec,
        )

    @pytest.mark.parametrize("index,expected", ((0, 1.0), (8, 9.0)))
    def test_time(self, universe, index, expected):
        assert_almost_equal(
            expected, universe.trajectory[index].time, self.prec
        )

    def test_nframes(self, universe):
        assert_equal(10, universe.trajectory.n_frames)

    def test_dt(self, universe):
        ref = 1.0
        assert_almost_equal(ref, universe.trajectory.dt, self.prec)
        assert_almost_equal(ref, universe.trajectory.ts.dt, self.prec)

    @pytest.mark.parametrize("index,expected", ((0, 0), (8, 1)))
    def test_box(self, universe, index, expected):
        universe.trajectory[index]
        assert_almost_equal(self.box_refs[expected], universe.dimensions)


class TestNCDFReader4(object):
    """NCDF Trajectory exported by cpptaj, without `time` variable."""

    prec = 3

    @pytest.fixture(scope="class")
    def u(self):
        return mda.Universe(CPPTRAJ_TRAJ_TOP, [CPPTRAJ_TRAJ, CPPTRAJ_TRAJ])

    def test_chain_times(self, u):
        """Check times entries for a chain of trajectories without
        a defined time variable"""
        ref_times = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
        time_list = [ts.time for ts in u.trajectory]
        assert ref_times == time_list

    def test_dt(self, u):
        ref = 1.0
        assert u.trajectory.dt == pytest.approx(ref)
        assert u.trajectory.ts.dt == pytest.approx(ref)

    def test_warn_user_no_time_information(self, u):
        wmsg = (
            "NCDF trajectory does not contain `time` information;"
            " `time` will be set as an increasing index"
        )
        with pytest.warns(UserWarning, match=wmsg):
            u2 = mda.Universe(CPPTRAJ_TRAJ_TOP, CPPTRAJ_TRAJ)


class _NCDFGenerator(object):
    """A class for generating abitrary ncdf files and exhaustively test
    edge cases which might not be found in the wild"""

    def create_ncdf(self, params):
        """A basic modular ncdf writer based on :class:`NCDFWriter`"""
        # Create under context manager
        with netcdf_file(
            params["filename"], mode="w", version=params["version_byte"]
        ) as ncdf:
            # Top level attributes
            if params["Conventions"]:
                setattr(ncdf, "Conventions", params["Conventions"])
            if params["ConventionVersion"]:
                setattr(ncdf, "ConventionVersion", params["ConventionVersion"])
            if params["program"]:
                setattr(ncdf, "program", params["program"])
            if params["programVersion"]:
                setattr(ncdf, "programVersion", params["programVersion"])

            # Dimensions
            if params["frame"]:
                ncdf.createDimension("frame", None)
            if params["n_atoms"]:
                ncdf.createDimension("atom", params["n_atoms"])
            if params["spatial"]:
                ncdf.createDimension("spatial", params["spatial"])
            if params["time"]:
                ncdf.createDimension("time", 1)
            ncdf.createDimension("label", 5)
            ncdf.createDimension("cell_spatial", 3)
            ncdf.createDimension("cell_angular", 3)

            # Variables
            if params["time"]:
                time = ncdf.createVariable("time", "d", ("time",))
                setattr(time, "units", params["time"])
                time[:] = 1.0
            cell_spatial = ncdf.createVariable(
                "cell_spatial", "c", ("cell_spatial",)
            )
            cell_spatial[:] = np.asarray(list("abc"))
            cell_angular = ncdf.createVariable(
                "cell_angular", "c", ("cell_angular", "label")
            )
            cell_angular[:] = np.asarray(
                [list("alpha"), list("beta "), list("gamma")]
            )

            # Spatial or atom dependent variables
            if (params["spatial"]) and (params["n_atoms"]):
                spatial = ncdf.createVariable("spatial", "c", ("spatial",))
                spatial[:] = np.asarray(list("xyz")[: params["spatial"]])
                if params["frame"]:
                    if params["coordinates"]:
                        coords = ncdf.createVariable(
                            "coordinates", "f4", ("frame", "atom", "spatial")
                        )
                    velocs = ncdf.createVariable(
                        "velocities", "f4", ("frame", "atom", "spatial")
                    )
                    forces = ncdf.createVariable(
                        "forces", "f4", ("frame", "atom", "spatial")
                    )
                    cell_lengths = ncdf.createVariable(
                        "cell_lengths", "f8", ("frame", "cell_spatial")
                    )
                    cell_angles = ncdf.createVariable(
                        "cell_angles", "f8", ("frame", "cell_angular")
                    )
                else:
                    if params["coordinates"]:
                        coords = ncdf.createVariable(
                            "coordinates", "f8", ("atom", "spatial")
                        )
                    cell_lengths = ncdf.createVariable(
                        "cell_lengths", "f8", ("cell_spatial",)
                    )
                    cell_angles = ncdf.createVariable(
                        "cell_angles", "f8", ("cell_angular",)
                    )
                    velocs = ncdf.createVariable(
                        "velocities", "f8", ("atom", "spatial")
                    )
                    forces = ncdf.createVariable(
                        "forces", "f8", ("atom", "spatial")
                    )

                # Set units
                if params["coordinates"]:
                    setattr(coords, "units", params["coordinates"])
                setattr(velocs, "units", params["velocities"])
                setattr(forces, "units", params["forces"])
                setattr(cell_lengths, "units", params["cell_lengths"])
                setattr(cell_angles, "units", params["cell_angles"])

                # Assign value
                if params["frame"]:
                    for index in range(params["frame"]):
                        if params["coordinates"]:
                            coords[index, :] = np.asarray(
                                range(params["spatial"]), dtype=np.float32
                            )
                        cell_lengths[index, :] = np.array(
                            [20.0, 20.0, 20.0], dtype=np.float32
                        )
                        cell_angles[index, :] = np.array(
                            [90.0, 90.0, 90.0], dtype=np.float32
                        )
                        velocs[index, :] = np.asarray(
                            range(params["spatial"]), dtype=np.float32
                        )
                        forces[index, :] = np.asarray(
                            range(params["spatial"]), dtype=np.float32
                        )
                else:
                    if params["coordinates"]:
                        coords[:] = np.asarray(
                            range(params["spatial"]), dtype=np.float32
                        )
                    cell_lengths[:] = np.array(
                        [20.0, 20.0, 20.0], dtype=np.float32
                    )
                    cell_angles[:] = np.array(
                        [90.0, 90.0, 90.0], dtype=np.float32
                    )
                    velocs[:] = np.asarray(
                        range(params["spatial"]), dtype=np.float32
                    )
                    forces[:] = np.asarray(
                        range(params["spatial"]), dtype=np.float32
                    )

            # self.scale_factor overrides which variable gets a scale_factor
            if params["scale_factor"]:
                setattr(
                    ncdf.variables[params["scale_factor"]],
                    "scale_factor",
                    params["scale_factor_value"],
                )

    def gen_params(self, keypair=None, restart=False):
        """Generate writer parameters, keypair can be used to overwrite
        given dictonary entries (expects dictionary)
        """

        params = {
            "filename": "test.nc",
            "version_byte": 2,
            "Conventions": "AMBER",
            "ConventionVersion": "1.0",
            "program": "mda test_writer",
            "programVersion": "V42",
            "n_atoms": 1,
            "spatial": 3,
            "coordinates": "angstrom",
            "velocities": "angstrom/picosecond",
            "forces": "kilocalorie/mole/angstrom",
            "cell_lengths": "angstrom",
            "cell_angles": "degree",
            "time": "picosecond",
            "scale_factor": None,
            "scale_factor_value": 2.0,
            "frame": 2,
        }

        if restart:
            params["filename"] = "test.ncrst"
            params["frame"] = None

        if keypair:
            for entry in keypair:
                params[entry] = keypair[entry]

        return params


class TestScaleFactorImplementation(_NCDFGenerator):

    prec = 5

    def test_scale_factor_coordinates(self, tmpdir):
        mutation = {"scale_factor": "coordinates"}
        params = self.gen_params(keypair=mutation, restart=False)
        expected = np.asarray(range(3), dtype=np.float32) * 2.0
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            u = mda.Universe(params["filename"], to_guess=())
            for ts in u.trajectory:
                assert_almost_equal(ts.positions[0], expected, self.prec)

    def test_scale_factor_velocities(self, tmpdir):
        mutation = {"scale_factor": "velocities", "scale_factor_value": 3.0}
        params = self.gen_params(keypair=mutation, restart=False)
        expected = np.asarray(range(3), dtype=np.float32) * 3.0
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            u = mda.Universe(params["filename"], to_guess=())
            for ts in u.trajectory:
                assert_almost_equal(ts.velocities[0], expected, self.prec)

    def test_scale_factor_forces(self, tmpdir):
        mutation = {"scale_factor": "forces", "scale_factor_value": 10.0}
        params = self.gen_params(keypair=mutation, restart=False)
        expected = np.asarray(range(3), dtype=np.float32) * 10.0 * 4.184
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            u = mda.Universe(params["filename"], to_guess=())
            for ts in u.trajectory:
                assert_almost_equal(ts.forces[0], expected, self.prec)

    @pytest.mark.parametrize(
        "mutation,expected",
        (
            (
                {"scale_factor": "cell_lengths", "scale_factor_value": 0.75},
                np.array([15.0, 15.0, 15.0, 90.0, 90.0, 90.0]),
            ),
            (
                {"scale_factor": "cell_angles", "scale_factor_value": 0.5},
                np.array([20.0, 20.0, 20.0, 45.0, 45.0, 45.0]),
            ),
        ),
    )
    def test_scale_factor_box(self, tmpdir, mutation, expected):
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            u = mda.Universe(params["filename"], to_guess=())
            for ts in u.trajectory:
                assert_almost_equal(ts.dimensions, expected, self.prec)

    def test_scale_factor_not_float(self, tmpdir):
        mutation = {
            "scale_factor": "coordinates",
            "scale_factor_value": "parsnips",
        }
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            errmsg = "b'parsnips' is not a float"
            with pytest.raises(TypeError, match=errmsg):
                u = mda.Universe(params["filename"])


class TestNCDFReaderExceptionsWarnings(_NCDFGenerator):

    @pytest.mark.parametrize(
        "mutation",
        [{"Conventions": "Foo"}, {"version_byte": 1}, {"spatial": 2}],
    )
    def test_type_errors(self, tmpdir, mutation):
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.raises(TypeError):
                NCDFReader(params["filename"])

    @pytest.mark.parametrize(
        "mutation",
        [
            {"Conventions": None},
            {"ConventionVersion": None},
            {"spatial": None},
            {"n_atoms": None},
            {"frame": None},
        ],
    )
    def test_value_errors(self, tmpdir, mutation):
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.raises(ValueError):
                NCDFReader(params["filename"])

    @pytest.mark.parametrize(
        "mutation",
        [
            {"scale_factor": "cell_spatial"},
            {"time": "femtosecond"},
            {"coordinates": "nanometer"},
            {"velocities": "angstrom/akma"},
            {"forces": "kilojoule/mole/angstrom"},
            {"cell_lengths": "nanometer"},
            {"cell_angles": "radians"},
        ],
    )
    def test_notimplemented_errors(self, tmpdir, mutation):
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.raises(NotImplementedError):
                NCDFReader(params["filename"])

    @pytest.mark.parametrize(
        "evaluate,expected", (("yard", "foot"), ("second", "minute"))
    )
    def test_verify_units_errors(self, evaluate, expected):
        """Directly tests expected failures of _verify_units"""
        with pytest.raises(NotImplementedError):
            NCDFReader._verify_units(evaluate.encode("utf-8"), expected)

    def test_ioerror(self, tmpdir):
        params = self.gen_params(restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.raises(IOError):
                u = mda.Universe(params["filename"], to_guess=())
                u.trajectory.close()
                u.trajectory[-1]

    def test_conventionversion_warn(self, tmpdir):
        mutation = {"ConventionVersion": "2.0"}
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.warns(UserWarning) as record:
                NCDFReader(params["filename"])

            assert len(record) == 1
            wmsg = (
                "NCDF trajectory format is 2.0 but the reader "
                "implements format 1.0"
            )
            assert str(record[0].message.args[0]) == wmsg

    @pytest.mark.parametrize(
        "mutation", [{"program": None}, {"programVersion": None}]
    )
    def test_program_warn(self, tmpdir, mutation):
        params = self.gen_params(keypair=mutation, restart=False)
        with tmpdir.as_cwd():
            self.create_ncdf(params)
            with pytest.warns(UserWarning) as record:
                NCDFReader(params["filename"])

            assert len(record) == 1
            wmsg = (
                "NCDF trajectory test.nc may not fully adhere to AMBER "
                "standards as either the `program` or `programVersion` "
                "attributes are missing"
            )
            assert str(record[0].message.args[0]) == wmsg

    def test_no_dt_warning(self, tmpdir):
        """Issue 3166 - not being able to call dt should throw a warning.
        Also at the same time checks that single frame chain reading works"""
        u = mda.Universe(PFncdf_Top, PFncdf_Trj)
        with tmpdir.as_cwd():
            with NCDFWriter("single_frame.nc", u.atoms.n_atoms) as W:
                W.write(u)

            # Using the ChainReader implicitly calls dt() and thus _get_dt()
            wmsg = "Reader has no dt information, set to 1.0 ps"
            with pytest.warns(UserWarning, match=wmsg):
                u2 = mda.Universe(PFncdf_Top, [PFncdf_Trj, "single_frame.nc"])


class _NCDFWriterTest(object):
    prec = 5

    @pytest.fixture()
    def universe(self):
        return mda.Universe(self.topology, self.filename)

    @pytest.fixture(params=["nc", "ncdf"])
    def outfile_extensions(self, tmpdir, request):
        # Issue 3030, test all extensions of NCDFWriter
        ext = request.param
        return str(tmpdir) + f"ncdf-writer-1.{ext}"

    @pytest.fixture()
    def outfile(self, tmpdir):
        return str(tmpdir) + "ncdf-writer-1.ncdf"

    @pytest.fixture()
    def outtop(self, tmpdir):
        return str(tmpdir) + "ncdf-writer-top.pdb"

    def _test_write_trajectory(self, universe, outfile):
        # explicit import so that we can artifically remove netCDF4
        # before calling
        from MDAnalysis.coordinates import TRJ

        t = universe.trajectory
        with TRJ.NCDFWriter(outfile, t.n_atoms, dt=t.dt) as W:
            self._copy_traj(W, universe)
        self._check_new_traj(universe, outfile)
        # for issue #518 -- preserve float32 data in ncdf output
        # NOTE: This originally failed with the dtype('>f4') instead
        #       of dtype('<f4') == dtype('f') == np.float32, i.e. then
        #       endianness is different. The current hack-ish solution
        #       ignores endianness by comparing the name of the types,
        #       which should be "float32".
        #       See http://docs.scipy.org/doc/numpy-1.10.0/reference/arrays.dtypes.html
        #       and https://github.com/MDAnalysis/mdanalysis/pull/503
        dataset = netcdf_file(outfile, "r")
        coords = dataset.variables["coordinates"]
        time = dataset.variables["time"]
        assert_equal(
            coords[:].dtype.name,
            np.dtype(np.float32).name,
            err_msg="ncdf coord output not float32 "
            "but {}".format(coords[:].dtype),
        )
        assert_equal(
            time[:].dtype.name,
            np.dtype(np.float32).name,
            err_msg="ncdf time output not float32 "
            "but {}".format(time[:].dtype),
        )

    def test_write_trajectory_netCDF4(self, universe, outfile):
        pytest.importorskip("netCDF4")
        return self._test_write_trajectory(universe, outfile)

    def test_write_trajectory_netcdf(self, universe, outfile):
        import MDAnalysis.coordinates.TRJ

        loaded_netCDF4 = sys.modules["MDAnalysis.coordinates.TRJ"].netCDF4
        try:
            # cannot use @block_import('netCDF4') because TRJ was already imported
            # during setup() and already sits in the global module list so we just
            # set it to None because that is what TRJ does if it cannot find netCDF4
            sys.modules["MDAnalysis.coordinates.TRJ"].netCDF4 = None
            assert (
                MDAnalysis.coordinates.TRJ.netCDF4 is None
            )  # should happen if netCDF4 not found
            return self._test_write_trajectory(universe, outfile)
        finally:
            sys.modules["MDAnalysis.coordinates.TRJ"].netCDF4 = loaded_netCDF4

    def test_OtherWriter(self, universe, outfile_extensions):
        t = universe.trajectory
        with t.OtherWriter(outfile_extensions) as W:
            self._copy_traj(W, universe)
        self._check_new_traj(universe, outfile_extensions)

    def _copy_traj(self, writer, universe):
        for ts in universe.trajectory:
            writer.write(universe)

    def _check_new_traj(self, universe, outfile):
        uw = mda.Universe(self.topology, outfile)

        # check that the trajectories are identical for each time step
        for orig_ts, written_ts in zip(universe.trajectory, uw.trajectory):
            assert_almost_equal(
                written_ts._pos,
                orig_ts._pos,
                self.prec,
                err_msg="coordinate mismatch between "
                "original and written trajectory at "
                "frame %d (orig) vs %d (written)"
                % (orig_ts.frame, written_ts.frame),
            )
            # not a good test because in the example trajectory all times are 0
            assert_almost_equal(
                orig_ts.time,
                written_ts.time,
                self.prec,
                err_msg="Time for step {0} are not the "
                "same.".format(orig_ts.frame),
            )
            assert_almost_equal(
                written_ts.dimensions,
                orig_ts.dimensions,
                self.prec,
                err_msg="unitcells are not identical",
            )
        # check that the NCDF data structures are the same
        nc_orig = universe.trajectory.trjfile
        nc_copy = uw.trajectory.trjfile

        # note that here 'dimensions' is a specific netcdf data structure and
        # not the unit cell dimensions in MDAnalysis
        for k, dim in nc_orig.dimensions.items():
            try:
                dim_new = nc_copy.dimensions[k]
            except KeyError:
                raise AssertionError(
                    "NCDFWriter did not write " "dimension '{0}'".format(k)
                )
            else:
                assert_equal(
                    dim,
                    dim_new,
                    err_msg="Dimension '{0}' size mismatch".format(k),
                )

        for k, v in nc_orig.variables.items():
            try:
                v_new = nc_copy.variables[k]
            except KeyError:
                raise AssertionError(
                    "NCDFWriter did not write " "variable '{0}'".format(k)
                )
            else:
                try:
                    assert_almost_equal(
                        v[:],
                        v_new[:],
                        self.prec,
                        err_msg="Variable '{0}' not "
                        "written correctly".format(k),
                    )
                except TypeError:
                    assert_equal(
                        v[:],
                        v_new[:],
                        err_msg="Variable {0} not written "
                        "correctly".format(k),
                    )

    def test_TRR2NCDF(self, outfile):
        trr = mda.Universe(GRO, TRR)
        with mda.Writer(
            outfile, trr.trajectory.n_atoms, velocities=True, format="ncdf"
        ) as W:
            for ts in trr.trajectory:
                W.write(trr)

        uw = mda.Universe(GRO, outfile)

        for orig_ts, written_ts in zip(trr.trajectory, uw.trajectory):
            assert_almost_equal(
                written_ts._pos,
                orig_ts._pos,
                self.prec,
                err_msg="coordinate mismatch between "
                "original and written trajectory at "
                "frame {0} (orig) vs {1} (written)".format(
                    orig_ts.frame, written_ts.frame
                ),
            )
            assert_almost_equal(
                written_ts._velocities,
                orig_ts._velocities,
                self.prec,
                err_msg="velocity mismatch between "
                "original and written trajectory at "
                "frame {0} (orig) vs {1} (written)".format(
                    orig_ts.frame, written_ts.frame
                ),
            )
            assert_almost_equal(
                orig_ts.time,
                written_ts.time,
                self.prec,
                err_msg="Time for step {0} are not the "
                "same.".format(orig_ts.frame),
            )
            assert_almost_equal(
                written_ts.dimensions,
                orig_ts.dimensions,
                self.prec,
                err_msg="unitcells are not identical",
            )
        del trr

    def test_write_AtomGroup(self, universe, outfile, outtop):
        """test to write NCDF from AtomGroup (Issue 116)"""
        p = universe.select_atoms("not resname WAT")
        p.write(outtop)
        with mda.Writer(outfile, n_atoms=p.n_atoms, format="ncdf") as W:
            for ts in universe.trajectory:
                W.write(p)

        uw = mda.Universe(outtop, outfile)
        pw = uw.atoms

        for orig_ts, written_ts in zip(universe.trajectory, uw.trajectory):
            assert_almost_equal(
                p.positions,
                pw.positions,
                self.prec,
                err_msg="coordinate mismatch between "
                "original and written trajectory at "
                "frame %d (orig) vs %d (written)"
                % (orig_ts.frame, written_ts.frame),
            )
            assert_almost_equal(
                orig_ts.time,
                written_ts.time,
                self.prec,
                err_msg="Time for step {0} are not the "
                "same.".format(orig_ts.frame),
            )
            assert_almost_equal(
                written_ts.dimensions,
                orig_ts.dimensions,
                self.prec,
                err_msg="unitcells are not identical",
            )


class TestNCDFWriter(_NCDFWriterTest, RefVGV):
    pass


class TestNCDFWriterTZ2(_NCDFWriterTest, RefTZ2):
    pass


class TestNCDFWriterVelsForces(object):
    """Test writing NCDF trajectories with a mixture of options"""

    prec = 3
    top = XYZ_mini
    n_atoms = 3

    @pytest.fixture()
    def u1(self):
        u = make_Universe(
            size=(self.n_atoms, 1, 1),
            trajectory=True,
            velocities=True,
            forces=True,
        )
        # Memory reader so changes should be in-place
        u.atoms.velocities += 100
        u.atoms.forces += 200
        return u

    @pytest.fixture()
    def u2(self):
        u = make_Universe(
            size=(self.n_atoms, 1, 1),
            trajectory=True,
            velocities=True,
            forces=True,
        )
        # Memory reader so changes should be in-place
        u.atoms.positions += 300
        u.atoms.velocities += 400
        u.atoms.forces += 500
        return u

    @pytest.mark.parametrize(
        "pos, vel, force",
        (
            (True, False, False),
            (True, True, False),
            (True, False, True),
            (True, True, True),
        ),
    )
    def test_write_u(self, pos, vel, force, tmpdir, u1, u2):
        """Write the two reference universes, then open them up and check values

        pos vel and force are bools which define whether these properties
        should be in universe
        """
        outfile = str(tmpdir) + "ncdf-write-vels-force.ncdf"
        with NCDFWriter(
            outfile, n_atoms=self.n_atoms, velocities=vel, forces=force
        ) as w:
            w.write(u1)
            w.write(u2)

        # test that the two reference states differ
        for ts1, ts2 in zip(u1.trajectory, u2.trajectory):
            assert_almost_equal(ts1._pos + 300, ts2._pos)
            assert_almost_equal(ts1._velocities + 300, ts2._velocities)
            assert_almost_equal(ts1._forces + 300, ts2._forces)

        u = mda.Universe(self.top, outfile)
        # check the trajectory contents match reference universes
        for ts, ref_ts in zip(
            u.trajectory, [u1.trajectory.ts, u2.trajectory.ts]
        ):
            if pos:
                assert_almost_equal(ts._pos, ref_ts._pos, self.prec)
            else:
                with pytest.raises(mda.NoDataError):
                    getattr(ts, "positions")
            if vel:
                assert_almost_equal(
                    ts._velocities, ref_ts._velocities, self.prec
                )
            else:
                with pytest.raises(mda.NoDataError):
                    getattr(ts, "velocities")
            if force:
                assert_almost_equal(ts._forces, ref_ts._forces, self.prec)
            else:
                with pytest.raises(mda.NoDataError):
                    getattr(ts, "forces")

        u.trajectory.close()


class TestNCDFWriterScaleFactors:
    """Class to check that the NCDF Writer properly handles the
    application of scale factors"""

    @pytest.fixture()
    def outfile(self, tmpdir):
        return str(tmpdir) + "ncdf-write-scale.ncdf"

    @pytest.fixture()
    def outfile2(self, tmpdir):
        return str(tmpdir) + "ncdf-write-scale2.ncdf"

    @pytest.fixture()
    def universe(self):
        return mda.Universe(PRM_NCBOX, TRJ_NCBOX)

    def get_scale_factors(self, ncdfile):
        """Get a dictionary of scale factors stored in netcdf file"""
        sfactors = {}
        # being overly cautious by setting mmap to False, probably would
        # be faster & ok to set it to True
        with netcdf_file(ncdfile, mmap=False) as f:
            for var in f.variables:
                if hasattr(f.variables[var], "scale_factor"):
                    sfactors[var] = f.variables[var].scale_factor

        return sfactors

    def get_variable(self, ncdfile, variable, frame):
        """Return a variable array from netcdf file"""
        with netcdf_file(ncdfile, mmap=False) as f:
            return f.variables[variable][frame]

    def test_write_read_factors_default(self, outfile, universe):
        with universe.trajectory.Writer(outfile) as W:
            W.write(universe.atoms)

        # check scale_factors
        sfactors = self.get_scale_factors(outfile)
        assert len(sfactors) == 1
        assert sfactors["velocities"] == 20.455

    def test_write_bad_scale_factor(self, outfile, universe):
        errmsg = "scale_factor parsnips is not a float"
        with pytest.raises(TypeError, match=errmsg):
            NCDFWriter(
                outfile,
                n_atoms=len(universe.atoms),
                scale_velocities="parsnips",
            )

    @pytest.mark.parametrize(
        "stime, slengths, sangles, scoords, svels, sfrcs",
        (
            (-2.0, -2.0, -2.0, -2.0, -2.0, -2.0),
            (1.0, 1.0, 1.0, 1.0, 1.0, 1.0),
            (2.0, 4.0, 8.0, 16.0, 32.0, 64.0),
        ),
    )
    def test_write_read_write(
        self,
        outfile,
        outfile2,
        universe,
        stime,
        slengths,
        sangles,
        scoords,
        svels,
        sfrcs,
    ):
        """Write out a file with assorted scale_factors, then
        read it back in, then write it out to make sure that the
        new assorted scale_factors have been retained by Write"""

        with NCDFWriter(
            outfile,
            n_atoms=len(universe.atoms),
            velocities=True,
            forces=True,
            scale_time=stime,
            scale_cell_lengths=slengths,
            scale_cell_angles=sangles,
            scale_coordinates=scoords,
            scale_velocities=svels,
            scale_forces=sfrcs,
        ) as W:
            for ts in universe.trajectory:
                W.write(universe.atoms)

        universe2 = mda.Universe(PRM_NCBOX, outfile)

        # Write back checking that the same scale_factors as used
        with universe2.trajectory.Writer(outfile2) as OWriter:
            OWriter.write(universe2.atoms)

        universe3 = mda.Universe(PRM_NCBOX, outfile2)

        # check stored scale_factors
        sfactors1 = self.get_scale_factors(outfile)
        sfactors2 = self.get_scale_factors(outfile2)

        assert sfactors1 == sfactors2
        assert len(sfactors1) == 6
        assert sfactors1["time"] == stime
        assert sfactors1["cell_lengths"] == slengths
        assert sfactors1["cell_angles"] == sangles
        assert sfactors1["coordinates"] == scoords
        assert sfactors1["velocities"] == svels
        assert sfactors1["forces"] == sfrcs

        # check that the stored values are indeed scaled
        assert_almost_equal(
            universe.trajectory.time / stime,
            self.get_variable(outfile, "time", 0),
            4,
        )
        assert_almost_equal(
            universe.dimensions[:3] / slengths,
            self.get_variable(outfile, "cell_lengths", 0),
            4,
        )
        assert_almost_equal(
            universe.dimensions[3:] / sangles,
            self.get_variable(outfile, "cell_angles", 0),
            4,
        )
        assert_almost_equal(
            universe.atoms.positions / scoords,
            self.get_variable(outfile, "coordinates", 0),
            4,
        )
        assert_almost_equal(
            universe.atoms.velocities / svels,
            self.get_variable(outfile, "velocities", 0),
            4,
        )
        # note: kJ/mol -> kcal/mol = 4.184 conversion
        assert_almost_equal(
            universe.atoms.forces / (sfrcs * 4.184),
            self.get_variable(outfile, "forces", 0),
            4,
        )

        # check that the individual components were saved/read properly
        for ts1, ts3 in zip(universe.trajectory, universe3.trajectory):
            assert_almost_equal(ts1.time, ts3.time)
            assert_almost_equal(ts1.dimensions, ts3.dimensions)
            assert_almost_equal(
                universe.atoms.positions, universe3.atoms.positions, 4
            )
            assert_almost_equal(
                universe.atoms.velocities, universe3.atoms.velocities, 4
            )
            assert_almost_equal(
                universe.atoms.forces, universe3.atoms.forces, 4
            )


class TestScipyScaleFactors(TestNCDFWriterScaleFactors):
    """As above, but netCDF4 is disabled since scaleandmask is different
    between the two libraries"""

    @pytest.fixture(autouse=True)
    def block_netcdf4(self, monkeypatch):
        monkeypatch.setattr(
            sys.modules["MDAnalysis.coordinates.TRJ"], "netCDF4", None
        )

    def test_ncdf4_not_present(self, outfile, universe):
        # whilst we're here, let's also test this warning
        with pytest.warns(UserWarning, match="Could not find netCDF4 module"):
            with universe.trajectory.Writer(outfile) as W:
                W.write(universe.atoms)


class TestNCDFWriterUnits(object):
    """Tests that the writer adheres to AMBER convention units"""

    @pytest.fixture()
    def outfile(self, tmpdir):
        return str(tmpdir) + "ncdf-writer-1.ncdf"

    @pytest.mark.parametrize(
        "var, expected",
        (
            ("coordinates", "angstrom"),
            ("time", "picosecond"),
            ("cell_lengths", "angstrom"),
            ("cell_angles", "degree"),
            ("velocities", "angstrom/picosecond"),
            ("forces", "kilocalorie/mole/angstrom"),
        ),
    )
    def test_writer_units(self, outfile, var, expected):
        trr = mda.Universe(DLP_CONFIG, format="CONFIG")

        with mda.Writer(
            outfile,
            trr.trajectory.n_atoms,
            velocities=True,
            forces=True,
            format="ncdf",
        ) as W:
            for ts in trr.trajectory:
                W.write(trr)

        with netcdf_file(outfile, mode="r") as ncdf:
            unit = ncdf.variables[var].units.decode("utf-8")
            assert_equal(unit, expected)


class TestNCDFWriterErrorsWarnings(object):
    @pytest.fixture()
    def outfile(self, tmpdir):
        return str(tmpdir) + "out.ncdf"

    def test_zero_atoms_VE(self, outfile):
        with pytest.raises(ValueError):
            NCDFWriter(outfile, 0)

    def test_wrong_n_atoms(self, outfile):
        with NCDFWriter(outfile, 100) as w:
            u = make_Universe(trajectory=True)
            with pytest.raises(IOError):
                w.write(u)