コード例 #1
0
                    default='SONDE_,',
                    help='Remove,replace on sonde files')
parser.add_argument('sondes', nargs='+', help='Sonde files')
parser.add_argument('figpath', help='Path for output figure')

args = parser.parse_args()
modellabel = args.label
args.season = seasons[args.season]
mf = None
sf = None
sids = []
lats = []
tk = None
for path in args.sondes:
    modpath = path.replace(*args.modreplace.split(','))
    modf = pncopen(modpath, format='netcdf', addcf=False)
    mt = modf.getTimes()
    ismine = np.array([i for i, t in enumerate(mt) if t.month in args.season])
    if ismine.size == 0:
        continue
    if tk is None:
        if 'TSTEP' in modf.dimensions:
            tk = 'TSTEP'
        else:
            tk = 'time'
    modf = modf.sliceDimensions(**{tk: ismine})\
               .applyAlongDimensions(**{tk: 'mean'})
    if mf is None:
        mf = modf
    else:
        mf = mf.stack(modf, tk)
コード例 #2
0
                    type=lambda x: tuple([eval(s) for s in x.split(',')]),
                    help='Label for model in figure')
parser.add_argument('--modreplace',
                    default='SONDE_,',
                    help='Remove,replace on sonde files')
parser.add_argument('sondes', nargs='+', help='Sonde files')
parser.add_argument('figpath', help='Path for output figure')

args = parser.parse_args()
modellabel = args.label
mf = None
sf = None
tk = None
for path in args.sondes:
    modpath = path.replace(*args.modreplace.split(','))
    modf = pncopen(modpath, format='netcdf', addcf=False)
    if tk is None:
        if 'TSTEP' in modf.dimensions:
            tk = 'TSTEP'
        else:
            tk = 'time'
    if mf is None:
        mf = modf
    else:
        mf = mf.stack(modf, tk)
    sonf = pncopen(path, format='netcdf', addcf=False)
    if sf is None:
        sf = sonf
    else:
        sf = sf.stack(sonf, tk)
コード例 #3
0
ファイル: extrareader.py プロジェクト: barronh/evalwoudc
    def __init__(self, *args, **kwds):
        """
        GCNC is defined here for the sole purpose of evalwoudc
        It copies variables and attributes from the new netCDF
        formatted GEOS-Chem and renames them to be consistent
        with the PseudoNetCDF bpch reader output. This allows
        the PseudoNetCDF bpch_base class to provide coordinate
        manipulation avgSigma and ll2ij
        """
        # Read the GEOS-Chem netCDF file
        ds = pncopen(*args, format='netcdf', **kwds)

        # Make quick references to object dictionaries
        gvs = ds.variables
        gds = ds.dimensions

        # Copy all global attributes
        self.setncatts(ds.getncatts())

        # Make a list of coordinate variables to copy
        coordkeys = 'time lat lon lev ilev hyai hybi'.split()
        for coordk in coordkeys:
            if coordk in gds:
                self.copyDimension(gds[coordk], key=coordk)
            self.copyVariable(gvs[coordk], key=coordk)

        # Copy SpeciesConc_O3 as O3
        self.copyVariable(gvs['SpeciesConc_O3'], 'O3')

        # Rename lat/lon dimensions and coordinate variables
        self.renameDimensions(lat='latitude', lon='longitude', inplace=True)
        self.renameVariables(lat='latitude', lon='longitude', inplace=True)

        # Get lat/lon for calculating edges
        lat = self.variables['latitude'][:]
        lon = self.variables['longitude'][:]
        dlat = np.abs(np.median(np.diff(lat))) / 2.
        dlon = np.abs(np.median(np.diff(lon))) / 2.

        # Create variables for holding lat/lon boundaries
        self.createDimension('nv', 2)
        latb = self.createVariable('latitude_bounds', lat.dtype,
                                   ('latitude', 'nv'))
        latb[:, 0] = np.maximum(lat - dlat, -90)
        latb[:, 1] = np.minimum(lat + dlat, 90)
        lonb = self.createVariable('longitude_bounds', lat.dtype,
                                   ('longitude', 'nv'))
        lonb[:, 0] = lon - dlon
        lonb[:, 1] = lon + dlon

        # Convert the units of O3 to ppb
        ov = self.variables['O3']
        ov[:] *= 1e9
        ov.units = 'ppb'

        # Convert times to have an absolute reference
        # much more convenient stacking later.
        times = self.getTimes()
        tv = self.variables['time']
        tv.units = 'seconds since 1970-01-01 00:00:00+0000'
        tv[:] = np.array([t.timestamp() for t in times])
コード例 #4
0
ファイル: plot_sondes.py プロジェクト: barronh/evalwoudc
    default=False,
    action='store_true',
    help='Print bias on plot for each bin (every fifth between 1000-500hPa)')
parser.add_argument('--label', default='Mod', help='Label for model in figure')
parser.add_argument('--season',
                    default='ALL',
                    choices=list(seasons),
                    help='Choose a season, default ALL')
parser.add_argument('obs', help='Sonde observations')
parser.add_argument('mod', help='Mod for sondes')
parser.add_argument('figpath', help='Path for output figure')

args = parser.parse_args()
modellabel = args.label
args.season = seasons[args.season]
sf = pncopen(args.obs, format='ioapi', addcf=False)
mf = pncopen(args.mod, format='ioapi', addcf=False)
st = sf.getTimes()
sti = np.array([ti for ti, t in enumerate(st) if t.month in args.season])
mt = mf.getTimes()
mti = np.array([ti for ti, t in enumerate(mt) if t.month in args.season])
if 'TSTEP' in sf.dimensions:
    tk = 'TSTEP'
else:
    tk = 'time'

# sf = sf.sliceDimensions(**{tk: sti})
# mf = mf.sliceDimensions(**{tk: mti})

if 'Press' in sf.variables:
    psrf = sf.variables['Press'][:].mean()
コード例 #5
0
ファイル: _vaisala.py プロジェクト: yosukefk/pseudonetcdf
        # Get altitude edges
        altedges = self.variables['altitude_edges']
        pr = self.variables['blview_backscatter_profile']
        plot_kw.setdefault('norm', plt.matplotlib.colors.LogNorm())
        patches = ax.pcolormesh(time_edges[:], altedges[:], pr[:].T, **plot_kw)
        plt.colorbar(patches, cax=cax, label=pr.units.strip())
        return fig


class test_vaisala(unittest.TestCase):
    def setUp(self):
        from PseudoNetCDF.testcase import ceilometerfiles_paths
        self.vaisalapath = ceilometerfiles_paths['vaisala']

    def testVAISALA2NCF(self):
        import os
        vfile = ceilometerl2(self.vaisalapath)
        outpath = self.vaisalapath + '.nc'
        ncfile = vfile.save(outpath)
        for k, ncv in ncfile.variables.items():
            vpv = vfile.variables[k]
            np.testing.assert_allclose(ncv[...], vpv[...])
        os.remove(outpath)


if __name__ == '__main__':
    from PseudoNetCDF import pncopen
    testdir = '/work/ROMO/users/kpc/flintfx/convceilometer/'
    testpath = testdir + 'CEILOMETER_1_LEVEL_2_20.his'
    f = pncopen(testpath, format='ceilometerl2')
コード例 #6
0
ファイル: extract_sondes.py プロジェクト: barronh/evalwoudc
        print('Skipping', ', '.join(notmydatestrs))

    if len(mydatestrs) == 0:
        print('Skipping', prefix, 'no files match filter')
        continue

    # Make a list of model paths for the sonde files in teh season of interest
    modpaths = [
        dates[datestr].strftime(args.intemplate) for datestr in mydatestrs
    ]

    # Make a list of paths for the sonde files in the season of interest
    sondepaths = [sondepathdict[datestr] for datestr in mydatestrs]

    # Open an example file for metadata
    tmpf = pncopen(modpaths[0], **modformat, addcf=False)

    # Use temp file to calculate i, j indices and ensure they are not arrays
    nrow = len(tmpf.dimensions[dim2dim['latitude']])
    ncol = len(tmpf.dimensions[dim2dim['longitude']])
    try:
        modi, modj = tmpf.ll2ij(lon, lat)
        modi = int(modi)
        modj = int(modj)
        if ((modi < 0) or (modj < 0) or (modi >= ncol) or (modj >= nrow)):
            raise ValueError()
    except Exception:
        print('Skipping', prefix, 'not in domain')
        continue

    # For CMAQ construct a "hybrid" sigma eta
コード例 #7
0
from glob import glob
from PseudoNetCDF import pncopen
from collections import OrderedDict
import json
import sys

dataroot = sys.argv[1]
paths = glob(dataroot + '/*.l100')
out = {}
for path in paths:
    print(path)
    try:
        f = pncopen(path, format='l100', addcf=False)
        stype = 'NOAA'
        sid = f.Flight_Number[:2]
        key = '%s%s' % (stype, sid)
        print(key)
        name = f.Station
        lat = f.variables['latitude'][0]
        lon = f.variables['longitude'][0]
        hght = f.Station_Height
        hght_unit = hght.split(' ')[1]
        outdate = f.getTimes()[0]
        datestr = outdate.strftime('%F %H:%M:%S%z')
        print(outdate)

        if key not in out:
            print('addedkey', key)
            mine = out[key] = OrderedDict()
            mine['Station'] = name
            mine['Station Height'] = '{} {}'.format(hght, hght_unit)
コード例 #8
0
ファイル: getlocs.py プロジェクト: barronh/evalwoudc
from glob import glob
from PseudoNetCDF import pncopen
from collections import OrderedDict
import json
import sys

dataroot = sys.argv[1]
paths = glob(dataroot + '/*')
out = {}
for path in paths:
    print(path)
    try:
        f = pncopen(path, format='woudcsonde', addcf=False)
        stype = f.variables['PLATFORM_Type'].view('S64')[0, 0].decode().strip()
        sid = f.variables['PLATFORM_ID'][:].array()[0]
        key = '%s%03d' % (stype, sid)
        print(key)
        name = f.variables['PLATFORM_Name'].view('S64')[0, 0].decode()
        lat = f.variables['LOCATION_Latitude'][0]
        lon = f.variables['LOCATION_Longitude'][0]
        hghtv = f.variables['LOCATION_Height']
        hght = hghtv[0]
        hght_unit = hghtv.units.strip().replace('unknown', 'meters')
        outdate = f.getTimes()[0]
        datestr = outdate.strftime('%F %H:%M:%S%z')
        print(outdate)

        if key not in out:
            print('addedkey', key)
            mine = out[key] = OrderedDict()
            mine['Station'] = name
コード例 #9
0
import matplotlib.pyplot as plt
from warnings import warn
from scipy.stats import pearsonr, spearmanr
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--seasonkeys',
                    default='ALL DJF MAM JJA SON'.split(),
                    type=lambda x: x.split(','),
                    help='comma delimited seasons (ALL,DJF,MAM,SON)')
parser.add_argument('--obsname', default='OBS')
parser.add_argument('--modname', default='MOD')
parser.add_argument('obspath')
parser.add_argument('modpath')
args = parser.parse_args()

cmaqf = pncopen(args.modpath, addcf=False, format='netcdf')
cnetf = pncopen(args.obspath, addcf=False, format='netcdf')

times = cmaqf.getTimes()
season = dict(JULY=[7],
              DJF=[12, 1, 2],
              MAM=[3, 4, 5],
              JJA=[6, 7, 8],
              SON=[9, 10, 11],
              ALL=range(1, 13))

season = dict([(seasonk, season[seasonk]) for seasonk in args.seasonkeys])

sitenames = cnetf.SITENAMES.split('|')
for monthk, monthi in season.items():
    tslice = np.array([t.month in monthi for t in times])
コード例 #10
0
        INT*4 Number of particles
        INT*4 Number of pollutants
        INT*4 Time of particle dump (YEAR, MONTH, DAY, HOUR, MINUTES)
        """
        rec1 = np.fromfile(self._path, dtype='>i,>7i,>i', count=1)
        self._file.seek(0, 0)
        hdr = rec1['f1'][0]
        self.NPARTICLES = hdr[0]
        self.NPOLLUTANTS = hdr[1]
        self.YEAR = hdr[2]
        self.MONTH = hdr[3]
        self.DAY = hdr[4]
        self.HOUR = hdr[5]
        self.MINUTES = hdr[6]
        assert (rec1['f0'] == rec1['f2'])
        datfmt = '>i,>({},)f,>i,>i,>6f,>i,>i,>5i,>i'.format(self.NPOLLUTANTS)
        self._fmt = np.dtype([('header', '>i,>7i,>i'),
                              ('data', datfmt, (self.NPARTICLES, ))])
        data = np.fromfile(self._path, dtype=self._fmt)
        assert (data.shape[0] == 1)
        self._data = data[0]
        db = self._data['data']
        assert ((db['f0'] == db['f2']).all())
        assert ((db['f3'] == db['f5']).all())
        assert ((db['f6'] == db['f8']).all())


if __name__ == '__main__':
    from PseudoNetCDF import pncopen
    f = pncopen('PARDUMP_143', format='arlpardump')
コード例 #11
0
ファイル: hist2d.py プロジェクト: barronh/evalcastnet
    ax.plot([0, vmax], [0, vmax], color='k')
    pr = pearsonr(xc, yc)
    sr = spearmanr(xc, yc)
    ax.set_title(
        'Pearson (r={:0.2f}, p={:0.2f}); Spearman (r={:0.2f}, p={:0.2f})'.
        format(*(pr + sr)))
    ax.set_xlabel(
        args.obsname +
        ' O3 ppb ({:.0f}, {:.0f}, {:.0f})'.format(cnetmin, cnetmean, cnetmax))
    ax.set_ylabel(
        args.modname +
        ' O3 ppb ({:.0f}, {:.0f}, {:.0f})'.format(cmaqmin, cmaqmean, cmaqmax))
    return ax.figure


cmaqf = pncopen(args.modpath, addcf=False,
                format='netcdf').copy().removeSingleton()
cnetf = pncopen(args.obspath, addcf=False, format='netcdf').copy()
times = cmaqf.getTimes()

for monthk, monthi in season.items():
    tslice = np.array([t.month in monthi for t in times])
    print(monthk, monthi, tslice.sum())
    fig = plotslice(cmaqf, cnetf, tslice)
    fig.savefig('hist2d/hist2d_O3_' + monthk + '.png')

lidx = (np.arange(24,
                  len(times) - 24)[:, None] -
        cnetf.variables['TIME_OFFSET']).astype('i')
lidx.dimensions = ('time', 'site')
uidx, sidx = np.indices(lidx[:].shape)
sidx = lidx * 0 + sidx