def fromCDF(cls, filepath, **kwargs): ''' Create a DataModel from a CDF data file. Not available without SpacePy. ''' if USE_SPACEPY: return cls.fromSpaceData(spdm.fromCDF(filepath, **kwargs)) else: raise NotImplementedError
def data_in(f_name, LOCATION): try: data = dm.fromCDF(f_name).copy() except: try: data = pycdf.CDF(f_name) except: print('Issue with:', f_name) return pd.DataFrame(), False time = data['thg_mag_' + LOCATION + '_time'].copy() D = data['thg_mag_' + LOCATION].copy() df = pd.DataFrame(D, index=time) df.index = pd.to_datetime(df.index, unit='s') df = df.resample('1min').mean() return df, True
def release(filename): connection = pymongo.MongoClient() tdb = connection.CDF_Release post_attrs = tdb.attrs post_datas = tdb.data cdf_file = dm.fromCDF(filename) cdf = readCDF(filename, cdf_file) attrs_value = cdf[0] data_value = cdf[1] try: if post_attrs.find_one({'_id': filename}) == None: post_attrs.insert(attrs_value) post_datas.insert(data_value) else: print 'the file attrs has been imported' except Exception as e: print '---------------wrong cdf---------------'
def get_cdf(*args, **kwargs): """ Get a CDF file and read it (all arguments are passed to cdaweb.get_file) Example: from datetime import datetime get_cdf('sp_phys','OMNI2_H0_MRG1HR',datetime(2005,1,1),datetime(2005,2,1),['KP1800']) """ resp = get_file(*args, **kwargs) from tempfile import NamedTemporaryFile from shutil import copyfileobj import spacepy.datamodel as dm with NamedTemporaryFile() as tmpfile: copyfileobj(resp, tmpfile) tmpfile.seek(0) data = dm.fromCDF(tmpfile.name) return data
def test_toCDFroundtrip_method(self): """toCDF should be able to make a file and then read it in the same""" self.SDobj.toCDF(self.testfile) tst = dm.fromCDF(self.testfile) for k in self.SDobj: np.testing.assert_array_equal(self.SDobj[k], tst[k])
#-*-coding:utf8-*- """ releaseCDF releaseCDF input/output filters Written by Zahi Yang May 2017 - contains read function """ import pymongo import time, datetime import spacepy.datamodel as dm from spacepy import pycdf filename = 'ac_at_def_19970826_v01.cdf' cdf_file = dm.fromCDF(filename) def readCDF(filename, cdf_file): diction_attrs = {} diction_data = {} diction_attrs['_id'] = filename for key in cdf_file.attrs.keys(): value = cdf_file.attrs[key] if not value == ' ': diction_attrs[key] = value else: diction_attrs[key] = None data_recorder = [] keys = cdf_file.keys() for key in keys:
def test_toCDF_method(self): """Convert to CDF, using the method, catching #404""" a = dm.SpaceData({'dat': dm.dmarray([1, 2, 3])}) a.toCDF(self.testfile, mode='a') newobj = dm.fromCDF(self.testfile) np.testing.assert_array_equal([1, 2, 3], newobj['dat'])
def integrate_HOPE_moments(time_start, time_end, probe, pad, rbsp_path='E://DATA//RBSP//'): ''' Testing self-integration of ion moments from pitch angle data :: Compare to actual calculated L3-MOM products. Based on equation from RBSPICE Data Handbook Rev. E (2018) p. 31. Questions: - Do we have to account for gaps between energy channels? As in, fit a smooth curve to the fluxes and integrate that way? - Should be able to ignore channels below 30eV for the comparison, and then include them once the fluxes are corrected for a few things? To get an idea of the plasmaspheric composition ''' qp = 1.602e-19 mp = 1.673e-27 import spacepy.datamodel as dm pa_data = dm.fromCDF(_test_file_pa) mom_data = dm.fromCDF(_test_file_mom) mom_times = mom_data['Epoch_Ion'] mom_hdens = mom_data['Dens_p_30'] pa_times = pa_data['Epoch_Ion'] n_times = pa_times.shape[0] vdens = np.zeros(n_times, dtype=np.float64) # Organize pitch angle stuff pitch = pa_data['PITCH_ANGLE'] # PA bin centers n_pitch= pitch.shape[0] da = np.ones(n_pitch) * 18. # PA bin widths da[0] = da[-1] = 9. da *= np.pi / 180. # Do energy stuff energy = pa_data['HOPE_ENERGY_Ion'] # Energy bin centers n_energy = energy.shape[1] # Energy bin limits erange = np.zeros((n_times, n_energy, 2)) for ii in range(n_energy): erange[:, ii, 0] = (pa_data['HOPE_ENERGY_Ion'][:, ii] - pa_data['ENERGY_Ion_DELTA'][:, ii]) erange[:, ii, 1] = (pa_data['HOPE_ENERGY_Ion'][:, ii] + pa_data['ENERGY_Ion_DELTA'][:, ii]) E_min = 30.0; E_max = None for ii in range(n_times): E = energy[ii] dE = erange[ii] # Find energy min/max channels for integration if E_min is None: min_idx = 0 else: diffs = np.abs(energy - E_min) min_idx = np.where(diffs == diffs.min())[0][0] if E_max is None: max_idx = energy.shape[0] - 1 else: diffs = np.abs(energy - E_max) min_idx = np.where(diffs == diffs.min())[0][0] #vdens[ii] = vdist[ii, min_idx:max_idx].sum() # Need: # Pitch angle bin list # Pitch angle widths # Energy bin list # Energy bin widths # Filter bad values (fill value -1E-31, and no density should be negative) for ii in range(mom_hdens.shape[0]): if mom_hdens[ii] < 0.0: mom_hdens[ii] = np.nan for ii in range(vdens.shape[0]): if vdens[ii] < 0.0: vdens[ii] = np.nan plt.ioff() plt.figure() plt.plot(mom_times, mom_hdens, label='MOM-L3') plt.plot(pa_times , vdens , label='Manual') plt.legend() plt.show() return