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_sourceselfrN/lfs/h1/ops/prod/packages/nwm.v3.0.13/ush/ace_download/analysis/Observation.pysource8szObservation.sourcecCs ||_dSr r r srrrr<scCs|jSr Z _stationIDr rrr stationID@szObservation.stationIDcCs ||_dSr rrrrrrDscCs|jSr Z _stationNamer rrr stationNameHszObservation.stationNamecCs ||_dSr rrrrrrLscCs|jSr Z _obvPeriodr rrr obvPeriodPszObservation.obvPeriodcCs ||_dSr r)r prrrrTscCs|jSr Z_unitr rrrunitXszObservation.unitcCs ||_dSr r)r urrrr\scCs|jSr Z_timeValueQualityr rrrtimeValueQuality`szObservation.timeValueQualitycCs ||_dSr r)r ZtvqrrrrdscCsdSr r)r filenamerrr__init__hszObservation.__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)rgetabsappendsortedzip)r Zat_time resolutionZ closestTimes distanceskZclosestrrrgetTimeValueAtms     zObservation.getTimeValueAtN)__name__ __module__ __qualname____doc__r __metaclass__propertyrsetterrrrrrrr rr.rrrrr 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 getLoggerr/logger observations RuntimeErrorindexr timePeriod)r obvsobvrrrr 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 r8r7)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 rr8)r.r(rr)r timestamptimeresolutionZstation_time_value_listrAZ closestObvZ timeSlicerrr makeTimeSlices    zAll_Observations.makeTimeSliceusaceTimeSlice.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 USACE 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.r8z,FATAL ERROR: observation time period wrong! zmaking time slice for z Time slice: /Tz updated!z not updated!)secondsr=rr?yearmonthdayhourr;info isoformatrIgetSliceNCFileNameisEmptyospathisfiler fromNetCDFmergeOldtoNetCDF) r rHoutdirsuffixZ startTimecountZoneSliceZ updatedOrNewZ slicefilenameZoldslicerrrmakeAllTimeSlicessV            z"All_Observations.makeAllTimeSlicesN)rJ) r/r0r1r2r rBrErFrIr^rrrrr6s r6)rUr9stringrrZdateutil.parserZdateutilZpytzabcrrrrr r6rrrrs p