def validate_roadcast_start_time(dConf): sKey = 'INIT_ROADCAST_START_DATE' sStart_time = dConf[sKey]['VALUE'] if sStart_time != "": try: metro_date.parse_date_string(sStart_time) except metro_error.Metro_date_error, inst: sMessage = _("Fatal error, the date string '%s' passed to the\n ")\ % (sStart_time)+\ _("option '--roadcast-start-date' doesn't conform to ") +\ _("ISO 8601") metro_logger.print_init_message(metro_logger.LOGGER_INIT_ERROR, sMessage) sys.exit(3)
def read_date(sTag, nodeBranch): sDate = metro_xml.extract_data_from_node(sTag, nodeBranch) try: fDate = metro_date.parse_date_string(sDate) except metro_error.Metro_date_error, inst: metro_logger.print_message(metro_logger.LOGGER_MSG_WARNING, str(inst)) fDate = 0.0
def read_date(sTag,nodeBranch): sDate = metro_xml.extract_data_from_node(sTag,nodeBranch) try: fDate = metro_date.parse_date_string(sDate) except metro_error.Metro_date_error, inst: metro_logger.print_message(metro_logger.LOGGER_MSG_WARNING, str(inst)) fDate = 0.0
def __set_first_roadcast_date(self, observation_data, roadcast_data): """ Name: ___set_first_roadcast_date Parameters: Observation and roadcast Description: If the date of the first roadcast is not specified in the input (command line or config file), set it to first round 20 minutes after the last observation. Author: Miguel Tremblay Date: March 5th 2007 """ # Check if the value is given as an argument if metro_config.get_value('INIT_ROADCAST_START_DATE') != "": # Set the xml header value controlled_roadcast = roadcast_data.get_subsampled_data() controlled_roadcast.set_header_value('FIRST_ROADCAST',\ metro_config.get_value('INIT_ROADCAST_START_DATE')) return 0 else : # if value of INIT_ROADCAST_START_DATE is not given, fetch the first # round 20 minutes. naOT = observation_data.get_matrix_col('OBSERVATION_TIME') fLast_observation_date = naOT[len(naOT)-1] sLast_observation_date = metro_date.seconds2iso8601(fLast_observation_date) # Fetch the first 20 minutes value. iNb_timesteps = roadcast_data.get_attribute('FORECAST_NB_TIMESTEPS') nSecondsForOutput = metro_constant.nMinutesForOutput*60 lTimeStep = roadcast_data.get_controlled_data().get_matrix_col('HH') for i in range(0, iNb_timesteps): fCurrentTime = lTimeStep[i]*3600 # Forecast at every 20 minutes, i.e. 1200 seconds # if current time is a 20 minute interval # and roadcast time is >= roadcast start date if round(fCurrentTime)%nSecondsForOutput == 0: sLast_observation = metro_config.\ get_value('DATA_ATTRIBUTE_LAST_OBSERVATION') fLast_observation = metro_date.parse_date_string(sLast_observation) sStart_date = metro_date.seconds2iso8601(fLast_observation + \ (i+1)*\ metro_constant.fTimeStep) metro_config.set_value('INIT_ROADCAST_START_DATE', sStart_date) sMessage = _("Roadcast start date set to: ''%s'") % (sStart_date) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE,\ sMessage) # Set the xml header value controlled_roadcast = roadcast_data.get_subsampled_data() controlled_roadcast.set_header_value('FIRST_ROADCAST', sStart_date) return i # Something bad hapened. sMessage = _("Unable to determine the first time of roadcast!") metro_logger.print_message(metro_logger.LOGGER_MSG_STOP,\ sMessage)
def __send_data_to_metro_core( self, forecast, observation, station ): wf_data = forecast.get_interpolated_data() ro_data = observation.get_interpolated_data() cs_data = station # Start time from model is the last observation sStart_time = metro_config.get_value('DATA_ATTRIBUTE_LAST_OBSERVATION') fStart_time = metro_date.parse_date_string(sStart_time) iModel_start_y = metro_date.get_year(fStart_time) sMessage = _("year: [%s]") % (iModel_start_y) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_m = metro_date.get_month(fStart_time) sMessage = _("month: [%s]") % (iModel_start_m) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_d = metro_date.get_day(fStart_time) sMessage = _("day: [%s]") % (iModel_start_d) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_h = metro_date.get_hour(fStart_time) sMessage = _("hour: [%s]") % (iModel_start_h) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) dStation_header = cs_data.get_header() # test observation dObservation_header = ro_data.get_header() lObservation_data = ro_data.get_matrix() sMessage = "Observation_header=" + str(dObservation_header) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = "Observation_data=" + str(lObservation_data) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # test forecast dForecast_header = wf_data.get_header() lForecast_data = wf_data.get_matrix() sMessage = "Forecast_header=" + str(dForecast_header) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = "Forecast_data=" + str(lForecast_data) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # start roadlayer MATRIX npLayerType = cs_data.get_matrix_col('TYPE') lLayerType = npLayerType.astype(numpy.int32).tolist() lLayerThick = cs_data.get_matrix_col('THICKNESS').tolist() nNbrOfLayer = len(lLayerType) sMessage = _("Number of layer=") + str(nNbrOfLayer) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # Append an empty box for the manuel mode lLayerType.append(0) lLayerThick.append(0.0) sMessage = _("roadlayer type=") + str(lLayerType) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = _("roadlayer thick=") + str(lLayerThick) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # end roadlayer MATRIX fTime = dStation_header['PRODUCTION_DATE'] fTimeForecast = dForecast_header['PRODUCTION_DATE'] sTimeZone = dStation_header['TIME_ZONE'] sMessage = _("timezone=") + sTimeZone metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) fIssue_time = fStart_time sMessage = _("issue time=") + time.ctime(fIssue_time) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) wforiginal = forecast.get_original_data() ft = wforiginal.get_matrix_col('FORECAST_TIME') ################################################################## # Forecast # Get the interpolated values. wf_interpolated_data = forecast.get_interpolated_data() npAT = wf_interpolated_data.get_matrix_col('AT') lAT = npAT.tolist() lQP = wf_interpolated_data.get_matrix_col('QP').tolist() npWS = wf_interpolated_data.get_matrix_col('WS') lWS = npWS.tolist() npTD = wf_interpolated_data.get_matrix_col('TD') lTD = npTD.tolist() lAP = wf_interpolated_data.get_matrix_col('AP').tolist() lSF = wf_interpolated_data.get_matrix_col('SF').tolist() lIR = wf_interpolated_data.get_matrix_col('IR').tolist() npFA = wf_interpolated_data.get_matrix_col('FA') lFA = npFA.tolist() lPI = wf_interpolated_data.get_matrix_col('PI').astype(numpy.int32).tolist() lSC = wf_interpolated_data.get_matrix_col('SC').astype(numpy.int32).tolist() # Number of 30 seconds step. npFT = wf_interpolated_data.get_matrix_col('Time') nNbrTimeSteps = self.__get_nb_timesteps(forecast) lAH = wf_interpolated_data.get_matrix_col('AH').tolist() # Observation data ############################################### ro_interpolated_data = observation.get_interpolated_data() lAT_obs = ro_interpolated_data.get_matrix_col('AT').tolist() lST_obs = ro_interpolated_data.get_matrix_col('ST').tolist() lSST_obs = ro_interpolated_data.get_matrix_col('SST').tolist() lTime_obs = ro_interpolated_data.get_matrix_col('Time').tolist() # Deep soil value given in command line bDeepTemp = metro_config.get_value('DEEP_SOIL_TEMP') dDeepTemp = float(metro_config.get_value('DEEP_SOIL_TEMP_VALUE')) nLenObservation = self.__get_observation_lenght(observation) fDeltaTMetroObservation = self.__get_observation_delta_t(observation) # Concatenate the information to send it to C. npSWO1 = observation.get_attribute('SST_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO2 = observation.get_attribute('AT_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO3 = observation.get_attribute('TD_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO4 = observation.get_attribute('WS_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO = numpy.zeros(4*metro_constant.nNL) # Put all the arrays in one for the fortran code. for i in range(0,len(npSWO1)): npSWO[4*i] = npSWO1[i] npSWO[4*i+1] = npSWO2[i] npSWO[4*i+2] = npSWO3[i] npSWO[4*i+3] = npSWO4[i] lSWO = npSWO.astype(numpy.int32).tolist() bNoObs = observation.get_attribute('NO_OBS') sMessage = _( "------------station config START---------------------") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sShort_time_zone = metro_date.get_short_time_zone\ (fIssue_time,sTimeZone) sMessage = _( "Short time zone = ") + sShort_time_zone metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) tLatlon = dStation_header['COORDINATE'] fLat = tLatlon[0] fLon = tLatlon[1] sMessage = _( "lat,lon: ")+ "(" + str(fLat) + ", " + str(fLon) + ")" metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) bFlat = cs_data.get_station_type() dSstDepth = cs_data.get_sst_depth() sMessage = _( "SST sensor depth: ") + str(dSstDepth) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = _("------------station config END---------------------") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) bSilent = not metro_config.get_value('INIT_LOGGER_SHELL_DISPLAY') metro_logger.print_message(metro_logger.LOGGER_MSG_EXECPRIMARY, _("Start sending data to METRo core")) bEchec = [] macadam.Do_Metro(bFlat, fLat, fLon, lLayerThick, \ nNbrOfLayer, lLayerType, lAT, lQP, \ lWS, lAP, lSF, lIR, \ lFA, lPI, lSC, lAT_obs, \ lST_obs, lSST_obs, lAH, lTime_obs, \ lSWO, bNoObs, fDeltaTMetroObservation, \ nLenObservation, nNbrTimeSteps, bSilent, \ dSstDepth, bDeepTemp, dDeepTemp) bEchec = (macadam.get_echec())[0] # Check if the execution of the model was a succes: if bEchec != 0: sError_message = _("Fatal error in METRo physical model.") metro_logger.print_message(metro_logger.LOGGER_MSG_STOP, sError_message) else: metro_logger.print_message(metro_logger.LOGGER_MSG_EXECPRIMARY, _("End of METRo core"))
def __check_time_order(self, ro_controlled_data, wf_controlled_data): """ Name: __check_time_order Parameters: metro_data controlled_data : controlled observation data Returns: None Functions Called: metro_data.get_matrix_col metro_util.get_difference_array numpy.where, nonzero, arange metro_date.get_day, get_hour, get_minute metro_data.del_matrix_row metro_logger.print_message Description: Check if the time of the observation are in order. Cut the information that are spaced by more than 240 minutes. Notes: Revision History: Author Date Reason Miguel Tremblay August 4th 2004 """ npTime = ro_controlled_data.get_matrix_col('Time') npCheck = metro_util.get_difference_array(npTime) # If a gap of more than nGapMinuteObservation # minutes is identify, cut the value before. npCheck = metro_util.get_difference_array(npTime) npBad = numpy.where( npCheck > metro_constant.\ nGapMinuteObservation*60, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] if len(npBadIndice) > 0: sMessage = _("More than %d minutes between 2 measures")\ % (metro_constant.nGapMinuteObservation) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE,\ sMessage) npOT = ro_controlled_data.get_matrix_col('OBSERVATION_TIME') for i in range(0,len(npBadIndice)): nIndice = npBadIndice[i] sMessage = _("Indice: %d") % (nIndice) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) sMessage = _("Cutoff time: day:%d hour:%d minute:%d") %\ (metro_date.get_day(npOT[nIndice]),\ metro_date.get_hour(npOT[nIndice]),\ metro_date.get_minute(npOT[nIndice])) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) toto = numpy.arange(0,npBadIndice[len(npBadIndice)-1]+1) ro_controlled_data.del_matrix_row(toto) npTime = ro_controlled_data.get_matrix_col('Time') npBad = numpy.where( npCheck < 0, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] # Accept 1 value under zero because the last value of # npBadIndice = npCheck[len(npCheck)-1] - npCheck[0] if len(npBadIndice) > 1: sMessage = _("Time of observation are not in order. ") + \ _("Check the %d th value") %(npBadIndice[1]) metro_logger.print_message(metro_logger.LOGGER_MSG_STOP,\ sMessage ) # Remove the values that are equal. npBad = numpy.where( npCheck == 0, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] ro_controlled_data.del_matrix_row(npBadIndice) ######################################################## npFT = wf_controlled_data.get_matrix_col('FORECAST_TIME') npOT = ro_controlled_data.get_matrix_col('Time') nHourStart = metro_date.get_hour(npFT[0]) npDiff = - npOT + nHourStart*3600 npBad = numpy.where(npDiff > metro_constant.\ nHourForExpirationOfObservation*3600, 1, 0) if len(npBad) > 0: npBadIndices = (numpy.nonzero(npBad))[0] if len(npBadIndices) > 0: npBadIndices = (numpy.nonzero(npBad))[0] sMessage = _("Observation is more than %d hours") \ % ( metro_constant.nHourForExpirationOfObservation)\ + _("before the first roadcast") metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) for i in range(0,len(npBadIndices)): nIndice = npBadIndices[i] sMessage = _("Indice: %d") % (nIndice) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) ro_controlled_data.del_matrix_row(npBadIndices) # Get start time sStart_time = metro_config.get_value('INIT_ROADCAST_START_DATE') # If start time is not specified, default will be used if sStart_time == "": return # Check if the observation are not before the start of the roadcast # if specified. fStart_time = metro_date.parse_date_string(sStart_time) npOT = ro_controlled_data.get_matrix_col('Time')\ +nHourStart*3600 npDiff = - npOT + int(metro_date.get_hour(fStart_time))*3600 npBad = numpy.where(npDiff > metro_constant\ .nHourForExpirationOfObservation*3600, 1, 0) if len(npBad) > 0: npBadIndices = (numpy.nonzero(npBad))[0] if len(npBadIndices) > 0: sMessage = _("Observation after the first roadcast time of METRo") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) sMessage = _("Threshold: %d") \ % (int(metro_date.get_hour(fStart_time))*3600 \ + int(metro_date.get_month(fStart_time))*60) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) for i in range(0,len(npBadIndices)): nIndice = npBadIndices[i] sMessage = _("Time difference: %f") \ % (npDiff[nIndice]) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) ro_controlled_data.del_matrix_row(npBadIndices)
def __check_time_order(self, ro_controlled_data, wf_controlled_data): """ Name: __check_time_order Parameters: metro_data controlled_data : controlled observation data Returns: None Functions Called: metro_data.get_matrix_col metro_util.get_difference_array numpy.where, nonzero, arange metro_date.get_day, get_hour, get_minute metro_data.del_matrix_row metro_logger.print_message Description: Check if the time of the observation are in order. Cut the information that are spaced by more than 240 minutes. Notes: Revision History: Author Date Reason Miguel Tremblay August 4th 2004 """ npTime = ro_controlled_data.get_matrix_col('Time') npCheck = metro_util.get_difference_array(npTime) # If a gap of more than nGapMinuteObservation # minutes is identify, cut the value before. npCheck = metro_util.get_difference_array(npTime) npBad = numpy.where( npCheck > metro_constant.\ nGapMinuteObservation*60, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] if len(npBadIndice) > 0: sMessage = _("More than %d minutes between 2 measures")\ % (metro_constant.nGapMinuteObservation) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE,\ sMessage) npOT = ro_controlled_data.get_matrix_col('OBSERVATION_TIME') for i in range(0, len(npBadIndice)): nIndice = npBadIndice[i] sMessage = _("Indice: %d") % (nIndice) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) sMessage = _("Cutoff time: day:%d hour:%d minute:%d") %\ (metro_date.get_day(npOT[nIndice]),\ metro_date.get_hour(npOT[nIndice]),\ metro_date.get_minute(npOT[nIndice])) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) toto = numpy.arange(0, npBadIndice[len(npBadIndice) - 1] + 1) ro_controlled_data.del_matrix_row(toto) npTime = ro_controlled_data.get_matrix_col('Time') npBad = numpy.where(npCheck < 0, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] # Accept 1 value under zero because the last value of # npBadIndice = npCheck[len(npCheck)-1] - npCheck[0] if len(npBadIndice) > 1: sMessage = _("Time of observation are not in order. ") + \ _("Check the %d th value") %(npBadIndice[1]) metro_logger.print_message(metro_logger.LOGGER_MSG_STOP,\ sMessage ) # Remove the values that are equal. npBad = numpy.where(npCheck == 0, 1, 0) npBadIndice = (numpy.nonzero(npBad))[0] ro_controlled_data.del_matrix_row(npBadIndice) ######################################################## npFT = wf_controlled_data.get_matrix_col('FORECAST_TIME') npOT = ro_controlled_data.get_matrix_col('Time') nHourStart = metro_date.get_hour(npFT[0]) npDiff = -npOT + nHourStart * 3600 npBad = numpy.where(npDiff > metro_constant.\ nHourForExpirationOfObservation*3600, 1, 0) if len(npBad) > 0: npBadIndices = (numpy.nonzero(npBad))[0] if len(npBadIndices) > 0: npBadIndices = (numpy.nonzero(npBad))[0] sMessage = _("Observation is more than %d hours") \ % ( metro_constant.nHourForExpirationOfObservation)\ + _("before the first roadcast") metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) for i in range(0, len(npBadIndices)): nIndice = npBadIndices[i] sMessage = _("Indice: %d") % (nIndice) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) ro_controlled_data.del_matrix_row(npBadIndices) # Get start time sStart_time = metro_config.get_value('INIT_ROADCAST_START_DATE') # If start time is not specified, default will be used if sStart_time == "": return # Check if the observation are not before the start of the roadcast # if specified. fStart_time = metro_date.parse_date_string(sStart_time) npOT = ro_controlled_data.get_matrix_col('Time')\ +nHourStart*3600 npDiff = -npOT + int(metro_date.get_hour(fStart_time)) * 3600 npBad = numpy.where(npDiff > metro_constant\ .nHourForExpirationOfObservation*3600, 1, 0) if len(npBad) > 0: npBadIndices = (numpy.nonzero(npBad))[0] if len(npBadIndices) > 0: sMessage = _( "Observation after the first roadcast time of METRo") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) sMessage = _("Threshold: %d") \ % (int(metro_date.get_hour(fStart_time))*3600 \ + int(metro_date.get_month(fStart_time))*60) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) for i in range(0, len(npBadIndices)): nIndice = npBadIndices[i] sMessage = _("Time difference: %f") \ % (npDiff[nIndice]) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG,\ sMessage) ro_controlled_data.del_matrix_row(npBadIndices)
def __subsample_roadcast(self, roadcast, nFirstRoundTimestep): # Get all roadcast items lStandard_roadcast = metro_config.get_value( \ 'XML_ROADCAST_PREDICTION_STANDARD_ITEMS') lExtended_roadcast = metro_config.get_value( \ 'XML_ROADCAST_PREDICTION_EXTENDED_ITEMS') lRoadcast_items = lStandard_roadcast + lExtended_roadcast dElement_Array = {} rc_controlled = roadcast.get_controlled_data() # Put array in a dictionnary for dRoadcast_item in lRoadcast_items: sElement = dRoadcast_item['NAME'] dElement_Array[sElement] = rc_controlled.get_matrix_col(sElement) # Get some values necessary for the creation of a complete roadcast iObservation_len = roadcast.get_attribute('OBSERVATION_LENGTH') fObservation_delta_t = roadcast.get_attribute('OBSERVATION_DELTAT_T') iNb_timesteps = roadcast.get_attribute('FORECAST_NB_TIMESTEPS') fEndCoupling = (iObservation_len*\ metro_constant.fTimeStep/3600.0-fObservation_delta_t) sStartDate = metro_config.get_value('INIT_ROADCAST_START_DATE') fStartDate = metro_date.parse_date_string(sStartDate) sMessage = _("Output file roadcast start date: '%s'") % (sStartDate) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) # # Create the data matrix for roadcast # rc_subsampled = roadcast.get_subsampled_data() nSecondsForOutput = metro_constant.nMinutesForOutput*60 for i in range(nFirstRoundTimestep, iNb_timesteps): fCurrentTime = dElement_Array['HH'][i]*3600 # Forecast at every 20 minutes, i.e. 1200 seconds lRCvect = [0]*rc_subsampled.get_real_nb_matrix_col() lMatrix_line = [None]*rc_subsampled.get_real_nb_matrix_col() # if current time is a 20 minute interval # and roadcast time is >= roadcast start date if round(fCurrentTime)%nSecondsForOutput == 0 and \ dElement_Array['ROADCAST_TIME'][i] >= fStartDate : for sElement in dElement_Array.keys(): lIndexList = rc_subsampled.index_of_matrix_col(sElement) if not rc_subsampled.is_multi_col(sElement) : # single col lMatrix_line[rc_subsampled.index_of_matrix_col(sElement)[0]] = \ dElement_Array[sElement][i] else: # multicol # FFTODO optimisation could probably be done here iCol = 0 for j in lIndexList: lMatrix_line[j] = \ dElement_Array[sElement][iCol][i] iCol +=1 # Ugly stuff. Don't know. No clue about the 600. for j in range(int(max(1,i-600/metro_constant.fTimeStep)),\ int(min(iNb_timesteps,i+600/\ metro_constant.fTimeStep))): if dElement_Array['RC'][j] == 0: continue iRC = int(round(dElement_Array['RC'][j])) lRCvect[iRC-1] = lRCvect[iRC-1] +1 # Identify the highest value for the road condition nTop = max(lRCvect) nRCcode = lRCvect.index(nTop)+1 lMatrix_line[rc_subsampled.index_of_matrix_col('RC')[0]] = nRCcode # Adding the new line in the matrix rc_subsampled.append_matrix_row(lMatrix_line) fActual_start_date = dElement_Array['ROADCAST_TIME'][0] sMessage = _("Roadcast start date of the model is: '%s'") \ % (metro_date.seconds2iso8601(fActual_start_date)) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) roadcast.set_subsampled_data(rc_subsampled)
def __set_first_roadcast_date(self, observation_data, roadcast_data): """ Name: ___set_first_roadcast_date Parameters: Observation and roadcast Description: If the date of the first roadcast is not specified in the input (command line or config file), set it to first round 20 minutes after the last observation. Author: Miguel Tremblay Date: March 5th 2007 """ # Check if the value is given as an argument if metro_config.get_value('INIT_ROADCAST_START_DATE') != "": # Set the xml header value controlled_roadcast = roadcast_data.get_subsampled_data() controlled_roadcast.set_header_value('FIRST_ROADCAST',\ metro_config.get_value('INIT_ROADCAST_START_DATE')) return 0 else: # if value of INIT_ROADCAST_START_DATE is not given, fetch the first # round 20 minutes. naOT = observation_data.get_matrix_col('OBSERVATION_TIME') fLast_observation_date = naOT[len(naOT) - 1] sLast_observation_date = metro_date.seconds2iso8601( fLast_observation_date) # Fetch the first 20 minutes value. iNb_timesteps = roadcast_data.get_attribute( 'FORECAST_NB_TIMESTEPS') nSecondsForOutput = metro_constant.nMinutesForOutput * 60 lTimeStep = roadcast_data.get_controlled_data().get_matrix_col( 'HH') for i in range(0, iNb_timesteps): fCurrentTime = lTimeStep[i] * 3600 # Forecast at every 20 minutes, i.e. 1200 seconds # if current time is a 20 minute interval # and roadcast time is >= roadcast start date if round(fCurrentTime) % nSecondsForOutput == 0: sLast_observation = metro_config.\ get_value('DATA_ATTRIBUTE_LAST_OBSERVATION') fLast_observation = metro_date.parse_date_string( sLast_observation) sStart_date = metro_date.seconds2iso8601(fLast_observation + \ (i+1)*\ metro_constant.fTimeStep) metro_config.set_value('INIT_ROADCAST_START_DATE', sStart_date) sMessage = _("Roadcast start date set to: ''%s'") % ( sStart_date) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE,\ sMessage) # Set the xml header value controlled_roadcast = roadcast_data.get_subsampled_data() controlled_roadcast.set_header_value( 'FIRST_ROADCAST', sStart_date) return i # Something bad hapened. sMessage = _("Unable to determine the first time of roadcast!") metro_logger.print_message(metro_logger.LOGGER_MSG_STOP,\ sMessage)
def __subsample_roadcast(self, roadcast, nFirstRoundTimestep): # Get all roadcast items lStandard_roadcast = metro_config.get_value( \ 'XML_ROADCAST_PREDICTION_STANDARD_ITEMS') lExtended_roadcast = metro_config.get_value( \ 'XML_ROADCAST_PREDICTION_EXTENDED_ITEMS') lRoadcast_items = lStandard_roadcast + lExtended_roadcast dElement_Array = {} rc_controlled = roadcast.get_controlled_data() # Put array in a dictionnary for dRoadcast_item in lRoadcast_items: sElement = dRoadcast_item['NAME'] dElement_Array[sElement] = rc_controlled.get_matrix_col(sElement) # Get some values necessary for the creation of a complete roadcast iObservation_len = roadcast.get_attribute('OBSERVATION_LENGTH') fObservation_delta_t = roadcast.get_attribute('OBSERVATION_DELTAT_T') iNb_timesteps = roadcast.get_attribute('FORECAST_NB_TIMESTEPS') fEndCoupling = (iObservation_len*\ metro_constant.fTimeStep/3600.0-fObservation_delta_t) sStartDate = metro_config.get_value('INIT_ROADCAST_START_DATE') fStartDate = metro_date.parse_date_string(sStartDate) sMessage = _("Output file roadcast start date: '%s'") % (sStartDate) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) # # Create the data matrix for roadcast # rc_subsampled = roadcast.get_subsampled_data() nSecondsForOutput = metro_constant.nMinutesForOutput * 60 for i in range(nFirstRoundTimestep, iNb_timesteps): fCurrentTime = dElement_Array['HH'][i] * 3600 # Forecast at every 20 minutes, i.e. 1200 seconds lRCvect = [0] * rc_subsampled.get_real_nb_matrix_col() lMatrix_line = [None] * rc_subsampled.get_real_nb_matrix_col() # if current time is a 20 minute interval # and roadcast time is >= roadcast start date if round(fCurrentTime)%nSecondsForOutput == 0 and \ dElement_Array['ROADCAST_TIME'][i] >= fStartDate : for sElement in dElement_Array.keys(): lIndexList = rc_subsampled.index_of_matrix_col(sElement) if not rc_subsampled.is_multi_col(sElement): # single col lMatrix_line[rc_subsampled.index_of_matrix_col(sElement)[0]] = \ dElement_Array[sElement][i] else: # multicol # FFTODO optimisation could probably be done here iCol = 0 for j in lIndexList: lMatrix_line[j] = \ dElement_Array[sElement][iCol][i] iCol += 1 # Ugly stuff. Don't know. No clue about the 600. for j in range(int(max(1,i-600/metro_constant.fTimeStep)),\ int(min(iNb_timesteps,i+600/\ metro_constant.fTimeStep))): if dElement_Array['RC'][j] == 0: continue iRC = int(round(dElement_Array['RC'][j])) lRCvect[iRC - 1] = lRCvect[iRC - 1] + 1 # Identify the highest value for the road condition nTop = max(lRCvect) nRCcode = lRCvect.index(nTop) + 1 lMatrix_line[rc_subsampled.index_of_matrix_col('RC') [0]] = nRCcode # Adding the new line in the matrix rc_subsampled.append_matrix_row(lMatrix_line) fActual_start_date = dElement_Array['ROADCAST_TIME'][0] sMessage = _("Roadcast start date of the model is: '%s'") \ % (metro_date.seconds2iso8601(fActual_start_date)) metro_logger.print_message(metro_logger.LOGGER_MSG_INFORMATIVE, sMessage) roadcast.set_subsampled_data(rc_subsampled)
def __send_data_to_metro_core( self, forecast, observation, station ): wf_data = forecast.get_interpolated_data() ro_data = observation.get_interpolated_data() cs_data = station # Start time from model is the last observation sStart_time = metro_config.get_value('DATA_ATTRIBUTE_LAST_OBSERVATION') fStart_time = metro_date.parse_date_string(sStart_time) iModel_start_y = metro_date.get_year(fStart_time) sMessage = _("year: [%s]") % (iModel_start_y) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_m = metro_date.get_month(fStart_time) sMessage = _("month: [%s]") % (iModel_start_m) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_d = metro_date.get_day(fStart_time) sMessage = _("day: [%s]") % (iModel_start_d) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) iModel_start_h = metro_date.get_hour(fStart_time) sMessage = _("hour: [%s]") % (iModel_start_h) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) dStation_header = cs_data.get_header() # test observation dObservation_header = ro_data.get_header() lObservation_data = ro_data.get_matrix() sMessage = "Observation_header=" + str(dObservation_header) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = "Observation_data=" + str(lObservation_data) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # test forecast dForecast_header = wf_data.get_header() lForecast_data = wf_data.get_matrix() sMessage = "Forecast_header=" + str(dForecast_header) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = "Forecast_data=" + str(lForecast_data) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # start roadlayer MATRIX npLayerType = cs_data.get_matrix_col('TYPE') lLayerType = npLayerType.astype(numpy.int32).tolist() lLayerThick = cs_data.get_matrix_col('THICKNESS').tolist() nNbrOfLayer = len(lLayerType) sMessage = _("Number of layer=") + str(nNbrOfLayer) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # Append an empty box for the manuel mode lLayerType.append(0) lLayerThick.append(0.0) sMessage = _("roadlayer type=") + str(lLayerType) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = _("roadlayer thick=") + str(lLayerThick) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) # end roadlayer MATRIX fTime = dStation_header['PRODUCTION_DATE'] fTimeForecast = dForecast_header['PRODUCTION_DATE'] sTimeZone = dStation_header['TIME_ZONE'] sMessage = _("timezone=") + sTimeZone metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) fIssue_time = fStart_time sMessage = _("issue time=") + time.ctime(fIssue_time) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) wforiginal = forecast.get_original_data() ft = wforiginal.get_matrix_col('FORECAST_TIME') ################################################################## # Forecast # Get the interpolated values. wf_interpolated_data = forecast.get_interpolated_data() npAT = wf_interpolated_data.get_matrix_col('AT') lAT = npAT.tolist() lQP = wf_interpolated_data.get_matrix_col('QP').tolist() npWS = wf_interpolated_data.get_matrix_col('WS') lWS = npWS.tolist() npTD = wf_interpolated_data.get_matrix_col('TD') lTD = npTD.tolist() lAP = wf_interpolated_data.get_matrix_col('AP').tolist() lSF = wf_interpolated_data.get_matrix_col('SF').tolist() lIR = wf_interpolated_data.get_matrix_col('IR').tolist() npFA = wf_interpolated_data.get_matrix_col('FA') lFA = npFA.tolist() lPI = wf_interpolated_data.get_matrix_col('PI').tolist() lSC = wf_interpolated_data.get_matrix_col('SC').tolist() # Number of 30 seconds step. npFT = wf_interpolated_data.get_matrix_col('Time') nNbrTimeSteps = self.__get_nb_timesteps(forecast) lAH = wf_interpolated_data.get_matrix_col('AH').tolist() # Observation data ############################################### ro_interpolated_data = observation.get_interpolated_data() lAT_obs = ro_interpolated_data.get_matrix_col('AT').tolist() lST_obs = ro_interpolated_data.get_matrix_col('ST').tolist() lSST_obs = ro_interpolated_data.get_matrix_col('SST').tolist() lTime_obs = ro_interpolated_data.get_matrix_col('Time').tolist() # Deep soil value given in command line bDeepTemp = metro_config.get_value('DEEP_SOIL_TEMP') dDeepTemp = float(metro_config.get_value('DEEP_SOIL_TEMP_VALUE')) nLenObservation = self.__get_observation_lenght(observation) fDeltaTMetroObservation = self.__get_observation_delta_t(observation) # Concatenate the information to send it to C. npSWO1 = observation.get_attribute('SST_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO2 = observation.get_attribute('AT_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO3 = observation.get_attribute('TD_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO4 = observation.get_attribute('WS_VALID_INTERPOLATED')\ .astype(numpy.int32) npSWO = numpy.zeros(4*metro_constant.nNL) # Put all the arrays in one for the fortran code. for i in range(0,len(npSWO1)): npSWO[4*i] = npSWO1[i] npSWO[4*i+1] = npSWO2[i] npSWO[4*i+2] = npSWO3[i] npSWO[4*i+3] = npSWO4[i] lSWO = npSWO.astype(numpy.int32).tolist() bNoObs = observation.get_attribute('NO_OBS') sMessage = _( "------------station config START---------------------") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sShort_time_zone = metro_date.get_short_time_zone\ (fIssue_time,sTimeZone) sMessage = _( "Short time zone = ") + sShort_time_zone metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) tLatlon = dStation_header['COORDINATE'] fLat = tLatlon[0] fLon = tLatlon[1] sMessage = _( "lat,lon: ")+ "(" + str(fLat) + ", " + str(fLon) + ")" metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) bFlat = cs_data.get_station_type() dSstDepth = cs_data.get_sst_depth() sMessage = _( "SST sensor depth: ") + str(dSstDepth) metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) sMessage = _("------------station config END---------------------") metro_logger.print_message(metro_logger.LOGGER_MSG_DEBUG, sMessage) bSilent = not metro_config.get_value('INIT_LOGGER_SHELL_DISPLAY') metro_logger.print_message(metro_logger.LOGGER_MSG_EXECPRIMARY, _("Start sending data to METRo core")) bEchec = [] macadam.Do_Metro(bFlat,\ fLat, fLon, \ lLayerThick, nNbrOfLayer, lLayerType, \ lAT, lQP, lWS, lAP, lSF, lIR, lFA, lPI, lSC,\ lAT_obs,lST_obs, lSST_obs, \ lAH, lTime_obs, lSWO, bNoObs,\ fDeltaTMetroObservation, nLenObservation, \ nNbrTimeSteps, bSilent, \ dSstDepth, bDeepTemp, dDeepTemp) bEchec = (macadam.get_echec())[0] # Check if the execution of the model was a succes: if bEchec != 0: sError_message = _("Fatal error in METRo physical model.") metro_logger.print_message(metro_logger.LOGGER_MSG_STOP, sError_message) else: metro_logger.print_message(metro_logger.LOGGER_MSG_EXECPRIMARY, _("End of METRo core"))