def __init__(self, filename, mode): if mode == 'w': self.FILE = pycdf.CDF( filename, pycdf.NC.WRITE | pycdf.NC.CREATE | pycdf.NC.TRUNC) if mode == 'r': self.FILE = pycdf.CDF(filename, pycdf.NC.NOWRITE) if mode == 'a': self.FILE = pycdf.CDF(filename, pycdf.NC.WRITE) self.FILE.automode()
def __init__(self, filename, perm='r', interface=Interface, version=3, ncdump=False): if interface != 'netcdf4': version = 3 if perm == 't': # trunc if os.path.isfile(filename): os.remove(filename) perm = 'w' if interface == 'pycdf': if perm == 'w': perm = pnc.NC.WRITE | pnc.NC.CREATE elif perm == 'a': perm = pnc.NC.WRITE | pnc.NC.CREATE else: perm = pnc.NC.NOWRITE nc = pnc.CDF(filename, perm) nc.automode() elif interface == 'scientific': nc = pnc.NetCDFFile(filename, perm) elif interface == 'netcdf4': if perm == 'w' and os.path.isfile(filename): perm = 'a' if not isinstance(version, basestring): format = 'NETCDF' + str(version) if format == 'NETCDF3': format = 'NETCDF3_CLASSIC' else: format = version if not isinstance(filename, basestring) or filename.find( '*') != -1 or filename.find('?') != -1: nc = pnc.MFDataset(filename) else: if os.path.isfile(filename): nc = pnc.Dataset(filename, perm) else: nc = pnc.Dataset(filename, perm, format=format) self.perm = perm # root info: info = {} info['interface'] = interface info['filename'] = filename info['version'] = version if ncdump: from ncdump import ncdump_info info['ncdump_info'] = ncdump_info(filename) else: info['ncdump_info'] = False # init group: Pyncgroup.__init__(self, nc, info=info)
def add_landmask(self): g = pycdf.CDF(self.datadir + '/scb_grid.nc') self.landmask = g.var('mask_rho')[:]
def main(argv): sUsage = "%%prog [options] DATA_DIRECTORY BEGIN END" sDesc = """ Reads Themis spectral density auto-correlation values from archive CDFs. Format is similar to the Cluster Active Archive, see document: CAA-EST-UG-002 for details. """ psr = optparse.OptionParser( usage=sUsage, description=sDesc, prog=bname(argv[0]) ) psr.add_option('-l', "--log-level", dest="sLevel", metavar="LOG_LEVEL", help="Logging level one of [critical, error, warning, "+\ "info, debug]. The default is info.", type="string", action="store", default="info") (opts, lArgs) = psr.parse_args(argv[1:]) log = setupLogger(opts.sLevel) log = logging.getLogger('') if len(lArgs) < 1: return serverErr(log, "Misconfigured DSDF, data directory is missing") sRoot = lArgs[0] if len(lArgs) < 3: return queryErr(log, "Start and or Stop time is missing") try: dtBeg = das2.DasTime(lArgs[1]) except: return queryErr(log, "Couldn't parse time value '%s'"%lArgs[1]) try: dtEnd = das2.DasTime(lArgs[2]) except: return queryErr(log, "Couldn't parse time value '%s'"%lArgs[2]) # Take all the rest of the arguments and glop them together in a single # string. That way running the reader from the command line feels the # same as running it from Autoplot sParams = '' if len(lArgs) > 3: sParams = ' '.join(lArgs[3:]) # pull out the polar style output, i.e: Magnitude and Phase Angle bPolar = True if sParams.find('complex') != -1: sParams = sParams.replace('complex','').strip() bPolar = False # Default to printing all the autocorrelations sComp = 'BxBx ByBy BzBz ExEx EyEy EzEz' if len(sParams) > 0: sComp = sParams lComp = sComp.split() lComp.sort() # Look in directory tree for files that match. We sort the file names # under the assumption that sort order = numerical time order, but that # may not be true for some file types lDir = os.listdir(sRoot) lDir.sort() nSent = 0 bSentHdr = False for sF in lDir: if not sF.endswith('.cdf'): continue # only want CDFs if not sF.startswith('tha_l3_sm'): continue # Only want L3 SM # Make ISO-8601 strings sBeg = "%s-%s-%sT%s:%s:%s"%( sF[10:14], sF[14:16], sF[16:18], sF[19:21], sF[21:23], sF[23:25] ) sEnd = "%s-%s-%sT%s:%s:%s"%( sF[26:30], sF[30:32], sF[32:34], sF[35:37], sF[37:39], sF[39:41] ) sPath = pjoin(sRoot, sF) try: dtFileBeg = das2.DasTime(sBeg) dtFileEnd = das2.DasTime(sEnd) # Since the themis files truncate the seconds field, round up by # one second for the file end time... dtFileEnd += 1.0 except ValueError as e: log.waring("Unknown file %s in data area"%sPath) continue # If overlaps with desired range, include it in the output, send header # if haven't done so if (dtFileBeg < dtEnd) and (dtFileEnd > dtBeg): log.info("Reading %s"%sPath) cdf = pycdf.CDF(sPath) # Assmue all the files are similar enough that an informational # header can be created from the first one that fits the range if not bSentHdr: lIgnore = ['TIME_MAX','TIME_MIN', 'TIME_resolution'] dExtra = { 'title':getVespaTitle(cdf, 'THEMIS', lComp), 'Datum:xTagWidth':'0.5 s' # Max interp width for Autoplot } cdfToDas22Hdr(cdf, lIgnore, dExtra) bSentHdr = True nSent += sendDataPackets(cdf, dtBeg, dtEnd, lComp, bPolar) if nSent == 0: if not bSentHdr: writeHdrPkt(0, '<stream version="2.2" />\n') sFmt = '<exception type="NoDataInInterval" message="%s" />\n' sOut = sFmt%("No data in interval %s to %s UTC"%(str(dtBeg), str(dtEnd))) writeHdrPkt('xx', sOut) return 0
def display_cdf(filename, desc): cdf = pycdf.CDF(filename) print("R%04d %s" %(0, cdf[desc][0])) print("R%04d %s" %(len(cdf[desc]), cdf[desc][-1]))