Beispiel #1
0
 def testNCF2BPCH(self):
     import warnings
     with warnings.catch_warnings():
         warnings.filterwarnings(
             'ignore', 'Not scaling variables; good for direct writing'
         )
         bpchfile = bpch(self.bpchpath, noscale=True)
     outpath = self.bpchpath + '.check'
     from PseudoNetCDF.pncgen import pncgen
     pncgen(bpchfile, outpath, inmode='r',
            outmode='w', format='bpch', verbose=0)
     orig = open(self.bpchpath, 'rb').read()
     new = open(outpath, 'rb').read()
     assert(orig == new)
     os.remove(outpath)
     from PseudoNetCDF.sci_var import reduce_dim, slice_dim
     ALD2 = bpchfile.variables['IJ-AVG-$_ALD2']
     ALD2_check = np.array(
         [1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02,
          1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02,
          2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02,
          3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02,
          3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02,
          1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02,
          1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02,
          2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02,
          3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02,
          3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02,
          1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02,
          1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02,
          2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02,
          3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02,
          3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02])\
         .reshape(ALD2.shape)
     slided_reduced_bpchfile = slice_dim(
         reduce_dim(bpchfile, 'layer,mean'), 'time,0')
     pncgen(slided_reduced_bpchfile, outpath, inmode='r',
            outmode='w', format='bpch', verbose=0)
     ALD2_check_slided_reduced = ALD2_check[0].mean(0)[None, None]
     ALD2 = slided_reduced_bpchfile.variables['IJ-AVG-$_ALD2']
     np.testing.assert_allclose(ALD2, ALD2_check_slided_reduced * 1e-9)
     with warnings.catch_warnings():
         warnings.filterwarnings(
             'ignore', 'Not scaling variables; good for direct writing'
         )
         bpchfile = bpch(outpath)
     ALD2 = bpchfile.variables['IJ-AVG-$_ALD2']
     np.testing.assert_allclose(ALD2, ALD2_check_slided_reduced)
Beispiel #2
0
def get_group(file_objs, src, dates):
    """
    Return the first file with the SRC group
    """
    from PseudoNetCDF.sci_var import extract, slice_dim
    for f in file_objs:
        if hasattr(f, 'groups'):
            if src in f.groups:
                grp = f.groups[src]
                if 'TFLAG' in grp.variables.keys():
                    tflag = grp.variables['TFLAG']
                    thisdate = [datetime.strptime('%d %06d' % (d, t), '%Y%j %H%M%S') for d, t in tflag[:, 0]]
                    tname = 'TSTEP'
                elif 'time' in grp.variables.keys():
                    time = grp.variables['time']
                    unit, starttxt = grp.variables['time'].units.split(' since ')
                    starttxt = starttxt.replace('UTC', '').strip()
                    start_date = datetime.strptime(starttxt, '%Y-%m-%d %H:%M:%S')
                    thisdate = [start_date + timedelta(**{unit: t}) for t in time[:]]
                    tname = 'time'
                elif 'tau0' in grp.variables.keys():
                    time = grp.variables['tau0']
                    unit, starttxt = time.units.split(' since ')
                    start_date = datetime.strptime(start_date)
                    thisdate = [start_date + timedelta(**{unit: t}) for t in time]
                    tname = 'time'
                elif isinstance(f, (icon_profile, bcon_profile, profile_to_bdy, profile_to_grid)):
                    if len(dates) > 1:
                        for k in grp.variables.keys():
                            v = grp.variables[k]
                            if v.shape[0] == 1:
                                grp.variables[k] = v.repeat(len(dates), 0)
                    return grp
                else:
                    return grp
                thisdate = np.array(thisdate)
                idx, = np.where(np.logical_and(thisdate >= dates[0], thisdate <= dates[-1]))
                grpt = slice_dim(grp, '%s,%d,%d' % (tname, idx[0], idx[-1] + 1))
                return grpt
                
    else:
        raise KeyError('No file provided has the %s group' % src)
Beispiel #3
0
 def testNCF2BPCH(self):
     bpchfile=bpch(self.bpchpath, noscale = True)
     outpath = self.bpchpath + '.check'
     from PseudoNetCDF.pncgen import pncgen
     pncgen(bpchfile,outpath, inmode = 'r', outmode = 'w', format = 'bpch', verbose = False)
     orig = open(self.bpchpath, 'rb').read()
     new = open(outpath, 'rb').read()
     assert(orig == new)
     os.remove(outpath)
     from PseudoNetCDF.sci_var import reduce_dim, slice_dim
     ALD2 = bpchfile.variables['IJ-AVG-$_ALD2']
     ALD2_check = np.array([1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02, 1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02, 2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02, 3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02, 3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02, 1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02, 1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02, 2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02, 3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02, 3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02, 1.60520077e-02, 1.82803553e-02, 2.00258084e-02, 2.01461259e-02, 1.84865110e-02, 2.49667447e-02, 2.73083989e-02, 2.87465211e-02, 2.89694592e-02, 2.87686456e-02, 2.87277419e-02, 3.08121163e-02, 3.22086290e-02, 3.35262120e-02, 3.41329686e-02, 3.05218045e-02, 3.30278911e-02, 3.58164124e-02, 3.93186994e-02, 4.15412188e-02]).reshape(ALD2.shape)
     bpchfile = slice_dim(reduce_dim(bpchfile, 'layer,mean'), 'time,0')
     pncgen(bpchfile,outpath, inmode = 'r', outmode = 'w', format = 'bpch', verbose = False)
     ALD2_check = ALD2_check[0].mean(0)[None, None]
     ALD2 = bpchfile.variables['IJ-AVG-$_ALD2']
     np.testing.assert_allclose(ALD2, ALD2_check * 1e-9)
     bpchfile = bpch(outpath)
     ALD2 = bpchfile.variables['IJ-AVG-$_ALD2']
     np.testing.assert_allclose(ALD2, ALD2_check)
Beispiel #4
0
def makemaps(args):
    ifiles = args.ifiles
    cbar = None
    ifile = ifiles[0]
    if args.iter != []:
        ifile, = ifiles
        ifiles = []
        for dimk in args.iter:
            ifiles += [
                slice_dim(getvarpnc(ifile, None), '%s,%d' % (dimk, i))
                for i in range(len(ifile.dimensions[dimk]))
            ]
    ax = plt.gca()
    map = getmap(ifile, resolution=args.resolution)
    if args.coastlines:
        map.drawcoastlines(ax=ax)
    if args.countries:
        map.drawcountries(ax=ax)
    if args.states:
        map.drawstates(ax=ax)
    if args.counties:
        map.drawcounties(ax=ax)
    for si, shapefile in enumerate(args.shapefiles):
        shapeopts = shapefile.split(',')
        shapepath = shapeopts[0]
        shapeoptdict = eval('dict(' + ','.join(shapeopts[1:]) + ')')
        shapename = os.path.basename(shapepath)[:-3] + str(si)
        map.readshapefile(shapepath, shapename, ax=ax, **shapeoptdict)
    args.map = map
    fig = plt.gcf()
    if len(args.figure_keywords) > 0:
        plt.setp(fig, **args.figure_keywords)

    ax = plt.gca()
    if len(args.axes_keywords) > 0:
        plt.setp(ax, **args.axes_keywords)

    map = args.map
    nborders = len(ax.collections)
    for fi, ifile in enumerate(ifiles):
        if map.projection in ('lcc', 'merc'):
            lat = ifile.variables['latitude']
            lon = ifile.variables['longitude']
            latb, latunit = getybnds(ifile)[:]
            lonb, lonunit = getxbnds(ifile)[:]
        else:
            lat = ifile.variables['latitude']
            lon = ifile.variables['longitude']
            latb, latunit = getlatbnds(ifile)[:]
            lonb, lonunit = getlonbnds(ifile)[:]

        if latb.ndim == lonb.ndim and lonb.ndim == 2:
            LON, LAT = lonb, latb
        else:
            LON, LAT = np.meshgrid(lonb.view(np.ndarray),
                                   latb.view(np.ndarray))

        variables = args.variables
        if variables is None:

            def isgeo(var):
                geo2d = set(['latitude', 'longitude'])
                vard = getattr(var, 'coordinates', '').split()
                hasgeo2d = len(geo2d.intersection(vard)) == 2
                return hasgeo2d

            variables = [
                key for key, var in ifile.variables.items() if isgeo(var)
            ]
        if len(variables) == 0:
            raise ValueError('Unable to heuristically determin plottable ' +
                             'variables; use -v to specify variables for ' +
                             'plotting')
        for varkey in variables:
            ax = plt.gca()

            if not args.overlay:
                del ax.collections[nborders:]
            var = ifile.variables[varkey]
            if args.squeeze:
                vals = var[:].squeeze()
            else:
                vals = var[:]
            vmin, vmax = vals.min(), vals.max()
            if args.normalize is None:
                from scipy.stats import normaltest
                if normaltest(vals.ravel())[1] < 0.001:
                    cvals = np.ma.compressed(vals)
                    boundaries = np.percentile(cvals, np.arange(0, 110, 10))
                    warn('Autoselect deciles colormap of %s; override ' +
                         'width --norm' % varkey)
                else:
                    boundaries = np.linspace(vmin, vmax, num=11)
                    warn(('Autoselect linear colormap of %s; override ' +
                          'width --norm') % varkey)
                ordermag = (boundaries.max() /
                            np.ma.masked_values(boundaries, 0).min())
                if (ordermag) > 10000:
                    formatter = LogFormatter(labelOnlyBase=False)
                else:
                    formatter = None
                norm = BoundaryNorm(boundaries, ncolors=256)
            else:
                norm = eval(args.normalize)
                formatter = None
            if args.colorbarformatter is not None:
                try:
                    formatter = eval(args.colorbarformatter)
                except Exception:
                    formatter = args.colorbarformatter

            if norm.vmin is not None:
                vmin = norm.vmin
            if norm.vmax is not None:
                vmax = norm.vmax
            varunit = getattr(var, 'units', 'unknown').strip()
            if args.verbose > 0:
                print(varkey, sep='')
            if vals.ndim == 1:
                notmasked = ~(np.ma.getmaskarray(lon[:]) | np.ma.getmaskarray(
                    lat[:]) | np.ma.getmaskarray(vals[:]))
                scatlon = lon[:][notmasked]
                scatlat = lat[:][notmasked]
                scatvals = vals[:][notmasked]
                patches = map.scatter(scatlon[:],
                                      scatlat[:],
                                      c=scatvals,
                                      edgecolors='none',
                                      s=24,
                                      norm=norm,
                                      ax=ax,
                                      zorder=2)
            else:
                if vals.ndim != 2:
                    dimlendictstr = str(dict(zip(var.dimensions, var.shape)))
                    warn('Maps require 2-d data; values right now %s %s' %
                         (str(vals.shape), dimlendictstr))
                patches = map.pcolor(LON, LAT, vals, norm=norm, ax=ax)
            if lonunit == 'x (m)':
                ax.xaxis.get_major_formatter().set_scientific(True)
                ax.xaxis.get_major_formatter().set_powerlimits((-3, 3))
            if latunit == 'y (m)':
                ax.yaxis.get_major_formatter().set_scientific(True)
                ax.yaxis.get_major_formatter().set_powerlimits((-3, 3))
            ax.set_xlabel(lonunit)
            ax.set_ylabel(latunit)
            height = np.abs(np.diff(ax.get_ylim()))
            width = np.abs(np.diff(ax.get_xlim()))
            if width >= height:
                orientation = 'horizontal'
            else:
                orientation = 'vertical'
            if cbar is None:
                cax = None
            else:
                cax = cbar.ax
                cax.cla()

            if vals.max() > vmax and vals.min() < vmin:
                extend = 'both'
            elif vals.max() > vmax:
                extend = 'max'
            elif vals.min() < vmin:
                extend = 'min'
            else:
                extend = 'neither'
            cbar = plt.gcf().colorbar(patches,
                                      orientation=orientation,
                                      cax=cax,
                                      extend=extend,
                                      format=formatter,
                                      spacing='proportional')
            del cbar.ax.texts[:]
            varminmaxtxt = ('; min=%.3g; max=%.3g)' %
                            (var[:].min(), var[:].max()))
            cbar.set_label(varkey + ' (' + varunit + varminmaxtxt)
            # if orientation == 'vertical':
            #     cbar.ax.text(.5, 1.05, '%.3g' % var[:].max(),
            #                  horizontalalignment = 'center',
            #                  verticalalignment = 'bottom')
            #     cbar.ax.text(.5, -.06, '%.3g ' % var[:].min(),
            #                  horizontalalignment = 'center',
            #                  verticalalignment = 'top')
            # else:
            #     cbar.ax.text(1.05, .5, ' %.3g' % var[:].max(),
            #                  verticalalignment = 'center',
            #                  horizontalalignment = 'left')
            #     cbar.ax.text(-.06, .5, '%.3g ' % var[:].min(),
            #                  verticalalignment = 'center',
            #                  horizontalalignment = 'right')
            cbar.update_ticks()
            fmt = args.figformat
            outpath = args.outpath
            if len(ifiles) > 1:
                lstr = str(fi).rjust(len(str(len(ifiles))), '0')
                if args.verbose > 0:
                    print('adding numeric suffix for file', lstr)
            else:
                lstr = ''

            figpath = os.path.join(outpath + varkey + lstr + '.' + fmt)
            if args.interactive:
                csl = PNCConsole(locals=globals())
                csl.interact()
            for cmd in args.plotcommands:
                exec(cmd)
            plt.savefig(figpath)
            if args.verbose > 0:
                print('Saved fig', figpath)