Our first task with python was to read a
csv file using
That function has few properties to define the dlimiter of the columns, skip rows, read commented lines, convert values while reading, etc.
However, the result is an array, without the information of the metadata that file may have included (name, units, ...).
Astropy offers a ascii reader that improves many of these steps while provides templates to read common ascii files in astronomy.
from astropy.io import ascii
# Read a sample file: sources.dat
Automatically, read has identified the header and the format of each column. The result is a
Table object, and that brings some additional properties.
# Show the info of the data read
# Get the name of the columns
# Get just the values of a particular column
# get the first element
# Read the data from the source table = ascii.read("ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/snrs.dat", readme="ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/ReadMe")
# See the stats of the table
# If we want to see the first 10 entries
# the units are also stored, we can extract them too
# Adding values of different columns
# adding values of different columns but being aware of column units
# Create a new column in the table
# Show table's new column
# add a description to the new column
# Now it does show the values
# Using numpy to calculate the sin of the RA
# Let's change the units...
# does the sin now works?
the reading of the table has many properties, let's imagine the following easy example:
weather_data = """ # Country = Finland # City = Helsinki # Longitud = 24.9375 # Latitud = 60.170833 # Week = 32 # Year = 2015 day, precip, type Mon,1.5,rain Tues,, Wed,1.1,snow Thur,2.3,rain Fri,0.2, Sat,1.1,snow Sun,5.4,snow """
# Read the table
# Blank values are interpreted by default as bad/missing values
# Let's define missing values for the columns we want:
# Use filled to show the value filled.
# We can see the meta as a dictionary, but not as key, value pairs
# To get it the header as a table
VOTables are an special type of tables which should be self-consistent and can be tied to a particular scheme. This mean the file will contain where the data comes from (and which query produced it) and the properties for each field, making it easier to ingest by a machine.
from astropy.io.votable import parse_single_table
# Read the example table from HELIO (hfc_ar.xml)
# See the fields of the table
# extract one (NOAA_NUMBER) or all of the columns
# Show the data
# See the mask
# Shee the whole array.
# Convert the table to an astropy table
# See the table
# Different results because quantities are not
# And it can also be converted to other units