def fix_date(date): from calc import isiterable from air_sea import julianmd, greg2 ''' Support for "wrong" dates like (2011,13,-1) Input must be an iterable, like list or tuple, not string Example: fix_date((2011,13,-1)) returns datetime.datetime(2011, 12, 30, 0, 0) ''' if isiterable(date) and not isinstance(date, basestring): try: y, m, d = date hh, mm, ss = 0., 0., 0. except: y, m, d, hh, mm, ss = date y, m, d, hh, mm, ss = greg2( julianmd(y, m, d, hh, mm, ss) - julianmd(y, 1, 1), y) return datetime.datetime(y, m, d, hh, mm, ss) else: return date
def fix_date(date): from calc import isiterable from air_sea import julianmd, greg2 """ Support for "wrong" dates like (2011,13,-1) Input must be an iterable, like list or tuple, not string Example: fix_date((2011,13,-1)) returns datetime.datetime(2011, 12, 30, 0, 0) """ if isiterable(date) and not isinstance(date, basestring): try: y, m, d = date hh, mm, ss = 0.0, 0.0, 0.0 except: y, m, d, hh, mm, ss = date y, m, d, hh, mm, ss = greg2(julianmd(y, m, d, hh, mm, ss) - julianmd(y, 1, 1), y) return datetime.datetime(y, m, d, hh, mm, ss) else: return date
def use(filename, varname, interface="auto", **kargs): nc, close = __open(filename, interface) if varname not in nc.varnames: return v = nc.vars[varname] shape = v.shape() if not shape: try: return v[:][0] # may be needed for dtype='|S1' except: return v[:] d = v.dims dimStr = [":" for i in d.keys()] for k in kargs.keys(): dval = kargs[k] if k.isdigit(): k = d.keys()[int(k)] # allow use dim indice elif k.startswith("SEARCH"): # allow *dimname or dimname* kk = k[len("SEARCH") :] for dn in nc.vars[varname].dimnames: if dn.find(kk) >= 0: k = dn break elif k.endswith("SEARCH"): kk = k[: -len("SEARCH")] for dn in nc.vars[varname].dimnames: if dn.find(kk) >= 0: k = dn break if k in d.keys(): i = d.index(k) if isinstance(dval, basestring): dimStr[i] = dval elif isinstance(dval, int): if dval < 0: dval = shape[i] + dval if dval > shape[i] - 1: print ":: max allowed " + k + " = " + str(shape[i] - 1) return else: dimStr[i] = str(dval) elif calc.isiterable(dval): exec (k + "_val=dval") dimStr[i] = k + "_val" else: dimStr[i] = str(dval) cmd = "res=v[" + sjoin(dimStr, ",") + "]" exec (cmd) if interface in ("pycdf", "scientific"): # about missing value: miss = False if "_FillValue" in v.attnames: miss = v.atts["_FillValue"]["value"] elif "missing_value" in v.attnames: miss = v.atts["missing_value"]["value"] maskMissing = kargs.get("maskMissing", True) if not miss is False and maskMissing and v.nctype() != "STRING": res = np.ma.masked_where(res == miss, res) ## ensure strings have no mask: # if v.nctype()=='STRING' and np.ma.isMA(res): # res=np.array(res) # about scale and offset: if v.nctype() != "STRING": scale = 1 offset = 0 if "scale_factor" in v.attnames: scale = v.atts["scale_factor"]["value"] if "add_offset" in v.attnames: offset = v.atts["add_offset"]["value"] if (scale, offset) != (1, 0): res = res * scale + offset if close: nc.close() if 1 in res.shape and res.ndim > 1: res = np.squeeze(res) # mask nan maskNaN = kargs.get("maskNaN", True) if maskNaN and not res.dtype.type == np.string_ and not np.ma.isMA(res) and np.any(np.isnan(res)): res = np.ma.masked_where(np.isnan(res), res) return res
def use(filename,varname,interface='auto',**kargs): nc,close=__open(filename,interface) if varname not in nc.varnames: return v=nc.vars[varname] shape=v.shape() if not shape: try: return v[:][0] # may be needed for dtype='|S1' except: return v[:] d=v.dims dimStr=[':' for i in d.keys()] for k in kargs.keys(): dval=kargs[k] if k.isdigit(): k=d.keys()[int(k)] # allow use dim indice elif k.startswith('SEARCH'): # allow *dimname or dimname* kk=k[len('SEARCH'):] for dn in nc.vars[varname].dimnames: if dn.find(kk)>=0: k=dn break elif k.endswith('SEARCH'): kk=k[:-len('SEARCH')] for dn in nc.vars[varname].dimnames: if dn.find(kk)>=0: k=dn break if k in d.keys(): i=d.index(k) if isinstance(dval,basestring): dimStr[i]=dval elif isinstance(dval,int): if dval<0: dval=shape[i]+dval if dval>shape[i]-1: print ':: max allowed '+k+' = '+str(shape[i]-1) return else: dimStr[i]=str(dval) elif calc.isiterable(dval): exec(k+'_val=dval') dimStr[i]=k+'_val' else: dimStr[i]=str(dval) cmd='res=v['+sjoin(dimStr,',')+']' exec(cmd) if interface in ('pycdf','scientific'): # about missing value: miss=False if '_FillValue' in v.attnames: miss = v.atts['_FillValue']['value'] elif 'missing_value' in v.attnames: miss = v.atts['missing_value']['value'] maskMissing=kargs.get('maskMissing',True) if not miss is False and maskMissing and v.nctype()!='STRING': res=np.ma.masked_where(res==miss,res) ## ensure strings have no mask: #if v.nctype()=='STRING' and np.ma.isMA(res): # res=np.array(res) # about scale and offset: if v.nctype()!='STRING': scale=1 offset=0 if 'scale_factor' in v.attnames: scale=v.atts['scale_factor']['value'] if 'add_offset' in v.attnames: offset=v.atts['add_offset']['value'] if (scale,offset)!=(1,0): res = res*scale + offset if close: nc.close() if 1 in res.shape and res.ndim>1: res=np.squeeze(res) # mask nan maskNaN=kargs.get('maskNaN',True) if maskNaN and not res.dtype.type==np.string_ and not np.ma.isMA(res) and np.any(np.isnan(res)): res=np.ma.masked_where(np.isnan(res),res) return res