cdf_to_xarray
- cdflib.xarray.cdf_to_xarray(filename, to_datetime=True, to_unixtime=False, fillval_to_nan=False)[source][source]
This function converts CDF files into XArray Dataset Objects.
- Parameters:
filename (str) – The path to the CDF file to read
to_datetime (bool, optional) – Whether or not to convert CDF_EPOCH/EPOCH_16/TT2000 to datetime64, or leave them as is
to_unixtime (bool, optional) – Whether or not to convert CDF_EPOCH/EPOCH_16/TT2000 to unixtime, or leave them as is
fillval_to_nan (bool, optional) – If True, any data values that match the FILLVAL attribute for a variable will be set to NaN
- Returns:
An XArray Dataset Object
- Return type:
- Example MMS:
>>> # Import necessary libraries >>> import cdflib.xarray >>> import xarray as xr >>> import os >>> import urllib.request
>>> # Download a CDF file >>> fname = 'mms2_fgm_srvy_l2_20160809_v4.47.0.cdf' >>> url = ("https://lasp.colorado.edu/maven/sdc/public/data/sdc/web/cdflib_testing/mms2_fgm_srvy_l2_20160809_v4.47.0.cdf") >>> if not os.path.exists(fname): >>> urllib.request.urlretrieve(url, fname)
>>> # Load in and display the CDF file >>> mms_data = cdflib.xarray.cdf_to_xarray("mms2_fgm_srvy_l2_20160809_v4.47.0.cdf", to_unixtime=True, fillval_to_nan=True)
>>> # Show off XArray functionality >>> >>> # Slice the data using built in XArray functions >>> mms_data2 = mms_data.isel(dim0=0) >>> # Plot the sliced data using built in XArray functions >>> mms_data2['mms2_fgm_b_gse_srvy_l2'].plot() >>> # Zoom in on the slices data in time using built in XArray functions >>> mms_data3 = mms_data2.isel(Epoch=slice(716000,717000)) >>> # Plot the zoomed in sliced data using built in XArray functionality >>> mms_data3['mms2_fgm_b_gse_srvy_l2'].plot()
- Example THEMIS:
>>> # Import necessary libraries >>> import cdflib.xarray >>> import xarray as xr >>> import os >>> import urllib.request
>>> # Download a CDF file >>> fname = 'thg_l2_mag_amd_20070323_v01.cdf' >>> url = ("https://lasp.colorado.edu/maven/sdc/public/data/sdc/web/cdflib_testing/thg_l2_mag_amd_20070323_v01.cdf") >>> if not os.path.exists(fname): >>> urllib.request.urlretrieve(url, fname)
>>> # Load in and display the CDF file >>> thg_data = cdflib.xarray.cdf_to_xarray(fname, to_unixtime=True, fillval_to_nan=True)
- Processing Steps:
- For each variable in the CDF file
- Determine the name of the dimension that spans the data “records”
Check if the variable itself might be a dimension
The DEPEND_0 likely points to the approrpiate dimensions
If neither of the above, we create a new dimensions named “recordX”
- Determine the name of the other dimensions of the variable, if they exist
Check if the variable name itself might be a dimension
The DEPEND_X probably points to the appropriate dimensions for that variable, so we check those
If either of the above are time varying, the code appends “_dim” to the end of the name
If no dimensions are found through the above checks, create a dumension named “dimX”
Gather all attributes that belong to the variable
Add a few attributes that enable better plotting with built-in xarray functions (name, units, etc)
Optionally, convert FILLVALs to NaNs in the data
Optionally, convert CDF_EPOCH/EPOCH16/TT2000 variables to unixtime or datetime
Create an XArray Variable object using the dimensions determined in steps 1 and 2, the attributes from steps 3 and 4, and then the variable data
Gather all the Variable objects created in the first step, and separate them into data variables or coordinate variables
Gather all global scope attributes in the CDF file
Create an XArray Dataset objects with the data variables, coordinate variables, and global attributes.