pyyeti.nastran.bulk.rdcards¶
- pyyeti.nastran.bulk.rdcards(f, name, *, blank=None, return_var='array', dtype=<class 'float'>, no_data_return=None, regex=False, keep_name=False, keep_comments=False, follow_includes=True, include_symbols=None, include_root_dirs=None, encoding='utf_8')[source]¶
Read Nastran cards (lines) into a matrix, dictionary, or list.
- Parameters:
f (string or file_like or None) – Either a name of a file, or is a file_like object as returned by
open(). If file_like object, it is rewound first. Can also be the name of a directory or None; in these cases, a GUI is opened for file selection.name (string) – Usually the card name, but is really just the initial part of the string to look for. This means that name can span more than one field, in case that is handy. It can also be a regular expression if the regex option is set to True. This routine is case insensitive: if regex is False, name is converted to lower case as are the lines that are read in from the file; if regex is True, the
re.IGNORECASEoption is used.blank (None, scalar or string; optional) – If reading into an array or dictionary (see return_var), blank is expected to be a numeric value to use for blank fields and string-valued fields. If reading into a list, it can also be a string (like
""). If None (the default), it is internally reset to""if return_var is"list"and to0otherwise.return_var (string; optional) – Specifies which data structure to use for the data:
return_var
Routine returns
‘array’
a numpy ndarray of type dtype; non-numeric fields are set to blank
‘list’
a list of lists; this is the “truest” reader since string fields are retained and only blank fields are set to blank
‘dict’
a dictionary; each card becomes a dictionary entry: the value is a numpy ndarray of type dtype and the key is the first element of that ndarray (but before converting to dtype)
dtype (data-type; optional) – The desired array data-type for the ‘array’ or ‘dict’ output options.
no_data_return (any variable; optional) – If no data is found, this routine returns no_data_return.
regex (bool; optional) – If set to True, use regular expression matching instead of literal string matching.
keep_name (bool; optional) – If reading into a list, keep_name can be set to True to retain the card name in the output. This option is ignored if not reading into a list.
keep_comments (bool; optional) – If reading into a list, keep_comments can be set to True to retain all comment cards in the output. This option is ignored if not reading into a list. Note that only the comments that start at the beginning of the line are retained.
follow_includes (bool; optional) – If True, INCLUDE statements will be followed recursively. Note that if f is a StringIO object, or another object that does not have a name property, this parameter will be set to False.
include_symbols (dict; optional) – A dictionary mapping Nastran symbols to an associated path. These can be read from a file using
rdsymbols().include_root_dirs (None; optional) – This parameter is only used when this function is called recursively while following INCLUDE statements. Users should keep it as the default value of None.
encoding (string; optional) – Encoding to use when opening text file. This option will be passed to any files read in via the follow_includes option.
- Returns:
cards (ndarray or list or dictionary or no_data_return) –
If an ndarray is returned, each row is a card, padded with blanks as necessary.
If a list (of lists) is returned, each item is a card and is a list of values from the card, the length of which is the number of items on the card.
If a dictionary is returned, the first number from each card is used as the key and the value are all the numbers from the card in a row vector (including the first value).
no_data_return is returned if no cards of requested name were found.
Notes
This routine can read fixed field (8 or 16 chars wide) or comma-delimited and can handle any number of continuations. Note that the characters in the continuation fields are ignored. It also uses
nas_sscanf()to read the numbers, so numbers like 1.-3 (which means 1.e-3) are okay. Blank fields and string fields are set to blank, except when return_var is ‘list’; in that case, string fields are kept as is and only blank fields are set to blank.Note: this routine has no knowledge of any card, which means that it will not append trailing blanks to a card. For example, if a GRID card is: ‘GRID, 1’, then this routine would return
[1], not[1, 0, 0, 0, 0, 0, 0, 0]. Therdgrids()routine would return[1, 0, 0, 0, 0, 0, 0, 0]since it knows the number of fields a GRID card has.Examples
Create some bulk data to read (not necessarily valid):
>>> from io import StringIO >>> from pyyeti.nastran import bulk >>> fs = StringIO(''' ... DTI SELOAD 1 2 ... dti seload 3 4 ... $ a comment for testing ... dti,seload,5,6 ... DTI, SELOAD, , 8.0, 'a' ... DTI,SETREE,100,0 ... ''')
Read all the DTI cards to a list:
>>> lst = bulk.rdcards(fs, 'dti', return_var='list') >>> for item in lst: print(item) ['SELOAD', 1, 2] ['seload', 3, 4] ['seload', 5, 6] ['SELOAD', '', 8.0, "'a'"] ['SETREE', 100, 0]
Read some of the DTI,SELOAD cards (it’s case-insensitive):
>>> bulk.rdcards(fs, 'dti seload') array([[ 0., 1., 2.], [ 0., 3., 4.]])
Use a regular expression for more power:
>>> bulk.rdcards(fs, r'DTI(,\s*|\s+)SELOAD', regex=True) array([[ 0., 1., 2., 0.], [ 0., 3., 4., 0.], [ 0., 5., 6., 0.], [ 0., 0., 8., 0.]])
Same, but read into a list:
>>> lst = bulk.rdcards(fs, r'DTI(,\s*|\s+)SELOAD', regex=True, ... return_var='list') >>> for item in lst: print(item) ['SELOAD', 1, 2] ['seload', 3, 4] ['seload', 5, 6] ['SELOAD', '', 8.0, "'a'"]
Include the card name as well:
>>> lst = bulk.rdcards(fs, r'DTI(,\s*|\s+)SELOAD', regex=True, ... return_var='list', keep_name=True) >>> for item in lst: print(item) ['DTI', 'SELOAD', 1, 2] ['dti', 'seload', 3, 4] ['dti', 'seload', 5, 6] ['DTI', 'SELOAD', '', 8.0, "'a'"]
For the most accurate read of the data, include comments:
>>> lst = bulk.rdcards(fs, r'DTI(,\s*|\s+)SELOAD', regex=True, ... return_var='list', keep_name=True, ... keep_comments=True) >>> for item in lst: print(f"{item!r}") ['DTI', 'SELOAD', 1, 2] ['dti', 'seload', 3, 4] '$ a comment for testing\n' ['dti', 'seload', 5, 6] ['DTI', 'SELOAD', '', 8.0, "'a'"]