This page was generated by nbsphinx from docs/examples/Loading_CDF.ipynb.
Interactive online version: Binder badge Google Colab badge

Loading CDF files

[1]:
import pycdfpp
import requests
import numpy as np
import matplotlib.pyplot as plt
import xarray as xr
%matplotlib widget

Let’s load some ACE data directly from CDA database:

[2]:
cdf = pycdfpp.load(requests.get('https://cdaweb.gsfc.nasa.gov/pub/data/ace/mag/level_2_cdaweb/mfi_h0/2019/ac_h0_mfi_20190116_v07.cdf').content)

You can have an overview of the CDF file by just using it’s repr:

[3]:
cdf
[3]:
CDF:
  version: 2.5.22
  majority: Adaptative column
  compression: None

Attributes:
  TITLE: "ACE> Magnetometer Parameters"
  Project: [ [ "ACE>Advanced Composition Explorer", "ISTP>International Solar-Terrestrial Physics" ] ]
  Discipline: "Space Physics>Interplanetary Studies"
  Source_name: "AC>Advanced Composition Explorer"
  Data_type: "H0>16-Sec Level 2 Data"
  Descriptor: "MAG>ACE Magnetic Field Instrument"
  Data_version: "7"
  Generated_by: "ACE Science Center"
  Generation_date: "20190429"
  LINK_TEXT: "Release notes and other info available at"
  LINK_TITLE: "the ACE Science Center Level 2 Data website"
  HTTP_LINK: "http://www.srl.caltech.edu/ACE/ASC/level2/index.html"
  TEXT: [ [ "MAG - ACE Magnetic Field Experiment", "References: http://www.srl.caltech.edu/ACE/", "The quality of ACE level 2 data is such that it is suitable for serious ", "scientific study.  However, to avoid confusion and misunderstanding, it ", "is recommended that users consult with the appropriate ACE team members", "before publishing work derived from the data. The ACE team has worked ", "hard to ensure that the level 2 data are free from errors, but the team ", "cannot accept responsibility for erroneous data, or for misunderstandings ", "about how the data may be used. This is especially true if the appropriate ", "ACE team members are not consulted before publication. At the very ", "least, preprints should be forwarded to the ACE team before publication." ] ]
  MODS: [ [ "Initial Release 9/7/01 ", "12/04/02: Fixed description of Epoch time variable." ] ]
  ADID_ref: "NSSD0327"
  Logical_file_id: "AC_H0_MAG_20190116_V07"
  Logical_source: "AC_H0_MFI"
  Logical_source_description: "H0 - ACE Magnetic Field 16-Second Level 2 Data"
  PI_name: "N. Ness"
  PI_affiliation: "Bartol Research Institute"
  Mission_group: "ACE"
  Instrument_type: "Magnetic Fields (space)"
  Time_resolution: "16 second"
  Web_site: "http://www.srl.caltech.edu/ACE/"
  Acknowledgement: [ [ "Please acknowledge the Principal ", "Investigator, N. Ness of Bartol Research ", "Institute" ] ]
  Rules_of_use: [ [ "See the rules of use available from the ACE ", "Science Center at: ", "http://www.srl.caltech.edu/ACE/ASC/level2/policy_lvl2.html" ] ]

Variables:
  Epoch: [ 5400, 1 ], [CDF_EPOCH], record varry:True, compression: None
  Time_PB5: [ 0, 3 ], [CDF_INT1], record varry:True, compression: None
  Magnitude: [ 5400, 1 ], [CDF_REAL4], record varry:True, compression: None
  BGSEc: [ 5400, 3 ], [CDF_REAL4], record varry:True, compression: None
  label_BGSE: [ 3, 6 ], [CDF_CHAR], record varry:Flase, compression: None
  BGSM: [ 5400, 3 ], [CDF_REAL4], record varry:True, compression: None
  label_bgsm: [ 3, 8 ], [CDF_CHAR], record varry:Flase, compression: None
  dBrms: [ 5400, 1 ], [CDF_REAL4], record varry:True, compression: None
  Q_FLAG: [ 5400, 1 ], [CDF_INT1], record varry:True, compression: None
  SC_pos_GSE: [ 5400, 3 ], [CDF_REAL4], record varry:True, compression: None
  label_pos_GSE: [ 3, 10 ], [CDF_CHAR], record varry:Flase, compression: None
  SC_pos_GSM: [ 5400, 3 ], [CDF_REAL4], record varry:True, compression: None
  label_pos_GSM: [ 3, 10 ], [CDF_CHAR], record varry:Flase, compression: None
  unit_time: [ 3, 4 ], [CDF_CHAR], record varry:Flase, compression: None
  label_time: [ 3, 27 ], [CDF_CHAR], record varry:Flase, compression: None
  format_time: [ 3, 2 ], [CDF_CHAR], record varry:Flase, compression: None
  cartesian: [ 3, 11 ], [CDF_CHAR], record varry:Flase, compression: None

CDF attributes contains a list of entries where each entry is either a list of scalar values of the same type or a string. Note that CDF variables attributes can only have one entry.

[4]:
print(cdf.attributes["TITLE"][0])
print(cdf.attributes["TEXT"][1])
print(cdf["Epoch"].attributes["FILLVAL"][0])
ACE> Magnetometer Parameters
References: http://www.srl.caltech.edu/ACE/
[-1e+31]

Accessing variables values

Except for string values:

  • Variables values can either be accessed through .values member or using Python buffer protocol.

  • With both .values member and Python buffer protocol 0 data copy is performed to access values.

[5]:
plt.figure()
plt.plot(pycdfpp.to_datetime64(cdf["Epoch"]), cdf["BGSM"])
plt.show()
[6]:
cdf["BGSM"].values
[6]:
array([[ 4.772, -2.697,  1.75 ],
       [ 4.851, -2.635,  1.653],
       [ 4.769, -2.64 ,  1.722],
       ...,
       [ 6.525, -0.342, -1.095],
       [ 6.419, -1.46 , -0.793],
       [ 6.737, -0.599, -1.087]], dtype=float32)
[7]:
xr.DataArray(cdf["BGSM"])
[7]:
<xarray.DataArray 'BGSM' (dim_0: 5400, dim_1: 3)>
array([[ 4.772, -2.697,  1.75 ],
       [ 4.851, -2.635,  1.653],
       [ 4.769, -2.64 ,  1.722],
       ...,
       [ 6.525, -0.342, -1.095],
       [ 6.419, -1.46 , -0.793],
       [ 6.737, -0.599, -1.087]], dtype=float32)
Dimensions without coordinates: dim_0, dim_1
[ ]: