Example #1
0
def generateInitialProfileWRF(fstr):
    wrfout  = ncdf('%s' % (fstr))
    hgt    = wrfout.variables['HGT'][0,:,:]
    wlat,wlon = wrfdict.latlon(wrfout)
    z,zs = wrfdict.getheight(wrfout)

    dx = wrfout.DX ; dy = wrfout.DY 

    u = wrfdict.unstagger3d(wrfout.variables['U'][0,:,:,:],2)
    v = wrfdict.unstagger3d(wrfout.variables['V'][0,:,:,:],1)
    w = wrfdict.unstagger3d(wrfout.variables['W'][0,:,:,:],0) 
    T = wrfout.variables['T'][0,:,:,:]+300.0

    avgu  = np.mean(np.mean(u,axis=2),axis=1)
    avgv  = np.mean(np.mean(v,axis=2),axis=1)
    avgw  = np.mean(np.mean(w,axis=2),axis=1)
    avgT  = np.mean(np.mean(T,axis=2),axis=1) 
    avgzs = np.mean(np.mean(zs,axis=2),axis=1)

    avgu  = np.append(np.zeros(1),avgu)
    avgv  = np.append(np.zeros(1),avgv)
    avgw  = np.append(np.zeros(1),avgw)
    avgT  = np.append(avgT[0],avgT)
    avgzs = np.append(np.zeros(1),avgzs)
    nz = np.shape(avgzs)[0]
 
    return avgzs,avgu,avgv,avgw,avgT
Example #2
0
 def getvar(self, var, domain, datestr):
     '''
     Read variable form netCDF file and return array
     '''
     # define and load input file
     input_fn = var + '_d0' + str(domain) + '_' + datestr
     input_file = os.path.join(self.rundir, input_fn)
     ncfile = ncdf(input_file, 'r')
     # read variable and close netCDF file
     tmp = ncfile.variables[var][:]
     ncfile.close()
     return tmp
Example #3
0
def timesteps_wrfout(wrfout):
    '''
  return a list of timesteps (as datetime objects) in a wrfout file
  Input variables:
    - wrfout: path to a wrfout file
  '''
    from netCDF4 import Dataset as ncdf
    from datetime import timedelta
    check_file_exists(wrfout)  # check if wrfout file exists
    # read time information from wrfout file
    ncfile = ncdf(wrfout, format='NETCDF4')
    # minutes since start of simulation, rounded to 1 decimal float
    tvar = [round(nc, 0) for nc in ncfile.variables['XTIME'][:]]
    ncfile.close()
    # get start date from wrfout filename
    time_string = wrfout[-19:-6]
    start_time = return_validate(time_string)
    # times in netcdf file
    time_steps = [start_time + timedelta(minutes=step) for step in tvar]
    return time_steps
Example #4
0
def timesteps_wrfout(wrfout):
  '''
  return a list of timesteps (as datetime objects) in a wrfout file
  Input variables:
    - wrfout: path to a wrfout file
  '''
  from netCDF4 import Dataset as ncdf
  from datetime import timedelta
  check_file_exists(wrfout)  # check if wrfout file exists
  # read time information from wrfout file
  ncfile = ncdf(wrfout, format='NETCDF4')
  # minutes since start of simulation, rounded to 1 decimal float
  tvar = [round(nc,0) for nc in ncfile.variables['XTIME'][:]]
  ncfile.close()
  # get start date from wrfout filename
  time_string = wrfout[-19:-6]
  start_time = return_validate(time_string)
  # times in netcdf file
  time_steps = [start_time + timedelta(minutes=step) for step in tvar]
  return time_steps
Example #5
0
    cdict = {'red': reds, 'green': greens, 'blue': blues}
    
    return clr.LinearSegmentedColormap('my_colormap', cdict, 256)


if __name__ == '__main__':
    # import packages for basemap
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    from netCDF4 import Dataset as ncdf
    
    # setting fig size
    fig = plt.figure(figsize=(10, 6))

    # calling the netCDF of ndvi
    nc = ncdf('dem_test.nc')
    
    # setting variables for basemap
    lat = nc.variables['lat'][:]
    lon = nc.variables['lon'][:]
    elv = nc.variables['elv'][:]
    lon,lat = np.meshgrid(lon,lat)

    # setting up the basemap object            
    m = Basemap(projection='merc',llcrnrlat=lat.min(),urcrnrlat=lat.max(),\
                llcrnrlon=lon.min(),urcrnrlon=lon.max(),lat_ts=28.125,\
                resolution='c')
    
    # plotting the lat and lon coordinates to Basemap projection
    x,y = m(lon,lat)
    
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset as ncdf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime

##############
# part 1
##############

# setting figure size
fig = plt.figure(figsize=(12,8))

# calling the netCDF of ndvi
nc = ncdf('http://apdrc.soest.hawaii.edu:80/dods/public_data/hydrological_data/UDel_water_budget')

# setting variables
time = nc.variables['time'][:]
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
soilm = nc.variables['w150'][599, 0, :, :]
lon,lat = np.meshgrid(lon,lat)

# setting up the basemap object
m = Basemap(projection='laea', width=12000000,\
            height=8000000, resolution='c', lat_ts=50., lat_0=50.,\
            lon_0=-100.)

# plotting the lat and lon coordinates to Basemap projection
x,y = m(lon,lat)
Example #7
0
def write_WRF_to_NCDF(lat,lon,datadir,outputfile,dom=1,
                      prefix='wrfout_d{:02d}_*00'):
    """
    What it does: Given a WRF file and a lat/lon pair, several surface
                  variables and profiles will be extracted and written
                  to a new file.

    Who made it: [email protected] 
    When: 2/4/19
    """

    # Find all wrfout_d0X files to loop over. Expects 1 time per file
    wrfoutf = sorted(glob(os.path.join(datadir,prefix.format(dom))))
    nt = len(wrfoutf) # Number of times

    # Initialize time-series variables; 
    pblh  = np.zeros((nt)); ustr  = np.zeros((nt))
    u10   = np.zeros((nt)); v10   = np.zeros((nt))
    T2    = np.zeros((nt)); TH2   = np.zeros((nt))
    swdwn = np.zeros((nt)); psfc  = np.zeros((nt))
    time  = np.zeros((nt)); wdate = np.zeros((nt))
    hfx   = np.zeros((nt))

    # - - - WRF Data - - -
    for cc,ff in enumerate(wrfoutf): # Loop over all WRF files
        wrfout     = ncdf(ff)
        year,month,day,hour = wrfdict.wrf_times_to_hours(wrfout) # Finds the WRF time
        wdate[cc] = year*10000 + month*100 + day # Generates an integer in form YYYMMDD
        time[cc]  = hour
        if cc == 0: # Initialize 2D vars and gather necessary variables
            poii, poij = wrfdict.latlon_to_ij(wrfout,lat,lon) # i,j location closest to given lat/lon
            z,zs = wrfdict.get_height_at_ind(wrfout,poij,poii) # Get z values at location
            nz = len(zs) # number of heights
            height = zs[:] # tower heights
            # Initialize 2D variables
            u = np.zeros((nt,nz))
            v = np.zeros((nt,nz))
            w = np.zeros((nt,nz))
            T = np.zeros((nt,nz))
            P = np.zeros((nt,nz))
            Q = np.zeros((nt,nz))
        # Load variables from WRF file
        hfx[cc]    = wrfout.variables['HFX'][0,poij,poii]
        pblh[cc]   = wrfout.variables['PBLH'][0,poij,poii]
        psfc[cc]   = wrfout.variables['PSFC'][0,poij,poii]
        ustr[cc]   = wrfout.variables['UST'][0,poij,poii]
        u10[cc]    = wrfout.variables['U10'][0,poij,poii]
        v10[cc]    = wrfout.variables['V10'][0,poij,poii]
        T2[cc]     = wrfout.variables['T2'][0,poij,poii]
        TH2[cc]    = wrfout.variables['TH2'][0,poij,poii]
        swdwn[cc]  = wrfout.variables['SWDOWN'][0,poij,poii]
        # U and V need to be interpolated to cell-center
        u[cc]      = wrfdict.unstagger(wrfout.variables['U'][0,:,poij,poii-1:poii+1],ax=1)[:,0]
        v[cc]      = wrfdict.unstagger(wrfout.variables['V'][0,:,poij-1:poij+1,poii],ax=1)[:,0]
        # W needs to be unstaggered in the vertical direction
        ws         = wrfout.variables['W'][0,:,poij,poii]
        w[cc]      = (ws[1:] + ws[:-1])*0.5
        # T is perturbation temp... need to add 300.0 K
        T[cc]      = wrfout.variables['T'][0,:,poij,poii]+300.0
        # Pressure is perturbation + base
        P[cc]      = wrfout.variables['P'][0,:,poij,poii]+wrfout.variables['PB'][0,:,poij,poii]
        # Mixing ratio of water vapor
        Q[cc]      = wrfout.variables['QVAPOR'][0,:,poij,poii]

    # Write new NetCDF file
    newncdf = ncdf(outputfile.format(dom),'w',format='NETCDF4_CLASSIC')

    # Create time and height dimensions
    newncdf.createDimension('NZ',nz)
    newncdf.createDimension('time',nt)

    # Set global values
    newncdf.location = '({:f}, {:f})'.format(
            wrfout.variables['XLAT'][0,poij,poii], wrfout.variables['XLONG'][0,poij,poii])
    newncdf.elevation = '{:f} m'.format(wrfout.variables['HGT'][0,poij,poii])
    newncdf.description = \
            'Extracted using mmctools.wrf.extract on {:s} from wrfout files located at {:s}'.format(
                str(datetime.now()),datadir)

    # Create new variables
    times    = newncdf.createVariable('Time',np.float64, ('time',))
    dates    = newncdf.createVariable('Date',np.float64, ('time',))
    hgts     = newncdf.createVariable('Height',np.float64, ('NZ',))
    hfxo     = newncdf.createVariable('HFX',np.float64, ('time',))
    pblho    = newncdf.createVariable('PBLH',np.float64, ('time',))
    psfco    = newncdf.createVariable('PSFC',np.float64, ('time',))
    ustro    = newncdf.createVariable('USTAR',np.float64, ('time',))
    u10o     = newncdf.createVariable('U10',np.float64, ('time',))
    v10o     = newncdf.createVariable('V10',np.float64, ('time',))
    T2o      = newncdf.createVariable('T2',np.float64, ('time',))
    TH2o     = newncdf.createVariable('TH2',np.float64, ('time',))
    swdwno   = newncdf.createVariable('SWDOWN',np.float64, ('time',))
    uout     = newncdf.createVariable('U',np.float64, ('time','NZ',))
    vout     = newncdf.createVariable('V',np.float64, ('time','NZ',))
    wout     = newncdf.createVariable('W',np.float64, ('time','NZ',))
    Tout     = newncdf.createVariable('T',np.float64, ('time','NZ',))
    Pout     = newncdf.createVariable('P',np.float64, ('time','NZ',))
    Qout     = newncdf.createVariable('Q',np.float64, ('time','NZ',))

    # Assign data to new variables
    times[:]   = time
    dates[:]   = wdate
    hgts[:]    = height
    hfxo[:]    = hfx
    pblho[:]   = pblh 
    psfco[:]   = psfc 
    ustro[:]   = ustr
    u10o[:]    = u10
    v10o[:]    = v10
    T2o[:]     = T2
    TH2o[:]    = TH2
    swdwno[:]  = swdwn
    uout[:]    = u
    vout[:]    = v
    wout[:]    = w
    Tout[:]    = T
    Pout[:]    = P
    Qout[:]    = Q

    # Write and close the new NetCDF file
    newncdf.close()
Example #8
0
def write_combined_data_netcdf(data, stationid, lon, lat, elevation):
  '''
  description
  '''
  from netCDF4 import Dataset as ncdf
  import netcdftime
  from datetime import datetime
  from dateutil import tz
  from numpy import zeros
  from numpy import nan as npnan
  from numpy import dtype
  import time
  ncfile = ncdf('output'+str(stationid)+'.nc', 'w', format='NETCDF4')
  # description of the file
  ncfile.description = 'KNMI ' + str(stationid)
  ncfile.history = 'Created ' + time.ctime(time.time())
  # create time dimension
  timevar = ncfile.createDimension('time', None)
  # create lon/lat dimensions
  lonvar = ncfile.createDimension('longitude', 1)
  latvar = ncfile.createDimension('latitude', 1)
  # elevation
  elvar = ncfile.createDimension('elevation', 1)
  # inititalize time axis
  timeaxis = [int(round(netcdftime.date2num(data['datetime'][idx], units='minutes since 2010-01-01 00:00:00',
                                 calendar='gregorian'))) for idx in range(0,len(data['datetime']))]
  # netcdf time variable UTC
  timevar = ncfile.createVariable('time', 'i4', ('time',),
                                  zlib=True)
  timevar[:] = timeaxis
  timevar.units = 'minutes since 2010-01-01 00:00:00'
  timevar.calendar = 'gregorian'
  timevar.standard_name = 'time'
  timevar.long_name = 'time in UTC'

  # lon/lat variables
  lonvar = ncfile.createVariable('longitude',dtype('float32').char,('longitude',))
  lonvar.units = 'degrees_east'
  lonvar.axis = 'X'
  lonvar.standard_name = 'longitude'
  lonvar[:] = lon
  latvar = ncfile.createVariable('latitude',dtype('float32').char,('latitude',))
  latvar.units = 'degrees_north'
  latvar.axis = 'Y'
  latvar.standard_name = 'latitude'
  latvar[:] = lat

  # elevation variable
  elvar = ncfile.createVariable('elevation', dtype('float32').char, ('elevation',))
  elvar.units = 'meter'
  elvar.axis = 'Z'
  elvar.standard_name = 'elevation'
  elvar[:] = elevation
  
  # create other variables in netcdf file
  for variable in data.keys():
    if variable not in ['YYYMMDD', 'Time', '<br>', 'datetime', '# STN', None]:
      # add variables in netcdf file
      # convert strings to npnan if array contains numbers
      if True in [is_number(c)
        for c in data[variable]]:
          data[variable] = [npnan if isinstance(
            fitem(c), str) else fitem(c) for c in data[
              variable]]
      # check if variable is a string
      if not isinstance(data[variable][1], str):
          # fill variable
          variableName = variable
          values = ncfile.createVariable(
            variableName, type(data[variable][1]),
            ('time',), zlib=True, fill_value=-999)
      else:
        # string variables cannot have fill_value
        values = ncfile.createVariable(
          variable, type(data[variable][1]),
          ('time',), zlib=True)
      try:  # fill variable
        values[:] = data[variable][:]
      except IndexError:
        # for strings the syntax is slightly different
        values = data[variable][:]
Example #9
0
    if smoothed:
        smooth_str = 'smooth'
    else:
        smooth_str = 'raw'

    if (new_data != 'OVERWRITE') and (new_data != 'FILL'):
        new_file = '{}{}/{}'.format(new_dir, smooth_str,
                                    met_file.split('/')[-1])
    else:
        new_file = '{}{}'.format(new_dir, met_file.split('/')[-1])
    print(new_file)
    if path.exists(new_file):
        print('skipping...')
    else:

        met = ncdf(met_file, 'r')

        m_lat = met.variables['XLAT_M'][0, :, :]
        m_lon = met.variables['XLONG_M'][0, :, :]
        if reanalysis == 'GFS':
            m_sst = met.variables['SKINTEMP'][0, :, :]
        else:
            m_sst = met.variables['SST'][0, :, :]
        m_mask = met.variables['LANDMASK'][0, :, :]
        met_timeb = met.variables['Times'][:].data[0]
        met_dx = met.DX
        met_dy = met.DY
        met.close()

        #        if reanalysis != 'ERA5':
        #if new_data != 'OVERWRITE':
Example #10
0
pblh = np.zeros((nt))
ustr = np.zeros((nt))
u10 = np.zeros((nt))
v10 = np.zeros((nt))
T2 = np.zeros((nt))
TH2 = np.zeros((nt))
swdwn = np.zeros((nt))
psfc = np.zeros((nt))
time = np.zeros((nt))
wdate = np.zeros((nt))
hfx = np.zeros((nt))

# - - - WRF Data - - -
cc = 0  # Start counter
for ff in wrfoutf:  # Loop over all WRF files
    wrfout = ncdf(ff)
    year, month, day, hour = wrfdict.wrftimes2hours(
        wrfout)  # Finds the WRF time
    wdate[
        cc] = year * 10000 + month * 100 + day  # Generates an integer in form YYYMMDD
    time[cc] = hour
    if cc == 0:  # Initialize 2D vars and gather necessary variables
        poii, poij = wrfdict.latlon2ij(wrfout, stnlat, stnlon)  # i,j location
        # closest to given lat/lon
        z, zs = wrfdict.getheightloc(wrfout, poij,
                                     poii)  # Get z values at location
        nz = np.shape(zs)[0]  # number of heights
        height = zs[:, poij, poii]  # tower heights
        # Initialize 2D variables
        u = np.zeros((nt, nz))
        v = np.zeros((nt, nz))
Example #11
0
 def archive(self):
     '''
     archive standard output files
     '''
     # loop over all domains
     for domain in range(1, self.ndoms + 1):
         # get lat/lon information from wrfout
         datestr_fn = self.startdate.strftime('%Y-%m-%d_%H:%M:%S')
         wrfout_n = 'wrfout_d0' + str(domain) + '_' + datestr_fn
         wrfout = ncdf(os.path.join(self.rundir, wrfout_n), 'r')
         lat = wrfout.variables['XLAT'][:]
         lon = wrfout.variables['XLONG'][:]
         lat_u = wrfout.variables['XLAT_U'][:]
         lon_u = wrfout.variables['XLONG_U'][:]
         lat_v = wrfout.variables['XLAT_V'][:]
         lon_v = wrfout.variables['XLONG_V'][:]
         frc_urb = wrfout.variables['FRC_URB2D'][:]
         wrfout.close()
         # iterate over all variables that need to be archived
         for var in (self.hour_var + self.minute_var):
             print(var)
             output_fn = (var + '_d0' + str(domain) +
                          '_' + datestr_fn + '.nc')
             output_file = os.path.join(self.archivedir, output_fn)
             for cdate in pandas.date_range(self.startdate, self.enddate,
                                            freq='2H')[:-1]:
                 datestr_in = cdate.strftime('%Y-%m-%d_%H:%M:%S')
                 if not var == 'TC2M_URB':
                     tmp = self.getvar(var, domain, datestr_in)
                 else:
                     # compute TC2M_URB from T2, TP2M_URB and FRC_URB2D
                     # load required variables
                     tp2m_urb = self.getvar('TP2M_URB', domain, datestr_in)
                     # set non-urban points to NaN instead of 0
                     tp2m_urb[tp2m_urb == 0] = np.nan
                     t2 = self.getvar('T2', domain, datestr_in)
                     # compute tc2m_urb
                     tmp = (t2 - (1 - frc_urb) * tp2m_urb) / frc_urb
                     # compute spatial filtered variant
                     tmpF = (self.spatial_filter(t2) -
                             (1 - frc_urb) *
                             self.spatial_filter(tp2m_urb)) / frc_urb
                     # overwrite outer edges of domain with original data
                     tmpF[:, 0, :] = tmp[:, 0, :]
                     tmpF[:, -1, :] = tmp[:, -1, :]
                     tmpF[:, :, 0] = tmp[:, :, 0]
                     tmpF[:, :, -1] = tmp[:, :, -1]
                     # difference between filtered/unfiltered
                     diff = np.abs(tmp - tmpF)
                     # replace points in tmp where diff>1 with tmpF
                     tmp[diff > 1] = tmpF[diff > 1]
                     # set NaN to 0 in tc2m_urb
                     tmp[np.isnan(tmp)] = 0
                 # combine steps from input files
                 if var in self.deac_var:
                     # need to deaccumulate this variable
                     try:
                         output = np.vstack((output,
                                             np.diff(tmp, axis=0)))
                     except NameError:
                         output = np.vstack((tmp[0, :][np.newaxis, :],
                                             np.diff(tmp, axis=0)))
                 else:
                     # variable only needs appending
                     try:
                         output = np.vstack((output, tmp[1:]))
                     except NameError:
                         output = tmp
             # find number of dimensions (3d/4d variable)
             dim = np.ndim(tmp)
             del tmp  # cleanup
             # define time variable in output file
             if (var in self.minute_var) and (domain == self.ndoms):
                 # minute variable in inner domain => minute output
                 dt = pandas.date_range(self.startdate, self.enddate,
                                        freq='1min')[:]
             else:
                 # else hourly output
                 dt = pandas.date_range(self.startdate, self.enddate,
                                        freq='1H')[:]
             # write netcdf outfile
             if var == 'U':
                 self.write_netcdf(var, output, lat_u, lon_u, dt, dim,
                                   output_file)
             elif var == 'V':
                 self.write_netcdf(var, output, lat_v, lon_v, dt, dim,
                                   output_file)
             else:
                 self.write_netcdf(var, output, lat, lon, dt, dim,
                                   output_file)
             del output
Example #12
0
 def write_netcdf(self, var, inpdata, lat, lon, dt, dim, outfile):
     '''
     Write netcdf output file
     '''
     # open output file
     ncfile = ncdf(outfile, 'w')
     # create dimensions and variables
     if dim == 3:
         ncfile.createDimension('time', len(dt))
         ncfile.createDimension('south_north', np.shape(inpdata)[1])
         ncfile.createDimension('west_east', np.shape(inpdata)[2])
         data = ncfile.createVariable(var, 'f4',
                                      ('time', 'south_north', 'west_east',),
                                      zlib=True, fill_value=-999)
     elif dim == 4:
         ncfile.createDimension('time', len(dt))
         ncfile.createDimension('bottom_top', np.shape(inpdata)[1])
         ncfile.createDimension('south_north', np.shape(inpdata)[2])
         ncfile.createDimension('west_east', np.shape(inpdata)[3])
         data = ncfile.createVariable(var, 'f4',
                                      ('time', 'bottom_top',
                                       'south_north', 'west_east',),
                                      zlib=True, fill_value=-999)
     data1 = ncfile.createVariable('latitude', 'f4',
                                   ('south_north', 'west_east',), zlib=True)
     data2 = ncfile.createVariable('longitude', 'f4',
                                   ('south_north', 'west_east',), zlib=True)
     timevar = ncfile.createVariable('time', 'f4', ('time',), zlib=True)
     # time axis UTC
     dt = [date2num(d.to_datetime(),
                    units='minutes since 2010-01-01 00:00:00',
                    calendar='gregorian') for d in dt]
     # define attributes
     timevar.units = 'minutes since 2010-01-01 00:00:00'
     timevar.calendar = 'gregorian'
     timevar.standard_name = 'time'
     timevar.long_name = 'time in UTC'
     data1.units = 'degree_east'
     data1.standard_name = 'longitude'
     data1.FieldType = 104
     data1.description = "longitude, west is negative"
     data1.MemoryOrder = "XY"
     data1.coordinates = "lon lat"
     data2.units = 'degree_north'
     data2.standard_name = 'latitude'
     data2.description = 'latitude, south is negative'
     data2.FieldType = 104
     data2.MemoryOrder = "XY"
     data2.coordinates = "lon lat"
     try:
         data[:] = inpdata[:]
     except IndexError:
         raise
     # lat/lon should be a static field
     try:
         data1[:] = lat[0, :]
         data2[:] = lon[0, :]
     except IndexError:
         raise
     timevar[:] = dt
     # Add global attributes
     ncfile.history = 'Created ' + time.ctime(time.time())
     ncfile.close()
Example #13
0
def createNewNetCDF(fname,dimnames,dims,ncformat='NETCDF4_CLASSIC'):
    newncdf = ncdf(fname,'w',format=ncformat)
    for dd,dim in enumerate(dimnames):
        newncdf.createDimension(dim,dims[dd])
    return newncdf
Example #14
0
 def write_netcdf(self, filename, data, lon, lat, elevation):
     '''
 write data to netcdf file
 '''
     # write time, longitude, latitude, elevation
     ncfile = ncdf(filename, 'w', format='NETCDF4')
     # description of the file
     ncfile.description = 'UK METOFF '
     ncfile.history = 'Created ' + time.ctime(time.time())
     # create time dimension
     timevar = ncfile.createDimension('time', None)
     # create lon/lat dimensions
     lonvar = ncfile.createDimension('longitude', 1)
     latvar = ncfile.createDimension('latitude', 1)
     elevar = ncfile.createDimension('elevation', 1)
     # inititalize time axis
     timevar = ncfile.createVariable('time', 'i4', ('time', ), zlib=True)
     timevar.units = 'minutes since 2010-01-01 00:00:00'
     timevar.calendar = 'gregorian'
     timevar.standard_name = 'time'
     timevar.long_name = 'time in UTC'
     # lon/lat variables
     lonvar = ncfile.createVariable('longitude', 'float32', ('longitude', ))
     lonvar.units = 'degrees_east'
     lonvar.axis = 'X'
     lonvar.standard_name = 'longitude'
     latvar = ncfile.createVariable('latitude', 'float32', ('latitude', ))
     latvar.units = 'degrees_north'
     latvar.axis = 'Y'
     latvar.standard_name = 'latitude'
     # elevation
     elevar = ncfile.createVariable('elevation', 'i4', ('elevation', ))
     elevar.units = 'meter'
     elevar.standard_name = 'elevation'
     timeaxis = [
         int(
             round(
                 date2num(data['datetime'][idx],
                          units='minutes since 2010-01-01 00:00:00',
                          calendar='gregorian')))
         for idx in range(0, len(data['datetime']))
     ]
     timevar[:] = timeaxis
     lonvar[:] = lon
     latvar[:] = lat
     elevar[:] = elevation
     # create other variables in netcdf file
     for variable in data.keys():
         if variable not in [
                 'YYYMMDD', 'Time', '<br>', 'datetime', '# STN', None
         ]:
             # add variables in netcdf file
             # convert strings to npnan if array contains numbers
             if True in [is_number(c) for c in data[variable]]:
                 data[variable] = [
                     npnan if isinstance(fitem(c), str) else fitem(c)
                     for c in data[variable]
                 ]
             data[variable] = ma.masked_array(data[variable],
                                              mask=pd.isnull(
                                                  data[variable]))
             # check if variable is a string
             try:
                 if not isinstance(data[variable][~data[variable].mask][0],
                                   str):
                     try:
                         # fill variable
                         variableName = variable
                         values = ncfile.createVariable(
                             variableName,
                             type(data[variable][~data[variable].mask][0]),
                             ('time', ),
                             zlib=True,
                             fill_value=-999)
                     except TypeError:
                         continue
                 else:
                     # string variables cannot have fill_value
                     try:
                         values = ncfile.createVariable(
                             variable,
                             type(data[variable][~data[variable].mask][0]),
                             ('time', ),
                             zlib=True)
                     except TypeError:
                         continue
             except IndexError:
                 continue
             try:  # fill variable
                 values[:] = data[variable][:]
             except IndexError:
                 # for strings the syntax is slightly different
                 values = data[variable][:]
                 #self.fill_attribute_data()
             except UnboundLocalError:
                 continue
             except TypeError:
                 values = data[variable][:]
     ncfile.close()
Example #15
0
 def write_combined_data_netcdf(self, data, stationid, lon, lat, elevation):
     '''
 description
 '''
     from netCDF4 import Dataset as ncdf
     import netcdftime
     from datetime import datetime
     from dateutil import tz
     from numpy import zeros
     from numpy import nan as npnan
     from numpy import dtype
     import time
     ncfile = ncdf(os.path.join(
         self.outputdir,
         'output' + "".join(str(stationid).split()) + '.nc'),
                   'w',
                   format='NETCDF4')
     # description of the file
     ncfile.description = 'KNMI ' + str(stationid)
     ncfile.history = 'Created ' + time.ctime(time.time())
     # create time dimension
     timevar = ncfile.createDimension('time', None)
     # create lon/lat dimensions
     lonvar = ncfile.createDimension('longitude', 1)
     latvar = ncfile.createDimension('latitude', 1)
     # elevation
     elvar = ncfile.createDimension('elevation', 1)
     # inititalize time axis
     timeaxis = [
         int(
             round(
                 date2num(data['datetime'][idx],
                          units='minutes since 2010-01-01 00:00:00',
                          calendar='gregorian')))
         for idx in range(0, len(data['datetime']))
     ]
     # netcdf time variable UTC
     timevar = ncfile.createVariable('time', 'i4', ('time', ), zlib=True)
     timevar[:] = timeaxis
     timevar.units = 'minutes since 2010-01-01 00:00:00'
     timevar.calendar = 'gregorian'
     timevar.standard_name = 'time'
     timevar.long_name = 'time in UTC'
     # lon/lat variables
     lonvar = ncfile.createVariable('longitude',
                                    dtype('float32').char, ('longitude', ))
     lonvar.units = 'degrees_east'
     lonvar.axis = 'X'
     lonvar.standard_name = 'longitude'
     lonvar[:] = lon
     latvar = ncfile.createVariable('latitude',
                                    dtype('float32').char, ('latitude', ))
     latvar.units = 'degrees_north'
     latvar.axis = 'Y'
     latvar.standard_name = 'latitude'
     latvar[:] = lat
     # elevation variable
     elvar = ncfile.createVariable('elevation',
                                   dtype('float32').char, ('elevation', ))
     elvar.units = 'meter'
     elvar.axis = 'Z'
     elvar.standard_name = 'elevation'
     elvar[:] = elevation
     # create other variables in netcdf file
     for variable in data.keys():
         if variable not in [
                 'YYYMMDD', 'Time', '<br>', 'datetime', '# STN', None
         ]:
             # add variables in netcdf file
             # convert strings to npnan if array contains numbers
             if True in [is_number(c) for c in data[variable]]:
                 data[variable] = [
                     npnan if isinstance(fitem(c), str) else fitem(c)
                     for c in data[variable]
                 ]
             data[variable] = ma.masked_array(data[variable],
                                              mask=pd.isnull(
                                                  data[variable]))
             try:
                 # check if variable is a string
                 if not isinstance(data[variable][~data[variable].mask][0],
                                   str):
                     try:
                         # fill variable
                         variableName = variable
                         values = ncfile.createVariable(
                             variableName,
                             type(data[variable][~data[variable].mask][0]),
                             ('time', ),
                             zlib=True,
                             fill_value=-999)
                     except TypeError:
                         continue
                 else:
                     # string variables cannot have fill_value
                     try:
                         values = ncfile.createVariable(
                             variable,
                             type(data[variable][~data[variable].mask][0]),
                             ('time', ),
                             zlib=True)
                     except TypeError:
                         continue
             except IndexError:
                 continue
             try:  # fill variable
                 values[:] = data[variable][:]
             except IndexError:
                 # for strings the syntax is slightly different
                 values = data[variable][:]
                 #self.fill_attribute_data()
             except UnboundLocalError:
                 continue
             except TypeError:
                 values = data[variable][:]
     ncfile.close()
Example #16
0
    def write_combined_data_netcdf(self):
        ncfile = ncdf(self.outputfile, 'w', format='NETCDF4')
        # description of the file
        ncfile.description = 'Hobby meteorologists data ' + self.inputdir
        ncfile.history = 'Created ' + time.ctime(time.time())
        # create time dimension
        timevar = ncfile.createDimension('time', None)
        # create time variable local time Europe/Amsterdam
        timeaxisLocal = zeros(len(self.data[self.dateUTCstring][1:]))
        # define UTC and local time-zone (hardcoded)
        from_zone = tz.gettz('UTC')
        to_zone = tz.gettz('Europe/Amsterdam')
        # convert time string to datetime object
        for idx in range(1, len(self.data[self.dateUTCstring])):
            # define time object from string
            timeObject = datetime.strptime(self.data[self.dateUTCstring][idx],
                                           '%Y-%m-%d %H:%M:%S')
            # tell timeObject that it is in UTC
            timeObject = timeObject.replace(tzinfo=from_zone)
            # time axis UTC
            try:
              timeaxis = npappend(timeaxis, ncdf_date2num(
                timeObject.replace(tzinfo=None),
                units='minutes since 2010-01-01 00:00:00',
                calendar='gregorian'))
            except NameError:
              timeaxis = ncdf_date2num(
                timeObject.replace(tzinfo=None),
                units='minutes since 2010-01-01 00:00:00',
                calendar='gregorian')

        # netcdf time variable UTC
        timevar = ncfile.createVariable('time', 'i4', ('time',),
                                        zlib=True)
        timevar[:] = timeaxis
        timevar.units = 'minutes since 2010-01-01 00:00:00'
        timevar.calendar = 'gregorian'
        timevar.standard_name = 'time'
        timevar.long_name = 'time in UTC'

        # write lon/lat variables if available
        if ((self.lat) and (self.lon)):
            lonvar = ncfile.createDimension('longitude', 1)
            lonvar = ncfile.createVariable('longitude', 'float32',('longitude',))
            lonvar.units = 'degrees_east'
            lonvar.axis = 'X'
            lonvar.standard_name = 'longitude'
            lonvar[:] = self.lon
            latvar = ncfile.createDimension('latitude', 1)
            latvar = ncfile.createVariable('latitude', 'float32',('latitude',))
            latvar.units = 'degrees_north'
            latvar.axis = 'Y'
            latvar.standard_name = 'latitude'
            latvar[:] = self.lat
        # create other variables in netcdf file
        for self.variable in self.data.keys():
            if self.variable not in [self.dateUTCstring, 'Time', '<br>', None]:
                # add variables in netcdf file
                # convert strings to npnan if array contains numbers
                if True in [utils.is_number(c)
                            for c in self.data[self.variable]]:
                    self.data[self.variable] = [npnan if isinstance(
                        utils.fitem(c), str) else utils.fitem(c) for c in self.data[
                            self.variable]]
                # check if variable is a string
                if not isinstance(self.data[self.variable][1], str):
                    # fill variable
                    if self.variable == 'SolarRadiationWatts/m^2':
                        #variableName = 'SolarRadiation'
                        continue
                    elif ((self.variable == 'TemperatureC') or
                          (self.variable == 'TemperatureF')):
                        variableName = 'temperature'
                    else:
                        variableName = self.variable
                    self.values = ncfile.createVariable(
                        variableName, type(self.data[self.variable][1]),
                        ('time',), zlib=True, fill_value=-999)
                else:
                    # string variables cannot have fill_value
                    self.values = ncfile.createVariable(
                        self.variable, type(self.data[self.variable][1]),
                        ('time',), zlib=True)
                # TODO: km/h->m/s ??
                try:  # fill variable
                    if not self.variable in ['TemperatureC', 'TemperatureF']:
                      self.values[:] = self.data[self.variable][1:]
                    elif self.variable == 'TemperatureC':
                      self.values[:] = 273.15 + nparray(self.data[self.variable][1:])
                    elif self.variable == 'TemperatureF':
                      self.values[:] = (nparray(self.data[self.variable][1:]) - 32.)/1.8
                except IndexError:
                    # for strings the syntax is slightly different
                    self.values = self.data[self.variable][1:]
                self.fill_attribute_data()
        ncfile.close()