1
$\begingroup$

I downloaded Coastal buoy data from the CDIP webpages and saved it as a text file. The trouble is that the data has spaces between the YYY MM DD hh mm.

{Edit 4/5/2019: Updated link Please see comment below}

With FileNames and Import, I was able to import the data from a text file.

fn = FileNames["*.txt"]; (*I am interested in the first text file in this list*)
h2018 = Import[fn[[1]], "Data"]

What I want to do is convert the first 5 columns to DateList and then plot the 9th column against this new DateList.

I tried doing this to no effect because I am trying to convert the data to a string and then replacing spaces in the new string with / and then using DateList.

StringReplace[ToString[h2018[[3 ;;, 1 ;; 3]]], "," .. -> "/"];
StringReplace[%, " " -> ""];
dl=DateList[%];

How do I convert this string to a DateList and complete my plotting of column 9 in h2018 with the DateList, dl?

I did try to convert this string to a list of numbers using the information in this question but without success.

Flatten@ToExpression@StringSplit[%];
$\endgroup$
  • 1
    $\begingroup$ Link to txt file no longer works. This works. $\endgroup$ – Rohit Namjoshi Apr 6 '19 at 18:10
  • $\begingroup$ @RohitNamjoshi Thank you! I have updated the question with your link. $\endgroup$ – dearN Apr 6 '19 at 18:13
2
$\begingroup$

Use the functions in the Date & Time guide to manipulate dates. This step-by-step answer.

Import the table.

raw = Import["http://www.ndbc.l.noaa.gov/data/realtime2/46218.txt", "Table"];

Notice that Mathematica has imported the numbers as numbers and not as strings. Look at row 3 and columns from the start to column 5.

Head /@ raw[[3, ;; 5]]
{Integer, Integer, Integer, Integer, Integer}

First collapse the 5 column headers for the date-times in row 1 and 2. This is not strictly needed but will keep the headers consistent with the data.

raw[[;; 2]] = Flatten@MapAt[StringJoin, 1]@TakeDrop[#, 5] & /@ raw[[;; 2]];

Next feed the values in the first 5 columns into DateObject. The columns are in the exact order that DateObject expects so there is no need to adjust their positions. This is performed on row 3 until the end.

raw[[3 ;;]] = Flatten@MapAt[DateObject, 1]@TakeDrop[#, 5] & /@ raw[[3 ;;]];

Since the first 5 columns have been collapsed into 1 the information is now in column 5 instead of column 9, (9 - 5 + 1). Take from row 3 until the end and columns 1 and 5 to plot with DateListPlot.

DateListPlot[raw[[3 ;;, {1, 5}]]]

Mathematica graphics

Hope this helps.

| improve this answer | |
$\endgroup$
2
$\begingroup$

Here's one way to do it:

data = Import["http://www.ndbc.l.noaa.gov/data/realtime2/46218.txt",
              "Table", HeaderLines -> 2][[All, {1, 2, 3, 4, 5, 9}]];

ts = TimeSeries[Composition[Flatten, MapAt[DateObject, #, 1] &,
                            Reverse, TakeDrop[#, -1] &] /@ data];

DateListPlot[ts]

plot of wave heights over time

(If you don't want to convert to a TimeSeries[], you can omit that part.)


Since the other answer has already been accepted, let me just illustrate how one might use the functionality of Dataset[] for this task:

data = Import["http://www.ndbc.l.noaa.gov/data/realtime2/46218.txt", "Table"];
data = MapAt[StringReplace[#, "#" -> ""] &, Delete[data, 2], 1];
data = Dataset[AssociationThread[First[data], #] & /@ Rest[data]];

ds = <|"Timestamp" -> DateObject[{#YY, #MM, #DD, #hh, #mm}], 
       "WaveHeight" -> Quantity[#WVHT, "Meters"]|> & /@ data

Show[ds[DateListPlot, All], FrameLabel -> {None, "Wave Height (m)"}]

labeled plot

| improve this answer | |
$\endgroup$
  • $\begingroup$ Woah! This seems to be fairly complex an operation. $\endgroup$ – dearN Mar 13 '18 at 15:47
  • $\begingroup$ Very interesting on the DataSet example. $\endgroup$ – dearN Mar 19 '18 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.