def imshow(self, *args, **kwargs): """ Display an image on the 3D axes. :param x: (*array_like*) Optional. X coordinate array. :param y: (*array_like*) Optional. Y coordinate array. :param z: (*array_like*) 2-D or 3-D (RGB) z value array. :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level curves to draw, in increasing order. :param cmap: (*string*) Color map string. :param colors: (*list*) If None (default), the colormap specified by cmap will be used. If a string, like ‘r’ or ‘red’, all levels will be plotted in this color. If a tuple of matplotlib color args (string, float, rgb, etc), different levels will be plotted in different colors in the order specified. :returns: (*RasterLayer*) RasterLayer created from array data. """ n = len(args) cmap = plotutil.getcolormap(**kwargs) fill_value = kwargs.pop('fill_value', -9999.0) xaxistype = None isrgb = False if n <= 2: if isinstance(args[0], (list, tuple)): isrgb = True rgbdata = args[0] if isinstance(rgbdata[0], MIArray): x = minum.arange(0, rgbdata[0].shape[1]) y = minum.arange(0, rgbdata[0].shape[0]) else: x = rgbdata[0].dimvalue(1) y = rgbdata[0].dimvalue(0) elif args[0].ndim > 2: isrgb = True rgbdata = args[0] if isinstance(rgbdata, MIArray): x = minum.arange(0, rgbdata.shape[1]) y = minum.arange(0, rgbdata.shape[0]) else: x = rgbdata.dimvalue(1) y = rgbdata.dimvalue(0) else: gdata = minum.asgridarray(args[0]) if isinstance(args[0], DimArray): if args[0].islondim(1): xaxistype = 'lon' elif args[0].islatdim(1): xaxistype = 'lat' elif args[0].istimedim(1): xaxistype = 'time' args = args[1:] elif n <= 4: x = args[0] y = args[1] a = args[2] if isinstance(a, (list, tuple)): isrgb = True rgbdata = a elif a.ndim > 2: isrgb = True rgbdata = a else: gdata = minum.asgridarray(a, x, y, fill_value) args = args[3:] offset = kwargs.pop('offset', 0) zdir = kwargs.pop('zdir', 'z') interpolation = kwargs.pop('interpolation', None) if isrgb: if isinstance(rgbdata, (list, tuple)): rgbd = [] for d in rgbdata: rgbd.append(d.asarray()) rgbdata = rgbd else: rgbdata = rgbdata.asarray() x = plotutil.getplotdata(x) y = plotutil.getplotdata(y) graphics = GraphicFactory.createImage(x, y, rgbdata, offset, zdir, interpolation) ls = None else: if len(args) > 0: level_arg = args[0] if isinstance(level_arg, int): cn = level_arg ls = LegendManage.createImageLegend(gdata, cn, cmap) else: if isinstance(level_arg, MIArray): level_arg = level_arg.aslist() ls = LegendManage.createImageLegend(gdata, level_arg, cmap) else: ls = plotutil.getlegendscheme(args, gdata.min(), gdata.max(), **kwargs) ls = ls.convertTo(ShapeTypes.Image) plotutil.setlegendscheme(ls, **kwargs) if zdir == 'xy': sepoint = kwargs.pop('sepoint', [0, 0, 1, 1]) else: sepoint = None graphics = GraphicFactory.createImage(gdata, ls, offset, zdir, sepoint, interpolation) visible = kwargs.pop('visible', True) if visible: self.add_graphic(graphics) return graphics
def ncwrite(fn, data, varname, dims=None, attrs=None, gattrs=None, largefile=False): """ Write a netCDF data file from an array. :param: fn: (*string*) netCDF data file path. :param data: (*array_like*) A numeric array variable of any dimensionality. :param varname: (*string*) Variable name. :param dims: (*list of dimensions*) Dimension list. :param attrs: (*dict*) Variable attributes. :param gattrs: (*dict*) Global attributes. :param largefile: (*boolean*) Create netCDF as large file or not. """ if dims is None: if isinstance(data, MIArray): dims = [] for s in data.shape: dimvalue = minum.arange(s) dimname = 'dim' + str(len(dims)) dims.append(dimension(dimvalue, dimname)) else: dims = data.dims #New netCDF file ncfile = addfile(fn, 'c', largefile=largefile) #Add dimensions ncdims = [] for dim in dims: ncdims.append(ncfile.adddim(dim.getShortName(), dim.getLength())) #Add global attributes ncfile.addgroupattr('Conventions', 'CF-1.6') ncfile.addgroupattr('Tools', 'Created using MeteoInfo') if not gattrs is None: for key in gattrs: ncfile.addgroupattr(key, gattrs[key]) #Add dimension variables dimvars = [] wdims = [] for dim, midim in zip(ncdims, dims): dimtype = midim.getDimType() dimname = dim.getShortName() if dimtype == DimensionType.T: var = ncfile.addvar(dimname, 'int', [dim]) var.addattr('units', 'hours since 1900-01-01 00:00:0.0') var.addattr('long_name', 'Time') var.addattr('standard_name', 'time') var.addattr('axis', 'T') tvar = var elif dimtype == DimensionType.Z: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'Z') elif dimtype == DimensionType.Y: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'Y') elif dimtype == DimensionType.X: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'X') else: var = None if not var is None: dimvars.append(var) wdims.append(midim) #Add variable var = ncfile.addvar(varname, data.dtype, ncdims) if attrs is None: var.addattr('name', varname) else: for key in attrs: var.addattr(key, attrs[key]) #Create netCDF file ncfile.create() #Write variable data for dimvar, dim in zip(dimvars, wdims): if dim.getDimType() == DimensionType.T: sst = datetime.datetime(1900, 1, 1) tt = miutil.nums2dates(dim.getDimValue()) hours = [] for t in tt: hours.append((t - sst).total_seconds() // 3600) ncfile.write(dimvar, minum.array(hours)) else: ncfile.write(dimvar, minum.array(dim.getDimValue())) ncfile.write(var, data) #Close netCDF file ncfile.close()
def imshow(self, *args, **kwargs): """ Display an image on the map. :param x: (*array_like*) Optional. X coordinate array. :param y: (*array_like*) Optional. Y coordinate array. :param z: (*array_like*) 2-D z value array. :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level curves to draw, in increasing order. :param cmap: (*string*) Color map string. :param colors: (*list*) If None (default), the colormap specified by cmap will be used. If a string, like ‘r’ or ‘red’, all levels will be plotted in this color. If a tuple of matplotlib color args (string, float, rgb, etc), different levels will be plotted in different colors in the order specified. :param fill_value: (*float*) Fill_value. Default is ``-9999.0``. :param fill_color: (*color*) Fill_color. Default is None (white color). :param proj: (*ProjectionInfo*) Map projection of the data. Default is None. :param zorder: (*int*) Z-order of created layer for display. :param interpolation: (*string*) Interpolation option [None | bilinear | bicubic]. :returns: (*RasterLayer*) RasterLayer created from array data. """ cmap = plotutil.getcolormap(**kwargs) fill_value = kwargs.pop('fill_value', -9999.0) ls = kwargs.pop('symbolspec', None) n = len(args) isrgb = False if n <= 2: if isinstance(args[0], (list, tuple)): isrgb = True rgbdata = args[0] if isinstance(rgbdata[0], DimArray): x = rgbdata[0].dimvalue(1) y = rgbdata[0].dimvalue(0) else: x = minum.arange(0, rgbdata[0].shape[1]) y = minum.arange(0, rgbdata[0].shape[0]) elif args[0].ndim > 2: isrgb = True rgbdata = args[0] x = rgbdata.dimvalue(1) y = rgbdata.dimvalue(0) else: gdata = minum.asgridarray(args[0]) args = args[1:] elif n <=4: x = args[0] y = args[1] a = args[2] if isinstance(a, (list, tuple)): isrgb = True rgbdata = a elif a.ndim > 2: isrgb = True rgbdata = a else: gdata = minum.asgridarray(a, x, y, fill_value) args = args[3:] isadd = kwargs.pop('isadd', True) interpolation = kwargs.pop('interpolation', None) if isrgb: if isinstance(rgbdata, (list, tuple)): rgbd = [] for d in rgbdata: rgbd.append(d.asarray()) rgbdata = rgbd else: rgbdata = rgbdata.asarray() extent = [x[0],x[-1],y[0],y[-1]] igraphic = GraphicFactory.createImage(rgbdata, extent) x = plotutil.getplotdata(x) y = plotutil.getplotdata(y) layer = DrawMeteoData.createImageLayer(x, y, igraphic, 'layer_image') else: if len(args) > 0: if ls is None: level_arg = args[0] if isinstance(level_arg, int): cn = level_arg ls = LegendManage.createImageLegend(gdata, cn, cmap) else: if isinstance(level_arg, MIArray): level_arg = level_arg.aslist() ls = LegendManage.createImageLegend(gdata, level_arg, cmap) else: if ls is None: ls = LegendManage.createImageLegend(gdata, cmap) plotutil.setlegendscheme(ls, **kwargs) fill_color = kwargs.pop('fill_color', None) if not fill_color is None: cb = ls.getLegendBreaks().get(ls.getBreakNum() - 1) if cb.isNoData(): cb.setColor(plotutil.getcolor(fill_color)) layer = DrawMeteoData.createRasterLayer(gdata, 'layer', ls) proj = kwargs.pop('proj', None) if not proj is None: layer.setProjInfo(proj) if not interpolation is None: layer.setInterpolation(interpolation) if isadd: zorder = kwargs.pop('zorder', None) select = kwargs.pop('select', True) if zorder is None: zorder = 0 self.add_layer(layer, zorder, select) self.axes.setDrawExtent(layer.getExtent().clone()) self.axes.setExtent(layer.getExtent().clone()) return MILayer(layer)
def ncwrite(fn, data, varname, dims=None, attrs=None, largefile=False): """ Write a netCDF data file. :param: fn: (*string*) netCDF data file path. :param data: (*array_like*) A numeric array variable of any dimensionality. :param varname: (*string*) Variable name. :param dims: (*list of dimensions*) Dimension list. :param attrs: (*list of attributes*) Attribute list. :param largefile: (*boolean*) Create netCDF as large file or not. """ if dims is None: if isinstance(data, MIArray): dims = [] for s in data.shape: dimvalue = minum.arange(s) dimname = 'dim' + str(len(dims)) dims.append(dimension(dimvalue, dimname)) else: dims = data.dims #New netCDF file ncfile = midata.addfile(fn, 'c') #Add dimensions ncdims = [] for dim in dims: ncdims.append(ncfile.adddim(dim.getShortName(), dim.getLength())) #Add global attributes ncfile.addgroupattr('Conventions', 'CF-1.6') ncfile.addgroupattr('Tools', 'Created using MeteoInfo') #Add dimension variables dimvars = [] for dim,midim in zip(ncdims,dims): dimtype = midim.getDimType() dimname = dim.getShortName() if dimtype == DimensionType.T: var = ncfile.addvar(dimname, 'int', [dim]) var.addattr('units', 'hours since 1900-01-01 00:00:0.0') var.addattr('long_name', 'Time') var.addattr('standard_name', 'time') var.addattr('axis', 'T') tvar = var elif dimtype == DimensionType.Z: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'Z') elif dimtype == DimensionType.Y: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'Y') elif dimtype == DimensionType.X: var = ncfile.addvar(dimname, 'float', [dim]) var.addattr('axis', 'X') else: var = ncfile.addvar(dim.getShortName(), 'float', [dim]) var.addattr('axis', 'null') dimvars.append(var) #Add variable var = ncfile.addvar(varname, data.dtype, ncdims) if attrs is None: var.addattr('name', varname) else: for key in attrs: var.addattr(key, attr[key]) #Create netCDF file ncfile.create() #Write variable data for dimvar, dim in zip(dimvars, dims): if dim.getDimType() == DimensionType.T: sst = datetime.datetime(1900,1,1) tt = miutil.nums2dates(dim.getDimValue()) hours = [] for t in tt: hours.append((t - sst).total_seconds() // 3600) ncfile.write(dimvar, minum.array(hours)) else: ncfile.write(dimvar, minum.array(dim.getDimValue())) ncfile.write(var, data) #Close netCDF file ncfile.close()
def imshow(self, *args, **kwargs): """ Display an image on the 3D axes. :param x: (*array_like*) Optional. X coordinate array. :param y: (*array_like*) Optional. Y coordinate array. :param z: (*array_like*) 2-D or 3-D (RGB) z value array. :param levs: (*array_like*) Optional. A list of floating point numbers indicating the level curves to draw, in increasing order. :param cmap: (*string*) Color map string. :param colors: (*list*) If None (default), the colormap specified by cmap will be used. If a string, like ‘r’ or ‘red’, all levels will be plotted in this color. If a tuple of matplotlib color args (string, float, rgb, etc), different levels will be plotted in different colors in the order specified. :returns: (*RasterLayer*) RasterLayer created from array data. """ n = len(args) cmap = plotutil.getcolormap(**kwargs) fill_value = kwargs.pop('fill_value', -9999.0) xaxistype = None isrgb = False if n <= 2: if isinstance(args[0], (list, tuple)): isrgb = True rgbdata = args[0] if isinstance(rgbdata[0], MIArray): x = minum.arange(0, rgbdata[0].shape[1]) y = minum.arange(0, rgbdata[0].shape[0]) else: x = rgbdata[0].dimvalue(1) y = rgbdata[0].dimvalue(0) elif args[0].ndim > 2: isrgb = True rgbdata = args[0] if isinstance(rgbdata, MIArray): x = minum.arange(0, rgbdata.shape[1]) y = minum.arange(0, rgbdata.shape[0]) else: x = rgbdata.dimvalue(1) y = rgbdata.dimvalue(0) else: gdata = minum.asgridarray(args[0]) if isinstance(args[0], DimArray): if args[0].islondim(1): xaxistype = 'lon' elif args[0].islatdim(1): xaxistype = 'lat' elif args[0].istimedim(1): xaxistype = 'time' args = args[1:] elif n <=4: x = args[0] y = args[1] a = args[2] if isinstance(a, (list, tuple)): isrgb = True rgbdata = a elif a.ndim > 2: isrgb = True rgbdata = a else: gdata = minum.asgridarray(a, x, y, fill_value) args = args[3:] offset = kwargs.pop('offset', 0) zdir = kwargs.pop('zdir', 'z') interpolation = kwargs.pop('interpolation', None) if isrgb: if isinstance(rgbdata, (list, tuple)): rgbd = [] for d in rgbdata: rgbd.append(d.asarray()) rgbdata = rgbd else: rgbdata = rgbdata.asarray() x = plotutil.getplotdata(x) y = plotutil.getplotdata(y) graphics = GraphicFactory.createImage(x, y, rgbdata, offset, zdir, interpolation) ls = None else: if len(args) > 0: level_arg = args[0] if isinstance(level_arg, int): cn = level_arg ls = LegendManage.createImageLegend(gdata, cn, cmap) else: if isinstance(level_arg, MIArray): level_arg = level_arg.aslist() ls = LegendManage.createImageLegend(gdata, level_arg, cmap) else: ls = plotutil.getlegendscheme(args, gdata.min(), gdata.max(), **kwargs) ls = ls.convertTo(ShapeTypes.Image) plotutil.setlegendscheme(ls, **kwargs) if zdir == 'xy': sepoint = kwargs.pop('sepoint', [0,0,1,1]) else: sepoint = None graphics = GraphicFactory.createImage(gdata, ls, offset, zdir, sepoint, interpolation) visible = kwargs.pop('visible', True) if visible: self.add_graphic(graphics) return graphics