Skip to content

Commit

Permalink
Updated the way a function was written and readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
bibhuraushan committed Feb 16, 2024
1 parent 6f8563d commit 9ddfb12
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 146 deletions.
162 changes: 64 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,140 +1,106 @@
![aft Logo](aftpy_logo.png)
# AFTpy
The Python package enables users to download, visualize, and analyze
AFT HDF (h5) data seamlessly. `AFTPy` offers a comprehensive solution for
analyzing and downloading Advective Flux Transport (AFT) data,
streamlining the process of data downloading in parallel and
also facilitating the conversion of H5 files into the most popular
FITS file format or various other formats with ease.

## Installation
### From PyPI
```shell
pip install aftpy
```

### From source
```shell
git clone [email protected]:bibhuraushan/aftpy.git
cd aftpy
python setup.py install
```

# AFTpy
An installable python package to download, visualize and analyze AFT HDF (h5) data.
## Discriptions
`aftpy` provides two importent python module named `aftmap` and `aftgetdata`. These modules can be used
to read the single **aftmap file** or load all the files from a given directory.

## aftmap
This is the module that read
either one file or all files from a given directory as an object.
## aftmap module
`aftmap` modules also provides two python class `AFTmap` and `AFTload`. The `AFTmap` class
provide an interface to read the singale H5 **AFTmap** file and provide you the functions and isntances
to get the information and plot the data. The other class `AFTload` provides the interface to load
all the data from a directory and provide the instances and function to know about the loaded data. It also
provides a function to convert all the loaded data in to popular **FITS** file.

These objects can be used to visulaize the data use tha data
and also convert the data to desired format.
### AFTmap Class

## AFTload Class
### AFTload Class

A class for loading AFT maps from files.
A class for loading all AFT maps from directory.

### Attributes
#### Attributes

- `path` (str): The path to the directory containing AFT map files.
- `filetype` (str): The file extension of the AFT map files (e.g., "h5").
- `date_fmt` (str): The date format string used to parse timestamps from filenames.
- `filelist` (list): A list of file paths to AFT map files in the specified directory.
- `filenames` (numpy.ndarray): An array of filenames extracted from the filelist.

### Methods

### `__init__(path=".", filetype="h5", date_fmt="AFTmap_%Y%m%d_%H%M.h5", verbose=True)`

Initialize the AFTload object.

#### Arguments

- `path` (str, optional): The path to the directory containing AFT map files. Defaults to current directory.
- `filetype` (str, optional): The file extension of the AFT map files. Defaults to "h5".
- `date_fmt` (str, optional): The date format string used to parse timestamps from filenames. Defaults to "AFTmap_%Y%m%d_%H%M.h5".
- `verbose` (bool, optional): Whether to print statistics about the loaded AFT map files. Defaults to True.

### `convert_all(convert_to="fits", outpath=".", verbose=True)`

Convert all loaded AFT map files to the specified format.
#### Methods

#### Arguments
- `convert_all(convert_to="fits", outpath=".", verbose=True)` Convert all loaded AFT map files to the specified format.
- `convert_to` (str, optional): The output format to convert the AFT map files to. Defaults to "fits".
- `outpath` (str, optional): The directory path to save the converted files. Defaults to current directory.
- `verbose` (bool, optional): Whether to print conversion progress. Defaults to True.

- `convert_to` (str, optional): The output format to convert the AFT map files to. Defaults to "fits".
- `outpath` (str, optional): The directory path to save the converted files. Defaults to current directory.
- `verbose` (bool, optional): Whether to print conversion progress. Defaults to True.

### Example Usage
#### Example Usage

```python
import aftpy.aftmap as aft

# Initialize AFTload object
loader = AFTload(path="/path/to/aft/maps", filetype="h5")
loader = aft.AFTload(path="/path/to/aft/maps", filetype="h5")

# Convert all AFT map files to FITS format
# Convert all AFT map files to FITS format to '/path/to/converted/maps'
loader.convert_all(convert_to="fits", outpath="/path/to/converted/maps", verbose=True)
```

# AFTdownload Class

## aftgetdata module

### AFTdownlaod Clas
A class for downloading AFT map files from a specified URL.

## Attributes
#### Attributes

- `ncpu` (int): Number of CPU cores to utilize for downloading files. Defaults to `cpu_count() - 1`.
- `dt` (module): Alias for the `datetime` module.
- `root_url` (str): The root URL from where AFT map files will be downloaded.
- `root_url` (str): The root URL from where AFT map files will be downloaded. Defaults to "https://data.boulder.swri.edu/lisa/".
- `urls` (list): List of URLs of AFT map files.
- `times` (list): List of timestamps corresponding to the AFT map files.
- `counts` (int): Total count of AFT map files.
- `datafile` (str): File path to store the list of files in CSV format.
- `datalist` (DataFrame): DataFrame containing the list of files and corresponding timestamps.

## Methods

### `__init__(root_url="https://data.boulder.swri.edu/lisa/")`

Initialize the AFTdownload object.

#### Arguments

- `root_url` (str, optional): The root URL from where AFT map files will be downloaded. Defaults to "https://data.boulder.swri.edu/lisa/".

### `get_list(t0=None, t1=None, dt=1)`

Get a list of AFT map files within a specified time range.

#### Arguments

- `t0` (datetime.datetime, optional): Start time of the time range. Defaults to None.
- `t1` (datetime.datetime, optional): End time of the time range. Defaults to None.
- `dt` (int, optional): Time interval for sampling files within the time range. Defaults to 1.

#### Returns

- `data` (DataFrame): DataFrame containing the list of files within the specified time range.

### `reload_files(url=None, filetype="h5")`

Reload the list of AFT map files from the root URL.

#### Arguments

- `url` (str, optional): The URL to reload the list of files from. Defaults to None.
- `filetype` (str, optional): The file extension of AFT map files. Defaults to "h5".

#### Returns

- `bool`: True if the list of files is successfully reloaded.

### `download(dataframe, rootpath=None, ncpu=None)`

Download AFT map files listed in the DataFrame.

#### Arguments

- `dataframe` (DataFrame): DataFrame containing the list of files to download.
- `rootpath` (str, optional): Root directory path to save the downloaded files. Defaults to None.
- `ncpu` (int, optional): Number of CPU cores to utilize for downloading files. Defaults to `cpu_count() - 1`.

### `_download(args)`

Internal function to download AFT map files.

#### Arguments

- `args` (tuple): Tuple containing URL and root path of the file to download.
#### Methods
- `get_list(t0=None, t1=None, dt=1) -> data (DataFrame)`
- `t0` (datetime.datetime, optional): Start time of the time range. Defaults to None.
- `t1` (datetime.datetime, optional): End time of the time range. Defaults to None.
- `dt` (int, optional): Time interval for sampling files within the time range. Defaults to 1.
- `Returns` data (DataFrame): DataFrame containing the list of files within the specified time range.

#### Returns
- `reload_files(url=None, filetype="h5")` Reload the list of AFT map files from the root URL.
- `url` (str, optional): The URL to reload the list of files from. Defaults to None.
- `filetype` (str, optional): The file extension of AFT map files. Defaults to "h5".
- `Returns`: True if the list of files is successfully reloaded.
- `download(dataframe, rootpath=None, ncpu=None)` Download AFT map files listed in the DataFrame.
- `dataframe` (DataFrame): DataFrame containing the list of files to download.
- `rootpath` (str, optional): Root directory path to save the downloaded files. Defaults to None.
- `ncpu` (int, optional): Number of CPU cores to utilize for downloading files. Defaults to `cpu_count() - 1`.

- `str`: Message indicating whether the file was successfully downloaded or not.

## Example Usage
#### Example Usage

```python
import aftpy.getaftdata as aftget
# Initialize AFTdownload object
downloader = AFTdownload()
downloader = aftget.AFTdownload()

# Reload the list of AFT map files
downloader.reload_files()
Expand Down
4 changes: 3 additions & 1 deletion aftpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

__author__ = 'Bibhuti Kumar Jha'
__email__ = '[email protected]'
__version__ = '1.0.1'
51 changes: 29 additions & 22 deletions aftpy/aftmap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple, Any

import h5py as hdf
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -6,6 +8,9 @@
from astropy.io import fits
import os
from pathlib import Path

from numpy import ndarray, dtype

this_directory = Path(__file__).parent
import sunpy.visualization.colormaps

Expand All @@ -16,7 +21,7 @@
print("Using default matplotlib style.")


def file_search(path, fileext=None):
def file_search(path, fileext: str = None) -> np.ndarray:
"""
Search for files with the specified file extension in the given directory.
Expand Down Expand Up @@ -66,7 +71,9 @@ class AFTmap:
"vlon": "Phi Component of flows at the surface.",
"magmap": "Assimilated magnetogram in Carrington Grid."}

def __init__(self, file, filetype="h5", date_fmt="AFTmap_%Y%m%d_%H%M.h5", timestamp=None):
def __init__(self, file, filetype: str = "h5",
date_fmt: str = "AFTmap_%Y%m%d_%H%M.h5",
timestamp: dt.datetime = None):
"""
Initialize an AFTmap object.
Expand All @@ -91,11 +98,11 @@ def __init__(self, file, filetype="h5", date_fmt="AFTmap_%Y%m%d_%H%M.h5", timest
self.timestamp = Time(timestamp).fits

@property
def contents(self):
def contents(self) -> list:
return self.map_list

@property
def aftmap(self):
def aftmap(self) -> np.ndarray:
"""
Get the AFT map data.
Expand All @@ -121,7 +128,7 @@ def aftmap(self):
return bmap

@property
def mask(self):
def mask(self) -> np.ndarray:
"""
Get the mask data.
Expand All @@ -138,7 +145,7 @@ def mask(self):
return mask

@property
def vv(self):
def vv(self) -> tuple[ndarray[Any, dtype[Any]] | None, ndarray[Any, dtype[Any]] | None]:
"""
Get vlat and vlon data.
Expand All @@ -155,7 +162,7 @@ def vv(self):
return vlat, vlon

@property
def metadata(self):
def metadata(self) -> dict[str, Any]:
"""
Get metadata.
Expand All @@ -175,23 +182,23 @@ def metadata(self):
return header

@property
def time(self):
def time(self) -> str:
if self.filetype == "h5":
return self.metadata["map_date"]
elif self.filetype == "dat":
_time = Time(datetime.datetime.strptime(
_time = Time(dt.datetime.strptime(
self.name, self.date_fmt))
elif self.filetype == "hipft":
_time = self.timestamp
return _time

@property
def ymd(self):
def ymd(self) -> tuple:
year, month, day = self.time.split("-")[0:3]
return year, month, day[0:2]

@property
def info(self):
def info(self) -> dict[str, Any]:
"""
Get additional information.
Expand All @@ -211,7 +218,7 @@ def info(self):
info = None
return info

def convert(self, convert_to="fits", outpath=".", verbose=True):
def convert(self, convert_to: str = "fits", outpath: str = ".", verbose: bool = True):
if self.filetype == "h5":
header = self.metadata
data = self.aftmap
Expand All @@ -224,7 +231,7 @@ def convert(self, convert_to="fits", outpath=".", verbose=True):
else:
print("No implemented Yet.")

def plot(self, show_mask=True, save=False):
def plot(self, show_mask: bool = True, save: bool = False) -> tuple:
"""
Display or save the AFT map visualization.
Expand Down Expand Up @@ -268,7 +275,7 @@ def plot(self, show_mask=True, save=False):
plt.show()
return fig, ax

def __str__(self):
def __str__(self) -> str:
"""
Get a string representation of the object.
Expand Down Expand Up @@ -312,9 +319,9 @@ class AFTload:
- stats(): Prints statistics about the loaded AFT map files.
"""

def __init__(self, path=".", filetype="h5",
date_fmt="AFTmap_%Y%m%d_%H%M.h5",
verbose=True, hipft_prop=None):
def __init__(self, path: str = ".", filetype: str = "h5",
date_fmt: str = "AFTmap_%Y%m%d_%H%M.h5",
verbose: bool = True, hipft_prop: dict = None):
"""
Initialize the AFTload object.
Expand All @@ -328,7 +335,7 @@ def __init__(self, path=".", filetype="h5",
self.path = path
self.filetype = filetype
self.date_fmt = date_fmt
if filetype=="hipft":
if filetype == "hipft":
_fileext = "h5"
else:
_fileext = filetype
Expand All @@ -355,13 +362,13 @@ def timestamps(self):
else:
_t0 = self.hipft_prop["T0"]
_dt = self.hipft_prop["dt"]
_timestamp = [_t0 + dt.timedelta(i*_dt) for i in range(len(self.filelist))]
_timestamp = [_t0 + dt.timedelta(i * _dt) for i in range(len(self.filelist))]
return Time(_timestamp)

def get_filelist(self):
def get_filelist(self) -> np.ndarray:
return self.filelist

def get_filenames(self):
def get_filenames(self) -> np.ndarray:
return np.array(self.filenames)

def stats(self):
Expand All @@ -374,7 +381,7 @@ def stats(self):
for _s in _stats.keys():
print("%-10s: %-30s" % (_s, _stats.get(_s)))

def convert_all(self, convert_to="fits", outpath=".", verbose=True):
def convert_all(self, convert_to: str = "fits", outpath: str = ".", verbose: bool = True):
"""
Convert all loaded AFT map files to the specified format.
Expand Down
Loading

0 comments on commit 9ddfb12

Please sign in to comment.