I am running a .py file that obtains cdf files and plots them. Following is the code from the start of the file to where it gives an error--
import cdflib
from astropy.time import Time, TimeDelta
import datetime
import numpy as np
import matplotlib.colors as colors
import matplotlib.ticker as ticker
from tqdm import tqdm
import os
from glob import glob
import pandas
from mpl_toolkits import axes_grid1
import junoEphemeris
def PathsFromTimeDifference(t1, t2, pathFormat):
""" Creates output path names between two times in a specified format
Arguments:
t1, t2 -- (str) Input times between which the paths will be created. Must be in the format "YYYY-MM-DDThh:mm:ss"
pathFormat -- (str) Path format which the files to be downloaded are in
Returns:
A list of the paths following the pathFormat. One path for each day between t1 and t2
"""
date1, time1 = t1.split("T")
date2, time2 = t2.split("T")
year1, month1, day1 = date1.split("-")
year2, month2, day2 = date2.split("-")
hours1, minutes1, seconds1 = time1.split(":")
startDate = datetime.date(int(year1), int(month1), int(day1))
# NOTE: Waves files start from 00:01:09 on each day and end on 00:01:08 the next day meaning to plot from 00:00:00 we need the day before's data.
if hours1 == "00" and int(minutes1) <= 1 and int(seconds1) < 10:
startDate = startDate - datetime.timedelta(days=1)
endDate = datetime.date(int(year2), int(month2), int(day2))
pathExtensions = []
if startDate == endDate:
pathExtensions.append(startDate.strftime(pathFormat))
else:
for date in DateRange(startDate, endDate):
pathExtension = date.strftime(pathFormat)
pathExtensions.append(pathExtension)
return pathExtensions
def DownloadWavesData(dataPath, downloadPath, timeFrame):
""" Downloads the waves data using system command wget
Arguments:
dataPath -- (str) Path to directory where data will be saved
downloadPath -- (str) Download link passed to wget
timeFrame -- (list) The time frame with which to download data
"""
pathList = [f"{downloadPath}{extension}" for extension in PathsFromTimeDifference(timeFrame[0], timeFrame[1], "%Y/%m/jno_wav_cdr_lesia_%Y%m%d_v02.cdf")]
print(f"Downloading Waves files from {downloadPath} to {dataPath}\n")
for path in tqdm(pathList):
os.system(f"wget -r --show-progress -nd -np -nH -N -P {dataPath} {path}")
def LoadCdfFiles(dataDirectory, measurements, timeFrame, downloadPath):
# Inputs are a directory containing the files to be loaded and a list of the measurements to be pulled from the files.
# NEED TO CHECK TO ONLY LOAD FILES WITHIN THE TIME FRAME, REUSE PATHSFROMTIMEDIFFERENCE?
""" Loads the downloaded cdf files from the data directory
Arguments:
dataDirectory -- (str) Path do directory where data is stored
measurements -- (list) A list containing strings of the data measurments to pull from the cdf files
Returns:
A list of dictionaries of each file which contains the measurements as keys.
"""
print(f"Loading CDF files from {dataDirectory}")
# Check if all filepaths between data are in the folder
filePathsNeeded = PathsFromTimeDifference(timeFrame[0], timeFrame[1], f"{dataDirectory}jno_wav_cdr_lesia_%Y%m%d_v02.cdf")
filePathsNeeded.sort()
filePaths = glob(f"{dataDirectory}*.cdf") # returns a list of downloaded file paths (unsorted)
filePaths.sort() # Because the date in in the file is in format yyyymmdd it can be sorted numerically.
filesToBeDownloaded = [file for file in filePathsNeeded if file not in filePaths]
fileLinks = PathsFromTimeDifference(timeFrame[0], timeFrame[1], "%Y/%m/jno_wav_cdr_lesia_%Y%m%d_v02.cdf")
if len(filesToBeDownloaded) > 0:
print("Downloading missing data...")
for path in tqdm(filesToBeDownloaded):
linkIndex = [i for i, link in enumerate(fileLinks) if path.replace(dataDirectory, '') in link][0]
os.system(f"wget -r -q -nd -nv -np -nH -N -P {dataDirectory} {downloadPath}{fileLinks[linkIndex]}")
filePaths = filePathsNeeded
filesInfoList = []
print("Loading data...")
for filePath in tqdm(filePaths):
file = cdflib.CDF(filePath)
fileInfo = dict()
for measurment in measurements:
measurmentData = file.varget(measurment)
# measurementUnit = file.varinq(measurment)["Data_Type_Description"]
fileInfo[measurment] = measurmentData
filesInfoList.append(fileInfo)
return filesInfoList
When I run this file, I get an error AttributeError: 'CDF' object has no attribute 'varget' in the last bit of code in the line -- measurmentData = file.varget(measurment).
I have checked the cdflib docs available here and the attribute is available, so I am not sure what the issue is. What can I do to fix this?
For reference I have obtained the .py file from here on github