Exemple #1
0
 def load_obs(self, forecast_hour=6, madis=True, variance=False):
     """
     Loads the observations corresponding with n hours after ensemble was
     initialized (default 6 hours). Returns the observations from the text file.
     -Loads MADIS observations if madis=True, loads gridded observations if False.
     -If variance == True, loads gridded observations which also contain 
     ensemble variance at the ob location.
     -Gridded and MADIS observations must be generated/obtained before calling this function.
     Returns a list of observations. 
     """
     
     dty, dtm, dtd, dth = ef.dt_str_timedelta(self.date,forecast_hour)
     
     ob_str = ef.var_string([self.ob_type])        
     # directory where the observations are
     if madis==True:
         obs_file = '/home/disk/hot/stangen/Documents/surface_obs/MADIS/'+dty+dtm+'/combined_'+self.ob_type+'/'+self.ob_type+'_'+dty+dtm+dtd+'_'+dth+'00.txt'
     elif madis==False:
         if self.new_format == False:
             obs_file = '/home/disk/hot/stangen/Documents/gridded_obs/'+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/'+self.ob_type+'_'+dty+dtm+dtd+'_'+dth+'00'
         elif self.new_format == True:
             obs_file = '/home/disk/hot/stangen/Documents/gridded_obs/'+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/'+dty+dtm+dtd+'_'+dth+'00_'+str(self.l)+'_'+str(self.r)+'_'+str(self.t)+'_'+str(self.b)+'_'+str(self.s)
         if variance==True:
             obs_file += '_variance'
         obs_file += '.txt'
     print('loading '+ob_str+' obs from '+dty+dtm+dtd+'_'+dth+'00')
     f1 = open(obs_file, 'r')
     obs = f1.readlines()
     
     return obs
Exemple #2
0
    def load_ens_netcdf(self, forecast_hour=0):
        """
        Loads the ensemble from n forecast hours after the forecast was 
        initialized and uses its 0-hour forecast as the "observation" grid. 
        Returns the variable of interest and the lats/lons. 
        The filepath loads non-EFA'd netCDF files (it loads the prior).
        var is  nlats x nlons x nmems. (at time t=0)
        lats, lons are 1-d float arrays (masked).
        """  
        dty, dtm, dtd, dth = ef.dt_str_timedelta(self.date,forecast_hour)
        
        print('loading analysis grid: '+self.ens_type+' '+dty+dtm+dtd+'_'+dth+'00')
        
        if self.new_format == False:
            infile = '/home/disk/hot/stangen/Documents/prior_ensembles/'+self.ens_type+'/'+dty+dtm+'/'+dty+'-'+dtm+'-'+dtd+'_'+dth+'_'+self.ens_type+'_'+self.var_string+'.nc'
        elif self.new_format == True:
            infile = '/home/disk/hot/stangen/Documents/prior_ensembles/'+self.ens_type+'/'+dty+dtm+'/'+dty+'-'+dtm+'-'+dtd+'_'+dth+'_'+self.efh+'_'+self.var_string+'.nc'
        ncdata = Dataset(infile,'r')
        #gridded ensemble 0-hour forecasts n forecast hours after the ensemble
        #we are updating was initialized
        var = ncdata.variables[self.ob_type][0,:,:,:]

        lats = ncdata.variables['lat'][:]
        lons = ncdata.variables['lon'][:]
        
        return var, lats, lons
Exemple #3
0
#dictionaries for plotting
obtype_dict = {'madis': 'Metar & Maritime', 'gridded': 'Gridded'}

var_dict = {
    'T2M': '2-Meter Temperature',
    'ALT': 'Altimeter',
    'IWV': 'Integrated Water Vapor',
    'IVT': 'Integrated Vapor Transport'
}

var_unit = {'T2M': '(K)', 'ALT': 'hPa', 'IWV': 'mm', 'IVT': 'kg/m/s'}

save_dir = '/home/disk/hot/stangen/Documents/EFA/duplicate_madaus/plots/'

#get date strings from datetime object for accessing file
y, m, d, h = ef.dt_str_timedelta(date)
dstr = y + m + d
time = h + '00'

#Open MADIS text data file
#f = open('/home/disk/hot/stangen/Documents/EFA/duplicate_madaus/plots/2013040106obs_allmaritime.txt','r')
if ob_type == 'madis':
    f = open(
        "/home/disk/hot/stangen/Documents/surface_obs/MADIS/" + dstr[0:6] +
        "/combined_" + variable + "/" + variable + "_" + dstr + "_" + time +
        ".txt", "r")
elif ob_type == 'gridded':
    f = open(
        "/home/disk/hot/stangen/Documents/gridded_obs/" + ens + "/" +
        dstr[0:6] + "/" + variable + "/" + dstr + "_" + time + "_" + grid +
        ".txt", "r")
Exemple #4
0
 def save_gridded_obs(self, forecast_hour=0, get_variance=False):
     """
     Loads the ensemble from n hours after the forecast was initialized
     and uses its 0-hour forecast as the "observation" grid. 
     Saves information about the generated gridded obs to a .txt file,
     with the same format as MADIS observations.
     
     If get_variance is true, will also save the variance of the ensemble
     at the observation locations at the end of each line.
     """
     basedir = '/home/disk/hot/stangen/Documents/gridded_obs/'
     
     #get the list of points
     points = ef.get_ob_points(self.l,self.r,self.t,self.b,self.s)
     #get the 0-hour ensemble forecast, n hours after the model was initialized
     dt0 = self.date
     dt = dt0.replace(minute = 0, second=0, microsecond=0)
     dt = dt + timedelta(hours=forecast_hour)
     
     #convert to epoch time for saving observation
     epoch = str(dt.replace(tzinfo=pytz.utc).timestamp())
     
     dty, dtm, dtd, dth = ef.dt_str_timedelta(self.date,forecast_hour)
     
     print('starting saving of '+self.ens_type+' gridded '+self.ob_type+ ' "obs" at: '+dty+dtm+dtd+'_'+dth+'00')
     
     var, lats, lons = self.load_ens_netcdf(forecast_hour)
         
     #obtain the mean of the ensemble (nlats x nlons)
     ens_mean = var.mean(axis=-1)
     
     #obtain variance of the ensemble (nlats x nlons)
     variance = np.var(var,axis=-1,ddof=1)
     
     #initialize the obs list to append to
     obs = []
     #loop through each point (lat/lon pair)
     for i, p in enumerate(points):
         ob_lat = p[0]
         ob_lon = p[1]
         #get the ensemble value at the lat/lon pair
         ob_value, ob_variance = ef.closest_points(ob_lat,ob_lon,lats,lons,variable=ens_mean,
                                      need_interp=True,gen_obs=True,variance=variance)
         obs.append(str(i)+','+str(ob_lat)+','+str(ob_lon)+','+str(0)+','+epoch+','+str(ob_value)+',GRIDDED,'+str(0))
         if get_variance == True:
             obs.append(','+str(ob_variance))
         obs.append('\n')
         
     #save directory for the observations
     if (os.path.isdir(basedir+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/')):
         pass
     else:
         os.makedirs(basedir+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/')
         
     #save the list of observations
     if self.new_format == False:
         f = open(basedir+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/'+self.ob_type+'_'+dty+dtm+dtd+'_'+dth+'00.txt',"w")
     elif self.new_format == True:
         savestr = basedir+self.ens_type+'/'+dty+dtm+'/'+self.ob_type+'/'+dty+dtm+dtd+'_'+dth+'00_'+str(self.l)+'_'+str(self.r)+'_'+str(self.t)+'_'+str(self.b)+'_'+str(self.s)
         if get_variance == True:
             savestr += '_variance'
         savestr += '.txt'
         f = open(savestr,"w")
     for s in obs:
         f.write(s)
     f.close()