Example #1
0
        if not os.path.exists(tracerpath):
            ncffile._tracerinfofile.seek(0, 0)
            ncffile._tracerinfofile.seek(0, 0)
            outtrace = open(tracerpath, 'w')
            outtrace.write(ncffile._tracerinfofile.read())
            outtrace.flush()
    if hasattr(ncffile, '_diaginfofile'):
        if not os.path.exists(diagpath):
            ncffile._diaginfofile.seek(0, 0)
            outdiag = open(diagpath, 'w')
            outdiag.write(ncffile._diaginfofile.read())
            outdiag.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('bpch', ncf2bpch)

import unittest
class TestMemmaps(unittest.TestCase):
    def setUp(self):
        from PseudoNetCDF.testcase import geoschemfiles_paths
        self.bpchpath=geoschemfiles_paths['bpch']
    
    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)
Example #2
0
        time_hdr[di].tofile(outfile)
        for spc_key, spc_name in zip(spc_names, spc_hdr[0]['DATA']):
            for zi in range(nz):
                var = ncffile.variables[str(np.char.strip(spc_key))]
                data = var[di, zi].astype('>f')
                buf = np.array(4+40+data.size*4).astype('>i')
                buf.tofile(outfile)
                np.array(1).astype('>i').tofile(outfile)
                spc_name.tofile(outfile)
                np.ma.filled(data).tofile(outfile)
                buf.tofile(outfile)
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.uamiv', ncf2uamiv)
registerwriter('uamiv', ncf2uamiv)


def write_emissions_ncf(infile,outfile):
    from operator import concat
    #initialize hdr_fmts with species count
    hdr_fmts=["10i60i3ifif","ffiffffiiiiifff","iiii","10i"*len(infile.variables.keys())]
    hdrlines=[]

    hdrlines.append(reduce(concat,[Asc2Int(s) for s in [infile.name, infile.note]])+[infile.ione, len(infile.variables.keys()),infile.start_date,infile.start_time,infile.end_date,infile.end_time])

    hdrlines.append([infile.rdum, infile.rdum, infile.iutm, infile.xorg, infile.yorg, infile.delx, infile.dely, len(infile.dimensions['COL']), len(infile.dimensions['ROW']), len(infile.dimensions['LAY']), infile.idum, infile.idum, infile.rdum, infile.rdum, infile.rdum])
    
    hdrlines.append([infile.ione,infile.ione,len(infile.dimensions['COL']),len(infile.dimensions['ROW'])])
    hdrlines.append(reduce(concat,[Asc2Int(s.ljust(10)) for s in infile.variables.keys()]))
Example #3
0
        _other_dtype = np.dtype(dict(names = ['SPAD1', 'KEY', 'EPAD1', 'SPAD2', 'DATA', 'EPAD2'], formats = ['>i', '8>S', '>i', '>i', '(%d, %d)>f' % (nrows, ncols), '>i']))
    else:
        _fland_dtype = np.dtype(dict(names = ['SPAD2', 'DATA', 'EPAD2'], formats = ['>i', '(%d, %d, %d)>f' % (nland, nrows, ncols), '>i']))
        _other_dtype = np.dtype(dict(names = ['SPAD2', 'DATA', 'EPAD2'], formats = ['>i', '(%d, %d)>f' % (nrows, ncols), '>i']))

    outfile=open(outpath,'wb')
    keys = [key for key in ['FLAND', 'VAR1', 'LAI', 'TOPO', 'LUCAT11', 'LUCAT26'] if key in ncffile.variables.keys()]
    
    keyandvar = [(key, np.empty(shape = (1,), dtype = {'FLAND': _fland_dtype, 'LUCAT11': _fland_dtype, 'LUCAT26': _fland_dtype}.get(key, _other_dtype))) for key in keys]
    for key, var in keyandvar:
        invar = ncffile.variables[key]
        if newstyle:
            if key == 'FLAND'.strip():
                key = 'LUCAT%02d' % nland
        
            var['SPAD1'] = 8
            var['KEY'] = key.ljust(8)
            var['EPAD1'] = 8

        var['SPAD2'][...] = invar.size * 4
        var['DATA'][...] = invar[:]
        var['EPAD2'][...] = invar.size * 4
        var.tofile(outfile)

    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.landuse', ncf2landuse)
registerwriter('landuse', ncf2landuse)
Example #4
0
    buf = np.array(len(ncffile.FILEDESC) + 12, dtype = '>i').tostring()
    outfile.write(buf)
    outfile.write(np.array(ncffile.FILEDESC, dtype = 'c'))
    nxcl = len(ncffile.dimensions['COL'])
    nycl = len(ncffile.dimensions['ROW'])
    nzcl = len(ncffile.dimensions['LAY'])
    outfile.write(np.array([nxcl, nycl, nzcl], dtype = '>i'))
    outfile.write(buf)
        
    for di, (d,t) in enumerate(ncffile.variables[tflag][:,0,:]):
        t=np.array(t.astype('>f')/100,ndmin=1).astype('>f')
        d=np.array(d,ndmin=1).astype('>i')
        d=(d%(d//100000*100000)).astype('>i')
        buf = np.array(8, dtype = '>i').tostring()
        outfile.write(buf+t.tostring()+d.tostring()+buf)
        for zi in range(nzcl):
            for varkey in varkeys:
                vals = ncffile.variables[varkey][di, zi].astype('>f')
                buf=np.array((vals.size)*4,ndmin=1).astype('>i').tostring()
                outfile.write(buf)
                vals.tofile(outfile)
                outfile.write(buf)
    
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.cloud_rain', ncf2cloud_rain)
registerwriter('cloud_rain', ncf2cloud_rain)

Example #5
0
    for ename, ei in [('WEST', 1), ('EAST', 2), ('SOUTH', 3), ('NORTH', 4)]:
        if hasattr(ncffile, '_boundary_def'):
            ncffile._boundary_def[ename].tofile(outfile)
        else:
            nbcell = dict(WEST = NROWS, EAST = NROWS,
                          SOUTH = NCOLS, NORTH = NCOLS)[ename]
            buf = (nbcell * 4 + 3) * 4
            np.array([buf, 1, ei, nbcell, 0, 0, 0, 0] + [2, 0, 0, 0] * (nbcell - 2) + [0, 0, 0, 0, buf]).astype('>i').tofile(outfile)
    
    for di, (d, t) in enumerate(ncffile.variables['TFLAG'][:, 0]):
        time_hdr[di].tofile(outfile)
        for spc_key, spc_name in zip(spc_names, spc_hdr[0]['DATA']):
            for ename, ei in [('WEST', 1), ('EAST', 2), ('SOUTH', 3), ('NORTH', 4)]:
                var = ncffile.variables[ename + '_' + spc_key[0].decode().strip()]
                data = var[di].astype('>f')
                buf = np.array(4+40+4+data.size*4).astype('>i')
                buf.tofile(outfile)
                np.array(1).astype('>i').tofile(outfile)
                spc_name.tofile(outfile)
                np.array(ei).astype('>i').tofile(outfile)
                data.tofile(outfile)
                buf.tofile(outfile)
    
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.lateral_boundary', ncf2lateral_boundary)
registerwriter('lateral_boundary', ncf2lateral_boundary)

Example #6
0
__all__ = ["ncf2kv"]
__doc__ = """
.. _Write
:mod:`Write` -- CAMx vertical diffusivity  writer
=================================================

.. module:: Write
   :platform: Unix, Windows
   :synopsis: Provides :ref:`PseudoNetCDF` writer for CAMx vertical
              diffusivity files.  See PseudoNetCDF.sci_var.PseudoNetCDFFile 
              for interface details
.. moduleauthor:: Barron Henderson <[email protected]>
"""

from PseudoNetCDF.camxfiles.one3d.Write import ncf2one3d as ncf2vertical_diffusivity

from PseudoNetCDF._getwriter import registerwriter

registerwriter("camxfiles.vertical_diffusivity", ncf2vertical_diffusivity)
registerwriter("vertical_diffusivity", ncf2vertical_diffusivity)
Example #7
0
        props['DATA']['plmht'] = ncffile.variables['PLMHT'][di]
        props.tofile(outfile)
        for spc_key, spc_name in zip(spc_names, spc_hdr[0]['NAME']):
            var = ncffile.variables[spc_key]
            data = var[di].astype('>f')
            buf = np.array([4+40+data.size*4]).astype('>i')
            buf.tofile(outfile)
            np.array([1]).astype('>i').tofile(outfile)
            spc_name.tofile(outfile)
            data.tofile(outfile)
            buf.tofile(outfile)
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.point_source', ncf2point_source)
registerwriter('point_source', ncf2point_source)

def write_point(start_date,start_time,time_step,hdr,vals):
    #Internalize header
    hdr=[h for h in hdr]
    species=hdr[-4]
    nstk=hdr[-3][1]
    timeprops=hdr.pop()
    
    #initialize hdr_fmts with species count
    hdr_fmts=["10i60i3ifif","ffiffffiiiiifff","iiii","10i"*len(species),"ii","ffffff"*nstk]
    
    #initialize output variable
    pt_string=''
    
Example #8
0
    print('%d, %d' % (len(f.ncattrs()) + len(f.variables), 1001), file = outfile)
    print(getattr(f, 'PI_NAME', 'Unknown'), file = outfile)
    print(getattr(f, 'ORGANIZATION_NAME', 'Unknown'), file = outfile)
    print(getattr(f, 'SOURCE_DESCRIPTION', 'Unknown'), file = outfile)
    print(getattr(f, 'VOLUME_INFO', 'Unknown'), file = outfile)
    print(f.SDATE, f.WDATE, file = outfile)
    print(f.TIME_INTERVAL, file = outfile)
    print(f.INDEPENDENT_VARIABLE, file = outfile)
    print('%d' % len(f.variables), file = outfile)
    for key, var in f.variables.items():
        print('%s, %s' % (key, getattr(var, 'units', 'unknown')), file = outfile)
    
    print(len(f.ncattrs()), file = outfile)
    for key in f.ncattrs():
        print('%s: %s' % (key, getattr(f, key, '')), file = outfile)
    
    vals = [filled(f.variables[f.INDEPENDENT_VARIABLE][:]).ravel()]
    keys = [f.INDEPENDENT_VARIABLE]
    for key, var in f.variables.items():
        if key == f.INDEPENDENT_VARIABLE: continue
        keys.append(key)
        vals.append(filled(var[:]).ravel())
        
    print(', '.join(keys), file = outfile)
    for row in array(vals).T:
        row.tofile(outfile, format = '%.6e', sep = ', ')
        print('', file = outfile)

from PseudoNetCDF._getwriter import registerwriter
registerwriter('ffi1001', ncf2ffi1001)    
Example #9
0
            for varkey in varkeys:
                vals = ncffile.variables[varkey][di, zi].astype('>f')
                buf=np.array([(vals.size)*4],ndmin=1).astype('>i').tostring()
                outfile.write(buf)
                vals.tofile(outfile)
                outfile.write(buf)
        vals = np.array(0, dtype = '>i')
        buf=np.array([(vals.size)*4],ndmin=1).astype('>i').tostring()
        outfile.write(buf)
        vals.astype('>f').tofile(outfile)
        outfile.write(buf)
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.wind', ncf2wind)    
registerwriter('wind', ncf2wind)    

def write_wind(sdate,stime,time_step,vals,lstagger=None):
    """
    Takes an iterable and some defining information
    and creates a CAMx read wind file
    
    sdate - integer start date
    stime - float start time
    time_step - float increment between times
    
    vals - array axes time,uv,xy,z
    """
    wind_string=""    
    edate,etime=timeadd((sdate,stime),(0,vals.shape[0]*time_step))
Example #10
0
__all__ = ['ncf2hum']
__doc__ = """
.. _Write
:mod:`Write` -- CAMx humidity  writer
============================================

.. module:: Write
   :platform: Unix, Windows
   :synopsis: Provides :ref:`PseudoNetCDF` writer for CAMx
              humidity files.  See PseudoNetCDF.sci_var.PseudoNetCDFFile 
              for interface details
.. moduleauthor:: Barron Henderson <[email protected]>
"""

from PseudoNetCDF.camxfiles.one3d.Write import ncf2one3d as ncf2humidity

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.humidity', ncf2humidity)
registerwriter('humidity', ncf2humidity)
Example #11
0
        d=(d%(d//100000*100000)).astype('>i')
        for i,(h2d,p2d) in enumerate(zip(h3d,p3d)):
            h2d=h2d.astype('>f')
            p2d=p2d.astype('>f')
            buf=array((h2d.size+2)*4,ndmin=1).astype('>i').tostring()
            outfile.write(buf+t.tostring()+d.tostring());
            h2d.tofile(outfile)
            outfile.write(buf)
            outfile.write(buf+t.tostring()+d.tostring())
            p2d.tofile(outfile)
            outfile.write(buf)
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.height_pressure', ncf2height_pressure)
registerwriter('height_pressure', ncf2height_pressure)


def write_hgtprss(sdate,stime,time_step,vals):
    """
    Takes an iterable and some defining information
    and creates a CAMx read wind file
    
    sdate - integer start date
    stime - float start time
    time_step - float increment between times
    
    vals - array axes time,uv,xy,z
    """
    hp_string=""    
Example #12
0
        vars = [ifile.variables[k] for k in header]
        outtext = delimiter.join(dimheader + header)
        print(outtext, file = outfile)
        for idx in np.ndindex(ifile.variables[header[-1]].shape):
            outvals = []
            for dk, dv in zip(dimheader, dimvars):
                dv = ifile.variables[dk]
                didx = tuple([iidx for i, iidx in enumerate(idx) if dim[i] in dv.dimensions])
                outvals.append(repr(dv[didx]))
            for vk, vv in zip(header, vars):
                outvals.append(repr(vv[idx]))
            outtext = delimiter.join(outvals)
            print(outtext, file = outfile)

from PseudoNetCDF._getwriter import registerwriter
registerwriter('csv', ncf2csv)
import unittest
class TestMemmaps(unittest.TestCase):
    def setUp(self):
        import sys
        from PseudoNetCDF import PseudoNetCDFFile
        from PseudoNetCDF.pncgen import pncgen
        self.checkval = """time,layer,latitude,longitude,test
0.0,0.0,0.0,0.0,0.0
0.0,0.0,0.0,1.0,1.0
0.0,0.0,0.0,2.0,2.0
0.0,0.0,0.0,3.0,3.0
0.0,0.0,0.0,4.0,4.0
0.0,0.0,1.0,0.0,5.0
0.0,0.0,1.0,1.0,6.0
0.0,0.0,1.0,2.0,7.0
Example #13
0
def ncf2temperature(ncffile, outpath):
    outfile = open(outpath, 'wb')
    sfc = ncffile.variables['SURFTEMP']
    air = ncffile.variables['AIRTEMP']
    nz, nr, nc = air.shape[-3:]
    nelem = nr * nc * 4 + 8
    for di,(d,t) in enumerate(ncffile.variables['TFLAG'][:, 0]):
        t=np.array(t/100,ndmin=1, dtype = '>f')
        d=np.array(d,ndmin=1).astype('>i')
        d=(d%(d//100000*100000)).astype('>i')
        buf = np.array(nelem, dtype = '>i').tostring()
        outfile.write(buf)
        t.tofile(outfile)
        d.tofile(outfile)
        sfc[di].astype('>f').tofile(outfile)
        outfile.write(buf)
        for zi in range(nz):
            outfile.write(buf)
            t.tofile(outfile)
            d.tofile(outfile)
            air[di, zi].astype('>f').tofile(outfile)
            outfile.write(buf)
    
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.temperature', ncf2temperature)
registerwriter('temperature', ncf2temperature)
Example #14
0
.. module:: Write
   :platform: Unix, Windows
   :synopsis: Provides :ref:`PseudoNetCDF` writer for CAMx generic
              1 3D variable files.  See PseudoNetCDF.sci_var.PseudoNetCDFFile 
              for interface details
.. moduleauthor:: Barron Henderson <[email protected]>
"""

from numpy import array

def ncf2one3d(ncffile,outpath,key = None,tflag='TFLAG'):
    outfile=open(outpath,'wb')
    keys = ncffile.variables.keys()
    if key is None:
        key, = [k for k in keys if k != 'TFLAG']
    for (d,t),v3d in zip(ncffile.variables[tflag][:,0,:],ncffile.variables[key]):
        t=array(t.astype('>f')/100,ndmin=1).astype('>f')
        d=array(d,ndmin=1).astype('>i')
        d=(d%(d//100000*100000)).astype('>i')
        for v2d in v3d:
            v2d=v2d.astype('>f')
            buf=array((v2d.size+2)*4,ndmin=1).astype('>i').tostring()
            outfile.write(buf+t.tostring()+d.tostring()+v2d.tostring()+buf)
    outfile.flush()
    return outfile

from PseudoNetCDF._getwriter import registerwriter
registerwriter('camxfiles.one3d', ncf2one3d)
registerwriter('one3d', ncf2one3d)