Exemplo n.º 1
0
def save_2D(filenameout,var,lat,lon,miss_val):
    import numpy as np
    from scipy.io.netcdf import netcdf_file as NetCDFFile

    n = NetCDFFile(filenameout, 'w',) # open it for writing 
    n.title = 'saved netcdf variable'

    n.createDimension('lat',len(lat))
    n.createDimension('lon',len(lon))

    latitude = n.createVariable('lat','f',('lat',))
    latitude.longname = 'latitude'
    latitude.units = 'degrees_north'
    latitude[:] = lat.astype(np.float32)

    longitude = n.createVariable('lon','f',('lon',))
    longitude.longname = 'longitude'
    longitude.units = 'degrees_east'
    longitude[:] = lon[:].astype(np.float32)

    varnc = n.createVariable('varnc','f',('lat','lon',))
    varnc.missing_value = np.array(miss_val,np.float32)
    varnc.FillValue = np.array(miss_val,np.float32)

    varnc[:,:]=var[:,:].astype(np.float32)

    n.close()
Exemplo n.º 2
0
def save_3D_stack(filenameout, var, varname, lat, lon, miss_val, time_it):
    import numpy as np
    from scipy.io.netcdf import netcdf_file as NetCDFFile
    """
    savenetcdf_3D(filenameout,var,varname,lat,lon,miss_val,time_it)
    save a 3D (time,lat,lon) numpy array into netcdf
    arguments:
    filenameout: string 
    var: np.array
    varname: string
    lat: np.array 
    lon: np.array
    miss_val: real
    time_it: np.array (dtype=int)
    """

    n = NetCDFFile(
        filenameout,
        'w',
    )  # open it for writing
    n.title = 'saved netcdf variable'

    n.createDimension('time', None)
    n.createDimension('lat', len(lat))
    n.createDimension('lon', len(lon))

    latitude = n.createVariable('lat', 'f', ('lat', ))
    latitude.longname = 'latitude'
    latitude.units = 'degrees_north'
    latitude[:] = lat[:].astype(np.float32)

    longitude = n.createVariable('lon', 'f', ('lon', ))
    longitude.longname = 'longitude'
    longitude.units = 'degrees_east'
    longitude[:] = lon[:].astype(np.float32)

    time = n.createVariable('time', 'i', ('time', ))
    time.units = 'days since 1979-1-1 00:00:0.0'
    time.delta_t = '0000-00-01 00:00:00'

    varnc = n.createVariable(varname, 'f', (
        'time',
        'lat',
        'lon',
    ))
    varnc.missing_value = np.array(miss_val, np.float32)
    varnc._FillValue = np.array(miss_val, np.float32)

    for l in range(0, var.shape[0]):
        time[l] = time_it
        varnc[l, :, :] = var[l, :, :].astype(np.float32)

    n.close()
Exemplo n.º 3
0
Arquivo: save_3D.py Projeto: SPREP/ICU
def save_3D(filenameout,var,varname,lat,lon,miss_val,time_it):
    import numpy as np
    from scipy.io.netcdf import netcdf_file as NetCDFFile
    """
    savenetcdf_3D(filenameout,var,varname,lat,lon,miss_val,time_it)
    save a 3D (time,lat,lon) numpy array into netcdf
    arguments:
    filenameout: string 
    var: np.array
    varname: string
    lat: np.array 
    lon: np.array
    miss_val: real
    time_it: np.array (dtype=int)
    """

    n = NetCDFFile(filenameout, 'w',) # open it for writing 
    n.title = 'saved netcdf variable'

    n.createDimension('time',None)
    n.createDimension('lat',len(lat))
    n.createDimension('lon',len(lon))

    latitude = n.createVariable('lat','f',('lat',))
    latitude.longname = 'latitude'
    latitude.units = 'degrees_north'
    latitude[:] = lat[:].astype(np.float32)

    longitude = n.createVariable('lon','f',('lon',))
    longitude.longname = 'longitude'
    longitude.units = 'degrees_east'
    longitude[:] = lon[:].astype(np.float32)

    time =  n.createVariable('time','i',('time',))
    time.units = 'days since 1979-1-1 00:00:0.0'
    time.delta_t = '0000-00-01 00:00:00'

    varnc = n.createVariable(varname,'f',('time','lat','lon',))
    varnc.missing_value = np.array(miss_val,np.float32)
    varnc._FillValue = np.array(miss_val,np.float32)

    for l in range(0,var.shape[0]):
        time[l]=time_it[l,]
        varnc[l,:,:]=var[l,:,:].astype(np.float32)

    n.close()
Exemplo n.º 4
0
 def writeToNc(self, ncFileName):
     ncFile = NetCDFFile(ncFileName, 'w')
     ncFile.createDimension("nrows", self.RaCoords.shape[0])
     ncFile.createDimension("ncols", self.DecCoords.shape[0])
     dims = ("nrows", "ncols")
     drow = ("nrows", )
     dcol = ("ncols", )
     ncFile.createVariable("signal", "d", dims)
     ncFile.createVariable("weight", "d", dims)
     ncFile.createVariable("kernel", "d", dims)
     ncFile.createVariable("rowCoordsPhys", "d", drow)
     ncFile.createVariable("colCoordsPhys", "d", dcol)
     ncFile.createVariable("xCoordsAbs", "d", dims)
     ncFile.createVariable("yCoordsAbs", "d", dims)
     ncFile.createVariable("filteredSignal", "d", dims)
     ncFile.createVariable("filteredWeight", "d", dims)
     ncFile.createVariable("filteredKernel", "d", dims)
     setattr(ncFile, "source", "%s" % self.source)
     setattr(ncFile, "MasterGrid[0]", self.SourceRa / 180.0 * (npy.pi))
     setattr(ncFile, "MasterGrid[1]", self.SourceDec / 180.0 * (npy.pi))
     ncFile.variables['signal'][:] = self.signal.value
     ncFile.variables['weight'][:] = self.weight.value
     ncFile.variables['rowCoordsPhys'][:] = self.RaCoords / 180.0 * (npy.pi)
     ncFile.variables['colCoordsPhys'][:] = self.DecCoords / 180.0 * (
         npy.pi)
     ncFile.variables['kernel'][:] = self.kernel
     ncFile.variables['xCoordsAbs'][:] = self.AbsRaCoords.to("rad").value
     ncFile.variables['yCoordsAbs'][:] = self.AbsDecCoords.to("rad").value
     if self.filtered:
         ncFile.variables['filteredSignal'][:] = self.fSignal.value
         ncFile.variables['filteredWeight'][:] = self.fWeight.value
         ncFile.variables['filteredKernel'][:] = self.fKernel
     else:
         ncFile.variables['filteredSignal'][:] = self.signal.value
         ncFile.variables['filteredWeight'][:] = self.weight.value
         ncFile.variables['filteredKernel'][:] = self.kernel
     ncFile.sync()
     ncFile.close()