U g&@sxddlZddlZddlTddlmZmZddlZddlZddlm Z m Z m Z ddl m Z GdddZ Gdd d ZdS) N)*)datetime timedelta)ABCMetaabstractmethodabstractproperty) TimeSlicec@seZdZdZeZeddZejddZeddZ e jddZ edd Z e jd d Z ed d Z e jd d Z eddZ e jddZ eddZ e jddZ eddZefddZdS) Observationz9 Abstract real-time flow time series. cCs|jSNZ_sourceselfrO/lfs/h1/ops/prod/packages/nwm.v3.0.13/ush/usgs_download/analysis/Observation.pysource7szObservation.sourcecCs ||_dSr r r srrrr;scCs|jSr Z _stationIDr rrr stationID?szObservation.stationIDcCs ||_dSr rrrrrrCscCs|jSr Z _stationNamer rrr stationNameGszObservation.stationNamecCs ||_dSr rrrrrrKscCs|jSr  _obvPeriodr rrr obvPeriodOszObservation.obvPeriodcCs ||_dSr r)r prrrrSscCs|jSr Z_unitr rrrunitWszObservation.unitcCs ||_dSr r)r urrrr[scCs|jSr _timeValueQualityr rrrtimeValueQuality_szObservation.timeValueQualitycCs ||_dSr r)r tvqrrrr cscCsdSr r)r filenamerrr__init__gszObservation.__init__cCsg}g}||jkr"||j|fS|jD]4}t|||dkr(|||t||q(|sfdSddtt||Dd}||j|fSdS)aG Get the closest time-value pair for a given time Input: at_time - the given time resolution - the tolerance time period around the given time Return: Tuple of a time-value pair NcSsg|] \}}|qSrr).0yxrrr sz.Observation.getTimeValueAt..r)r getabsappendsortedzip)r Zat_time resolutionZ closestTimesZ distanceskZclosestrrrgetTimeValueAtls     zObservation.getTimeValueAtN)__name__ __module__ __qualname____doc__rZ __metaclass__propertyrsetterrrrrr rr#rr0rrrrr s:             r c@sBeZdZdZddZddZddZdd Zd d Zdd dZ dS)All_ObservationszStore all obvserved datacCstt|_||_|js tdd|_|jdj|_|jD]X}|jd|jdkrh|jd|jdf|_|jd|jdkr:|jd|jdf|_q:dS)z Initialize the All_Objections object for a given Observation Input: list of Observation object zFATAL ERROR: has no datarN) logging getLoggerr1logger observations RuntimeErrorindexr timePeriod)r Zobvsobvrrrr#s  zAll_Observations.__init__cCs|S)z) The iterator rr rrr__iter__szAll_Observations.__iter__cCs6|jt|jdkrd|_t|jd|_|j|jS)z7 The next Observation object r9r8)r?lenr= StopIterationr rrr__next__s  zAll_Observations.__next__cCs|jS)z Get the earlist and latest time for all observations Return: Tuple of start time and end time )r@r rrrtimePeriodForAllsz!All_Observations.timePeriodForAllcCsVg}|D]<}|||}|r||j|d|dd|ddfqt|||}|S)z Create one time slice for a given time and resolution Input: timestamp - the given time timeresolution - the resolution Return: A time slice object rr9)r0r+rr)r timestamptimeresolutionZstation_time_value_listrAZ closestObvZ timeSlicerrr makeTimeSlices    zAll_Observations.makeTimeSliceusgsTimeSlice.ncdfc Cs\d|jdkrtdt|jdj|jdj|jdj|jdj}||jdkr\||7}qD||jdkrrtdd}||jdkrX|j d| | ||}|j d|d| || sNd }|d| |}tj|rt|} || }|r0||||j | |d n|j | |d |d}||7}qv|S) a Create the time slice NetCDF files for all USGS observations Input: timeresolution - resolution outdir - the output directory Return: Total number of time slice files created irzLFATAL Error: Time slice resolution must divide 60 minutes with no remainder.r9z,FATAL Error: observation time period wrong! zmaking time slice for z Time slice: /Tz updated!z not updated!)secondsr>rr@yearmonthdayhourr<info isoformatrIZgetSliceNCFileNameisEmptyospathisfilerZ fromNetCDFZmergeOldZtoNetCDF) r rHoutdirsuffixZ startTimecountZoneSliceZ updatedOrNewZ slicefilenameZoldslicerrrmakeAllTimeSlicessV            z"All_Observations.makeAllTimeSlicesN)rJ) r1r2r3r4r#rBrErFrIrZrrrrr7s r7)rTr:stringrrdateutil.parserdateutilpytzabcrrrrr r7rrrrs p