def set_matrix_col(self, sCol_name, npCol): """ Set matrix column with a new one. Overwrite the old values. Parameters: sCol_name (string): tag of the column to set npCol (numpy array): the array corresponding to this column. """ # FFTODO set multi col iCol = self.index_of_matrix_col(sCol_name)[0] if not self.is_readonly(): if iCol > len(self.npMatrix[0, :]): sOutOfBoundError = _("Array does not contain this indice: %d") \ % iCol raise metro_error.Metro_data_error(sOutOfBoundError) elif len(self.npMatrix[:, iCol]) != len(npCol): sLengthError = _("Array does not have the right lenght.\n") + \ _("Array length: %d \n") % len(npCol) + \ _("Matrix length: %d \n") % len(self.npMatrix[:,iCol]) raise metro_error.Metro_data_error(sLengthError) else: self.npMatrix[:, iCol] = npCol else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def __check_if_all_value_are_numbers(self, wf_controlled_data): """ Check if all the values in the forecast are numbers. """ for sElement in wf_controlled_data.get_matrix_col_list(): if sElement.find('TIME') > 0: continue npElement = wf_controlled_data.get_matrix_col(sElement) # In the case of 'CC', only return an error if both SF and IR # are not given if sElement is 'CC': if metro_config.get_value('SF') and metro_config.get_value( 'IR'): continue for fElement in npElement: if fpconst.isNaN(fElement): if wf_controlled_data.is_standardCol(sElement): sMessage = _("Value in forecast file must be valid.\n") \ + _("A value for the element <%s> is invalid")\ % (sElement.lower())+\ _(" in the file\n'%s'") %\ (metro_config.get_value(\ "FILE_FORECAST_IN_FILENAME")) raise metro_error.Metro_data_error(sMessage) else: sMessage = _("A value for the extended element <%s> is invalid") % (sElement.lower())+\ _(" in the file\n'%s'") % (metro_config.get_value("FILE_FORECAST_IN_FILENAME")) metro_logger.print_message(metro_logger.LOGGER_MSG_STOP,\ sMessage)
def __check_precipitation_well_define(self, wf_controlled_data): """ Name: __check_precipitation_well_define Parameters:[I] metro_data wf_controlled_data : controlled data. Read-only data. Returns: None Functions Called: self.forecast_data.get_matrix_col Description: Check if the values of precipitations represent the total amount of precipitation from the beginning of the period. Thus, all the values must monotone in the RA and SN array. Notes: Revision History: Author Date Reason Miguel Tremblay February 28th 2005 """ lPrecip = [] lPrecip.append(wf_controlled_data.get_matrix_col('RA')) lPrecip.append(wf_controlled_data.get_matrix_col('SN')) for npPrecip in lPrecip: npDiff = npPrecip - metro_util.shift_right(npPrecip, 0) if len(numpy.where(npDiff[:-1] < 0)[0]) != 0: sMessage = _("All the precipitation value (<ra> and <sn>)") +\ _(" of atmospheric forecast represent the TOTAL") +\ _(" amount of precipitation since the beginning") +\ _(" of the period. See file '%s'") %\ (metro_config.get_value("FILE_FORECAST_IN_FILENAME")) raise metro_error.Metro_data_error(sMessage)
def __check_if_cloud_octal(self, wf_controlled_data): """ Name: __check_if_cloud_octal Parameters:[I] metro_data wf_controlled_data : controlled data. Read-only data. Returns: None Functions Called: self.forecast_data.get_matrix_col Description: Check if all the value of the variable CC is in [0,8] Notes: Metro gives funny surface temperature if a strange value of CC is entered. Raise an exception if this occurs. Revision History: Author Date Reason Miguel Tremblay February 28th 2005 """ npCloudsOctal = wf_controlled_data.get_matrix_col('CC') if len(numpy.where(npCloudsOctal<0)[0]) != 0 or \ len(numpy.where(npCloudsOctal>8)[0]) != 0 : import metro_config sMessage = _("All the clouds value (<cc>) of atmospheric forecast must be") +\ _(" integers in interval 0-8. See file '%s'") %\ (metro_config.get_value("FILE_FORECAST_IN_FILENAME")) raise metro_error.Metro_data_error(sMessage)
def append_matrix_multiCol(self, sCol_name, lColOfList): """ Name: append_matrix_col Parameters: I sCol_name : column name I npData_col : column to insert in the matrix. Returns: 0 if success Descriptions: Append a new column of data to the matrix. Matix column will be accessible with the name specified by sCol_name. Column will be treated as extended """ if not self.is_readonly(): if sCol_name not in self.lMatrix_col_name: self.lMatrix_col_name.append(sCol_name) if self.lMatrix_col_usage == []: nextCol = 0 else: nextCol = self.lMatrix_col_usage[-1][-1] + 1 nbCol = len(lColOfList[0]) self.lMatrix_col_usage.append(range(nextCol, nextCol + nbCol)) self.lMatrix_ext_col_name.append(sCol_name) # Append column in the matrix for i in range(0, nbCol): colToInsert = [] for col in lColOfList: colToInsert.append(col[i]) self.npMatrix = self.__append_col_to_matrix(self.npMatrix,\ colToInsert) else: sError = _("Cant append column '%s'.%s") % (sCol_name, MESSAGE_COL_EXIST) raise metro_error.Metro_data_error(sError) else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def index_of_matrix_col(self, sCol_name): """Get index value of a matrix column identified by sCol_name.""" if sCol_name in self.lMatrix_col_name: index = self.lMatrix_col_name.index(sCol_name) return self.lMatrix_col_usage[index] else: sMatrix_col_list = metro_util.list2string(self.lMatrix_col_name) sError = _("%s is not a valid column name. Valid column name ") \ % (sCol_name) + \ _("are: %s") % (sMatrix_col_list) raise metro_error.Metro_data_error(sError)
def set_matrix(self, npMatrix): """Set the whole matrix with a new one. This method should be used with care because the new and old matrix must have the same column count. Also column name of the old matrix will be the only way to access column of the new one. """ if not self.is_readonly(): self.npMatrix = npMatrix else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def init_matrix(self, iNb_row, iNb_col, fVal=metro_constant.NaN): """ Init a matrix of n row and m column filled with a value. """ if not self.is_readonly(): self.npMatrix = numpy.zeros((iNb_row, iNb_col)) self.npMatrix = self.npMatrix + fVal else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def set_matrix_multiCol(self, sCol_name, lColOfList): """ Name: set_matrix_col Parameters: I sCol_name : column name I npData_col : column to insert in the matrix. Returns: 0 if success Descriptions: Append a new column of data to the matrix. Matix column will be accessible with the name specified by sCol_name. Column will be treated as extended """ # FFTODO set multi col lCol_list = self.index_of_matrix_col(sCol_name) if not self.is_readonly(): if lCol_list[-1] > len(self.npMatrix[0, :]): sOutOfBoundError = _("Array does not contain this indice: %s") \ % str(lCol_list) raise metro_error.Metro_data_error(sOutOfBoundError) # FFTODO need to activate that check # elif len(self.npMatrix[:,lColOfList[0]]) != len(lColOfList[0]): # sLengthError = _("Array does not have the right lenght.\n") + \ # _("Array length: %d \n") % len(lColOfList[0]) + \ # _("Matrix length: %d \n") % len(self.npMatrix[:,lColOfList[0]]) # raise metro_error.Metro_data_error(sLengthError) else: iInCol = 0 for iCol in lCol_list: self.npMatrix[:, iCol] = lColOfList[iInCol] iInCol += 1 else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def del_matrix_row(self, npIndiceToRemove): """ Delete one or more row identified by indices. Used in QC to remove wrong data. Indices must be in increasing order. Arguments: npIndiceToRemove (numpy) of indices to remove. Indices must be in increasing order. """ if not self.is_readonly(): for i in range(len(npIndiceToRemove) - 1, -1, -1): nIndice = npIndiceToRemove[i] sMessage = "removing %d" % ((nIndice)) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) npFirstPart = self.npMatrix[0:nIndice, :] sMessage = "len(%s)" % (len(self.npMatrix)) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) if nIndice + 1 < len(self.npMatrix): npSecondPart = self.npMatrix[nIndice + 1:len(self.npMatrix), :] self.npMatrix = numpy.concatenate( (npFirstPart, npSecondPart)) else: self.npMatrix = npFirstPart # Check if there at least one element left if len(self.npMatrix) == 0: sEmptyMatrixError = _("All the data are invalid") metro_logger.print_message(metro_logger.LOGGER_MSG_WARNING,\ sMessage) raise metro_error.Metro_data_error(sEmptyMatrixError) else: raise metro_error.Metro_data_error(MESSAGE_READONLY)
def get_header_value(self, sKey): """ Get value of a specific header key Parameters: sKey (string): key of the value to return (tag of element) Returns value (undefined format) for this key. """ if sKey in self.dHeader: return self.dHeader[sKey] else: sError = _("'%s' is not a valid key. Valid keys are:\n%s") \ % (sKey, metro_util.list2string(self.dHeader.keys())) raise metro_error.Metro_data_error(sError)
def set_header(self, dComplete_header): """ Set complete header Parameters: dComplete_header (dictionnary): dictionnary containing the tag of the elements (the keys) and their corresponding value. """ if not self.is_readonly(): self.dHeader = dComplete_header else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def set_header_value(self, sKey, value): """ Set/add a value to the header Parameters: sKey (string): element to set this value to value (??): value of the element sKey """ if not self.is_readonly(): self.dHeader[sKey] = value else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)
def append_matrix_row(self, lData_row): """ Name: append_matrix_row Parameters: I lData_row : line of the matrix Returns: 0 if success Descriptions: Append a new row of data to the matrix. """ if not self.is_readonly(): # Replace the None by metro_constant.NaN while None in lData_row: iIndex = lData_row.index(None) lData_row[iIndex] = metro_constant.NaN self.npMatrix = self.__append_row_to_matrix( self.npMatrix, lData_row) else: metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, MESSAGE_READONLY) raise metro_error.Metro_data_error(MESSAGE_READONLY)