U gU@sjddlZddlZddlZddlZddlTddlmZmZddlZddlZddl Z ddl Z GdddZ dS)N)*)datetime timedeltac@seZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd Zd Zd Zd ZdZd ZdZdZdZdZdZeddZejddZeddZejddZddZddZddZ d d!Z!d"d#Z"d$d%Z#d&d'Z$d(d)Z%d9d+d,Z&d-d.Z'd:d0d1Z(e)d2d3Z*d4d5Z+d6d7Z,d8S); RFCTimeSeriesz Description: Store one RFC time series data Author: Zhengtao Cui (Zhengtao.Cui@noaa.gov) Date: July. 10, 2019 Date modified: z!RFC station identifer of length 5UTCzYYYY-MM-DD_HH:mm:ss UTCzm^3/saObsverved and forecasted discharges. There are 48 hours of observed values before issue time (T0), that is the start time is T0 - 48 hours. Values after T0 (including T0) are forecasts that genenrally extend to 10 days, that is T0 + 240 hours. The total length of dischargs is 12 days in general except in cases where forecasts or observations that are longer than 10 days or 2 days respectively are available.-zPWhether the discharge value is synthetic or orginal, 1 - synthetic, 0 - originalz/Discharge quality 0 to 100 to be scaled by 100.z2Total count of all observation and forecast valuesz Total observed values before T0.z/Total forecasted values including and after T0.secondsz0Frequency/temporal resolution of forecast values*seconds since 1970-01-01 00:00:00 local TZzQuery time at RFCcCs|jSNZ_issueTimeStampsselfrH/lfs/h1/ops/prod/packages/nwm.v3.0.13/ush/rfc_ingestion/RFCTimeSeries.pyissueTimeStamps@szRFCTimeSeries.issueTimeStampscCs ||_dSr r)rZitsrrrrCscCs|jSr Z_startTimeStamprrrrstartTimeStampGszRFCTimeSeries.startTimeStampcCs ||_dSr r)rstsrrrrJsc Cs@||_||_||_||_||_||_||_||_| |_| |_ dS)a0 Initialize a RFC TimeSeries object Input: time_stamp - a time stamp resolution - time resolution of the time series station_time_value - Tuple of (station, time, flow, quality) N) rseriesTimeResolution stationIDr dischargesdischarge_qualities synthetics timeSteps querytime missingValue) rZstaID time_stamp resolutionZ starttimedisqualityrZtimesteprmissValrrr__init__Ns zRFCTimeSeries.__init__cCs|j S)z\ Test if the time series is empty Return: boolean rrrrrisEmptybszRFCTimeSeries.isEmptycCstd|jt|jt|jt|jt|jt|j|jD]6\}}td|d|j|D]}td|qnqHdS)z? Print the time series data zSeries: start time: z issue time:  z N)printr isoformatrrrrzip)r issuetimer!vrrrprint_station_offsettime_valueis$      z,RFCTimeSeries.print_station_offsettime_valuecCs"g}|jD]}|t|q |Sr )rappendlen)rcountsr!rrrget_total_counts{s zRFCTimeSeries.get_total_countscCs8g}t|j|jD] \}}|||j|q|Sr )r*rrr.r total_seconds)rr0tsrrrget_observed_countss z!RFCTimeSeries.get_observed_countscCs8g}|}t||jD]\}}|t||q|Sr )r5r*rr.r/)rr0 obvcountsocr!rrrget_forecast_countss z!RFCTimeSeries.get_forecast_countscCs`t|j}t||jkr[sz,RFCTimeSeries.fromNetCDF..recSsg|] }t|qSrrrrrrr_srcSsg|] }t|qSrrrrrrrbsrcSsg|]}t|dqS)rg)rstrptime)rr3rrrrgsr^) rmrnrr getncattrrrJ chartostring variablestolistrrI)r ncfilenamerr time_resolr#Z newest_seriesr>rrreZ qualitiesrZ issueTimesrrr fromNetCDFBsN     zRFCTimeSeries.fromNetCDFc CsXdStddSg}tt|jD]P}tt|jD]<}|j||j|kr4|j||j|<|j ||j |<q"q4q"tt|jD]F}d}tt|jD] }|j||j|krd}qq|s| |q|rP|D]x}t|jdt|j|krnt|jdt|j|kr|jD]8}tt|jdt|j|D]}| |jq@q|j D]6} tt|j dt|j |D]}| dqq^nHtt|j|t|jdD](}|j| |j|j | dq|j |j||j |j ||j |j||j |j ||j |j ||j |j |qdSdS)z Merge data in an existing NetCDF time series file with this one Input: oldTimeSeries - the NetCDF filename of the existing time series FzeThe two time series have different start time, temporal resolutions, or station IDs, not merging ...TrCr9N)rrrrr(r<r/rrrr.rrr) rZ oldTimeSeriesZextraIssueTimesrmfoundr?djr4rrrmergeOldus             zRFCTimeSeries.mergeOldcCsX|j|jkrdS|j|jkr dS|j|jkr0dSt|jt|jkrHdSt|jt|jkr`dS|j|jkrpdS|j|jkrdS|j|jkrdS|j |j krdSt |j|jD]J\}}t|t|krdSt ||D] \}}t j ||ddsdSqqt |j|jD]J\}}t|t|kr(dSt ||D]\}}||kr2dSq2qdS)NFgMbP?)abs_tolT) rrrr/rrrrrrr*mathisclose)rZotherTimeSeriesryabrrrcomparesF       zRFCTimeSeries.compareN)rB)rQrB)-__name__ __module__ __qualname____doc__r;rrrVrtrsZqualifierStrLenrvrurxrwrrrzryr|r{r~r}rrZ queryTimeUnitZqueryTimeLong_namepropertyrsetterrr$r&r-r1r5r8r@rArOrPr classmethodrrrrrrrrs`       2Kr) ossystimerstringrrcalendarrmnumpyrrandomrrrrrs