Esempio n. 1
0
def wrf_file_data(file, quiet=False):
    '''
  WRF data for ROMS

  '''

    out = {}

    # time:
    if not quiet: print(' --> get time')
    time = read_time(file)

    out['time'] = time

    # lon,lat:
    if not quiet: print(' --> reading x,y')
    x = netcdf.use(file, 'XLONG', Time=0)  #**{'0': 0})
    y = netcdf.use(file, 'XLAT', Time=0)  #**{'0': 0})

    # tair [K-->C]
    if not quiet: print(' --> T air')
    tair = netcdf.use(file, 'T2') - 273.15
    out['tair'] = Data(x, y, tair, 'Celsius')

    # R humidity [kg/kg --> 0--1]
    if not quiet: print(' --> R humidity from QV at 2m')
    wv = netcdf.use(file, 'Q2')  # water vapor mixing ratio at 2m
    rhum = wv / air_sea.qsat(tair)
    rhum[rhum > 1] = 1
    out['rhum'] = Data(x, y, rhum, '0--1')

    # surface pressure [Pa]
    if not quiet: print(' --> Surface pressure')
    pres = netcdf.use(file, 'PSFC')
    out['pres'] = Data(x, y, pres, 'Pa')

    # P rate [mm --> cm day-1]
    if not quiet: print(' --> P rate (rainc+rainnc)')
    rainc = netcdf.use(file, 'RAINC')
    rainnc = netcdf.use(file, 'RAINNC')
    prate = rainc + rainnc
    if not quiet: print('      accum2avg...')
    prate = accum2avg(prate, dt=time[1] - time[0])  # mm s-1
    conv = 0.1 * 86400  # from mm s-1      --> cm day-1
    prate = prate * conv  # cm day-1
    prate[prate < 0] = 0  # interpolation errors may result in negative rain!
    out['prate'] = Data(x, y, prate, 'cm day-1')

    # LW, SW, latent, sensible signs:
    # positive (downward flux, heating) or negative (upward flux, cooling)
    #https://www.myroms.org/forum/viewtopic.php?f=1&t=2621

    # Net shortwave flux  [W m-2]
    if not quiet: print(' --> Net shortwave flux')
    sw_down = netcdf.use(file, 'SWDOWN')
    albedo = netcdf.use(file, 'ALBEDO')
    sw_net = sw_down * (1 - albedo)
    out['radsw'] = Data(x, y, sw_net, 'W m-2', info='positive downward')

    # Net longwave flux  [W m-2]
    if not quiet: print(' --> Net longwave flux')
    lw_down = netcdf.use(file, 'GLW')  # positive
    # sst needed:
    if not quiet: print('     --> SST for LW up')
    sst = netcdf.use(file, 'SST')  # K
    lw_net = air_sea.lwhf(sst, lw_down)  # positive down
    # here vars have roms-agrif signs --> radlw is positive upward!
    #conversion to ROMS is done in surface.py
    out['radlw'] = Data(x, y, -lw_net, 'W m-2', info='positive upward')
    out['dlwrf'] = Data(x, y, -lw_down, 'W m-2', info='positive upward')

    # U and V wind speed 10m
    if not quiet: print(' --> U and V wind')
    uwnd = netcdf.use(file, 'U10')
    vwnd = netcdf.use(file, 'V10')
    if not quiet: print(' --> calc wind speed and stress')
    speed = np.sqrt(uwnd**2 + vwnd**2)
    taux, tauy = air_sea.wind_stress(uwnd, vwnd)

    out['wspd'] = Data(x, y, speed, 'm s-1')
    out['uwnd'] = Data(x, y, uwnd, 'm s-1')
    out['vwnd'] = Data(x, y, vwnd, 'm s-1')
    out['sustr'] = Data(x, y, taux, 'Pa')
    out['svstr'] = Data(x, y, tauy, 'Pa')

    # Cloud cover [0--1]:
    if not quiet:
        print(' --> Cloud cover for LONGWAVE. Use LONGWAVE_OUT instead...')
    if 0:
        pass


# next code is wrong! If cloud cover is really needed, it needs to be calculated using wrfpost.
# See http://www2.mmm.ucar.edu/wrf/users/docs/user_guide/users_guide_chap8.html#_ARWpost_1
#
#  if 'CLDFRA' in netcdf.varnames(file):
#    clouds=netcdf.use(file,'CLDFRA').sum(-3)
#    clouds=np.where(clouds>1,1,clouds)
    else:
        if not quiet:
            print('CLDFRA not found!! Using SST and air_sea.cloud_fraction')
        sst = netcdf.use(file, 'SST') - 273.15
        clouds = air_sea.cloud_fraction(lw_net, sst, tair, rhum, Wtype='net')
        clouds[clouds < 0] = 0
        clouds[clouds > 1] = 1

    out['cloud'] = Data(x, y, clouds, 'fraction (0--1)')

    return out
Esempio n. 2
0
def gfs_file_data(fname, xlim=False, ylim=False, quiet=False):
    '''
  Returns bulk data from one GFS file
  '''
    # the way to ectract differes if using pygrib or grib2 ! so check first:
    try:
        import pygrib
        isPygrib = True
    except:
        isPygrib = False

    out = {}

    # T air 2m [K->C]
    if not quiet: print(' --> T air')
    if isPygrib:
        #x,y,tair=gribu.getvar(fname,'temperature',tags=(':2 metre',),lons=xlim,lats=ylim)
        #newest gribu:
        x, y, tair = gribu.getvar(fname, '2t', lons=xlim, lats=ylim)
    else:
        x, y, tair = gribu.getvar(fname,
                                  'temperature',
                                  tags=(':2 m', 'TMP'),
                                  lons=xlim,
                                  lats=ylim)
    tair = tair - 273.15
    out['tair'] = Data(x, y, tair, 'C')

    # R humidity 2m [%-->0--1]
    if not quiet: print(' --> R humidity')
    if 0:
        # kg/kg
        x, y, rhum = gribu.getvar(fname,
                                  'humidity',
                                  tags=(':2 m', 'kg'),
                                  lons=xlim,
                                  lats=ylim)
        rhum = rhum / air_sea.qsat(tair)
        rhum = np.where(rhum > 1.0, 1.0, rhum)
    else:
        # %
        #x,y,rhum=gribu.getvar(fname,'humidity',tags=('2 m','%'),lons=xlim,lats=ylim)
        x, y, rhum = gribu.getvar(fname, '2r', lons=xlim, lats=ylim)
        rhum = rhum / 100.

    out['rhum'] = Data(x, y, rhum, '0--1')

    # surface pressure [Pa]
    if not quiet: print(' --> Surface pressure')
    #x,y,pres=gribu.getvar(fname,'pressure',tags='surface',lons=xlim,lats=ylim)
    x, y, pres = gribu.getvar(fname, 'sp', lons=xlim, lats=ylim)
    out['pres'] = Data(x, y, pres, 'Pa')

    # P rate [kg m-2 s-1 -> cm/d]
    if not quiet: print(' --> P rate')
    #x,y,prate=gribu.getvar(fname,'precipitation rate',lons=xlim,lats=ylim)
    x, y, prate = gribu.getvar(fname, 'prate', lons=xlim, lats=ylim)
    # Conversion kg m^-2 s^-1  to cm/day
    prate = prate * 86400 * 100 / 1000.
    prate = np.where(abs(prate) < 1.e-4, 0, prate)
    out['prate'] = Data(x, y, prate, 'cm/d')

    # Net shortwave flux  [W/m^2]
    if not quiet: print(' --> Net shortwave flux')
    if not quiet: print('       SW down')
    if isPygrib:
        #x,y,sw_down = gribu.getvar(fname,'',tags='Downward short-wave radiation flux',lons=xlim,lats=ylim)
        x, y, sw_down = gribu.getvar(fname, 'dswrf', lons=xlim, lats=ylim)
    else:
        x, y, sw_down = gribu.getvar(fname,
                                     'downward short-wave',
                                     lons=xlim,
                                     lats=ylim)

    if not quiet: print('       SW up')
    #x,y,sw_up   = gribu.getvar(fname,'',tags='Upward short-wave radiation flux',lons=xlim,lats=ylim)
    x, y, sw_up = gribu.getvar(fname, 'uswrf', lons=xlim, lats=ylim)
    if sw_up is False:
        if not quiet: print('       SW up not found: using albedo')
        #x,y,albedo  = gribu.getvar(fname,'albedo',lons=xlim,lats=ylim)
        x, y, albedo = gribu.getvar(fname, 'al', lons=xlim, lats=ylim)
        albedo = albedo / 100.
        sw_net = sw_down * (1 - albedo)
    else:
        sw_net = sw_down - sw_up

    sw_net = np.where(sw_net < 1.e-10, 0, sw_net)
    out['radsw'] = Data(x, y, sw_net, 'W m-2', info='positive downward')

    # Net longwave flux  [W/m^2]
    if not quiet: print(' --> Net longwave flux')
    if not quiet: print('       LW down')
    if isPygrib:
        #x,y,lw_down = gribu.getvar(fname,'',tags='Downward long-wave radiation flux',lons=xlim,lats=ylim)
        x, y, lw_down = gribu.getvar(fname, 'dlwrf', lons=xlim, lats=ylim)
    else:
        x, y, lw_down = gribu.getvar(fname,
                                     'downward long-wave',
                                     lons=xlim,
                                     lats=ylim)

    if not quiet: print('       LW up')
    #x,y,lw_up   = gribu.getvar(fname,'',tags='Upward long-wave radiation flux',lons=xlim,lats=ylim)
    x, y, lw_up = gribu.getvar(fname, 'ulwrf', lons=xlim, lats=ylim)
    if lw_up is False:
        if not quiet: print('       LW up not found: using sst')
        if isPygrib:
            #x,y,sst=gribu.getvar(fname,'temperature',tags='surface',lons=xlim,lats=ylim) # K
            x, y, sst = gribu.getvar(fname, 't', lons=xlim, lats=ylim)  # K
        else:
            x, y, sst = gribu.getvar(fname,
                                     'temperature',
                                     tags='water surface',
                                     lons=xlim,
                                     lats=ylim)  # K

        lw_net = air_sea.lwhf(sst, lw_down)
        lw_up = lw_down - lw_net
    else:
        lw_net = lw_down - lw_up

    # ROMS convention: positive upward
    # GFS convention: positive downward --> * (-1)
    lw_net = np.where(np.abs(lw_net) < 1.e-10, 0, lw_net)
    out['radlw'] = Data(x, y, -lw_net, 'W m-2', info='positive upward')

    # downward lw:
    out['dlwrf'] = Data(x, y, -lw_down, 'W m-2', info='negative... downward')

    # U and V wind speed 10m
    if not quiet: print(' --> U and V wind')
    #x,y,uwnd  = gribu.getvar(fname,'u',tags=':10 m',lons=xlim,lats=ylim)
    #x,y,vwnd  = gribu.getvar(fname,'v',tags=':10 m',lons=xlim,lats=ylim)
    x, y, uwnd = gribu.getvar(fname, '10u', lons=xlim, lats=ylim)
    x, y, vwnd = gribu.getvar(fname, '10v', lons=xlim, lats=ylim)

    if not quiet: print(' --> calc wind speed and stress')
    speed = np.sqrt(uwnd**2 + vwnd**2)
    taux, tauy = air_sea.wind_stress(uwnd, vwnd)

    out['wspd'] = Data(x, y, speed, 'm s-1')
    out['uwnd'] = Data(x, y, uwnd, 'm s-1')
    out['vwnd'] = Data(x, y, vwnd, 'm s-1')
    out['sustr'] = Data(x, y, taux, 'Pa')
    out['svstr'] = Data(x, y, tauy, 'Pa')

    # Cloud cover [0--100 --> 0--1]:
    if not quiet: print(' --> Cloud cover')
    #x,y,clouds  = gribu.getvar(fname,'cloud cover',lons=xlim,lats=ylim)
    x, y, clouds = gribu.getvar(fname, 'tcc', lons=xlim, lats=ylim)
    if clouds is False:
        if not quiet: print('CALC clouds from LW,TAIR,TSEA and RH')
        # first get sst (maybe already done to calc lw_up)
        try:
            sst
        except:
            if not quiet: print('  get TSEA')
            if isPygrib:
                #x,y,sst=gribu.getvar(fname,'temperature',tags='surface',lons=xlim,lats=ylim) # K
                x, y, sst = gribu.getvar(fname, 't', lons=xlim, lats=ylim)  # K
            else:
                x, y, sst = gribu.getvar(fname,
                                         'temperature',
                                         tags='water surface',
                                         lons=xlim,
                                         lats=ylim)  # K

        clouds = air_sea.cloud(lw_net, sst - 273.15, tair, rhum, 'net')
    else:
        clouds = clouds / 100.

    out['cloud'] = Data(x, y, clouds, 'fraction (0--1)')

    return out
Esempio n. 3
0
def gfs_file_data(fname,xlim=False,ylim=False,quiet=False):
  '''
  Returns bulk data from one GFS file
  '''
  # the way to ectract differes if using pygrib or grib2 ! so check first:
  try:
    import pygrib
    isPygrib=True
  except: isPygrib=False


  out={}

  # T air 2m [K->C]
  if not quiet: print(' --> T air')
  if isPygrib:
    #x,y,tair=gribu.getvar(fname,'temperature',tags=(':2 metre',),lons=xlim,lats=ylim)
    #newest gribu:
    x,y,tair=gribu.getvar(fname,'2t',lons=xlim,lats=ylim)
  else:
    x,y,tair=gribu.getvar(fname,'temperature',tags=(':2 m','TMP'),lons=xlim,lats=ylim)
  tair=tair-273.15
  out['tair']=Data(x,y,tair,'C')

  # R humidity 2m [%-->0--1]
  if not quiet: print(' --> R humidity')
  if 0:
    # kg/kg
    x,y,rhum=gribu.getvar(fname,'humidity',tags=(':2 m','kg'),lons=xlim,lats=ylim)
    rhum=rhum/air_sea.qsat(tair)
    rhum=np.where(rhum>1.0,1.0,rhum)
  else:
    # %
    #x,y,rhum=gribu.getvar(fname,'humidity',tags=('2 m','%'),lons=xlim,lats=ylim)
    x,y,rhum=gribu.getvar(fname,'2r',lons=xlim,lats=ylim)
    rhum=rhum/100.

  out['rhum']=Data(x,y,rhum,'0--1')

  # surface pressure [Pa]
  if not quiet: print(' --> Surface pressure')
  #x,y,pres=gribu.getvar(fname,'pressure',tags='surface',lons=xlim,lats=ylim)
  x,y,pres=gribu.getvar(fname,'sp',lons=xlim,lats=ylim)
  out['pres']=Data(x,y,pres,'Pa')

  # P rate [kg m-2 s-1 -> cm/d]
  if not quiet: print(' --> P rate')
  #x,y,prate=gribu.getvar(fname,'precipitation rate',lons=xlim,lats=ylim)
  x,y,prate=gribu.getvar(fname,'prate',lons=xlim,lats=ylim)
  # Conversion kg m^-2 s^-1  to cm/day
  prate=prate*86400*100/1000.
  prate=np.where(abs(prate)<1.e-4,0,prate)
  out['prate']=Data(x,y,prate,'cm/d')

  # Net shortwave flux  [W/m^2]
  if not quiet: print(' --> Net shortwave flux')
  if not quiet: print('       SW down')
  if isPygrib:
    #x,y,sw_down = gribu.getvar(fname,'',tags='Downward short-wave radiation flux',lons=xlim,lats=ylim)
    x,y,sw_down = gribu.getvar(fname,'dswrf',lons=xlim,lats=ylim)
  else:
    x,y,sw_down = gribu.getvar(fname,'downward short-wave',lons=xlim,lats=ylim)

  if not quiet: print('       SW up')
  #x,y,sw_up   = gribu.getvar(fname,'',tags='Upward short-wave radiation flux',lons=xlim,lats=ylim)
  x,y,sw_up   = gribu.getvar(fname,'uswrf',lons=xlim,lats=ylim)
  if sw_up is False:
    if not quiet: print('       SW up not found: using albedo')
    #x,y,albedo  = gribu.getvar(fname,'albedo',lons=xlim,lats=ylim)
    x,y,albedo  = gribu.getvar(fname,'al',lons=xlim,lats=ylim)
    albedo=albedo/100.
    sw_net=sw_down*(1-albedo)
  else:
    sw_net=sw_down-sw_up

  sw_net=np.where(sw_net<1.e-10,0,sw_net)
  out['radsw']=Data(x,y,sw_net,'W m-2',info='positive downward')

  # Net longwave flux  [W/m^2]
  if not quiet: print(' --> Net longwave flux')
  if not quiet: print('       LW down')
  if isPygrib:
    #x,y,lw_down = gribu.getvar(fname,'',tags='Downward long-wave radiation flux',lons=xlim,lats=ylim)
    x,y,lw_down = gribu.getvar(fname,'dlwrf',lons=xlim,lats=ylim)
  else:
    x,y,lw_down = gribu.getvar(fname,'downward long-wave',lons=xlim,lats=ylim)

  if not quiet: print('       LW up')
  #x,y,lw_up   = gribu.getvar(fname,'',tags='Upward long-wave radiation flux',lons=xlim,lats=ylim)
  x,y,lw_up   = gribu.getvar(fname,'ulwrf',lons=xlim,lats=ylim)
  if lw_up is False:
    if not quiet: print('       LW up not found: using sst')
    if isPygrib:
      #x,y,sst=gribu.getvar(fname,'temperature',tags='surface',lons=xlim,lats=ylim) # K
      x,y,sst=gribu.getvar(fname,'t',lons=xlim,lats=ylim) # K
    else:
      x,y,sst=gribu.getvar(fname,'temperature',tags='water surface',lons=xlim,lats=ylim) # K

    lw_net=air_sea.lwhf(sst,lw_down)
    lw_up=lw_down-lw_net
  else:
    lw_net=lw_down-lw_up

  # ROMS convention: positive upward
  # GFS convention: positive downward --> * (-1)
  lw_net=np.where(np.abs(lw_net)<1.e-10,0,lw_net)
  out['radlw']=Data(x,y,-lw_net,'W m-2',info='positive upward')

  # downward lw:
  out['dlwrf']=Data(x,y,-lw_down,'W m-2',info='negative... downward')


  # U and V wind speed 10m
  if not quiet: print(' --> U and V wind')
  #x,y,uwnd  = gribu.getvar(fname,'u',tags=':10 m',lons=xlim,lats=ylim)
  #x,y,vwnd  = gribu.getvar(fname,'v',tags=':10 m',lons=xlim,lats=ylim)
  x,y,uwnd  = gribu.getvar(fname,'10u',lons=xlim,lats=ylim)
  x,y,vwnd  = gribu.getvar(fname,'10v',lons=xlim,lats=ylim)

  if not quiet: print(' --> calc wind speed and stress')
  speed = np.sqrt(uwnd**2+vwnd**2)
  taux,tauy=air_sea.wind_stress(uwnd,vwnd)

  out['wspd']=Data(x,y,speed,'m s-1')
  out['uwnd']=Data(x,y,uwnd,'m s-1')
  out['vwnd']=Data(x,y,vwnd,'m s-1')
  out['sustr']=Data(x,y,taux,'Pa')
  out['svstr']=Data(x,y,tauy,'Pa')


  # Cloud cover [0--100 --> 0--1]:
  if not quiet: print(' --> Cloud cover')
  #x,y,clouds  = gribu.getvar(fname,'cloud cover',lons=xlim,lats=ylim)
  x,y,clouds  = gribu.getvar(fname,'tcc',lons=xlim,lats=ylim)
  if clouds is False:
    if not quiet: print('CALC clouds from LW,TAIR,TSEA and RH')
    # first get sst (maybe already done to calc lw_up)
    try: sst
    except:
      if not quiet: print('  get TSEA')
      if isPygrib:
        #x,y,sst=gribu.getvar(fname,'temperature',tags='surface',lons=xlim,lats=ylim) # K
        x,y,sst=gribu.getvar(fname,'t',lons=xlim,lats=ylim) # K
      else:
        x,y,sst=gribu.getvar(fname,'temperature',tags='water surface',lons=xlim,lats=ylim) # K

    clouds=air_sea.cloud(lw_net,sst-273.15,tair,rhum,'net')
  else: clouds=clouds/100.

  out['cloud']=Data(x,y,clouds,'fraction (0--1)')

  return out
Esempio n. 4
0
File: wrf.py Progetto: jcmt/okean
def wrf_file_data(file,quiet=False):
  '''
  WRF data for ROMS

  '''

  out={}

  # time:
  if not quiet: print ' --> get time'
  time=read_time(file)

  out['time']=time

  # lon,lat:
  if not quiet: print ' --> reading x,y'
  x=netcdf.use(file,'XLONG',**{'0': 0})
  y=netcdf.use(file,'XLAT',**{'0': 0})

  # tair [K-->C]
  if not quiet: print ' --> T air'
  tair=netcdf.use(file,'T2')-273.15
  out['tair']=Data(x,y,tair,'Celsius')

  # R humidity [kg/kg --> 0--1]
  if not quiet: print ' --> R humidity from QV at 2m'
  wv=netcdf.use(file,'Q2') # water vapor mixing ratio at 2m
  rhum=wv/air_sea.qsat(tair)
  rhum[rhum>1]=1
  out['rhum']=Data(x,y,rhum,'0--1')

  # surface pressure [Pa]
  if not quiet: print ' --> Surface pressure'
  pres=netcdf.use(file,'PSFC')
  out['pres']=Data(x,y,pres,'Pa')

  # P rate [mm --> cm day-1]
  if not quiet: print ' --> P rate (rainc+rainnc)'
  rainc  = netcdf.use(file,'RAINC')
  rainnc = netcdf.use(file,'RAINNC')
  prate=rainc+rainnc
  if not quiet: print '      accum2avg...'
  prate=accum2avg(prate,dt=time[1]-time[0]) # mm s-1
  conv= 0.1*86400       # from mm s-1      --> cm day-1
  prate=prate*conv # cm day-1
  prate[prate<0]=0 # interpolation errors may result in negative rain!
  out['prate']=Data(x,y,prate,'cm day-1')

  # LW, SW, latent, sensible signs:
  # positive (downward flux, heating) or negative (upward flux, cooling)
  #https://www.myroms.org/forum/viewtopic.php?f=1&t=2621

  # Net shortwave flux  [W m-2]
  if not quiet: print ' --> Net shortwave flux'
  sw_down=netcdf.use(file,'SWDOWN')
  albedo=netcdf.use(file,'ALBEDO')
  sw_net=sw_down*(1-albedo)
  out['radsw']=Data(x,y,sw_net,'W m-2',info='positive downward')

  # Net longwave flux  [W m-2]
  if not quiet: print ' --> Net longwave flux'
  lw_down=netcdf.use(file,'GLW') # positive
  # sst needed:
  if not quiet: print '     --> SST for LW up'
  sst=netcdf.use(file,'SST') # K
  lw_net = air_sea.lwhf(sst,lw_down) # positive down
  # here vars have roms-agrif signs --> radlw is positive upward!
  #conversion to ROMS is done in surface.py
  out['radlw']=Data(x,y,-lw_net,'W m-2',info='positive upward')
  out['dlwrf']=Data(x,y,-lw_down,'W m-2',info='positive upward')

  # U and V wind speed 10m
  if not quiet: print ' --> U and V wind'
  uwnd=netcdf.use(file,'U10')
  vwnd=netcdf.use(file,'V10')
  if not quiet: print ' --> calc wind speed and stress'
  speed = np.sqrt(uwnd**2+vwnd**2)
  taux,tauy=air_sea.wind_stress(uwnd,vwnd)

  out['wspd']=Data(x,y,speed,'m s-1')
  out['uwnd']=Data(x,y,uwnd,'m s-1')
  out['vwnd']=Data(x,y,vwnd,'m s-1')
  out['sustr']=Data(x,y,taux,'Pa')
  out['svstr']=Data(x,y,tauy,'Pa')

  # Cloud cover [0--1]:
  if not quiet: print ' --> Cloud cover for LONGWAVE. Use LONGWAVE_OUT instead...'
  if 'CLDFRA' in netcdf.varnames(file):
    clouds=netcdf.use(file,'CLDFRA').sum(-3)
    clouds=np.where(clouds>1,1,clouds)
  else:
    if not quiet: print 'CLDFRA not found!! Using SST and air_sea.clouds'
    sst=netcdf.use(f,'SST')
    clouds=air_sea.clouds(lw_net,sst,tair,rhum,Wtype='net')

  out['cloud']=Data(x,y,clouds,'fraction (0--1)')

  return out