Skip to content

Commit

Permalink
Add repl language tags for examples (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhro authored May 3, 2024
1 parent 0ea99f4 commit 28d5845
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ The interface is inspired by Erin Sheldon's

`FITSIO.jl` can be installed using the built-in package manager

```julia
```julia-repl
pkg> add FITSIO
```

## Usage

To open an existing file for reading:
```julia
```julia-repl
julia> using FITSIO
julia> f = FITS("file.fits")
Expand All @@ -43,7 +43,7 @@ concatenated one after the other. The `FITS` object therefore is
represented as a collection of these HDUs.

Get information about the first HDU:
```julia
```julia-repl
julia> f[1]
File: file.fits
HDU: 1
Expand All @@ -53,7 +53,7 @@ Datasize: (800, 800)
```

Iterate over HDUs in the file:
```julia
```julia-repl
julia> for hdu in f; println(typeof(hdu)); end
FITSIO.ImageHDU
FITSIO.TableHDU
Expand All @@ -63,7 +63,7 @@ FITSIO.TableHDU
Each HDU can contain image data, or table data (either binary or
ASCII-formatted). For image extensions, get the size of the image
without reading it:
```julia
```julia-repl
julia> ndims(f[1])
2
Expand All @@ -76,15 +76,15 @@ julia> size(f[1], 2)


Read an image from disk:
```julia
```julia-repl
julia> data = read(f[1]); # read an image from disk
julia> data = read(f[1], :, 790:end); # read just a subset of image
```


Show info about a binary table:
```julia
```julia-repl
julia> f[2]
File: file.fits
HDU: 2
Expand All @@ -97,19 +97,19 @@ Columns: Name Size Type TFORM


Read a column from the table:
```julia
julia> data = read(f[2], "col1")
```julia-repl
julia> data = read(f[2], "col1")
```

Table HDUs implement the [Tables.jl](https://tables.juliadata.org/stable/) interface, so you can load them into other table types, like [DataFrames](https://dataframes.juliadata.org/stable/).
```
```julia-repl
julia> df = DataFrame(f[2])
```
Variable length columns are not supported by the Tables.jl interface, and `Tables` methods will ignore them.


Read the entire header into memory and get values from it:
```julia
```julia-repl
julia> header = read_header(f[1]); # read the entire header from disk
julia> length(header) # total number of records in header
Expand All @@ -130,7 +130,7 @@ julia> get_comment(header, "NAXIS") # get comment for a given keyword


Read just a single header record without reading the entire header:
```julia
```julia-repl
julia> read_key(f[1], 4) # by position
("NAXIS1",800,"length of data axis 1")
Expand All @@ -140,22 +140,22 @@ julia> read_key(f[1], "NAXIS1") # read by keyword


Manipulate a header in memory:
```julia
```julia-repl
julia> header["NEWKEY"] = 10 # change or add a keyword
julia> set_comment!(header, "NEWKEY", "this is a comment")
```


Close the file:
```julia
```julia-repl
julia> close(f)
```
(`FITS` objects are also closed automatically when garbage collected.)


Open a new file for writing:
```julia
```julia-repl
julia> f = FITS("newfile.fits", "w");
```
The second argument can be `"r"` (read-only; default), `"r+"`
Expand All @@ -164,7 +164,7 @@ the same name is overwritten.


Write an image to the file:
```julia
```julia-repl
julia> data = reshape([1:100;], 5, 20)
julia> write(f, data) # Write a new image extension with the data
Expand All @@ -175,7 +175,7 @@ To write some header keywords in the new extension, pass a


Overwrite image data in an existing file:
```julia
```julia-repl
julia> f = FITS("newfile.fits", "r+") # Reopen the file in read-write mode
julia> data = reshape([101:200;], 5, 20) # Prepare new image data
julia> image_hdu = f[1]
Expand All @@ -184,7 +184,7 @@ julia> write(image_hdu, data) # Overwrite the image


Write a table to the file:
```julia
```julia-repl
julia> data = Dict("col1"=>[1., 2., 3.], "col2"=>[1, 2, 3]);
julia> write(f, data) # write a new binary table to a new extension
Expand All @@ -193,7 +193,7 @@ julia> write(f, data) # write a new binary table to a new extension
!!! tip "Compressed storage"
Setting the file extension to `.gz` will automatically use GZIP compression and save on storage space.

```julia
```julia-repl
julia> FITS("abc.fits", "w") do f # save the image uncompressed
write(f, ones(200,200))
end
Expand All @@ -211,7 +211,7 @@ julia> write(f, data) # write a new binary table to a new extension

Alternately the compression algorithm might be specified in square brackets after the filename. Check the [CFITSIO website](https://heasarc.gsfc.nasa.gov/docs/software/fitsio/compression.html) for the details of this usage.

```julia
```julia-repl
julia> FITS("abc.fits[compress R 100,100]", "w") do f # Rice algorithm with a 100 x 100 pixel tile size
write(f, ones(200,200))
end
Expand All @@ -221,4 +221,4 @@ julia> write(f, data) # write a new binary table to a new extension
```

!!! warn
Compression is "loss-less" for images with integer pixel values, and might be lossy for floating-point images.
Compression is "loss-less" for images with integer pixel values, and might be lossy for floating-point images.

0 comments on commit 28d5845

Please sign in to comment.