Exemplo n.º 1
0
def data2romsblk(data, grd, **kargs):
    quiet = True
    keepOriginal = 2  # if 2, will keep data only inside rt.grid_vicinity rectange
    # if 1, all original data is kept
    Margin = 4  # for griddata and for original data to keep

    for k in kargs.keys():
        if k == "quiet":
            quiet = kargs[k]
        elif k.lower().startswith("keepor"):
            keepOriginal = kargs[k]
        elif k == "margin":
            Margin = kargs[k]

    g = roms.Grid(grd)

    cond = False
    out = {}

    for vname in data.keys():
        if vname.startswith("INFO") or vname in "xy":
            continue
        # vnames starting with INFO are an info key, without data

        if not quiet:
            print "  interp %s" % vname

        if cond is False:
            cond, inds = rt.grid_vicinity(grd, data["x"], data["y"], margin=Margin, rect=True, retinds=True)
            i1, i2, j1, j2 = inds

        out[vname] = calc.griddata(data["x"][cond], data["y"][cond], data[vname][cond], g.lon, g.lat, extrap=True)

        if keepOriginal:
            # keep original data:
            if keepOriginal == 1:
                out[vname + "_original"] = data[vname]  # keep all original data
            elif keepOriginal == 2:
                out[vname + "_original"] = data[vname][j1:j2, i1:i2]  # keep data only inside vicinity rectangle

            if not out.has_key("x_original"):
                if keepOriginal == 1:
                    out["x_original"] = data["x"]
                    out["y_original"] = data["y"]
                elif keepOriginal == 2:
                    out["x_original"] = data["x"][j1:j2, i1:i2]
                    out["y_original"] = data["y"][j1:j2, i1:i2]

    # about wind:
    if not quiet:
        print " --> rot U,V wind and U,V wind stress"
    angle = g.use("angle")
    out["uwnd"], out["vwnd"] = calc.rot2d(out["uwnd"], out["vwnd"], angle)
    out["sustr"], out["svstr"] = calc.rot2d(out["sustr"], out["svstr"], angle)

    out["sustr"] = rt.rho2uvp(out["sustr"], "u")
    out["svstr"] = rt.rho2uvp(out["svstr"], "v")

    return out
Exemplo n.º 2
0
def data2romsblk(data,grd,**kargs):
  quiet        = True
  keepOriginal = 2 # if 2, will keep data only inside rt.grid_vicinity rectange
                   # if 1, all original data is kept
  Margin       = 4 # for griddata and for original data to keep

  for k in kargs.keys():
    if k=='quiet': quiet=kargs[k]
    elif k.lower().startswith('keepor'): keepOriginal=kargs[k]
    elif k=='margin': Margin=kargs[k]


  g=roms.Grid(grd)

  cond=False
  out={}

  for vname in data.keys():
    if vname.startswith('INFO') or vname in 'xy': continue
    # vnames starting with INFO are an info key, without data

    if not quiet: print '  interp %s' % vname

    if cond is False:
      cond,inds=rt.grid_vicinity(grd,data['x'],data['y'],
                                 margin=Margin,rect=True,retinds=True)
      i1,i2,j1,j2=inds

    out[vname]=calc.griddata(data['x'][cond],data['y'][cond],
                           data[vname][cond],g.lon,g.lat,extrap=True)

    if keepOriginal:
      # keep original data:
      if keepOriginal==1:
        out[vname+'_original']=data[vname] # kepp all original data
      elif keepOriginal==2:
        out[vname+'_original']=data[vname][j1:j2,i1:i2] # keep data only inside vicinity rectangle

      if not out.has_key('x_original'):
        if keepOriginal==1:
          out['x_original']=data['x']
          out['y_original']=data['y']
        elif keepOriginal==2:
          out['x_original']=data['x'][j1:j2,i1:i2]
          out['y_original']=data['y'][j1:j2,i1:i2]

  # about wind:
  if not quiet: print ' --> rot U,V wind and U,V wind stress'
  angle=g.use('angle')
  out['uwnd'],out['vwnd']=calc.rot2d(out['uwnd'],out['vwnd'],angle)
  out['sustr'],out['svstr']=calc.rot2d(out['sustr'],out['svstr'],angle)

  out['sustr']=rt.rho2uvp(out['sustr'],'u')
  out['svstr']=rt.rho2uvp(out['svstr'],'v')


  return out
Exemplo n.º 3
0
def update_wind(fname, data, new_wind_info, **kargs):
    '''
  new_wind_info will be added to fname as global attribute
  (ex: 'new wind from database xxx')

  '''
    quiet = False
    Margin = 4  # for griddata and for original data to keep
    grd = False
    keepOriginal = 2

    for k in kargs.keys():
        if k == 'quiet': quiet = kargs[k]
        elif k == 'margin': Margin = kargs[k]
        elif k == 'grid': grd = kargs[k]
        elif k.lower().startswith('keepor'): keepOriginal = kargs[k]

    if not grd: grd = netcdf.fatt(fname, 'grd_file')
    g = roms.Grid(grd)

    x0 = data['x']
    y0 = data['y']
    if x0.ndim == 1: x0, y0 = np.meshgrid(x0, y0)
    dates = data.keys()[:]
    dates.remove('x')
    dates.remove('y')
    dates = np.array(dates)

    # interp in time:
    time = netcdf.nctime(fname, 'time')
    cond = False
    tind = -1
    fob = gennc.GenBlk(fname, grd)
    for t in time:
        newWind = {}
        tind += 1

        I, = np.where(dates == t)
        if I.size:
            I = I[0]
            uv = data[dates[I]]
            if not quiet: print(t, dates[I])
        else:
            i1, = np.where(dates > t)
            i0, = np.where(dates < t)
            if i0.size and i1.size:
                i1 = i1[0]
                i0 = i0[-1]
                d0 = t - dates[i0]
                d1 = dates[i1] - t
                d0 = d0.days + d0.seconds / 86400.
                d1 = d1.days + d1.seconds / 86400.
                uv = (data[dates[i0]] * d1 + data[dates[i1]] * d0) / (d0 + d1)
                if not quiet: print(t, dates[i0], dates[i1], d0, d1)
            elif not i1.size:
                uv = data[dates[-1]]
                if not quiet: print(t, dates[-1])
            elif not i0.size:
                uv = data[dates[0]]
                if not quiet: print(t, dates[0])

        # interp to grid:
        if cond is False:
            cond, inds = rt.grid_vicinity(grd,
                                          x0,
                                          y0,
                                          margin=Margin,
                                          rect=True,
                                          retinds=True)
            i1, i2, j1, j2 = inds

        if not quiet: print(' --> inter uv %s' % t.isoformat(' '))
        u = calc.griddata(x0[cond],
                          y0[cond],
                          uv.real[cond],
                          g.lon,
                          g.lat,
                          extrap=True)
        v = calc.griddata(x0[cond],
                          y0[cond],
                          uv.imag[cond],
                          g.lon,
                          g.lat,
                          extrap=True)

        # rotate wind, calc stress:
        if not quiet: print(' --> rot U,V wind and U,V wind stress')
        wspd = np.sqrt(u**2 + v**2)
        sustr, svstr = air_sea.wind_stress(u, v)
        angle = g.use('angle')
        uwnd, vwnd = calc.rot2d(u, v, angle)
        sustr, svstr = calc.rot2d(sustr, svstr, angle)
        sustr = rt.rho2uvp(sustr, 'u')
        svstr = rt.rho2uvp(svstr, 'v')

        # update wind data:
        newWind['date'] = t
        newWind['uwnd'] = uwnd
        newWind['vwnd'] = vwnd
        newWind['sustr'] = sustr
        newWind['svstr'] = svstr
        newWind['wspd'] = wspd
        # original xy:
        if tind == 0:
            newWind['attr'] = {'new_wind_info': new_wind_info}
            if not quiet: print(' --> original xy')
            if keepOriginal == 1:
                newWind['x_wind'] = x0
                newWind['y_wind'] = y0
            elif keepOriginal == 2:
                newWind['x_wind'] = x0[j1:j2, i1:i2]
                newWind['y_wind'] = y0[j1:j2, i1:i2]

        # add to file:
        if not quiet: print(' --> adding to file')
        fob.update_wind(newWind, quiet=quiet)
        if not quiet: print('')
Exemplo n.º 4
0
def update_wind_blended2(fname, datapaths, **kargs):
    '''
  In days without blended data will try to use quikscat data
  '''
    from okean.datasets import quikscat
    from okean.datasets import blended_wind
    a = blended_wind.WINDData(datapaths[0])
    b = quikscat.WINDData(datapaths[1])

    time = netcdf.nctime(fname, 'time')
    date0 = dts.next_date(time[0], -1)
    date1 = dts.next_date(time[-1], +2)

    data = a.data(date0, date1)

    # limit are... otherwise, quikscat interp will be very slow!
    grd = netcdf.fatt(fname, 'grd_file')
    import os
    if not os.path.isfile(grd): grd = kargs['grd']
    cond, inds = rt.grid_vicinity(grd,
                                  data['x'],
                                  data['y'],
                                  margin=5,
                                  rect=True,
                                  retinds=True)
    i1, i2, j1, j2 = inds
    for d in data.keys():
        if d == 'x': data[d] = data[d][i1:i2]
        elif d == 'y': data[d] = data[d][j1:j2]
        else: data[d] = data[d][j1:j2, i1:i2]

    # check for missing days:
    time0 = data.keys()
    x0 = data['x']
    y0 = data['y']
    x0, y0 = np.meshgrid(x0, y0)
    time0.remove('x')
    time0.remove('y')

    out = OrderedDict()
    out['x'] = x0
    out['y'] = y0
    info = ''
    qs_ij_limits_done = False
    for d in dts.drange(date0, date1):
        found = 0
        for t in time0:
            if (t.year, t.month, t.day) == (d.year, d.month, d.day):
                print('==> blended : ', t)
                out[t] = data[t]
                found = 1

        if not found:  # use quikscat:
            print('==> quikscat : ', d.strftime('%Y-%m-%d'))
            tmp = b.data(d, dts.next_date(d))
            if not tmp.has_key('x'): continue
            x, y = tmp['x'], tmp['y']
            x, y = np.meshgrid(x, y)

            # reduce qs data:
            if not qs_ij_limits_done:
                i1, i2, j1, j2 = calc.ij_limits(x, y,
                                                [x0.min(), x0.max()],
                                                [y0.min(), y0.max()])
                qs_ij_limits_done = True

            x = x[j1:j2, i1:i2]
            y = y[j1:j2, i1:i2]
            tmp[tmp.keys()[0]] = tmp[tmp.keys()[0]][j1:j2, i1:i2]

            print('  griddata u')
            u = calc.griddata(x, y, tmp[tmp.keys()[0]].real, x0, y0)
            print('  griddata v')
            v = calc.griddata(x, y, tmp[tmp.keys()[0]].imag, x0, y0)
            out[tmp.keys()[0]] = u + 1.j * v
            info += '#' + d.strftime('%Y%m%d')

    new_wind_info = 'blended+quikscat at days: ' + info
    update_wind(fname, out, new_wind_info, **kargs)
Exemplo n.º 5
0
def data2romsblk(data, grd, **kargs):
    quiet = kargs.get('quiet', True)
    Margin = kargs.get('margin',
                       4)  # for griddata and for original data to keep
    # if margin is 'no calc', no extraction of
    # a domain portion is done

    # about projection:
    # - if auto will use grid default projection
    # - if False, no projection is used
    # - any Basemap projection can be used
    proj = kargs.get('proj', 'auto')

    # about origina data to keep:
    # - if 2, will keep data only inside rt.grid_vicinity rectange
    # - if 1, all original data is kept
    keepOriginal = 2
    for k in kargs:
        if k.lower().startswith('keepor'): keepOriginal = kargs[k]

    if cb.isstr(grd): g = roms.Grid(grd)
    else: g = grd

    if proj == 'auto': proj = g.get_projection()

    if proj:
        data_x, data_y = proj(data['x'], data['y'])
        grd_x, grd_y = proj(g.lon, g.lat)
    else:
        data_x, data_y = data['x'], data['y']
        grd_x, grd_y = g.lon, g.lat

    cond = False
    out = {}

    for vname in data:
        if vname.startswith('INFO') or vname in 'xy': continue
        # vnames starting with INFO are an info key, without data

        if not quiet: print('  interp %s' % vname)

        if cond is False:
            if Margin == 'no calc':
                cond = np.ones_like(data['x'], 'bool')
                i1, i1 = 0, 0
                j2, i2 = data['x'].shape
            else:
                cond, inds = rt.grid_vicinity(grd,
                                              data['x'],
                                              data['y'],
                                              margin=Margin,
                                              rect=True,
                                              retinds=True)
                i1, i2, j1, j2 = inds

        out[vname] = calc.griddata(data_x[cond],
                                   data_y[cond],
                                   data[vname][cond],
                                   grd_x,
                                   grd_y,
                                   extrap=True)

        if keepOriginal:
            # keep original data:
            if keepOriginal == 1:
                out[vname +
                    '_original'] = data[vname]  # keep all original data
            elif keepOriginal == 2:
                out[vname + '_original'] = data[vname][
                    j1:j2, i1:i2]  # keep data only inside vicinity rectangle

            if 'x_original' not in out:
                if keepOriginal == 1:
                    out['x_original'] = data['x']
                    out['y_original'] = data['y']
                elif keepOriginal == 2:
                    out['x_original'] = data['x'][j1:j2, i1:i2]
                    out['y_original'] = data['y'][j1:j2, i1:i2]

    # about wind:
    if not quiet: print(' --> rot U,V wind and U,V wind stress')
    out['uwnd'], out['vwnd'] = calc.rot2d(out['uwnd'], out['vwnd'], g.angle)
    out['sustr'], out['svstr'] = calc.rot2d(out['sustr'], out['svstr'],
                                            g.angle)

    out['sustr'] = rt.rho2uvp(out['sustr'], 'u')
    out['svstr'] = rt.rho2uvp(out['svstr'], 'v')

    return out
Exemplo n.º 6
0
def update_wind(fname, data, new_wind_info, **kargs):
    """
  new_wind_info will be added to fname as global attribute
  (ex: 'new wind from database xxx')

  """
    quiet = False
    Margin = 4  # for griddata and for original data to keep
    grd = False
    keepOriginal = 2

    for k in kargs.keys():
        if k == "quiet":
            quiet = kargs[k]
        elif k == "margin":
            Margin = kargs[k]
        elif k == "grid":
            grd = kargs[k]
        elif k.lower().startswith("keepor"):
            keepOriginal = kargs[k]

    if not grd:
        grd = netcdf.fatt(fname, "grd_file")
    g = roms.Grid(grd)

    x0 = data["x"]
    y0 = data["y"]
    if x0.ndim == 1:
        x0, y0 = np.meshgrid(x0, y0)
    dates = data.keys()[:]
    dates.remove("x")
    dates.remove("y")
    dates = np.array(dates)

    # interp in time:
    time = netcdf.nctime(fname, "time")
    cond = False
    tind = -1
    fob = gennc.GenBlk(fname, grd)
    for t in time:
        newWind = {}
        tind += 1

        I, = np.where(dates == t)
        if I.size:
            I = I[0]
            uv = data[dates[I]]
            if not quiet:
                print t, dates[I]
        else:
            i1, = np.where(dates > t)
            i0, = np.where(dates < t)
            if i0.size and i1.size:
                i1 = i1[0]
                i0 = i0[-1]
                d0 = t - dates[i0]
                d1 = dates[i1] - t
                d0 = d0.days + d0.seconds / 86400.0
                d1 = d1.days + d1.seconds / 86400.0
                uv = (data[dates[i0]] * d1 + data[dates[i1]] * d0) / (d0 + d1)
                if not quiet:
                    print t, dates[i0], dates[i1], d0, d1
            elif not i1.size:
                uv = data[dates[-1]]
                if not quiet:
                    print t, dates[-1]
            elif not i0.size:
                uv = data[dates[0]]
                if not quiet:
                    print t, dates[0]

        # interp to grid:
        if cond is False:
            cond, inds = rt.grid_vicinity(grd, x0, y0, margin=Margin, rect=True, retinds=True)
            i1, i2, j1, j2 = inds

        if not quiet:
            print " --> inter uv %s" % t.isoformat(" ")
        u = calc.griddata(x0[cond], y0[cond], uv.real[cond], g.lon, g.lat, extrap=True)
        v = calc.griddata(x0[cond], y0[cond], uv.imag[cond], g.lon, g.lat, extrap=True)

        # rotate wind, calc stress:
        if not quiet:
            print " --> rot U,V wind and U,V wind stress"
        wspd = np.sqrt(u ** 2 + v ** 2)
        sustr, svstr = air_sea.wind_stress(u, v)
        angle = g.use("angle")
        uwnd, vwnd = calc.rot2d(u, v, angle)
        sustr, svstr = calc.rot2d(sustr, svstr, angle)
        sustr = rt.rho2uvp(sustr, "u")
        svstr = rt.rho2uvp(svstr, "v")

        # update wind data:
        newWind["date"] = t
        newWind["uwnd"] = uwnd
        newWind["vwnd"] = vwnd
        newWind["sustr"] = sustr
        newWind["svstr"] = svstr
        newWind["wspd"] = wspd
        # original xy:
        if tind == 0:
            newWind["attr"] = {"new_wind_info": new_wind_info}
            if not quiet:
                print " --> original xy"
            if keepOriginal == 1:
                newWind["x_wind"] = x0
                newWind["y_wind"] = y0
            elif keepOriginal == 2:
                newWind["x_wind"] = x0[j1:j2, i1:i2]
                newWind["y_wind"] = y0[j1:j2, i1:i2]

        # add to file:
        if not quiet:
            print " --> adding to file"
        fob.update_wind(newWind, quiet=quiet)
        if not quiet:
            print ""
Exemplo n.º 7
0
def update_wind_blended2(fname, datapaths, **kargs):
    """
  In days without blended data will try to use quikscat data
  """
    from okean.datasets import quikscat
    from okean.datasets import blended_wind

    a = blended_wind.WINDData(datapaths[0])
    b = quikscat.WINDData(datapaths[1])

    time = netcdf.nctime(fname, "time")
    date0 = dts.next_date(time[0], -1)
    date1 = dts.next_date(time[-1], +2)

    data = a.data(date0, date1)

    # limit are... otherwise, quikscat interp will be very slow!
    grd = netcdf.fatt(fname, "grd_file")
    import os

    if not os.path.isfile(grd):
        grd = kargs["grd"]
    cond, inds = rt.grid_vicinity(grd, data["x"], data["y"], margin=5, rect=True, retinds=True)
    i1, i2, j1, j2 = inds
    for d in data.keys():
        if d == "x":
            data[d] = data[d][i1:i2]
        elif d == "y":
            data[d] = data[d][j1:j2]
        else:
            data[d] = data[d][j1:j2, i1:i2]

    # check for missing days:
    time0 = data.keys()
    x0 = data["x"]
    y0 = data["y"]
    x0, y0 = np.meshgrid(x0, y0)
    time0.remove("x")
    time0.remove("y")

    out = cb.odict()
    out["x"] = x0
    out["y"] = y0
    info = ""
    qs_ij_limits_done = False
    for d in dts.drange(date0, date1):
        found = 0
        for t in time0:
            if (t.year, t.month, t.day) == (d.year, d.month, d.day):
                print "==> blended : ", t
                out[t] = data[t]
                found = 1

        if not found:  # use quikscat:
            print "==> quikscat : ", d.strftime("%Y-%m-%d")
            tmp = b.data(d, dts.next_date(d))
            if not tmp.has_key("x"):
                continue
            x, y = tmp["x"], tmp["y"]
            x, y = np.meshgrid(x, y)

            # reduce qs data:
            if not qs_ij_limits_done:
                i1, i2, j1, j2 = calc.ij_limits(x, y, [x0.min(), x0.max()], [y0.min(), y0.max()])
                qs_ij_limits_done = True

            x = x[j1:j2, i1:i2]
            y = y[j1:j2, i1:i2]
            tmp[tmp.keys()[0]] = tmp[tmp.keys()[0]][j1:j2, i1:i2]

            print "  griddata u"
            u = calc.griddata(x, y, tmp[tmp.keys()[0]].real, x0, y0)
            print "  griddata v"
            v = calc.griddata(x, y, tmp[tmp.keys()[0]].imag, x0, y0)
            out[tmp.keys()[0]] = u + 1.0j * v
            info += "#" + d.strftime("%Y%m%d")

    new_wind_info = "blended+quikscat at days: " + info
    update_wind(fname, out, new_wind_info, **kargs)
Exemplo n.º 8
0
def data2romsblk(data, grd, **kargs):
    quiet = True
    keepOriginal = 2  # if 2, will keep data only inside rt.grid_vicinity rectange
    # if 1, all original data is kept
    Margin = 4  # for griddata and for original data to keep

    for k in kargs.keys():
        if k == 'quiet': quiet = kargs[k]
        elif k.lower().startswith('keepor'): keepOriginal = kargs[k]
        elif k == 'margin': Margin = kargs[k]

    g = roms.Grid(grd)

    cond = False
    out = {}

    for vname in data.keys():
        if vname.startswith('INFO') or vname in 'xy': continue
        # vnames starting with INFO are an info key, without data

        if not quiet: print('  interp %s' % vname)

        if cond is False:
            cond, inds = rt.grid_vicinity(grd,
                                          data['x'],
                                          data['y'],
                                          margin=Margin,
                                          rect=True,
                                          retinds=True)
            i1, i2, j1, j2 = inds

        out[vname] = calc.griddata(data['x'][cond],
                                   data['y'][cond],
                                   data[vname][cond],
                                   g.lon,
                                   g.lat,
                                   extrap=True)

        if keepOriginal:
            # keep original data:
            if keepOriginal == 1:
                out[vname +
                    '_original'] = data[vname]  # keep all original data
            elif keepOriginal == 2:
                out[vname + '_original'] = data[vname][
                    j1:j2, i1:i2]  # keep data only inside vicinity rectangle

            if not out.has_key('x_original'):
                if keepOriginal == 1:
                    out['x_original'] = data['x']
                    out['y_original'] = data['y']
                elif keepOriginal == 2:
                    out['x_original'] = data['x'][j1:j2, i1:i2]
                    out['y_original'] = data['y'][j1:j2, i1:i2]

    # about wind:
    if not quiet: print(' --> rot U,V wind and U,V wind stress')
    angle = g.use('angle')
    out['uwnd'], out['vwnd'] = calc.rot2d(out['uwnd'], out['vwnd'], angle)
    out['sustr'], out['svstr'] = calc.rot2d(out['sustr'], out['svstr'], angle)

    out['sustr'] = rt.rho2uvp(out['sustr'], 'u')
    out['svstr'] = rt.rho2uvp(out['svstr'], 'v')

    return out
Exemplo n.º 9
0
def update_wind(fname,data,new_wind_info,**kargs):
  '''
  new_wind_info will be added to fname as global attribute
  (ex: 'new wind from database xxx')

  '''
  quiet  = False
  Margin = 4 # for griddata and for original data to keep
  grd    = False
  keepOriginal = 2

  for k in kargs.keys():
    if   k=='quiet':  quiet=kargs[k]
    elif k=='margin': Margin=kargs[k]
    elif k=='grid':   grd=kargs[k]
    elif k.lower().startswith('keepor'): keepOriginal=kargs[k]

  if not grd: grd=netcdf.fatt(fname,'grd_file')
  g=roms.Grid(grd)

  x0=data['x']
  y0=data['y']
  if x0.ndim==1: x0,y0=np.meshgrid(x0,y0)
  dates=data.keys()[:]
  dates.remove('x')
  dates.remove('y')
  dates=np.array(dates)


  # interp in time:
  time=netcdf.nctime(fname,'time')
  cond=False
  tind=-1
  fob=gennc.GenBlk(fname,grd)
  for t in time:
    newWind={}
    tind+=1

    I,=np.where(dates==t)
    if I.size:
      I=I[0]
      uv=data[dates[I]]
      if not quiet: print(t,dates[I])
    else:
      i1,=np.where(dates>t)
      i0,=np.where(dates<t)
      if i0.size and i1.size:
        i1=i1[0]
        i0=i0[-1]
        d0=t-dates[i0]
        d1=dates[i1]-t
        d0=d0.days+d0.seconds/86400.
        d1=d1.days+d1.seconds/86400.
        uv=(data[dates[i0]]*d1+data[dates[i1]]*d0)/(d0+d1)
        if not quiet: print(t,dates[i0],dates[i1], d0,d1)
      elif not i1.size:
        uv=data[dates[-1]]
        if not quiet: print(t,dates[-1])
      elif not i0.size:
        uv=data[dates[0]]
        if not quiet: print(t,dates[0])

    # interp to grid:
    if cond is False:
      cond,inds=rt.grid_vicinity(grd,x0,y0,margin=Margin,rect=True,retinds=True)
      i1,i2,j1,j2=inds

    if not quiet: print(' --> inter uv %s' % t.isoformat(' '))
    u=calc.griddata(x0[cond],y0[cond],uv.real[cond],g.lon,g.lat,extrap=True)
    v=calc.griddata(x0[cond],y0[cond],uv.imag[cond],g.lon,g.lat,extrap=True)


    # rotate wind, calc stress:
    if not quiet: print(' --> rot U,V wind and U,V wind stress')
    wspd=np.sqrt(u**2+v**2)
    sustr,svstr=air_sea.wind_stress(u,v)
    angle=g.use('angle')
    uwnd,vwnd=calc.rot2d(u,v,angle)
    sustr,svstr=calc.rot2d(sustr,svstr,angle)
    sustr=rt.rho2uvp(sustr,'u')
    svstr=rt.rho2uvp(svstr,'v')

    # update wind data:
    newWind['date']  = t
    newWind['uwnd']  = uwnd
    newWind['vwnd']  = vwnd
    newWind['sustr'] = sustr
    newWind['svstr'] = svstr
    newWind['wspd']  = wspd
    # original xy:
    if tind==0:
      newWind['attr']={'new_wind_info':new_wind_info}
      if not quiet: print(' --> original xy')
      if keepOriginal==1:
        newWind['x_wind']=x0
        newWind['y_wind']=y0
      elif keepOriginal==2:
        newWind['x_wind']=x0[j1:j2,i1:i2]
        newWind['y_wind']=y0[j1:j2,i1:i2]


    # add to file:
    if not quiet: print(' --> adding to file')
    fob.update_wind(newWind,quiet=quiet)
    if not quiet: print('')
Exemplo n.º 10
0
def update_wind_blended2(fname,datapaths,**kargs):
  '''
  In days without blended data will try to use quikscat data
  '''
  from okean.datasets import quikscat
  from okean.datasets import blended_wind
  a=blended_wind.WINDData(datapaths[0])
  b=quikscat.WINDData(datapaths[1])

  time=netcdf.nctime(fname,'time')
  date0=dts.next_date(time[0],-1)
  date1=dts.next_date(time[-1],+2)

  data=a.data(date0,date1)

  # limit are... otherwise, quikscat interp will be very slow!
  grd=netcdf.fatt(fname,'grd_file')
  import os
  if not os.path.isfile(grd): grd=kargs['grd']
  cond,inds=rt.grid_vicinity(grd,data['x'],data['y'],margin=5,rect=True,retinds=True)
  i1,i2,j1,j2=inds
  for d in data.keys():
    if   d == 'x': data[d]=data[d][i1:i2]
    elif d == 'y': data[d]=data[d][j1:j2]
    else: data[d]=data[d][j1:j2,i1:i2]


  # check for missing days:
  time0=data.keys()
  x0=data['x']
  y0=data['y']
  x0,y0=np.meshgrid(x0,y0)
  time0.remove('x')
  time0.remove('y')

  out=OrderedDict()
  out['x']=x0
  out['y']=y0
  info=''
  qs_ij_limits_done=False
  for d in dts.drange(date0,date1):
    found=0
    for t in time0:
      if (t.year,t.month,t.day)==(d.year,d.month,d.day):
        print('==> blended : ',t)
        out[t]=data[t]
        found=1

    if not found: # use quikscat:
      print('==> quikscat : ',d.strftime('%Y-%m-%d'))
      tmp= b.data(d,dts.next_date(d))
      if not tmp.has_key('x'): continue
      x,y=tmp['x'],tmp['y']
      x,y=np.meshgrid(x,y)

      # reduce qs data:
      if not qs_ij_limits_done:
        i1,i2,j1,j2=calc.ij_limits(x,y,[x0.min(),x0.max()],[y0.min(),y0.max()])
        qs_ij_limits_done=True

      x=x[j1:j2,i1:i2]
      y=y[j1:j2,i1:i2]
      tmp[tmp.keys()[0]]=tmp[tmp.keys()[0]][j1:j2,i1:i2]


      print('  griddata u')
      u=calc.griddata(x,y,tmp[tmp.keys()[0]].real,x0,y0)
      print('  griddata v')
      v=calc.griddata(x,y,tmp[tmp.keys()[0]].imag,x0,y0)
      out[tmp.keys()[0]]=u+1.j*v
      info+='#'+d.strftime('%Y%m%d')


  new_wind_info='blended+quikscat at days: '+info
  update_wind(fname,out,new_wind_info,**kargs)
Exemplo n.º 11
0
def data2romsblk(data,grd,**kargs):
  quiet=kargs.get('quiet',True)
  Margin=kargs.get('margin',4) # for griddata and for original data to keep
                               # if margin is 'no calc', no extraction of
                               # a domain portion is done

  # about projection:
  # - if auto will use grid default projection
  # - if False, no projection is used
  # - any Basemap projection can be used
  proj=kargs.get('proj','auto')

  # about origina data to keep:
  # - if 2, will keep data only inside rt.grid_vicinity rectange
  # - if 1, all original data is kept
  keepOriginal = 2
  for k in kargs:
    if k.lower().startswith('keepor'): keepOriginal=kargs[k]

  if cb.isstr(grd): g=roms.Grid(grd)
  else: g=grd

  if proj=='auto': proj=g.get_projection()

  if proj:
    data_x,data_y=proj(data['x'],data['y'])
    grd_x,grd_y=proj(g.lon,g.lat)
  else:
    data_x,data_y=data['x'],data['y']
    grd_x,grd_y=g.lon,g.lat

  cond=False
  out={}

  for vname in data:
    if vname.startswith('INFO') or vname in 'xy': continue
    # vnames starting with INFO are an info key, without data

    if not quiet: print('  interp %s' % vname)

    if cond is False:
      if Margin=='no calc':
        cond=np.ones_like(data['x'],'bool')
        i1,i1=0,0
        j2,i2=data['x'].shape
      else:
        cond,inds=rt.grid_vicinity(grd,data['x'],data['y'],
                                   margin=Margin,rect=True,retinds=True)
        i1,i2,j1,j2=inds

    out[vname]=calc.griddata(data_x[cond],data_y[cond],
                           data[vname][cond],grd_x,grd_y,extrap=True)

    if keepOriginal:
      # keep original data:
      if keepOriginal==1:
        out[vname+'_original']=data[vname] # keep all original data
      elif keepOriginal==2:
        out[vname+'_original']=data[vname][j1:j2,i1:i2] # keep data only inside vicinity rectangle

      if 'x_original' not in out:
        if keepOriginal==1:
          out['x_original']=data['x']
          out['y_original']=data['y']
        elif keepOriginal==2:
          out['x_original']=data['x'][j1:j2,i1:i2]
          out['y_original']=data['y'][j1:j2,i1:i2]

  # about wind:
  if not quiet: print(' --> rot U,V wind and U,V wind stress')
  out['uwnd'],out['vwnd']=calc.rot2d(out['uwnd'],out['vwnd'],g.angle)
  out['sustr'],out['svstr']=calc.rot2d(out['sustr'],out['svstr'],g.angle)

  out['sustr']=rt.rho2uvp(out['sustr'],'u')
  out['svstr']=rt.rho2uvp(out['svstr'],'v')

  return out