Example #1
0
    def test_checksum_compressed(self):
        # Setup the test_file
        cdf_spec = {'Compressed': 6, 'Checksum': True}
        self.checksum_test_file = cdfwrite.CDF(os.path.dirname(__file__) +
                                               "/testfiles/testing_checksum.cdf",
                                               cdf_spec=cdf_spec)
        var_spec = {}
        var_spec['Variable'] = 'Variable1'
        var_spec['Data_Type'] = 8
        var_spec['Num_Elements'] = 1
        var_spec['Rec_Vary'] = True
        var_spec['Dim_Sizes'] = []
        varatts = {}
        varatts['Attribute1'] = 1
        varatts['Attribute2'] = '500'

        self.checksum_test_file.write_var(var_spec, var_attrs=varatts,
                                          var_data=np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

        # Close the file so we can read
        self.checksum_test_file.close()

        # Open the file to read
        self.test_file_reader = cdfread.CDF(os.path.dirname(__file__) +
                                            "/testfiles/testing_checksum.cdf",
                                            validate=True)
        # Test CDF info
        var = self.test_file_reader.varget("Variable1")
        for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
            self.assertEqual(var[x], x)

        # Close the reading file
        self.test_file_reader.close()
Example #2
0
    def test_create_2d_r_and_z_variables(self):
        # Create a new test_file
        self.test_file.close()
        os.remove(os.path.dirname(__file__)+"/testfiles/testing.cdf")
        cdf_spec = {'rDim_sizes': [2, 20]}
        self.test_file = cdfwrite.CDF(os.path.dirname(__file__) +
                                      "/testfiles/testing.cdf",
                                      cdf_spec=cdf_spec)

        # Setup the test_file
        var_spec = {}
        var_spec['Variable'] = 'Variable1'
        var_spec['Var_Type'] = 'rvariable'
        var_spec['Data_Type'] = 22
        var_spec['Num_Elements'] = 1
        var_spec['Rec_Vary'] = True
        var_spec['Dim_Sizes'] = []
        var_spec['Dim_Vary'] = [True, False]

        self.test_file.write_var(var_spec, var_data=np.array([[0, 1],
                                                              [2, 3],
                                                              [4, 5],
                                                              [6, 7],
                                                              [8, 9]]))

        var_spec['Variable'] = 'Variable2'
        var_spec['Var_Type'] = 'zvariable'
        varatts = {}
        varatts['Attribute1'] = 2
        varatts['Attribute2'] = '1000'
        self.test_file.write_var(var_spec, var_attrs=varatts,
                                 var_data=np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))

        # Close the file so we can read
        self.test_file.close()

        # Open the file to read
        self.test_file_reader = cdfread.CDF(os.path.dirname(__file__) +
                                            "/testfiles/testing.cdf")

        # Test CDF info
        varinfo = self.test_file_reader.varinq("Variable1")
        self.assertEqual(varinfo['Data_Type'], 22)
        var = self.test_file_reader.varget("Variable1")
        for x in [0, 1, 2, 3, 4]:
            self.assertEqual(var[x][0], 2*x)
            self.assertEqual(var[x][1], 2*x+1)
        var = self.test_file_reader.varget("Variable2")
        for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
            self.assertEqual(var[x], x)

        att = self.test_file_reader.attget("Attribute1", entry='Variable2')
        self.assertEqual(att['Data'], [2])
        att = self.test_file_reader.attget("Attribute2", entry='Variable2')
        self.assertEqual(att['Data'], '1000')

        # Close the reading file
        self.test_file_reader.close()
Example #3
0
 def setUp(self):
     if os.path.isfile(os.path.dirname(__file__) + "/testfiles/testing.cdf"):
         os.remove(os.path.dirname(__file__) + "/testfiles/testing.cdf")
     cdf_spec = {'rDim_sizes': [1]}
     self.test_file = cdfwrite.CDF(os.path.dirname(__file__) +
                                   "/testfiles/testing.cdf", cdf_spec=cdf_spec)
     self.test_file_reader = False
     self.compressed_test_file = False
     self.checksum_test_file = False
     return
Example #4
0
    def test_create_2d_rvariable(self):
        # Create a new test_file
        self.test_file.close()
        os.remove(os.path.dirname(__file__)+"/testfiles/testing.cdf")
        cdf_spec = {'rDim_sizes': [2, 2]}
        self.test_file = cdfwrite.CDF(os.path.dirname(__file__) +
                                      "/testfiles/testing.cdf", cdf_spec=cdf_spec)

        # Setup the test_file
        var_spec = {}
        var_spec['Variable'] = 'Variable1'
        var_spec['Var_Type'] = 'rvariable'
        var_spec['Data_Type'] = 14
        var_spec['Num_Elements'] = 1
        var_spec['Rec_Vary'] = True
        var_spec['Dim_Sizes'] = []
        var_spec['Dim_Vary'] = [True, True]

        self.test_file.write_var(var_spec, var_data=np.array([[[0, 1], [1, 2]],
                                                              [[2, 3], [3, 4]],
                                                              [[4, 5], [5, 6]],
                                                              [[6, 7], [7, 8]],
                                                              [[8, 9], [9, 10]]]))

        # Close the file so we can read
        self.test_file.close()

        # Open the file to read
        self.test_file_reader = cdfread.CDF(os.path.dirname(__file__) +
                                            "/testfiles/testing.cdf")

        # Test CDF info
        varinfo = self.test_file_reader.varinq("Variable1")
        self.assertEqual(varinfo['Data_Type'], 14)
        var = self.test_file_reader.varget("Variable1")
        for x in [0, 1, 2, 3, 4]:
            self.assertEqual(var[x][0][0], 2*x)
            self.assertEqual(var[x][0][1], 2*x+1)
            self.assertEqual(var[x][1][0], 2*x+1)
            self.assertEqual(var[x][1][1], 2*x+2)

        # Close the reading file
        self.test_file_reader.close()
Example #5
0
def cdf_create(fn: Path, spec: dict):
    # str(fn) is a Python==3.5 workaround
    return cdfwrite.CDF(str(fn), cdf_spec=spec)
Example #6
0
def cdf_create(fn: Path, spec: dict):
    return cdfwrite.CDF(fn, cdf_spec=spec)
Example #7
0
#!env python3
from cdflib import cdfwrite, cdfepoch
from datetime import datetime, timedelta
import numpy as np
import math
import os

cd = cdfwrite.CDF("cd_with_time.cdf")
cd.write_globalattrs({
    "epoch": {
        0: [
            cdflib.cdfepoch.compute_epoch([0, 1, 1, 0, 0, 0, 0, 0, 0, 0]),
            'cdf_epoch'
        ]
    },
    "epoch16": {
        0: [
            cdflib.cdfepoch.compute_epoch16([0, 1, 1, 0, 0, 0, 0, 0, 0, 0]),
            cdflib.cdfepoch.compute_epoch16([0, 1, 2, 0, 0, 0, 0, 0, 0, 0]),
            cdflib.cdfepoch.compute_epoch16([0, 1, 1, 0, 0, 0, 0, 0, 0, 1]),
            'cdf_epoch16'
        ]
    },
    "tt2000": {
        0: ["0", 'cdf_tt2000']
    }
})
Example #8
0
 def _cdf_create(fn: Path, spec: dict):
     return cdfwrite.CDF(fn, cdf_spec=spec, delete=True)