@@ -482,10 +482,8 @@ def hapi(*args, **kwargs):
482482 opts ['dt_chunk' ] = None
483483
484484 backend = 'sequential'
485-
486485 if opts ['parallel' ]:
487486 backend = 'threading'
488-
489487 # multiprocessing was not tested. It may work, but will
490488 # need a speed comparison with threading.
491489 # backend = 'multiprocessing'
@@ -529,13 +527,21 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
529527
530528 resD = list (resD )
531529
530+ import sys
532531 from hapiclient .hapi import hapitime_reformat
533532
534- START = hapitime_reformat (resD [0 ]['Time' ][0 ].decode ('UTF-8' ), START )
535- resD [0 ] = resD [0 ][resD [0 ]['Time' ] >= bytes (START , 'UTF-8' )]
533+ if sys .version_info < (3 , ):
534+ START = hapitime_reformat (str (resD [0 ]['Time' ][0 ]), START )
535+ resD [0 ] = resD [0 ][resD [0 ]['Time' ] >= START ]
536+
537+ STOP = hapitime_reformat (str (resD [- 1 ]['Time' ][0 ]), STOP )
538+ resD [- 1 ] = resD [- 1 ][resD [- 1 ]['Time' ] < STOP ]
539+ else :
540+ START = hapitime_reformat (resD [0 ]['Time' ][0 ].decode ('UTF-8' ), START )
541+ resD [0 ] = resD [0 ][resD [0 ]['Time' ] >= bytes (START , 'UTF-8' )]
536542
537- STOP = hapitime_reformat (resD [- 1 ]['Time' ][0 ].decode ('UTF-8' ), STOP )
538- resD [- 1 ] = resD [- 1 ][resD [- 1 ]['Time' ] < bytes (STOP , 'UTF-8' )]
543+ STOP = hapitime_reformat (resD [- 1 ]['Time' ][0 ].decode ('UTF-8' ), STOP )
544+ resD [- 1 ] = resD [- 1 ][resD [- 1 ]['Time' ] < bytes (STOP , 'UTF-8' )]
539545
540546 data = np .concatenate (resD )
541547
@@ -716,7 +722,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
716722 log ('Reading %s' % fnamebin .replace (urld + '/' , '' ), opts )
717723 tic = time .time ()
718724 data = np .fromfile (fnamebin , dtype = dt )
719- toc = time .time () - tic
720725 else :
721726 from io import BytesIO
722727 log ('Creating buffer: %s' % urlbin , opts )
@@ -726,7 +731,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
726731 log ('Parsing buffer.' , opts )
727732 tic = time .time ()
728733 data = np .frombuffer (buff .read (), dtype = dt )
729- toc = time .time () - tic
730734 else :
731735 # HAPI CSV
732736 if opts ["cache" ]:
@@ -748,7 +752,6 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
748752 tic = time .time ()
749753 if opts ['method' ] == 'numpy' :
750754 data = np .genfromtxt (fnamecsv , dtype = dt , delimiter = ',' )
751- toc = time .time () - tic
752755 if opts ['method' ] == 'pandas' :
753756 # Read file into Pandas DataFrame
754757 df = pandas .read_csv (fnamecsv , sep = ',' , header = None )
@@ -757,17 +760,13 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
757760 # as computed to pandas.read_csv; pandas dtype is different
758761 # from numpy's dtype.)
759762 data = np .ndarray (shape = (len (df )), dtype = dt )
760- print (df )
761763 # Insert data from dataframe 'df' columns into N-D array 'data'
762764 for i in range (0 , len (pnames )):
763765 shape = np .append (len (data ), psizes [i ])
764766 # In numpy 1.8.2 and Python 2.7, this throws an error
765767 # for no apparent reason. Works as expected in numpy 1.10.4
766- print (cols )
767768 data [pnames [i ]] = np .squeeze (
768769 np .reshape (df .values [:, np .arange (cols [i ][0 ], cols [i ][1 ] + 1 )], shape ))
769-
770- toc = time .time () - tic
771770 else :
772771 # At least one requested string or isotime parameter does not
773772 # have a length in metadata. More work to do to read.
@@ -866,7 +865,7 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
866865 # Save memory by not copying (does this help?)
867866 # data2[pnames[i]] = np.array(data[pnames[i]],copy=False)
868867
869- toc = time .time () - tic
868+ toc = time .time () - tic
870869
871870 # Extra metadata associated with request will be saved in
872871 # a pkl file with same base name as npy data file.
@@ -919,10 +918,12 @@ def nhapi(SERVER, DATASET, PARAMETERS, pSTART, pDELTA, i, **opts):
919918 else :
920919 return data , meta
921920
922- def hapitime_reformat (form_to_match , given_form ):
921+
922+ def hapitime_reformat (form_to_match , given_form , logging = False ):
923923 """Reformat a given HAPI ISO 8601 time to match format of another HAPI ISO 8601 time."""
924924
925- print ('ref: {}\n given: {}' .format (form_to_match , given_form ))
925+ log ('ref: {}' .format (form_to_match ), {'logging' : logging })
926+ log ('given: {}' .format (given_form ), {'logging' : logging })
926927
927928 if 'T' in given_form :
928929 dt_given = isodate .parse_datetime (given_form )
@@ -962,10 +963,14 @@ def hapitime_reformat(form_to_match, given_form):
962963
963964 if len (converted ) > len (form_to_match ):
964965 converted = converted [0 :len (form_to_match )- 1 ] + "Z"
965- print ('converted: {}\n ref fmt: {}' .format (converted , format_ref ))
966- print ('----' )
966+
967+ log ('converted: {}' .format (converted ), {'logging' : logging })
968+ log ('ref fmt: {}' .format (format_ref ), {'logging' : logging })
969+ log ('----' , {'logging' : logging })
970+
967971 return converted
968972
973+
969974def hapitime_format_str (Time ):
970975 """Determine the time format string for a HAPI ISO 8601 time"""
971976
@@ -1093,9 +1098,7 @@ def hapitime2datetime(Time, **kwargs):
10931098 except :
10941099 tzinfo = datetime .timezone .utc
10951100
1096- opts = {'logging' : False }
1097-
1098- opts = setopts (opts , kwargs )
1101+ opts = kwargs .copy ()
10991102
11001103 if type (Time ) == list :
11011104 Time = np .asarray (Time )
@@ -1138,13 +1141,16 @@ def hapitime2datetime(Time, **kwargs):
11381141 # have trailing Z, in some cases, infer_datetime_format will not return
11391142 # a timezone-aware Timestamp.
11401143 # TODO: Use hapitime_format_str() and pass this as format=...
1141- Time = pandas .to_datetime (Time , infer_datetime_format = True ).tz_localize (tzinfo ).to_pydatetime ()
1144+ #import pdb;pdb.set_trace()
1145+ Timeo = Time [0 ]
1146+ Time = pandas .to_datetime (Time , infer_datetime_format = True ).tz_convert (tzinfo ).to_pydatetime ()
11421147 if reshape :
11431148 Time = np .reshape (Time , shape )
11441149 toc = time .time () - tic
1145- log ("Pandas processing time = %.4fs, Input = %s\n " % (toc , Time [ 0 ] ), opts )
1150+ log ("Pandas processing time = %.4fs, first time = %s" % (toc , Timeo ), opts )
11461151 return Time
11471152 except :
1153+ log ("Pandas processing failed, first time = %s" % Time [0 ], opts )
11481154 pass
11491155
11501156 # Convert from Python byte literals to unicode strings
@@ -1174,7 +1180,7 @@ def hapitime2datetime(Time, **kwargs):
11741180 error ("HAPI Times must have trailing Z. Time[" + str (i ) + "] = " + Time [i ] + " does not have trailing Z." )
11751181
11761182 toc = time .time () - tic
1177- log ("Manual processing time = %.4fs, Input = %s, fmt = %s\n " % (toc , Time [0 ], fmt ), opts )
1183+ log ("Manual processing time = %.4fs, Input = %s, fmt = %s" % (toc , Time [0 ], fmt ), opts )
11781184
11791185 if reshape :
11801186 pythonDateTime = np .reshape (pythonDateTime , shape )
0 commit comments