def check_results(outfile, vobsfile, vobsvariable, balancevel):
    ###### check the results
    fo = NetCDFFile(outfile, 'r')
    thickness = fo.variables['thickness'][0, :]
    ux = fo.variables['uReconstructX'][0, :, :]  # use the first time level
    uy = fo.variables['uReconstructY'][0, :, :]  # use the first time level
    if balancevel:
        vModel = ((ux[:, 0]**2 + uy[:, 0]**2)**0.5).mean(
            1)  # get vertically averaged velocity
    else:
        vModel = (ux[:, 0]**2 +
                  uy[:, 0]**2)**0.5  # surface velocity (top vertical layer)

    cellMask = fo.variables['cellMask'][0, :]
    ind = (
        (cellMask & 4) == 4
    )  # 4 bit = grounded ice  # this gets the indices that are grounded ice.

    fv = NetCDFFile('../' + vobsfile, 'r')
    vOb = fv.variables[vobsvariable][0, :]

    rmse = (((vModel[ind] - vOb[ind])**2).sum() /
            ind.sum())**0.5  # TODO  check if this is calculating right
    print "RMSE=" + str(rmse)
    fo.close()
    fv.close()
Exemple #2
0
 def __init__(self, filename, whatToRead=None):
     """Construct a reader from a filename."""
     self.whatToRead = whatToRead
     try:
         self.nc = NetCDFFile(filename, 'r')
     except IOError:
         self.nc = NetCDFFile(filename + ".nc", 'r')
Exemple #3
0
def ncep2fall3d(ncep_filename, fall3d_ncep_filename, verbose=True):
    """Convert standard NCEP file to fall3d NCEP format
    """

    # Copy standard NCEP file to fall3d NCEP file
    s = 'cp %s %s' % (ncep_filename, fall3d_ncep_filename)
    os.system(s)

    # Open files
    infile = NetCDFFile(ncep_filename)
    outfile = NetCDFFile(ncep_filename, 'a')

    # Establish special global attributes for fall3 NCEP format

    print 'Found dimensions:', infile.dimensions.keys()
    print 'Found variables:', infile.variables.keys()

    lon = infile.variables['lon'][:]
    lonmin = min(lon)
    lonmax = max(lon)

    lat = infile.variables['lat'][:]
    latmin = min(lat)
    latmax = max(lat)

    nx = infile.dimensions['lon']
    ny = infile.dimensions['lat']
    np = infile.dimensions['pres']
    nt = infile.dimensions['time']
    print nx, ny, np, nt

    infile.close()
    outfile.close()
Exemple #4
0
 def openOutputFile(self):
     # Create file
     os.system('mkdir -p results/%s-%s' % (self.Case1,self.Case2))
     FileName = 'results/%s-%s/Cam3Feedbacks.%s-%s.%03i.nc' % \
                (self.Case1,self.Case2,self.Case1,self.Case2,self.FileNumber)
     if not os.path.exists(FileName):
         print 'creating %s ...' % FileName
         File = NetCDFFile(FileName,'w')
         File.createDimension('lat',len(self.data.lat))
         var = File.createVariable('lat','f',('lat',))
         var.long_name = 'latitude'
         var.units = 'degrees_north'
         var[:] = self.data.lat.astype('f')
         File.createDimension('lon',len(self.data.lon))
         var = File.createVariable('lon','f',('lon',))
         var.long_name = 'longitude'
         var.units = 'degrees_east'
         var[:] = self.data.lon.astype('f')
         # create variables
         for Field in ['dR_Ts','dR_lapse','dR_q','dR_cld_sw','dR_cld_lw','dR_alb','dR_co2']:
             var = File.createVariable(Field,'f',('lat','lon'))
             var.long_name = 'TOA radiative perturbation'
             var.units = 'W m-2'
             var[:,:] = 0.
         File.NsnapsDone = 0
         return 0, File
     else:
         File = NetCDFFile(FileName,'a')
         NsnapsDone = int(File.NsnapsDone[0])
         if NsnapsDone < len(self.data.time):
             return NsnapsDone, File
         else:
             print 'No more snaps to be done'
             sys.exit(0)
    def init(self,agent,env,**kw):
        super(LoggingRLI,self).init(agent,env,**kw)

        self.step_count = self.ep_count = 0

        if os.access(self.episode_filename,os.F_OK):
            self.remove_or_rename(self.episode_filename)

        self.episode_data = ed = NetCDFFile(self.episode_filename,'w')
        ed.createDimension('index',None)
        ed.createDimension('value',1)
        ed.createVariable('start','d',('index','value'))
        ed.createVariable('length','d',('index','value'))
        ed.createVariable('reward','f',('index','value'))

        for name,(fn,type,size) in self.ep_vars.items():
            ed.createDimension(name+'_dim',size)
            ed.createVariable(name,type,('index',name+'_dim'))

        if self.step_vars:
            if os.access(self.step_filename,os.F_OK):
                self.remove_or_rename(self.step_filename)

            self.step_data = sd = NetCDFFile(self.step_filename,'a')
            sd.createDimension('index',None)
            for name,(fn,type,size) in self.step_vars.items():
                sd.createDimension(name+'_dim',size)
                sd.createVariable(name,type,('index',name+'_dim'))

        self.last_ckpt_step = 0
        self.last_ckpt_episode = 0
Exemple #6
0
def open_rl_data(filespec):
    if isinstance(filespec,type([])):
        return map(open_rl_data,filespec)    
    ep_data = NetCDFFile(filespec,'r')
    step_file_name = filespec.split('-episodes.cdf')[0]+'-steps.cdf'
    if os.access(step_file_name,os.F_OK):
        step_data = NetCDFFile(step_file_name,'r')
    else:
        step_data = None

    return ep_data,step_data
Exemple #7
0
    def runTest(self):

        # Some information about the test to run are displayed in the console and file loggers.
        LogMessage('info',
                   'ANALYSIS: %s --- TEST No: %s' % (self.longName, self.id),
                   ['console', 'file'])

        subprocess.call([
            sys.executable, GVAR['pmoldyn_path'], self.pMoldynArg, "--input",
            os.path.join(self.testPath,
                         self.shortName + "%s_Reference.py" % self.id)
        ])
        subprocess.call([
            sys.executable,
            os.path.join(self.testPath,
                         self.shortName + "%d_Current.py" % self.id)
        ])

        refFileName = os.path.join(TEMP_DIR, self.shortName + "_Reference.nc")
        curFileName = os.path.join(TEMP_DIR, self.shortName + "_Current.nc")

        refFile = NetCDFFile(refFileName, 'r')
        curFile = NetCDFFile(curFileName, 'r')

        for key, val in refFile.variables.items():

            if val.typecode() != 'c':

                if not curFile.variables.has_key(key):
                    continue

                refValue = val.getValue()
                curValue = curFile.variables[key].getValue()

                # Their difference is computed.
                errorMax = max(abs(N.ravel(refValue - curValue)))

                self.errors[key] = errorMax

                # Throws an assertion error if the difference is bigger than 1.0E-6.
                self.assertAlmostEqual(errorMax, 0.0, 6)

        curFile.close()

        refFile.close()

        try:
            os.remove(refFileName)
            os.remove(curFileName)
        except:
            pass
Exemple #8
0
def filter_netcdf(filename1, filename2, first=0, last=None, step=1):
    """Filter data file, selecting timesteps first:step:last.
    
    Read netcdf filename1, pick timesteps first:step:last and save to
    nettcdf file filename2
    """

    from Scientific.IO.NetCDF import NetCDFFile

    # Get NetCDF
    infile = NetCDFFile(filename1, netcdf_mode_r)  #Open existing file for read
    outfile = NetCDFFile(filename2, netcdf_mode_w)  #Open new file

    # Copy dimensions
    for d in infile.dimensions:
        outfile.createDimension(d, infile.dimensions[d])

    # Copy variable definitions
    for name in infile.variables:
        var = infile.variables[name]
        outfile.createVariable(name, var.dtype.char, var.dimensions)

    # Copy the static variables
    for name in infile.variables:
        if name == 'time' or name == 'stage':
            pass
        else:
            outfile.variables[name][:] = infile.variables[name][:]

    # Copy selected timesteps
    time = infile.variables['time']
    stage = infile.variables['stage']

    newtime = outfile.variables['time']
    newstage = outfile.variables['stage']

    if last is None:
        last = len(time)

    selection = range(first, last, step)
    for i, j in enumerate(selection):
        log.critical('Copying timestep %d of %d (%f)'
                     % (j, last-first, time[j]))
        newtime[i] = time[j]
        newstage[i,:] = stage[j,:]

    # Close
    infile.close()
    outfile.close()
Exemple #9
0
    def open(self, filename):
        self.ncfile = NetCDFFile(filename, 'r')

        # initialize variable name list
        self.VarNameList = {}
        for VarName in self.ncfile.variables.keys():
            self.VarNameList[VarName] = True
Exemple #10
0
    def finalize(self):
        """Finalizes the calculations (e.g. averaging the total term, output files creations ...).
        """

        # The NetCDF output file is opened for writing.
        outputFile = NetCDFFile(self.output, 'w')
        outputFile.title = self.__class__.__name__
        outputFile.jobinfo = self.information + '\nOutput file written on: %s\n\n' % asctime(
        )

        # Dictionnary whose keys are of the form Gi where i is the group number
        # and the entries are the list of the index of the atoms building the group.
        comp = 1
        for g in self.group:
            outputFile.jobinfo += 'Group %d: %s\n' % (comp,
                                                      [index for index in g])
            comp += 1

        # Some dimensions are created.
        outputFile.createDimension('NFRAMES', self.nFrames)

        # Creation of the NetCDF output variables.
        # The time.
        TIMES = outputFile.createVariable('time', N.Float, ('NFRAMES', ))
        TIMES[:] = self.times[:]
        TIMES.units = 'ps'

        avacfTotal = N.zeros((self.nFrames), typecode=N.Float)

        for k in self.AVACF.keys():

            AVACF = outputFile.createVariable('avacf-group%s' % k, N.Float,
                                              ('NFRAMES', ))
            AVACF[:] = self.AVACF[k][:]
            AVACF.units = 'rad^2*ps^-2'

            N.add(avacfTotal, self.AVACF[k], avacfTotal)

        avacfTotal /= self.nGroups

        AVACF = outputFile.createVariable('avacf-total', N.Float,
                                          ('NFRAMES', ))
        AVACF[:] = avacfTotal[:]
        AVACF.units = 'rad^2*ps^-2'

        asciiVar = sorted(outputFile.variables.keys())

        outputFile.close()

        self.toPlot = {
            'netcdf': self.output,
            'xVar': 'time',
            'yVar': 'avacf-total'
        }

        # Create an ASCII version of the NetCDF output file.
        convertNetCDFToASCII(inputFile = self.output,\
                             outputFile = os.path.splitext(self.output)[0] + '.cdl',\
                             variables = asciiVar)
Exemple #11
0
    def __init__(self, filename):
        from Scientific.IO.NetCDF import NetCDFFile
        self.nc = NetCDFFile(filename, 'w')

        self.nc.file_format = 'ETSF Nanoquanta'
        self.nc.file_format_version = np.array([3.3], dtype=np.float32)
        self.nc.Conventions = 'http://www.etsf.eu/fileformats/'
        self.nc.history = 'File generated by ASE'
    def ckpt_restore_state(self,filename):
        from plastk import pkl
        ckpt = pkl.load(filename)

        self.verbose("Restoring checkpoint state")
        for a in self.ckpt_attribs:
            self.verbose(a,' = ', ckpt[a])
            setattr(self,a,ckpt[a])
            
        rand.seed(*ckpt['rand_seed'])

        self.env.sim = self.agent.sim = self
        
        self.episode_data = NetCDFFile(self.episode_filename,'a')
        if self.step_vars:
            self.step_data = NetCDFFile(self.step_filename,'a')
        return ckpt
 def __init__(self, filename):
     self.filename = filename
     self.file = NetCDFFile(self.filename, 'r')
     try:
         self.block_size = self.file.dimensions['minor_step_number']
     except KeyError:
         self.block_size = 1
     self._countSteps()
Exemple #14
0
    def __init__(self,
                 Case1='uh0',
                 Case2='uh1',
                 FileNumber=0):
        # Identify files
        # NOTE: assumes directories contain full years of data !!
        Home = os.getenv('HOME')
        Dir = '%s/cam/runs' % Home
        Dir1 = '%s/%s' % (Dir,Case1)
        Dir2 = '%s/%s' % (Dir,Case2)
        FileNames1 = glob.glob('%s/*cam2*h1*.nc' % Dir1)
        FileNames2 = glob.glob('%s/*cam2*h1*.nc' % Dir2)
        FileNames1.sort()
        FileNames2.sort()
        N1 = len(FileNames1)
        N2 = len(FileNames2)
        N = min(N1,N2)
        if N1 > N:
            for i in range(N1-N): FileNames1.pop()
        if N2 > N:
            for i in range(N2-N): FileNames2.pop()
        #self.Files1 = [NetCDFFile(File,'r') for File in FileNames1]
        #self.Files2 = [NetCDFFile(File,'r') for File in FileNames2]
        self.N = N
        self.FileName1 = FileNames1[FileNumber]
        self.FileName2 = FileNames2[FileNumber]
        self.File1 = NetCDFFile(self.FileName1,'r')
        self.File2 = NetCDFFile(self.FileName2,'r')

        print 'Using input files:'
        print self.FileName1
        print self.FileName2

        #  Extract hybrid coord coefficients and ref press
        File = self.File1
        self.hyam = File.variables['hyam'][:]
        self.hybm = File.variables['hybm'][:]
        self.hyai = File.variables['hyai'][:]
        self.hybi = File.variables['hybi'][:]
        self.p0   = File.variables['P0'].getValue()
        # Extract lat, lon, lev, time
        self.lat = File.variables['lat'][:]
        self.lon = File.variables['lon'][:]
        self.lev = File.variables['lev'][:]
        self.time = File.variables['time'][:]
 def __init__(self, filename, axesnames, variablename, default=None):
     from Scientific.IO.NetCDF import NetCDFFile
     self.file = NetCDFFile(filename, 'r')
     self.axes = map(lambda n, f=self.file: f.variables[n], axesnames)
     self.values = self.file.variables[variablename]
     self.default = default
     self.shape = ()
     for axis in self.axes:
         self.shape = self.shape + axis.shape
def load_geoinfo (SrcFilename, DstFilename, GridSize, LatName, LonName):
    # get lat/long values from 400/640 level files
    ncfile = front_end_NetCDF_helper()

    ncfile.open (SrcFilename)

    # read lat array
    SrcLatData = ncfile.read_data (LatName)
    #print SrcLatData

    # read lon array
    SrcLonData = ncfile.read_data (LonName)
    #print SrcLonData

    ncfile.close ()

    # create down-sampled long array    
    DstLonData = N.zeros (GridSize)

    # 
    for DstLonNo in range(0, GridSize):
        SrcLonNo = DstLonNo * 2

        # down-sampling strategy 1, use avarage
        #DstLonData[DstLonNo] = (SrcLonData[SrcLonNo] + SrcLonData[SrcLonNo+1])/2.0
        #print DstLonNo, SrcLonNo, DstLonData[DstLonNo], SrcLonData[SrcLonNo], SrcLonData[SrcLonNo+1]

        # start with 0 and skip alternate points
        DstLonData[DstLonNo] = SrcLonData[SrcLonNo]
        #print DstLonNo, SrcLonNo, DstLonData[DstLonNo], SrcLonData[SrcLonNo]

    # write NetCDF file
    # open output file
    dst_ncfile = NetCDFFile (DstFilename, 'w')

    # define dimensions
    dst_lat_dim = dst_ncfile.createDimension (LatName, GridSize)
    dst_lon_dim = dst_ncfile.createDimension (LonName, GridSize)

    # define variables
    dst_lat_var = dst_ncfile.createVariable (LatName, 'd', (LatName,))
    #dst_lat_var.setattr (
    # NetCDFFile.setattr (dst_lat_var, 'attrname', attr_val)
    
    dst_lon_var = dst_ncfile.createVariable (LonName, 'd', (LonName,))

    # write lat data
    dst_lat_var.assignValue(SrcLatData)

    # write lon data
    dst_lon_var.assignValue(DstLonData)

    # close output file
    dst_ncfile.close ()
Exemple #17
0
    def __init__(self,
                 quantity_name,
                 file_name,
                 time_step_count,
                 time_step,
                 lon,
                 lat):
        """Instantiate a Write_nc instance (NetCDF file writer).

        time_step_count is the number of time steps.
        time_step is the time step size

        pre-condition: quantity_name must be 'HA', 'UA'or 'VA'.
        """

        self.quantity_name = quantity_name
        quantity_units = {'HA':'CENTIMETERS',
                          'UA':'CENTIMETERS/SECOND',
                          'VA':'CENTIMETERS/SECOND'}

        multiplier_dic = {'HA':100.0,   # To convert from m to cm
                          'UA':100.0,   #             and m/s to cm/sec
                          'VA':-100.0}  # MUX files have positive x in the
                                        # Southern direction.  This corrects
                                        # for it, when writing nc files.

        self.quantity_multiplier =  multiplier_dic[self.quantity_name]

        #self.file_name = file_name
        self.time_step_count = time_step_count
        self.time_step = time_step

        # NetCDF file definition
        self.outfile = NetCDFFile(file_name, netcdf_mode_w)
        outfile = self.outfile

        #Create new file
        nc_lon_lat_header(outfile, lon, lat)

        # TIME
        outfile.createDimension(time_name, None)
        outfile.createVariable(time_name, precision, (time_name,))

        #QUANTITY
        outfile.createVariable(self.quantity_name, precision,
                               (time_name, lat_name, lon_name))
        outfile.variables[self.quantity_name].missing_value = -1.e+034
        outfile.variables[self.quantity_name].units = \
                                 quantity_units[self.quantity_name]
        outfile.variables[lon_name][:]= ensure_numeric(lon)
        outfile.variables[lat_name][:]= ensure_numeric(lat)
Exemple #18
0
    def __init__(self, filename='gpaw', title='gpaw'):
        if not filename.endswith('-etsf.nc'):
            if filename.endswith('.nc'):
                filename = filename[:-3] + '-etsf.nc'
            else:
                filename = filename + '-etsf.nc'

        self.nc = NetCDFFile(filename, 'w')

        self.nc.file_format = 'ETSF Nanoquanta'
        self.nc.file_format_version = np.array([3.3], dtype=np.float32)
        self.nc.Conventions = 'http://www.etsf.eu/fileformats/'
        self.nc.history = 'File generated by GPAW'
        self.nc.title = title
Exemple #19
0
    def __init__(self,
                 filename,
                 axesnames,
                 variablename,
                 default=None,
                 period=None):
        """
        @param filename: the name of the netCDF file
        @type filename: C{str}

        @param axesnames: the names of the netCDF variables that contain the
            axes information
        @type axesnames: sequence of C{str}

        @param variablename: the name of the netCDF variable that contains
            the data values
        @type variablename: C{str}

        @param default: the value of the function outside the grid. A value
            of C{None} means that the function is undefined outside
            the grid and that any attempt to evaluate it there
            raises an exception.
        @type default: number or C{None}

        @param period: the period for each of the variables, or C{None} for
            variables in which the function is not periodic.
        @type period: sequence of numbers or C{None}
        """
        from Scientific.IO.NetCDF import NetCDFFile
        self.file = NetCDFFile(filename, 'r')
        self.axes = [self.file.variables[n] for n in axesnames]
        for a in self.axes:
            if len(a.dimensions) != 1:
                raise ValueError("axes must be 1d arrays")
        self.values = self.file.variables[variablename]
        if tuple(v.dimensions[0] for v in self.axes) != self.values.dimensions:
            raise ValueError("axes and values have incompatible dimensions")
        self.default = default
        self.shape = ()
        for axis in self.axes:
            self.shape = self.shape + axis.shape
        if period is None:
            period = len(self.axes) * [None]
        self.period = period
        if len(self.period) != len(self.axes):
            raise ValueError('Inconsistent arguments')
        for a, p in zip(self.axes, self.period):
            if p is not None and a[0] + p <= a[-1]:
                raise ValueError('Period too short')
Exemple #20
0
    def finalize(self):
        """Finalizes the calculations (e.g. averaging the total term, output files creations ...).
        """

        outputFile = NetCDFFile(self.output, 'w')
        outputFile.title = self.__class__.__name__
        outputFile.jobinfo = self.information + '\nOutput file written on: %s\n\n' % asctime(
        )

        outputFile.createDimension('NGROUPS', self.nGroups)
        outputFile.createDimension('NFRAMES', self.nFrames)

        TIMES = outputFile.createVariable('time', N.Float, ('NFRAMES', ))
        TIMES[:] = self.times
        TIMES.units = 'ps'

        GROUPNUMBER = outputFile.createVariable('group_number', N.Int32,
                                                ('NGROUPS', ))
        P2 = outputFile.createVariable('p2', N.Float, ('NGROUPS', 'NFRAMES'))
        P2AVG = outputFile.createVariable('p2-groupavg', N.Float,
                                          ('NFRAMES', ))
        S2 = outputFile.createVariable('s2', N.Float, ('NGROUPS', ))

        p2Avg = N.zeros((self.nFrames), typecode=N.Float)

        comp = 0
        for bKey in sorted(self.bondNames.keys()):

            bName = self.bondNames[bKey]

            GROUPNUMBER[comp] = bName
            S2[comp] = self.S2[bName]
            P2[comp, :] = self.P2[bName]
            N.add(p2Avg, self.P2[bName], p2Avg)
            comp += 1

        P2AVG[:] = p2Avg / float(self.nGroups)

        asciiVar = sorted(outputFile.variables.keys())

        outputFile.close()

        self.toPlot = {'netcdf': self.output, 'xVar': 'pair', 'yVar': 'S2'}

        # Creates an ASCII version of the NetCDF output file.
        convertNetCDFToASCII(inputFile = self.output,\
                             outputFile = os.path.splitext(self.output)[0] + '.cdl',\
                             variables = asciiVar)
Exemple #21
0
def read_dem(filename):
    ncf = NetCDFFile(filename, 'r')
    # Set ANUGA file - UTM grid params from netcdf header
    if hasattr(ncf, 'cellsize'):
        cellsz = ncf.cellsize[0]
        nrows = ncf.nrows[0]
        ncols = ncf.ncols[0]
        xll = ncf.xllcorner[0]  # Easting of lower left corner
        yll = ncf.yllcorner[0]  # Northing of lower left corner
        xur = xll + (ncols - 1) * cellsz
        yur = yll + (nrows - 1) * cellsz
        x = np.linspace(xll, xur, ncols)
        y = np.linspace(yll, yur, ncols)
        zone = ncf.zone[0]
        zone = 51
        zdat = np.flipud(ncf.variables['elevation'][:])
    # Made from GMT?
    elif 'x_range' in ncf.variables:
        xrng = ncf.variables['x_range']
        yrng = ncf.variables['y_range']
        zrng = ncf.variables['z_range']
        dxdy = ncf.variables['spacing']
        lnll = xrng[0]
        lnur = xrng[1]
        ltll = yrng[0]
        ltur = yrng[1]
        # Pixel registration
        if ncf.variables['z'].node_offset[0] == 1:
            lnll += 0.5 * dxdy[0]
            lnur -= 0.5 * dxdy[0]
            ltll += 0.5 * dxdy[1]
            ltur -= 0.5 * dxdy[1]
        nx = 1 + int(0.1 + (lnur - lnll) / dxdy[0])
        ny = 1 + int(0.1 + (ltur - ltll) / dxdy[1])
        zdat = ncf.variables['z'][:].reshape(ny, nx)
        x = np.linspace(lnll, lnur, nx)
        y = np.linspace(ltur, ltll, ny)
    elif ncf.Conventions == 'COARDS/CF-1.0':
        x = ncf.variables['x'][:]
        y = ncf.variables['y'][:]
        zdat = ncf.variables['z'][:]
    else:
        print 'Not GDAL/GMT netcdf file: %s' % filename
        return (-1)
    return (x, y, zdat)
Exemple #22
0
def saveNetCDF(data, filename, title):

    file = NetCDFFile(filename, 'w', 'Created ')
    file.title = title
    file.createDimension('TIME', len(data[2]))
    file.createDimension('LENGTH', len(data[1]))
    SF = file.createVariable('SF', N.Float, ('LENGTH', 'TIME'))
    time = file.createVariable('time', N.Float, ('TIME', ))
    qlenght = file.createVariable('qlenght', N.Float, ('LENGTH', ))

    for i in range(len(data[0])):
        for j in range(len(data[1])):
            SF[j, i] = data[2][i][j]
    for i in range(len(data[0])):
        time[i] = data[0][i]
    for i in range(len(data[1])):
        qlenght[i] = data[1][i]
    file.close()
Exemple #23
0
    def finalize(self):
        """Finalizes the calculations (e.g. averaging the total term, output files creations ...).
        """

        # The NetCDF output file is opened for writing.
        outputFile = NetCDFFile(self.output, 'w')
        outputFile.title = self.__class__.__name__
        outputFile.jobinfo = self.information + '\nOutput file written on: %s\n\n' % asctime(
        )

        outputFile.createDimension('NFRAMES', self.nFrames)
        TIMES = outputFile.createVariable('time', N.Float, ('NFRAMES', ))
        TIMES[:] = self.times[:]
        TIMES.units = 'ps'

        for oName, seq in self.sequence.items():
            outputFile.createDimension('SEQ%s' % oName, len(seq))

            SEQUENCE = outputFile.createVariable('%s_sequence' % oName,
                                                 N.Int32, ('SEQ%s' % oName, ))
            SEQUENCE[:] = N.array(seq)
            SEQUENCE.units = 'unitless'

            S2 = outputFile.createVariable('%s_s2' % oName, N.Float,
                                           ('SEQ%s' % oName, 'NFRAMES'))
            S2[:] = self.S2[oName][:, :]
            S2.units = 'unitless'

            S2AVG = outputFile.createVariable('%s_s2_timeavg' % oName, N.Float,
                                              ('SEQ%s' % oName, ))
            S2AVG[:] = self.S2[oName][:, :].sum(1) / float(self.nFrames)
            S2AVG.units = 'unitless'

        asciiVar = sorted(outputFile.variables.keys())

        outputFile.close()

        self.toPlot = None

        # Creates an ASCII version of the NetCDF output file.
        convertNetCDFToASCII(inputFile = self.output,\
                             outputFile = os.path.splitext(self.output)[0] + '.cdl',\
                             variables = asciiVar)
Exemple #24
0
    def __init__(self, master, filename):
        Frame.__init__(self, master)

        file = NetCDFFile(filename)
        self.q = file.variables['q'][1:]
        self.t = file.variables['time'][:]
        self.fn = file.variables['sf'][1:, :]

        Label(self, text='S(t) for various q').pack(side=TOP, fill=X)
        self.plot1 = PlotCanvas(self, 600, 250, zoom=1)
        self.plot1.pack(side=TOP, fill=BOTH, expand=YES)
        Label(self, text='S(q) for various t').pack(side=TOP, fill=X)
        self.plot2 = PlotCanvas(self, 600, 250, zoom=1)
        self.plot2.pack(side=TOP, fill=BOTH, expand=YES)
        frame = Frame(self)
        frame.pack(side=TOP, fill=X)
        self.first_q = IntEntry(frame, "q range:  from ", 0, 0, len(self.q))
        self.first_q.grid(row=0, column=0)
        self.first_q.bind('<Return>', self.draw)
        self.last_q = IntEntry(frame, " to ", len(self.q), 0, len(self.q))
        self.last_q.grid(row=0, column=1)
        self.skip_q = IntEntry(frame, " skip ", (len(self.q) + 10) / 10, 1,
                               len(self.q))
        self.skip_q.grid(row=0, column=2)
        self.first_t = IntEntry(frame, "t range:  from ", 0, 0, len(self.t))
        self.first_t.grid(row=1, column=0)
        self.last_t = IntEntry(frame, " to ", len(self.t), 0, len(self.t))
        self.last_t.grid(row=1, column=1)
        self.skip_t = IntEntry(frame, " skip ", (len(self.t) + 10) / 10, 1,
                               len(self.t))
        self.skip_t.grid(row=1, column=2)

        self.first_q.bind('<Return>', self.draw)
        self.last_q.bind('<Return>', self.draw)
        self.skip_q.bind('<Return>', self.draw)
        self.first_t.bind('<Return>', self.draw)
        self.last_t.bind('<Return>', self.draw)
        self.skip_t.bind('<Return>', self.draw)

        self.draw()
Exemple #25
0
def write_elevation_nc(file_out, lon, lat, depth_vector):
    """Write an nc elevation file."""

    # NetCDF file definition
    outfile = NetCDFFile(file_out, netcdf_mode_w)

    #Create new file
    nc_lon_lat_header(outfile, lon, lat)

    # ELEVATION
    zname = 'ELEVATION'
    outfile.createVariable(zname, precision, (lat_name, lon_name))
    outfile.variables[zname].units = 'CENTIMETERS'
    outfile.variables[zname].missing_value = -1.e+034

    outfile.variables[lon_name][:] = ensure_numeric(lon)
    outfile.variables[lat_name][:] = ensure_numeric(lat)

    depth = num.reshape(depth_vector, (len(lat), len(lon)))
    outfile.variables[zname][:] = depth

    outfile.close()
    def openNetCDFFile(self, event=None):
        """
        This method opens a NetCDF file and updates the dialog with the data read from that file.
        Arguments:
            -event: Tkinter event.
        """

        self.variablesInfo.text.config(state=NORMAL)
        self.variablesInfo.text.delete('2.0', END)
        self.variablesInfo.text.config(state=DISABLED)

        # Case where the user enters a file name directly in the entry widget without using the browser.
        if event is not None:
            if event.widget == self.inputFileBrowser.entry:
                filename = self.inputFileBrowser.getValue()
            else:
                return

        else:

            # The name of the NetCDF file to load.
            filename = askopenfilename(parent = self,\
                                       filetypes = [('NetCDF file','*.nc')],\
                                       initialdir = PREFERENCES['outputfile_path'])

        # The file must exist otherwise do nothing.
        if filename:
            self.netcdf = NetCDFFile(filename, 'r')

            self.displayNetCDFContents()

            # The filebrowser entry is updated with the loaded filename.
            self.inputFileBrowser.setValue(filename)

            # A default filename for the output ASCII file is built.
            self.outputFileBrowser.setValue(
                os.path.splitext(filename)[0] + '.cdl')

        return 'break'
Exemple #27
0
def NetCDFFile(file_name, netcdf_mode=netcdf_mode_r):
    """Wrapper to isolate changes of the netcdf libray.

    In theory we should be able to change over to NetCDF4 via this
    wrapper, by ensuring the interface to the NetCDF library isthe same as the
    the old Scientific.IO.NetCDF library.

    There is a difference between extracting dimensions. We have used the following
    to cover netcdf4 and scientific python

    try: # works with netcdf4
        number_of_timesteps = len(fid.dimensions['number_of_timesteps'])
        number_of_points = len(fid.dimensions['number_of_points'])
    except: # works with Scientific.IO.NetCDF
        number_of_timesteps = fid.dimensions['number_of_timesteps']
        number_of_points = fid.dimensions['number_of_points']
    
    """
   
    using_scientific = using_netcdf4 = False
    
    try:
        from netCDF4 import Dataset
        using_netcdf4 = True
    except: 
        from Scientific.IO.NetCDF import NetCDFFile
        using_scientific = True

    assert using_scientific or using_netcdf4

    if using_scientific:
        return NetCDFFile(file_name, netcdf_mode)

    if using_netcdf4:
        if netcdf_mode == 'wl' :
            return Dataset(file_name, 'w', format='NETCDF3_64BIT')
        else:
            return Dataset(file_name, netcdf_mode, format='NETCDF3_64BIT')
 def __parinit__(self, pid, nprocs, filename, split_dimension,
                 mode = 'r', local_access = 0):
     if mode != 'r': local_access = 0
     self.pid = pid
     self.nprocs = nprocs
     self.filename = filename
     self.split = split_dimension
     self.local_access = local_access
     self.read_only = mode == 'r'
     if local_access or pid == 0:
         self.file = NetCDFFile(filename, mode)
         try:
             length = self.file.dimensions[split_dimension]
             if length is None:
                 length = -1
         except KeyError:
             length = None
         vars = {}
         for name, var in self.file.variables.items():
             vars[name] = (name, var.dimensions)
             if length < 0 and split_dimension in var.dimensions:
                 index = list(var.dimensions).index(split_dimension)
                 length = var.shape[index]
     else:
         self.file = None
         self.split = split_dimension
         length = None
         vars = None
     if not local_access:
         length = self.broadcast(length)
         vars = self.broadcast(vars)
     if length is not None:
         self._divideData(length)
     self.variables = {}
     for name, var in vars.items():
         self.variables[name] = _ParNetCDFVariable(self, var[0], var[1],
                                                   split_dimension)
Exemple #29
0
    def write_netcdf(self,filename,rlon,rlat,z,title=None):
	#
	from Scientific.IO.NetCDF import NetCDFFile
	#nc = Dataset(filename,'w',format='NETCDF3_CLASSIC')
	print 'netcdf filename: ', filename
	print rlon[0], rlon[-1], rlat[0], rlat[-1], z.min(), z.max()
	print len(rlon), len(rlat), z.shape
	#nc = netcdf.netcdf_file(filename,'w')
	nc = NetCDFFile(filename,'w')
	if title is None:
	    title=''
	nc.title = title
	nc.source = ''
	nc.createDimension('side',2)
	nc.createDimension('xysize',len(rlon)*len(rlat))
	y_range = nc.createVariable('y_range','d', ('side',))
	y_range.units = 'y'
	y_range[:] = [rlat[0],rlat[-1]]
	x_range = nc.createVariable('x_range','d', ('side',))
	x_range.units = 'x'
	x_range[:] = [rlon[0],rlon[-1]]
	z_range = nc.createVariable('z_range','d', ('side',))
	z_range.units = 'z'
	z_range[:] = [z.min(),z.max()]
	spacing = nc.createVariable('spacing','d',('side',))
	spacing[:] = [rlon[1]-rlon[0],rlat[1]-rlat[0]]
	dimension = nc.createVariable('dimension','i',('side',))
	dimension[:] = [len(rlon),len(rlat)]
	grid_data = nc.createVariable('z','f', ('xysize',))
	grid_data.scale_factor = np.array([1.])
	grid_data.add_offset = np.array([0.])
	grid_data.node_offset = np.array([0])
	q = np.flipud(z)
	q = q.flatten()
	grid_data[:] = q.astype('float32')
	nc.close()
Exemple #30
0
 def test(self):
     # Create file
     FileName = '%s_out.nc' % self.Case
     print 'creating %s ...' % FileName
     File = NetCDFFile(FileName,'w')
     File.createDimension('time',None)
     var = File.createVariable('time','f',('time',))
     var.long_name = 'time'
     var.units = ' '
     File.createDimension('lat',len(self.data.lat))
     var = File.createVariable('lat','f',('lat',))
     var.long_name = 'latitude'
     var.units = 'degrees_north'
     var[:] = self.data.lat.astype('f')
     File.createDimension('lon',len(self.data.lon))
     var = File.createVariable('lon','f',('lon',))
     var.long_name = 'longitude'
     var.units = 'degrees_east'
     var[:] = self.data.lon.astype('f')
     for Field in ['SwToa','LwToa','SwToaCf','LwToaCf']:
         var = File.createVariable(Field,'f',('time','lat','lon'))
         var.long_name = ''
         var.units = 'W m-2'
     lmax = 3
     for l in range(lmax):
         print 'doing %s of %s' % (l+1,lmax)
         # get data
         Data = self.getFields(l)[0]
         Data.update(self.Fixed)
         # compute
         self.r(**Data)
         File.variables['SwToa'][l] = self.r['SwToa'].astype('f')
         File.variables['LwToa'][l] = -self.r['LwToa'].astype('f')
         File.variables['SwToaCf'][l] = self.r['SwToaCf'].astype('f')
         File.variables['LwToaCf'][l] = self.r['LwToaCf'].astype('f')
     File.close()