Beispiel #1
0
    def _single_file(self):
        """process for converting a single file"""

        _f = self._filename

        if self.platform == 'linux':

            self.sdr_path = windows_folder_path(self.sdr_path)[:-1]
            _f = windows_folder_path(_f)[:-1]
            wine = 'wine'

        else:
            wine = ''

        self.cmd = [
            wine, '"' + self.sdr_path + '"', self.command_switch,
            self.encryption_pin, '"' + _f + '"'
        ]

        try:
            if self.show_result:
                if self.progress_bar:
                    draw_progress_bar(self.counter, self.raw_count,
                                      self.start_time)

                else:
                    print("Converting  {0}/{1}  {2}  ...  ".format(
                        str(self.counter).rjust(self.pad),
                        str(self.raw_count).ljust(self.pad),
                        _f.split("\\")[-1]),
                          end="",
                          flush=True)

            subprocess.check_output(" ".join(self.cmd), shell=True)
            # subprocess.run(" ".join(self.cmd), stdout=subprocess.PIPE)

            if not self.progress_bar and not self.show_result:
                print("[DONE]")

            try:
                self._copy_txt_file()
            except:
                print('unable to copy {} to text folder'.format(_f))

        except:
            if not self.progress_bar and not self.show_result:
                print("[FAILED]")
            import traceback
            print(traceback.format_exc())
Beispiel #2
0
    def upload_file(self):
        if self.progress_bar:
            draw_progress_bar(self.counter, self.raw_count, self.start_time)
        elif self.raw_count == 1:
            print("{0} | API | uploading {1} ... ".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), os.path.basename(self.filename)), end="", flush=True)
        else:
            print("{0} | API | uploading {1}/{2} ... {3} ... ".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), str(self.counter).rjust(self.pad), str(self.raw_count).ljust(self.pad), os.path.basename(self.filename)), end="", flush=True)

        self.encoded_rld_bytes = self.prepare_file_bytes(self.filename)

        data = {
            'type': self.filename[-3:].upper(),
            'filebytes': self.encoded_rld_bytes
        }

        self.response = requests.request("POST", upload_url, headers=self.headers, data=data)

        if self.progress_bar is False:
            if int(self.response.status_code) < 300:
                print("[OK]")
            else:
                print(f"[FAILED] {self.response.status_code}")
Beispiel #3
0
    def concat_txt(self,
                   txt_dir='',
                   file_type='meas',
                   file_filter='',
                   filter2='',
                   start_date='1970-01-01',
                   end_date='2150-12-31',
                   ch_details=False,
                   output_txt=False,
                   out_file='',
                   progress_bar=True,
                   **kwargs):
        """Will concatenate all text files in the txt_dir

        files must match the site_filter argument. Note these are both blank by default.

        Parameters
        ----------
        txt_dir : str
            directory holding txt files
        file_type : str
            type of export (meas, event, comm, sample, etc...)
        file_filter : str
            text filter for txt files, like site number, etc.
        filter2 : str
            secondary text filter
        start_date : str
            for filtering files to concat based on date "YYYY-mm-dd"
        end_date : str
            for filtering files to concat based on date "YYYY-mm-dd"
        ch_details : bool
            show additional info in ch_info dataframe
        output_txt : bool
            create a txt output of data df
        out_file : str
            filename to write data dataframe too if output_txt = True
        progress_bar : bool
            show bar on concat [True] or list of files [False]

        Returns
        ---------
        ch_info : obj
            pandas dataframe of ch_list (below) pulled out of file with sympro_txt_read.arrange_ch_info()
        ch_list : list
            list of channel info; can be converted to json w/ import json ... json.dumps(fut.ch_info)
        data : obj
            pandas dataframe of all data
        head : obj
            lines at the top of the txt file..., used when rebuilding timeshifted files
        site_info : obj
            pandas dataframe of site information
        logger_sn : str
        ipack_sn : str
        logger_type : str
        ipack_type : str
        latitude : float
        longitude : float
        elevation : int
        site_number : str
        site_description : str
        start_date : str
        txt_file_names : list
            list of files included in concatenation

        Examples
        --------
        Read files into nrgpy reader object

        >>> import nrgpy
        >>> reader = nrgpy.sympro_txt_read()
        >>> reader.concat_txt(
                txt_dir='/path/to/txt/files/',
                file_filter='123456', # site 123456
                start_date='2020-01-01',
                end_date='2020-01-31',
            )
        Time elapsed: 2 s | 33 / 33 [=============================================] 100%
        Queue processed
        >>> reader.logger_sn
        '820600019'
        >>> reader.ch_info
         	Bearing: 	Channel: 	Description: 	Effective Date: 	Height: 	Offset: 	Scale Factor: 	Serial Number: 	Type: 	Units:
        0 	50.00 	    1 	        NRG S1 	        2020-01-31 00:00:00 	33.00 	0.13900 	0.09350 	    94120000059 	Anemometer 	m/s
        1 	230.00 	    2 	        NRG S1 	        2020-01-31 00:00:00 	0.00 	0.13900 	0.09350 	    94120000058 	Anemometer 	m/s
        2 	50.00 	    3 	        NRG S1 	        2020-01-31 00:00:00 	22.00 	0.13900 	0.09350 	    94120000057 	Anemometer 	m/s
        3 	230.00 	    4 	        NRG 40C Anem 	2020-01-31 00:00:00 	22.00 	0.35000 	0.76500 	    179500324860 	Anemometer 	m/s
        4 	50.00 	    5 	        NRG 40C Anem 	2020-01-31 00:00:00 	12.00 	0.35000 	0.76500 	    179500324859 	Anemometer 	m/s
        5 	230.00 	    6 	        NRG S1 	        2020-01-31 00:00:00 	12.00 	0.13900 	0.09350 	    94120000056 	Anemometer 	m/s
        6 	320.00 	    13 	        NRG 200M Vane 	2020-01-31 00:00:00 	32.00 	-1.46020 	147.91100 	    10700000125 	Vane 	        Deg
        7 	320.00 	    14 	        NRG 200M Vane 	2020-01-31 00:00:00 	21.00 	-1.46020 	147.91100 	    10700000124 	Vane 	        Deg
        8 	0.00 	    15 	        NRG T60 Temp 	2020-01-31 00:00:00 	34.00 	-40.85550 	44.74360 	    9400000705          Analog          C
        9 	0.00 	    16 	        NRG T60 Temp 	2020-01-31 00:00:00 	2.00 	-40.85550 	44.74360 	    9400000xxx          Analog          C
        10 	0.00 	    17 	        NRG RH5X Humi 	2020-01-31 00:00:00 	0.00 	0.00000 	20.00000 	    NaN 	        Analog          %RH
        11 	0.00 	    20 	        NRG BP60 Baro 	2020-01-31 00:00:00 	0.00 	495.27700 	243.91400 	    NaN 	        Analog          hPa
        12 	0.00 	    21 	        NRG BP60 Baro 	2020-01-31 00:00:00 	2.00 	495.04400 	244.23900 	    9396FT1937          Analog  	hPa
        """

        if 'site_filter' in kwargs and file_filter == '':
            self.file_filter = kwargs.get('site_filter')
        else:
            self.file_filter = file_filter

        self.ch_details = ch_details
        self.start_date = start_date
        self.end_date = end_date
        self.filter2 = filter2
        self.file_type = file_type
        self.txt_file_names = []

        if check_platform() == 'win32':
            self.txt_dir = windows_folder_path(txt_dir)
        else:
            self.txt_dir = linux_folder_path(txt_dir)

        first_file = True

        files = [
            f for f in sorted(glob(self.txt_dir + '*.txt'))\
            if self.file_filter in f and self.filter2 in f\
            and date_check(self.start_date, self.end_date, f)
        ]

        self.file_count = len(files)
        self.pad = len(str(self.file_count))
        self.counter = 1
        self.start_time = datetime.now()

        for f in files:

            if progress_bar:
                draw_progress_bar(self.counter, self.file_count,
                                  self.start_time)
            else:
                print("Adding {0}/{1} ... {2} ... ".format(
                    str(self.counter).rjust(self.pad),
                    str(self.file_count).ljust(self.pad), os.path.basename(f)),
                      end="",
                      flush=True)

            if first_file:
                first_file = False

                try:
                    base = sympro_txt_read(
                        f, text_timestamps=self.text_timestamps)
                    if progress_bar != True: print("[OK]")
                    self.txt_file_names.append(os.path.basename(f))
                except IndexError:
                    print('Only standard SymPRO headertypes accepted')
                    break
                except:
                    if progress_bar != True: print("[FAILED]")
                    print("could not concat {0}".format(os.path.basename(f)))
                    pass
            else:
                file_path = f

                try:
                    s = sympro_txt_read(
                        file_path,
                        ch_details=self.ch_details,
                        text_timestamps=self.text_timestamps,
                        site_details=False,
                    )
                    base.data = base.data.append(s.data)
                    base.ch_info = base.ch_info.append(s.ch_info)
                    if progress_bar != True: print("[OK]")
                    self.txt_file_names.append(os.path.basename(f))

                except:
                    if progress_bar != True: print("[FAILED]")
                    print("could not concat {0}".format(os.path.basename(f)))
                    pass

            self.counter += 1

        if out_file != "":
            self.out_file = out_file

        if output_txt:
            base.data.to_csv(os.path.join(txt_dir, out_file),
                             sep=',',
                             index=False)

        try:
            self.ch_info = s.ch_info
            self.ch_list = s.ch_list
            self.array = s.array
            self.data = base.data.drop_duplicates(subset=['Timestamp'],
                                                  keep='first')
            self.data.reset_index(drop=True, inplace=True)
            base.ch_info['ch'] = base.ch_info['Channel:'].astype(int)
            self.ch_info = base.ch_info.sort_values(by=['ch'])\
                .drop_duplicates(
                    subset=['Serial Number:', 'Channel:', 'Type:', 'Description:', 'Height:', 'Bearing:',
                            'Scale Factor:', 'Offset:', 'Units:'], ignore_index=True).drop(['ch'], 1)

            self.head = s.head
            self.site_info = s.site_info
            self.format_site_data()
            print("\n")

        except UnboundLocalError:
            print("No files match to contatenate.")
            return None
Beispiel #4
0
def shift_timestamps(txt_folder="",
                     out_folder="",
                     file_filter="",
                     start_date="1970-01-01",
                     end_date="2150-12-31",
                     seconds=3600):
    """Takes as input a folder of exported standard text files and
    time to shift in seconds.

    Parameters
    ----------
    txt_folder : str
        path to folder with txt files to shift
    out_folder : str
        where to put the shifted files (in subfolder by default)
    file_filter : str
        filter for restricting file set
    start_date : str
        date filter "YYYY-mm-dd"
    end_date : str
        date filter "YYYY-mm-dd"
    seconds : int
        time in seconds to shift timestamps (default 3600)

    Returns
    -------
    obj
        text files with shifted timestamps; new file names include shifted
        timestamp.

    """
    if out_folder:
        out_dir = out_folder
    else:
        out_dir = os.path.join(txt_folder, "shifted_timestamps")

    os.makedirs(out_dir, exist_ok=True)

    files = [
        f for f in sorted(glob(txt_folder + "/" + '*.txt'))\
        if file_filter in f and \
        date_check(start_date, end_date, f)
    ]

    file_count = len(files)
    counter = 1
    start_time = datetime.now()

    for f in files:

        try:
            draw_progress_bar(counter, file_count, start_time)
            f = os.path.join(txt_folder, f)
            fut = sympro_txt_read(filename=f)
            fut.format_site_data()
            fut.data['Timestamp'] = pd.to_datetime(
                fut.data['Timestamp']) + timedelta(seconds=seconds)
            fut.output_txt_file(shift_timestamps=True,
                                standard=False,
                                out_dir=out_dir,
                                out_file=f)
        except pd.errors.EmptyDataError:
            pass

        except Exception as e:
            print(traceback.format_exc())
            pass

        counter += 1
Beispiel #5
0
    def concat_txt(self, txt_dir='', output_txt=False, out_file='',
                   file_filter='', file_filter2='',
                   start_date='1970-01-01', end_date='2150-12-31',
                   progress_bar=True):
        """concatenate files in a folder

        parameters
        ----------
        txt_dir : str
            path to csv or csv.zip files
        output_txt : boolean
            export concatenated data
        out_file : str
            optional, filename of text export
        start_date : str
            yyy-mm-dd formatted string
        end_date : str
            yyy-mm-dd formatted string
        progress_bar : boolean
            show progress bar instead of each file being concatenated

        returns
        -------
        None
            adds data dataframe to reader object
        """
        self.txt_dir = txt_dir
        self.output_txt = output_txt
        self.out_file = out_file
        self.file_filter = file_filter
        self.file_filter2 = file_filter2
        self.start_date = start_date
        self.end_date = end_date

        if check_platform() == 'win32':
            self.txt_dir = windows_folder_path(txt_dir)
        else:
            self.txt_dir = linux_folder_path(txt_dir)

        first_file = True
        files = [
            f for f in sorted(glob(self.txt_dir + "*"))
            if self.file_filter in f and self.file_filter2 in f
            and date_check(self.start_date, self.end_date, f)
        ]

        self.file_count = len(files)
        self.pad = len(str(self.file_count))
        self.counter = 1
        self.start_time = datetime.now()

        for f in files:
            if self.file_filter in f and self.file_filter2 in f:
                if progress_bar:
                    draw_progress_bar(self.counter, self.file_count, self.start_time)
                else:
                    print("Adding {0}/{1}  ...  {2} ".format(str(self.counter).rjust(self.pad), str(self.file_count).ljust(self.pad), f), end="", flush=True)
                if first_file == True:
                    first_file = False
                    try:
                        base = spidar_data_read(f)
                        if progress_bar == False: print("[OK]")
                        pass
                    except IndexError:
                        print('Only standard Spidar headertypes accepted')
                        break
                else:
                    file_path = f
                    try:
                        s = spidar_data_read(file_path)
                        base.data = base.data.append(s.data, sort=False)
                        if not progress_bar:
                            print("[OK]")
                    except Exception as e:
                        if not progress_bar:
                            print("[FAILED]")
                        print("could not concat {0}".format(file_path))
                        print(e)
                        pass
            else:
                pass
            self.counter += 1

        if out_file != "":
            self.out_file = out_file
        if output_txt:
            base.data.to_csv(txt_dir + out_file, sep=',', index=False)

        try:
            self.base = base
            self.heights = base.heights
            self.serial_number = base.serial_number
            self.data = base.data.drop_duplicates(subset=['Timestamp'], keep='first')
            self.data.reset_index(drop=True, inplace=True)
            self.data.reset_index(drop=True, inplace=True)

        except Exception as e:
            print("No files match to contatenate.")
            print(e)
            return None
Beispiel #6
0
    def concat(self,
               output_txt=False,
               out_file='',
               file_filter='',
               filter2='',
               progress_bar=True):
        """combine exported rwd files (in txt format)

        parameters
        ----------
        output_txt : bool
            set to True to save a concatenated text file
        out_file : str
            filepath, absolute or relative
        file_filter : str
        filter2 : str
        progress_bar : bool
        """
        self.file_filter = file_filter

        if self.filter2 == '':
            self.filter2 = filter2

        if check_platform() == 'win32':
            self.txt_dir = windows_folder_path(self.txt_dir)
        else:
            self.txt_dir = linux_folder_path(self.txt_dir)

        first_file = True
        files = sorted(glob(self.txt_dir + '*.txt'))

        self.file_count = len(files)
        self.pad = len(str(self.file_count)) + 1

        self.counter = 1
        self.start_time = datetime.now()

        for f in files:

            if self.file_filter in f and self.filter2 in f:

                if progress_bar:
                    draw_progress_bar(self.counter, self.file_count,
                                      self.start_time)
                else:
                    print("Adding  {0}/{1}  {2}  ...  ".format(
                        str(self.counter).rjust(self.pad),
                        str(self.file_count).ljust(self.pad), f),
                          end="",
                          flush=True)

                if first_file:
                    first_file = False

                    try:
                        base = read_text_data(filename=f,
                                              data_type=self.data_type,
                                              file_filter=self.file_filter,
                                              file_ext=self.file_ext,
                                              sep=self.sep)
                        if not progress_bar:
                            print("[OK]")
                        pass

                    except IndexError:
                        print('Only standard headertypes accepted')
                        break

                else:
                    file_path = f
                    try:
                        s = read_text_data(filename=f,
                                           data_type=self.data_type,
                                           file_filter=self.file_filter,
                                           file_ext=self.file_ext,
                                           sep=self.sep)
                        base.data = base.data.append(s.data, sort=False)
                        if not progress_bar:
                            print("[OK]")
                    except:
                        if not progress_bar:
                            print("[FAILED]")
                        print("could not concat {0}".format(file_path))
                        pass
            else:
                pass
            self.counter += 1

        if output_txt:

            if out_file == "":
                out_file = f"{self.data_type}_" + datetime.today().strftime(
                    "%Y-%m-%d") + ".txt"
            base.data.to_csv(out_file, sep=',', index=False)
            self.out_file = out_file

        try:
            self.ch_info = s.ch_info
            self.ch_list = s.ch_list
            self.data = base.data.drop_duplicates(
                subset=[self.header_sections['data_header']], keep='first')
            self.head = s.head
            self.site_info = s.site_info
            self.filename = s.filename
            self.site_number = self.filename.split("\\")[-1][:4]
            self.format_rwd_site_data()

        except UnboundLocalError:
            print("No files match to contatenate.")
            return None
Beispiel #7
0
    def single_file(self, rld):
        try:
            if self.progress_bar:
                draw_progress_bar(self.counter, self.raw_count,
                                  self.start_time)
            else:
                print("Processing {0}/{1} ... {2} ... ".format(
                    str(self.counter).rjust(self.pad),
                    str(self.raw_count).ljust(self.pad),
                    os.path.basename(rld)),
                      end="",
                      flush=True)

            self.encoded_rld_bytes = self.prepare_file_bytes(rld)

            if self.nec_file:
                self.encoded_nec_bytes = self.prepare_file_bytes(self.nec_file)
            else:
                self.encoded_nec_bytes = ''

            if not self.token_valid():
                self.session_token, self.session_start_time = self.request_session_token(
                )

            headers = {"Authorization": "Bearer {}".format(self.session_token)}

            self.data = {
                'type': rld[-3:].upper(),
                'filebytes': self.encoded_rld_bytes,
                'necfilebytes': self.encoded_nec_bytes,
                'headertype':
                self.header_type,  # standard | columnonly  | none
                'exporttype':
                self.export_type,  # measurements (default) | samples
                'exportformat':
                self.export_format,  # csv_zipped (default)   | parquet
                'encryptionkey': self.encryption_pass,
                'columnheaderformat': '',  # not implemented yet
            }

            self.resp = requests.post(data=self.data,
                                      url=convert_url,
                                      headers=headers)

            zipped_data_file = zipfile.ZipFile(io.BytesIO(self.resp.content))
            reg_data_file = self.resp.content
            name = zipped_data_file.infolist().pop()
            out_filename = os.path.basename(rld)[:-3] + 'txt'

            with open(os.path.join(self.out_dir, out_filename),
                      'wb') as outputfile:
                outputfile.write(zipped_data_file.read(name))

            try:
                filename = os.path.join(self.out_dir, out_filename)
                file_contents = open(filename, "r").read()
                f = open(filename, "w", newline="\r\n")
                f.write(file_contents)
                f.close()

            except:
                print(
                    "Could not convert Windows newline characters properly; file may be unstable"
                )

            if self.progress_bar is False:
                print("[DONE]")

        except Exception as e:
            if self.progress_bar is False:
                print("[FAILED]")

            print('unable to process file: {0}'.format(rld))
            print(e)
            print(str(self.resp.status_code) + " " + self.resp.reason + "\n")
            pass