4

I am trying to extract data from a netcdf file using wrf-python. The data is for every hour. The date is being extracted as a number, and not a calendar-date-time. First I extract the data, convert it to a flat np array, then try to save the file. The format is saved as '%s'

np.savetxt((stn + 'WRF_T2_T10_WS_WD.csv'), np.transpose(arr2D), %s, delimiter=',', header=headers, comments='')

it looks like this: it looks like this

but it needs to look like this: but it needs to look like this

Thanks

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Like this? https://stackoverflow.com/questions/2623156/how-to-convert-the-integer-date-format-into-yyyymmdd – Bruck1701 Nov 07 '20 at 06:43

1 Answers1

5

By convention, dates are frequently stored as an offset in seconds from Jan 1, 1970

For the case of converting seconds, this answer Python Numpy Loadtxt - Convert unix timestamp suggests converting them by changing their datatype (should be as efficient as possible as it dodges by-row loops, copying data, etc.)

x = np.asarray(x, dtype='datetime64[s]')

However, the E+18 postfix implies that if you really have a date, your timestamps are in nanoseconds, so datetime64[ns] may work for you

import time
import numpy as np
>>> a = np.array([time.time() * 10**9])  # epoch seconds to ns
>>> a  # example array
array([1.60473147e+18])
>>> a = np.asarray(a, dtype='datetime64[ns]')
>>> a
array(['2020-11-07T06:44:29.714103040'], dtype='datetime64[ns]')
ti7
  • 16,375
  • 6
  • 40
  • 68