Giter VIP home page Giter VIP logo

rms-vicar's Introduction

GitHub release; latest by date GitHub Release Date Test Status Documentation Status Code coverage
PyPI - Version PyPI - Format PyPI - Downloads PyPI - Python Version
GitHub commits since latest release GitHub commit activity GitHub last commit
Number of GitHub open issues Number of GitHub closed issues Number of GitHub open pull requests Number of GitHub closed pull requests
GitHub License Number of GitHub stars GitHub forks

Introduction

vicar is a Python module that supports reading and writing of JPL's VICAR file format. It supports the definition of the VICAR file format as found here: https://pds-rings.seti.org/help/VICAR_file_fmt.pdf

Installation

The vicar module is available via the rms-vicar package on PyPI and can be installed with:

pip install rms-vicar

Getting Started

The vicar module provides these classes:

  • VicarLabel: Class for reading, writing, and parsing of VICAR labels.
  • VicarImage: Class for handling VICAR image (and other) data files.
  • VicarError: Extension of class ValueError to contain exceptions.

Details of each class are available in the module documentation.

To read a VICAR image file:

import vicar
vic = vicar.VicarImage("path/to/file")

The resulting object contains:

  • vic.array: The 3-D data array converted to native format.
  • vic.array2d: Same as above, but with leading dimension (typically, bands) stripped.
  • vic.prefix: The array prefix bytes as a 3-D array of unsigned bytes.
  • vic.prefix2d: Same as above, but with the leading dimension stripped.
  • vic.binheader: The binary header as a bytes object; use vic.binheader_array() to extract information.
  • vic.label: The internal VicarLabel object that manages the VICAR label information, if direct access is needed.

VICAR parameter values can be extracted from the label using dictionary-like syntax:

  • len(vic): The number of parameters in the VICAR label.
  • vic['LBLSIZE']: The value of the LBLSIZE parameter (an integer).
  • vic[0]: The value of the first parameter.
  • vic[-1]: The value of the last parameter.
  • vic['LBLSIZE',-1]: The value of the last occurrence of the LBLSIZE parameter.
  • vic.get(('LBLSIZE',2), 99): The value of the third occurrence of the LBLSIZE parameter, or 99 if there are fewer than 3 occurrences.
  • vic.arg('LBLSIZE'): The numeric index of "LBLSIZE" among the VICAR parameters.

You can also use dictionary-like syntax to modify and insert header values:

  • vic['SOLDIST'] = 1.e9: Set SOLDICT to this value.
  • del vic['SOLDIST',0]: Remove the first occurrence of SOLDIST from the label.
  • vic['LBLSIZE+'] = 2000: Insert a new LBLSIZE parameter instead of modifying an existing one.

Note that certain required VICAR parameters contain structural information about the file; these cannot generally be modified directly.

Numerous methods are available to iterate over the VICAR label parameters:

for (name,value) in vic.items(): ...
for key in vic.keys(): ...
for name in vic.names(): ...
for value in vic.values(): ...

Iterators can take a regular expression as input to restrict the items returned:

for value in vic.values(r'LAB\d\d'): ...

Use str(vic) to get the VICAR label content represented as a string.

Here are the steps to create and write a VICAR image file:

import vicar
vic = vicar.VicarImage()
vic.array = array
vic.prefix = prefix
vic.binheader = binheader
vic['NOTES'] = ['add as many more VICAR parameters', 'as you wish']
vic.write_file("path/to/file")

Contributing

Information on contributing to this package can be found in the Contributing Guide.

Links

Licensing

This code is licensed under the Apache License v2.0.

rms-vicar's People

Contributors

rfrenchseti avatar markshowalter avatar

Watchers

 avatar  avatar  avatar Yu-Jen Chang avatar Matthew Tiscareno avatar Debby Stopp avatar

Forkers

andrewannex

rms-vicar's Issues

Failure reading Cassini calibrated images

Calling vicar.VicarImage.from_file('COISS_2xxx/COISS_2025/data/1536500809_1536639048/N1536633072_1_CALIB.IMG') results in:

  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/oops/hosts/cassini/iss.py", line 38, in from_file
    vic = vicar.VicarImage.from_file(filespec)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/vicar/vicarimage.py", line 391, in from_file
    info = VicarImage._read_file(filepath, extraneous=extraneous)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/vicar/vicarimage.py", line 529, in _read_file
    (label, extra) = VicarLabel.read_label(f, _extra=True)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/vicar/vicarlabel.py", line 1089, in read_label
    ldict = VicarLabel(label)
            ^^^^^^^^^^^^^^^^^
  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/vicar/vicarlabel.py", line 153, in __init__
    params = _LABEL_GRAMMAR.parse_string(source).as_list()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/seti/research/f-ring/venv_fring/lib/python3.12/site-packages/pyparsing/core.py", line 1197, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected end of text, found 'UNEVEN'  (at char 2772), (line:1, col:2773)

The problem is that the label item UNEVEN_BIT_WEIGHT_CORRECTION_FLAG violates the VICAR standard by being more than 32 characters long.

write_label ignores filepath parameter

the function write_label in VicarLabel ignores the user provided filepath parameter:

rms-vicar/vicar/vicarlabel.py

Lines 1150 to 1158 in 255c991

if not filepath:
filepath = self._filepath
if not self._filepath:
raise ValueError('file path is missing')
with self._filepath.open('r+b') as f:
snippet = f.read(40).decode('latin8')

now that first read at snippet may be important to do from the original file, but this function should actually write bits to whatever filepath parameter is provided. It may also be useful to have the ability to write labels that exist purely in memory also so the label size may need to be computed without the snippet if the filepath points to a file that does not exist on disk yet.

Add tests

There are currently no tests. Add some tests and then update run-tests.yml to run them, and the GitHub repo settings to require passing tests before merging to the main branch.

Add tests

There are currently no tests. Add some tests and then update run-tests.yml to run them, and the GitHub repo settings to require passing tests before merging to the main branch.

VicarError on inserting new parameter with occurrence

For some use cases, I have a large number of parameters with repeated occurrences I want to convert to a VICAR label that is essentially empty as the data will be coming to me as some large nested json object. However, I get a VicarError in VicarLabel.__setitem__ when I attempt to use tuples as the key like label[('TEST',1)] = 2.

I am able to update the 2nd occurrence one I've created the first and then create the 2nd

lbl['TEST']  = 1
lbl['TEST+'] = 2
print('before', lbl[('TEST',1)])
lbl[('TEST',1)] =5
print('after', lbl[('TEST',1)])

but I get the error if I do

lbl[('TEST',0)]  = 1
# or
lbl[('TEST+',1)] = 2
# or
lbl['TEST',0] = 1,
# but the prior line does work if I called lbl['TEST'] = 1 earlier,

maybe this is just expected behavior, but it complicates situations where order of parameters is not known ahead of time or cannot be guaranteed for whatever reason. I am not sure exactly what the correct behavior should be though. If a user wants to update the 3rd occurrence of a parameter that has not yet had the first occurrence set, it could be reasonable to check for prior occurrences, setting those that are missing to a null value so that users don't have to track the order of things. If the keys all exist already then it shouldn't modify those prior values, and then just do the normal thing for the user provided occurrence (either set or append).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.