123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725 |
- // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
- // Copyright 2008-2016 National ICT Australia (NICTA)
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ------------------------------------------------------------------------
- //! \addtogroup diskio
- //! @{
- //! Generate the first line of the header used for saving matrices in text format.
- //! Format: "ARMA_MAT_TXT_ABXYZ".
- //! A is one of: I (for integral types) or F (for floating point types).
- //! B is one of: U (for unsigned types), S (for signed types), N (for not applicable) or C (for complex types).
- //! XYZ specifies the width of each element in terms of bytes, e.g. "008" indicates eight bytes.
- template<typename eT>
- inline
- arma_cold
- std::string
- diskio::gen_txt_header(const Mat<eT>&)
- {
- arma_type_check(( is_supported_elem_type<eT>::value == false ));
-
- if( is_u8<eT>::value) { return std::string("ARMA_MAT_TXT_IU001"); }
- else if( is_s8<eT>::value) { return std::string("ARMA_MAT_TXT_IS001"); }
- else if(is_u16<eT>::value) { return std::string("ARMA_MAT_TXT_IU002"); }
- else if(is_s16<eT>::value) { return std::string("ARMA_MAT_TXT_IS002"); }
- else if(is_u32<eT>::value) { return std::string("ARMA_MAT_TXT_IU004"); }
- else if(is_s32<eT>::value) { return std::string("ARMA_MAT_TXT_IS004"); }
- #if defined(ARMA_USE_U64S64)
- else if(is_u64<eT>::value) { return std::string("ARMA_MAT_TXT_IU008"); }
- else if(is_s64<eT>::value) { return std::string("ARMA_MAT_TXT_IS008"); }
- #endif
- #if defined(ARMA_ALLOW_LONG)
- else if(is_ulng_t_32<eT>::value) { return std::string("ARMA_MAT_TXT_IU004"); }
- else if(is_slng_t_32<eT>::value) { return std::string("ARMA_MAT_TXT_IS004"); }
- else if(is_ulng_t_64<eT>::value) { return std::string("ARMA_MAT_TXT_IU008"); }
- else if(is_slng_t_64<eT>::value) { return std::string("ARMA_MAT_TXT_IS008"); }
- #endif
- else if( is_float<eT>::value) { return std::string("ARMA_MAT_TXT_FN004"); }
- else if( is_double<eT>::value) { return std::string("ARMA_MAT_TXT_FN008"); }
- else if( is_cx_float<eT>::value) { return std::string("ARMA_MAT_TXT_FC008"); }
- else if(is_cx_double<eT>::value) { return std::string("ARMA_MAT_TXT_FC016"); }
-
- return std::string();
- }
- //! Generate the first line of the header used for saving matrices in binary format.
- //! Format: "ARMA_MAT_BIN_ABXYZ".
- //! A is one of: I (for integral types) or F (for floating point types).
- //! B is one of: U (for unsigned types), S (for signed types), N (for not applicable) or C (for complex types).
- //! XYZ specifies the width of each element in terms of bytes, e.g. "008" indicates eight bytes.
- template<typename eT>
- inline
- arma_cold
- std::string
- diskio::gen_bin_header(const Mat<eT>&)
- {
- arma_type_check(( is_supported_elem_type<eT>::value == false ));
-
- if( is_u8<eT>::value) { return std::string("ARMA_MAT_BIN_IU001"); }
- else if( is_s8<eT>::value) { return std::string("ARMA_MAT_BIN_IS001"); }
- else if(is_u16<eT>::value) { return std::string("ARMA_MAT_BIN_IU002"); }
- else if(is_s16<eT>::value) { return std::string("ARMA_MAT_BIN_IS002"); }
- else if(is_u32<eT>::value) { return std::string("ARMA_MAT_BIN_IU004"); }
- else if(is_s32<eT>::value) { return std::string("ARMA_MAT_BIN_IS004"); }
- #if defined(ARMA_USE_U64S64)
- else if(is_u64<eT>::value) { return std::string("ARMA_MAT_BIN_IU008"); }
- else if(is_s64<eT>::value) { return std::string("ARMA_MAT_BIN_IS008"); }
- #endif
- #if defined(ARMA_ALLOW_LONG)
- else if(is_ulng_t_32<eT>::value) { return std::string("ARMA_MAT_BIN_IU004"); }
- else if(is_slng_t_32<eT>::value) { return std::string("ARMA_MAT_BIN_IS004"); }
- else if(is_ulng_t_64<eT>::value) { return std::string("ARMA_MAT_BIN_IU008"); }
- else if(is_slng_t_64<eT>::value) { return std::string("ARMA_MAT_BIN_IS008"); }
- #endif
- else if( is_float<eT>::value) { return std::string("ARMA_MAT_BIN_FN004"); }
- else if( is_double<eT>::value) { return std::string("ARMA_MAT_BIN_FN008"); }
- else if( is_cx_float<eT>::value) { return std::string("ARMA_MAT_BIN_FC008"); }
- else if(is_cx_double<eT>::value) { return std::string("ARMA_MAT_BIN_FC016"); }
-
- return std::string();
- }
- //! Generate the first line of the header used for saving matrices in binary format.
- //! Format: "ARMA_SPM_BIN_ABXYZ".
- //! A is one of: I (for integral types) or F (for floating point types).
- //! B is one of: U (for unsigned types), S (for signed types), N (for not applicable) or C (for complex types).
- //! XYZ specifies the width of each element in terms of bytes, e.g. "008" indicates eight bytes.
- template<typename eT>
- inline
- arma_cold
- std::string
- diskio::gen_bin_header(const SpMat<eT>&)
- {
- arma_type_check(( is_supported_elem_type<eT>::value == false ));
-
- if( is_u8<eT>::value) { return std::string("ARMA_SPM_BIN_IU001"); }
- else if( is_s8<eT>::value) { return std::string("ARMA_SPM_BIN_IS001"); }
- else if(is_u16<eT>::value) { return std::string("ARMA_SPM_BIN_IU002"); }
- else if(is_s16<eT>::value) { return std::string("ARMA_SPM_BIN_IS002"); }
- else if(is_u32<eT>::value) { return std::string("ARMA_SPM_BIN_IU004"); }
- else if(is_s32<eT>::value) { return std::string("ARMA_SPM_BIN_IS004"); }
- #if defined(ARMA_USE_U64S64)
- else if(is_u64<eT>::value) { return std::string("ARMA_SPM_BIN_IU008"); }
- else if(is_s64<eT>::value) { return std::string("ARMA_SPM_BIN_IS008"); }
- #endif
- #if defined(ARMA_ALLOW_LONG)
- else if(is_ulng_t_32<eT>::value) { return std::string("ARMA_SPM_BIN_IU004"); }
- else if(is_slng_t_32<eT>::value) { return std::string("ARMA_SPM_BIN_IS004"); }
- else if(is_ulng_t_64<eT>::value) { return std::string("ARMA_SPM_BIN_IU008"); }
- else if(is_slng_t_64<eT>::value) { return std::string("ARMA_SPM_BIN_IS008"); }
- #endif
- else if( is_float<eT>::value) { return std::string("ARMA_SPM_BIN_FN004"); }
- else if( is_double<eT>::value) { return std::string("ARMA_SPM_BIN_FN008"); }
- else if( is_cx_float<eT>::value) { return std::string("ARMA_SPM_BIN_FC008"); }
- else if(is_cx_double<eT>::value) { return std::string("ARMA_SPM_BIN_FC016"); }
-
- return std::string();
- }
- //! Generate the first line of the header used for saving cubes in text format.
- //! Format: "ARMA_CUB_TXT_ABXYZ".
- //! A is one of: I (for integral types) or F (for floating point types).
- //! B is one of: U (for unsigned types), S (for signed types), N (for not applicable) or C (for complex types).
- //! XYZ specifies the width of each element in terms of bytes, e.g. "008" indicates eight bytes.
- template<typename eT>
- inline
- arma_cold
- std::string
- diskio::gen_txt_header(const Cube<eT>&)
- {
- arma_type_check(( is_supported_elem_type<eT>::value == false ));
-
- if( is_u8<eT>::value) { return std::string("ARMA_CUB_TXT_IU001"); }
- else if( is_s8<eT>::value) { return std::string("ARMA_CUB_TXT_IS001"); }
- else if(is_u16<eT>::value) { return std::string("ARMA_CUB_TXT_IU002"); }
- else if(is_s16<eT>::value) { return std::string("ARMA_CUB_TXT_IS002"); }
- else if(is_u32<eT>::value) { return std::string("ARMA_CUB_TXT_IU004"); }
- else if(is_s32<eT>::value) { return std::string("ARMA_CUB_TXT_IS004"); }
- #if defined(ARMA_USE_U64S64)
- else if(is_u64<eT>::value) { return std::string("ARMA_CUB_TXT_IU008"); }
- else if(is_s64<eT>::value) { return std::string("ARMA_CUB_TXT_IS008"); }
- #endif
- #if defined(ARMA_ALLOW_LONG)
- else if(is_ulng_t_32<eT>::value) { return std::string("ARMA_CUB_TXT_IU004"); }
- else if(is_slng_t_32<eT>::value) { return std::string("ARMA_CUB_TXT_IS004"); }
- else if(is_ulng_t_64<eT>::value) { return std::string("ARMA_CUB_TXT_IU008"); }
- else if(is_slng_t_64<eT>::value) { return std::string("ARMA_CUB_TXT_IS008"); }
- #endif
- else if( is_float<eT>::value) { return std::string("ARMA_CUB_TXT_FN004"); }
- else if( is_double<eT>::value) { return std::string("ARMA_CUB_TXT_FN008"); }
- else if( is_cx_float<eT>::value) { return std::string("ARMA_CUB_TXT_FC008"); }
- else if(is_cx_double<eT>::value) { return std::string("ARMA_CUB_TXT_FC016"); }
-
- return std::string();
- }
- //! Generate the first line of the header used for saving cubes in binary format.
- //! Format: "ARMA_CUB_BIN_ABXYZ".
- //! A is one of: I (for integral types) or F (for floating point types).
- //! B is one of: U (for unsigned types), S (for signed types), N (for not applicable) or C (for complex types).
- //! XYZ specifies the width of each element in terms of bytes, e.g. "008" indicates eight bytes.
- template<typename eT>
- inline
- arma_cold
- std::string
- diskio::gen_bin_header(const Cube<eT>&)
- {
- arma_type_check(( is_supported_elem_type<eT>::value == false ));
-
- if( is_u8<eT>::value) { return std::string("ARMA_CUB_BIN_IU001"); }
- else if( is_s8<eT>::value) { return std::string("ARMA_CUB_BIN_IS001"); }
- else if(is_u16<eT>::value) { return std::string("ARMA_CUB_BIN_IU002"); }
- else if(is_s16<eT>::value) { return std::string("ARMA_CUB_BIN_IS002"); }
- else if(is_u32<eT>::value) { return std::string("ARMA_CUB_BIN_IU004"); }
- else if(is_s32<eT>::value) { return std::string("ARMA_CUB_BIN_IS004"); }
- #if defined(ARMA_USE_U64S64)
- else if(is_u64<eT>::value) { return std::string("ARMA_CUB_BIN_IU008"); }
- else if(is_s64<eT>::value) { return std::string("ARMA_CUB_BIN_IS008"); }
- #endif
- #if defined(ARMA_ALLOW_LONG)
- else if(is_ulng_t_32<eT>::value) { return std::string("ARMA_CUB_BIN_IU004"); }
- else if(is_slng_t_32<eT>::value) { return std::string("ARMA_CUB_BIN_IS004"); }
- else if(is_ulng_t_64<eT>::value) { return std::string("ARMA_CUB_BIN_IU008"); }
- else if(is_slng_t_64<eT>::value) { return std::string("ARMA_CUB_BIN_IS008"); }
- #endif
- else if( is_float<eT>::value) { return std::string("ARMA_CUB_BIN_FN004"); }
- else if( is_double<eT>::value) { return std::string("ARMA_CUB_BIN_FN008"); }
- else if( is_cx_float<eT>::value) { return std::string("ARMA_CUB_BIN_FC008"); }
- else if(is_cx_double<eT>::value) { return std::string("ARMA_CUB_BIN_FC016"); }
-
- return std::string();
- }
- inline
- arma_deprecated
- file_type
- diskio::guess_file_type(std::istream& f)
- {
- arma_extra_debug_sigprint();
-
- return diskio::guess_file_type_internal(f);
- }
- inline
- arma_cold
- file_type
- diskio::guess_file_type_internal(std::istream& f)
- {
- arma_extra_debug_sigprint();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- f.clear();
- f.seekg(0, ios::end);
-
- f.clear();
- const std::fstream::pos_type pos2 = f.tellg();
-
- const uword N_max = ( (pos1 >= 0) && (pos2 >= 0) && (pos2 > pos1) ) ? uword(pos2 - pos1) : uword(0);
-
- f.clear();
- f.seekg(pos1);
-
- if(N_max == 0) { return file_type_unknown; }
-
- const uword N_use = (std::min)(N_max, uword(4096));
-
- podarray<unsigned char> data(N_use);
- data.zeros();
-
- unsigned char* data_mem = data.memptr();
-
- f.clear();
- f.read( reinterpret_cast<char*>(data_mem), std::streamsize(N_use) );
-
- const bool load_okay = f.good();
-
- f.clear();
- f.seekg(pos1);
-
- if(load_okay == false) { return file_type_unknown; }
-
- bool has_binary = false;
- bool has_bracket = false;
- bool has_comma = false;
-
- for(uword i=0; i<N_use; ++i)
- {
- const unsigned char val = data_mem[i];
-
- if( (val <= 8) || (val >= 123) ) { has_binary = true; break; } // the range checking can be made more elaborate
-
- if( (val == '(') || (val == ')') ) { has_bracket = true; }
-
- if( (val == ',') ) { has_comma = true; }
- }
-
- if(has_binary) { return raw_binary; }
-
- if(has_comma && (has_bracket == false)) { return csv_ascii; }
-
- return raw_ascii;
- }
- //! Append a quasi-random string to the given filename.
- //! Avoiding use of rand() to preserve its state.
- inline
- arma_cold
- std::string
- diskio::gen_tmp_name(const std::string& x)
- {
- union { uword val; void* ptr; } u;
-
- u.val = uword(0);
- u.ptr = const_cast<std::string*>(&x);
-
- const u16 a = u16( (u.val >> 8) & 0xFFFF );
- const u16 b = u16( (std::clock()) & 0xFFFF );
-
- std::ostringstream ss;
-
- ss << x << ".tmp_";
-
- ss.setf(std::ios_base::hex, std::ios_base::basefield);
-
- ss.width(4);
- ss.fill('0');
- ss << a;
-
- ss.width(4);
- ss.fill('0');
- ss << b;
-
- return ss.str();
- }
- //! Safely rename a file.
- //! Before renaming, test if we can write to the final file.
- //! This should prevent:
- //! (i) overwriting files that are write protected,
- //! (ii) overwriting directories.
- inline
- arma_cold
- bool
- diskio::safe_rename(const std::string& old_name, const std::string& new_name)
- {
- const char* new_name_c_str = new_name.c_str();
-
- std::fstream f(new_name_c_str, std::fstream::out | std::fstream::app);
- f.put(' ');
-
- if(f.good()) { f.close(); } else { return false; }
-
- if(std::remove( new_name_c_str) != 0) { return false; }
- if(std::rename(old_name.c_str(), new_name_c_str) != 0) { return false; }
-
- return true;
- }
- template<typename eT>
- inline
- bool
- diskio::convert_token(eT& val, const std::string& token)
- {
- const size_t N = size_t(token.length());
-
- if(N == 0) { val = eT(0); return true; }
-
- const char* str = token.c_str();
-
- if( (N == 3) || (N == 4) )
- {
- const bool neg = (str[0] == '-');
- const bool pos = (str[0] == '+');
-
- const size_t offset = ( (neg || pos) && (N == 4) ) ? 1 : 0;
-
- const char sig_a = str[offset ];
- const char sig_b = str[offset+1];
- const char sig_c = str[offset+2];
-
- if( ((sig_a == 'i') || (sig_a == 'I')) && ((sig_b == 'n') || (sig_b == 'N')) && ((sig_c == 'f') || (sig_c == 'F')) )
- {
- val = neg ? cond_rel< is_signed<eT>::value >::make_neg(Datum<eT>::inf) : Datum<eT>::inf;
-
- return true;
- }
- else
- if( ((sig_a == 'n') || (sig_a == 'N')) && ((sig_b == 'a') || (sig_b == 'A')) && ((sig_c == 'n') || (sig_c == 'N')) )
- {
- val = Datum<eT>::nan;
-
- return true;
- }
- }
-
-
- char* endptr = NULL;
-
- if(is_real<eT>::value)
- {
- val = eT( std::strtod(str, &endptr) );
- }
- else
- {
- if(is_signed<eT>::value)
- {
- // signed integer
-
- #if defined(ARMA_USE_CXX11) || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L))
- {
- val = eT( std::strtoll(str, &endptr, 10) );
- }
- #else
- {
- val = eT( std::strtol(str, &endptr, 10) );
- }
- #endif
- }
- else
- {
- // unsigned integer
-
- if(str[0] == '-') { val = eT(0); return true; }
-
- #if defined(ARMA_USE_CXX11) || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L))
- {
- val = eT( std::strtoull(str, &endptr, 10) );
- }
- #else
- {
- val = eT( std::strtoul(str, &endptr, 10) );
- }
- #endif
- }
- }
-
- if(str == endptr) { return false; }
-
- return true;
- }
- template<typename T>
- inline
- bool
- diskio::convert_token(std::complex<T>& val, const std::string& token)
- {
- const size_t N = size_t(token.length());
- const size_t Nm1 = N-1;
-
- if(N == 0) { val = std::complex<T>(0); return true; }
-
- const char* str = token.c_str();
-
- // valid complex number formats:
- // (real,imag)
- // (real)
- // ()
-
- if( (token[0] != '(') || (token[Nm1] != ')') )
- {
- // no brackets, so treat the token as a non-complex number
-
- T val_real;
-
- const bool state = diskio::convert_token(val_real, token); // use the non-complex version of this function
-
- val = std::complex<T>(val_real);
-
- return state;
- }
-
- // does the token contain only the () brackets?
- if(N <= 2) { val = std::complex<T>(0); return true; }
-
- size_t comma_loc = 0;
- bool comma_found = false;
-
- for(size_t i=0; i<N; ++i) { if(str[i] == ',') { comma_loc = i; comma_found = true; break; } }
-
- bool state = false;
-
- if(comma_found == false)
- {
- // only the real part is available
-
- const std::string token_real( &(str[1]), (Nm1 - 1) );
-
- T val_real;
-
- state = diskio::convert_token(val_real, token_real); // use the non-complex version of this function
-
- val = std::complex<T>(val_real);
- }
- else
- {
- const std::string token_real( &(str[1]), (comma_loc - 1 ) );
- const std::string token_imag( &(str[comma_loc+1]), (Nm1 - 1 - comma_loc) );
-
- T val_real;
- T val_imag;
-
- const bool state_real = diskio::convert_token(val_real, token_real);
- const bool state_imag = diskio::convert_token(val_imag, token_imag);
-
- state = ((state_real == true) && (state_imag == true));
-
- val = std::complex<T>(val_real, val_imag);
- }
-
- return state;
- }
- template<typename eT>
- inline
- std::streamsize
- diskio::prepare_stream(std::ostream& f)
- {
- std::streamsize cell_width = f.width();
-
- if(is_real<eT>::value)
- {
- f.unsetf(ios::fixed);
- f.setf(ios::scientific);
- f.fill(' ');
-
- f.precision(16);
- cell_width = 24;
-
- // NOTE: for 'float' the optimum settings are f.precision(8) and cell_width = 15
- // NOTE: however, to avoid introducing errors in case single precision data is loaded as double precision,
- // NOTE: the same settings must be used for both 'float' and 'double'
- }
- else
- if(is_cx<eT>::value)
- {
- f.unsetf(ios::fixed);
- f.setf(ios::scientific);
-
- f.precision(16);
- }
-
- return cell_width;
- }
-
- //! Save a matrix as raw text (no header, human readable).
- //! Matrices can be loaded in Matlab and Octave, as long as they don't have complex elements.
- template<typename eT>
- inline
- bool
- diskio::save_raw_ascii(const Mat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::fstream f(tmp_name.c_str(), std::fstream::out);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_raw_ascii(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a matrix as raw text (no header, human readable).
- //! Matrices can be loaded in Matlab and Octave, as long as they don't have complex elements.
- template<typename eT>
- inline
- bool
- diskio::save_raw_ascii(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- const std::streamsize cell_width = diskio::prepare_stream<eT>(f);
-
- for(uword row=0; row < x.n_rows; ++row)
- {
- for(uword col=0; col < x.n_cols; ++col)
- {
- f.put(' ');
-
- if(is_real<eT>::value) { f.width(cell_width); }
-
- arma_ostream::raw_print_elem(f, x.at(row,col));
- }
-
- f.put('\n');
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix as raw binary (no header)
- template<typename eT>
- inline
- bool
- diskio::save_raw_binary(const Mat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str(), std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_raw_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::save_raw_binary(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f.write( reinterpret_cast<const char*>(x.mem), std::streamsize(x.n_elem*sizeof(eT)) );
-
- return f.good();
- }
- //! Save a matrix in text format (human readable),
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_ascii(const Mat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str());
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_ascii(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a matrix in text format (human readable),
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_ascii(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- f << diskio::gen_txt_header(x) << '\n';
- f << x.n_rows << ' ' << x.n_cols << '\n';
-
- const std::streamsize cell_width = diskio::prepare_stream<eT>(f);
-
- for(uword row=0; row < x.n_rows; ++row)
- {
- for(uword col=0; col < x.n_cols; ++col)
- {
- f.put(' ');
-
- if(is_real<eT>::value) { f.width(cell_width); }
-
- arma_ostream::raw_print_elem(f, x.at(row,col));
- }
-
- f.put('\n');
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix in CSV text format (human readable)
- template<typename eT>
- inline
- bool
- diskio::save_csv_ascii(const Mat<eT>& x, const std::string& final_name, const field<std::string>& header, const bool with_header)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str());
-
- bool save_okay = f.is_open();
-
- if(save_okay == false) { return false; }
-
- if(with_header)
- {
- arma_extra_debug_print("diskio::save_csv_ascii(): writing header");
-
- for(uword i=0; i < header.n_elem; ++i)
- {
- f << header.at(i);
-
- if(i != (header.n_elem-1)) { f.put(','); }
- }
-
- f.put('\n');
-
- save_okay = f.good();
- }
-
- if(save_okay) { save_okay = diskio::save_csv_ascii(x, f); }
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
-
- return save_okay;
- }
- //! Save a matrix in CSV text format (human readable)
- template<typename eT>
- inline
- bool
- diskio::save_csv_ascii(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- diskio::prepare_stream<eT>(f);
-
- uword x_n_rows = x.n_rows;
- uword x_n_cols = x.n_cols;
-
- for(uword row=0; row < x_n_rows; ++row)
- {
- for(uword col=0; col < x_n_cols; ++col)
- {
- arma_ostream::raw_print_elem(f, x.at(row,col));
-
- if( col < (x_n_cols-1) ) { f.put(','); }
- }
-
- f.put('\n');
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix in CSV text format (human readable); complex numbers stored in "a+bi" format
- template<typename T>
- inline
- bool
- diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- typedef typename std::complex<T> eT;
-
- const arma_ostream_state stream_state(f);
-
- diskio::prepare_stream<eT>(f);
-
- uword x_n_rows = x.n_rows;
- uword x_n_cols = x.n_cols;
-
- for(uword row=0; row < x_n_rows; ++row)
- {
- for(uword col=0; col < x_n_cols; ++col)
- {
- const eT& val = x.at(row,col);
-
- const T tmp_r = std::real(val);
- const T tmp_i = std::imag(val);
- const T tmp_i_abs = (tmp_i < T(0)) ? T(-tmp_i) : T(tmp_i);
- const char tmp_sign = (tmp_i < T(0)) ? char('-') : char('+');
-
- arma_ostream::raw_print_elem(f, tmp_r );
- f.put(tmp_sign);
- arma_ostream::raw_print_elem(f, tmp_i_abs);
- f.put('i');
-
- if( col < (x_n_cols-1) ) { f.put(','); }
- }
-
- f.put('\n');
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix in binary format,
- //! with a header that stores the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const Mat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str(), std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a matrix in binary format,
- //! with a header that stores the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f << diskio::gen_bin_header(x) << '\n';
- f << x.n_rows << ' ' << x.n_cols << '\n';
-
- f.write( reinterpret_cast<const char*>(x.mem), std::streamsize(x.n_elem*sizeof(eT)) );
-
- return f.good();
- }
- //! Save a matrix as a PGM greyscale image
- template<typename eT>
- inline
- bool
- diskio::save_pgm_binary(const Mat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::fstream f(tmp_name.c_str(), std::fstream::out | std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_pgm_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //
- // TODO:
- // add functionality to save the image in a normalised format,
- // i.e. scaled so that every value falls in the [0,255] range.
- //! Save a matrix as a PGM greyscale image
- template<typename eT>
- inline
- bool
- diskio::save_pgm_binary(const Mat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f << "P5" << '\n';
- f << x.n_cols << ' ' << x.n_rows << '\n';
- f << 255 << '\n';
-
- const uword n_elem = x.n_rows * x.n_cols;
- podarray<u8> tmp(n_elem);
-
- uword i = 0;
-
- for(uword row=0; row < x.n_rows; ++row)
- for(uword col=0; col < x.n_cols; ++col)
- {
- tmp[i] = u8( x.at(row,col) ); // TODO: add round() ?
- ++i;
- }
-
- f.write(reinterpret_cast<const char*>(tmp.mem), std::streamsize(n_elem) );
-
- return f.good();
- }
- //! Save a matrix as a PGM greyscale image
- template<typename T>
- inline
- bool
- diskio::save_pgm_binary(const Mat< std::complex<T> >& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const uchar_mat tmp = conv_to<uchar_mat>::from(x);
-
- return diskio::save_pgm_binary(tmp, final_name);
- }
- //! Save a matrix as a PGM greyscale image
- template<typename T>
- inline
- bool
- diskio::save_pgm_binary(const Mat< std::complex<T> >& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const uchar_mat tmp = conv_to<uchar_mat>::from(x);
-
- return diskio::save_pgm_binary(tmp, f);
- }
- //! Save a matrix as part of a HDF5 file
- template<typename eT>
- inline
- bool
- diskio::save_hdf5_binary(const Mat<eT>& x, const hdf5_name& spec, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- {
- hdf5_misc::hdf5_suspend_printing_errors hdf5_print_suspender;
-
- bool save_okay = false;
-
- const bool append = bool(spec.opts.flags & hdf5_opts::flag_append);
- const bool replace = bool(spec.opts.flags & hdf5_opts::flag_replace);
-
- const bool use_existing_file = ((append || replace) && (arma_H5Fis_hdf5(spec.filename.c_str()) > 0));
-
- const std::string tmp_name = (use_existing_file) ? std::string() : diskio::gen_tmp_name(spec.filename);
-
- // Set up the file according to HDF5's preferences
- hid_t file = (use_existing_file) ? arma_H5Fopen(spec.filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT) : arma_H5Fcreate(tmp_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
-
- if(file < 0) { return false; }
-
- // We need to create a dataset, datatype, and dataspace
- hsize_t dims[2];
- dims[1] = x.n_rows;
- dims[0] = x.n_cols;
-
- hid_t dataspace = arma_H5Screate_simple(2, dims, NULL); // treat the matrix as a 2d array dataspace
- hid_t datatype = hdf5_misc::get_hdf5_type<eT>();
-
- // If this returned something invalid, well, it's time to crash.
- arma_check(datatype == -1, "Mat::save(): unknown datatype for HDF5");
-
- // MATLAB forces the users to specify a name at save time for HDF5;
- // Octave will use the default of 'dataset' unless otherwise specified.
- // If the user hasn't specified a dataset name, we will use 'dataset'
- // We may have to split out the group name from the dataset name.
- std::vector<hid_t> groups;
- std::string full_name = spec.dsname;
- size_t loc;
- while ((loc = full_name.find("/")) != std::string::npos)
- {
- // Create another group...
- if (loc != 0) // Ignore the first /, if there is a leading /.
- {
- hid_t gid = arma_H5Gcreate((groups.size() == 0) ? file : groups[groups.size() - 1], full_name.substr(0, loc).c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-
- if((gid < 0) && use_existing_file)
- {
- gid = arma_H5Gopen((groups.size() == 0) ? file : groups[groups.size() - 1], full_name.substr(0, loc).c_str(), H5P_DEFAULT);
- }
-
- groups.push_back(gid);
- }
-
- full_name = full_name.substr(loc + 1);
- }
-
- const std::string dataset_name = full_name.empty() ? std::string("dataset") : full_name;
-
- const hid_t last_group = (groups.size() == 0) ? file : groups[groups.size() - 1];
-
- if(use_existing_file && replace)
- {
- arma_H5Ldelete(last_group, dataset_name.c_str(), H5P_DEFAULT);
- // NOTE: H5Ldelete() in HDF5 v1.8 doesn't reclaim the deleted space; use h5repack to reclaim space: h5repack oldfile.h5 newfile.h5
- // NOTE: has this behaviour changed in HDF5 1.10 ?
- // NOTE: https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2017-August/010482.html
- // NOTE: https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2017-August/010486.html
- }
-
- hid_t dataset = arma_H5Dcreate(last_group, dataset_name.c_str(), datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-
- if(dataset < 0)
- {
- save_okay = false;
-
- err_msg = "couldn't create dataset in ";
- }
- else
- {
- save_okay = (arma_H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, x.mem) >= 0);
-
- arma_H5Dclose(dataset);
- }
-
- arma_H5Tclose(datatype);
- arma_H5Sclose(dataspace);
- for (size_t i = 0; i < groups.size(); ++i) { arma_H5Gclose(groups[i]); }
- arma_H5Fclose(file);
-
- if((use_existing_file == false) && (save_okay == true)) { save_okay = diskio::safe_rename(tmp_name, spec.filename); }
-
- return save_okay;
- }
- #else
- {
- arma_ignore(x);
- arma_ignore(spec);
- arma_ignore(err_msg);
-
- arma_stop_logic_error("Mat::save(): use of HDF5 must be enabled");
-
- return false;
- }
- #endif
- }
- //! Load a matrix as raw text (no header, human readable).
- //! Can read matrices saved as text in Matlab and Octave.
- //! NOTE: this is much slower than reading a file with a header.
- template<typename eT>
- inline
- bool
- diskio::load_raw_ascii(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_raw_ascii(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Load a matrix as raw text (no header, human readable).
- //! Can read matrices saved as text in Matlab and Octave.
- //! NOTE: this is much slower than reading a file with a header.
- template<typename eT>
- inline
- bool
- diskio::load_raw_ascii(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- //
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- bool f_n_cols_found = false;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- // TODO: does it make sense to stop processing the file if an empty line is found ?
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_n_cols = 0;
-
- while (line_stream >> token) { ++line_n_cols; }
-
- if(f_n_cols_found == false)
- {
- f_n_cols = line_n_cols;
- f_n_cols_found = true;
- }
- else
- {
- if(line_n_cols != f_n_cols)
- {
- load_okay = false;
- err_msg = "inconsistent number of columns in ";
- }
- }
-
- ++f_n_rows;
- }
-
-
- if(load_okay)
- {
- f.clear();
- f.seekg(pos1);
-
- x.set_size(f_n_rows, f_n_cols);
-
- for(uword row=0; ((row < x.n_rows) && load_okay); ++row)
- for(uword col=0; ((col < x.n_cols) && load_okay); ++col)
- {
- f >> token;
-
- if(diskio::convert_token(x.at(row,col), token) == false)
- {
- load_okay = false;
- err_msg = "couldn't interpret data in ";
- }
- }
- }
-
-
- // an empty file indicates an empty matrix
- if( (f_n_cols_found == false) && (load_okay == true) ) { x.reset(); }
-
-
- return load_okay;
- }
- //! Load a matrix in binary format (no header);
- //! the matrix is assumed to have one column
- template<typename eT>
- inline
- bool
- diskio::load_raw_binary(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f;
- f.open(name.c_str(), std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_raw_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_raw_binary(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
- arma_ignore(err_msg);
-
- f.clear();
- const std::streampos pos1 = f.tellg();
-
- f.clear();
- f.seekg(0, ios::end);
- f.clear();
- const std::streampos pos2 = f.tellg();
-
- const uword N = ( (pos1 >= 0) && (pos2 >= 0) ) ? uword(pos2 - pos1) : 0;
-
- f.clear();
- //f.seekg(0, ios::beg);
- f.seekg(pos1);
-
- x.set_size(N / uword(sizeof(eT)), 1);
-
- f.clear();
- f.read( reinterpret_cast<char *>(x.memptr()), std::streamsize(x.n_elem * uword(sizeof(eT))) );
-
- return f.good();
- }
- //! Load a matrix in text format (human readable),
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_ascii(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f(name.c_str());
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_ascii(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Load a matrix in text format (human readable),
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_ascii(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::streampos pos = f.tellg();
-
- bool load_okay = true;
-
- std::string f_header;
- uword f_n_rows;
- uword f_n_cols;
-
- f >> f_header;
- f >> f_n_rows;
- f >> f_n_cols;
-
- if(f_header == diskio::gen_txt_header(x))
- {
- x.zeros(f_n_rows, f_n_cols);
-
- std::string token;
-
- for(uword row=0; row < x.n_rows; ++row)
- for(uword col=0; col < x.n_cols; ++col)
- {
- f >> token;
-
- diskio::convert_token( x.at(row,col), token );
- }
-
- load_okay = f.good();
- }
- else
- {
- load_okay = false;
- err_msg = "incorrect header in ";
- }
-
-
- // allow automatic conversion of u32/s32 matrices into u64/s64 matrices
-
- if(load_okay == false)
- {
- if( (sizeof(eT) == 8) && is_same_type<uword,eT>::yes )
- {
- Mat<u32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_ascii(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Mat<eT> >::from(tmp); }
- }
- else
- if( (sizeof(eT) == 8) && is_same_type<sword,eT>::yes )
- {
- Mat<s32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_ascii(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Mat<eT> >::from(tmp); }
- }
- }
-
- return load_okay;
- }
- //! Load a matrix in CSV text format (human readable)
- template<typename eT>
- inline
- bool
- diskio::load_csv_ascii(Mat<eT>& x, const std::string& name, std::string& err_msg, field<std::string>& header, const bool with_header)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in);
-
- bool load_okay = f.is_open();
-
- if(load_okay == false) { return false; }
-
- if(with_header)
- {
- arma_extra_debug_print("diskio::load_csv_ascii(): reading header");
-
- std::string header_line;
- std::stringstream header_stream;
- std::vector<std::string> header_tokens;
-
- std::getline(f, header_line);
-
- load_okay = f.good();
-
- if(load_okay)
- {
- std::string token;
-
- header_stream.clear();
- header_stream.str(header_line);
-
- uword header_n_tokens = 0;
-
- while(header_stream.good())
- {
- std::getline(header_stream, token, ',');
- ++header_n_tokens;
- header_tokens.push_back(token);
- }
-
- if(header_n_tokens == uword(0))
- {
- header.reset();
- }
- else
- {
- header.set_size(1,header_n_tokens);
-
- for(uword i=0; i < header_n_tokens; ++i) { header.at(i) = header_tokens[i]; }
- }
- }
- }
-
- if(load_okay)
- {
- load_okay = diskio::load_csv_ascii(x, f, err_msg);
- }
-
- f.close();
-
- return load_okay;
- }
- //! Load a matrix in CSV text format (human readable)
- template<typename eT>
- inline
- bool
- diskio::load_csv_ascii(Mat<eT>& x, std::istream& f, std::string&)
- {
- arma_extra_debug_sigprint();
-
- // TODO: replace with more efficient implementation
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- //
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_n_cols = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
- ++line_n_cols;
- }
-
- if(f_n_cols < line_n_cols) { f_n_cols = line_n_cols; }
-
- ++f_n_rows;
- }
-
- f.clear();
- f.seekg(pos1);
-
- x.zeros(f_n_rows, f_n_cols);
-
- uword row = 0;
-
- while(f.good())
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword col = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
-
- diskio::convert_token( x.at(row,col), token );
-
- ++col;
- }
-
- ++row;
- }
-
- return load_okay;
- }
- //! Load a matrix in CSV text format (human readable); complex numbers stored in "a+bi" format
- template<typename T>
- inline
- bool
- diskio::load_csv_ascii(Mat< std::complex<T> >& x, std::istream& f, std::string&)
- {
- arma_extra_debug_sigprint();
-
- // TODO: replace with more efficient implementation
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- //
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_n_cols = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
- ++line_n_cols;
- }
-
- if(f_n_cols < line_n_cols) { f_n_cols = line_n_cols; }
-
- ++f_n_rows;
- }
-
- f.clear();
- f.seekg(pos1);
-
- x.zeros(f_n_rows, f_n_cols);
-
- uword row = 0;
-
- std::string str_real;
- std::string str_imag;
-
- while(f.good())
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword col = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
-
- const size_t token_len = size_t( token.length() );
-
- if(token_len == 0) { col++; continue; }
-
- // handle special cases: inf and nan, without the imaginary part
- if( (token_len == 3) || (token_len == 4) )
- {
- const char* str = token.c_str();
-
- const bool neg = (str[0] == '-');
- const bool pos = (str[0] == '+');
-
- const size_t offset = ( (neg || pos) && (token_len == 4) ) ? 1 : 0;
-
- const char sig_a = str[offset ];
- const char sig_b = str[offset+1];
- const char sig_c = str[offset+2];
-
- bool found_val_real = false;
- T val_real = T(0);
-
- if( ((sig_a == 'i') || (sig_a == 'I')) && ((sig_b == 'n') || (sig_b == 'N')) && ((sig_c == 'f') || (sig_c == 'F')) )
- {
- val_real = (neg) ? -(Datum<T>::inf) : Datum<T>::inf;
-
- found_val_real = true;
- }
- else
- if( ((sig_a == 'n') || (sig_a == 'N')) && ((sig_b == 'a') || (sig_b == 'A')) && ((sig_c == 'n') || (sig_c == 'N')) )
- {
- val_real = Datum<T>::nan;
-
- found_val_real = true;
- }
-
- if(found_val_real)
- {
- x.at(row,col) = std::complex<T>(val_real, T(0));
-
- col++; continue; // get next token
- }
- }
-
- bool found_x = false;
- std::string::size_type loc_x = 0; // location of the separator (+ or -) between the real and imaginary part
-
- std::string::size_type loc_i = token.find_last_of('i'); // location of the imaginary part indicator
-
- if(loc_i == std::string::npos)
- {
- str_real = token;
- str_imag.clear();
- }
- else
- {
- bool found_plus = false;
- bool found_minus = false;
-
- std::string::size_type loc_plus = token.find_last_of('+');
-
- if(loc_plus != std::string::npos)
- {
- if(loc_plus >= 1)
- {
- const char prev_char = token.at(loc_plus-1);
-
- // make sure we're not looking at the sign of the exponent
- if( (prev_char != 'e') && (prev_char != 'E') )
- {
- found_plus = true;
- }
- else
- {
- // search again, omitting the exponent
- loc_plus = token.find_last_of('+', loc_plus-1);
-
- if(loc_plus != std::string::npos) { found_plus = true; }
- }
- }
- else
- {
- // loc_plus == 0, meaning we're at the start of the string
- found_plus = true;
- }
- }
-
- std::string::size_type loc_minus = token.find_last_of('-');
-
- if(loc_minus != std::string::npos)
- {
- if(loc_minus >= 1)
- {
- const char prev_char = token.at(loc_minus-1);
-
- // make sure we're not looking at the sign of the exponent
- if( (prev_char != 'e') && (prev_char != 'E') )
- {
- found_minus = true;
- }
- else
- {
- // search again, omitting the exponent
- loc_minus = token.find_last_of('-', loc_minus-1);
-
- if(loc_minus != std::string::npos) { found_minus = true; }
- }
- }
- else
- {
- // loc_minus == 0, meaning we're at the start of the string
- found_minus = true;
- }
- }
-
- if(found_plus && found_minus)
- {
- if( (loc_i > loc_plus) && (loc_i > loc_minus) )
- {
- // choose the sign closest to the "i" to be the separator between the real and imaginary part
- loc_x = ( (loc_i - loc_plus) < (loc_i - loc_minus) ) ? loc_plus : loc_minus;
- found_x = true;
- }
- }
- else if(found_plus ) { loc_x = loc_plus; found_x = true; }
- else if(found_minus) { loc_x = loc_minus; found_x = true; }
-
- if(found_x)
- {
- if( loc_x > 0 ) { str_real = token.substr(0,loc_x); } else { str_real.clear(); }
- if((loc_x+1) < token.size()) { str_imag = token.substr(loc_x, token.size()-loc_x-1); } else { str_imag.clear(); }
- }
- else
- {
- str_real.clear();
- str_imag.clear();
- }
- }
-
- T val_real = T(0);
- T val_imag = T(0);
-
- diskio::convert_token(val_real, str_real);
- diskio::convert_token(val_imag, str_imag);
-
- x.at(row,col) = std::complex<T>(val_real, val_imag);
-
- ++col;
- }
-
- ++row;
- }
-
- return load_okay;
- }
- //! Load a matrix in binary format,
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f;
- f.open(name.c_str(), std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::streampos pos = f.tellg();
-
- bool load_okay = true;
-
- std::string f_header;
- uword f_n_rows;
- uword f_n_cols;
-
- f >> f_header;
- f >> f_n_rows;
- f >> f_n_cols;
-
- if(f_header == diskio::gen_bin_header(x))
- {
- //f.seekg(1, ios::cur); // NOTE: this may not be portable, as on a Windows machine a newline could be two characters
- f.get();
-
- x.set_size(f_n_rows,f_n_cols);
- f.read( reinterpret_cast<char *>(x.memptr()), std::streamsize(x.n_elem*sizeof(eT)) );
-
- load_okay = f.good();
- }
- else
- {
- load_okay = false;
- err_msg = "incorrect header in ";
- }
-
-
- // allow automatic conversion of u32/s32 matrices into u64/s64 matrices
-
- if(load_okay == false)
- {
- if( (sizeof(eT) == 8) && is_same_type<uword,eT>::yes )
- {
- Mat<u32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_binary(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Mat<eT> >::from(tmp); }
- }
- else
- if( (sizeof(eT) == 8) && is_same_type<sword,eT>::yes )
- {
- Mat<s32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_binary(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Mat<eT> >::from(tmp); }
- }
- }
-
- return load_okay;
- }
- inline
- void
- diskio::pnm_skip_comments(std::istream& f)
- {
- while( isspace(f.peek()) )
- {
- while( isspace(f.peek()) ) { f.get(); }
-
- if(f.peek() == '#')
- {
- while( (f.peek() != '\r') && (f.peek() != '\n') ) { f.get(); }
- }
- }
- }
- //! Load a PGM greyscale image as a matrix
- template<typename eT>
- inline
- bool
- diskio::load_pgm_binary(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_pgm_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Load a PGM greyscale image as a matrix
- template<typename eT>
- inline
- bool
- diskio::load_pgm_binary(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- bool load_okay = true;
-
- std::string f_header;
-
- f >> f_header;
-
- if(f_header == "P5")
- {
- uword f_n_rows = 0;
- uword f_n_cols = 0;
- int f_maxval = 0;
-
- diskio::pnm_skip_comments(f);
-
- f >> f_n_cols;
- diskio::pnm_skip_comments(f);
-
- f >> f_n_rows;
- diskio::pnm_skip_comments(f);
-
- f >> f_maxval;
- f.get();
-
- if( (f_maxval > 0) && (f_maxval <= 65535) )
- {
- x.set_size(f_n_rows,f_n_cols);
-
- if(f_maxval <= 255)
- {
- const uword n_elem = f_n_cols*f_n_rows;
- podarray<u8> tmp(n_elem);
-
- f.read( reinterpret_cast<char*>(tmp.memptr()), std::streamsize(n_elem) );
-
- uword i = 0;
-
- //cout << "f_n_cols = " << f_n_cols << endl;
- //cout << "f_n_rows = " << f_n_rows << endl;
-
- for(uword row=0; row < f_n_rows; ++row)
- for(uword col=0; col < f_n_cols; ++col)
- {
- x.at(row,col) = eT(tmp[i]);
- ++i;
- }
- }
- else
- {
- const uword n_elem = f_n_cols*f_n_rows;
- podarray<u16> tmp(n_elem);
-
- f.read( reinterpret_cast<char *>(tmp.memptr()), std::streamsize(n_elem*2) );
-
- uword i = 0;
-
- for(uword row=0; row < f_n_rows; ++row)
- for(uword col=0; col < f_n_cols; ++col)
- {
- x.at(row,col) = eT(tmp[i]);
- ++i;
- }
- }
- }
- else
- {
- load_okay = false;
- err_msg = "functionality unimplemented to handle loading ";
- }
-
- if(f.good() == false) { load_okay = false; }
- }
- else
- {
- load_okay = false;
- err_msg = "unsupported header in ";
- }
-
- return load_okay;
- }
- //! Load a PGM greyscale image as a matrix
- template<typename T>
- inline
- bool
- diskio::load_pgm_binary(Mat< std::complex<T> >& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- uchar_mat tmp;
- const bool load_okay = diskio::load_pgm_binary(tmp, name, err_msg);
-
- x = conv_to< Mat< std::complex<T> > >::from(tmp);
-
- return load_okay;
- }
- //! Load a PGM greyscale image as a matrix
- template<typename T>
- inline
- bool
- diskio::load_pgm_binary(Mat< std::complex<T> >& x, std::istream& is, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- uchar_mat tmp;
- const bool load_okay = diskio::load_pgm_binary(tmp, is, err_msg);
-
- x = conv_to< Mat< std::complex<T> > >::from(tmp);
-
- return load_okay;
- }
- //! Load a HDF5 file as a matrix
- template<typename eT>
- inline
- bool
- diskio::load_hdf5_binary(Mat<eT>& x, const hdf5_name& spec, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- {
- hdf5_misc::hdf5_suspend_printing_errors hdf5_print_suspender;
-
- bool load_okay = false;
-
- hid_t fid = arma_H5Fopen(spec.filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
-
- if(fid >= 0)
- {
- // MATLAB HDF5 dataset names are user-specified;
- // Octave tends to store the datasets in a group, with the actual dataset being referred to as "value".
- // If the user hasn't specified a dataset, we will search for "dataset" and "value",
- // and if those are not found we will take the first dataset we do find.
-
- std::vector<std::string> searchNames;
-
- const bool exact = (spec.dsname.empty() == false);
-
- if(exact)
- {
- searchNames.push_back(spec.dsname);
- }
- else
- {
- searchNames.push_back("dataset");
- searchNames.push_back("value" );
- }
-
- hid_t dataset = hdf5_misc::search_hdf5_file(searchNames, fid, 2, exact);
-
- if(dataset >= 0)
- {
- hid_t filespace = arma_H5Dget_space(dataset);
-
- // This must be <= 2 due to our search rules.
- const int ndims = arma_H5Sget_simple_extent_ndims(filespace);
-
- hsize_t dims[2];
- const herr_t query_status = arma_H5Sget_simple_extent_dims(filespace, dims, NULL);
-
- // arma_check(query_status < 0, "Mat::load(): cannot get size of HDF5 dataset");
- if(query_status < 0)
- {
- err_msg = "cannot get size of HDF5 dataset in ";
-
- arma_H5Sclose(filespace);
- arma_H5Dclose(dataset);
- arma_H5Fclose(fid);
-
- return false;
- }
-
- if(ndims == 1) { dims[1] = 1; } // Vector case; fake second dimension (one column).
-
- x.set_size(dims[1], dims[0]);
-
- // Now we have to see what type is stored to figure out how to load it.
- hid_t datatype = arma_H5Dget_type(dataset);
- hid_t mat_type = hdf5_misc::get_hdf5_type<eT>();
-
- // If these are the same type, it is simple.
- if(arma_H5Tequal(datatype, mat_type) > 0)
- {
- // Load directly; H5S_ALL used so that we load the entire dataset.
- hid_t read_status = arma_H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, void_ptr(x.memptr()));
-
- if(read_status >= 0) { load_okay = true; }
- }
- else
- {
- // Load into another array and convert its type accordingly.
- hid_t read_status = hdf5_misc::load_and_convert_hdf5(x.memptr(), dataset, datatype, x.n_elem);
-
- if(read_status >= 0) { load_okay = true; }
- }
-
- // Now clean up.
- arma_H5Tclose(datatype);
- arma_H5Tclose(mat_type);
- arma_H5Sclose(filespace);
- }
-
- arma_H5Dclose(dataset);
-
- arma_H5Fclose(fid);
-
- if(load_okay == false)
- {
- err_msg = "unsupported or missing HDF5 data in ";
- }
- }
- else
- {
- err_msg = "cannot open file ";
- }
-
- return load_okay;
- }
- #else
- {
- arma_ignore(x);
- arma_ignore(spec);
- arma_ignore(err_msg);
-
- arma_stop_logic_error("Mat::load(): use of HDF5 must be enabled");
-
- return false;
- }
- #endif
- }
- //! Try to load a matrix by automatically determining its type
- template<typename eT>
- inline
- bool
- diskio::load_auto_detect(Mat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- // We're currently using the C bindings for the HDF5 library, which don't support C++ streams
- if( arma_H5Fis_hdf5(name.c_str()) ) { return load_hdf5_binary(x, name, err_msg); }
- #endif
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_auto_detect(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Try to load a matrix by automatically determining its type
- template<typename eT>
- inline
- bool
- diskio::load_auto_detect(Mat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- const char* ARMA_MAT_TXT_str = "ARMA_MAT_TXT";
- const char* ARMA_MAT_BIN_str = "ARMA_MAT_BIN";
- const char* P5_str = "P5";
-
- const uword ARMA_MAT_TXT_len = uword(12);
- const uword ARMA_MAT_BIN_len = uword(12);
- const uword P5_len = uword(2);
-
- podarray<char> header(ARMA_MAT_TXT_len + 1);
-
- char* header_mem = header.memptr();
-
- std::streampos pos = f.tellg();
-
- f.read( header_mem, std::streamsize(ARMA_MAT_TXT_len) );
- f.clear();
- f.seekg(pos);
-
- header_mem[ARMA_MAT_TXT_len] = '\0';
-
- if( std::strncmp(ARMA_MAT_TXT_str, header_mem, size_t(ARMA_MAT_TXT_len)) == 0 )
- {
- return load_arma_ascii(x, f, err_msg);
- }
- else
- if( std::strncmp(ARMA_MAT_BIN_str, header_mem, size_t(ARMA_MAT_BIN_len)) == 0 )
- {
- return load_arma_binary(x, f, err_msg);
- }
- else
- if( std::strncmp(P5_str, header_mem, size_t(P5_len)) == 0 )
- {
- return load_pgm_binary(x, f, err_msg);
- }
- else
- {
- const file_type ft = guess_file_type_internal(f);
-
- switch(ft)
- {
- case csv_ascii:
- return load_csv_ascii(x, f, err_msg);
- break;
-
- case raw_binary:
- return load_raw_binary(x, f, err_msg);
- break;
-
- case raw_ascii:
- return load_raw_ascii(x, f, err_msg);
- break;
-
- default:
- err_msg = "unknown data in ";
- return false;
- }
- }
-
- return false;
- }
- //
- // sparse matrices
- //
- //! Save a sparse matrix in CSV format
- template<typename eT>
- inline
- bool
- diskio::save_csv_ascii(const SpMat<eT>& x, const std::string& final_name, const field<std::string>& header, const bool with_header)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str());
-
- bool save_okay = f.is_open();
-
- if(save_okay == false) { return false; }
-
- if(with_header)
- {
- arma_extra_debug_print("diskio::save_csv_ascii(): writing header");
-
- for(uword i=0; i < header.n_elem; ++i)
- {
- f << header(i);
-
- if(i != (header.n_elem-1)) { f.put(','); }
- }
-
- f.put('\n');
-
- save_okay = f.good();
- }
-
- if(save_okay) { save_okay = diskio::save_csv_ascii(x, f); }
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
-
- return save_okay;
- }
- //! Save a sparse matrix in CSV format
- template<typename eT>
- inline
- bool
- diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- diskio::prepare_stream<eT>(f);
-
- x.sync();
-
- uword x_n_rows = x.n_rows;
- uword x_n_cols = x.n_cols;
-
- for(uword row=0; row < x_n_rows; ++row)
- {
- for(uword col=0; col < x_n_cols; ++col)
- {
- const eT val = x.at(row,col);
-
- if(val != eT(0)) { arma_ostream::raw_print_elem(f, val); }
-
- if( col < (x_n_cols-1) ) { f.put(','); }
- }
-
- f.put('\n');
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a sparse matrix in CSV format (complex numbers)
- template<typename T>
- inline
- bool
- diskio::save_csv_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- arma_ignore(x);
- arma_ignore(f);
-
- arma_warn("saving complex sparse matrices as csv_ascii not yet implemented");
-
- return false;
- }
- //! Save a matrix in ASCII coord format
- template<typename eT>
- inline
- bool
- diskio::save_coord_ascii(const SpMat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str());
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_coord_ascii(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a matrix in ASCII coord format
- template<typename eT>
- inline
- bool
- diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- diskio::prepare_stream<eT>(f);
-
- typename SpMat<eT>::const_iterator iter = x.begin();
- typename SpMat<eT>::const_iterator iter_end = x.end();
-
- for(; iter != iter_end; ++iter)
- {
- const eT val = (*iter);
-
- f << iter.row() << ' ' << iter.col() << ' ' << val << '\n';
- }
-
-
- // make sure it's possible to figure out the matrix size later
- if( (x.n_rows > 0) && (x.n_cols > 0) )
- {
- const uword max_row = (x.n_rows > 0) ? x.n_rows-1 : 0;
- const uword max_col = (x.n_cols > 0) ? x.n_cols-1 : 0;
-
- if( x.at(max_row, max_col) == eT(0) )
- {
- f << max_row << ' ' << max_col << " 0\n";
- }
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix in ASCII coord format (complex numbers)
- template<typename T>
- inline
- bool
- diskio::save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- typedef typename std::complex<T> eT;
-
- const arma_ostream_state stream_state(f);
-
- diskio::prepare_stream<eT>(f);
-
- typename SpMat<eT>::const_iterator iter = x.begin();
- typename SpMat<eT>::const_iterator iter_end = x.end();
-
- for(; iter != iter_end; ++iter)
- {
- const eT val = (*iter);
-
- f << iter.row() << ' ' << iter.col() << ' ' << val.real() << ' ' << val.imag() << '\n';
- }
-
- // make sure it's possible to figure out the matrix size later
- if( (x.n_rows > 0) && (x.n_cols > 0) )
- {
- const uword max_row = (x.n_rows > 0) ? x.n_rows-1 : 0;
- const uword max_col = (x.n_cols > 0) ? x.n_cols-1 : 0;
-
- if( x.at(max_row, max_col) == eT(0) )
- {
- f << max_row << ' ' << max_col << " 0 0\n";
- }
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a matrix in binary format,
- //! with a header that stores the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const SpMat<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str(), std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a matrix in binary format,
- //! with a header that stores the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const SpMat<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f << diskio::gen_bin_header(x) << '\n';
- f << x.n_rows << ' ' << x.n_cols << ' ' << x.n_nonzero << '\n';
-
- f.write( reinterpret_cast<const char*>(x.values), std::streamsize(x.n_nonzero*sizeof(eT)) );
- f.write( reinterpret_cast<const char*>(x.row_indices), std::streamsize(x.n_nonzero*sizeof(uword)) );
- f.write( reinterpret_cast<const char*>(x.col_ptrs), std::streamsize((x.n_cols+1)*sizeof(uword)) );
-
- return f.good();
- }
- template<typename eT>
- inline
- bool
- diskio::load_csv_ascii(SpMat<eT>& x, const std::string& name, std::string& err_msg, field<std::string>& header, const bool with_header)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in);
-
- bool load_okay = f.is_open();
-
- if(load_okay == false) { return false; }
-
- if(with_header)
- {
- arma_extra_debug_print("diskio::load_csv_ascii(): reading header");
-
- std::string header_line;
- std::stringstream header_stream;
- std::vector<std::string> header_tokens;
-
- std::getline(f, header_line);
-
- load_okay = f.good();
-
- if(load_okay)
- {
- std::string token;
-
- header_stream.clear();
- header_stream.str(header_line);
-
- uword header_n_tokens = 0;
-
- while(header_stream.good())
- {
- std::getline(header_stream, token, ',');
- ++header_n_tokens;
- header_tokens.push_back(token);
- }
-
- if(header_n_tokens == uword(0))
- {
- header.reset();
- }
- else
- {
- header.set_size(1,header_n_tokens);
-
- for(uword i=0; i < header_n_tokens; ++i) { header.at(i) = header_tokens[i]; }
- }
- }
- }
-
- if(load_okay)
- {
- load_okay = diskio::load_csv_ascii(x, f, err_msg);
- }
-
- f.close();
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_csv_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
- arma_ignore(err_msg);
-
- // TODO: replace with more efficient implementation
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- //
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_n_cols = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
- ++line_n_cols;
- }
-
- if(f_n_cols < line_n_cols) { f_n_cols = line_n_cols; }
-
- ++f_n_rows;
- }
-
- f.clear();
- f.seekg(pos1);
-
- x.zeros(f_n_rows, f_n_cols);
-
- uword row = 0;
-
- while(f.good())
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword col = 0;
-
- while(line_stream.good())
- {
- std::getline(line_stream, token, ',');
-
- eT val = eT(0);
-
- diskio::convert_token( val, token );
-
- if(val != eT(0)) { x(row,col) = val; }
-
- ++col;
- }
-
- ++row;
- }
-
- return load_okay;
- }
- template<typename T>
- inline
- bool
- diskio::load_csv_ascii(SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- arma_ignore(x);
- arma_ignore(f);
- arma_ignore(err_msg);
-
- arma_warn("loading complex sparse matrices as csv_ascii not yet implemented");
-
- return false;
- }
- template<typename eT>
- inline
- bool
- diskio::load_coord_ascii(SpMat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_coord_ascii(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
- arma_ignore(err_msg);
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- bool size_found = false;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_row = 0;
- uword line_col = 0;
-
- // a valid line in co-ord format has at least 2 entries
-
- line_stream >> line_row;
-
- if(line_stream.good() == false) { load_okay = false; break; }
-
- line_stream >> line_col;
-
- size_found = true;
-
- if(f_n_rows < line_row) { f_n_rows = line_row; }
- if(f_n_cols < line_col) { f_n_cols = line_col; }
- }
-
-
- // take into account that indices start at 0
- if(size_found) { ++f_n_rows; ++f_n_cols; }
-
-
- if(load_okay)
- {
- f.clear();
- f.seekg(pos1);
-
- MapMat<eT> tmp(f_n_rows, f_n_cols);
-
- while(f.good())
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_row = 0;
- uword line_col = 0;
-
- line_stream >> line_row;
- line_stream >> line_col;
-
- eT val = eT(0);
-
- line_stream >> token;
-
- if(line_stream.fail() == false)
- {
- diskio::convert_token( val, token );
- }
-
- if(val != eT(0)) { tmp(line_row,line_col) = val; }
- }
-
- x = tmp;
- }
-
- return load_okay;
- }
- template<typename T>
- inline
- bool
- diskio::load_coord_ascii(SpMat< std::complex<T> >& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
- arma_ignore(err_msg);
-
- bool load_okay = f.good();
-
- f.clear();
- const std::fstream::pos_type pos1 = f.tellg();
-
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- bool size_found = false;
-
- std::string line_string;
- std::stringstream line_stream;
-
- std::string token_real;
- std::string token_imag;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_row = 0;
- uword line_col = 0;
-
- // a valid line in co-ord format has at least 2 entries
-
- line_stream >> line_row;
-
- if(line_stream.good() == false) { load_okay = false; break; }
-
- line_stream >> line_col;
-
- size_found = true;
-
- if(f_n_rows < line_row) f_n_rows = line_row;
- if(f_n_cols < line_col) f_n_cols = line_col;
- }
-
- // take into account that indices start at 0
- if(size_found) { ++f_n_rows; ++f_n_cols; }
-
- if(load_okay)
- {
- f.clear();
- f.seekg(pos1);
-
- MapMat< std::complex<T> > tmp(f_n_rows, f_n_cols);
-
- while(f.good())
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- line_stream.clear();
- line_stream.str(line_string);
-
- uword line_row = 0;
- uword line_col = 0;
-
- line_stream >> line_row;
- line_stream >> line_col;
-
- T val_real = T(0);
- T val_imag = T(0);
-
- line_stream >> token_real;
-
- if(line_stream.fail() == false)
- {
- diskio::convert_token( val_real, token_real );
- }
-
-
- line_stream >> token_imag;
-
- if(line_stream.fail() == false)
- {
- diskio::convert_token( val_imag, token_imag );
- }
-
- if( (val_real != T(0)) || (val_imag != T(0)) )
- {
- tmp(line_row,line_col) = std::complex<T>(val_real, val_imag);
- }
- }
-
- x = tmp;
- }
-
- return load_okay;
- }
- //! Load a matrix in binary format,
- //! with a header that indicates the matrix type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(SpMat<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f;
- f.open(name.c_str(), std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(SpMat<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- bool load_okay = true;
-
- std::string f_header;
-
- f >> f_header;
-
- if(f_header == diskio::gen_bin_header(x))
- {
- uword f_n_rows;
- uword f_n_cols;
- uword f_n_nz;
-
- f >> f_n_rows;
- f >> f_n_cols;
- f >> f_n_nz;
-
- //f.seekg(1, ios::cur); // NOTE: this may not be portable, as on a Windows machine a newline could be two characters
- f.get();
-
- x.reserve(f_n_rows, f_n_cols, f_n_nz);
-
- f.read( reinterpret_cast<char*>(access::rwp(x.values)), std::streamsize(x.n_nonzero*sizeof(eT)) );
-
- std::streampos pos = f.tellg();
-
- f.read( reinterpret_cast<char*>(access::rwp(x.row_indices)), std::streamsize(x.n_nonzero*sizeof(uword)) );
- f.read( reinterpret_cast<char*>(access::rwp(x.col_ptrs)), std::streamsize((x.n_cols+1)*sizeof(uword)) );
-
- bool check1 = true; for(uword i=0; i < x.n_nonzero; ++i) { if(x.values[i] == eT(0)) { check1 = false; break; } }
- bool check2 = true; for(uword i=0; i < x.n_cols; ++i) { if(x.col_ptrs[i+1] < x.col_ptrs[i]) { check2 = false; break; } }
- bool check3 = (x.col_ptrs[x.n_cols] == x.n_nonzero);
-
- if((check1 == true) && ((check2 == false) || (check3 == false)))
- {
- if(sizeof(uword) == 8)
- {
- arma_extra_debug_print("detected inconsistent data while loading; re-reading integer parts as u32");
-
- // inconstency could be due to a different uword size used during saving,
- // so try loading the row_indices and col_ptrs under the assumption of 32 bit unsigned integers
-
- f.clear();
- f.seekg(pos);
-
- podarray<u32> tmp_a(x.n_nonzero ); tmp_a.zeros();
- podarray<u32> tmp_b(x.n_cols + 1); tmp_b.zeros();
-
- f.read( reinterpret_cast<char*>(tmp_a.memptr()), std::streamsize( x.n_nonzero * sizeof(u32)) );
- f.read( reinterpret_cast<char*>(tmp_b.memptr()), std::streamsize((x.n_cols + 1) * sizeof(u32)) );
-
- check2 = true; for(uword i=0; i < x.n_cols; ++i) { if(tmp_b[i+1] < tmp_b[i]) { check2 = false; break; } }
- check3 = (tmp_b[x.n_cols] == x.n_nonzero);
-
- load_okay = f.good();
-
- if( load_okay && (check2 == true) && (check3 == true) )
- {
- arma_extra_debug_print("reading integer parts as u32 succeeded");
-
- arrayops::convert(access::rwp(x.row_indices), tmp_a.memptr(), x.n_nonzero );
- arrayops::convert(access::rwp(x.col_ptrs), tmp_b.memptr(), x.n_cols + 1);
- }
- else
- {
- arma_extra_debug_print("reading integer parts as u32 failed");
- }
- }
- }
-
- if((check1 == false) || (check2 == false) || (check3 == false))
- {
- load_okay = false;
- err_msg = "inconsistent data in ";
- }
- else
- {
- load_okay = f.good();
- }
- }
- else
- {
- load_okay = false;
- err_msg = "incorrect header in ";
- }
-
- return load_okay;
- }
- // cubes
- //! Save a cube as raw text (no header, human readable).
- template<typename eT>
- inline
- bool
- diskio::save_raw_ascii(const Cube<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::fstream f(tmp_name.c_str(), std::fstream::out);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = save_raw_ascii(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a cube as raw text (no header, human readable).
- template<typename eT>
- inline
- bool
- diskio::save_raw_ascii(const Cube<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- const std::streamsize cell_width = diskio::prepare_stream<eT>(f);
-
- for(uword slice=0; slice < x.n_slices; ++slice)
- {
- for(uword row=0; row < x.n_rows; ++row)
- {
- for(uword col=0; col < x.n_cols; ++col)
- {
- f.put(' ');
-
- if(is_real<eT>::value) { f.width(cell_width); }
-
- arma_ostream::raw_print_elem(f, x.at(row,col,slice));
- }
-
- f.put('\n');
- }
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a cube as raw binary (no header)
- template<typename eT>
- inline
- bool
- diskio::save_raw_binary(const Cube<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str(), std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_raw_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::save_raw_binary(const Cube<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f.write( reinterpret_cast<const char*>(x.mem), std::streamsize(x.n_elem*sizeof(eT)) );
-
- return f.good();
- }
- //! Save a cube in text format (human readable),
- //! with a header that indicates the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_ascii(const Cube<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str());
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_ascii(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a cube in text format (human readable),
- //! with a header that indicates the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_ascii(const Cube<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- const arma_ostream_state stream_state(f);
-
- f << diskio::gen_txt_header(x) << '\n';
- f << x.n_rows << ' ' << x.n_cols << ' ' << x.n_slices << '\n';
-
- const std::streamsize cell_width = diskio::prepare_stream<eT>(f);
-
- for(uword slice=0; slice < x.n_slices; ++slice)
- {
- for(uword row=0; row < x.n_rows; ++row)
- {
- for(uword col=0; col < x.n_cols; ++col)
- {
- f.put(' ');
-
- if(is_real<eT>::value) { f.width(cell_width); }
-
- arma_ostream::raw_print_elem(f, x.at(row,col,slice));
- }
-
- f.put('\n');
- }
- }
-
- const bool save_okay = f.good();
-
- stream_state.restore(f);
-
- return save_okay;
- }
- //! Save a cube in binary format,
- //! with a header that stores the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const Cube<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f(tmp_name.c_str(), std::fstream::binary);
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- //! Save a cube in binary format,
- //! with a header that stores the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::save_arma_binary(const Cube<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- f << diskio::gen_bin_header(x) << '\n';
- f << x.n_rows << ' ' << x.n_cols << ' ' << x.n_slices << '\n';
-
- f.write( reinterpret_cast<const char*>(x.mem), std::streamsize(x.n_elem*sizeof(eT)) );
-
- return f.good();
- }
- //! Save a cube as part of a HDF5 file
- template<typename eT>
- inline
- bool
- diskio::save_hdf5_binary(const Cube<eT>& x, const hdf5_name& spec, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- {
- hdf5_misc::hdf5_suspend_printing_errors hdf5_print_suspender;
-
- bool save_okay = false;
-
- const bool append = bool(spec.opts.flags & hdf5_opts::flag_append);
- const bool replace = bool(spec.opts.flags & hdf5_opts::flag_replace);
-
- const bool use_existing_file = ((append || replace) && (arma_H5Fis_hdf5(spec.filename.c_str()) > 0));
-
- const std::string tmp_name = (use_existing_file) ? std::string() : diskio::gen_tmp_name(spec.filename);
-
- // Set up the file according to HDF5's preferences
- hid_t file = (use_existing_file) ? arma_H5Fopen(spec.filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT) : arma_H5Fcreate(tmp_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
-
- if(file < 0) { return false; }
-
- // We need to create a dataset, datatype, and dataspace
- hsize_t dims[3];
- dims[2] = x.n_rows;
- dims[1] = x.n_cols;
- dims[0] = x.n_slices;
-
- hid_t dataspace = arma_H5Screate_simple(3, dims, NULL); // treat the cube as a 3d array dataspace
- hid_t datatype = hdf5_misc::get_hdf5_type<eT>();
-
- // If this returned something invalid, well, it's time to crash.
- arma_check(datatype == -1, "Cube::save(): unknown datatype for HDF5");
-
- // MATLAB forces the users to specify a name at save time for HDF5;
- // Octave will use the default of 'dataset' unless otherwise specified.
- // If the user hasn't specified a dataset name, we will use 'dataset'
- // We may have to split out the group name from the dataset name.
- std::vector<hid_t> groups;
- std::string full_name = spec.dsname;
- size_t loc;
- while ((loc = full_name.find("/")) != std::string::npos)
- {
- // Create another group...
- if (loc != 0) // Ignore the first /, if there is a leading /.
- {
- hid_t gid = arma_H5Gcreate((groups.size() == 0) ? file : groups[groups.size() - 1], full_name.substr(0, loc).c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-
- if((gid < 0) && use_existing_file)
- {
- gid = arma_H5Gopen((groups.size() == 0) ? file : groups[groups.size() - 1], full_name.substr(0, loc).c_str(), H5P_DEFAULT);
- }
-
- groups.push_back(gid);
- }
-
- full_name = full_name.substr(loc + 1);
- }
-
- const std::string dataset_name = full_name.empty() ? std::string("dataset") : full_name;
-
- const hid_t last_group = (groups.size() == 0) ? file : groups[groups.size() - 1];
-
- if(use_existing_file && replace)
- {
- arma_H5Ldelete(last_group, dataset_name.c_str(), H5P_DEFAULT);
- // NOTE: H5Ldelete() in HDF5 v1.8 doesn't reclaim the deleted space; use h5repack to reclaim space: h5repack oldfile.h5 newfile.h5
- // NOTE: has this behaviour changed in HDF5 1.10 ?
- // NOTE: https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2017-August/010482.html
- // NOTE: https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2017-August/010486.html
- }
-
- hid_t dataset = arma_H5Dcreate(last_group, dataset_name.c_str(), datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-
- if(dataset < 0)
- {
- save_okay = false;
-
- err_msg = "couldn't create dataset in ";
- }
- else
- {
- save_okay = (arma_H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, x.mem) >= 0);
-
- arma_H5Dclose(dataset);
- }
-
- arma_H5Tclose(datatype);
- arma_H5Sclose(dataspace);
- for (size_t i = 0; i < groups.size(); ++i) { arma_H5Gclose(groups[i]); }
- arma_H5Fclose(file);
-
- if((use_existing_file == false) && (save_okay == true)) { save_okay = diskio::safe_rename(tmp_name, spec.filename); }
-
- return save_okay;
- }
- #else
- {
- arma_ignore(x);
- arma_ignore(spec);
- arma_ignore(err_msg);
-
- arma_stop_logic_error("Cube::save(): use of HDF5 must be enabled");
-
- return false;
- }
- #endif
- }
- //! Load a cube as raw text (no header, human readable).
- //! NOTE: this is much slower than reading a file with a header.
- template<typename eT>
- inline
- bool
- diskio::load_raw_ascii(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- Mat<eT> tmp;
- const bool load_okay = diskio::load_raw_ascii(tmp, name, err_msg);
-
- if(load_okay)
- {
- if(tmp.is_empty() == false)
- {
- x.set_size(tmp.n_rows, tmp.n_cols, 1);
-
- x.slice(0) = tmp;
- }
- else
- {
- x.reset();
- }
- }
-
- return load_okay;
- }
- //! Load a cube as raw text (no header, human readable).
- //! NOTE: this is much slower than reading a file with a header.
- template<typename eT>
- inline
- bool
- diskio::load_raw_ascii(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- Mat<eT> tmp;
- const bool load_okay = diskio::load_raw_ascii(tmp, f, err_msg);
-
- if(load_okay)
- {
- if(tmp.is_empty() == false)
- {
- x.set_size(tmp.n_rows, tmp.n_cols, 1);
-
- x.slice(0) = tmp;
- }
- else
- {
- x.reset();
- }
- }
-
- return load_okay;
- }
- //! Load a cube in binary format (no header);
- //! the cube is assumed to have one slice with one column
- template<typename eT>
- inline
- bool
- diskio::load_raw_binary(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f;
- f.open(name.c_str(), std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_raw_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_raw_binary(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
- arma_ignore(err_msg);
-
- f.clear();
- const std::streampos pos1 = f.tellg();
-
- f.clear();
- f.seekg(0, ios::end);
-
- f.clear();
- const std::streampos pos2 = f.tellg();
-
- const uword N = ( (pos1 >= 0) && (pos2 >= 0) ) ? uword(pos2 - pos1) : 0;
-
- f.clear();
- //f.seekg(0, ios::beg);
- f.seekg(pos1);
-
- x.set_size(N / uword(sizeof(eT)), 1, 1);
-
- f.clear();
- f.read( reinterpret_cast<char *>(x.memptr()), std::streamsize(x.n_elem * uword(sizeof(eT))) );
-
- return f.good();
- }
- //! Load a cube in text format (human readable),
- //! with a header that indicates the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_ascii(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f(name.c_str());
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_ascii(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
-
- //! Load a cube in text format (human readable),
- //! with a header that indicates the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_ascii(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::streampos pos = f.tellg();
-
- bool load_okay = true;
-
- std::string f_header;
- uword f_n_rows;
- uword f_n_cols;
- uword f_n_slices;
-
- f >> f_header;
- f >> f_n_rows;
- f >> f_n_cols;
- f >> f_n_slices;
-
- if(f_header == diskio::gen_txt_header(x))
- {
- x.set_size(f_n_rows, f_n_cols, f_n_slices);
- for(uword slice = 0; slice < x.n_slices; ++slice)
- for(uword row = 0; row < x.n_rows; ++row )
- for(uword col = 0; col < x.n_cols; ++col )
- {
- f >> x.at(row,col,slice);
- }
-
- load_okay = f.good();
- }
- else
- {
- load_okay = false;
- err_msg = "incorrect header in ";
- }
-
-
- // allow automatic conversion of u32/s32 cubes into u64/s64 cubes
-
- if(load_okay == false)
- {
- if( (sizeof(eT) == 8) && is_same_type<uword,eT>::yes )
- {
- Cube<u32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_ascii(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Cube<eT> >::from(tmp); }
- }
- else
- if( (sizeof(eT) == 8) && is_same_type<sword,eT>::yes )
- {
- Cube<s32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_ascii(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Cube<eT> >::from(tmp); }
- }
- }
-
- return load_okay;
- }
- //! Load a cube in binary format,
- //! with a header that indicates the cube type as well as its dimensions
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f;
- f.open(name.c_str(), std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_arma_binary(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::streampos pos = f.tellg();
-
- bool load_okay = true;
-
- std::string f_header;
- uword f_n_rows;
- uword f_n_cols;
- uword f_n_slices;
-
- f >> f_header;
- f >> f_n_rows;
- f >> f_n_cols;
- f >> f_n_slices;
-
- if(f_header == diskio::gen_bin_header(x))
- {
- //f.seekg(1, ios::cur); // NOTE: this may not be portable, as on a Windows machine a newline could be two characters
- f.get();
-
- x.set_size(f_n_rows, f_n_cols, f_n_slices);
- f.read( reinterpret_cast<char *>(x.memptr()), std::streamsize(x.n_elem*sizeof(eT)) );
-
- load_okay = f.good();
- }
- else
- {
- load_okay = false;
- err_msg = "incorrect header in ";
- }
-
-
- // allow automatic conversion of u32/s32 cubes into u64/s64 cubes
-
- if(load_okay == false)
- {
- if( (sizeof(eT) == 8) && is_same_type<uword,eT>::yes )
- {
- Cube<u32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_binary(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Cube<eT> >::from(tmp); }
- }
- else
- if( (sizeof(eT) == 8) && is_same_type<sword,eT>::yes )
- {
- Cube<s32> tmp;
- std::string junk;
-
- f.clear();
- f.seekg(pos);
-
- load_okay = diskio::load_arma_binary(tmp, f, junk);
-
- if(load_okay) { x = conv_to< Cube<eT> >::from(tmp); }
- }
- }
-
- return load_okay;
- }
- //! Load a HDF5 file as a cube
- template<typename eT>
- inline
- bool
- diskio::load_hdf5_binary(Cube<eT>& x, const hdf5_name& spec, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- {
- hdf5_misc::hdf5_suspend_printing_errors hdf5_print_suspender;
-
- bool load_okay = false;
-
- hid_t fid = arma_H5Fopen(spec.filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
-
- if(fid >= 0)
- {
- // MATLAB HDF5 dataset names are user-specified;
- // Octave tends to store the datasets in a group, with the actual dataset being referred to as "value".
- // If the user hasn't specified a dataset, we will search for "dataset" and "value",
- // and if those are not found we will take the first dataset we do find.
-
- std::vector<std::string> searchNames;
-
- const bool exact = (spec.dsname.empty() == false);
-
- if(exact)
- {
- searchNames.push_back(spec.dsname);
- }
- else
- {
- searchNames.push_back("dataset");
- searchNames.push_back("value" );
- }
-
- hid_t dataset = hdf5_misc::search_hdf5_file(searchNames, fid, 3, exact);
-
- if(dataset >= 0)
- {
- hid_t filespace = arma_H5Dget_space(dataset);
-
- // This must be <= 3 due to our search rules.
- const int ndims = arma_H5Sget_simple_extent_ndims(filespace);
-
- hsize_t dims[3];
- const herr_t query_status = arma_H5Sget_simple_extent_dims(filespace, dims, NULL);
-
- // arma_check(query_status < 0, "Cube::load(): cannot get size of HDF5 dataset");
- if(query_status < 0)
- {
- err_msg = "cannot get size of HDF5 dataset in ";
-
- arma_H5Sclose(filespace);
- arma_H5Dclose(dataset);
- arma_H5Fclose(fid);
-
- return false;
- }
-
- if (ndims == 1) { dims[1] = 1; dims[2] = 1; } // Vector case; one row/colum, several slices
- if (ndims == 2) { dims[2] = 1; } // Matrix case; one column, several rows/slices
-
- x.set_size(dims[2], dims[1], dims[0]);
-
- // Now we have to see what type is stored to figure out how to load it.
- hid_t datatype = arma_H5Dget_type(dataset);
- hid_t mat_type = hdf5_misc::get_hdf5_type<eT>();
-
- // If these are the same type, it is simple.
- if(arma_H5Tequal(datatype, mat_type) > 0)
- {
- // Load directly; H5S_ALL used so that we load the entire dataset.
- hid_t read_status = arma_H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, void_ptr(x.memptr()));
-
- if(read_status >= 0) { load_okay = true; }
- }
- else
- {
- // Load into another array and convert its type accordingly.
- hid_t read_status = hdf5_misc::load_and_convert_hdf5(x.memptr(), dataset, datatype, x.n_elem);
-
- if(read_status >= 0) { load_okay = true; }
- }
-
- // Now clean up.
- arma_H5Tclose(datatype);
- arma_H5Tclose(mat_type);
- arma_H5Sclose(filespace);
- }
-
- arma_H5Dclose(dataset);
-
- arma_H5Fclose(fid);
-
- if(load_okay == false)
- {
- err_msg = "unsupported or missing HDF5 data in ";
- }
- }
- else
- {
- err_msg = "cannot open file ";
- }
-
- return load_okay;
- }
- #else
- {
- arma_ignore(x);
- arma_ignore(spec);
- arma_ignore(err_msg);
-
- arma_stop_logic_error("Cube::load(): use of HDF5 must be enabled");
-
- return false;
- }
- #endif
- }
- //! Try to load a cube by automatically determining its type
- template<typename eT>
- inline
- bool
- diskio::load_auto_detect(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_HDF5)
- // We're currently using the C bindings for the HDF5 library, which don't support C++ streams
- if( arma_H5Fis_hdf5(name.c_str()) ) { return load_hdf5_binary(x, name, err_msg); }
- #endif
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_auto_detect(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Try to load a cube by automatically determining its type
- template<typename eT>
- inline
- bool
- diskio::load_auto_detect(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- const char* ARMA_CUB_TXT_str = "ARMA_CUB_TXT";
- const char* ARMA_CUB_BIN_str = "ARMA_CUB_BIN";
- const char* P6_str = "P6";
-
- const uword ARMA_CUB_TXT_len = uword(12);
- const uword ARMA_CUB_BIN_len = uword(12);
- const uword P6_len = uword(2);
-
- podarray<char> header(ARMA_CUB_TXT_len + 1);
-
- char* header_mem = header.memptr();
-
- std::streampos pos = f.tellg();
-
- f.read( header_mem, std::streamsize(ARMA_CUB_TXT_len) );
- f.clear();
- f.seekg(pos);
-
- header_mem[ARMA_CUB_TXT_len] = '\0';
-
- if( std::strncmp(ARMA_CUB_TXT_str, header_mem, size_t(ARMA_CUB_TXT_len)) == 0 )
- {
- return load_arma_ascii(x, f, err_msg);
- }
- else
- if( std::strncmp(ARMA_CUB_BIN_str, header_mem, size_t(ARMA_CUB_BIN_len)) == 0 )
- {
- return load_arma_binary(x, f, err_msg);
- }
- else
- if( std::strncmp(P6_str, header_mem, size_t(P6_len)) == 0 )
- {
- return load_ppm_binary(x, f, err_msg);
- }
- else
- {
- const file_type ft = guess_file_type_internal(f);
-
- switch(ft)
- {
- // case csv_ascii:
- // return load_csv_ascii(x, f, err_msg);
- // break;
-
- case raw_binary:
- return load_raw_binary(x, f, err_msg);
- break;
-
- case raw_ascii:
- return load_raw_ascii(x, f, err_msg);
- break;
-
- default:
- err_msg = "unknown data in ";
- return false;
- }
- }
-
- return false;
- }
- // fields
- template<typename T1>
- inline
- bool
- diskio::save_arma_binary(const field<T1>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f( tmp_name.c_str(), std::fstream::binary );
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_arma_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::save_arma_binary(const field<T1>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- arma_type_check(( (is_Mat<T1>::value == false) && (is_Cube<T1>::value == false) ));
-
- if(x.n_slices <= 1)
- {
- f << "ARMA_FLD_BIN" << '\n';
- f << x.n_rows << '\n';
- f << x.n_cols << '\n';
- }
- else
- {
- f << "ARMA_FL3_BIN" << '\n';
- f << x.n_rows << '\n';
- f << x.n_cols << '\n';
- f << x.n_slices << '\n';
- }
-
- bool save_okay = true;
-
- for(uword i=0; i<x.n_elem; ++i)
- {
- save_okay = diskio::save_arma_binary(x[i], f);
-
- if(save_okay == false) { break; }
- }
-
- return save_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::load_arma_binary(field<T1>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f( name.c_str(), std::fstream::binary );
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_arma_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::load_arma_binary(field<T1>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- arma_type_check(( (is_Mat<T1>::value == false) && (is_Cube<T1>::value == false) ));
-
- bool load_okay = true;
-
- std::string f_type;
- f >> f_type;
-
- if(f_type == "ARMA_FLD_BIN")
- {
- uword f_n_rows;
- uword f_n_cols;
-
- f >> f_n_rows;
- f >> f_n_cols;
-
- x.set_size(f_n_rows, f_n_cols);
-
- f.get();
-
- for(uword i=0; i<x.n_elem; ++i)
- {
- load_okay = diskio::load_arma_binary(x[i], f, err_msg);
-
- if(load_okay == false) { break; }
- }
- }
- else
- if(f_type == "ARMA_FL3_BIN")
- {
- uword f_n_rows;
- uword f_n_cols;
- uword f_n_slices;
-
- f >> f_n_rows;
- f >> f_n_cols;
- f >> f_n_slices;
-
- x.set_size(f_n_rows, f_n_cols, f_n_slices);
-
- f.get();
-
- for(uword i=0; i<x.n_elem; ++i)
- {
- load_okay = diskio::load_arma_binary(x[i], f, err_msg);
-
- if(load_okay == false) { break; }
- }
- }
- else
- {
- load_okay = false;
- err_msg = "unsupported field type in ";
- }
-
- return load_okay;
- }
- inline
- bool
- diskio::save_std_string(const field<std::string>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f( tmp_name.c_str(), std::fstream::binary );
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_std_string(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- inline
- bool
- diskio::save_std_string(const field<std::string>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- for(uword row=0; row<x.n_rows; ++row)
- for(uword col=0; col<x.n_cols; ++col)
- {
- f << x.at(row,col);
-
- if(col < x.n_cols-1)
- {
- f << ' ';
- }
- else
- {
- f << '\n';
- }
- }
-
- return f.good();
- }
- inline
- bool
- diskio::load_std_string(field<std::string>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::ifstream f( name.c_str() );
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_std_string(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- inline
- bool
- diskio::load_std_string(field<std::string>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- bool load_okay = true;
-
- //
- // work out the size
-
- uword f_n_rows = 0;
- uword f_n_cols = 0;
-
- bool f_n_cols_found = false;
-
- std::string line_string;
- std::string token;
-
- while( f.good() && load_okay )
- {
- std::getline(f, line_string);
-
- if(line_string.size() == 0) { break; }
-
- std::stringstream line_stream(line_string);
-
- uword line_n_cols = 0;
-
- while (line_stream >> token) { line_n_cols++; }
-
- if(f_n_cols_found == false)
- {
- f_n_cols = line_n_cols;
- f_n_cols_found = true;
- }
- else
- {
- if(line_n_cols != f_n_cols)
- {
- load_okay = false;
- err_msg = "inconsistent number of columns in ";
- }
- }
-
- ++f_n_rows;
- }
-
- if(load_okay)
- {
- f.clear();
- f.seekg(0, ios::beg);
- //f.seekg(start);
-
- x.set_size(f_n_rows, f_n_cols);
-
- for(uword row=0; row < x.n_rows; ++row)
- for(uword col=0; col < x.n_cols; ++col)
- {
- f >> x.at(row,col);
- }
- }
-
- if(f.good() == false) { load_okay = false; }
-
- return load_okay;
- }
- //! Try to load a field by automatically determining its type
- template<typename T1>
- inline
- bool
- diskio::load_auto_detect(field<T1>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_auto_detect(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- //! Try to load a field by automatically determining its type
- template<typename T1>
- inline
- bool
- diskio::load_auto_detect(field<T1>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- arma_type_check(( is_Mat<T1>::value == false ));
-
- static const std::string ARMA_FLD_BIN = "ARMA_FLD_BIN";
- static const std::string ARMA_FL3_BIN = "ARMA_FL3_BIN";
- static const std::string P6 = "P6";
-
- podarray<char> raw_header(uword(ARMA_FLD_BIN.length()) + 1);
-
- std::streampos pos = f.tellg();
-
- f.read( raw_header.memptr(), std::streamsize(ARMA_FLD_BIN.length()) );
-
- f.clear();
- f.seekg(pos);
-
- raw_header[uword(ARMA_FLD_BIN.length())] = '\0';
-
- const std::string header = raw_header.mem;
-
- if(ARMA_FLD_BIN == header.substr(0, ARMA_FLD_BIN.length()))
- {
- return load_arma_binary(x, f, err_msg);
- }
- else
- if(ARMA_FL3_BIN == header.substr(0, ARMA_FL3_BIN.length()))
- {
- return load_arma_binary(x, f, err_msg);
- }
- else
- if(P6 == header.substr(0, P6.length()))
- {
- return load_ppm_binary(x, f, err_msg);
- }
- else
- {
- err_msg = "unsupported header in ";
- return false;
- }
- }
- //
- // handling of PPM images by cubes
- template<typename eT>
- inline
- bool
- diskio::load_ppm_binary(Cube<eT>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_ppm_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::load_ppm_binary(Cube<eT>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- bool load_okay = true;
-
- std::string f_header;
-
- f >> f_header;
-
- if(f_header == "P6")
- {
- uword f_n_rows = 0;
- uword f_n_cols = 0;
- int f_maxval = 0;
-
- diskio::pnm_skip_comments(f);
-
- f >> f_n_cols;
- diskio::pnm_skip_comments(f);
-
- f >> f_n_rows;
- diskio::pnm_skip_comments(f);
-
- f >> f_maxval;
- f.get();
-
- if( (f_maxval > 0) && (f_maxval <= 65535) )
- {
- x.set_size(f_n_rows, f_n_cols, 3);
-
- if(f_maxval <= 255)
- {
- const uword n_elem = 3*f_n_cols*f_n_rows;
- podarray<u8> tmp(n_elem);
-
- f.read( reinterpret_cast<char*>(tmp.memptr()), std::streamsize(n_elem) );
-
- uword i = 0;
-
- //cout << "f_n_cols = " << f_n_cols << endl;
- //cout << "f_n_rows = " << f_n_rows << endl;
-
- for(uword row=0; row < f_n_rows; ++row)
- for(uword col=0; col < f_n_cols; ++col)
- {
- x.at(row,col,0) = eT(tmp[i+0]);
- x.at(row,col,1) = eT(tmp[i+1]);
- x.at(row,col,2) = eT(tmp[i+2]);
- i+=3;
- }
- }
- else
- {
- const uword n_elem = 3*f_n_cols*f_n_rows;
- podarray<u16> tmp(n_elem);
-
- f.read( reinterpret_cast<char *>(tmp.memptr()), std::streamsize(2*n_elem) );
-
- uword i = 0;
-
- for(uword row=0; row < f_n_rows; ++row)
- for(uword col=0; col < f_n_cols; ++col)
- {
- x.at(row,col,0) = eT(tmp[i+0]);
- x.at(row,col,1) = eT(tmp[i+1]);
- x.at(row,col,2) = eT(tmp[i+2]);
- i+=3;
- }
- }
- }
- else
- {
- load_okay = false;
- err_msg = "currently no code available to handle loading ";
- }
-
- if(f.good() == false) { load_okay = false; }
- }
- else
- {
- load_okay = false;
- err_msg = "unsupported header in ";
- }
-
- return load_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::save_ppm_binary(const Cube<eT>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
-
- std::ofstream f( tmp_name.c_str(), std::fstream::binary );
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_ppm_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- template<typename eT>
- inline
- bool
- diskio::save_ppm_binary(const Cube<eT>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- arma_debug_check( (x.n_slices != 3), "diskio::save_ppm_binary(): given cube must have exactly 3 slices" );
-
- const uword n_elem = 3 * x.n_rows * x.n_cols;
- podarray<u8> tmp(n_elem);
-
- uword i = 0;
- for(uword row=0; row < x.n_rows; ++row)
- {
- for(uword col=0; col < x.n_cols; ++col)
- {
- tmp[i+0] = u8( access::tmp_real( x.at(row,col,0) ) );
- tmp[i+1] = u8( access::tmp_real( x.at(row,col,1) ) );
- tmp[i+2] = u8( access::tmp_real( x.at(row,col,2) ) );
-
- i+=3;
- }
- }
-
- f << "P6" << '\n';
- f << x.n_cols << '\n';
- f << x.n_rows << '\n';
- f << 255 << '\n';
-
- f.write( reinterpret_cast<const char*>(tmp.mem), std::streamsize(n_elem) );
-
- return f.good();
- }
- //
- // handling of PPM images by fields
- template<typename T1>
- inline
- bool
- diskio::load_ppm_binary(field<T1>& x, const std::string& name, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- std::fstream f;
- f.open(name.c_str(), std::fstream::in | std::fstream::binary);
-
- bool load_okay = f.is_open();
-
- if(load_okay)
- {
- load_okay = diskio::load_ppm_binary(x, f, err_msg);
- f.close();
- }
-
- return load_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::load_ppm_binary(field<T1>& x, std::istream& f, std::string& err_msg)
- {
- arma_extra_debug_sigprint();
-
- arma_type_check(( is_Mat<T1>::value == false ));
- typedef typename T1::elem_type eT;
-
- bool load_okay = true;
-
- std::string f_header;
-
- f >> f_header;
-
- if(f_header == "P6")
- {
- uword f_n_rows = 0;
- uword f_n_cols = 0;
- int f_maxval = 0;
-
- diskio::pnm_skip_comments(f);
-
- f >> f_n_cols;
- diskio::pnm_skip_comments(f);
-
- f >> f_n_rows;
- diskio::pnm_skip_comments(f);
-
- f >> f_maxval;
- f.get();
-
- if( (f_maxval > 0) && (f_maxval <= 65535) )
- {
- x.set_size(3);
- Mat<eT>& R = x(0);
- Mat<eT>& G = x(1);
- Mat<eT>& B = x(2);
-
- R.set_size(f_n_rows,f_n_cols);
- G.set_size(f_n_rows,f_n_cols);
- B.set_size(f_n_rows,f_n_cols);
-
- if(f_maxval <= 255)
- {
- const uword n_elem = 3*f_n_cols*f_n_rows;
- podarray<u8> tmp(n_elem);
-
- f.read( reinterpret_cast<char*>(tmp.memptr()), std::streamsize(n_elem) );
-
- uword i = 0;
-
- //cout << "f_n_cols = " << f_n_cols << endl;
- //cout << "f_n_rows = " << f_n_rows << endl;
-
-
- for(uword row=0; row < f_n_rows; ++row)
- {
- for(uword col=0; col < f_n_cols; ++col)
- {
- R.at(row,col) = eT(tmp[i+0]);
- G.at(row,col) = eT(tmp[i+1]);
- B.at(row,col) = eT(tmp[i+2]);
- i+=3;
- }
-
- }
- }
- else
- {
- const uword n_elem = 3*f_n_cols*f_n_rows;
- podarray<u16> tmp(n_elem);
-
- f.read( reinterpret_cast<char *>(tmp.memptr()), std::streamsize(2*n_elem) );
-
- uword i = 0;
-
- for(uword row=0; row < f_n_rows; ++row)
- for(uword col=0; col < f_n_cols; ++col)
- {
- R.at(row,col) = eT(tmp[i+0]);
- G.at(row,col) = eT(tmp[i+1]);
- B.at(row,col) = eT(tmp[i+2]);
- i+=3;
- }
- }
- }
- else
- {
- load_okay = false;
- err_msg = "currently no code available to handle loading ";
- }
-
- if(f.good() == false) { load_okay = false; }
- }
- else
- {
- load_okay = false;
- err_msg = "unsupported header in ";
- }
-
- return load_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::save_ppm_binary(const field<T1>& x, const std::string& final_name)
- {
- arma_extra_debug_sigprint();
-
- const std::string tmp_name = diskio::gen_tmp_name(final_name);
- std::ofstream f( tmp_name.c_str(), std::fstream::binary );
-
- bool save_okay = f.is_open();
-
- if(save_okay)
- {
- save_okay = diskio::save_ppm_binary(x, f);
-
- f.flush();
- f.close();
-
- if(save_okay) { save_okay = diskio::safe_rename(tmp_name, final_name); }
- }
-
- return save_okay;
- }
- template<typename T1>
- inline
- bool
- diskio::save_ppm_binary(const field<T1>& x, std::ostream& f)
- {
- arma_extra_debug_sigprint();
-
- arma_type_check(( is_Mat<T1>::value == false ));
-
- typedef typename T1::elem_type eT;
-
- arma_debug_check( (x.n_elem != 3), "diskio::save_ppm_binary(): given field must have exactly 3 matrices of equal size" );
-
- bool same_size = true;
- for(uword i=1; i<3; ++i)
- {
- if( (x(0).n_rows != x(i).n_rows) || (x(0).n_cols != x(i).n_cols) )
- {
- same_size = false;
- break;
- }
- }
-
- arma_debug_check( (same_size != true), "diskio::save_ppm_binary(): given field must have exactly 3 matrices of equal size" );
-
- const Mat<eT>& R = x(0);
- const Mat<eT>& G = x(1);
- const Mat<eT>& B = x(2);
-
- f << "P6" << '\n';
- f << R.n_cols << '\n';
- f << R.n_rows << '\n';
- f << 255 << '\n';
- const uword n_elem = 3 * R.n_rows * R.n_cols;
- podarray<u8> tmp(n_elem);
- uword i = 0;
- for(uword row=0; row < R.n_rows; ++row)
- for(uword col=0; col < R.n_cols; ++col)
- {
- tmp[i+0] = u8( access::tmp_real( R.at(row,col) ) );
- tmp[i+1] = u8( access::tmp_real( G.at(row,col) ) );
- tmp[i+2] = u8( access::tmp_real( B.at(row,col) ) );
-
- i+=3;
- }
-
- f.write( reinterpret_cast<const char*>(tmp.mem), std::streamsize(n_elem) );
-
- return f.good();
- }
- //! @}
|