Ejemplo n.º 1
0
  def Common_Attributes(self,so,file_name,step):
      
      
      #nc.set_var_attribute(water_fname, 'sea_pressure', 'sea_uuid', sea_uuid)
      nc.set_global_attribute(file_name, 'uuid', str(uuid.uuid4()))
  
      #append air pressure
      instr_dict = nc.get_instrument_data(so.air_fname, 'air_pressure')
      nc.append_air_pressure(file_name, so.interpolated_air_pressure[::step], so.air_fname)
      nc.set_instrument_data(file_name, 'air_pressure', instr_dict)
  
      #update the lat and lon comments
      lat_comment = nc.get_variable_attr(file_name, 'latitude', 'comment')
      nc.set_var_attribute(file_name, 'latitude', 'comment',  \
                           ''.join([lat_comment, ' Latitude of sea pressure sensor used to derive ' \
                                    'sea surface elevation.']))
      lon_comment = nc.get_variable_attr(file_name, 'longitude', 'comment')
      nc.set_var_attribute(file_name, 'longitude', 'comment',  \
                           ''.join([lon_comment, ' Longitude of sea pressure sensor used to derive ' \
                                    'sea surface elevation.']))
  
      #set sea_pressure instrument data to global variables in water_level netCDF
      sea_instr_data = nc.get_instrument_data(so.sea_fname,'sea_pressure')
      for x in sea_instr_data:
          attrname = ''.join(['sea_pressure_',x])
          nc.set_global_attribute(file_name,attrname,sea_instr_data[x])
      lat = nc.get_variable_data(file_name, 'latitude')
      lon = nc.get_variable_data(file_name, 'longitude')
 
      first_stamp = nc.get_global_attribute(file_name, 'time_coverage_start')
      last_stamp = nc.get_global_attribute(file_name, 'time_coverage_end')
      
      nc.set_global_attribute(file_name,'title','Calculation of water level at %.4f latitude,'
                              ' %.4f degrees longitude from the date range of %s to %s.'
                              % (lat,lon,first_stamp,last_stamp))
Ejemplo n.º 2
0
 def get_meta_data(self):
     if self.datum is None:
         self.datum = nc.get_geospatial_vertical_reference(self.sea_fname)
         self.latitude = nc.get_variable_data(self.sea_fname,'latitude')
         self.longitude = nc.get_variable_data(self.sea_fname,'longitude')
         self.stn_station_number = nc.get_global_attribute(self.sea_fname, 'stn_station_number')
         self.stn_instrument_id = nc.get_global_attribute(self.sea_fname, 'stn_instrument_id')
Ejemplo n.º 3
0
 def get_meta_data(self):
     if self.datum is None:
         self.datum = nc.get_geospatial_vertical_reference(self.sea_fname)
         self.latitude = nc.get_variable_data(self.sea_fname, 'latitude')
         self.longitude = nc.get_variable_data(self.sea_fname, 'longitude')
         self.stn_station_number = nc.get_global_attribute(
             self.sea_fname, 'stn_station_number')
         self.stn_instrument_id = nc.get_global_attribute(
             self.sea_fname, 'stn_instrument_id')
Ejemplo n.º 4
0
 def get_wind_meta_data(self):
     if self.wind_latitude is None:
         
         try:
             self.wind_latitude = nc.get_variable_data(self.wind_fname,'latitude')
             self.wind_longitude = nc.get_variable_data(self.wind_fname,'longitude')
             self.wind_stn_station_number = nc.get_global_attribute(self.wind_fname, 'stn_station_number')
         except:
             self.wind_latitude = 0
             self.wind_longitude = 0
             self.wind_stn_station_number = 'na'
Ejemplo n.º 5
0
 def get_air_meta_data(self):
     if self.air_latitude is None:
         
         if self.from_water_level_file == False:
             self.air_latitude = nc.get_variable_data(self.air_fname,'latitude')
             self.air_longitude = nc.get_variable_data(self.air_fname,'longitude')
             self.air_stn_station_number = nc.get_global_attribute(self.air_fname, 'stn_station_number')
             self.air_stn_instrument_id = nc.get_global_attribute(self.air_fname, 'stn_instrument_id')
         else:
             self.air_latitude = nc.get_variable_data(self.air_fname,'latitude')
             self.air_longitude = nc.get_variable_data(self.air_fname,'longitude')
             self.air_stn_station_number = nc.get_global_attribute(self.air_fname, 'stn_station_number')
             self.air_stn_instrument_id = nc.get_variable_attr(self.air_fname, 'air_pressure', 'instrument_serial_number')
Ejemplo n.º 6
0
    def get_wind_meta_data(self):
        if self.wind_latitude is None:

            try:
                self.wind_latitude = nc.get_variable_data(
                    self.wind_fname, 'latitude')
                self.wind_longitude = nc.get_variable_data(
                    self.wind_fname, 'longitude')
                self.wind_stn_station_number = nc.get_global_attribute(
                    self.wind_fname, 'stn_station_number')
            except:
                self.wind_latitude = 0
                self.wind_longitude = 0
                self.wind_stn_station_number = 'na'
Ejemplo n.º 7
0
    def get_air_meta_data(self):
        if self.air_latitude is None:

            if self.from_water_level_file == False:
                self.air_latitude = nc.get_variable_data(
                    self.air_fname, 'latitude')
                self.air_longitude = nc.get_variable_data(
                    self.air_fname, 'longitude')
                self.air_stn_station_number = nc.get_global_attribute(
                    self.air_fname, 'stn_station_number')
                self.air_stn_instrument_id = nc.get_global_attribute(
                    self.air_fname, 'stn_instrument_id')
            else:
                self.air_latitude = nc.get_variable_data(
                    self.air_fname, 'latitude')
                self.air_longitude = nc.get_variable_data(
                    self.air_fname, 'longitude')
                self.air_stn_station_number = nc.get_global_attribute(
                    self.air_fname, 'stn_station_number')
                self.air_stn_instrument_id = nc.get_variable_attr(
                    self.air_fname, 'air_pressure', 'instrument_serial_number')
Ejemplo n.º 8
0
    def get_nc_info(self, path):

        file_types = ['.nc']
        for root, sub_folders, files in os.walk(path):
            for file_in_root in files:

                index = file_in_root.rfind('.')
                if file_in_root[index:] in file_types:
                    file = ''.join([root, '\\', file_in_root])
                    lat = nc.get_variable_data(file, 'latitude')
                    lon = nc.get_variable_data(file, 'longitude')

                    type = 'sea'
                    try:
                        nc.get_pressure(file)
                    except:

                        try:
                            type = 'air'
                            nc.get_air_pressure(file)
                        except:
                            type = 'neither'
Ejemplo n.º 9
0
 def get_nc_info(self,path):
     
     
     file_types = ['.nc']
     for root, sub_folders, files in os.walk(path):
         for file_in_root in files:
             
             index = file_in_root.rfind('.')
             if file_in_root[index:] in file_types:
                 file = ''.join([root,'\\',file_in_root])
                 lat = nc.get_variable_data(file, 'latitude')
                 lon = nc.get_variable_data(file, 'longitude')
                 
                 type = 'sea'
                 try:
                     nc.get_pressure(file)
                 except:
                     
                     try:
                         type = 'air'
                         nc.get_air_pressure(file)
                     except:
                         type='neither'
Ejemplo n.º 10
0
 def get_surge_water_level(self): 
     if self.surge_water_level is None:
         self.get_corrected_pressure()
         self.get_sensor_orifice_elevation()
         self.get_pressure_mean()
         self.get_surge_sea_pressure()
         
         if self.from_water_level_file:
             self.surge_water_level = nc.get_variable_data(self.sea_fname, "water_surface_height_above_reference_datum")
         else:
             self.surge_water_level = np.array(self.derive_filtered_water_level(self.surge_sea_pressure, 
                                                                   self.sea_pressure_mean, 
                                                                   self.sensor_orifice_elevation,
                                                                   self.salinity))
Ejemplo n.º 11
0
    def Common_Attributes(self, so, file_name, step):

        #nc.set_var_attribute(water_fname, 'sea_pressure', 'sea_uuid', sea_uuid)
        nc.set_global_attribute(file_name, 'uuid', str(uuid.uuid4()))

        #append air pressure
        instr_dict = nc.get_instrument_data(so.air_fname, 'air_pressure')
        nc.append_air_pressure(file_name, so.interpolated_air_pressure[::step],
                               so.air_fname)
        nc.set_instrument_data(file_name, 'air_pressure', instr_dict)

        #update the lat and lon comments
        lat_comment = nc.get_variable_attr(file_name, 'latitude', 'comment')
        nc.set_var_attribute(file_name, 'latitude', 'comment',  \
                             ''.join([lat_comment, ' Latitude of sea pressure sensor used to derive ' \
                                      'sea surface elevation.']))
        lon_comment = nc.get_variable_attr(file_name, 'longitude', 'comment')
        nc.set_var_attribute(file_name, 'longitude', 'comment',  \
                             ''.join([lon_comment, ' Longitude of sea pressure sensor used to derive ' \
                                      'sea surface elevation.']))

        #set sea_pressure instrument data to global variables in water_level netCDF
        sea_instr_data = nc.get_instrument_data(so.sea_fname, 'sea_pressure')
        for x in sea_instr_data:
            attrname = ''.join(['sea_pressure_', x])
            nc.set_global_attribute(file_name, attrname, sea_instr_data[x])
        lat = nc.get_variable_data(file_name, 'latitude')
        lon = nc.get_variable_data(file_name, 'longitude')

        first_stamp = nc.get_global_attribute(file_name, 'time_coverage_start')
        last_stamp = nc.get_global_attribute(file_name, 'time_coverage_end')

        nc.set_global_attribute(
            file_name, 'title', 'Calculation of water level at %.4f latitude,'
            ' %.4f degrees longitude from the date range of %s to %s.' %
            (lat, lon, first_stamp, last_stamp))
Ejemplo n.º 12
0
    def get_surge_water_level(self):
        if self.surge_water_level is None:
            self.get_corrected_pressure()
            self.get_sensor_orifice_elevation()
            self.get_pressure_mean()
            self.get_surge_sea_pressure()

            if self.from_water_level_file:
                self.surge_water_level = nc.get_variable_data(
                    self.sea_fname,
                    "water_surface_height_above_reference_datum")
            else:
                self.surge_water_level = np.array(
                    self.derive_filtered_water_level(
                        self.surge_sea_pressure, self.sea_pressure_mean,
                        self.sensor_orifice_elevation, self.salinity))
Ejemplo n.º 13
0
 def get_corrected_pressure(self):
     if self.corrected_sea_pressure is None:
         if self.from_water_level_file == False:
             self.slice_series()
             self.corrected_sea_pressure = self.raw_sea_pressure - self.interpolated_air_pressure
         else:
             self.sea_time = self.extract_time(self.sea_fname)
             self.raw_water_level = nc.get_variable_data(self.sea_fname
                                                         , 'unfiltered_water_surface_height_above_reference_datum')
             self.interpolated_air_pressure = nc.get_air_pressure(self.sea_fname)
             self.sensor_orifice_elevation = np.array(self.extract_sensor_orifice_elevation(self.sea_fname, \
                                         len(self.sea_time)))
             self.land_surface_elevation = np.array(self.extract_land_surface_elevation(self.sea_fname, \
                                         len(self.sea_time)))
             self.corrected_sea_pressure = p2d.hydrostatic_pressure(self.raw_water_level - self.sensor_orifice_elevation)  \
                                         
                                         
         self.get_pressure_mean()      
Ejemplo n.º 14
0
    def get_corrected_pressure(self):
        if self.corrected_sea_pressure is None:
            if self.from_water_level_file == False:
                self.slice_series()
                self.corrected_sea_pressure = self.raw_sea_pressure - self.interpolated_air_pressure
            else:
                self.sea_time = self.extract_time(self.sea_fname)
                self.raw_water_level = nc.get_variable_data(
                    self.sea_fname,
                    'unfiltered_water_surface_height_above_reference_datum')
                self.interpolated_air_pressure = nc.get_air_pressure(
                    self.sea_fname)
                self.sensor_orifice_elevation = np.array(self.extract_sensor_orifice_elevation(self.sea_fname, \
                                            len(self.sea_time)))
                self.land_surface_elevation = np.array(self.extract_land_surface_elevation(self.sea_fname, \
                                            len(self.sea_time)))
                self.corrected_sea_pressure = p2d.hydrostatic_pressure(self.raw_water_level - self.sensor_orifice_elevation)  \

            self.get_pressure_mean()
Ejemplo n.º 15
0
def custom_copy(fname, out_fname):
    if os.path.exists(out_fname):
        os.remove(out_fname)
    
    #get station id for the station_id dimension
    stn_site_id = nc.get_variable_data(fname, 'station_id')
    t = nc.get_time(fname)[:]
    d = Dataset(fname)
    output = Dataset(out_fname, 'w', format='NETCDF4_CLASSIC')
    output.createDimension('time', len(t))
    output.createDimension("timeSeries", 1)
    output.createDimension("station_id", len(stn_site_id))
    
    # copy globals
    for att in d.ncattrs():
        setattr(output, att, d.__dict__[att])
    setattr(output, "creator_name", "N/a")
    setattr(output, "creator_email", "N/a")
    setattr(output, "cdm_data_type", "STATION")
   
    # copy variables
    for key in d.variables:
        
        
        if key == 'wave_wl':
            continue
        
        
        
        name = key
            
        datatype = d.variables[key].datatype 
          
        dim = d.variables[key].dimensions
        
        if datatype == "int32":
            var = output.createVariable(name, datatype, dim)
        else:
            if name == "station_id":
                var = output.createVariable("station_name", datatype, dim, fill_value=FILL_VALUE)
                data = output.getncattr('stn_station_number')
            else:
                var = output.createVariable(name, datatype, dim, fill_value=FILL_VALUE)
                data = None
        
        for att in d.variables[key].ncattrs():
            if att != '_FillValue':
                if att == "cf_role":
                    setattr(var, att, "timeseries_id")
                elif att == "long_name" and name == "station_id":
                    setattr(var, att, data)
                else:
                    setattr(var, att, d.variables[key].__dict__[att])
         
        if key in ['time', 'latitude','longitude','altitude']:       
            setattr(var, 'featureType', 'Point')
        else:
            setattr(var, 'featureType', 'Station')
           
        if data is None: 
            var[:] = d.variables[key][:]
        else:
            var[:] = list(data)
                
    d.close()
    output.close()
Ejemplo n.º 16
0
def run_wave_stats(p_window):
    sea_pressure = nc.get_variable_data(sea_fname, 'sea_pressure')
    sea_time = nc.get_variable_data(sea_fname, 'time')
    air_pressure = nc.get_variable_data(air_fname, 'air_pressure')
    air_time = nc.get_variable_data(air_fname, 'time' )
    
    c_pressure = sea_pressure - np.interp(sea_time,air_time,air_pressure)
    start_index, end_index, step = 0, 4096, 2048
    
    corrected_pressure = []
    while end_index < len(c_pressure):
        corrected_pressure.append(c_pressure[start_index:end_index])
        start_index += step
        end_index += step
        
    corrected_pressure = np.array(corrected_pressure)
    # time2 = nc.get_variable_data(fname, 'time2')
    # corrected_pressure = nc.get_variable_data(fname,'P_1ac')
    # depth = float(nc.get_variable_data(fname, 'depth'))
    pspec = nc.get_variable_data(fname2, 'pspec')
    file_freq = nc.get_variable_data(fname2, 'frequency')
     
    sig_height = nc.get_variable_data(fname2,'wh_4061')
    average_period = nc.get_variable_data(fname2,'wp_4060')

    stat_o = Stats()
    
    our_sig_wave_height = []
    our_avg_period = []
    
    get_index = []
     
    fs = 4.0
    for x in range(0, len(corrected_pressure)):
        
        get_index.append(x)
        ms = np.array([1430049600000.0 + (y * 166.66667) for y in range(0,len(corrected_pressure))])
        final_pressure = corrected_pressure[x]
        
    #     coeff = np.polyfit(ms, final_pressure, 1)
    #     lin_trend = coeff[1] + coeff[0]*ms
    #     detrended_pressure = detrend(final_pressure, type='linear', axis=-1)
    # #     final_pressure - lin_trend
    #
        window = np.hanning(p_window)
    # #     window = window ** 2
    #     windowed_pressure = detrended_pressure 
        #perform real fourier function on the scaled pressure and get the frequencies
    
        
        amps = []
        freqs= np.fft.rfftfreq(p_window, d=1/fs)
         
        final_pressure -= np.mean(final_pressure)
        final_pressure = detrend(final_pressure, type='linear', axis=-1) 
        
        for y in range(0, int(4096/p_window)):
            p = final_pressure[y * p_window:y * p_window + p_window]
            
            p *= window
             
             
            amps.append(np.fft.rfft(p))
            
           
        if p_window == 4096:
            print('4096!!')
            amp_sum = amps[0]
        else: 
            amp_sum = np.array(amps).sum(axis=0)
        
        amp_sum *= np.conjugate(amp_sum)
        scale = 1.0 / (fs * (window**2).sum())      
        amps = amp_sum * scale
        amps = amps.real 
    #     print(amps)
        freqs,amps = welch(final_pressure, 6, window = window, nperseg=256, noverlap=0, detrend='linear')
        
        amps = np.array(amps,dtype=np.float64)
        freqs, amps = freqs[1:], amps[1:]
        
        cut_off = np.where(freqs<=1.0)
        
        freqs = freqs[cut_off]
        amps = amps[cut_off]
        
        if x == 0:
            plt.plot(freqs,amps)
            plt.show()
    
        amps2 = np.array([pspec[x][p][0][0] for p in range(0,len(pspec[x]))], dtype=np.float64)
        freqs2 = file_freq
        
        #get the approximate depth
        approx_depth = 0.2926
        
    #     step = float(len(freqs)/float(len(freqs2)))
    #     freqs = freqs[step::step]  
          
        #get the wave numbers
        k = p2d.echart_omega_to_k(freqs * 2.0 * np.pi, np.repeat(approx_depth,len(freqs)))
        kz = np.array(np.cosh(0.1829*k)/np.cosh(approx_depth*k))
        
#         print('kz',kz)
         
    #     query = np.where(kz < 0.45)
    # #      
    # # #     if len(query[0]) > 0:
    # #     print('cut')
    #     cut = query[0][0]
    #     lin_trans1 = amps[0:cut]/(kz[0:cut]**2)
    #       
    #     #this adds the f^-4 tail to the end of the spectrum
    #     lin_trans2 = (lin_trans1[cut-1] * (freqs[cut-1:]**-4)) \
    #     * ( 1 /  (freqs[cut-1]**-4))
    #       
    #     lin_transform = np.concatenate((  
    #                                     lin_trans1[:len(lin_trans1)-1],
    #                                     lin_trans2
    #                                     ))
    #        
           
              
    #     else:
    #         print('no cut')
        lin_transform = amps/(kz**2) 
         
    #     freqs = np.fft.rfftfreq(len(lin_transform), d=1/6)
    #     print(len(amps))
    # #     step = float(len(freqs)/float(len(freqs2)))
    # #     freqs = freqs[step::step]  
    #     print('freqs1',freqs)
    #     print('freqs2', freqs2)
    #     fig = plt.figure()
    #     ax = fig.add_subplot('211')
    #     ax2 = fig.add_subplot('212')   
    #     ax.plot(freqs2,amps2)
    # #     stat_o.power_spectrum(detre, tstep)
    # #     divide = []
    # #     for x in range(0,len(amps2)):
    # #         print(freqs[x], amps2[x],amps[x], amps2[x]/amps[x])
    # #         divide.append(amps2[x]/amps[x])
    #     
    #     ax2.plot(freqs,amps) 
    #     plt.show()
    #      
        our_sig_wave_height.append(stat_o.significant_wave_height(lin_transform, freqs, \
                                                            None, None))
        our_avg_period.append(stat_o.mean_wave_period(lin_transform, freqs, \
                                                            None, None))
     
    
      
    diff = []
#     for x in range(0,len(corrected_pressure)):#len(end_time)):
#          
#         print(' ')
#         print('H1/3: ', sig_height[get_index[x]])
#         print('avg period: ', average_period[get_index[x]])
#              
#         print('H1/3 ours', our_sig_wave_height[x])
#         print('Avg Period ours', our_avg_period[x])
#              
#         diff.append(np.abs(sig_height[get_index[x]][0][0] - our_sig_wave_height[x]))  
#           
#                
#     
#     print(np.array(diff).sum()/len(corrected_pressure))
    
    plt.plot(range(0,len(our_sig_wave_height)),our_sig_wave_height)
    plt.show()
    
    plt.plot(range(0,len(our_sig_wave_height)),our_avg_period)
    plt.show()
Ejemplo n.º 17
0
    def multi_air_pressure(self, path, title):
        self.create_header()

        ax = self.figure.add_subplot(self.grid_spec[1, 0:])
        pos1 = ax.get_position()  # get the original position
        pos2 = [pos1.x0, pos1.y0, pos1.width, pos1.height + .06]
        ax.set_position(pos2)  # set a new position

        #create the second graph title
        first_title = "%s Air Pressure Time Series Comparison" % title

        titleText = ax.text(0.5, 1.065,first_title,  \
            va='center', ha='center', transform=ax.transAxes)

        ax.set_ylabel('Air Pressure in Inches of Mercury')
        ax.set_xlabel('Timezone GMT')

        #plot major grid lines
        ax.grid(b=True, which='major', color='grey', linestyle="-")

        #x axis formatter for dates (function format_date() below)
        ax.xaxis.set_major_formatter(ticker.FuncFormatter(self.format_date))

        legend_list, label_list = [], []

        file_types = ['.nc']
        for root, sub_folders, files in os.walk(path):
            for file_in_root in files:

                index = file_in_root.rfind('.')
                if file_in_root[index:] in file_types:
                    file = ''.join([root, '\\', file_in_root])
                    try:
                        air_pressure = nc.get_air_pressure(
                            file) * unit_conversion.DBAR_TO_INCHES_OF_MERCURY
                        time = nc.get_time(file)

                        first_date = unit_conversion.convert_ms_to_date(
                            time[0], pytz.UTC)
                        last_date = unit_conversion.convert_ms_to_date(
                            time[-1], pytz.UTC)

                        first_date = mdates.date2num(first_date)
                        last_date = mdates.date2num(last_date)

                        self.time_nums = np.linspace(first_date, last_date,
                                                     len(time))

                        lat = nc.get_variable_data(file, 'latitude')
                        lon = nc.get_variable_data(file, 'longitude')
                        stn_id = nc.get_global_attribute(
                            file, 'stn_instrument_id')
                        stn_station = nc.get_global_attribute(
                            file, 'stn_station_number')

                        p1, = ax.plot(self.time_nums, air_pressure, alpha=.5)
                        legend_list.append(p1)
                        label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                          %(stn_id, stn_station, lat, lon))

                    except:
                        pass

        legend = ax.legend(legend_list,label_list
        , \
                  bbox_to_anchor=(.95, 1.355), loc=1, borderaxespad=0.0, prop={'size':10.3},frameon=False,numpoints=1, \
                  title="EXPLANATION")
        legend.get_title().set_position((-340, 0))

        plt.savefig('b')
Ejemplo n.º 18
0
    def multi_water_level(self,path,title, mode='Raw'):
        
        self.create_header()
        
        ax = self.figure.add_subplot(self.grid_spec[1,0:])
        pos1 = ax.get_position() # get the original position 
        pos2 = [pos1.x0, pos1.y0,  pos1.width, pos1.height + .06] 
        ax.set_position(pos2) # set a new position
        
        #create the second graph title
        first_title = "%s %s Water Level Time Series Comparison" % (title, mode)
       
        titleText = ax.text(0.5, 1.065,first_title,  \
            va='center', ha='center', transform=ax.transAxes)
      
        ax.set_ylabel('Water Level in Feet')
        ax.set_xlabel('Timezone GMT')
        
        #plot major grid lines
        ax.grid(b=True, which='major', color='grey', linestyle="-")

        #x axis formatter for dates (function format_date() below)
        ax.xaxis.set_major_formatter(ticker.FuncFormatter(self.format_date))
        
        legend_list, label_list = [], []
        air, sea, sea_file, air_file = False, False, '', ''
        file_types = ['.nc']
        for root, sub_folders, files in os.walk(path):
            for file_in_root in files:
                
                index = file_in_root.rfind('.')
                if file_in_root[index:] in file_types:    
                    file = ''.join([root,'\\',file_in_root])
                    try:         
                        nc.get_pressure(file)
                        sea = True
                        sea_file = file
                    except:
                        try:
                            nc.get_air_pressure(file)
                            air = True
                            air_file = file
                        except:
                            pass
                        
                    
                    if sea and air:  
                        sea, air = False, False 
                        
                        so = StormOptions()
                        so.air_fname = air_file
                        so.sea_fname = sea_file
                   
                        so.timezone = 'GMT'
                        so.daylight_savings = False
                        
                        so.get_meta_data()
                        so.get_air_meta_data()
                        so.get_raw_water_level()
                        so.get_surge_water_level()
                        so.test_water_elevation_below_sensor_orifice_elvation()
                        so.get_wave_water_level()
                        
                        
                        first_date = unit_conversion.convert_ms_to_date(so.sea_time[0], pytz.UTC)
                        last_date = unit_conversion.convert_ms_to_date(so.sea_time[-1], pytz.UTC)
    
                        first_date = mdates.date2num(first_date)
                        last_date = mdates.date2num(last_date)
           
                        self.time_nums = np.linspace(first_date, last_date, len(so.sea_time))
                        
                        lat = nc.get_variable_data(sea_file, 'latitude')
                        lon = nc.get_variable_data(sea_file, 'longitude')
    
                        stn_id = nc.get_global_attribute(sea_file, 'stn_instrument_id')
                        stn_station = nc.get_global_attribute(sea_file, 'stn_station_number')
                        
                        if mode=='Surge':
                            p1, = ax.plot(self.time_nums, so.surge_water_level, alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))
                        if mode=='Wave':
                            p1, = ax.plot(self.time_nums, so.wave_water_level, alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))
                        if mode=='Raw':
                            p1, = ax.plot(self.time_nums, so.raw_water_level, alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))
                        
                   
        legend = ax.legend(legend_list,label_list
        , \
                  bbox_to_anchor=(.95, 1.355), loc=1, borderaxespad=0.0, prop={'size':10.3},frameon=False,numpoints=1, \
                  title="EXPLANATION")
        legend.get_title().set_position((-340, 0))
                    
       
        plt.savefig(''.join([title,mode]))
Ejemplo n.º 19
0
 def extract_wind_v(self, fname):
     return nc.get_variable_data(fname, 'v')
Ejemplo n.º 20
0
    def multi_air_pressure(self,path, title):
        self.create_header()
        
        ax = self.figure.add_subplot(self.grid_spec[1,0:])
        pos1 = ax.get_position() # get the original position 
        pos2 = [pos1.x0, pos1.y0,  pos1.width, pos1.height + .06] 
        ax.set_position(pos2) # set a new position
        
        #create the second graph title
        first_title = "%s Air Pressure Time Series Comparison" % title
       
        titleText = ax.text(0.5, 1.065,first_title,  \
            va='center', ha='center', transform=ax.transAxes)
      
        ax.set_ylabel('Air Pressure in Inches of Mercury')
        ax.set_xlabel('Timezone GMT')
        
        #plot major grid lines
        ax.grid(b=True, which='major', color='grey', linestyle="-")

        #x axis formatter for dates (function format_date() below)
        ax.xaxis.set_major_formatter(ticker.FuncFormatter(self.format_date))
        
        legend_list, label_list = [], []
        
        file_types = ['.nc']
        for root, sub_folders, files in os.walk(path):
            for file_in_root in files:
                
                index = file_in_root.rfind('.')
                if file_in_root[index:] in file_types:    
                    file = ''.join([root,'\\',file_in_root])
                    try:         
                        air_pressure = nc.get_air_pressure(file) * unit_conversion.DBAR_TO_INCHES_OF_MERCURY
                        time = nc.get_time(file)
                        
                        first_date = unit_conversion.convert_ms_to_date(time[0], pytz.UTC)
                        last_date = unit_conversion.convert_ms_to_date(time[-1], pytz.UTC)

                        first_date = mdates.date2num(first_date)
                        last_date = mdates.date2num(last_date)
           
                        self.time_nums = np.linspace(first_date, last_date, len(time))
                        
                        lat = nc.get_variable_data(file, 'latitude')
                        lon = nc.get_variable_data(file, 'longitude')
                        stn_id = nc.get_global_attribute(file, 'stn_instrument_id')
                        stn_station = nc.get_global_attribute(file, 'stn_station_number')

                        p1, = ax.plot(self.time_nums, air_pressure, alpha=.5)
                        legend_list.append(p1)
                        label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                          %(stn_id, stn_station, lat, lon))
                        
                    except:
                        pass
                    
        legend = ax.legend(legend_list,label_list
        , \
                  bbox_to_anchor=(.95, 1.355), loc=1, borderaxespad=0.0, prop={'size':10.3},frameon=False,numpoints=1, \
                  title="EXPLANATION")
        legend.get_title().set_position((-340, 0))
                    
        plt.savefig('b')
Ejemplo n.º 21
0
def run_wave_stats(p_window):
    sea_pressure = nc.get_variable_data(sea_fname, 'sea_pressure')
    sea_time = nc.get_variable_data(sea_fname, 'time')
    air_pressure = nc.get_variable_data(air_fname, 'air_pressure')
    air_time = nc.get_variable_data(air_fname, 'time' )
    
    c_pressure = sea_pressure - np.interp(sea_time,air_time,air_pressure)
    start_index, end_index, step = 0, 4096, 2048
    
    corrected_pressure = []
    while end_index < len(c_pressure):
        corrected_pressure.append(c_pressure[start_index:end_index])
        start_index += step
        end_index += step
        
    corrected_pressure = np.array(corrected_pressure)
    # time2 = nc.get_variable_data(fname, 'time2')
    # corrected_pressure = nc.get_variable_data(fname,'P_1ac')
    # depth = float(nc.get_variable_data(fname, 'depth'))
    pspec = nc.get_variable_data(fname2, 'pspec')
    file_freq = nc.get_variable_data(fname2, 'frequency')
     
    sig_height = nc.get_variable_data(fname2,'wh_4061')
    average_period = nc.get_variable_data(fname2,'wp_4060')

    stat_o = Stats()
    
    our_sig_wave_height = []
    our_avg_period = []
    
    get_index = []
     
    fs = 4.0
    for x in range(0, len(corrected_pressure)):
        
        get_index.append(x)
        ms = np.array([1430049600000.0 + (y * 166.66667) for y in range(0,len(corrected_pressure))])
        final_pressure = corrected_pressure[x]
        
    #     coeff = np.polyfit(ms, final_pressure, 1)
    #     lin_trend = coeff[1] + coeff[0]*ms
    #     detrended_pressure = detrend(final_pressure, type='linear', axis=-1)
    # #     final_pressure - lin_trend
    #
        window = np.hanning(p_window)
    # #     window = window ** 2
    #     windowed_pressure = detrended_pressure 
        #perform real fourier function on the scaled pressure and get the frequencies
    
        
        amps = []
        freqs= np.fft.rfftfreq(p_window, d=1/fs)
         
        final_pressure -= np.mean(final_pressure)
        final_pressure = detrend(final_pressure, type='linear', axis=-1) 
        
        for y in range(0, int(4096/p_window)):
            p = final_pressure[y * p_window:y * p_window + p_window]
            
            p *= window
             
             
            amps.append(np.fft.rfft(p))
            
           
        if p_window == 4096:
            print('4096!!')
            amp_sum = amps[0]
        else: 
            amp_sum = np.array(amps).sum(axis=0)
        
        amp_sum *= np.conjugate(amp_sum)
        scale = 1.0 / (fs * (window**2).sum())      
        amps = amp_sum * scale
        amps = amps.real 
    #     print(amps)
        freqs,amps = welch(final_pressure, 6, window = [1,1,1,1,1,1,1,1,1,1,1,1,1], nperseg=256, noverlap=0, detrend='linear')
Ejemplo n.º 22
0
    def multi_water_level(self, path, title, mode='Raw'):

        self.create_header()

        ax = self.figure.add_subplot(self.grid_spec[1, 0:])
        pos1 = ax.get_position()  # get the original position
        pos2 = [pos1.x0, pos1.y0, pos1.width, pos1.height + .06]
        ax.set_position(pos2)  # set a new position

        #create the second graph title
        first_title = "%s %s Water Level Time Series Comparison" % (title,
                                                                    mode)

        titleText = ax.text(0.5, 1.065,first_title,  \
            va='center', ha='center', transform=ax.transAxes)

        ax.set_ylabel('Water Level in Feet')
        ax.set_xlabel('Timezone GMT')

        #plot major grid lines
        ax.grid(b=True, which='major', color='grey', linestyle="-")

        #x axis formatter for dates (function format_date() below)
        ax.xaxis.set_major_formatter(ticker.FuncFormatter(self.format_date))

        legend_list, label_list = [], []
        air, sea, sea_file, air_file = False, False, '', ''
        file_types = ['.nc']
        for root, sub_folders, files in os.walk(path):
            for file_in_root in files:

                index = file_in_root.rfind('.')
                if file_in_root[index:] in file_types:
                    file = ''.join([root, '\\', file_in_root])
                    try:
                        nc.get_pressure(file)
                        sea = True
                        sea_file = file
                    except:
                        try:
                            nc.get_air_pressure(file)
                            air = True
                            air_file = file
                        except:
                            pass

                    if sea and air:
                        sea, air = False, False

                        so = StormOptions()
                        so.air_fname = air_file
                        so.sea_fname = sea_file

                        so.timezone = 'GMT'
                        so.daylight_savings = False

                        so.get_meta_data()
                        so.get_air_meta_data()
                        so.get_raw_water_level()
                        so.get_surge_water_level()
                        so.test_water_elevation_below_sensor_orifice_elvation()
                        so.get_wave_water_level()

                        first_date = unit_conversion.convert_ms_to_date(
                            so.sea_time[0], pytz.UTC)
                        last_date = unit_conversion.convert_ms_to_date(
                            so.sea_time[-1], pytz.UTC)

                        first_date = mdates.date2num(first_date)
                        last_date = mdates.date2num(last_date)

                        self.time_nums = np.linspace(first_date, last_date,
                                                     len(so.sea_time))

                        lat = nc.get_variable_data(sea_file, 'latitude')
                        lon = nc.get_variable_data(sea_file, 'longitude')

                        stn_id = nc.get_global_attribute(
                            sea_file, 'stn_instrument_id')
                        stn_station = nc.get_global_attribute(
                            sea_file, 'stn_station_number')

                        if mode == 'Surge':
                            p1, = ax.plot(self.time_nums,
                                          so.surge_water_level,
                                          alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))
                        if mode == 'Wave':
                            p1, = ax.plot(self.time_nums,
                                          so.wave_water_level,
                                          alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))
                        if mode == 'Raw':
                            p1, = ax.plot(self.time_nums,
                                          so.raw_water_level,
                                          alpha=.5)
                            legend_list.append(p1)
                            label_list.append('STN Site ID: %s, STN Instrument ID: %s, Lat: %s, Lon: %s' \
                                              %(stn_id, stn_station, lat, lon))


        legend = ax.legend(legend_list,label_list
        , \
                  bbox_to_anchor=(.95, 1.355), loc=1, borderaxespad=0.0, prop={'size':10.3},frameon=False,numpoints=1, \
                  title="EXPLANATION")
        legend.get_title().set_position((-340, 0))

        plt.savefig(''.join([title, mode]))
Ejemplo n.º 23
0
 def extract_wind_v(self, fname):
     return nc.get_variable_data(fname, 'v')
Ejemplo n.º 24
0
    def display(self):
#         try:
        t = nc.get_time(self.filename) / 1000

        #get datum
        datum = nc.get_geospatial_vertical_reference(self.filename)
#             times =  nc.get_datetimes(self.file_name)
        
#             d = nc.get_depth(self.filename) 
        d = nc.get_variable_data(self.filename, 'wave_wl') 
        tstep = t[1] - t[0]
        
        #STATISTICS FUNCTION AVAILABLE FOR PLOTTING
        periodfunc = lambda depth: stats.significant_wave_period(depth, tstep)
        sigfunc = stats.significant_wave_height_standard
        t10func = lambda depth: stats.ten_percent_wave_height(t, depth)
        t1func = lambda depth: stats.one_percent_wave_height(t, depth)
        rms_func = lambda depth: stats.rms_wave_height(t, depth)
        median_func = lambda depth: stats.median_wave_height(t, depth)
        maximum_func = lambda depth: stats.maximum_wave_height(t, depth)
        average_func = lambda depth: stats.average_wave_height(t, depth)
        average_z_func = lambda depth: stats.average_zero_crossing_period(t, depth)
        mean_func = lambda depth: stats.mean_wave_period(t, depth)
        crest_func = lambda depth: stats.crest_wave_period(t, depth)
        peak_wave_func = lambda depth: stats.peak_wave_period(t, depth)
        
        funcs = {'H1/3': (sigfunc, 'H 1/3 (feet)'), # contains functions and labels
                 'T1/3': (periodfunc, 'T 1/3 (seconds)'),
                 'T 10%': (t10func, 'T 10%'),
                 'T 1%': (t1func, 'T 1%'),
                 'RMS' : (rms_func, 'RMS'),
                 'Median': (median_func, 'Median'),
                 'Maximum': (maximum_func, 'Maximum'),
                 'Average': (average_func, 'Average'),
                 'Average Z Cross': (average_z_func, 'Average Z Cross'),
                 'Mean Wave Period': (mean_func, 'Mean Wave Period'),
                 'Crest': (crest_func, 'Crest'),
                 'Peak Wave': (peak_wave_func, 'Peak')}
        
        size = self.sizes[self.chunk_size.get()] * float(self.number.get())
        try:
            tchunks = stats.split_into_chunks(t, tstep, size)
            dchunks = stats.split_into_chunks(d, tstep, size)
        except ValueError:
            print('no chunks')
            tchunks = [t]
            dchunks = [d]
        t_stat = [np.average(tchunk) for tchunk in tchunks]
        # t_stat = [uc.convert_ms_to_datestring(t, pytz.utc) for t in t_stat]
        func = funcs[self.stat.get()][0]
        label = funcs[self.stat.get()][1]
        d_stat = [func(dchunk) for dchunk in dchunks]
        print(d_stat)
        
        #The first x axis time conversion
        t = [uc.convert_ms_to_date(x * 1000, pytz.UTC) for x in t]
        t = uc.adjust_from_gmt(t, self.tzstringvar.get(), self.daylightSavings.get())
        t = [mdates.date2num(x) for x in t]
        
        #The second x axis time conversion
        t_stat = [uc.convert_ms_to_date(x * 1000, pytz.UTC) for x in t_stat]
        t_stat = uc.adjust_from_gmt(t_stat, self.tzstringvar.get(), self.daylightSavings.get())
        t_stat = [mdates.date2num(x) for x in t_stat]
        
        self.plot_stats(t, d, t_stat, d_stat, label, datum)
        plt.show()