Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.




CDFlib Example

This Jupyter Notebook demonstrates the reading and writing of CDF files using the CDFlib Python package.

Contents

  1. Prerequisites
  2. Import Packages
  3. Reading CDFs
  4. Writing CDFs

Prerequisites

Import Packages

import glob
import numpy as np
import datetime as dt

import cdflib
from cdflib import xarray

print('CDFlib Version\n{}'.format(cdflib.__version__))
CDFlib Version
1.2.4

Reading CDFs

Load Local File

Please put CDF file in the same folder/location as script.

# CDF File
filename = glob.glob('mms*_edp_*.cdf')[0]
print('Filename \n{}'.format(filename))

# Read CDF [Method #1: CDFlib]
cdf1 = cdflib.CDF(filename)
print('\nRead Type: CDFlib \n{}'.format(type(cdf1)))

# Read CDF [Method #2: Xarray]
cdf2 = xarray.cdf_to_xarray(filename,to_unixtime=True,fillval_to_nan=True)
print('\nRead Type: Xarray \n{}'.format(type(cdf2)))
Filename 
mms1_edp_fast_l2_scpot_20201029000000_v2.7.3.cdf

Read Type: CDFlib 
<class 'cdflib.cdfread.CDF'>

Read Type: Xarray 
<class 'xarray.core.dataset.Dataset'>

Global Metadata

# CDF Summary [Method #1: CDFlib]
print('CDF Summary\n{}'.format(cdf1.cdf_info().Attributes))

# Individual Global Variables
print('\n\nIndividual Global Variables')
for variable in cdf1.globalattsget():
    print('{}: {}\n'.format(variable,cdf1.globalattsget()[variable]))
CDF Summary
[{'Data_type': 'Global'}, {'Data_version': 'Global'}, {'Descriptor': 'Global'}, {'Discipline': 'Global'}, {'Calibration_file': 'Global'}, {'Generation_date': 'Global'}, {'Instrument_type': 'Global'}, {'Logical_file_id': 'Global'}, {'Logical_source': 'Global'}, {'Logical_source_description': 'Global'}, {'File_naming_convention': 'Global'}, {'Mission_group': 'Global'}, {'PI_affiliation': 'Global'}, {'PI_name': 'Global'}, {'Project': 'Global'}, {'Source_name': 'Global'}, {'TEXT': 'Global'}, {'HTTP_LINK': 'Global'}, {'LINK_TEXT': 'Global'}, {'LINK_TITLE': 'Global'}, {'MODS': 'Global'}, {'Acknowledgement': 'Global'}, {'Generated_by': 'Global'}, {'Skeleton_version': 'Global'}, {'Rules_of_use': 'Global'}, {'Time_resolution': 'Global'}, {'Parents': 'Global'}, {'CATDESC': 'Variable'}, {'COORDINATE_SYSTEM': 'Variable'}, {'DELTA_PLUS_VAR': 'Variable'}, {'DELTA_MINUS_VAR': 'Variable'}, {'DEPEND_0': 'Variable'}, {'DEPEND_1': 'Variable'}, {'DISPLAY_TYPE': 'Variable'}, {'FIELDNAM': 'Variable'}, {'FILLVAL': 'Variable'}, {'FORMAT': 'Variable'}, {'FORM_PTR': 'Variable'}, {'LABLAXIS': 'Variable'}, {'LABL_PTR_1': 'Variable'}, {'REPRESENTATION_1': 'Variable'}, {'SI_CONVERSION': 'Variable'}, {'TENSOR_ORDER': 'Variable'}, {'UNITS': 'Variable'}, {'UNIT_PTR': 'Variable'}, {'VALIDMIN': 'Variable'}, {'VALIDMAX': 'Variable'}, {'VAR_TYPE': 'Variable'}, {'MONOTON': 'Variable'}, {'TIME_BASE': 'Variable'}]


Individual Global Variables
Data_type: ['fast_l2_scpot']

Data_version: ['v2.7.3']

Descriptor: ['EDP>Electric Double Probe']

Discipline: ['Space Physics>Magnetospheric Science']

Calibration_file: ['mms1_edp_sdp_scpot_20160204_v0.0.0']

Generation_date: ['20201031']

Instrument_type: ['Electric Fields (space)']

Logical_file_id: ['mms1_edp_fast_l2_scpot_20201029000000_v2.7.3']

Logical_source: ['mms1_edp_fast_l2_scpot']

Logical_source_description: ['MMS 1 dual probe scpot (fast), Spacecraft potential']

File_naming_convention: ['source_descriptor_datatype_yyyyMMddHHmmss']

Mission_group: ['MMS']

PI_affiliation: ['SWRI, LASP, KTH']

PI_name: ['J.Burch, R.Ergun, P.Lindqvist.']

Project: ['STP>Solar-Terrestrial Physics']

Source_name: ['MMS1>MMS Satellite Number 1']

TEXT: ['https://mms.gsfc.nasa.gov/', 'The full name of PI affiliations: SWRI - Southwest Research Institute. LASP - Laboratory for Atmospheric and Space Physics. KTH - Kungliga Tekniska Hogskolan (Swedish Royal Institute of Technology). ', 'For detailed timing information, such as needed for cross spectral analysis, please consult the EDP Data Products Guide.']

HTTP_LINK: ['https://mms.gsfc.nasa.gov/', 'http://mms.space.swri.edu/']

LINK_TEXT: ['Magnetospheric Multiscale (MMS) mission home page', 'SMART package home page']

LINK_TITLE: ['At NASA GSFC', 'At SWRI']

MODS: ['V.0. Initial release.', 'V.1. QL (v1.0.z), SCPOT (v1.0.z), L2A (v0.1.z) now uses ASPOC srvy l2 and DEFATT, if these are available. Brst QL uses intermediate L2A file from Fast mode for delta offsets. Bitmask changed to uint16 and Quality to uint8.', 'V.2. SCPOT (v2.0.z), L2A (v1.0.z) now uses variable names in accordance with new recommended standard for FIELDS, All products change shortening factor to 1.25 on SDP, offsets applied indicated by GlobalAttribute Calibration_file.', 'V.2. L2a (v2.0.z), QL (v1.6.z) now try to remove solar wind wake which previously left a clear sinusodial signal in the data.', 'V.3. L2a (v3.0.z) Slow Mode probe Gain set to 1.0 when orbital radius less than 5 RE (1.25 otherwise), L2pre (v2.0.z) DSL offsets removed from field is now included in the file as the Slow mode is dependent on scpot product (Fast/Brst is simply based on offset in Calibration_file).']

Generated_by: ['Y.Khotyaintsev and T.Nilsson, IRFU, using IRFU Matlab v1.15.1 while running at SDC.']

Skeleton_version: ['v0.0.7']

Parents: ['CDF>mms1_fields_hk_l1b_10e_20201029_v0.5.5', 'CDF>mms1_fields_hk_l1b_105_20201029_v0.2.5', 'CDF>mms1_edp_fast_l1b_dce_20201029_v1.4.3', 'CDF>MMS1_DEFATT_2020302_2020303.V00', 'CDF>MMS1_DEFATT_2020303_2020304.V00', 'CDF>mms1_aspoc_srvy_l2_20201028_v2.0.3', 'CDF>mms1_aspoc_srvy_l2_20201029_v2.0.3']
# CDF Summary [Method #2: Xarray]
print('CDF Summary\n{}'.format(cdf2.info))

# Individual Global Variables
print('\n\nIndividual Global Variables')
for variable in cdf2.attrs:
    print('{}: {}\n'.format(variable,cdf2.attrs[variable]))
CDF Summary
<bound method Dataset.info of <xarray.Dataset> Size: 65MB
Dimensions:                   (dim0: 6, mms1_edp_epoch_fast_l2: 1519680)
Coordinates:
  * mms1_edp_epoch_fast_l2    (mms1_edp_epoch_fast_l2) float64 12MB 1.604e+09...
Dimensions without coordinates: dim0
Data variables:
    mms1_edp_label1_fast_l2   (dim0) <U6 144B 'PSP_P1' 'PSP_P2' ... 'PSP_P6'
    mms1_edp_scpot_fast_l2    (mms1_edp_epoch_fast_l2) float32 6MB 5.244 ... ...
    mms1_edp_psp_fast_l2      (mms1_edp_epoch_fast_l2) float32 6MB -3.286 ......
    mms1_edp_dcv_fast_l2      (mms1_edp_epoch_fast_l2, dim0) float32 36MB -3....
    mms1_edp_bitmask_fast_l2  (mms1_edp_epoch_fast_l2) uint16 3MB 0 0 ... 64 64
    mms1_edp_quality_fast_l2  (mms1_edp_epoch_fast_l2) uint8 2MB 3 3 3 ... 2 2 2
    mms1_edp_deltap_fast_l2   int64 8B 15625000
Attributes: (12/24)
    Data_type:                   ['fast_l2_scpot']
    Data_version:                ['v2.7.3']
    Descriptor:                  ['EDP>Electric Double Probe']
    Discipline:                  ['Space Physics>Magnetospheric Science']
    Calibration_file:            ['mms1_edp_sdp_scpot_20160204_v0.0.0']
    Generation_date:             ['20201031']
    ...                          ...
    LINK_TEXT:                   ['Magnetospheric Multiscale (MMS) mission ho...
    LINK_TITLE:                  ['At NASA GSFC', 'At SWRI']
    MODS:                        ['V.0. Initial release.', 'V.1. QL (v1.0.z),...
    Generated_by:                ['Y.Khotyaintsev and T.Nilsson, IRFU, using ...
    Skeleton_version:            ['v0.0.7']
    Parents:                     ['CDF>mms1_fields_hk_l1b_10e_20201029_v0.5.5...>


Individual Global Variables
Data_type: ['fast_l2_scpot']

Data_version: ['v2.7.3']

Descriptor: ['EDP>Electric Double Probe']

Discipline: ['Space Physics>Magnetospheric Science']

Calibration_file: ['mms1_edp_sdp_scpot_20160204_v0.0.0']

Generation_date: ['20201031']

Instrument_type: ['Electric Fields (space)']

Logical_file_id: ['mms1_edp_fast_l2_scpot_20201029000000_v2.7.3']

Logical_source: ['mms1_edp_fast_l2_scpot']

Logical_source_description: ['MMS 1 dual probe scpot (fast), Spacecraft potential']

File_naming_convention: ['source_descriptor_datatype_yyyyMMddHHmmss']

Mission_group: ['MMS']

PI_affiliation: ['SWRI, LASP, KTH']

PI_name: ['J.Burch, R.Ergun, P.Lindqvist.']

Project: ['STP>Solar-Terrestrial Physics']

Source_name: ['MMS1>MMS Satellite Number 1']

TEXT: ['https://mms.gsfc.nasa.gov/', 'The full name of PI affiliations: SWRI - Southwest Research Institute. LASP - Laboratory for Atmospheric and Space Physics. KTH - Kungliga Tekniska Hogskolan (Swedish Royal Institute of Technology). ', 'For detailed timing information, such as needed for cross spectral analysis, please consult the EDP Data Products Guide.']

HTTP_LINK: ['https://mms.gsfc.nasa.gov/', 'http://mms.space.swri.edu/']

LINK_TEXT: ['Magnetospheric Multiscale (MMS) mission home page', 'SMART package home page']

LINK_TITLE: ['At NASA GSFC', 'At SWRI']

MODS: ['V.0. Initial release.', 'V.1. QL (v1.0.z), SCPOT (v1.0.z), L2A (v0.1.z) now uses ASPOC srvy l2 and DEFATT, if these are available. Brst QL uses intermediate L2A file from Fast mode for delta offsets. Bitmask changed to uint16 and Quality to uint8.', 'V.2. SCPOT (v2.0.z), L2A (v1.0.z) now uses variable names in accordance with new recommended standard for FIELDS, All products change shortening factor to 1.25 on SDP, offsets applied indicated by GlobalAttribute Calibration_file.', 'V.2. L2a (v2.0.z), QL (v1.6.z) now try to remove solar wind wake which previously left a clear sinusodial signal in the data.', 'V.3. L2a (v3.0.z) Slow Mode probe Gain set to 1.0 when orbital radius less than 5 RE (1.25 otherwise), L2pre (v2.0.z) DSL offsets removed from field is now included in the file as the Slow mode is dependent on scpot product (Fast/Brst is simply based on offset in Calibration_file).']

Generated_by: ['Y.Khotyaintsev and T.Nilsson, IRFU, using IRFU Matlab v1.15.1 while running at SDC.']

Skeleton_version: ['v0.0.7']

Parents: ['CDF>mms1_fields_hk_l1b_10e_20201029_v0.5.5', 'CDF>mms1_fields_hk_l1b_105_20201029_v0.2.5', 'CDF>mms1_edp_fast_l1b_dce_20201029_v1.4.3', 'CDF>MMS1_DEFATT_2020302_2020303.V00', 'CDF>MMS1_DEFATT_2020303_2020304.V00', 'CDF>mms1_aspoc_srvy_l2_20201028_v2.0.3', 'CDF>mms1_aspoc_srvy_l2_20201029_v2.0.3']

Data Variables

# Variable Summary [Method #1: CDFlib]
print('Variables as List\n{}'.format(cdf1.cdf_info().zVariables))

# Access Variable Data
variable = 'mms1_edp_dcv_fast_l2'
data = cdf1.varget(variable)
print('\n{}\n{}\n{}'.format(variable,data,data.shape))
Variables as List
['mms1_edp_epoch_fast_l2', 'mms1_edp_label1_fast_l2', 'mms1_edp_scpot_fast_l2', 'mms1_edp_psp_fast_l2', 'mms1_edp_dcv_fast_l2', 'mms1_edp_bitmask_fast_l2', 'mms1_edp_quality_fast_l2', 'mms1_edp_deltap_fast_l2']

mms1_edp_dcv_fast_l2
[[-3.2267735 -3.2671506 -3.1827219 -3.469033  -3.982991  -3.980059 ]
 [-3.2267735 -3.2671506 -3.1900637 -3.4745395 -3.986662  -3.9833632]
 [-3.2304444 -3.2708216 -3.1974058 -3.4800463 -3.990333  -3.9870343]
 ...
 [-2.4191623 -2.4760575 -2.37144   -2.635727  -3.2084186 -3.2003553]
 [-2.4228332 -2.4797285 -2.3751109 -2.6412332 -3.2120895 -3.2036598]
 [-2.4265041 -2.4852345 -2.3751109 -2.6430686 -3.2157605 -3.2073307]]
(1519680, 6)
# Variable Summary [Method #2: Xarray]
print('{}'.format(cdf2.data_vars))

# Access Variable Data
variable = 'mms1_edp_dcv_fast_l2'
data = cdf2[variable].data
print('\n{}\n{}\n{}'.format(variable,data,data.shape))
Data variables:
    mms1_edp_label1_fast_l2   (dim0) <U6 144B 'PSP_P1' 'PSP_P2' ... 'PSP_P6'
    mms1_edp_scpot_fast_l2    (mms1_edp_epoch_fast_l2) float32 6MB 5.244 ... ...
    mms1_edp_psp_fast_l2      (mms1_edp_epoch_fast_l2) float32 6MB -3.286 ......
    mms1_edp_dcv_fast_l2      (mms1_edp_epoch_fast_l2, dim0) float32 36MB -3....
    mms1_edp_bitmask_fast_l2  (mms1_edp_epoch_fast_l2) uint16 3MB 0 0 ... 64 64
    mms1_edp_quality_fast_l2  (mms1_edp_epoch_fast_l2) uint8 2MB 3 3 3 ... 2 2 2
    mms1_edp_deltap_fast_l2   int64 8B 15625000

mms1_edp_dcv_fast_l2
[[-3.2267735 -3.2671506 -3.1827219 -3.469033  -3.982991  -3.980059 ]
 [-3.2267735 -3.2671506 -3.1900637 -3.4745395 -3.986662  -3.9833632]
 [-3.2304444 -3.2708216 -3.1974058 -3.4800463 -3.990333  -3.9870343]
 ...
 [-2.4191623 -2.4760575 -2.37144   -2.635727  -3.2084186 -3.2003553]
 [-2.4228332 -2.4797285 -2.3751109 -2.6412332 -3.2120895 -3.2036598]
 [-2.4265041 -2.4852345 -2.3751109 -2.6430686 -3.2157605 -3.2073307]]
(1519680, 6)

Variable Metadata

# [Method #1: CDFlib]
for attribute in cdf1.varattsget(variable):
    print('\n{}: {}'.format(attribute,cdf1.varattsget(variable)[attribute]))
CATDESC: Individual probes. P1=V1, P2=(V1-0.120*E12), P3=V3, P4=(V3-0.120*E34), P5=V5, P6=(V5-0.0292*E56)

DEPEND_0: mms1_edp_epoch_fast_l2

DISPLAY_TYPE: time_series

FIELDNAM: Probe to spacecraft potential individual probe

FILLVAL: -9.999999848243207e+30

FORMAT: F8.3

LABL_PTR_1: mms1_edp_label1_fast_l2

SI_CONVERSION: 1.0>V

TENSOR_ORDER: 0

UNITS: V

VALIDMIN: -120.0

VALIDMAX: 50.0

VAR_TYPE: data
# [Method #2: Xarray]
for attribute in cdf2[variable].attrs:
    print('\n{}: {}'.format(attribute,cdf2[variable].attrs[attribute]))
CATDESC: Individual probes. P1=V1, P2=(V1-0.120*E12), P3=V3, P4=(V3-0.120*E34), P5=V5, P6=(V5-0.0292*E56)

DEPEND_0: mms1_edp_epoch_fast_l2

DISPLAY_TYPE: time_series

FIELDNAM: Probe to spacecraft potential individual probe

FILLVAL: -9.999999848243207e+30

FORMAT: F8.3

LABL_PTR_1: mms1_edp_label1_fast_l2

SI_CONVERSION: 1.0>V

TENSOR_ORDER: 0

UNITS: V

VALIDMIN: -120.0

VALIDMAX: 50.0

VAR_TYPE: data

standard_name: Probe to spacecraft potential individual probe

units: V

Writing CDFs

Create Variables

# Epoch Variable
times = [dt.datetime(2020,3,10,12,tmp).strftime('%d-%b-%Y %H:%M:%S.000') for tmp in range(60)]
print('Epochs\n{}'.format(times))
# Convert DateTime Strings into CDF_EPOCH DataType
epochs = cdflib.epochs.CDFepoch.parse(times)
epochs = epochs.astype('int')

# Data Variable
data = np.random.rand(len(times),4)
print('\nData\n{}'.format(data))
Epochs
['10-Mar-2020 12:00:00.000', '10-Mar-2020 12:01:00.000', '10-Mar-2020 12:02:00.000', '10-Mar-2020 12:03:00.000', '10-Mar-2020 12:04:00.000', '10-Mar-2020 12:05:00.000', '10-Mar-2020 12:06:00.000', '10-Mar-2020 12:07:00.000', '10-Mar-2020 12:08:00.000', '10-Mar-2020 12:09:00.000', '10-Mar-2020 12:10:00.000', '10-Mar-2020 12:11:00.000', '10-Mar-2020 12:12:00.000', '10-Mar-2020 12:13:00.000', '10-Mar-2020 12:14:00.000', '10-Mar-2020 12:15:00.000', '10-Mar-2020 12:16:00.000', '10-Mar-2020 12:17:00.000', '10-Mar-2020 12:18:00.000', '10-Mar-2020 12:19:00.000', '10-Mar-2020 12:20:00.000', '10-Mar-2020 12:21:00.000', '10-Mar-2020 12:22:00.000', '10-Mar-2020 12:23:00.000', '10-Mar-2020 12:24:00.000', '10-Mar-2020 12:25:00.000', '10-Mar-2020 12:26:00.000', '10-Mar-2020 12:27:00.000', '10-Mar-2020 12:28:00.000', '10-Mar-2020 12:29:00.000', '10-Mar-2020 12:30:00.000', '10-Mar-2020 12:31:00.000', '10-Mar-2020 12:32:00.000', '10-Mar-2020 12:33:00.000', '10-Mar-2020 12:34:00.000', '10-Mar-2020 12:35:00.000', '10-Mar-2020 12:36:00.000', '10-Mar-2020 12:37:00.000', '10-Mar-2020 12:38:00.000', '10-Mar-2020 12:39:00.000', '10-Mar-2020 12:40:00.000', '10-Mar-2020 12:41:00.000', '10-Mar-2020 12:42:00.000', '10-Mar-2020 12:43:00.000', '10-Mar-2020 12:44:00.000', '10-Mar-2020 12:45:00.000', '10-Mar-2020 12:46:00.000', '10-Mar-2020 12:47:00.000', '10-Mar-2020 12:48:00.000', '10-Mar-2020 12:49:00.000', '10-Mar-2020 12:50:00.000', '10-Mar-2020 12:51:00.000', '10-Mar-2020 12:52:00.000', '10-Mar-2020 12:53:00.000', '10-Mar-2020 12:54:00.000', '10-Mar-2020 12:55:00.000', '10-Mar-2020 12:56:00.000', '10-Mar-2020 12:57:00.000', '10-Mar-2020 12:58:00.000', '10-Mar-2020 12:59:00.000']

Data
[[0.05094469 0.90135566 0.98150111 0.68025706]
 [0.85541172 0.47031289 0.70291093 0.2422167 ]
 [0.85949017 0.37431706 0.18921194 0.16484722]
 [0.19119964 0.11308365 0.19013194 0.45897901]
 [0.84225526 0.86402405 0.41880536 0.48776994]
 [0.72268283 0.0961738  0.18890418 0.33726762]
 [0.87182354 0.27345345 0.6463367  0.9058462 ]
 [0.42885118 0.59900621 0.58041693 0.5967743 ]
 [0.87133068 0.47778337 0.10297038 0.72760488]
 [0.38096434 0.49704659 0.62558639 0.96697067]
 [0.80936269 0.56017934 0.94889815 0.91528132]
 [0.89111854 0.35931152 0.39088376 0.08414771]
 [0.24320455 0.80550937 0.47118721 0.59332489]
 [0.07904999 0.12545175 0.72614842 0.42912162]
 [0.7089719  0.39619156 0.79559698 0.26283251]
 [0.76089647 0.61117754 0.22679476 0.84028129]
 [0.65744965 0.34789475 0.06914054 0.28252578]
 [0.06812065 0.98171195 0.62673619 0.04830301]
 [0.21634601 0.60807326 0.85530628 0.31737205]
 [0.00601339 0.68741419 0.93676143 0.18795908]
 [0.6546492  0.85336428 0.30821589 0.59122374]
 [0.23963823 0.80555993 0.32768337 0.9168717 ]
 [0.54343136 0.00670369 0.50342336 0.91851374]
 [0.43399758 0.98715986 0.53890725 0.10912413]
 [0.75938629 0.47778052 0.23355366 0.29961331]
 [0.68536801 0.82337115 0.9636923  0.73095582]
 [0.24129876 0.06807325 0.83558557 0.15790888]
 [0.88390019 0.81055892 0.94424646 0.44734545]
 [0.48682278 0.92498096 0.88911676 0.37211006]
 [0.01464895 0.77255815 0.20630478 0.92690338]
 [0.84769012 0.63264388 0.7967672  0.11073734]
 [0.53591083 0.80733973 0.95605236 0.16464185]
 [0.94084999 0.52056952 0.3773708  0.76294649]
 [0.12785892 0.30429838 0.86305179 0.77152205]
 [0.05003086 0.0028701  0.67649955 0.60400032]
 [0.61567646 0.40264572 0.95491177 0.48539818]
 [0.90844871 0.46288492 0.73383664 0.94309328]
 [0.23521088 0.67045569 0.05207413 0.83444829]
 [0.12894192 0.88418026 0.46470606 0.36888506]
 [0.01561731 0.69149742 0.30488399 0.61976685]
 [0.31757034 0.48329377 0.41874552 0.51980053]
 [0.11263969 0.84782124 0.86427222 0.41214163]
 [0.67557516 0.28179531 0.39262249 0.38629624]
 [0.17629565 0.69609972 0.14940791 0.83043014]
 [0.34583408 0.0869765  0.11928823 0.13127439]
 [0.9823115  0.54969102 0.18510442 0.53827905]
 [0.08389111 0.29141479 0.57308027 0.69502777]
 [0.92785808 0.6782276  0.72245775 0.13548338]
 [0.61514865 0.9550492  0.61300124 0.0178664 ]
 [0.04001266 0.53540744 0.54136621 0.8104434 ]
 [0.86313858 0.03232725 0.3138245  0.61058029]
 [0.4467725  0.11162262 0.66277574 0.85780594]
 [0.0482555  0.20918505 0.29582431 0.75127596]
 [0.91457971 0.03654048 0.85994031 0.25240632]
 [0.43789393 0.10018169 0.84917638 0.33548783]
 [0.67482904 0.01599105 0.20649479 0.9821289 ]
 [0.70492508 0.30199185 0.68348307 0.51127617]
 [0.84735593 0.9396623  0.13657643 0.97097583]
 [0.03366173 0.2844484  0.99102465 0.58367016]
 [0.1813236  0.14565854 0.65321137 0.67067502]]

Create Local CDF

cdf = cdflib.cdfwrite.CDF('cdflib_example.cdf')

Add Global Variables

# Create Global Metadata
globalAttrs = {}
globalAttrs['Author'] = {0:'John Doe'}
globalAttrs['CreateDate'] = {0:dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}

# Write to File
cdf.write_globalattrs(globalAttrs)

Add Data Variables

# Epoch Variable
# Specifications
vs1_spec = {}
vs1_spec['Variable'] = 'Epoch'
# data_type: CDF_EPOCH
vs1_spec['Data_Type'] = 14
vs1_spec['Num_Elements'] = 1
vs1_spec['Rec_Vary'] = True
vs1_spec['Dim_Sizes'] = [60]
vs1_spec['Dim_Vary'] = True
vs1_spec['Compress'] = 6
# Attributes
vs1_attr = {}
# Data
vs1_data = epochs
# Wrtie to CDF
cdf.write_var(vs1_spec, var_attrs=vs1_attr, var_data=vs1_data)


# Data Variable
# Specifications
vs2_spec = {}
vs2_spec['Variable'] = 'Data'
# data_type: CDF_FLOAT
vs2_spec['Data_Type'] = 21
vs2_spec['Num_Elements'] = 1
vs2_spec['Rec_Vary'] = True
vs2_spec['Dim_Sizes'] = [60,4]
vs2_spec['Dim_Vary'] = True
vs2_spec['Compress'] = 6
# Attributes
vs2_attr = {}
vs2_attr['UNITS'] = 'percent'
# Data
vs2_data = data
# Wrtie to CDF
cdf.write_var(vs2_spec, var_attrs=vs2_attr, var_data=vs2_data)

Close File

cdf.close()
print('Check for cdflib_example.cdf file!')
Check for cdflib_example.cdf file!