예제 #1
0
def is_valid_nc(filename):
    """ Return True if the file is a valid NetCDF file """
    try:
        file = netcdf(filename, 'r')
        file.close()
        return True
    except Exception:
        return False
예제 #2
0
def getSlabDepth(x, y, utm=True):
    '''
    Get the slab depth for any point
    Args:   
        * x, y : coordinates of the point(s) you want to have the depth
        * [OPT] utm: if True (default), give UTM coordinates. Else lon/lat
    return:
        * z : depth of the slab    
    '''

    # import some stuffs
    from scipy.interpolate import interp2d
    from netCDF4 import Dataset as netcdf

    # Read slab
    fid = os.path.join(os.environ['DATA'], 'SLAB', 'SlabMrCrory.grd')
    fin = netcdf(fid, 'r', format='NETCDF4')
    los = fin.variables['x'][:]
    las = fin.variables['y'][:]
    zs = fin.variables['z'][:]
    zs = np.ma.filled(zs, fill_value=0)

    #mask = ~zs.mask.flatten()

    # Get dimensions
    nx = len(los)
    ny = len(las)

    LO, LA = np.meshgrid(los, las)
    LO = LO.flatten()
    LA = LA.flatten()

    # Create converter from ll to utm
    string = '+proj=utm +lat_0={} +lon_0={} +ellps={}'.format(
        48.5, -123.6, 'WGS84')
    putm = pp.Proj(string)

    # Create interpolator
    #f = interp2d(LO[mask],LA[mask],zs[~zs.mask])
    f = interp2d(los, las, zs)

    # convert to lon lat if necessary
    if utm:
        lo, la = putm(x, y, inverse=True)
        lo, la = np.array((lo)), np.array((la))
    else:
        lo, la = np.array((x)), np.array((y))

    # get depth
    if lo.size == 1:
        z = -1 * f(lo, la)
    else:
        z = np.zeros((len(lo)))
        for i in range(len(lo)):
            z[i] = -1 * f(lo[i], la[i])[0]

    # all done
    return z
예제 #3
0
파일: input.py 프로젝트: moghimis/verif
 def __init__(self, filename):
    self.fullname = filename
    self._filename = os.path.expanduser(filename)
    self._file = netcdf(self._filename, 'r')
    self.times = self._get_times()
    self.leadtimes = self._get_leadtimes()
    self.locations = self._get_locations()
    self.thresholds = self._get_thresholds()
    self.quantiles = self._get_quantiles()
    self.variable = self._get_variable()
예제 #4
0
def get_grd(gname):
    ncgrd = netcdf(gname, mode='r')
    mask = ncgrd.variables['mask_rho'][:]
    lon = ncgrd.variables['lon_rho'][:]
    lat = ncgrd.variables['lat_rho'][:]
    pm = ncgrd.variables['pm'][:]
    pn = ncgrd.variables['pn'][:]
    f = ncgrd.variables['f'][:]
    ncgrd.close()

    return mask, lon, lat, pm, pn, f
예제 #5
0
파일: Input.py 프로젝트: dsiuta/verif
 def isValid(filename):
     try:
         file = netcdf(filename, 'r')
     except:
         return False
     valid = False
     if (hasattr(file, "Conventions")):
         if (file.Conventions == "verif_1.0.0"):
             valid = True
     file.close()
     return valid
예제 #6
0
파일: input.py 프로젝트: moghimis/verif
 def is_valid(filename):
    """ Checks that 'filename' is a valid object of this type """
    valid = True
    try:
       file = netcdf(filename, 'r')
       required_dimensions = ["Offset", "Date", "Location"]
       for dim in required_dimensions:
          valid = valid & (dim in file.dimensions)
       file.close()
       return valid
    except:
       return False
예제 #7
0
 def __init__(self, filename):
    self.fullname = filename
    self._filename = os.path.expanduser(filename)
    self._file = netcdf(self._filename, 'r')
    self.times = self._get_times()
    self.leadtimes = self._get_leadtimes()
    self.locations = self._get_locations()
    self.thresholds = self._get_thresholds()
    self.quantiles = self._get_quantiles()
    self.variable = self._get_variable()
    regular_names = self.get_regular_names() + ["threshold", "cdf", "quantile", "x"]
    self.other_fields = [var for var in self._file.variables if var not in regular_names]
예제 #8
0
파일: input.py 프로젝트: moghimis/verif
   def __init__(self, filename):
      self.fullname = filename
      self._filename = os.path.expanduser(filename)
      self._file = netcdf(self._filename, 'r')

      # Pre-load these variables, to save time when queried repeatedly
      dates = verif.util.clean(self._file.variables["Date"])
      self.times = np.array([verif.util.date_to_unixtime(int(date)) for date in dates], int)
      self.leadtimes = verif.util.clean(self._file.variables["Offset"])
      self.thresholds = self._get_thresholds()
      self.quantiles = self._get_quantiles()
      self.locations = self._get_locations()
      self.variable = self._get_variable()
예제 #9
0
def create_nc_part_2d(nc_name, nq):
    '''
    CREATE OUTPUT FILE 
    '''
    nc = netcdf(nc_name, 'w')
    nc.createDimension('time',0)
    nc.createDimension('nq',nq)
    
    nct  = nc.createVariable('time','d',('time',))
    ncf  = nc.createVariable('frame','i',('time',))
    ncpx = nc.createVariable('px','d',('time','nq'))
    ncpy = nc.createVariable('py','d',('time','nq'))
    nc.close()
예제 #10
0
def fill_cache(ds_var):
    nc = netcdf(f'cru_ts3.24.01.{startyear}.{endyear}.{ds_var}.dat.nc', 'r')
    nc_attrs, nc_dims, nc_vars = ncdump(nc)
    # Extract data from NetCDF file
    lats = nc.variables['lat'][:]  # extract/copy the data
    lons = nc.variables['lon'][:]
    time = nc.variables['time'][:]
    nc_ds = nc.variables[ds_var][:]
    CACHE[ds_var]['lats'] = lats
    CACHE[ds_var]['lons'] = lons
    ts_dt = fix_time(time)
    CACHE[ds_var]['time'] = ts_dt
    CACHE[ds_var]['days'] = time
    CACHE[ds_var]['ds'] = nc_ds
예제 #11
0
def freadcrm():

    crmvars = ['MCUP', 'Q1C', 'Q2']

    crmdata = {}
    fid = netcdf(fcrm, 'r')
    crmdata['lev'] = fid.variables['p'][:] * 100
    for ivar in range(len(crmvars)):
        crmdata[crmvars[ivar]] = fid.variables[crmvars[ivar]][:]

    print "CRM: ", tcrm[0], tcrm[-1]
    fid.close()

    return crmdata
예제 #12
0
def grid_for(bbox):
    """
    Extract a grid of locations in bbox

    :param hdf5: optional hdf5 file.
    :param save: save / update hdf5 data
    :return: None.
    """
    lats = [p[1] for p in bbox]
    lons = [p[0] for p in bbox]

    lat_min = min(lats)
    lon_min = min(lons)

    lat_max = max(lats)
    lon_max = max(lons)

    ds_var = 'tmp'
    nc = netcdf(f'cru_ts3.24.01.{startyear}.{endyear}.{ds_var}.dat.nc', 'r')
    nc_attrs, nc_dims, nc_vars = ncdump(nc)
    lats = nc.variables['lat'][:]  # extract/copy the data
    lons = nc.variables['lon'][:]

    lats = numpy.array(lats)
    lons = numpy.array(lons)

    # invalidate all locations outside our given bbox
    # with 1 extra box spacing
    padding_lat = 0.55
    padding_lon = 0.55
    lats[lats < lat_min - padding_lat] = 0
    lats[lats > lat_max + padding_lat] = 0
    lons[lons < lon_min - padding_lon] = 0
    lons[lons > lon_max + padding_lon] = 0

    grid = []
    grid_idx = []

    for lon_idx, lat in enumerate(lats):
        for lat_idx, lon in enumerate(lons):
            if lat == 0:
                continue
            if lon == 0:
                continue

            grid.append((lon, lat))
            grid_idx.append((lon_idx, lat_idx))

    return grid, grid_idx
예제 #13
0
파일: input.py 프로젝트: moghimis/verif
   def is_valid(filename):
      # First check if the file is a valid Netcdf file
      try:
         file = netcdf(filename, 'r')
         valid = True

         # Check required dimensions
         dims = [dim for dim in file.dimensions]
         required_dims = ["time", "location", "leadtime"]
         for required_dim in required_dims:
            valid = valid and required_dim in dims

         # Check required variables
         required_vars = ["time", "location", "leadtime"]
         vars = [var for var in file.variables]
         for required_var in required_vars:
            valid = valid and required_var in vars

         file.close()
         return valid
      except:
         return False
예제 #14
0
def fncread(fn,var):
    # read a variable from a netcdf file
    fid = netcdf(fn, 'r')
    data = fid.variables[var][:]
    fid.close()
    return data
예제 #15
0
def create_bryfile(bryname,grdname,title,obc,\
                        theta_s,theta_b,hc,N,\
                        bry_time,cycle,vtransform):

#
#
################################################################
#
# function create_bryfile(bryname,grdname,title,obc...
#                          theta_s,theta_b,hc,N,...
#                          bry_time,cycle,clobber)
#
#   This function create the header of a Netcdf climatology 
#   file.
#
#   Input:
#
#   bryname      Netcdf climatology file name (character string).
#   grdname      Netcdf grid file name (character string).
#   obc          open boundaries flag (1=open , [S E N W]).
#   theta_s      S-coordinate surface control parameter.(Real)
#   theta_b      S-coordinate bottom control parameter.(Real)
#   hc           Width (m) of surface or bottom boundary layer 
#                where higher vertical resolution is required 
#                during stretching.(Real)
#   N            Number of vertical levels.(Integer)
#   bry_time     time.(vector)
#   cycle        Length (days) for cycling the climatology.(Real)
#
#
################################################################
#
#

  print(' ')
  print(' Creating the file : '+bryname)
  print(' ')
  print(' VTRANSFORM = '+str(vtransform))

#
#  Read the grid file and check the topography
#

  nc=netcdf(grdname, 'r')
  h=nc.variables['h'][:]
  maskr=nc.variables['mask_rho'][:]
  [Mp,Lp]=np.shape(h)
  nc.close()

#
#
#

  hmin=np.min(h[np.where(maskr==1)])
  if vtransform == 1:
    if hc > hmin:
      print('Error: hc ('+hc+' m) > hmin ('+hmin+' m)')


  L=Lp-1
  M=Mp-1
  Np=N+1

  Nt=0

#
#  Create the boundary file
#
  type = 'BOUNDARY file'
  history = 'CROCO' 

  if os.path.exists(bryname):
    os.remove(bryname)

  print('Create: ' +bryname)
  nc=netcdf(bryname,'w',format='NETCDF4')

#
# set global attributes
#

  nc.type='CROCO boundary file'
  nc.history = 'Created '+str(time.ctime(time.time()))
  nc.title = title
  nc.bry_file = bryname
  nc.grd_file = grdname

#
#  Create dimensions
#

  nc_dim_xi_u=nc.createDimension('xi_u',L)
  nc_dim_xi_v=nc.createDimension('xi_v',Lp)
  nc_dim_xi_rho=nc.createDimension('xi_rho',Lp)
  nc_dim_eta_u=nc.createDimension('eta_u',Mp)
  nc_dim_eta_v=nc.createDimension('eta_v',M)
  nc_dim_eta_rho=nc.createDimension('eta_rho',Mp)
  nc_dim_s_rho=nc.createDimension('s_rho',N)
  nc_dim_s_w=nc.createDimension('s_w',Np)
  nc_dim_tracer=nc.createDimension('tracer',2)
  nc_dim_bry_time=nc.createDimension('bry_time',Nt)
  nc_dim_tclm_time=nc.createDimension('tclm_time',Nt)
  nc_dim_temp_time=nc.createDimension('temp_time',Nt)
  nc_dim_sclm_time=nc.createDimension('sclm_time',Nt)
  nc_dim_salt_time=nc.createDimension('salt_time',Nt)
  nc_dim_uclm_time=nc.createDimension('uclm_time',Nt)
  nc_dim_vclm_time=nc.createDimension('vclm_time',Nt)
  nc_dim_v2d_time=nc.createDimension('v2d_time',Nt)
  nc_dim_v3d_time=nc.createDimension('v3d_time',Nt)
  nc_dim_ssh_time=nc.createDimension('ssh_time',Nt)
  nc_dim_zeta_time=nc.createDimension('zeta_time',Nt)
  nc_dim_one=nc.createDimension('one',1)

#
#  Create variables and attributes
#

  nc_spherical=nc.createVariable('spherical','S1', ('one',))
  nc_spherical.long_name = 'grid type logical switch'
  nc_spherical.flag_values = 'T, F'
  nc_spherical.flag_meanings = 'spherical Cartesian'
#
  nc_Vtransform=nc.createVariable('Vtransform','i4', ('one',))
  nc_Vtransform.long_name = 'vertical terrain-following transformation equation'
#
  nc_Vstretching=nc.createVariable('Vstretching','i4', ('one',))
  nc_Vstretching.long_name = 'vertical terrain-following stretching function'
#
  nc_tstart=nc.createVariable('tstart',np.float64, ('one',))
  nc_tstart.long_name = 'start processing day'
  nc_tstart.units = 'day'
#
  nc_tend=nc.createVariable('tend',np.float64, ('one',))
  nc_tend.long_name = 'end processing day'
  nc_tend.units = 'day'
#
  nc_theta_s=nc.createVariable('theta_s',np.float64, ('one',))
  nc_theta_s.long_name = 'S-coordinate surface control parameter'
  nc_theta_s.units = 'nondimensional'
#
  nc_theta_b=nc.createVariable('theta_b',np.float64, ('one',))
  nc_theta_b.long_name = 'S-coordinate bottom control parameter'
  nc_theta_b.units = 'nondimensional'
#
  nc_Tcline=nc.createVariable('Tcline',np.float64, ('one',))
  nc_Tcline.long_name = 'S-coordinate surface/bottom layer width'
  nc_Tcline.units = 'meter'
#
  nc_hc=nc.createVariable('hc',np.float64, ('one',))
  nc_hc.long_name = 'S-coordinate parameter, critical depth'
  nc_hc.units = 'meter'
#
  nc_sc_r=nc.createVariable('sc_r',np.float64, ('s_rho',))
  nc_sc_r.long_name = 'S-coordinate at RHO-points'
  nc_sc_r.valid_min = -1.
  nc_sc_r.valid_max = 0.
  nc_sc_r.positive = 'up'

  if vtransform == 1:
    nc_sc_r.standard_name = 'ocean_s_coordinate_g1'
  elif vtransform == 2:
    nc_sc_r.standard_name = 'ocean_s_coordinate_g2'     

  nc_sc_r.formula_terms = 's: s_rho C: Cs_r eta: zeta depth: h depth_c: hc'
#
  nc_sc_w=nc.createVariable('sc_w',np.float64, ('s_w',))
  nc_sc_w.long_name = 'S-coordinate at W-points'
  nc_sc_w.valid_min = -1. 
  nc_sc_w.valid_max = 0. 
  nc_sc_w.positive = 'up'

  if vtransform == 1:
    nc_sc_w.standard_name = 'ocean_s_coordinate_g1'
  elif vtransform == 2:
    nc_sc_w.standard_name = 'ocean_s_coordinate_g2'

  nc_sc_w.formula_terms = 's: s_w C: Cs_w eta: zeta depth: h depth_c: hc'
#
  nc_Cs_r=nc.createVariable('Cs_r',np.float64, ('s_rho',))
  nc_Cs_r.long_name = 'S-coordinate stretching curves at RHO-points'
  nc_Cs_r.units = 'nondimensional'
  nc_Cs_r.valid_min = -1
  nc_Cs_r.valid_max = 0
#
  nc_Cs_w=nc.createVariable('Cs_w',np.float64, ('s_w',))
  nc_Cs_w.long_name = 'S-coordinate stretching curves at W-points'
  nc_Cs_w.units = 'nondimensional'
  nc_Cs_w.valid_min = -1
  nc_Cs_w.valid_max = 0
#
  nc_bry_time=nc.createVariable('bry_time',np.float64, ('bry_time',))
  nc_bry_time.long_name = 'time for boundary climatology'
  nc_bry_time.units = 'day'
  nc_bry_time.calendar = 'XXX days in every year'
  nc_bry_time.cycle_length = cycle
#
  nc_tclm_time=nc.createVariable('tclm_time',np.float64, ('tclm_time',))
  nc_tclm_time.long_name = 'time for temperature climatology'
  nc_tclm_time.units = 'day'
  nc_tclm_time.calendar = 'XXX days in every year'
  nc_tclm_time.cycle_length = cycle
#
  nc_temp_time=nc.createVariable('temp_time',np.float64, ('temp_time',))
  nc_temp_time.long_name = 'time for temperature climatology'
  nc_temp_time.units = 'day'
  nc_temp_time.calendar = 'XXX days in every year'
  nc_temp_time.cycle_length = cycle
#
  nc_sclm_time=nc.createVariable('sclm_time',np.float64, ('sclm_time',))
  nc_sclm_time.long_name = 'time for salinity climatology'
  nc_sclm_time.units = 'day'
  nc_sclm_time.calendar = 'XXX days in every year'
  nc_sclm_time.cycle_length = cycle
#
  nc_salt_time=nc.createVariable('salt_time',np.float64, ('salt_time',))
  nc_salt_time.long_name = 'time for salinity climatology'
  nc_salt_time.units = 'day'
  nc_salt_time.calendar = 'XXX days in every year'
  nc_salt_time.cycle_length = cycle
#
  nc_uclm_time=nc.createVariable('uclm_time',np.float64, ('uclm_time',))
  nc_uclm_time.long_name = 'time climatological u'
  nc_uclm_time.units = 'day'
  nc_uclm_time.calendar = 'XXX days in every year'
  nc_uclm_time.cycle_length = cycle
#
  nc_vclm_time=nc.createVariable('vclm_time',np.float64, ('vclm_time',))
  nc_vclm_time.long_name = 'time climatological v'
  nc_vclm_time.units = 'day'
  nc_vclm_time.calendar = 'XXX days in every year'
  nc_vclm_time.cycle_length = cycle
#
  nc_v2d_time=nc.createVariable('v2d_time',np.float64, ('v2d_time',))
  nc_v2d_time.long_name = 'time for 2D velocity climatology'
  nc_v2d_time.units = 'day'
  nc_v2d_time.calendar = 'XXX days in every year'
  nc_v2d_time.cycle_length = cycle
#
  nc_v3d_time=nc.createVariable('v3d_time',np.float64, ('v3d_time',))
  nc_v3d_time.long_name = 'time for 3D velocity climatology'
  nc_v3d_time.units = 'day'
  nc_v3d_time.calendar = 'XXX days in every year'
  nc_v3d_time.cycle_length = cycle
#
  nc_ssh_time=nc.createVariable('ssh_time',np.float64, ('ssh_time',))
  nc_ssh_time.long_name = 'time for sea surface height'
  nc_ssh_time.units = 'day'
  nc_ssh_time.calendar = 'XXX days in every year'
  nc_ssh_time.cycle_length = cycle
#
  nc_zeta_time=nc.createVariable('zeta_time',np.float64, ('zeta_time',))
  nc_zeta_time.long_name = 'time for sea surface height'
  nc_zeta_time.units = 'day'
  nc_zeta_time.calendar = 'XXX days in every year'
  nc_zeta_time.cycle_length = cycle
#
  if obc[0]==1:
#
#   Southern boundary
#
    nc_temp_south=nc.createVariable('temp_south',np.float64, ('temp_time','s_rho','xi_rho',))
    nc_temp_south.long_name = 'southern boundary potential temperature'
    nc_temp_south.units = 'Celsius'
    nc_temp_south.coordinates = 'lon_rho s_rho temp_time'
#
    nc_salt_south=nc.createVariable('salt_south',np.float64, ('salt_time','s_rho','xi_rho',))
    nc_salt_south.long_name = 'southern boundary salinity'
    nc_salt_south.units = 'PSU'
    nc_salt_south.coordinates = 'lon_rho s_rho salt_time'
#
    nc_u_south=nc.createVariable('u_south',np.float64, ('v3d_time','s_rho','xi_u',))
    nc_u_south.long_name = 'southern boundary u-momentum component'
    nc_u_south.units = 'meter second-1'
    nc_u_south.coordinates = 'lon_u s_rho u_bry_time'
#
    nc_v_south=nc.createVariable('v_south',np.float64, ('v3d_time','s_rho','xi_rho',))
    nc_v_south.long_name = 'southern boundary v-momentum component'
    nc_v_south.units = 'meter second-1'
    nc_v_south.coordinates = 'lon_v s_rho vclm_time'
#
    nc_ubar_south=nc.createVariable('ubar_south',np.float64, ('v2d_time','xi_u',))
    nc_ubar_south.long_name = 'southern boundary vertically integrated u-momentum component'
    nc_ubar_south.units = 'meter second-1'
    nc_ubar_south.coordinates = 'lon_u uclm_time'
#
    nc_vbar_south=nc.createVariable('vbar_south',np.float64, ('v2d_time','xi_rho',))
    nc_vbar_south.long_name = 'southern boundary vertically integrated v-momentum component'
    nc_vbar_south.units = 'meter second-1'
    nc_vbar_south.coordinates = 'lon_v vclm_time'
#
    nc_zeta_south=nc.createVariable('zeta_south',np.float64, ('zeta_time','xi_rho',))
    nc_zeta_south.long_name = 'southern boundary sea surface height'
    nc_zeta_south.units = 'meter'
    nc_zeta_south.coordinates = 'lon_rho zeta_time'
#

  if obc[1]==1:
#
#   Eastern boundary
#
    nc_temp_east=nc.createVariable('temp_east',np.float64, ('temp_time','s_rho','eta_rho',))
    nc_temp_east.long_name = 'eastern boundary potential temperature'
    nc_temp_east.units = 'Celsius'
    nc_temp_east.coordinates = 'lat_rho s_rho temp_time'
#
    nc_salt_east=nc.createVariable('salt_east',np.float64, ('salt_time','s_rho','eta_rho',))
    nc_salt_east.long_name = 'eastern boundary salinity'
    nc_salt_east.units = 'PSU'
    nc_salt_east.coordinates = 'lat_rho s_rho salt_time'
#
    nc_u_east=nc.createVariable('u_east',np.float64, ('v3d_time','s_rho','eta_rho',))
    nc_u_east.long_name = 'eastern boundary u-momentum component'
    nc_u_east.units = 'meter second-1'
    nc_u_east.coordinates = 'lat_u s_rho u_bry_time'
#
    nc_v_east=nc.createVariable('v_east',np.float64, ('v3d_time','s_rho','eta_v',))
    nc_v_east.long_name = 'eastern boundary v-momentum component'
    nc_v_east.units = 'meter second-1'
    nc_v_east.coordinates = 'lat_v s_rho vclm_time'
#
    nc_ubar_east=nc.createVariable('ubar_east',np.float64, ('v2d_time','eta_rho',))
    nc_ubar_east.long_name = 'eastern boundary vertically integrated u-momentum component'
    nc_ubar_east.units = 'meter second-1'
    nc_ubar_east.coordinates = 'lat_u uclm_time'
#
    nc_vbar_east=nc.createVariable('vbar_east',np.float64, ('v2d_time','eta_v',))
    nc_vbar_east.long_name = 'eastern boundary vertically integrated v-momentum component'
    nc_vbar_east.units = 'meter second-1'
    nc_vbar_east.coordinates = 'lat_v vclm_time'
#
    nc_zeta_east=nc.createVariable('zeta_east',np.float64, ('zeta_time','eta_rho',))
    nc_zeta_east.long_name = 'eastern boundary sea surface height'
    nc_zeta_east.units = 'meter'
    nc_zeta_east.coordinates = 'lat_rho zeta_time'
#

  if obc[2]==1:
#
#   Northern boundary
#
    nc_temp_north=nc.createVariable('temp_north',np.float64, ('temp_time','s_rho','xi_rho',))
    nc_temp_north.long_name = 'northern boundary potential temperature'
    nc_temp_north.units = 'Celsius'
    nc_temp_north.coordinates = 'lon_rho s_rho temp_time'
#
    nc_salt_north=nc.createVariable('salt_north',np.float64, ('salt_time','s_rho','xi_rho',))
    nc_salt_north.long_name = 'northern boundary salinity'
    nc_salt_north.units = 'PSU'
    nc_salt_north.coordinates = 'lon_rho s_rho salt_time'
#
    nc_u_north=nc.createVariable('u_north',np.float64, ('v3d_time','s_rho','xi_u',))
    nc_u_north.long_name = 'northern boundary u-momentum component'
    nc_u_north.units = 'meter second-1'
    nc_u_north.coordinates = 'lon_u s_rho u_bry_time'
#
    nc_v_north=nc.createVariable('v_north',np.float64, ('v3d_time','s_rho','xi_rho',))
    nc_v_north.long_name = 'northern boundary v-momentum component'
    nc_v_north.units = 'meter second-1'
    nc_v_north.coordinates = 'lon_v s_rho vclm_time'
#
    nc_ubar_north=nc.createVariable('ubar_north',np.float64, ('v2d_time','xi_u',))
    nc_ubar_north.long_name = 'northern boundary vertically integrated u-momentum component'
    nc_ubar_north.units = 'meter second-1'
    nc_ubar_north.coordinates = 'lon_u uclm_time'
#
    nc_vbar_north=nc.createVariable('vbar_north',np.float64, ('v2d_time','xi_rho',))
    nc_vbar_north.long_name = 'northern boundary vertically integrated v-momentum component'
    nc_vbar_north.units = 'meter second-1'
    nc_vbar_north.coordinates = 'lon_v vclm_time'

    nc_zeta_north=nc.createVariable('zeta_north',np.float64, ('zeta_time','xi_rho',))
    nc_zeta_north.long_name = 'northern boundary sea surface height'
    nc_zeta_north.units = 'meter'
    nc_zeta_north.coordinates = 'lon_rho zeta_time'
#

  if obc[3]==1:
#
#   Western boundary
#
    nc_temp_west=nc.createVariable('temp_west',np.float64, ('temp_time','s_rho','eta_rho',))
    nc_temp_west.long_name = 'western boundary potential temperature'
    nc_temp_west.units = 'Celsius'
    nc_temp_west.coordinates = 'lat_rho s_rho temp_time'
#
    nc_salt_west=nc.createVariable('salt_west',np.float64, ('salt_time','s_rho','eta_rho',))
    nc_salt_west.long_name = 'western boundary salinity'
    nc_salt_west.units = 'PSU'
    nc_salt_west.coordinates = 'lat_rho s_rho salt_time'
#
    nc_u_west=nc.createVariable('u_west',np.float64, ('v3d_time','s_rho','eta_rho',))
    nc_u_west.long_name = 'western boundary u-momentum component'
    nc_u_west.units = 'meter second-1'
    nc_u_west.coordinates = 'lat_u s_rho u_bry_time'
#
    nc_v_west=nc.createVariable('v_west',np.float64, ('v3d_time','s_rho','eta_v',))
    nc_v_west.long_name = 'western boundary v-momentum component'
    nc_v_west.units = 'meter second-1'
    nc_v_west.coordinates = 'lat_v s_rho vclm_time'
#
    nc_ubar_west=nc.createVariable('ubar_west',np.float64, ('v2d_time','eta_rho',))
    nc_ubar_west.long_name = 'western boundary vertically integrated u-momentum component'
    nc_ubar_west.units = 'meter second-1'
    nc_ubar_west.coordinates = 'lat_u uclm_time'
#
    nc_vbar_west=nc.createVariable('vbar_west',np.float64, ('v2d_time','eta_v',))
    nc_vbar_west.long_name = 'western boundary vertically integrated v-momentum component'
    nc_vbar_west.units = 'meter second-1'
    nc_vbar_west.coordinates = 'lat_v vclm_time'
#
    nc_zeta_west=nc.createVariable('zeta_west',np.float64, ('zeta_time','eta_rho',))
    nc_zeta_west.long_name = 'western boundary sea surface height'
    nc_zeta_west.units = 'meter'
    nc_zeta_west.coordinates = 'lat_rho zeta_time'
#

#
# Compute S coordinates
#

  (sc_r,Cs_r,sc_w,Cs_w) = vgrd.scoordinate(theta_s,theta_b,N,hc,vtransform)
  print('vtransform = '+str(vtransform))

#
# Write variables
#
  nc_spherical[:]='T'
  nc_Vtransform[:]=vtransform
  nc_Vstretching[:]=1
  nc_tstart[:] =   np.min(bry_time) 
  nc_tend[:] =     np.max(bry_time) 
  nc_theta_s[:] =  theta_s 
  nc_theta_b[:] =  theta_b 
  nc_Tcline[:] =  hc 
  nc_hc[:] =  hc 
  nc_sc_r[:] = sc_r
  nc_sc_w[:] = sc_w
  nc_Cs_r[:] = Cs_r  
  nc_Cs_w[:] = Cs_w
  nc_tclm_time[:] =  bry_time 
  nc_temp_time[:] =  bry_time 
  nc_sclm_time[:] =  bry_time 
  nc_salt_time[:] =  bry_time 
  nc_uclm_time[:] =  bry_time 
  nc_vclm_time[:] =  bry_time 
  nc_v2d_time[:]  =  bry_time 
  nc_v3d_time[:]  =  bry_time 
  nc_ssh_time[:]  =  bry_time
  nc_zeta_time[:] =  bry_time
  nc_bry_time[:]  =  bry_time 

  if obc[0]==1:
    nc_u_south[:] =  0. 
    nc_v_south[:] =  0. 
    nc_ubar_south[:] =  0. 
    nc_vbar_south[:] =  0. 
    nc_zeta_south[:] =  0. 
    nc_temp_south[:] =  0. 
    nc_salt_south[:] =  0.

  if obc[1]==1:
    nc_u_east[:] =  0. 
    nc_v_east[:] =  0. 
    nc_ubar_east[:] =  0. 
    nc_vbar_east[:] =  0. 
    nc_zeta_east[:] =  0. 
    nc_temp_east[:] =  0. 
    nc_salt_east[:] =  0.

  if obc[2]==1:
    nc_u_north[:] =  0. 
    nc_v_north[:] =  0. 
    nc_ubar_north[:] =  0. 
    nc_vbar_north[:] =  0. 
    nc_zeta_north[:] =  0. 
    nc_temp_north[:] =  0. 
    nc_salt_north[:] =  0.

  if obc[3]==1:
    nc_u_west[:] =  0. 
    nc_v_west[:] =  0. 
    nc_ubar_west[:] =  0. 
    nc_vbar_west[:] =  0. 
    nc_zeta_west[:] =  0. 
    nc_temp_west[:] =  0. 
    nc_salt_west[:] =  0.
  
  nc.close()

  return
예제 #16
0
  print(' ')
  print(' Title: '+title)

#
# Create the CROCO boundary file
#

  glor.create_bryfile(bryname,grdname,title,obc,\
                        theta_s,theta_b,hc,N,\
                        time_bry,cycle_bry,vtransform)

#
# get the CROCO grid
#

  ncg     = netcdf(grdname,'r')
  ncgrd   = ncg.variables
  lon_rho = np.array(ncgrd['lon_rho'][:])
  lat_rho = np.array(ncgrd['lat_rho'][:])
  lon_u   = np.array(ncgrd['lon_u'][:])
  lat_u   = np.array(ncgrd['lat_u'][:])
  lon_v   = np.array(ncgrd['lon_v'][:])
  lat_v   = np.array(ncgrd['lat_v'][:])
  h       = np.array(ncgrd['h'][:])
  mask    = np.array(ncgrd['mask_rho'][:])
  angle   = np.array(ncgrd['angle'][:])
  [M,L]   = np.shape(lon_rho)
  ncg.close()

#
# Open the CROCO boundary file for writing
예제 #17
0
        time[1][1], '02') + '-' + format(time[1][2], '02') + '-' + format(
            time[1][3], '02')

print tstr

t0 = time[0][0] * 86400 + time[0][1] * 3600 + time[0][2] * 60 + time[0][3]
t1 = time[-1][0] * 86400 + time[-1][1] * 3600 + time[-1][2] * 60 + time[-1][3]
pics = [np.zeros([nlat, nlon]) for i in range(len(vars))]
units = ['' for i in range(len(vars))]
names = ['' for i in range(len(vars))]

files = glob.glob(pdata + '*_d*.nc')
files.sort(key=os.path.basename)
nfile = 0
for i in range(len(files)):
    fid = netcdf(files[i], 'r')
    t = fid.variables['time'][:]
    if (t >= t0 and t <= t1):
        for ivar in range(len(vars)):
            print 'from: ', files[i], ', read ', vars[ivar]
            data = fid.variables[vars[ivar]]
            hyam = fid.variables['hyam'][:]
            hybm = fid.variables['hybm'][:]
            ps = np.exp(fid.variables['PS'][:][0])
            if len(data[:][0].shape) == 2:
                pics[ivar] += data[:][0]
            else:
                for ilev in range(nlev - 1):
                    flag1 = np.zeros([nlat, nlon]).astype(int)
                    flag2 = np.zeros([nlat, nlon]).astype(int)
                    p1 = hyam[ilev] * 1e5 + hybm[ilev] * ps
예제 #18
0
def create_inifile(ininame,grdname,title,theta_s,theta_b,hc,N,ini_time,vtransform):

#
#
################################################################
#
#
#   This function create the header of a Netcdf climatology 
#   file.
#
#   Input: 
# 
#   ininame      Netcdf initial file name (character string).
#   grdname      Netcdf grid file name (character string).
#   theta_s      S-coordinate surface control parameter.(Real)
#   theta_b      S-coordinate bottom control parameter.(Real)
#   hc           Width (m) of surface or bottom boundary layer
#                where higher vertical resolution is required 
#                during stretching.(Real)
#   N            Number of vertical levels.(Integer)  
#   ini_time     Initial time.(Real) 
#   clobber      Switch to allow or not writing over an existing
#                file.(character string) 
#
# 
#
#
################################################################
#
#

  print(' ')
  print(' Creating the file : '+ininame)
  print(' VTRANSFORM = '+str(vtransform))

#
#  Read the grid file and check the topography
#

  nc=netcdf(grdname, 'r')
  h=nc.variables['h'][:]
  maskr=nc.variables['mask_rho'][:]
  [Mp,Lp]=np.shape(h)
  nc.close()
#
#
#
  hmin=np.min(h[np.where(maskr==1)])
  if vtransform == 1:
    if hc > hmin:
      print('Error: hc ('+hc+' m) > hmin ('+hmin+' m)')


  L=Lp-1
  M=Mp-1
  Np=N+1

#
#  Create the initial file
#

  type = 'INITIAL file'  
  history = 'CROCO' 
  if os.path.exists(ininame):
      os.remove(ininame)

  print('Create: ' +ininame)
  nc=netcdf(ininame,'w',format='NETCDF3_CLASSIC')

#
# Create global attributes
#

  nc.title = title
  nc.history = 'Created '+str(time.ctime(time.time()))
  nc.ini_file = ininame
  nc.grd_file = grdname
  nc.type='CROCO initial file'

#
#  Create dimensions
#

  nc_dim_xi_u=nc.createDimension('xi_u',L)
  nc_dim_xi_v=nc.createDimension('xi_v',Lp)
  nc_dim_xi_rho=nc.createDimension('xi_rho',Lp)
  nc_dim_eta_u=nc.createDimension('eta_u',Mp)
  nc_dim_eta_v=nc.createDimension('eta_v',M)
  nc_dim_eta_rho=nc.createDimension('eta_rho',Mp)
  nc_dim_s_rho=nc.createDimension('s_rho',N)
  nc_dim_s_w=nc.createDimension('s_w',Np)
  nc_dim_tracer=nc.createDimension('tracer',2)
  nc_dim_ini_time=nc.createDimension('ini_time',0)
  nc_dim_one=nc.createDimension('one',1)

#
#  Create variables and attributes
#

  nc_spherical=nc.createVariable('spherical','S1', ('one',))
  nc_spherical.long_name = 'grid type logical switch'
  nc_spherical.flag_values = 'T, F'
  nc_spherical.flag_meanings = 'spherical Cartesian'
#
  nc_Vtransform=nc.createVariable('Vtransform','i4', ('one',))
  nc_Vtransform.long_name = 'vertical terrain-following transformation equation'
#
  nc_Vstretching=nc.createVariable('Vstretching','i4', ('one',))
  nc_Vstretching.long_name = 'vertical terrain-following stretching function'
#
  nc_tstart=nc.createVariable('tstart',np.float64, ('one',))
  nc_tstart.long_name = 'start processing day'
  nc_tstart.units = 'day'
#
  nc_tend=nc.createVariable('tend',np.float64, ('one',))
  nc_tend.long_name = 'end processing day'
  nc_tend.units = 'day'
#
  nc_theta_s=nc.createVariable('theta_s',np.float64, ('one',))
  nc_theta_s.long_name = 'S-coordinate surface control parameter'
  nc_theta_s.units = 'nondimensional'
#
  nc_theta_b=nc.createVariable('theta_b',np.float64, ('one',))
  nc_theta_b.long_name = 'S-coordinate bottom control parameter'
  nc_theta_b.units = 'nondimensional'
#
  nc_Tcline=nc.createVariable('Tcline',np.float64, ('one',))
  nc_Tcline.long_name = 'S-coordinate surface/bottom layer width'
  nc_Tcline.units = 'meter'
#
  nc_hc=nc.createVariable('hc',np.float64, ('one',))
  nc_hc.long_name = 'S-coordinate parameter, critical depth'
  nc_hc.units = 'meter'
#
  nc_sc_r=nc.createVariable('sc_r',np.float64, ('s_rho',))
  nc_sc_r.long_name = 'S-coordinate at RHO-points'
  nc_sc_r.valid_min = -1.
  nc_sc_r.valid_max = 0.
  nc_sc_r.positive = 'up'

  if vtransform == 1:
    nc_sc_r.standard_name = 'ocean_s_coordinate_g1'
  elif vtransform == 2:
    nc_sc_r.standard_name = 'ocean_s_coordinate_g2'     

  nc_sc_r.formula_terms = 's: s_rho C: Cs_r eta: zeta depth: h depth_c: hc'
#
  nc_sc_w=nc.createVariable('sc_w',np.float64, ('s_w',))
  nc_sc_w.long_name = 'S-coordinate at W-points'
  nc_sc_w.valid_min = -1. 
  nc_sc_w.valid_max = 0. 
  nc_sc_w.positive = 'up'

  if vtransform == 1:
    nc_sc_w.standard_name = 'ocean_s_coordinate_g1'
  elif vtransform == 2:
    nc_sc_w.standard_name = 'ocean_s_coordinate_g2'

  nc_sc_w.formula_terms = 's: s_w C: Cs_w eta: zeta depth: h depth_c: hc'
#
  nc_Cs_r=nc.createVariable('Cs_r',np.float64, ('s_rho',))
  nc_Cs_r.long_name = 'S-coordinate stretching curves at RHO-points'
  nc_Cs_r.units = 'nondimensional'
  nc_Cs_r.valid_min = -1
  nc_Cs_r.valid_max = 0
#
  nc_Cs_w=nc.createVariable('Cs_w',np.float64, ('s_w',))
  nc_Cs_w.long_name = 'S-coordinate stretching curves at W-points'
  nc_Cs_w.units = 'nondimensional'
  nc_Cs_w.valid_min = -1
  nc_Cs_w.valid_max = 0
#
  nc_scrum_time=nc.createVariable('scrum_time',np.float64, ('ini_time',))
  nc_scrum_time.long_name = 'ini_time since initialization'
  nc_scrum_time.units = 'second'
#
  nc_ocean_time=nc.createVariable('ocean_time',np.float64, ('ini_time',))
  nc_ocean_time.long_name = 'ini_time since initialization'
  nc_ocean_time.units = 'second'
#
  nc_u=nc.createVariable('u',np.float64, ('ini_time','s_rho','eta_u','xi_u',)) 
  nc_u.long_name = 'u-momentum component'
  nc_u.units = 'meter second-1'
#
  nc_v=nc.createVariable('v',np.float64, ('ini_time','s_rho','eta_v','xi_v',)) 
  nc_v.long_name = 'v-momentum component'
  nc_v.units = 'meter second-1'
#
  nc_ubar=nc.createVariable('ubar',np.float64, ('ini_time','eta_u','xi_u',)) 
  nc_ubar.long_name = 'vertically integrated u-momentum component'
  nc_ubar.units = 'meter second-1'
#
  nc_vbar=nc.createVariable('vbar',np.float64, ('ini_time','eta_v','xi_v',)) 
  nc_vbar.long_name = 'vertically integrated v-momentum component'
  nc_vbar.units = 'meter second-1'
#
  nc_zeta=nc.createVariable('zeta',np.float64, ('ini_time','eta_rho','xi_rho',)) 
  nc_zeta.long_name = 'free-surface'
  nc_zeta.units = 'meter'
#
  nc_temp=nc.createVariable('temp',np.float64, ('ini_time','s_rho','eta_rho','xi_rho',)) 
  nc_temp.long_name = 'potential temperature'
  nc_temp.units = 'Celsius'
#
  nc_salt=nc.createVariable('salt',np.float64, ('ini_time','s_rho','eta_rho','xi_rho',))
  nc_salt.long_name = 'salinity'
  nc_salt.units = 'PSU'

#
# Compute S coordinates
#

  (sc_r,Cs_r,sc_w,Cs_w) = vgrd.scoordinate(theta_s,theta_b,N,hc,vtransform)
  print('vtransform = '+str(vtransform))

#
# Write variables
#

  nc_spherical[:]='T'
  nc_Vtransform[:]=vtransform
  nc_Vstretching[:]=1
  nc_tstart[:] =  ini_time 
  nc_tend[:] =  ini_time 
  nc_theta_s[:] =  theta_s 
  nc_theta_b[:] =  theta_b 
  nc_Tcline[:] =  hc 
  nc_hc[:] =  hc 
  nc_sc_r[:] =  sc_r 
  nc_Cs_r[:] =  Cs_r 
  nc_scrum_time[0] = ini_time*24.*3600. 
  nc_ocean_time[0] = ini_time*24.*3600. 
  nc_u[:] =  0 
  nc_v[:] =  0 
  nc_zeta[:] =  0 
  nc_ubar[:] =  0 
  nc_vbar[:] =  0 
  nc_temp[:] =  0 
  nc_salt[:] =  0 
#
  nc.close()
#  
  return
예제 #19
0
 assuming that the time unit are well documented.
"""
import os, sys
#sys.path.append("/home/jouanno/Tools/MyPython/")
from netCDF4 import Dataset as netcdf
import datetime
import netcdftime as nctime
import numpy as np
#
if len(sys.argv) == 2:
    cfile = sys.argv[-1]
else:
    print "USAGE :", sys.argv[0], "FILE"
    quit()

cfile = sys.argv[-1]
print " working for file :", cfile
ncfile = netcdf(cfile, 'r')
time_counter = ncfile.variables['time_counter']
units = time_counter.getncattr('units')
cal = 'standard'
cal = time_counter.getncattr('calendar')
time = ncfile.variables['time_counter'][:]
cdftime = nctime.utime(units, calendar=cal)
dat = cdftime.num2date(time)
npl = len(dat)

for i in range(0, npl):
    print i + 1, dat[i]
quit()
예제 #20
0
def fncread(fn, var):
    fid = netcdf(fn, 'r')
    data = fid.variables[var][:]
    fid.close()
    return data
예제 #21
0
    print(' Making initial file: ' + ininame)
    print(' ')
    print(' Title: ' + title)

    #
    # Initial file
    #

    glor.create_inifile(ininame, grdname, title, theta_s, theta_b, hc, N, Tini,
                        vtransform)

    #
    # get the CROCO grid
    #

    ncg = netcdf(grdname, 'r')
    ncgrd = ncg.variables
    lon_rho = np.array(ncgrd['lon_rho'][:])
    lat_rho = np.array(ncgrd['lat_rho'][:])
    lon_u = np.array(ncgrd['lon_u'][:])
    lat_u = np.array(ncgrd['lat_u'][:])
    lon_v = np.array(ncgrd['lon_v'][:])
    lat_v = np.array(ncgrd['lat_v'][:])
    h = np.array(ncgrd['h'][:])
    mask = np.array(ncgrd['mask_rho'][:])
    angle = np.array(ncgrd['angle'][:])
    [M, L] = np.shape(lon_rho)
    ncg.close()

    lonmin = np.min(lon_rho)
    lonmax = np.max(lon_rho)
예제 #22
0
import cartopy.crs as ccrs
import numpy as np
from netCDF4 import Dataset as netcdf
from datetime import datetime
from datetime import timedelta

ufilt_filename = sys.argv[1]
vfilt_filename = sys.argv[2]
u_filename = sys.argv[3]
v_filename = sys.argv[4]
ufield = sys.argv[5]
vfield = sys.argv[6]
to = int(sys.argv[7])
tf = int(sys.argv[8])

ufilt_file = netcdf(ufilt_filename, 'r', format='NETCDF4')
vfilt_file = netcdf(vfilt_filename, 'r', format='NETCDF4')
u_file = netcdf(u_filename, 'r', format='NETCDF4')
v_file = netcdf(v_filename, 'r', format='NETCDF4')
lon = u_file.variables['lon'][:]
lat = u_file.variables['lat'][:]
time = u_file.variables['time'][:]
lons, lats = lons, lats = np.meshgrid(lon, lat)

cmap = plt.cm.RdBu_r
cmap.set_bad((0.2, 0.2, 0.2), 1.)

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='JRA55do Wind', artist='Mike Bueti')
writer = FFMpegWriter(fps=12, metadata=metadata)
예제 #23
0
파일: Input.py 프로젝트: dsiuta/verif
 def isValid(filename):
     try:
         file = netcdf(filename, 'r')
     except:
         return False
     return True
예제 #24
0
#
Ymin = 1993
Mmin = 01
Dmin = 01
#
# Last date to process
#
#
Ymax = 2017
Mmax = 05
Dmax = 10
#
#
dt = 3  # Interval between AVISO maps [days]
#
nc = netcdf('glorys_moz_sst.nc')
lon_glo = nc.variables['lon'][:]
lat_glo = nc.variables['lat'][:]
T_glo = nc.variables['time'][:]
#
# choose domain to subset: !! don't go north of 23N !!
#

latmin = -30
latmax = -10
lonmin = 32
lonmax = 55

#
# Time in days since 1/1/Yorig
# romsdate=date.fromordinal(np.int(date.toordinal(date(Yorig,1,1))+t/(24*3600)))
예제 #25
0
파일: Input.py 프로젝트: dsiuta/verif
 def __init__(self, filename):
     Input.__init__(self, filename)
     self._file = netcdf(filename, 'r')
예제 #26
0
def main(argv):
    # Optional for the user to specify are the following
    # parameters:
    # -b <basedir>
    # -p <prefix>
    # -c <country> - the country in which all stations
    # will be iterated through.
    # (If not specified - the script iterates through
    # all countries.)
    # Mandatory for the user to specify are the following:
    # -s <source_name> - each user has a specific source_name
    # that they should know (if not, see Instructions, point 7).
    # -d <env> - the environment in which the data from the
    # WRF model is going to be stored.
    # -o <output> - either insert data into database or
    # export it to txt fomrat.
    basedir = './'
    prefix = 'wrfout_d02'
    source_name = ''
    country = 'All'  # By default: 'All'.
    # Possible options are 'BG', 'GR', ...
    env = ''  # possible options are 'dev' and 'prod'.
    output = 'db'  # By default: 'db'.
    # Possible options: 'db' (write to SUADA db),
    # 'tro' (write to troposinex txt format).
    instrument_name = 'GNSS'

    try:
        opts, args = getopt.getopt(argv, "h:b:p:s:c:d:o:", [
            "basedir=", "prefix=", "source_name=", "country=", "env=",
            "output="
        ])
    except getopt.GetoptError:
        print 'ncdf2db.py -b <basedir> [' + basedir + '] -p <prefix> [' + prefix + '] -s <source_name> [' + str(
            source_name) + '] -c <country> [' + str(
                country) + '] -d <env> [' + str(env) + '] -o <output> [' + str(
                    output) + ']'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'ncdf2db.py -b <basedir> [' + basedir + '] -p <prefix> [' + prefix + '] -s <source_name> [' + str(
                source_name) + '] -c <country> [' + str(
                    country) + '] -d <env> [' + str(
                        env) + '] -o <output> [' + str(output) + ']'
            sys.exit()
        elif opt in ("-b", "--basedir"):
            basedir = arg
        elif opt in ("-p", "--prefix"):
            prefix = arg
        elif opt in ("-s", "--source_name"):
            source_name = str(arg)
        elif opt in ("-c", "--country"):
            if country:
                country = str(arg)
            else:
                country = 'All'
        elif opt in ("-d", "--env"):
            env = str(arg)
        elif opt in ("-o", "--output"):
            output = str(arg)

    # Check whether the user has specified source name.
    # If not -> Error.
    if source_name == '':
        print 'Error: You must specify the source name! (-s <source_name>)'
        sys.exit()

    # Check whether the user has specified the database.
    # If not -> Error.
    if env == '':
        print 'Error: You must specify the database! (-d <env>)'
        sys.exit()

    if not output in {'db', 'tro'}:
        print('Error: Not a possible output {}'.format(output))
        sys.exit()

    # Retrieve the list of all data files
    # starting with [prefix] inside [basedir] folder
    flist = listfiles(basedir, prefix)

    # Create the DB connection:
    db = None
    cur = None
    try:
        if env == 'dev':
            print('DB -> {}'.format(cfg.dev['db']))
            db = MySQLdb.connect(host=cfg.dev['host'], \
             user=cfg.dev['user'], \
             passwd=cfg.dev['passwd'], \
             db=cfg.dev['db'])
        elif env == 'prod':
            print('DB -> {}'.format(cfg.prod['db']))
            db = MySQLdb.connect(host=cfg.prod['host'], \
             user=cfg.prod['user'], \
             passwd=cfg.prod['passwd'], \
             db=cfg.prod['db'])
        elif env != {'dev', 'prod'}:
            print 'Error: No such database! (Possible options for -d <env> are "dev" and "prod".)'
            sys.exit()
        cur = db.cursor()
    except Exception as e:
        print('Failed to establish connection: {0}'.format(e))
        cur.close()
        sys.exit(1)

    # Fetching source_id...
    print('Trying to fetch the source_id ...')
    source_id = get_source_id(cur, source_name)
    if source_id < 0:
        print 'Error: Can not find source_id for source_name: {}'.format(
            source_name)
        sys.exit(1)

    print('Source id: {} found for source name: {}'.format(
        source_id, source_name))

    # Call the procedure that selects the stations' information
    # from the SUADA information tables:
    print('Get stations')
    stations = getstations(cur, source_name, country, instrument_name)

    # Now iterating over list of all data files:
    print('Iterate files')

    for file in flist:
        field2D = []
        print 'Processing: ', file
        ncfile = netcdf(file)
        strDateTime = ncfile.variables['Times'][0].tostring().replace('_', ' ')
        local_tz = get_localzone()
        date = parser.parse(strDateTime)
        strDateTimeLocal = local_tz.localize(date)
        # Print the timestamp
        print('Dataset timestamp: {}'.format(strDateTimeLocal))
        xlong = ncfile.variables['XLONG'][0]
        xlat = ncfile.variables['XLAT'][0]
        alt = ncfile.variables['HGT'][0]
        truelat1 = ncfile.TRUELAT1
        truelat2 = ncfile.TRUELAT2
        ref_lat = ncfile.CEN_LAT
        ref_lon = ncfile.CEN_LON
        stand_lon = ncfile.STAND_LON
        dx = ncfile.DX
        dy = ncfile.DY
        west_east = ncfile.dimensions['west_east'].size
        south_north = ncfile.dimensions['south_north'].size

        # Empty list to contain data:
        station_data = []
        for station in stations:
            stationName = station['name']
            stationId = station['id']
            sensorId = station['senid']
            print 'Station: ', station['name'], ' ID: ', station[
                'id'], ' sensorId: ', sensorId, 'Country Code: ', station[
                    'country']
            x0 = station['long']
            y0 = station['latt']
            z0 = station['alt']
            indx = wrf.ll_to_ij(1, truelat1, truelat2, stand_lon, dx, dy,
                                ref_lat, ref_lon, y0, x0)
            j0 = west_east / 2 + indx[0] - 1
            i0 = south_north / 2 + indx[1] - 1
            station['i0'] = i0
            station['j0'] = j0
            station['source_id'] = source_id

            if (i0 >= 0 and
                    i0 < south_north) and (j0 >= 0 and j0 < west_east) and (
                        (country == 'All') or (country == station['country'])):
                if output == 'db':
                    process_station(db, cur, station, ncfile, date)
                elif output == 'tro':
                    # save result in
                    # tropo_station_data
                    tropo_station_data = process_station_tro(
                        station, ncfile, date)
                    # if tropo_station_data is
                    # not None,
                    # append to data list
                    if tropo_station_data:
                        station_data.append(tropo_station_data.copy())
        if output == 'tro' and len(station_data) > 0:
            tropo_out(station_data)

    if not (len(flist)):
        print 'No candidates for import files found ...'
        sys.exit(1)
예제 #27
0
print 'Setting ROMS solutions paths for: ' + init_dict['ROMS_ID']
ROMS_obj = ROMS_out_paths.ROMS_run(init_dict['ROMS_ID'])
ROMS_obj.set_paths()

#####################################
#SET OUTPUT AND GRID FILE PATHS/NAMES
####################################
grd_name = ROMS_obj.path_grid + ROMS_obj.grid_name
dat_name = ROMS_obj.path_output + ROMS_obj.out_base_name
if t_interp == 'spline':
    uv_name = ROMS_obj.path_ts_uv + 'ts_' + ROMS_obj.out_base_name

#################################
# READ IN GRID to obtain dx, dy
##################################
nc_grd = netcdf(grd_name, 'r')
pm = nc_grd.variables['pm'][:]
pn = nc_grd.variables['pn'][:]
dx = 1. / pm.T
dy = 1. / pn.T
[Lx, Ly] = dx.shape
################################################################

#############################################################
# Make list of filenames with frame numbers of velocity data
#############################################################
fnum = init_dict['fnum']
sub = init_dict['sub']
frpf = init_dict['frpf']
nfr = init_dict['nfr']
dfr = init_dict['dfr']
예제 #28
0
#SET OUTPUT AND GRID FILE PATHS/NAMES
####################################
grd_name = ROMS_obj.path_grid + ROMS_obj.grid_name
dat_name = ROMS_obj.path_output + ROMS_obj.out_base_name
try:
    uv_name = ROMS_obj.path_ts_uv + 'ts_' + ROMS_obj.out_base_name  #from mtspline
    omega_name = ROMS_obj.path_omega + 'ts_' + ROMS_obj.omega_name  # from mtspline
    zs_name = ROMS_obj.path_zs + ROMS_obj.zs_name  # from mtspline
except:
    pass
################################################################

#################################
# READ IN GRID to obtain dx, dy
##################################
nc_grd = netcdf(grd_name, 'r')
pm = nc_grd.variables['pm'][:]
pn = nc_grd.variables['pn'][:]
dx = 1. / pm.T
dy = 1. / pn.T
mask_rho = nc_grd.variables['mask_rho'][:, :]
[Lx, Ly] = dx.shape
################################################################

#############################################################
# Make list of filenames with frame numbers of velocity data
#############################################################
t_interp = init_dict['t_interp']
omega_online = init_dict['omega_online']
tim_opt = init_dict['tim_opt']
fnum = init_dict['fnum']
예제 #29
0
def fncread(fn, var):
    # read a variable from a netcdf file
    fid = netcdf(fn, 'r')
    data = fid.variables[var][:]
    fid.close()
    return data
예제 #30
0
 else:
     ntime = timeend // foutfreq
 for itime in range(40, ntime):
     #for itime in range(18):
     if foutfreq < 0: dsec = -foutfreq * tunit * itime
     else: dsec = foutfreq * timestep * itime
     time = inittime + timedelta(seconds=dsec)
     tstr = format(time.year,'04')+format(time.month,'02')+ \
             format(time.day,'02')+'_'+format(time.hour,'02')+ \
             '-'+format(time.minute,'02')+'-'+format(time.second,'02')
     fn = pcases + casename + '/' + casename + '_' + tstr + '.nc'
     status, fn = commands.getstatusoutput('ls ' + fn)
     if status == 0:
         pictime += [time]
         print fn
         fid = netcdf(fn, 'r')
         lev = fid.variables['lev'][:]
         lon = fid.variables['lon'][:]
         for ivar in range(len(vars)):
             if piclevs[ivar] < 0:
                 data = np.mean(fid.variables[vars[ivar]][:, (nlat / 2 -
                                                              1):(nlat / 2 +
                                                                  1), :],
                                axis=1)
                 reallev[vars[ivar]] = -1
             else:
                 ilev = np.argmin(np.abs(lev - piclevs[ivar]))
                 reallev[vars[ivar]] = lev[ilev]
                 data = np.mean(fid.variables[vars[ivar]][:, ilev,
                                                          (nlat / 2 -
                                                           1):(nlat / 2 +
예제 #31
0
파일: csiutils.py 프로젝트: watpet/csi
def write2netCDF(filename,
                 lon,
                 lat,
                 z,
                 increments=None,
                 nSamples=None,
                 title='CSI product',
                 name='z',
                 scale=1.0,
                 offset=0.0,
                 mask=None,
                 xyunits=['Lon', 'Lat'],
                 units='None',
                 interpolation=True,
                 verbose=True,
                 noValues=np.nan):
    '''
    Creates a netCDF file  with the arrays in Z. 
    Z can be list of array or an array, the size of lon.
                
    .. Args:
        
        * filename -> Output file name
        * lon      -> 1D Array of lon values
        * lat      -> 1D Array of lat values
        * z        -> 2D slice to be saved
        * mask     -> if not None, must be a 2d-array of a polynome to mask 
                      what is outside of it. This option is really long, so I don't 
                      use it...
   
    .. Kwargs:
               
        * title    -> Title for the grd file
        * name     -> Name of the field in the grd file
        * scale    -> Scale value in the grd file
        * offset   -> Offset value in the grd file
                
    .. Returns:
          
        * None
    '''

    if interpolation:

        # Check
        if nSamples is not None:
            if type(nSamples) is int:
                nSamples = [nSamples, nSamples]
            dlon = (lon.max() - lon.min()) / nSamples[0]
            dlat = (lat.max() - lat.min()) / nSamples[1]
        if increments is not None:
            dlon, dlat = increments

        # Resample on a regular grid
        olon, olat = np.meshgrid(np.arange(lon.min(), lon.max(), dlon),
                                 np.arange(lat.min(), lat.max(), dlat))
    else:
        # Get lon lat
        olon = lon
        olat = lat
        if increments is not None:
            dlon, dlat = increments
        else:
            dlon = olon[0, 1] - olon[0, 0]
            dlat = olat[1, 0] - olat[0, 0]

    # Create a file
    fid = netcdf(filename, 'w')

    # Create a dimension variable
    fid.createDimension('side', 2)
    if verbose:
        print('Create dimension xysize with size {}'.format(np.prod(
            olon.shape)))
    fid.createDimension('xysize', np.prod(olon.shape))

    # Range variables
    fid.createVariable('x_range', 'd', ('side', ))
    fid.variables['x_range'].units = xyunits[0]

    fid.createVariable('y_range', 'd', ('side', ))
    fid.variables['y_range'].units = xyunits[1]

    # Spacing
    fid.createVariable('spacing', 'd', ('side', ))
    fid.createVariable('dimension', 'i4', ('side', ))

    # Informations
    if title is not None:
        fid.title = title
    fid.source = 'CSI.utils.write2netCDF'

    # Filing rnage and spacing
    if verbose:
        print('x_range from {} to {} with spacing {}'.format(
            olon[0, 0], olon[0, -1], dlon))
    fid.variables['x_range'][0] = olon[0, 0]
    fid.variables['x_range'][1] = olon[0, -1]
    fid.variables['spacing'][0] = dlon

    if verbose:
        print('y_range from {} to {} with spacing {}'.format(
            olat[0, 0], olat[-1, 0], dlat))
    fid.variables['y_range'][0] = olat[0, 0]
    fid.variables['y_range'][1] = olat[-1, 0]
    fid.variables['spacing'][1] = dlat

    if interpolation:
        # Interpolate
        interpZ = sciint.LinearNDInterpolator(np.vstack((lon, lat)).T,
                                              z,
                                              fill_value=noValues)
        oZ = interpZ(olon, olat)
    else:
        # Get values
        oZ = z

    # Masking?
    if mask is not None:

        # Import matplotlib.path
        import matplotlib.path as path

        # Create the path
        poly = path.Path([[lo, la] for lo, la in zip(mask[:, 0], mask[:, 1])],
                         closed=False)

        # Create the list of points
        xy = np.vstack((olon.flatten(), olat.flatten())).T

        # Findthose outside
        bol = poly.contains_points(xy)

        # Mask those out
        oZ = oZ.flatten()
        oZ[bol] = np.nan
        oZ = oZ.reshape(olon.shape)

    # Range
    zmin = np.nanmin(oZ)
    zmax = np.nanmax(oZ)
    fid.createVariable('{}_range'.format(name), 'd', ('side', ))
    fid.variables['{}_range'.format(name)].units = units
    fid.variables['{}_range'.format(name)][0] = zmin
    fid.variables['{}_range'.format(name)][1] = zmax

    # Create Variable
    fid.createVariable(name, 'd', ('xysize', ))
    fid.variables[name].long_name = name
    fid.variables[name].scale_factor = scale
    fid.variables[name].add_offset = offset
    fid.variables[name].node_offset = 0

    # Fill it
    fid.variables[name][:] = np.flipud(oZ).flatten()

    # Set dimension
    fid.variables['dimension'][:] = oZ.shape[::-1]

    # Synchronize and close
    fid.sync()
    fid.close()

    # All done
    return