most.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926
  1. /*
  2. MOST - Model Starter
  3. ---------------------------------------------------------------------------
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. ---------------------------------------------------------------------------
  16. */
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26. #include <X11/Xatom.h>
  27. #include <X11/keysym.h>
  28. #include <X11/XKBlib.h>
  29. #define INT int
  30. #define LINEMAX 256
  31. typedef char String[LINEMAX];
  32. String Buffer;
  33. #ifndef MIN
  34. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  35. #endif
  36. #ifndef MAX
  37. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  38. #endif
  39. #define FULLARC (360 * 64)
  40. #define WINDOW_WIDTH 820
  41. /* Models */
  42. #define PUMA 0
  43. #define SAM 1
  44. #define CAT 2
  45. #define PLASIM 3
  46. #define MODELS 4
  47. /* Resolutions */
  48. #define RES_T15 0
  49. #define RES_T21 1
  50. #define RES_T31 2
  51. #define RES_T42 3
  52. #define RES_T63 4
  53. #define RESOLUTIONS 5
  54. int PlasimSteps[RESOLUTIONS] =
  55. {
  56. 60, // T15
  57. 45, // T21
  58. 30, // T31
  59. 20, // T42
  60. 10 // T63
  61. };
  62. int PumaSteps[RESOLUTIONS] =
  63. {
  64. 60, // T15
  65. 60, // T21
  66. 45, // T31
  67. 30, // T42
  68. 15 // T63
  69. };
  70. int ResLat[RESOLUTIONS] =
  71. {
  72. 24,
  73. 32,
  74. 48,
  75. 64,
  76. 96
  77. };
  78. int Resolution = -1;
  79. char *ShortModelName[MODELS] =
  80. {
  81. "puma",
  82. "sam",
  83. "cat",
  84. "plasim"
  85. };
  86. char *FullModelName[MODELS] =
  87. {
  88. "PUMA",
  89. "SAM",
  90. "CAT",
  91. "Planet Simulator"
  92. };
  93. int Model = PUMA;
  94. #define ALL -1
  95. #define EARTH 0
  96. #define MARS 1
  97. #define EXO 2
  98. #define PLANETS 3
  99. char *PlanetName[PLANETS] =
  100. {
  101. "Earth",
  102. "Mars",
  103. "Exo"
  104. };
  105. int Planet = EARTH;
  106. double *OroEarth; // Earth Orography T42 (128 x 64)
  107. double *OroMars; // Mars Orography T42 (128 x 64)
  108. double *OroPrep; // Preprocessed Orography
  109. Pixmap OpmEarth;
  110. Pixmap OpmMars;
  111. Pixmap OpmPrep;
  112. double RevGra = 1.0 / 9.81;
  113. /* Object types */
  114. #define SEL_TEXT 1
  115. #define SEL_CHECK 2
  116. #define SEL_INT 3
  117. #define SEL_REAL 4
  118. #define SEL_TEVA 5
  119. #define SEL_PLANET 6
  120. struct ItemStruct
  121. {
  122. char list[80]; // Name of namelist
  123. char name[80]; // Name of variable
  124. char text[80]; // Text value
  125. float pvec[PLANETS]; // Planetary parameter
  126. double rval; // Real value
  127. int ival; // Integer value
  128. int model; // Model
  129. int flag; // Flag
  130. };
  131. struct SelStruct
  132. {
  133. struct SelStruct *Next; // Link to next Sel
  134. struct SelStruct *Prev; // Link to previous Sel
  135. struct ItemStruct *Item; // Link to item
  136. int type; // Type (TEXT,CHECK,INT,REAL)
  137. int x; // x coordinate of box
  138. int y; // y coordinate of box
  139. int h; // height of box
  140. int w; // width of box
  141. int xo; // x offset
  142. int xt; // x coordinate for text
  143. int yt; // y coordinate for text
  144. int lt; // length of text
  145. int iv; // integer value of box content
  146. int div; // default integer value
  147. int no; // 1: not selectable
  148. int teco; // text colour
  149. int edco; // edit column (cursor)
  150. int hide; // 1: don't show
  151. float fv; // floating value of box content
  152. float dfv; // default floating value
  153. float fpl[PLANETS]; // planet dependent floats
  154. int *piv; // pointer to linked integer variable
  155. float *pfv; // pointer to linked float variable
  156. char text[80]; // text to display
  157. char teva[16]; // box content
  158. };
  159. struct SelStruct SelStart;
  160. struct SelStruct SelModels[MODELS];
  161. struct SelStruct *CursorSel;
  162. struct SelStruct *ComEnd;
  163. struct SelStruct *SelAno;
  164. struct SelStruct *SelTgr;
  165. struct SelStruct *SelOro;
  166. struct SelStruct *SelMod;
  167. struct SelStruct *SelRes;
  168. struct SelStruct *SelOce;
  169. struct SelStruct *SelLsg;
  170. struct SelStruct *SelIce;
  171. struct SelStruct *SelAnn;
  172. struct SelStruct *SelPlanet[PLANETS];
  173. struct SelStruct *SelSYear;
  174. struct SelStruct *SelCPU;
  175. struct SelStruct *SelMulti;
  176. struct SelStruct *SelLat2;
  177. // Title lines for the entry of resolution
  178. char *DimText1;
  179. char *DimText2;
  180. char *DimText3;
  181. #define DIMLOGO 4
  182. struct SymbolStruct
  183. {
  184. int x; // x pos
  185. int y; // y pos
  186. int w; // width
  187. int h; // height
  188. int b; // background color
  189. int f; // foreground color
  190. char **i; // image
  191. char *d; // image data
  192. XImage *X; // X-Image
  193. char t[2][80]; // caption
  194. } Logo[DIMLOGO];
  195. int Logos;
  196. struct ColorStrip
  197. {
  198. double Lo;
  199. double Hi;
  200. char *Name;
  201. unsigned long pixel;
  202. };
  203. unsigned long WinBG = 0; // Black background
  204. unsigned long TextC = 0; // Text color
  205. unsigned long HeadC = 0; // Heading color
  206. #define DIMFRAME 3
  207. int Frames;
  208. struct FrameStruct
  209. {
  210. void (*Action)();
  211. Pixmap pixmap; // pixmap
  212. int x; // x pos
  213. int y; // y pos
  214. int w; // width
  215. int h; // height
  216. int b; // background color
  217. int f; // foreground color
  218. int xs; // x pos selection
  219. int ys; // y pos selection
  220. int ws; // width selection
  221. int hs; // height selection
  222. char t[3][80]; // text
  223. } Frame[DIMFRAME];
  224. int FrameNo;
  225. int Buttons;
  226. int Button1Down = 1;
  227. #define DIMBUTTON 5
  228. struct ButtonStruct
  229. {
  230. void (*Action)();
  231. int x; // x pos
  232. int y; // y pos
  233. int w; // width
  234. int h; // height
  235. int b; // background color
  236. int f; // foreground color
  237. char t[3][80]; // text
  238. } Button[DIMBUTTON];
  239. int Buttons;
  240. /* Coordinates */
  241. int opbox_x;
  242. int opbox_y;
  243. int opbox_w;
  244. int opbox_h;
  245. int nlbox_x;
  246. int nlbox_y;
  247. int nlbox_w;
  248. int nlbox_h;
  249. int nlpos_x;
  250. int nledi_x;
  251. int sfbox_x;
  252. int sfbox_y;
  253. int sfbox_w;
  254. int sfbox_h;
  255. int sfbox_b;
  256. int bubox_x;
  257. int bubox_y;
  258. int bubox_w;
  259. int bubox_h;
  260. int CursorCol;
  261. int LastSel;
  262. int EdiKeyCode;
  263. #define NL_MAX_ITEMS 100
  264. int NL_items = 0;
  265. struct ItemStruct NL_list[NL_MAX_ITEMS];
  266. int BigEndian;
  267. int Oce;
  268. int Ice;
  269. int Lsg;
  270. int SimStart;
  271. int SimYears;
  272. int nreadsr;
  273. int noutput;
  274. int ndebug;
  275. int nprec;
  276. int ngui;
  277. int noro = 1;
  278. int OroAno;
  279. int OroClear;
  280. int OroAqua;
  281. int TgrAno;
  282. int nac;
  283. int Preprocessed;
  284. int SAMindex;
  285. int ScreenHeight;
  286. int Expert = 0;
  287. int CatEnabled;
  288. int SamEnabled;
  289. int LsgEnabled;
  290. int ModeRadiusSq;
  291. int ForceRebuild;
  292. int dxsh;
  293. int dxs2;
  294. int Yoden; // PUMA setup for Yoden experiment
  295. int CatSim = 51; // simulation setup number for CAT
  296. /* Special parameter */
  297. int Latitudes = 32; // Number of latitudes in atmosphere
  298. int Latitude2 = 32; // Number of latitudes for 2nd. instance
  299. int Levels = 10; // Number of levels in atmosphere
  300. int Cores = 1; // Number of cores for parallel version
  301. int Multirun = 1; // Number of coupled runs
  302. int MultirunEnabled = 0; // Multirun module present?
  303. int Truncation = 21; // Spectral truncation computed from Latitudes
  304. unsigned Seed;
  305. char cfg_file[256] = "most_last_used.cfg";
  306. char exec_name[256] = "most_puma.x";
  307. char exec_nam2[256] = "most_puma.x"; // 2nd. instance
  308. char exec_ppp[256] = "most_ppp.x";
  309. char oro_name[256] = "N064_surf_0129.sra"; // Orography in T42 resolution
  310. char namelist_name[256] = "puma_namelist";
  311. char diag_name[256] = "puma_diag";
  312. char outp_name[256] = "puma_output";
  313. char build_name[256] = "most_puma_build";
  314. char build_ppp[256] = "most_ppp_build";
  315. char run_name[256] = "most_puma_run";
  316. char run_ppp[256] = "most_ppp_run";
  317. char res_name[256] = "resmod.f90";
  318. char hostname[256] = "localhost";
  319. char mpirun[256] = "mpirun"; // "openmpirun" for Open MPI
  320. int ScreenN;
  321. time_t CurrentDate;
  322. Display *display;
  323. unsigned int ScreenW,ScreenH;
  324. int SmallScreen = 1;
  325. static char *progname = "Most";
  326. int DimX;
  327. int DimY;
  328. int DimTr = 21;
  329. int DimSH = 11 * 23;
  330. int DimSE = 11 * 23 + 2;
  331. int ScreenD;
  332. int OffX;
  333. int OffY = 0;
  334. int WinXSize;
  335. int WinYSize;
  336. int WhitePix;
  337. int BlackPix;
  338. int *Ampli;
  339. int *ModeX;
  340. int *ModeY;
  341. int *ModeM;
  342. int *ModeN;
  343. Colormap colormap;
  344. XColor xcolor1,xcolor2;
  345. XColor Red,Green,Blue,Grey,LightRed,DarkRed,LightBlue,DarkBlue;
  346. XColor LightGreen,DarkGreen,Yellow,Cyan,Dummy;
  347. Window Cow; // Control bar
  348. XTextProperty WinconName1;
  349. XEvent WinEvent;
  350. XSizeHints CowSizeHints;
  351. int count;
  352. XEvent report;
  353. GC gc;
  354. char ModFontName[80] = "-misc-fixed-bold-r-normal--15-*-*-*-*-*-*-*";
  355. char FixFontName[80] = "-misc-fixed-bold-r-normal--15-*-*-*-*-*-*-*";
  356. char BigFontName[80] = "-misc-fixed-medium-r-normal--20-*-*-*-*-*-*-*";
  357. //char ModFontName[80] = "9x15bold";
  358. //char FixFontName[80] = "9x15bold";
  359. //char BigFontName[80] = "10x20";
  360. XFontStruct *ModFont;
  361. XFontStruct *FixFont;
  362. XFontStruct *BigFont;
  363. XFontStruct *SamFont;
  364. int ModFontHeight;
  365. int FixFontHeight;
  366. int BigFontHeight;
  367. int ModFontWidth;
  368. int FixFontWidth;
  369. int BigFontWidth;
  370. int ModFontAscent;
  371. int FixFontAscent;
  372. int BigFontAscent;
  373. int EdiFirstKey;
  374. int EdiLastKey;
  375. int EdiSymsPerKey;
  376. int NumLockMask;
  377. int ModeSwitchMask;
  378. int Debug = 0;
  379. KeySym *EdiKeymap;
  380. XModifierKeymap *Mok;
  381. char *display_name = NULL;
  382. XWMHints wm_hints;
  383. XClassHint class_hints;
  384. Atom Delwin;
  385. char *mona[12] =
  386. {
  387. "Jan","Feb","Mar",
  388. "Apr","May","Jun",
  389. "Jul","Aug","Sep",
  390. "Oct","Nov","Dec"
  391. };
  392. char datch[32];
  393. struct MapImageStruct
  394. {
  395. char *d; // Bitmap data
  396. int w; // Image width
  397. int h; // Image height
  398. int f; // Rotation factor
  399. float l; // Reference longitude
  400. float r; // Rotation speed [deg/step]
  401. XImage *X; // XImage structure
  402. };
  403. struct MapImageStruct MapHRE; // Hires (2560x1280) Earth image
  404. struct MapImageStruct MapHRM; // Hires ( ) Mars image
  405. struct MapImageStruct MapLRE; // Lores azimuthal Earth image
  406. struct MapImageStruct MapLRM; // Lores azimuthal Mars image
  407. struct MapImageStruct MapLRK; // Lores azimuthal Kepler-16
  408. struct MapImageStruct MapLRL; // Lores azimuthal Kepler-186b
  409. /* Isoarea data */
  410. #define TOLELO 0x0001
  411. #define TOLEHI 0x0002
  412. #define TORILO 0x0004
  413. #define TORIHI 0x0008
  414. #define BOLELO 0x0010
  415. #define BOLEHI 0x0020
  416. #define BORILO 0x0040
  417. #define BORIHI 0x0080
  418. #define TOLEIN 0x0100
  419. #define TOREIN 0x0200
  420. #define BOLEIN 0x0400
  421. #define BOREIN 0x0800
  422. #define IPX(l,m,h) (VGAX * (x + (l-m) / (l-h)))
  423. #define IPY(l,m,h) (VGAY * (y + (l-m) / (l-h)))
  424. int nlon = 128;
  425. int nlat = 64;
  426. int nlev = 5;
  427. int *Flag;
  428. double VGAX;
  429. double VGAY;
  430. Pixmap pix;
  431. /* Orography units are [m2/s2] */
  432. struct ColorStrip OroStrip[] =
  433. {
  434. {-99999.0, 80.0,"DarkBlue"},
  435. { 80.0, 800.0,"green"},
  436. { 800.0, 1600.0,"brown"},
  437. { 1600.0, 2400.0,"orange"},
  438. { 2400.0,99999.0,"white"},
  439. { 0.0, 0.0,NULL}
  440. };
  441. struct ColorStrip OroMarsStrip[] =
  442. {
  443. {-99999.0,-16000.0,"yellow"},
  444. {-16000.0,-12000.0,"RoyalBlue4"},
  445. {-12000.0, -8000.0,"RoyalBlue3"},
  446. { -8000.0, -4000.0,"RoyalBlue2"},
  447. { -4000.0, 0.0,"RoyalBlue1"},
  448. { 0.0, 4000.0,"dark green"},
  449. { 4000.0, 8000.0,"light green"},
  450. { 8000.0, 12000.0,"brown"},
  451. { 12000.0, 16000.0,"orange"},
  452. { 16000.0, 99999.0,"white"},
  453. { 0.0, 0.0,NULL}
  454. };
  455. struct ColorStrip GibbStrip[] =
  456. {
  457. {-99999.0, 0.0,"DarkBlue"},
  458. { 0.0,99999.0,"green"},
  459. { 0.0, 0.0,NULL}
  460. };
  461. struct ColorStrip TStrip[] =
  462. {
  463. {-99.0,-15.0,"RoyalBlue4"},
  464. {-15.0,-20.0,"RoyalBlue3"},
  465. {-10.0, -5.0,"RoyalBlue2"},
  466. { -5.0, -0.1,"RoyalBlue1"},
  467. { -0.1, 0.1,"yellow"},
  468. { 0.1, 5.0,"IndianRed1"},
  469. { 5.0, 10.0,"IndianRed2"},
  470. { 10.0, 15.0,"IndianRed3"},
  471. { 15.0, 20.0,"IndianRed4"},
  472. { 20.0,999.0,"red"},
  473. { 0.0, 0.0,NULL}
  474. };
  475. void DoNothing(void)
  476. {
  477. printf("Do nothing\n");
  478. }
  479. void ChangeModel(int NewMo)
  480. {
  481. int i;
  482. struct SelStruct *Sel;
  483. if (NewMo < 0) // locate active model
  484. {
  485. NewMo = PUMA; // default if none is specified
  486. for (i=0 , Sel = SelMod ; i < MODELS; ++i , Sel = Sel->Next)
  487. {
  488. if (Sel->iv == 1) NewMo = i;
  489. }
  490. }
  491. ComEnd->Next = &SelModels[NewMo];
  492. ComEnd->Next->Prev = ComEnd;
  493. for (i=0 , Sel = SelMod ; i < MODELS; ++i , Sel = Sel->Next)
  494. {
  495. if (i == NewMo) Sel->iv = 1;
  496. else Sel->iv = 0;
  497. }
  498. if (NewMo == PUMA || NewMo == SAM)
  499. {
  500. SelAno->hide = 0;
  501. SelAnn->no = 0;
  502. SelOce->no = 1;
  503. SelIce->no = 1;
  504. SelOro->no = 0;
  505. SelSYear->no = 1;
  506. if (SelLsg) SelLsg->no = 1;
  507. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 1;
  508. }
  509. if (NewMo == PLASIM)
  510. {
  511. SelAno->hide = 1;
  512. SelAnn->no = 1;
  513. SelOro->no = 1;
  514. SelSYear->no = 0;
  515. if (Expert)
  516. {
  517. SelOce->no = 0;
  518. SelIce->no = 0;
  519. if (SelLsg) SelLsg->no = 0;
  520. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 0;
  521. }
  522. else
  523. {
  524. SelOce->no = 0;
  525. SelIce->no = 0;
  526. for (i=0 ; i < PLANETS ; ++i) SelPlanet[i]->no = 0;
  527. }
  528. }
  529. if (Expert)
  530. {
  531. if (NewMo == CAT)
  532. {
  533. strcpy(DimText1,"NGX ");
  534. strcpy(DimText2,"NGY ");
  535. }
  536. else
  537. {
  538. strcpy(DimText1,"Latitudes #1 ");
  539. strcpy(DimText2,"Latitudes #2 ");
  540. }
  541. }
  542. else
  543. {
  544. if (NewMo == CAT)
  545. {
  546. strcpy(DimText1,"[ 64 x 64 ]");
  547. strcpy(DimText2,"[ 256 x 256 ]");
  548. strcpy(DimText3,"[1024 x 1024]");
  549. }
  550. else
  551. {
  552. strcpy(DimText1,"T21 [64x32]");
  553. strcpy(DimText2,"T31 [96x48]");
  554. strcpy(DimText3,"T42 [128x64]");
  555. }
  556. }
  557. Model = NewMo;
  558. }
  559. void WriteSettings(void)
  560. {
  561. int mxtl;
  562. char tb[80];
  563. FILE *fp;
  564. struct SelStruct *Sel;
  565. fp = fopen("most_last_used.cfg","w");
  566. if (!fp) return;
  567. fprintf(fp,"[MoSt 17 - configuration file] created %s\n",ctime(&CurrentDate));
  568. mxtl = 0;
  569. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  570. {
  571. if (Sel->type != SEL_TEXT ) mxtl = MAX(mxtl,strlen(Sel->text));
  572. }
  573. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  574. {
  575. if (Sel->type == SEL_TEXT ) fprintf(fp,"\n[%s]\n",Sel->text);
  576. else
  577. {
  578. memset(tb,' ',sizeof(tb));
  579. strncpy(tb,Sel->text,strlen(Sel->text));
  580. tb[mxtl] = 0;
  581. fprintf(fp,"%s = ",tb);
  582. }
  583. if (Sel->type == SEL_CHECK) fprintf(fp,"%6d\n",Sel->iv);
  584. else if (Sel->type == SEL_INT ) fprintf(fp,"%6d\n",Sel->iv);
  585. else if (Sel->type == SEL_TEVA ) fprintf(fp,"%s\n",Sel->teva);
  586. else if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  587. {
  588. if (Sel->type == SEL_PLANET) Sel->fv = Sel->fpl[Planet];
  589. if (fabs(Sel->fv) >= 1.0 && fabs(Sel->fv) < 9000.0) fprintf(fp,"%10.4f\n",Sel->fv);
  590. else fprintf(fp,"%10.4e\n",Sel->fv);
  591. }
  592. }
  593. fclose(fp);
  594. }
  595. void FormatReal(float fv, char *text)
  596. {
  597. if (fv == 0.0 ||
  598. (fv > 0.001 && fv < 99999.0) ||
  599. (fv > -9999.0 && fv < -0.001))
  600. {
  601. sprintf(text,"%11.4f",fv);
  602. if (!strcmp(text+8,"000")) text[ 8] = 0;
  603. else if (!strcmp(text+9,"00" )) text[ 9] = 0;
  604. else if (text[10] == '0') text[10] = 0;
  605. }
  606. else
  607. {
  608. sprintf(text,"%11.4e",fv);
  609. }
  610. }
  611. void UpdateSelections(struct SelStruct *Sel)
  612. {
  613. char text[80];
  614. int selx;
  615. selx = 0;
  616. for (; Sel ; Sel = Sel->Next)
  617. {
  618. if (Sel == ComEnd->Next) selx = 1;
  619. if (selx) Sel->x = nledi_x;
  620. if (Sel->type == SEL_INT )
  621. {
  622. sprintf(text,"%10d",Sel->iv);
  623. strcpy(Sel->teva,text+10-Sel->edco);
  624. if (Sel->piv) *Sel->piv = Sel->iv;
  625. }
  626. if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  627. {
  628. if (Sel->type == SEL_PLANET) Sel->fv = Sel->fpl[Planet];
  629. FormatReal(Sel->fv,text);
  630. strcpy(Sel->teva,text);
  631. }
  632. }
  633. // if (SelLat2) SelLat2->no = (SelMulti->iv != 2) ;
  634. }
  635. void ChangePlanet(int NewPlanet)
  636. {
  637. struct SelStruct *Sel;
  638. SelPlanet[Planet]->iv = 0;
  639. Planet = NewPlanet;
  640. SelPlanet[Planet]->iv = 1;
  641. strcpy(SelModels[PLASIM].text,PlanetName[Planet]);
  642. SelModels[PLASIM].lt = strlen(PlanetName[Planet]);
  643. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  644. {
  645. if (Sel->type == SEL_PLANET)
  646. {
  647. Sel->fv = Sel->fpl[Planet];
  648. }
  649. }
  650. UpdateSelections(ComEnd->Next);
  651. }
  652. int ReadSettings(char *fn)
  653. {
  654. int pl;
  655. char tb[80];
  656. char *eq;
  657. FILE *fp;
  658. struct SelStruct *Sel;
  659. fp = fopen(fn,"r");
  660. if (!fp)
  661. {
  662. printf("\nUsing default configuration\n");
  663. return 0;
  664. }
  665. fgets(tb,sizeof(tb),fp);
  666. if (strncmp(tb,"[MoSt",5))
  667. {
  668. printf("\nFileheader is: %s\n",tb);
  669. printf("Expected : [MoSt ...]\n");
  670. printf("Using default configuration\n");
  671. fclose(fp);
  672. return 0;
  673. }
  674. while (!feof(fp))
  675. {
  676. eq = strchr(tb,'=');
  677. if (eq)
  678. {
  679. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  680. {
  681. if (!strncmp(tb,Sel->text,strlen(Sel->text)))
  682. {
  683. if (!isalnum(tb[strlen(Sel->text)]))
  684. {
  685. if (Sel->type == SEL_REAL || Sel->type == SEL_PLANET)
  686. {
  687. Sel->fv = atof(eq+1);
  688. if (Sel->pfv) *Sel->pfv = Sel->fv;
  689. if (Sel->type == SEL_PLANET) Sel->fpl[Planet] = Sel->fv;
  690. }
  691. else
  692. {
  693. Sel->iv = atoi(eq+1);
  694. if (Sel->piv) *Sel->piv = Sel->iv;
  695. }
  696. }
  697. }
  698. }
  699. }
  700. else if (!strncmp(tb,"[Modules]",9)) ChangeModel(-1);
  701. else
  702. {
  703. for (pl=0 ; pl < PLANETS ; ++pl)
  704. {
  705. if (!strncmp(tb+1,PlanetName[pl],strlen(PlanetName[pl])) && pl != Planet)
  706. ChangePlanet(pl);
  707. }
  708. }
  709. fgets(tb,sizeof(tb),fp);
  710. }
  711. fclose(fp);
  712. return 1;
  713. }
  714. void Exit(void)
  715. {
  716. int i;
  717. WriteSettings();
  718. // XNextEvent(display,&WinEvent);
  719. // XCloseDisplay(display);
  720. exit(0);
  721. }
  722. void AbortMessage(char *s)
  723. {
  724. printf("\n\n%s\n",s);
  725. exit(1);
  726. }
  727. void Abort(void)
  728. {
  729. Exit();
  730. }
  731. void InitNextSelection(struct SelStruct *Sel, int dy, char *t)
  732. {
  733. Sel->type = Sel->Prev->type;
  734. Sel->y = Sel->Prev->y + dy;
  735. Sel->h = Sel->Prev->h;
  736. Sel->w = Sel->Prev->w;
  737. Sel->xo = Sel->Prev->xo;
  738. Sel->yt = Sel->Prev->yt + dy;
  739. strcpy(Sel->text,t);
  740. Sel->lt = strlen(Sel->text);
  741. Sel->teco = Sel->Prev->teco;
  742. Sel->edco = Sel->Prev->edco;
  743. }
  744. void CursorOn(void)
  745. {
  746. int x,y,w,h;
  747. if (CursorSel)
  748. {
  749. x = CursorSel->x + CursorCol * FixFontWidth;
  750. y = CursorSel->y;
  751. w = CursorSel->w;
  752. h = CursorSel->h;
  753. XSetForeground(display,gc,Red.pixel);
  754. XDrawRectangle(display,Cow,gc,x,y,FixFontWidth,h);
  755. XDrawRectangle(display,Cow,gc,x-1,y-1,FixFontWidth+2,h+2);
  756. }
  757. }
  758. void RemoveBlanks(char *p)
  759. {
  760. int l;
  761. l = strlen(p);
  762. while (l && p[l-1] == ' ') p[--l] = 0;
  763. }
  764. struct SelStruct *NewSel(struct SelStruct *OldSel)
  765. {
  766. struct SelStruct *Sel;
  767. Sel = calloc(1,sizeof(struct SelStruct));
  768. OldSel->Next = Sel;
  769. Sel->Prev = OldSel;
  770. return Sel;
  771. }
  772. void NL_i(int m, char *list, char *name, int i)
  773. {
  774. if (NL_items > NL_MAX_ITEMS-2)
  775. {
  776. printf("\n*** Error ***\n");
  777. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  778. printf("Increase NL_MAX_ITEMS in most,c\n");
  779. exit(1);
  780. }
  781. NL_list[NL_items].model = m;
  782. strcpy(NL_list[NL_items].list,list);
  783. strcpy(NL_list[NL_items].name,name);
  784. NL_list[NL_items].flag = SEL_INT;
  785. NL_list[NL_items].ival = i;
  786. NL_list[NL_items].rval = 0.0;
  787. NL_list[NL_items].text[0] = 0;
  788. NL_items++;
  789. }
  790. void NL_r(int m, char *list, char *name, double r)
  791. {
  792. if (NL_items > NL_MAX_ITEMS-2)
  793. {
  794. printf("\n*** Error ***\n");
  795. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  796. printf("Increase NL_MAX_ITEMS in most,c\n");
  797. exit(1);
  798. }
  799. NL_list[NL_items].model = m;
  800. strcpy(NL_list[NL_items].list,list);
  801. strcpy(NL_list[NL_items].name,name);
  802. NL_list[NL_items].flag = SEL_REAL;
  803. NL_list[NL_items].ival = 0;
  804. NL_list[NL_items].rval = r;
  805. NL_list[NL_items].text[0] = 0;
  806. NL_items++;
  807. }
  808. void NL_p(char *name, float r[])
  809. {
  810. if (NL_items > NL_MAX_ITEMS-2)
  811. {
  812. printf("\n*** Error ***\n");
  813. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  814. printf("Increase NL_MAX_ITEMS in most,c\n");
  815. exit(1);
  816. }
  817. NL_list[NL_items].model = PLASIM;
  818. strcpy(NL_list[NL_items].list,"planet");
  819. strcpy(NL_list[NL_items].name,name);
  820. NL_list[NL_items].flag = SEL_PLANET;
  821. NL_list[NL_items].ival = 0;
  822. NL_list[NL_items].rval = 0.0;
  823. NL_list[NL_items].text[0] = 0;
  824. memcpy(NL_list[NL_items].pvec,r,sizeof(float) * PLANETS);
  825. NL_items++;
  826. }
  827. void NL_t(int m, char *list, char *name, char *t)
  828. {
  829. if (NL_items > NL_MAX_ITEMS-2)
  830. {
  831. printf("\n*** Error ***\n");
  832. printf("Number of namelist items exceeds %d \n",NL_MAX_ITEMS);
  833. printf("Increase NL_MAX_ITEMS in most,c\n");
  834. exit(1);
  835. }
  836. NL_list[NL_items].model = m;
  837. strcpy(NL_list[NL_items].list,list);
  838. strcpy(NL_list[NL_items].name,name);
  839. NL_list[NL_items].flag = SEL_TEVA;
  840. NL_list[NL_items].ival = 0;
  841. NL_list[NL_items].rval = 0.0;
  842. strcpy(NL_list[NL_items].text,t);
  843. NL_items++;
  844. }
  845. // Earth Mars Exo
  846. float eccen_vec[PLANETS] = { 0.016715, 0.09341233, 0.0};
  847. float mvelp_vec[PLANETS] = { 102.7 , 336.04084 , 0.0};
  848. float obliq_vec[PLANETS] = { 23.44 , 25.19 , 0.0};
  849. float gsol0_vec[PLANETS] = {1367.0 , 595.0 ,1000.0};
  850. void InitNamelist(void)
  851. {
  852. // Planet Simulator
  853. NL_p("ECCEN" , eccen_vec);
  854. NL_p("MVELP" , mvelp_vec);
  855. NL_p("OBLIQ" , obliq_vec);
  856. NL_p("GSOL0" , gsol0_vec);
  857. NL_i(PLASIM,"planet" ,"NFIXORB" , 0);
  858. NL_r(PLASIM,"radmod" ,"CO2" ,360.0);
  859. NL_i(PLASIM,"plasim" ,"KICK" , 1);
  860. NL_i(PLASIM,"plasim" ,"MPSTEP" , 0);
  861. //FL0318 NL_i(PLASIM,"plasim" ,"NAQUA" , 0);
  862. NL_i(PLASIM,"plasim" ,"NDIAG" , 0);
  863. //FL0318 NL_i(PLASIM,"plasim" ,"NGUIDBG" , 0);
  864. //FL0318 NL_i(PLASIM,"plasim" ,"NQSPEC" , 1);
  865. //FL0318 NL_i(PLASIM,"plasim" ,"NVEG" , 0);
  866. NL_i(PLASIM,"plasim" ,"NWPD" , 1);
  867. NL_i(PLASIM,"plasim" ,"NPRINT" , 0);
  868. //FL0318 NL_i(PLASIM,"plasim" ,"NSYNC" , 1);
  869. //FL0318 NL_i(PLASIM,"rainmod","NCLOUDS" , 1);
  870. //FL0318 NL_i(PLASIM,"rainmod","NSTORAIN", 0);
  871. //FL0318 NL_r(PLASIM,"plasim" ,"SYNCSTR", 0.0);
  872. if (Expert)
  873. {
  874. NL_i(PLASIM,"plasim" ,"NSYNC" , 0);
  875. NL_r(PLASIM,"plasim" ,"SYNCSTR", 0.0);
  876. }
  877. // SAM
  878. NL_i(SAM,"sam","KICK" , 1);
  879. NL_i(SAM,"sam","NAFTER" , 24);
  880. NL_i(SAM,"sam","NDEL" , 8);
  881. NL_i(SAM,"sam","NDIAG" ,240);
  882. NL_i(SAM,"sam","NEXP" , 4);
  883. NL_i(SAM,"sam","NGUIDBG", 0);
  884. NL_i(SAM,"sam","NTSPD" , 0);
  885. NL_r(SAM,"sam","DISP" ,0.0);
  886. NL_r(SAM,"sam","ROTSPD" ,1.0);
  887. // PUMA
  888. NL_i(PUMA,"puma","KICK" , 1);
  889. NL_i(PUMA,"puma","MPSTEP" , 0);
  890. NL_i(PUMA,"puma","NDEL" , 6);
  891. NL_i(PUMA,"puma","NDHEAT" , 0);
  892. NL_i(PUMA,"puma","NDIAG" , 0);
  893. NL_i(PUMA,"puma","NEWSR" , 0);
  894. NL_i(PUMA,"puma","NGUIDBG", 0);
  895. NL_i(PUMA,"puma","NHELSUA", 0);
  896. NL_i(PUMA,"puma","NSYNC" , 0);
  897. NL_i(PUMA,"puma","NWPD" , 1);
  898. NL_r(PUMA,"puma","DTEP" , 60);
  899. NL_r(PUMA,"puma","DTNS" ,-70.0);
  900. NL_r(PUMA,"puma","DTROP" , 12000.0);
  901. NL_r(PUMA,"puma","DTTRP" , 2.0);
  902. NL_r(PUMA,"puma","SYNCSTR", 0.0);
  903. NL_r(PUMA,"puma","ROTSPD" , 1.0);
  904. NL_r(PUMA,"puma","TGR" , 288.0);
  905. // CAT
  906. NL_r(CAT,"cat","ALPHA" , 0.0);
  907. NL_r(CAT,"cat","BETA" , 0.0);
  908. NL_r(CAT,"cat","AFORC" , 0.001);
  909. NL_r(CAT,"cat","TFORC" , 0.01);
  910. NL_i(CAT,"cat","NFORC" , 0);
  911. NL_i(CAT,"cat","NGUIDBG" , 0);
  912. NL_i(CAT,"cat","NPOST" , 0);
  913. NL_i(CAT,"cat","NSIM" , 0);
  914. NL_i(CAT,"cat","KFMIN" , 0);
  915. NL_i(CAT,"cat","KFMAX" , 8);
  916. NL_i(CAT,"cat","JACMTHD" , 1);
  917. NL_i(CAT,"cat","NSTEPS" , 10000);
  918. }
  919. void NamelistSelector(int model)
  920. {
  921. int i,yn,n,ml;
  922. struct SelStruct *Sel;
  923. int ledi_x,lbox_w,lbox_h;
  924. yn = opbox_y;
  925. n = 0;
  926. ml = 0;
  927. Sel = &SelModels[model];
  928. if (model == PLASIM)
  929. {
  930. Sel->type = SEL_TEXT;
  931. Sel->teco = HeadC;
  932. Sel->y = opbox_y;
  933. Sel->xt = nlpos_x;
  934. Sel->yt = Sel->y + BigFontAscent + 1;
  935. strcpy(Sel->text,PlanetName[Planet]);
  936. Sel->lt = strlen(Sel->text);
  937. yn += BigFontHeight + 1;
  938. Sel = NewSel(Sel);
  939. }
  940. for (i=0 ; i < NL_items ; ++i)
  941. if (NL_list[i].model == model)
  942. {
  943. Sel->Item = NL_list + i;
  944. strcpy(Sel->text,NL_list[i].name);
  945. Sel->lt = strlen(Sel->text);
  946. Sel->type = NL_list[i].flag;
  947. Sel->teco = BlackPix;
  948. Sel->h = FixFontHeight + 1;
  949. Sel->y = yn;
  950. Sel->xt = nlpos_x;
  951. Sel->yt = Sel->y + FixFont->ascent + 1;
  952. if (Sel->lt > ml) ml = Sel->lt;
  953. if (Sel->lt > ml) ml = Sel->lt;
  954. if (NL_list[i].flag == SEL_INT)
  955. {
  956. Sel->w = 6 * FixFontWidth + 2;
  957. Sel->edco = 6;
  958. Sel->div = Sel->iv = NL_list[i].ival;
  959. }
  960. else if (NL_list[i].flag == SEL_REAL)
  961. {
  962. Sel->edco = 11;
  963. Sel->w = 11 * FixFontWidth + 2;
  964. Sel->dfv = Sel->fv = NL_list[i].rval;
  965. }
  966. else if (NL_list[i].flag == SEL_PLANET)
  967. {
  968. Sel->edco = 11;
  969. Sel->w = 11 * FixFontWidth + 2;
  970. Sel->dfv = Sel->fv = NL_list[i].pvec[Planet];
  971. memcpy(Sel->fpl,NL_list[i].pvec,sizeof(float) * PLANETS);
  972. }
  973. else
  974. {
  975. Sel->edco = 11;
  976. Sel->w = 11 * FixFontWidth + 2;
  977. strncpy(Sel->teva,NL_list[i].text,15);
  978. }
  979. yn += FixFontHeight + 1;
  980. Sel = NewSel(Sel);
  981. ++n;
  982. }
  983. Sel->Prev->Next = NULL; // Mark end of chain
  984. /* Compute size of namelist box */
  985. ledi_x = nlpos_x + (ml + 1) * FixFontWidth;
  986. lbox_w = (ml + 14) * FixFontWidth;
  987. lbox_h = BigFontHeight + (n+2) * FixFontHeight;
  988. if (nledi_x < ledi_x) nledi_x = ledi_x;
  989. if (nlbox_w < lbox_w) nlbox_w = lbox_w;
  990. if (nlbox_h < lbox_h) nlbox_h = lbox_h;
  991. UpdateSelections(ComEnd->Next);
  992. }
  993. void LoadFonts(void)
  994. {
  995. if (ScreenH < 700)
  996. {
  997. strcpy(ModFontName,"5x8");
  998. strcpy(FixFontName,"6x10");
  999. strcpy(BigFontName,"7x13");
  1000. }
  1001. else if (ScreenH < 800)
  1002. {
  1003. strcpy(ModFontName,"6x10");
  1004. strcpy(FixFontName,"7x13");
  1005. strcpy(BigFontName,"9x15bold");
  1006. }
  1007. if ((ModFont = XLoadQueryFont(display,ModFontName)) == NULL)
  1008. {
  1009. printf("%s: Cannot open %s font\n",progname,ModFontName);
  1010. exit(-1);
  1011. }
  1012. if ((FixFont = XLoadQueryFont(display,FixFontName)) == NULL)
  1013. {
  1014. printf("%s: Cannot open %s font\n",progname,FixFontName);
  1015. exit(-1);
  1016. }
  1017. if ((BigFont = XLoadQueryFont(display,BigFontName)) == NULL)
  1018. {
  1019. printf("%s: Cannot open %s font\n",progname,BigFontName);
  1020. exit(-1);
  1021. }
  1022. SamFont = XLoadQueryFont(display,"rk24");
  1023. ModFontWidth = XTextWidth(ModFont,"X",1);
  1024. FixFontWidth = XTextWidth(FixFont,"X",1);
  1025. BigFontWidth = XTextWidth(BigFont,"X",1);
  1026. FixFontAscent = FixFont->ascent;
  1027. ModFontAscent = ModFont->ascent;
  1028. BigFontAscent = BigFont->ascent;
  1029. ModFontHeight = ModFont->ascent + ModFont->descent;
  1030. FixFontHeight = FixFont->ascent + FixFont->descent;
  1031. BigFontHeight = BigFont->ascent + BigFont->descent;
  1032. if (Debug)
  1033. {
  1034. printf("ModFont %2d x %2d %s\n",ModFontWidth,ModFontHeight,ModFontName);
  1035. printf("FixFont %2d x %2d %s\n",FixFontWidth,FixFontHeight,FixFontName);
  1036. printf("BigFont %2d x %2d %s\n",BigFontWidth,BigFontHeight,BigFontName);
  1037. }
  1038. }
  1039. void InitSelections(void)
  1040. {
  1041. int i,l,n,dyn,dys,mw,x,dx;
  1042. char text[80];
  1043. struct SelStruct *Sel;
  1044. struct tm *dati;
  1045. FILE *fp;
  1046. opbox_x = FixFontWidth / 2;
  1047. dyn = FixFontHeight + 1;
  1048. dys = (3 * dyn) / 2;
  1049. // Initialize Sel at anchor SelStart
  1050. Sel = &SelStart;
  1051. // Model
  1052. Sel->type = SEL_TEXT;
  1053. Sel->teco = HeadC;
  1054. Sel->y = opbox_y;
  1055. Sel->yt = Sel->y + BigFontAscent + 1;
  1056. strcpy(Sel->text,"Model");
  1057. Sel->lt = strlen(Sel->text);
  1058. // PUMA
  1059. Sel = NewSel(Sel);
  1060. InitNextSelection(Sel,BigFontHeight,FullModelName[PUMA]);
  1061. Sel->type = SEL_CHECK;
  1062. Sel->teco = BlackPix;
  1063. Sel->h = FixFontHeight + 1;
  1064. Sel->w = FixFontHeight + 1;
  1065. Sel->yt = Sel->y + FixFontAscent + 1;
  1066. Sel->div = Sel->iv = 1;
  1067. SelMod = Sel;
  1068. // SAM
  1069. Sel = NewSel(Sel);
  1070. InitNextSelection(Sel,dyn,"SAM");
  1071. // Hide SAM ?
  1072. if (!SamEnabled)
  1073. {
  1074. Sel->no = 1;
  1075. Sel->lt = 0;
  1076. }
  1077. // CAT
  1078. Sel = NewSel(Sel);
  1079. InitNextSelection(Sel,dyn,FullModelName[CAT]);
  1080. // Hide CAT until released
  1081. if (!CatEnabled)
  1082. {
  1083. Sel->no = 1;
  1084. Sel->lt = 0;
  1085. }
  1086. // Planet Simulator
  1087. Sel = NewSel(Sel);
  1088. InitNextSelection(Sel,dyn,FullModelName[PLASIM]);
  1089. // Earth
  1090. Sel = NewSel(Sel);
  1091. InitNextSelection(Sel,dyn,"Earth");
  1092. SelPlanet[EARTH] = Sel;
  1093. Sel->div = Sel->iv = 1;
  1094. Sel->no = 1;
  1095. Sel->xo = (5 * FixFontWidth) / 2;
  1096. // Mars
  1097. Sel = NewSel(Sel);
  1098. InitNextSelection(Sel,dyn,"Mars");
  1099. SelPlanet[MARS] = Sel;
  1100. Sel->no = 1;
  1101. // Exo
  1102. Sel = NewSel(Sel);
  1103. InitNextSelection(Sel,dyn,"Exo");
  1104. SelPlanet[EXO] = Sel;
  1105. Sel->no = 1;
  1106. // Modules
  1107. Sel = NewSel(Sel);
  1108. InitNextSelection(Sel,dys,"Modules");
  1109. Sel->type = SEL_TEXT;
  1110. Sel->teco = HeadC;
  1111. Sel->h = 0;
  1112. Sel->w = 0;
  1113. Sel->yt = Sel->y + BigFontAscent + 1;
  1114. Sel->xo = 0;
  1115. // Mixed Layer Ocean
  1116. Sel = NewSel(Sel);
  1117. InitNextSelection(Sel,BigFontHeight,"ML Ocean");
  1118. Sel->type = SEL_CHECK;
  1119. Sel->teco = BlackPix;
  1120. Sel->h = FixFontHeight + 1;
  1121. Sel->w = FixFontHeight + 1;
  1122. Sel->yt = Sel->y + FixFontAscent + 1;
  1123. Sel->div = Sel->iv = 0;
  1124. Sel->no = 1;
  1125. SelOce = Sel;
  1126. Sel->piv = &Oce;
  1127. // LSG Ocean
  1128. if (LsgEnabled)
  1129. {
  1130. Sel = NewSel(Sel);
  1131. InitNextSelection(Sel,FixFontHeight,"LSG Ocean");
  1132. Sel->yt = Sel->y + FixFontAscent + 1;
  1133. Sel->div = Sel->iv = 0;
  1134. Sel->no = 1;
  1135. SelLsg = Sel;
  1136. Sel->piv = &Lsg;
  1137. }
  1138. // Ice
  1139. Sel = NewSel(Sel);
  1140. InitNextSelection(Sel,FixFontHeight,"Sea Ice");
  1141. Sel->type = SEL_CHECK;
  1142. Sel->teco = BlackPix;
  1143. Sel->h = FixFontHeight + 1;
  1144. Sel->w = FixFontHeight + 1;
  1145. Sel->yt = Sel->y + FixFontAscent + 1;
  1146. Sel->div = Sel->iv = 0;
  1147. Sel->no = 1;
  1148. SelIce = Sel;
  1149. Sel->piv = &Ice;
  1150. // Hardware
  1151. Sel = NewSel(Sel);
  1152. InitNextSelection(Sel,dys,"Parallelism");
  1153. Sel->type = SEL_TEXT;
  1154. Sel->teco = HeadC;
  1155. Sel->h = 0;
  1156. Sel->w = 0;
  1157. Sel->yt = Sel->y + BigFontAscent + 1;
  1158. // Number of CPUs
  1159. Sel = NewSel(Sel);
  1160. InitNextSelection(Sel,dys,"Cores");
  1161. Sel->h = FixFontHeight + 1;
  1162. Sel->w = FixFontHeight + 1;
  1163. Sel->yt = Sel->y + FixFont->ascent + 1;
  1164. Sel->w = 4 * FixFontWidth + 2;
  1165. Sel->type = SEL_INT;
  1166. Sel->edco = 4;
  1167. Sel->div = Sel->iv = Cores;
  1168. Sel->piv = &Cores;
  1169. SelCPU = Sel;
  1170. fp = fopen("most_compiler_mpi","r");
  1171. if (fp) fclose(fp);
  1172. else Sel->no = 1;
  1173. // Number of synchronous runs
  1174. if (MultirunEnabled && Expert)
  1175. {
  1176. Sel = NewSel(Sel);
  1177. InitNextSelection(Sel,dys,"Instances");
  1178. Sel->h = FixFontHeight + 1;
  1179. Sel->w = FixFontHeight + 1;
  1180. Sel->yt = Sel->y + FixFont->ascent + 1;
  1181. Sel->w = 4 * FixFontWidth + 2;
  1182. Sel->type = SEL_INT;
  1183. Sel->edco = 4;
  1184. Sel->div = Sel->iv = Multirun;
  1185. Sel->piv = &Multirun;
  1186. SelMulti = Sel;
  1187. fp = fopen("most_compiler_mpi","r");
  1188. if (fp) fclose(fp);
  1189. else Sel->no = 1;
  1190. }
  1191. // Resolution
  1192. Sel = NewSel(Sel);
  1193. InitNextSelection(Sel,dys,"Resolution");
  1194. Sel->type = SEL_TEXT;
  1195. Sel->h = 0;
  1196. Sel->w = 0;
  1197. Sel->yt = Sel->y + BigFont->ascent + 1;
  1198. // Horizontal resolution
  1199. if (Expert)
  1200. {
  1201. Sel = NewSel(Sel);
  1202. InitNextSelection(Sel,dys,"Latitudes #1");
  1203. Sel->h = FixFontHeight + 1;
  1204. Sel->yt = Sel->y + FixFont->ascent + 1;
  1205. Sel->w = 4 * FixFontWidth + 2;
  1206. Sel->edco = 4;
  1207. Sel->type = SEL_INT;
  1208. Sel->div = Sel->iv = Latitudes;
  1209. Sel->piv = &Latitudes;
  1210. DimText1 = Sel->text;
  1211. Sel = NewSel(Sel);
  1212. InitNextSelection(Sel,dys,"Latitudes #2");
  1213. Sel->div = Sel->iv = Latitudes;
  1214. Sel->piv = &Latitude2;
  1215. SelLat2 = Sel;
  1216. DimText2 = Sel->text;
  1217. }
  1218. else
  1219. {
  1220. Sel = NewSel(Sel);
  1221. InitNextSelection(Sel,dys,"T21 [64x32]");
  1222. Sel->type = SEL_CHECK;
  1223. Sel->h = FixFontHeight + 1;
  1224. Sel->w = FixFontHeight + 1;
  1225. Sel->yt = Sel->y + FixFont->ascent + 1;
  1226. Sel->div = Sel->iv = 1;
  1227. SelRes = Sel;
  1228. DimText1 = Sel->text;
  1229. Sel = NewSel(Sel);
  1230. InitNextSelection(Sel,dyn,"T31 [96x48]");
  1231. Sel->type = SEL_CHECK;
  1232. Sel->h = FixFontHeight + 1;
  1233. Sel->w = FixFontHeight + 1;
  1234. Sel->div = Sel->iv = 0;
  1235. DimText2 = Sel->text;
  1236. Sel = NewSel(Sel);
  1237. InitNextSelection(Sel,dyn,"T42 [128x64]");
  1238. Sel->type = SEL_CHECK;
  1239. Sel->h = FixFontHeight + 1;
  1240. Sel->w = FixFontHeight + 1;
  1241. Sel->div = Sel->iv = 0;
  1242. DimText3 = Sel->text;
  1243. }
  1244. // Vertical resolution
  1245. if (Expert)
  1246. {
  1247. Sel = NewSel(Sel);
  1248. InitNextSelection(Sel,dys,"Levels");
  1249. Sel->type = SEL_INT;
  1250. Sel->div = Sel->iv = Levels;
  1251. Sel->piv = &Levels;
  1252. }
  1253. // Options
  1254. Sel = NewSel(Sel);
  1255. InitNextSelection(Sel,dys,"Options");
  1256. Sel->type = SEL_TEXT;
  1257. Sel->h = 0;
  1258. Sel->w = 0;
  1259. Sel->yt = Sel->y + BigFont->ascent + 1;
  1260. // Global debug switch
  1261. Sel = NewSel(Sel);
  1262. InitNextSelection(Sel,dys,"Debug mode");
  1263. Sel->type = SEL_CHECK;
  1264. Sel->h = FixFontHeight + 1;
  1265. Sel->w = FixFontHeight + 1;
  1266. Sel->yt = Sel->y + FixFont->ascent + 1;
  1267. Sel->div = Sel->iv = 0;
  1268. Sel->piv = &ndebug;
  1269. // Precision switch
  1270. Sel = NewSel(Sel);
  1271. InitNextSelection(Sel,dyn,"Double Precision");
  1272. Sel->piv = &nprec;
  1273. // Global output switch
  1274. Sel = NewSel(Sel);
  1275. InitNextSelection(Sel,dys,"Write Output");
  1276. Sel->type = SEL_CHECK;
  1277. Sel->h = FixFontHeight + 1;
  1278. Sel->w = FixFontHeight + 1;
  1279. Sel->yt = Sel->y + FixFont->ascent + 1;
  1280. Sel->div = Sel->iv = 0;
  1281. Sel->piv = &noutput;
  1282. // GUI
  1283. Sel = NewSel(Sel);
  1284. InitNextSelection(Sel,dyn,"Run with GUI");
  1285. Sel->type = SEL_CHECK;
  1286. Sel->h = FixFontHeight + 1;
  1287. Sel->w = FixFontHeight + 1;
  1288. Sel->div = Sel->iv = 1;
  1289. Sel->piv = &ngui;
  1290. // Orography
  1291. Sel = NewSel(Sel);
  1292. InitNextSelection(Sel,dyn,"Orography");
  1293. Sel->type = SEL_CHECK;
  1294. //FL0318 Sel->div = Sel->iv = 1;
  1295. Sel->div = Sel->iv = 0;
  1296. Sel->piv = &noro;
  1297. SelOro = Sel;
  1298. // Annual cycle
  1299. Sel = NewSel(Sel);
  1300. InitNextSelection(Sel,dyn,"Annual cycle");
  1301. Sel->type = SEL_CHECK;
  1302. //FL0318 Sel->div = Sel->iv = 1;
  1303. Sel->div = Sel->iv = 0;
  1304. SelAnn = Sel;
  1305. // Experiment
  1306. Sel = NewSel(Sel);
  1307. InitNextSelection(Sel,dys,"Simulation");
  1308. Sel->type = SEL_TEXT;
  1309. Sel->h = 0;
  1310. Sel->w = 0;
  1311. Sel->yt = Sel->y + BigFont->ascent + 1;
  1312. // Simulation Start
  1313. dati = localtime(&CurrentDate);
  1314. Sel = NewSel(Sel);
  1315. InitNextSelection(Sel,dys,"Start year");
  1316. Sel->h = FixFontHeight + 1;
  1317. Sel->yt = Sel->y + FixFont->ascent + 1;
  1318. Sel->w = 4 * FixFontWidth + 2;
  1319. Sel->edco = 4;
  1320. Sel->type = SEL_INT;
  1321. Sel->div = Sel->iv = 1;
  1322. Sel->piv = &SimStart;
  1323. SelSYear = Sel;
  1324. // Years
  1325. Sel = NewSel(Sel);
  1326. InitNextSelection(Sel,dyn,"Years to run");
  1327. Sel->h = FixFontHeight + 1;
  1328. Sel->yt = Sel->y + FixFont->ascent + 1;
  1329. Sel->w = 4 * FixFontWidth + 2;
  1330. Sel->type = SEL_INT;
  1331. Sel->div = Sel->iv = 10;
  1332. Sel->piv = &SimYears;
  1333. opbox_h = Sel->y + dys - opbox_y;
  1334. // Orography edit
  1335. Sel = NewSel(Sel);
  1336. InitNextSelection(Sel,dys,"Change [gpm]");
  1337. Sel->h = FixFontHeight + 1;
  1338. Sel->yt = Sel->y + FixFont->ascent + 1;
  1339. Sel->w = 6 * FixFontWidth + 2;
  1340. Sel->type = SEL_INT;
  1341. Sel->div = Sel->iv = 0;
  1342. Sel->piv = &OroAno;
  1343. SelAno = Sel;
  1344. // Ground temperature edit
  1345. /*
  1346. Sel = NewSel(Sel);
  1347. InitNextSelection(Sel,dys,"Change [K]");
  1348. Sel->h = FixFontHeight + 1;
  1349. Sel->yt = Sel->y + FixFont->ascent + 1;
  1350. Sel->w = 6 * FixFontWidth + 2;
  1351. Sel->type = SEL_INT;
  1352. Sel->div = Sel->iv = 0;
  1353. Sel->piv = &TgrAno;
  1354. SelTgr = Sel;
  1355. */
  1356. // Mark end of common selection chain
  1357. ComEnd = Sel;
  1358. mw = 0;
  1359. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1360. if (Sel->w > mw) mw = Sel->w;
  1361. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1362. {
  1363. Sel->xt = Sel->xo + FixFontWidth * 2 + mw;
  1364. if (!Sel->x) Sel->x = Sel->xt - Sel->w - FixFontWidth;
  1365. if (Sel->type == SEL_INT )
  1366. {
  1367. sprintf(text,"%10d",Sel->iv);
  1368. strcpy(Sel->teva,text+10-Sel->edco);
  1369. }
  1370. }
  1371. for (x = 0 , Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1372. {
  1373. if (Sel->type == SEL_TEXT) l = Sel->lt * BigFontWidth;
  1374. else l = Sel->lt * FixFontWidth;
  1375. if (x < l) x = l;
  1376. }
  1377. opbox_w = x + mw + 2 * FixFontWidth;
  1378. nlbox_x = opbox_x + opbox_w + FixFontWidth;
  1379. nlbox_y = opbox_y;
  1380. nlpos_x = nlbox_x + FixFontWidth;
  1381. }
  1382. void CheckMark(int x, int y, int d)
  1383. {
  1384. XSetForeground(display,gc,LightGreen.pixel);
  1385. XFillArc(display,Cow,gc,x+1,y+1,d-5,d-5,0,FULLARC);
  1386. }
  1387. void DisplayTeVa(int x, int y, int w, char *t)
  1388. {
  1389. int l;
  1390. l = strlen(t);
  1391. if (l > w / FixFontWidth) l = w / FixFontWidth;
  1392. XSetForeground(display,gc,WhitePix);
  1393. XSetBackground(display,gc,BlackPix);
  1394. if (l) XDrawImageString(display,Cow,gc,x,y,t,l);
  1395. }
  1396. void ShowSelection(struct SelStruct *Sel)
  1397. {
  1398. int x,y,h,w;
  1399. if (Sel->hide) return;
  1400. h = Sel->h;
  1401. w = Sel->w;
  1402. x = Sel->x;
  1403. y = Sel->y;
  1404. if (!Sel->no)
  1405. {
  1406. if (Sel->type == SEL_CHECK)
  1407. {
  1408. XSetForeground(display,gc,TextC);
  1409. XDrawArc(display,Cow,gc,x,y,w-2,h-2,0,FULLARC);
  1410. if (Sel->iv) CheckMark(x,y,w+1);
  1411. }
  1412. else DisplayTeVa(x+1,Sel->yt,w,Sel->teva);
  1413. }
  1414. XSetForeground(display,gc,TextC);
  1415. XSetBackground(display,gc,WinBG);
  1416. if (Sel->type == SEL_TEXT)
  1417. {
  1418. XSetFont(display, gc, BigFont->fid);
  1419. XSetForeground(display,gc,Sel->teco);
  1420. XDrawImageString(display,Cow,gc,Sel->xt,Sel->yt,Sel->text,Sel->lt);
  1421. XSetFont(display, gc, FixFont->fid);
  1422. }
  1423. else
  1424. XDrawImageString(display,Cow,gc,Sel->xt,Sel->yt,Sel->text,Sel->lt);
  1425. }
  1426. void InitLogo(void)
  1427. {
  1428. int n;
  1429. // KlimaCampus
  1430. n = 0;
  1431. Logo[n].x = FixFontWidth;
  1432. Logo[n].y = 8;
  1433. // PUMA
  1434. ++n;
  1435. strcpy(Logo[n].t[0],FullModelName[PUMA]);
  1436. // Plasim
  1437. ++n;
  1438. strcpy(Logo[n].t[0],FullModelName[PLASIM]);
  1439. // Cat
  1440. ++n;
  1441. strcpy(Logo[n].t[0],FullModelName[CAT]);
  1442. Logos = n+1;
  1443. }
  1444. void GenerateNames(void)
  1445. {
  1446. Truncation = (2 * Latitudes - 1) / 3;
  1447. sprintf(namelist_name,"%s_namelist",ShortModelName[Model]);
  1448. if (Model == CAT)
  1449. {
  1450. if (Cores < 2) strcpy(exec_name,"most_cat.x");
  1451. else strcpy(exec_name,"most_cat_mpi.x");
  1452. }
  1453. else if (Model == PUMA)
  1454. {
  1455. if (Cores < 2) strcpy(exec_name,"most_puma.x");
  1456. else strcpy(exec_name,"most_puma_mpi.x");
  1457. }
  1458. else if (Model == SAM)
  1459. {
  1460. if (Cores < 2) strcpy(exec_name,"most_sam.x");
  1461. else strcpy(exec_name,"most_sam_mpi.x");
  1462. }
  1463. else
  1464. sprintf(exec_name,"most_%s_t%d_l%d_p%d.x",
  1465. ShortModelName[Model],Truncation,Levels,Cores);
  1466. sprintf(diag_name,"%s_diag",ShortModelName[Model]);
  1467. sprintf(outp_name,"%s_output",ShortModelName[Model]);
  1468. sprintf(build_name,"most_%s_build",ShortModelName[Model]);
  1469. sprintf(build_ppp,"most_%s_build","ppp");
  1470. sprintf(run_name,"most_%s_run",ShortModelName[Model]);
  1471. if (Latitudes < 1000) sprintf(oro_name,"N%3.3d_surf_0129.sra",Latitudes);
  1472. else sprintf(oro_name,"N%d_surf_0129.sra",Latitudes);
  1473. }
  1474. int WriteRunScript(int model)
  1475. {
  1476. int i;
  1477. int porm;
  1478. FILE *fp;
  1479. char command[256];
  1480. char run[256];
  1481. strcpy(exec_nam2,exec_name); // Duplicate exec name
  1482. if (model == CAT) // Add Dimensions
  1483. {
  1484. sprintf(exec_name+strlen(exec_name)," %d %d",Latitudes,Latitude2);
  1485. }
  1486. if (model == PUMA) // Add Latitudes and Levels as arguments
  1487. {
  1488. sprintf(exec_name+strlen(exec_name)," %d %d",Latitudes,Levels);
  1489. sprintf(exec_nam2+strlen(exec_nam2)," %d %d",Latitude2,Levels);
  1490. }
  1491. if (model == SAM) // Add Latitudes as arguments
  1492. {
  1493. sprintf(exec_name+strlen(exec_name)," %d",Latitudes);
  1494. sprintf(exec_nam2+strlen(exec_nam2)," %d",Latitude2);
  1495. }
  1496. // porm is for Parallel OR Multiprocessing
  1497. porm = MAX(Cores,Multirun);
  1498. strcpy(run,ShortModelName[model]);
  1499. strcat(run,"/run/");
  1500. strcat(run,run_name);
  1501. fp = fopen(run,"w");
  1502. if (fp == NULL)
  1503. {
  1504. printf("Could not open file <%s> for writing\n",run);
  1505. return 0; /* Failure */
  1506. }
  1507. fputs("#!/bin/bash\n",fp);
  1508. fprintf(fp,"# run-script generated by Most %s",ctime(&CurrentDate));
  1509. fputs("EXP=MOST # Name your experiment here\n",fp);
  1510. fprintf(fp,"[ $# == 1 ] && cd $1\n");
  1511. fprintf(fp,"rm -f %s_restart\n",ShortModelName[model]);
  1512. fputs("rm -f Abort_Message\n",fp);
  1513. fputs("YEAR=0\n",fp);
  1514. fprintf(fp,"YEARS=%d\n",SimYears);
  1515. if (Multirun > 1) fprintf(fp,"INSTANCES=%d\n",Multirun);
  1516. if (ngui) fputs("# Remove '#' from 'while' and 'end' lines for restart loop\n",fp);
  1517. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1518. fputs("while [ $YEAR -lt $YEARS ]\n",fp);
  1519. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1520. fputs("do\n",fp);
  1521. fputs(" YEAR=`expr $YEAR + 1`\n",fp);
  1522. fputs(" DATANAME=`printf '%s.%03d' $EXP $YEAR`\n",fp);
  1523. fputs(" DIAGNAME=`printf '%s_DIAG.%03d' $EXP $YEAR`\n",fp);
  1524. fputs(" RESTNAME=`printf '%s_REST.%03d' $EXP $YEAR`\n",fp);
  1525. if (porm < 2)
  1526. {
  1527. fprintf(fp," ./%s\n",exec_name);
  1528. }
  1529. else
  1530. {
  1531. if (Multirun > 1)
  1532. {
  1533. fprintf(fp," %s",mpirun);
  1534. fprintf(fp," -np 1 %s : -np 1 %s\n",exec_name,exec_nam2);
  1535. }
  1536. else
  1537. {
  1538. fprintf(fp," %s -np %d %s\n",mpirun,porm,exec_name);
  1539. }
  1540. }
  1541. fputs(" [ -e Abort_Message ] && exit 1\n",fp);
  1542. if (Multirun > 1)
  1543. {
  1544. fputs(" INST=0\n",fp);
  1545. fputs(" while [ $INST -lt $INSTANCES ]\n do\n",fp);
  1546. fputs(" EXT=`printf '%02d' $INST`\n",fp);
  1547. fprintf(fp," [ -e %s_$EXT ] && mv %s_$EXT ${EXT}_$DATANAME\n",
  1548. outp_name,outp_name);
  1549. fprintf(fp," mv %s_$EXT ${EXT}_$DIAGNAME\n",diag_name);
  1550. fprintf(fp," cp %s_status_$EXT %s_restart_$EXT\n",
  1551. ShortModelName[model],ShortModelName[model]);
  1552. fprintf(fp," mv %s_status_$EXT ${EXT}_$RESTNAME\n",ShortModelName[model]);
  1553. fputs(" INST=`expr $INST + 1`\n",fp);
  1554. fputs(" done\n",fp);
  1555. }
  1556. else
  1557. {
  1558. fprintf(fp," [ -e %s ] && mv %s $DATANAME\n",outp_name,outp_name);
  1559. fprintf(fp," [ -e %s ] && mv %s $DIAGNAME\n",diag_name,diag_name);
  1560. fprintf(fp," [ -e %s_status ] && cp %s_status %s_restart\n",
  1561. ShortModelName[model],ShortModelName[model],ShortModelName[model]);
  1562. fprintf(fp," [ -e %s_status ] && mv %s_status $RESTNAME\n",
  1563. ShortModelName[model],ShortModelName[model]);
  1564. }
  1565. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1566. fputs("done\n",fp);
  1567. fclose(fp);
  1568. sprintf(command,"chmod a+x %s",run);
  1569. system(command);
  1570. return 1; /* Success */
  1571. }
  1572. void BlueMessage(char *mess)
  1573. {
  1574. int l,x,y,w,h;
  1575. XSetFont(display, gc, FixFont->fid);
  1576. l = strlen(mess);
  1577. w = FixFontWidth * (l + 4);
  1578. h = FixFontHeight * 3;
  1579. if (w > WinXSize) w = WinXSize;
  1580. XSetForeground(display,gc,Blue.pixel);
  1581. x = (WinXSize - w) / 2;
  1582. y = (WinYSize - h) / 2;
  1583. XFillRectangle(display,Cow,gc,x,y,w,h);
  1584. XSetForeground(display,gc,WhitePix);
  1585. XSetBackground(display,gc,Blue.pixel);
  1586. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1587. x += 2 * FixFontWidth;
  1588. y += 2 * FixFontHeight;
  1589. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1590. XSync(display,0);
  1591. }
  1592. void RedMessage(char *mess)
  1593. {
  1594. int l,x,y,w,h;
  1595. XSetFont(display, gc, FixFont->fid);
  1596. l = strlen(mess);
  1597. w = FixFontWidth * (l + 4);
  1598. h = FixFontHeight * 3;
  1599. if (w > WinXSize) w = WinXSize;
  1600. XSetForeground(display,gc,Red.pixel);
  1601. x = (WinXSize - w) / 2;
  1602. y = (WinYSize - h) / 2;
  1603. XFillRectangle(display,Cow,gc,x,y,w,h);
  1604. XSetForeground(display,gc,WhitePix);
  1605. XSetBackground(display,gc,Red.pixel);
  1606. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1607. x += 2 * FixFontWidth;
  1608. y += 2 * FixFontHeight;
  1609. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1610. XSync(display,0);
  1611. }
  1612. int WriteRunPPP(void)
  1613. {
  1614. int i;
  1615. FILE *fp;
  1616. char command[256];
  1617. char run[256];
  1618. strcpy(run,ShortModelName[Model]);
  1619. strcat(run,"/run/");
  1620. strcat(run,run_ppp);
  1621. fp = fopen(run,"w");
  1622. if (fp == NULL)
  1623. {
  1624. printf("Could not open file <%s> for writing\n",run);
  1625. return 0; /* Failure */
  1626. }
  1627. fputs("#!/bin/bash\n",fp);
  1628. fprintf(fp,"# run-script generated by Most %s",ctime(&CurrentDate));
  1629. fputs("[ $# == 1 ] && cd $1\n",fp);
  1630. if (OroClear) fprintf(fp,"rm -f %s\n",oro_name);
  1631. else if (!OroAqua)
  1632. fprintf(fp,"[ ! -e %s -a -e ../dat/%s ] && cp -p ../dat/%s .\n",
  1633. oro_name,oro_name,oro_name);
  1634. fprintf(fp," ./%s >!ppp.out\n",exec_ppp);
  1635. fclose(fp);
  1636. sprintf(command,"chmod a+x %s",run);
  1637. system(command);
  1638. strcpy(command,run);
  1639. BlueMessage("Running PUMA Preprocessor");
  1640. sprintf(command,"%s/run/%s %s/run",ShortModelName[Model],run_ppp,ShortModelName[Model]);
  1641. system(command);
  1642. OroAno = 0; /* Anomaly applied -> reset it */
  1643. SelAno->iv = 0;
  1644. return 1; /* Success */
  1645. }
  1646. void WriteResmod(char *res)
  1647. {
  1648. FILE *fp;
  1649. int OldLat,OldLev,OldPro;
  1650. char Line[80];
  1651. // Read existing file if there and check for changes
  1652. fp = fopen(res,"r");
  1653. if (fp)
  1654. {
  1655. fgets(Line,80,fp); // header line 1
  1656. fgets(Line,80,fp); // header line 2
  1657. fgets(Line,80,fp);
  1658. OldLat = atoi(Line+27);
  1659. fgets(Line,80,fp);
  1660. OldLev = atoi(Line+27);
  1661. fgets(Line,80,fp);
  1662. OldPro = atoi(Line+27);
  1663. fclose(fp);
  1664. if (OldLat == Latitudes && OldLev == Levels && OldPro == Cores) return;
  1665. }
  1666. fp = fopen(res,"w");
  1667. if (fp == NULL)
  1668. {
  1669. printf("Could not open file <%s> for writing\n",res);
  1670. exit(1); /* Failure */
  1671. }
  1672. fprintf(fp," module resmod ! generated by MoSt %s\n",ctime(&CurrentDate));
  1673. fprintf(fp," parameter(NLAT_ATM = %d)\n",Latitudes);
  1674. fprintf(fp," parameter(NLEV_ATM = %d)\n",Levels);
  1675. fprintf(fp," parameter(NPRO_ATM = %d)\n",Cores);
  1676. fprintf(fp," end module resmod\n");
  1677. fclose(fp);
  1678. }
  1679. int Build(int model)
  1680. {
  1681. int i,l,x,y,ierr;
  1682. int porm;
  1683. FILE *fp;
  1684. char script_backup[256];
  1685. char command[256];
  1686. char message[256];
  1687. char bld[256];
  1688. char res[256];
  1689. char *shomo;
  1690. porm = MAX(Cores,Multirun);
  1691. shomo = ShortModelName[model];
  1692. strcpy(bld,shomo);
  1693. strcat(bld,"/bld/");
  1694. strcpy(res,bld);
  1695. strcat(bld,build_name);
  1696. strcat(res,res_name);
  1697. strcpy(script_backup,bld);
  1698. strcat(script_backup,".bak");
  1699. rename(bld,script_backup);
  1700. if (model == PLASIM) WriteResmod(res);
  1701. fp = fopen(bld,"w");
  1702. if (fp == NULL)
  1703. {
  1704. printf("Could not open file <%s> for writing\n",bld);
  1705. return 0; /* Failure */
  1706. }
  1707. fputs("#!/bin/bash\n",fp);
  1708. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1709. fputs("[ $# == 1 ] && cd $1\n",fp);
  1710. if (ForceRebuild) fputs("rm -f *.x *.o *.mod\n",fp);
  1711. fputs("cp -p ../src/* .\n",fp);
  1712. if (ngui == 0) // Use stub routines and switch off X11 lib
  1713. {
  1714. putenv("GUIMOD=guimod_stub");
  1715. putenv("PUMAX=pumax_stub");
  1716. putenv("GUIX11=guix11_stub");
  1717. putenv("GUILIB=");
  1718. }
  1719. if (Planet == MARS) putenv("PLAMOD=p_mars");
  1720. if (Planet == EXO ) putenv("PLAMOD=p_exo");
  1721. if (Lsg)
  1722. {
  1723. fputs("cp -p ../../lsg/src/lsgmod.f90 .\n",fp);
  1724. putenv("OCEANCOUP=cpl");
  1725. }
  1726. else
  1727. {
  1728. putenv("OCEANCOUP=cpl_stub");
  1729. }
  1730. if (Latitudes < 4) putenv("FFTMOD=fft991mod");
  1731. if (porm > 1)
  1732. {
  1733. fputs("[ ! -e MPI ] && rm -f *.o *.mod *.x\n",fp);
  1734. fputs("touch MPI\n",fp);
  1735. fputs("cat ../../most_compiler_mpi",fp);
  1736. if (Multirun > 1) putenv("MPIMOD=mpimod_multi");
  1737. }
  1738. else
  1739. {
  1740. fputs("[ -e MPI ] && rm -f *.o *.mod *.x MPI\n",fp);
  1741. fputs("cat ../../most_compiler",fp);
  1742. }
  1743. if (ndebug) fputs(" ../../most_debug_options",fp);
  1744. if (nprec)
  1745. {
  1746. fputs(" ../../most_precision_options",fp);
  1747. }
  1748. fprintf(fp," make_%s > makefile\n",shomo);
  1749. fputs("make -e\n",fp);
  1750. fprintf(fp,"[ $? == 0 ] && cp %s.x ../bin/%s\n",shomo,exec_name);
  1751. fclose(fp);
  1752. sprintf(command,"chmod a+x %s",bld);
  1753. system(command);
  1754. sprintf(message,"Building %s - wait a minute!",FullModelName[Model]);
  1755. BlueMessage(message);
  1756. strcat(bld," ");
  1757. strcat(bld,shomo);
  1758. strcat(bld,"/bld");
  1759. if (system(bld))
  1760. {
  1761. RedMessage("Error in build process");
  1762. sleep(5);
  1763. return 0; // error
  1764. }
  1765. sprintf(command,"cp %s/bin/%s %s/run\n",shomo,exec_name,shomo);
  1766. system(command);
  1767. // copy CAT simulation namelist to run directory
  1768. if (model == CAT)
  1769. {
  1770. sprintf(command,"cp cat/dat/sim_%4.4d.nl cat/run/sim_namelist\n",CatSim);
  1771. system(command);
  1772. }
  1773. // copy hires background bitmap for Earth or Mars
  1774. if (model == PUMA || model == PLASIM )
  1775. {
  1776. if (model == PLASIM && Planet == MARS)
  1777. {
  1778. sprintf(command,"cp images/mars.bmp %s/run/map.bmp\n",shomo);
  1779. system(command);
  1780. }
  1781. else if (model == PLASIM && Planet == EARTH)
  1782. {
  1783. sprintf(command,"cp images/earth.bmp %s/run/map.bmp\n",shomo);
  1784. system(command);
  1785. }
  1786. }
  1787. // copy surface data in matching resolution to run directory
  1788. if (model == PLASIM)
  1789. {
  1790. if (Planet == MARS)
  1791. sprintf(command,"cp plasim/dat/T%d_mars/* plasim/run/\n",Truncation);
  1792. else if (Planet == EXO)
  1793. sprintf(command,"cp plasim/dat/T%d_exo/* plasim/run/\n",Truncation);
  1794. else
  1795. sprintf(command,"cp plasim/dat/T%d/* plasim/run/\n",Truncation);
  1796. system(command);
  1797. if (Lsg) // copy data for LSG ocean model and select LSG GUI configuration
  1798. {
  1799. sprintf(command,"cp lsg/dat/* %s/run\n",shomo);
  1800. system(command);
  1801. sprintf(command,"cp %s/dat/GUI_LSG.cfg %s/run/GUI.cfg\n",shomo,shomo);
  1802. system(command);
  1803. }
  1804. else
  1805. {
  1806. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1807. system(command);
  1808. }
  1809. }
  1810. else
  1811. {
  1812. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1813. system(command);
  1814. }
  1815. // copy second GUI configuration for two-instances run
  1816. if (Multirun > 1)
  1817. {
  1818. sprintf(command,"cp %s/dat/GUI_0?.cfg %s/run\n",shomo,shomo);
  1819. system(command);
  1820. }
  1821. return 1; /* Success */
  1822. }
  1823. int BuildPPP(void)
  1824. {
  1825. int i,l,x,y;
  1826. FILE *fp;
  1827. char script_backup[256];
  1828. char command[256];
  1829. char message[256];
  1830. char bld[256];
  1831. char res[256];
  1832. strcpy(bld,ShortModelName[Model]);
  1833. strcat(bld,"/bld/");
  1834. strcpy(res,bld);
  1835. strcat(bld,build_ppp);
  1836. strcat(res,res_name);
  1837. strcpy(script_backup,bld);
  1838. strcat(script_backup,".bak");
  1839. rename(bld,script_backup);
  1840. fp = fopen(bld,"w");
  1841. if (fp == NULL)
  1842. {
  1843. printf("Could not open file <%s> for writing\n",bld);
  1844. return 0; /* Failure */
  1845. }
  1846. fputs("#!/bin/bash\n",fp);
  1847. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1848. fputs("[ $# == 1 ] && cd $1\n",fp);
  1849. fputs("rm -f *.o *.mod\n",fp);
  1850. fputs("cp ../src/make_ppp .\n",fp);
  1851. fputs("cp ../src/*.f90 .\n",fp);
  1852. fputs("cat ../../most_compiler",fp);
  1853. if (ndebug) fputs(" ../../most_debug_options",fp);
  1854. if (nprec) fputs(" ../../most_precision_options",fp);
  1855. fprintf(fp," make_ppp > makefile_ppp\n");
  1856. fputs("make -e -f makefile_ppp\n",fp);
  1857. fprintf(fp,"cp ppp.x ../bin/%s\n",exec_ppp);
  1858. fclose(fp);
  1859. sprintf(command,"chmod a+x %s",bld);
  1860. system(command);
  1861. sprintf(command,"%s/bin/%s",ShortModelName[Model],exec_ppp);
  1862. if (!(fp = fopen(command,"r")))
  1863. {
  1864. sprintf(message,"Building preprocessor ppp");
  1865. BlueMessage(message);
  1866. strcat(bld," ");
  1867. strcat(bld,ShortModelName[Model]);
  1868. strcat(bld,"/bld");
  1869. system(bld);
  1870. }
  1871. else fclose(fp); // Executable exists already
  1872. sprintf(command,"cp %s/bin/%s %s/run\n",ShortModelName[Model],exec_ppp,ShortModelName[Model]);
  1873. system(command);
  1874. return 1; /* Success */
  1875. }
  1876. void FinishLine(void)
  1877. {
  1878. char text[80];
  1879. int OldCores;
  1880. OldCores = Cores;
  1881. if (CursorSel == NULL) return;
  1882. if (CursorSel->type == SEL_INT)
  1883. {
  1884. CursorSel->iv = atoi(CursorSel->teva);
  1885. sprintf(text,"%10d",CursorSel->iv);
  1886. strcpy(CursorSel->teva,text+10-CursorSel->edco);
  1887. if (CursorSel->piv) *CursorSel->piv = CursorSel->iv;
  1888. }
  1889. if (CursorSel->type == SEL_REAL)
  1890. {
  1891. CursorSel->fv = atof(CursorSel->teva);
  1892. FormatReal(CursorSel->fv,text);
  1893. strcpy(CursorSel->teva,text);
  1894. }
  1895. /*
  1896. if (CursorSel == SelMulti) // Enable or disable Lat2
  1897. {
  1898. SelLat2->no = (SelMulti->iv != 2) ;
  1899. }
  1900. */
  1901. if (OldCores == 1 && Cores > 1) ForceRebuild = 1;
  1902. if (OldCores > 1 && Cores == 1) ForceRebuild = 1;
  1903. if (Model == PLASIM && OldCores != Cores) ForceRebuild = 1;
  1904. }
  1905. int CheckPumaNamelist(void)
  1906. {
  1907. int i,safe_ntspd;
  1908. int *ntspd;
  1909. int *nyoden = NULL;
  1910. double s;
  1911. struct SelStruct *Sel;
  1912. FILE *fp;
  1913. FinishLine();
  1914. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1915. {
  1916. if (Sel->piv) *Sel->piv = Sel->iv;
  1917. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1918. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1919. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1920. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1921. }
  1922. // Check for resolution defines as T value
  1923. if (Resolution > 0) Latitudes = ResLat[Resolution];
  1924. // Check # of latitudes for correct values (FFT requirements)
  1925. /*
  1926. if (Latitudes >= 2048) Latitudes = 2048; // T1365
  1927. else if (Latitudes >= 1024) Latitudes = 1024; // T682
  1928. else if (Latitudes >= 512) Latitudes = 512; // T341
  1929. else if (Latitudes >= 256) Latitudes = 256; // T170
  1930. else if (Latitudes >= 192) Latitudes = 192; // T127
  1931. else if (Latitudes >= 160) Latitudes = 160; // T106
  1932. else if (Latitudes >= 128) Latitudes = 128; // T85
  1933. else if (Latitudes >= 96) Latitudes = 96; // T63
  1934. else if (Latitudes >= 64) Latitudes = 64; // T42
  1935. else if (Latitudes >= 48) Latitudes = 48; // T31
  1936. else if (Latitudes >= 32) Latitudes = 32; // T21
  1937. else if (Latitudes >= 24) Latitudes = 24; // T15
  1938. else Latitudes = 16; // T10
  1939. */
  1940. if (Debug) printf("Latitudes = %d\n",Latitudes);
  1941. // Force # of levels to 20 for predefined Yoden profiles
  1942. if (nyoden && *nyoden > 0)
  1943. {
  1944. Levels = 20;
  1945. Yoden = *nyoden;
  1946. }
  1947. // Make sure # of CPU's is a power of 2 and in the proper range
  1948. if (Cores > Latitudes/2) Cores = Latitudes/2;
  1949. if (Cores < 1) Cores = 1;
  1950. if ((Latitudes/2) % Cores != 0) Cores = 1;
  1951. // Multirun is currently implemented for two runs only
  1952. if (Multirun >= 2)
  1953. {
  1954. Multirun = SelMulti->iv = 2;
  1955. Cores = SelCPU->iv = 1;
  1956. }
  1957. return 0; /* Success */
  1958. }
  1959. int CheckCATNamelist(void)
  1960. {
  1961. int i,safe_ntspd;
  1962. int *ntspd;
  1963. int *nyoden = NULL;
  1964. double s;
  1965. struct SelStruct *Sel;
  1966. FILE *fp;
  1967. FinishLine();
  1968. /*
  1969. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1970. {
  1971. if (Sel->piv) *Sel->piv = Sel->iv;
  1972. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1973. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1974. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1975. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1976. }
  1977. */
  1978. // Check for resolution
  1979. if (Resolution == 1)
  1980. Latitudes = Latitude2 = 64;
  1981. else if (Resolution == 2)
  1982. Latitudes = Latitude2 = 256;
  1983. else if (Resolution == 3)
  1984. Latitudes = Latitude2 = 1024;
  1985. if (Debug) printf("NGX = NGY = %d\n",Latitudes);
  1986. return 0; /* Success */
  1987. }
  1988. int CheckPlasimNamelist(void)
  1989. {
  1990. int i,safe_mpstep;
  1991. int *mpstep;
  1992. double s;
  1993. struct SelStruct *Sel;
  1994. FILE *fp;
  1995. FinishLine();
  1996. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1997. {
  1998. if (Sel->piv) *Sel->piv = Sel->iv;
  1999. if (!strcmp(Sel->text,"MPSTEP")) mpstep = &Sel->iv;
  2000. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  2001. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  2002. }
  2003. // Check for resolution defines as T value
  2004. if (Resolution > 0) Latitudes = ResLat[Resolution];
  2005. // LSG works currently only with T21 PlaSim
  2006. if (Lsg)
  2007. {
  2008. Resolution = RES_T21;
  2009. Latitudes = ResLat[RES_T21];
  2010. }
  2011. // Check # of latitudes for correct values (FFT requirements)
  2012. if (Latitudes >= 64) Latitudes = 64; // T42
  2013. else if (Latitudes >= 48) Latitudes = 48; // T31
  2014. else if (Latitudes >= 32) Latitudes = 32; // T21
  2015. else if (Latitudes >= 4) Latitudes = 4; // T2
  2016. else if (Latitudes >= 2) Latitudes = 2; // T1
  2017. // Set mpstep to a safe value
  2018. if (Resolution > 0) *mpstep = PlasimSteps[Resolution];
  2019. else
  2020. {
  2021. s = log(Latitudes / 32.0) / log(2.0);
  2022. safe_mpstep = 45.0 / pow(3.0,s);
  2023. if (*mpstep > safe_mpstep) *mpstep = safe_mpstep;
  2024. }
  2025. // Make sure # of CPU's is a power of 2 and in the proper range
  2026. if (Cores > Latitudes) Cores = Latitudes;
  2027. if (Cores < 1) Cores = 1;
  2028. if (Latitudes % Cores != 0) Cores = 1;
  2029. return 0; /* Success */
  2030. }
  2031. double *ReadGrid(const char *fn, int klat, double Sc)
  2032. {
  2033. int i,j,k,jlev,iteml,gridsize;
  2034. FILE *ifp;
  2035. int head[8];
  2036. double *gp;
  2037. char Line[256];
  2038. char Elem[16];
  2039. nlat = klat;
  2040. nlon = nlat + nlat;
  2041. VGAX = (double)sfbox_w / nlon;
  2042. VGAY = (double)sfbox_h / (nlat-1);
  2043. gridsize = (nlon+1) * nlat * sizeof(double);
  2044. gp = malloc(gridsize);
  2045. ifp = fopen(fn,"r");
  2046. if (!ifp) return NULL;
  2047. fgets(Line,sizeof(Line),ifp);
  2048. for (i = 0 ; i < 8 ; ++i)
  2049. {
  2050. strncpy(Elem,Line+i*10,10);
  2051. Elem[10] = 0;
  2052. head[i] = atoi(Elem);
  2053. }
  2054. if (head[4] == nlon && head[5] == nlat)
  2055. {
  2056. for (j = 0 ; j < nlat ; ++j)
  2057. {
  2058. for (i = 0 ; i < nlon ; i+=8)
  2059. {
  2060. fgets(Line,sizeof(Line),ifp);
  2061. iteml = strlen(Line) / 8;
  2062. for (k=0 ; k < 8 ; ++k)
  2063. {
  2064. strncpy(Elem,Line+k*iteml,iteml);
  2065. Elem[iteml] = 0;
  2066. gp[k+i+j*(nlon+1)] = atof(Elem) * Sc;
  2067. }
  2068. }
  2069. gp[nlon+j*(nlon+1)] = gp[j*(nlon+1)];
  2070. }
  2071. }
  2072. else printf("head[4] = %d head[5] = %d\n",head[4],head[5]);
  2073. fclose(ifp);
  2074. return gp;
  2075. }
  2076. void WriteOroNamelist(FILE *fp)
  2077. {
  2078. int lon1,lon2,lat1,lat2;
  2079. double w,h;
  2080. w = Frame[0].w;
  2081. h = Frame[0].h;
  2082. // Round to next gridpoint
  2083. lon1 = 1 + (Frame[0].xs * Latitudes*2) / w;
  2084. lon2 = 1 +((Frame[0].xs + Frame[0].ws) * Latitudes*2) / w;
  2085. lat1 = 1 + (Frame[0].ys * Latitudes) / h;
  2086. lat2 = 1 +((Frame[0].ys + Frame[0].hs) * Latitudes) / h;
  2087. fprintf(fp," %-8s=%d\n","NSRV" , 0);
  2088. fprintf(fp," %-8s=%d\n","NORO" ,noro);
  2089. fprintf(fp," %-8s=%d\n","OROANO" ,OroAno);
  2090. fprintf(fp," %-8s=%d\n","LON1ORO",lon1);
  2091. fprintf(fp," %-8s=%d\n","LON2ORO",lon2);
  2092. fprintf(fp," %-8s=%d\n","LAT1ORO",lat1);
  2093. fprintf(fp," %-8s=%d\n","LAT2ORO",lat2);
  2094. fprintf(fp," %-8s=%d\n","TGRANO" ,TgrAno);
  2095. lon1 = 1 + (Frame[1].xs * Latitudes*2) / Frame[1].w;
  2096. lon2 = 1 +((Frame[1].xs + Frame[1].ws) * Latitudes*2) / Frame[1].w;
  2097. lat1 = 1 + (Frame[1].ys * Latitudes) / Frame[1].h;
  2098. lat2 = 1 +((Frame[1].ys + Frame[1].hs) * Latitudes) / Frame[1].h;
  2099. fprintf(fp," %-8s=%d\n","LON1TGR",lon1);
  2100. fprintf(fp," %-8s=%d\n","LON2TGR",lon2);
  2101. fprintf(fp," %-8s=%d\n","LAT1TGR",lat1);
  2102. fprintf(fp," %-8s=%d\n","LAT2TGR",lat2);
  2103. return; /* Success */
  2104. }
  2105. void WriteResolutionNamelist(void)
  2106. {
  2107. int i;
  2108. char nln[256];
  2109. FILE *fp;
  2110. for (i = 0 ; i < Multirun ; ++i)
  2111. {
  2112. sprintf(nln,"%s/run/resolution_namelist",ShortModelName[Model]);
  2113. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",i);
  2114. fp = fopen(nln,"w");
  2115. if (fp == NULL)
  2116. {
  2117. printf("Could not open file <%s> for writing\n",nln);
  2118. return; /* Failure */
  2119. }
  2120. fprintf(fp," &RES\n");
  2121. fprintf(fp," NLAT = %d\n" ,Latitudes);
  2122. if (Model != SAM) fprintf(fp," NLEV = %d\n" ,Levels);
  2123. fprintf(fp," /END\n");
  2124. fclose(fp);
  2125. }
  2126. }
  2127. /* ================= */
  2128. /* WritePumaNamelist */
  2129. /* ================= */
  2130. int WritePumaNamelist(void) /* also used for model SAM */
  2131. {
  2132. int i,j,k,sum,val,imr;
  2133. FILE *fp;
  2134. char backup_name[256];
  2135. char nln[256];
  2136. struct SelStruct *Sel;
  2137. FinishLine();
  2138. // Write file <puma_namelist> or <sam_namelist>
  2139. for (imr = 0 ; imr < Multirun ; ++imr)
  2140. {
  2141. strcpy(nln,ShortModelName[Model]);
  2142. strcat(nln,"/run/");
  2143. strcat(nln,namelist_name);
  2144. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",imr);
  2145. fp = fopen(nln,"w");
  2146. if (fp == NULL)
  2147. {
  2148. printf("Could not open file <%s> for writing\n",nln);
  2149. return 0; /* Failure */
  2150. }
  2151. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2152. if (Preprocessed || noro) nreadsr = 1;
  2153. fprintf(fp," %-8s=%6d\n","NOUTPUT" ,noutput);
  2154. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2155. if (nac)
  2156. {
  2157. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2158. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2159. }
  2160. else
  2161. {
  2162. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2163. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2164. }
  2165. if (ngui) fprintf(fp," %-8s=%6d\n","NYEARS",SimYears);
  2166. else fprintf(fp," %-8s=%6d\n","NYEARS",1);
  2167. fprintf(fp," %-8s=%6d\n","NMONTHS",0);
  2168. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2169. {
  2170. if (Sel->type == SEL_INT)
  2171. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2172. if (Sel->type == SEL_REAL)
  2173. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2174. }
  2175. // Check for mode selections
  2176. for (i=0,sum=0 ; i < DimSH ; ++i) sum += Ampli[i];
  2177. if (sum != DimSH) // Some modes are off
  2178. {
  2179. fprintf(fp," NSPECSEL = ");
  2180. i = 0;
  2181. while (i < DimSH)
  2182. {
  2183. val = Ampli[i];
  2184. j = i;
  2185. while (j < DimSH && Ampli[j] == val) ++j;
  2186. k = j - i;
  2187. if (k == 1) fprintf(fp,"%d",val);
  2188. else fprintf(fp,"%d*%d",k,val);
  2189. if (j < DimSH) fprintf(fp,",");
  2190. i = j;
  2191. }
  2192. fprintf(fp,"\n");
  2193. }
  2194. fprintf(fp," /END\n");
  2195. fclose(fp);
  2196. }
  2197. return 1; /* Success */
  2198. }
  2199. /* ================ */
  2200. /* WriteCatNamelist */
  2201. /* ================ */
  2202. int WriteCatNamelist(void)
  2203. {
  2204. int i,j,k,sum,val;
  2205. FILE *fp;
  2206. char backup_name[256];
  2207. char nln[256];
  2208. struct SelStruct *Sel;
  2209. FinishLine();
  2210. // Write file <cat_namelist>
  2211. strcpy(nln,ShortModelName[Model]);
  2212. strcat(nln,"/run/");
  2213. strcat(nln,namelist_name);
  2214. fp = fopen(nln,"w");
  2215. if (fp == NULL)
  2216. {
  2217. printf("Could not open file <%s> for writing\n",nln);
  2218. return 0; /* Failure */
  2219. }
  2220. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2221. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2222. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2223. {
  2224. if (Sel->type == SEL_INT)
  2225. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2226. if (Sel->type == SEL_REAL)
  2227. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2228. if (!strncmp(Sel->text,"NSIM",4))
  2229. {
  2230. if (Sel->iv > 0) CatSim = Sel->iv;
  2231. }
  2232. }
  2233. fprintf(fp," /END\n");
  2234. fclose(fp);
  2235. return 1; /* Success */
  2236. }
  2237. /* ================ */
  2238. /* WritePPPNamelist */
  2239. /* ================ */
  2240. int WritePPPNamelist(void)
  2241. {
  2242. int i,j,k,sum,val;
  2243. FILE *fp;
  2244. char backup_name[256];
  2245. char nln[256];
  2246. struct SelStruct *Sel;
  2247. FinishLine();
  2248. WriteResolutionNamelist();
  2249. /* 2. Write file <ppp_namelist> */
  2250. strcpy(nln,ShortModelName[Model]);
  2251. strcat(nln,"/run/");
  2252. strcat(nln,"ppp_namelist");
  2253. strcpy(backup_name,nln);
  2254. strcat(backup_name,".bak");
  2255. rename(nln,backup_name);
  2256. fp = fopen(nln,"w");
  2257. if (fp == NULL)
  2258. {
  2259. printf("Could not open file <%s> for writing\n",nln);
  2260. return 0; /* Failure */
  2261. }
  2262. fprintf(fp," &ppp_nl\n");
  2263. if (Preprocessed || noro) nreadsr = 1;
  2264. fprintf(fp," %-8s=%d\n","NOUTPUT" ,noutput);
  2265. fprintf(fp," %-8s=%d\n","NGUI" ,ngui);
  2266. if (nac)
  2267. {
  2268. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2269. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2270. }
  2271. else
  2272. {
  2273. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2274. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2275. }
  2276. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2277. {
  2278. if (Sel->type == SEL_INT)
  2279. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2280. if (Sel->type == SEL_REAL)
  2281. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2282. }
  2283. WriteOroNamelist(fp);
  2284. fprintf(fp," /END\n");
  2285. fclose(fp);
  2286. return 1; /* Success */
  2287. }
  2288. /* ======== */
  2289. /* FillPoly */
  2290. /* ======== */
  2291. void FillPoly(int n, double Poly[])
  2292. {
  2293. int i;
  2294. XPoint xpol[8];
  2295. for (i=0; i < n ; ++i)
  2296. {
  2297. xpol[i].x = OffX + Poly[i+i ] + 0.5;
  2298. xpol[i].y = OffY + Poly[i+i+1] + 0.5;
  2299. }
  2300. XFillPolygon(display,pix,gc,xpol,n,Convex,CoordModeOrigin);
  2301. }
  2302. /* ======= */
  2303. /* IsoArea */
  2304. /* ======= */
  2305. void IsoArea(INT y, double vl, double vh, double Top[], double Bot[], INT Dim)
  2306. {
  2307. INT f,x,p;
  2308. double xl,xr,yt,yb;
  2309. double Poly[16];
  2310. for (x=0 ; x < Dim-1 ; x++)
  2311. {
  2312. Flag[x] = 0;
  2313. if (Top[x ] < vl) Flag[x] |= TOLELO;
  2314. if (Top[x ] >= vh) Flag[x] |= TOLEHI;
  2315. if (Top[x+1] < vl) Flag[x] |= TORILO;
  2316. if (Top[x+1] >= vh) Flag[x] |= TORIHI;
  2317. if (Bot[x ] < vl) Flag[x] |= BOLELO;
  2318. if (Bot[x ] >= vh) Flag[x] |= BOLEHI;
  2319. if (Bot[x+1] < vl) Flag[x] |= BORILO;
  2320. if (Bot[x+1] >= vh) Flag[x] |= BORIHI;
  2321. }
  2322. x = 0;
  2323. while (x < Dim-1)
  2324. {
  2325. xl = VGAX * x ;
  2326. xr = VGAX * (x+1);
  2327. yt = VGAY * y ;
  2328. yb = VGAY * (y+1);
  2329. f = Flag[x];
  2330. if (f == 0)
  2331. {
  2332. x++;
  2333. while (x < Dim-1 && Flag[x] == 0)
  2334. {
  2335. x++;
  2336. xr = VGAX * x;
  2337. }
  2338. Poly[0] = Poly[6] = xl;
  2339. Poly[1] = Poly[3] = yt;
  2340. Poly[2] = Poly[4] = xr;
  2341. Poly[5] = Poly[7] = yb;
  2342. FillPoly(4,Poly);
  2343. }
  2344. else if (f == (TOLELO | TORILO | BOLELO | BORILO) ||
  2345. f == (TOLEHI | TORIHI | BOLEHI | BORIHI)) x++;
  2346. else if (Top[x] < vl && Top[x+1] >= vl && Bot[x] >= vl && Bot[x+1] < vl)
  2347. {
  2348. Poly[1] = Poly[3] = yt;
  2349. Poly[2] = Poly[4] = xr;
  2350. Poly[0] = IPX(Top[x ],vl,Top[x+1]);
  2351. Poly[5] = IPY(Top[x+1],vl,Bot[x+1]);
  2352. FillPoly(3,Poly);
  2353. Poly[1] = Poly[3] = yb;
  2354. Poly[2] = Poly[4] = xl;
  2355. Poly[0] = IPX(Bot[x ],vl,Bot[x+1]);
  2356. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2357. FillPoly(3,Poly);
  2358. ++x;
  2359. }
  2360. else if (Top[x] >= vl && Top[x+1] < vl && Bot[x] < vl && Bot[x+1] >= vl)
  2361. {
  2362. Poly[0] = Poly[4] = xl;
  2363. Poly[1] = Poly[3] = yt;
  2364. Poly[2] = IPX(Top[x ],vl,Top[x+1]);
  2365. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2366. FillPoly(3,Poly);
  2367. Poly[0] = Poly[2] = xr;
  2368. Poly[3] = Poly[5] = yb;
  2369. Poly[1] = IPY(Top[x+1],vl,Bot[x+1]);
  2370. Poly[4] = IPX(Bot[x ],vl,Bot[x+1]);
  2371. FillPoly(3,Poly);
  2372. ++x;
  2373. }
  2374. else
  2375. {
  2376. p = 0;
  2377. if (Top[x] < vl)
  2378. {
  2379. if (Top[x+1] >= vl)
  2380. {
  2381. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2382. Poly[p++] = yt;
  2383. }
  2384. if (Top[x+1] >= vh)
  2385. {
  2386. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2387. Poly[p++] = yt;
  2388. }
  2389. }
  2390. else if (Top[x] >= vh)
  2391. {
  2392. if (Top[x+1] < vh)
  2393. {
  2394. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2395. Poly[p++] = yt;
  2396. }
  2397. if (Top[x+1] < vl)
  2398. {
  2399. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2400. Poly[p++] = yt;
  2401. }
  2402. }
  2403. else
  2404. {
  2405. Poly[p++] = xl;
  2406. Poly[p++] = yt;
  2407. if (Top[x+1] < vl)
  2408. {
  2409. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2410. Poly[p++] = yt;
  2411. }
  2412. if (Top[x+1] >= vh)
  2413. {
  2414. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2415. Poly[p++] = yt;
  2416. }
  2417. }
  2418. if (Top[x+1] < vl)
  2419. {
  2420. if (Bot[x+1] >= vl)
  2421. {
  2422. Poly[p++] = xr;
  2423. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2424. }
  2425. if (Bot[x+1] >= vh)
  2426. {
  2427. Poly[p++] = xr;
  2428. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2429. }
  2430. }
  2431. else if (Top[x+1] >= vh)
  2432. {
  2433. if (Bot[x+1] < vh)
  2434. {
  2435. Poly[p++] = xr;
  2436. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2437. }
  2438. if (Bot[x+1] < vl)
  2439. {
  2440. Poly[p++] = xr;
  2441. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2442. }
  2443. }
  2444. else
  2445. {
  2446. Poly[p++] = xr;
  2447. Poly[p++] = yt;
  2448. if (Bot[x+1] < vl)
  2449. {
  2450. Poly[p++] = xr;
  2451. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2452. }
  2453. if (Bot[x+1] >= vh)
  2454. {
  2455. Poly[p++] = xr;
  2456. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2457. }
  2458. }
  2459. if (Bot[x+1] < vl)
  2460. {
  2461. if (Bot[x] >= vl)
  2462. {
  2463. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2464. Poly[p++] = yb;
  2465. }
  2466. if (Bot[x] >= vh)
  2467. {
  2468. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2469. Poly[p++] = yb;
  2470. }
  2471. }
  2472. else if (Bot[x+1] >= vh)
  2473. {
  2474. if (Bot[x] < vh)
  2475. {
  2476. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2477. Poly[p++] = yb;
  2478. }
  2479. if (Bot[x] < vl)
  2480. {
  2481. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2482. Poly[p++] = yb;
  2483. }
  2484. }
  2485. else
  2486. {
  2487. Poly[p++] = xr;
  2488. Poly[p++] = yb;
  2489. if (Bot[x] < vl)
  2490. {
  2491. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2492. Poly[p++] = yb;
  2493. }
  2494. if (Bot[x] >= vh)
  2495. {
  2496. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2497. Poly[p++] = yb;
  2498. }
  2499. }
  2500. if (Bot[x] < vl)
  2501. {
  2502. if (Top[x] >= vl)
  2503. {
  2504. Poly[p++] = xl;
  2505. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2506. }
  2507. if (Top[x] >= vh)
  2508. {
  2509. Poly[p++] = xl;
  2510. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2511. }
  2512. }
  2513. else if (Bot[x] >= vh)
  2514. {
  2515. if (Top[x] < vh)
  2516. {
  2517. Poly[p++] = xl;
  2518. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2519. }
  2520. if (Top[x] < vl)
  2521. {
  2522. Poly[p++] = xl;
  2523. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2524. }
  2525. }
  2526. else
  2527. {
  2528. Poly[p++] = xl;
  2529. Poly[p++] = yb;
  2530. if (Top[x] < vl)
  2531. {
  2532. Poly[p++] = xl;
  2533. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2534. }
  2535. if (Top[x] >= vh)
  2536. {
  2537. Poly[p++] = xl;
  2538. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2539. }
  2540. }
  2541. FillPoly(p>>1,Poly);
  2542. x++;
  2543. }
  2544. }
  2545. }
  2546. /* ======== */
  2547. /* IsoAreas */
  2548. /* ======== */
  2549. void IsoAreas(double *Field, int DimX, int DimY, struct ColorStrip Strip[])
  2550. {
  2551. INT i;
  2552. INT y;
  2553. double *Top;
  2554. double *Bot;
  2555. i = 0;
  2556. while (Strip[i].Name)
  2557. {
  2558. Top = Field;
  2559. Bot = Field + DimX;
  2560. XSetForeground(display,gc,Strip[i].pixel);
  2561. for (y = 0 ; y < DimY-1 ; y++)
  2562. {
  2563. IsoArea(y,Strip[i].Lo,Strip[i].Hi,Top,Bot,DimX);
  2564. Top += DimX;
  2565. Bot += DimX;
  2566. }
  2567. ++i;
  2568. }
  2569. }
  2570. /* ========= */
  2571. /* CalcFrame */
  2572. /* ========= */
  2573. int CalcFrame(int lats)
  2574. {
  2575. // int i,l,x,y;
  2576. // double *Grid;
  2577. //
  2578. // nlat = lats;
  2579. // nlon = lats * 2;
  2580. // VGAX = (sfbox_w - 1.0) / nlon;
  2581. // VGAY = (sfbox_h - 1.0) / (nlat-1);
  2582. //
  2583. // if (!Flag) Flag = malloc((nlon+1) * sizeof(int));
  2584. // if (!Frame[0].Grid) Frame[0].Grid = malloc(nlon * nlat * nlev * sizeof(double));
  2585. // if (!Frame[0].pixmap) Frame[0].pixmap = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2586. // pix = Frame[0].pixmap;
  2587. // Grid = Frame[0].Grid;
  2588. // IsoAreas(Grid,nlon+1,nlat,Frame[0].Strip);
  2589. return 0;
  2590. }
  2591. Pixmap IsoPixmap(double *Grid, int nlon, int nlat, struct ColorStrip Strip[])
  2592. {
  2593. pix = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2594. //XFillRectangle(display,Cow,gc,0,0,Frame[0].w,Frame[0].h);
  2595. Flag = realloc(Flag,(nlon+1) * sizeof(int));
  2596. IsoAreas(Grid,nlon+1,nlat,Strip);
  2597. //XSetForeground(display,gc,Blue.pixel);
  2598. //XDrawRectangle(display,Cow,gc,Frame[0].x-1,Frame[0].y-1,Frame[0].w,Frame[0].h);
  2599. return pix;
  2600. }
  2601. int PPPCompiled(void)
  2602. {
  2603. FILE *fp;
  2604. char fn[128];
  2605. strcpy(fn,"puma/run/");
  2606. strcat(fn,exec_ppp);
  2607. if ((fp = fopen(fn,"r")))
  2608. {
  2609. fclose(fp);
  2610. return 1;
  2611. }
  2612. return 0;
  2613. }
  2614. void ShowOrography(void)
  2615. {
  2616. int i,m,n,k,l,x,y,h,w,lon1,lon2,lat1,lat2,tx,ty,dx;
  2617. int r,len,width,height,xp,yp;
  2618. char Text[80];
  2619. double flon,flat;
  2620. XSetFont(display, gc, FixFont->fid);
  2621. XSetForeground(display,gc,TextC);
  2622. XSetBackground(display,gc,WinBG);
  2623. if (Model == PLASIM)
  2624. {
  2625. if (Planet == MARS)
  2626. {
  2627. XCopyArea(display,OpmMars,Cow,gc,0,0,
  2628. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2629. }
  2630. else if (Planet == EARTH)
  2631. {
  2632. XCopyArea(display,OpmEarth,Cow,gc,0,0,
  2633. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2634. }
  2635. return;
  2636. }
  2637. k = 0; // Frame number 0 reserved for topography
  2638. if (noro == 0) // No orography
  2639. {
  2640. XSetForeground(display,gc,Blue.pixel);
  2641. XFillRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2642. XSetForeground(display,gc,WhitePix);
  2643. XSetBackground(display,gc,Blue.pixel);
  2644. l = strlen("Aqua Planet Setup");
  2645. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2646. ty = Frame[k].y + Frame[k].h / 2 - FixFontHeight;
  2647. XDrawImageString(display,Cow,gc,tx,ty,"Aqua Planet Setup",l);
  2648. l = strlen("To change mark option 'Orography'");
  2649. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2650. ty += FixFontHeight;
  2651. XDrawImageString(display,Cow,gc,tx,ty,"To change mark option 'Orography'",l);
  2652. XSetBackground(display,gc,WinBG);
  2653. }
  2654. else
  2655. {
  2656. XCopyArea(display,OpmPrep,Cow,gc,0,0,Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2657. XDrawRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2658. l = strlen(Frame[k].t[0]);
  2659. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2660. ty = Frame[k].y - FixFont->descent - 2;
  2661. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2662. }
  2663. if (noro && (Button1Down || (Frame[k].ws && Frame[k].hs)))
  2664. {
  2665. x = Frame[k].x+Frame[k].xs;
  2666. y = Frame[k].y+Frame[k].ys;
  2667. w = Frame[k].ws;
  2668. h = Frame[k].hs;
  2669. if (w < 0) {x += w; w = -w;}
  2670. if (h < 0) {y += h; h = -h;}
  2671. XSetForeground(display,gc,WhitePix);
  2672. XDrawArc(display,Cow,gc,x,y,w,h,0,FULLARC);
  2673. XSetForeground(display,gc,BlackPix);
  2674. XDrawArc(display,Cow,gc,x-1,y-1,w+2,h+2,0,FULLARC);
  2675. flon = 360.0 / Frame[k].w;
  2676. flat = 180.0 / Frame[k].h;
  2677. lon1 = flon * (x - Frame[k].x);
  2678. lon2 = lon1 + flon * w;
  2679. lat1 = 90.0 - flat * (y - Frame[k].y);
  2680. lat2 = lat1 - flat * h;
  2681. sprintf(Frame[k].t[1],"Lon: (%3d:%3d) Lat: (%3d:%3d)",lon1,lon2,lat2,lat1);
  2682. l = strlen(Frame[k].t[1]);
  2683. tx = Frame[k].x;
  2684. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2685. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2686. XSetForeground(display,gc,WhitePix);
  2687. sprintf(Frame[k].t[1],"Mouse marks area ");
  2688. l = strlen(Frame[k].t[1]);
  2689. tx = Frame[k].x;
  2690. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2691. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2692. }
  2693. }
  2694. void ShowMars(void)
  2695. {
  2696. if (Debug) printf("ShowMars\n");
  2697. XPutImage(display,Cow,gc,MapLRM.X,0,0,Frame[1].x,Frame[1].y,MapLRM.w,MapLRM.h);
  2698. }
  2699. void ShowExo(void)
  2700. {
  2701. XPutImage(display,Cow,gc,MapLRL.X,0,0,Frame[0].x,Frame[0].y,MapLRL.w,MapLRL.h);
  2702. XPutImage(display,Cow,gc,MapLRK.X,0,0,Frame[1].x,Frame[1].y,MapLRK.w,MapLRK.h);
  2703. }
  2704. void ShowEarth(void)
  2705. {
  2706. if (Debug) printf("ShowEarth\n");
  2707. XPutImage(display,Cow,gc,MapLRE.X,0,0,Frame[1].x,Frame[1].y,MapLRE.w,MapLRE.h);
  2708. }
  2709. void ShowModeSelector(void)
  2710. {
  2711. int k,dx,r,m,i,n,len,width,height,xp,yp,l,tx,ty;
  2712. char Text[80];
  2713. if (Debug) printf("ShowModeSelector\n");
  2714. k = 1; // Frame number 1 reserved for mode display
  2715. pix = Frame[k].pixmap;
  2716. strcpy(Frame[1].t[0],"Spherical Harmonics mode selector");
  2717. dx = dxsh;
  2718. r = (dx+dx) / 3;
  2719. XSetForeground(display,gc,BlackPix);
  2720. XFillRectangle(display,pix,gc,0,0,Frame[k].w,Frame[k].h);
  2721. for (m=0,i=0 ; m <= DimTr ; ++m)
  2722. {
  2723. for (n=m ; n <= DimTr ; ++n,++i)
  2724. {
  2725. if (Ampli[i]) XSetForeground(display,gc,Green.pixel);
  2726. else XSetForeground(display,gc, Red.pixel);
  2727. XFillArc(display,pix,gc,ModeX[i]-dxs2,ModeY[i]-dxs2,r,r,0,360*64);
  2728. }
  2729. }
  2730. /* Draw mode legend */
  2731. XSetForeground(display,gc,Yellow.pixel);
  2732. XSetBackground(display,gc,BlackPix);
  2733. XSetFont(display, gc, ModFont->fid);
  2734. for (i=0 ; i < 21 ; i+=2)
  2735. {
  2736. sprintf(Text,"%d",i);
  2737. len = strlen(Text);
  2738. width = XTextWidth(ModFont,Text,len);
  2739. height = ModFont->ascent + ModFont->descent;
  2740. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  2741. yp = ModFontHeight;
  2742. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2743. }
  2744. strcpy(Text,"PUMA T21 only!");
  2745. len = strlen(Text);
  2746. xp = dx - ModFontWidth/2 + 1;
  2747. yp = 2 * ModFontHeight + 10 * dx;
  2748. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2749. XSetForeground(display,gc,WhitePix);
  2750. strcpy(Text,"MB 1: Toggle mode");
  2751. len = strlen(Text);
  2752. yp += 2*dx;
  2753. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2754. strcpy(Text,"MB 2: Toggle column");
  2755. len = strlen(Text);
  2756. yp += dx;
  2757. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2758. strcpy(Text,"MB 3: Toggle row");
  2759. len = strlen(Text);
  2760. yp += dx;
  2761. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2762. XSetForeground(display,gc,Yellow.pixel);
  2763. strcpy(Text,"n : Total Wavenumber");
  2764. len = strlen(Text);
  2765. xp = dx - ModFontWidth/2 + 1;
  2766. yp = 2 * ModFontHeight + 18 * dx;
  2767. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2768. XSetForeground(display,gc,LightBlue.pixel);
  2769. for (i=0 ; i < 21 ; i+=2)
  2770. {
  2771. sprintf(Text,"%d",i);
  2772. len = strlen(Text);
  2773. width = XTextWidth(ModFont,Text,len);
  2774. height = ModFont->ascent + ModFont->descent;
  2775. xp = Frame[k].w - width - 2;
  2776. yp = 2 * ModFontHeight + i * dx;
  2777. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2778. }
  2779. strcpy(Text,"m : Zonal Wavenumber");
  2780. len = strlen(Text);
  2781. xp = dx - ModFontWidth/2 + 1;
  2782. yp = 2 * ModFontHeight + 19 * dx;
  2783. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2784. XSetForeground(display,gc,Green.pixel);
  2785. strcpy(Text,"Mode is on");
  2786. len = strlen(Text);
  2787. xp = dx - ModFontWidth/2 + 1;
  2788. yp = ModFontHeight + 16 * dx;
  2789. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2790. xp += dx;
  2791. yp += dx;
  2792. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2793. XSetForeground(display,gc,Red.pixel);
  2794. strcpy(Text,"Mode is off");
  2795. len = strlen(Text);
  2796. xp = dx - ModFontWidth/2 + 1;
  2797. yp = ModFontHeight + 17 * dx;
  2798. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2799. xp += dx;
  2800. yp += dx;
  2801. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2802. XSetForeground(display,gc,WhitePix);
  2803. strcpy(Text,"Switch all modes on");
  2804. len = strlen(Text);
  2805. xp = dx - ModFontWidth/2 + 1;
  2806. yp = ModFontHeight + 20 * dx;
  2807. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2808. xp += dx;
  2809. yp += dx;
  2810. XSetForeground(display,gc,Green.pixel);
  2811. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2812. XSetForeground(display,gc,WhitePix);
  2813. strcpy(Text,"Switch all modes off");
  2814. len = strlen(Text);
  2815. xp = dx - ModFontWidth/2 + 1;
  2816. yp = ModFontHeight + 21 * dx;
  2817. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2818. xp += dx;
  2819. yp += dx;
  2820. XSetForeground(display,gc,Red.pixel);
  2821. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2822. XCopyArea(display,Frame[k].pixmap,Cow,gc,0,0,Frame[k].w,Frame[k].h,Frame[k].x,Frame[k].y);
  2823. XSetForeground(display,gc,Cyan.pixel);
  2824. l = strlen(Frame[k].t[0]);
  2825. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2826. ty = Frame[k].y - FixFont->descent - 2;
  2827. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2828. XSetForeground(display,gc,LightBlue.pixel);
  2829. l = strlen(Frame[k].t[1]);
  2830. if (l)
  2831. {
  2832. tx = Frame[k].x + (3 * ModFontWidth) / 2;
  2833. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2834. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2835. }
  2836. }
  2837. void ShowFrame1(void)
  2838. {
  2839. if (Model == PLASIM)
  2840. {
  2841. if (Planet == MARS) ShowMars();
  2842. else if (Planet == EXO ) ShowExo();
  2843. else ShowEarth();
  2844. }
  2845. else ShowModeSelector();
  2846. }
  2847. void PreProcess(void)
  2848. {
  2849. FinishLine();
  2850. if (Model == PUMA)
  2851. {
  2852. if (CheckPumaNamelist()) return;
  2853. GenerateNames();
  2854. WritePPPNamelist();
  2855. WritePumaNamelist();
  2856. if (!PPPCompiled()) BuildPPP();
  2857. WriteRunPPP();
  2858. Preprocessed = 1;
  2859. if (Latitudes < 1000)
  2860. sprintf(oro_name,"puma/run/N%3.3d_surf_0129.sra",Latitudes);
  2861. else
  2862. sprintf(oro_name,"puma/run/N%d_surf_0129.sra",Latitudes);
  2863. OroPrep = ReadGrid(oro_name,Latitudes,RevGra);
  2864. OpmPrep = IsoPixmap(OroPrep,2*Latitudes,Latitudes,OroStrip);
  2865. free(OroPrep);
  2866. OroClear = 0;
  2867. }
  2868. else
  2869. {
  2870. RedMessage("Preprocessing is for PUMA only");
  2871. sleep(5);
  2872. }
  2873. }
  2874. void ClearOro(void)
  2875. {
  2876. OroClear = 1;
  2877. OroAqua = 1;
  2878. OroAno = 0;
  2879. PreProcess();
  2880. }
  2881. void WriteNamelistFile(char *nl, int instance)
  2882. {
  2883. char *shomo = ShortModelName[Model];
  2884. struct SelStruct *Sel;
  2885. char fn[256];
  2886. FILE *fp;
  2887. if (Multirun > 1)
  2888. sprintf(fn,"%s/run/%s_namelist_%2.2d",shomo,nl,instance);
  2889. else
  2890. sprintf(fn,"%s/run/%s_namelist",shomo,nl);
  2891. fp = fopen(fn,"w");
  2892. if (fp == NULL)
  2893. {
  2894. printf("Could not open file <%s> for writing\n",fn);
  2895. return; /* Failure */
  2896. }
  2897. // Write namelist file
  2898. fprintf(fp," &%s_nl\n",nl);
  2899. // Write special parameters
  2900. if (!strcmp(nl,"icemod"))
  2901. {
  2902. fprintf(fp," %-12s=%6d\n","NICE",Ice);
  2903. }
  2904. if (!strcmp(nl,"oceanmod"))
  2905. {
  2906. fprintf(fp," %-12s=%6d\n","NOCEAN",Oce);
  2907. //FL0318
  2908. if (Lsg)
  2909. {
  2910. fprintf(fp," %-12s=%6d\n","NLSG" ,Lsg);
  2911. }
  2912. }
  2913. if (!strcmp(nl,"plasim"))
  2914. {
  2915. fprintf(fp," %-12s=%6d\n","NOUTPUT",noutput);
  2916. fprintf(fp," %-12s=%6d\n","NGUI" ,ngui);
  2917. fprintf(fp," %-12s=%6d\n","N_START_YEAR",SimStart);
  2918. if (Lsg) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",360);
  2919. else if (Planet != MARS) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",365);
  2920. if (ngui) fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",SimYears);
  2921. else fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",1);
  2922. fprintf(fp," %-12s=%6d\n","N_RUN_MONTHS",0);
  2923. fprintf(fp," %-12s=%6d\n","N_RUN_DAYS",0);
  2924. }
  2925. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2926. {
  2927. if (Sel->Item && !strcasecmp(Sel->Item->list,nl))
  2928. {
  2929. if (Sel->type == SEL_TEVA)
  2930. fprintf(fp," %-12s=%c%s%c\n",Sel->text,'\"',Sel->teva,'\"');
  2931. else
  2932. fprintf(fp," %-12s=%s\n",Sel->text,Sel->teva);
  2933. }
  2934. }
  2935. fprintf(fp," /END\n");
  2936. fclose(fp);
  2937. }
  2938. /* =================== */
  2939. /* WritePlasimNamelist */
  2940. /* =================== */
  2941. void WritePlasimNamelist(void)
  2942. {
  2943. int imr;
  2944. FinishLine();
  2945. for (imr = 0 ; imr < Multirun ; ++imr)
  2946. {
  2947. WriteNamelistFile("fluxmod" ,imr);
  2948. WriteNamelistFile("icemod" ,imr);
  2949. WriteNamelistFile("landmod" ,imr);
  2950. WriteNamelistFile("vegmod" ,imr);
  2951. WriteNamelistFile("miscmod" ,imr);
  2952. WriteNamelistFile("oceanmod",imr);
  2953. WriteNamelistFile("planet" ,imr);
  2954. WriteNamelistFile("plasim" ,imr);
  2955. WriteNamelistFile("radmod" ,imr);
  2956. WriteNamelistFile("rainmod" ,imr);
  2957. WriteNamelistFile("seamod" ,imr);
  2958. WriteNamelistFile("surfmod" ,imr);
  2959. }
  2960. }
  2961. void BuildScripts(void)
  2962. {
  2963. if (Model == PUMA)
  2964. {
  2965. if (CheckPumaNamelist()) return;
  2966. GenerateNames();
  2967. WritePumaNamelist();
  2968. if (!Build(PUMA)) Exit();
  2969. WriteRunScript(PUMA);
  2970. }
  2971. if (Model == SAM)
  2972. {
  2973. if (CheckPumaNamelist()) return;
  2974. GenerateNames();
  2975. WritePumaNamelist();
  2976. if (!Build(SAM)) Exit();
  2977. WriteRunScript(SAM);
  2978. }
  2979. if (Model == CAT)
  2980. {
  2981. if (CheckCATNamelist()) return;
  2982. GenerateNames();
  2983. WriteCatNamelist();
  2984. if (!Build(CAT)) Exit();
  2985. WriteRunScript(CAT);
  2986. }
  2987. if (Model == PLASIM)
  2988. {
  2989. CheckPlasimNamelist();
  2990. GenerateNames();
  2991. WritePlasimNamelist();
  2992. if (!Build(PLASIM)) Exit();
  2993. WriteRunScript(PLASIM);
  2994. }
  2995. }
  2996. void SaveExit(void)
  2997. {
  2998. BuildScripts();
  2999. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3000. Exit();
  3001. }
  3002. void SaveRun(void)
  3003. {
  3004. char command[256];
  3005. BuildScripts();
  3006. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3007. sprintf(command,"%s/run/%s %s/run &",ShortModelName[Model],run_name,ShortModelName[Model]);
  3008. system(command);
  3009. printf("\n=== Success: Launched process %s ===\n\n",run_name);
  3010. Exit();
  3011. }
  3012. void MarkRectangle(void)
  3013. {
  3014. if (Model != PUMA) return;
  3015. if (WinEvent.type == ButtonPress)
  3016. {
  3017. Frame[FrameNo].xs = WinEvent.xbutton.x - Frame[FrameNo].x;
  3018. Frame[FrameNo].ys = WinEvent.xbutton.y - Frame[FrameNo].y;
  3019. Frame[FrameNo].ws = 0;
  3020. Frame[FrameNo].hs = 0;
  3021. }
  3022. if (WinEvent.type == ButtonRelease)
  3023. {
  3024. Frame[FrameNo].ws = WinEvent.xbutton.x - Frame[FrameNo].x - Frame[FrameNo].xs;
  3025. Frame[FrameNo].hs = WinEvent.xbutton.y - Frame[FrameNo].y - Frame[FrameNo].ys;
  3026. }
  3027. if (WinEvent.type == MotionNotify && Button1Down)
  3028. {
  3029. Frame[FrameNo].ws = WinEvent.xbutton.x - Frame[FrameNo].x - Frame[FrameNo].xs;
  3030. Frame[FrameNo].hs = WinEvent.xbutton.y - Frame[FrameNo].y - Frame[FrameNo].ys;
  3031. }
  3032. }
  3033. /* ==================================================== */
  3034. /* AzimuthalImage - Display map in azimuthal projection */
  3035. /* ==================================================== */
  3036. void AzimuthalImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3037. {
  3038. int lam; // lambda pixel coordinate in source image
  3039. int phi; // phi pixel coordinate in source image
  3040. int x ; // x pixel coordinate in destination image
  3041. int y ; // y pixel coordinate in destination image
  3042. int dxc; // pixel coordinate of centre position
  3043. int dxl; // pixel coordinate of left most position
  3044. int dxr; // pixel coordinate of right most position
  3045. int dx ; // centre relative x position
  3046. int dy ; // centre relative y position
  3047. int p00; // euqator
  3048. int l00; // reference longitude
  3049. unsigned int dih; // destination image height
  3050. unsigned int diw; // destination image width
  3051. unsigned int dpw; // destination image padded width
  3052. double rad; // pixel radius of new image
  3053. double ysc; // yscale = source height / destination height
  3054. double rho; // distance from centre
  3055. double xpi; // x scale factor = 2 * PI / width
  3056. double ypi; // y scale factor = PI / height
  3057. double xrf;
  3058. double yrf;
  3059. XImage *sX; // source image
  3060. XImage *dX; // destination image
  3061. // Destroy old image structure inclusive data storage
  3062. if (d->X) XDestroyImage(d->X);
  3063. // Set width of new image
  3064. diw = d->w;
  3065. // Pad width of new image to a multiple of 8
  3066. dpw = (diw + 7) & 0xFFF8;
  3067. // Set height of new image
  3068. dih = d->h;
  3069. // Allocate space for image data
  3070. d->d = calloc(dpw * dih,4);
  3071. // Create image structure
  3072. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3073. sX = s->X;
  3074. p00 = s->h >> 1;
  3075. rad = dih >> 1;
  3076. dxc = diw >> 1;
  3077. dxl = MAX(dxc - rad, 0);
  3078. dxr = MIN(dxc + rad, diw);
  3079. xpi = s->w / M_PI * 0.5;
  3080. ypi = s->h / M_PI;
  3081. ysc = (double)s->h / (double)dih;
  3082. l00 = (int)((d->l * s->w) / 360 + s->w/2) % s->w;
  3083. xrf = (double)diw / pow(2.0,31),
  3084. yrf = (double)dih / pow(2.0,31);
  3085. // Paint some stars on the sky
  3086. srandom(Seed);
  3087. for (y = 0 ; y < (diw * dih) >> 8 ; ++y)
  3088. XPutPixel(dX,xrf*random(),yrf*random(),WhitePix);
  3089. for (y = 0 ; y < dih ; ++y)
  3090. {
  3091. dy = y - rad;
  3092. phi = y * ysc;
  3093. for (x = dxl ; x < dxr ; ++x)
  3094. {
  3095. dx = x - dxc;
  3096. rho = sqrt(dx * dx + dy * dy);
  3097. if (rho < rad)
  3098. {
  3099. lam = l00 + xpi * atan2(dx / rad, cos(asin(rho / rad)));
  3100. phi = p00 + ypi * asin(dy / rad);
  3101. XPutPixel(dX,x,y,XGetPixel(sX,lam,phi));
  3102. }
  3103. }
  3104. }
  3105. }
  3106. /* =============================================== */
  3107. /* RectImage - Display map in azimuthal projection */
  3108. /* =============================================== */
  3109. void RectImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3110. {
  3111. int x ; // x pixel coordinate in destination image
  3112. int y ; // y pixel coordinate in destination image
  3113. unsigned int dih; // destination image height
  3114. unsigned int diw; // destination image width
  3115. unsigned int dpw; // destination image padded width
  3116. XImage *sX; // source image
  3117. XImage *dX; // destination image
  3118. // Destroy old image structure inclusive data storage
  3119. if (d->X) XDestroyImage(d->X);
  3120. // Set width of new image
  3121. diw = d->w;
  3122. // Pad width of new image to a multiple of 8
  3123. dpw = (diw + 7) & 0xFFF8;
  3124. // Set height of new image
  3125. dih = d->h;
  3126. // Allocate space for image data
  3127. d->d = calloc(dpw * dih,4);
  3128. // Create image structure
  3129. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3130. sX = s->X;
  3131. for (y = 0 ; y < dih ; ++y)
  3132. {
  3133. for (x = 0 ; x < diw ; ++x)
  3134. {
  3135. XPutPixel(dX,x,y,XGetPixel(sX,x,y));
  3136. }
  3137. }
  3138. }
  3139. void ToggleMode(void)
  3140. {
  3141. int i,j,m,n,mx,my,dx,dy;
  3142. if (Model == PLASIM) // Rotate image
  3143. {
  3144. if (WinEvent.xbutton.button == Button1) // Rotate right
  3145. {
  3146. if (Planet == MARS)
  3147. {
  3148. MapLRM.l += 10;
  3149. if (MapLRM.l > 180) MapLRM.l -= 360;
  3150. AzimuthalImage(&MapHRM,&MapLRM);
  3151. }
  3152. else
  3153. {
  3154. MapLRE.l += 10;
  3155. if (MapLRE.l > 180) MapLRE.l -= 360;
  3156. AzimuthalImage(&MapHRE,&MapLRE);
  3157. }
  3158. }
  3159. else if (WinEvent.xbutton.button == Button3) // Rotate left
  3160. {
  3161. if (Planet == MARS)
  3162. {
  3163. MapLRM.l -= 10;
  3164. if (MapLRM.l < -180) MapLRM.l += 360;
  3165. AzimuthalImage(&MapHRM,&MapLRM);
  3166. }
  3167. else
  3168. {
  3169. MapLRE.l -= 10;
  3170. if (MapLRE.l < -180) MapLRE.l += 360;
  3171. AzimuthalImage(&MapHRE,&MapLRE);
  3172. }
  3173. }
  3174. return;
  3175. }
  3176. if (WinEvent.type == ButtonPress)
  3177. {
  3178. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3179. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3180. for (i=0 ; i < DimSE ; ++i)
  3181. {
  3182. dx = mx - ModeX[i];
  3183. dy = my - ModeY[i];
  3184. if ((dx*dx + dy*dy) < ModeRadiusSq) break;
  3185. }
  3186. if (WinEvent.xbutton.button == Button1)
  3187. {
  3188. if (i == DimSH ) // All modes on
  3189. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 1;
  3190. else if (i == DimSH+1) // All modes off
  3191. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 0;
  3192. else Ampli[i] = 1 - Ampli[i];
  3193. }
  3194. else if (WinEvent.xbutton.button == Button2 && i < DimSH)
  3195. {
  3196. n = ModeN[i];
  3197. for (j=0 ; j < DimSH ; ++j)
  3198. if (ModeN[j] == n) Ampli[j] = 1 - Ampli[j];
  3199. }
  3200. else if (WinEvent.xbutton.button == Button3 && i < DimSH)
  3201. {
  3202. m = ModeM[i];
  3203. for (j=0 ; j < DimSH ; ++j)
  3204. if (ModeM[j] == m) Ampli[j] = 1 - Ampli[j];
  3205. }
  3206. }
  3207. if (WinEvent.type == MotionNotify)
  3208. {
  3209. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3210. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3211. for (i=0 ; i < DimSH ; ++i)
  3212. {
  3213. dx = mx - ModeX[i];
  3214. dy = my - ModeY[i];
  3215. if ((dx*dx + dy*dy) < ModeRadiusSq)
  3216. {
  3217. sprintf(Frame[1].t[1],"Mode (%2d:%2d)",ModeN[i],ModeM[i]);
  3218. return;
  3219. }
  3220. }
  3221. Frame[1].t[1][0] = 0;
  3222. }
  3223. return;
  3224. }
  3225. void InitFrames(void)
  3226. {
  3227. int i,j,n;
  3228. int moselw;
  3229. FILE *ftp;
  3230. /* Size and position of orography window */
  3231. sfbox_w = 360;
  3232. sfbox_h = 180;
  3233. sfbox_x = WINDOW_WIDTH - sfbox_w - 3 * FixFontWidth;
  3234. sfbox_y = nlbox_y + 4;
  3235. /* Parameter for default orographies */
  3236. nlat = 64;
  3237. nlon = nlat * 2;
  3238. VGAX = (sfbox_w - 1.0) / nlon;
  3239. VGAY = (sfbox_h - 1.0) / (nlat-1);
  3240. /* Read T42 orography for Earth and Mars */
  3241. OroEarth = ReadGrid("plasim/dat/T42/N064_surf_0129.sra",nlat,RevGra);
  3242. OroMars = ReadGrid("plasim/dat/T42_mars/N064_surf_0129.sra",nlat,1.0/3.74);
  3243. OpmEarth = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3244. OpmPrep = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3245. OpmMars = IsoPixmap(OroMars ,nlon,nlat,OroMarsStrip);
  3246. free(OroEarth);
  3247. free(OroMars);
  3248. /* Check for orography in run directory */
  3249. ftp = fopen(oro_name,"r");
  3250. if (ftp) fclose(ftp);
  3251. else strcpy(oro_name,"puma/dat/N064_surf_0129.sra");
  3252. // Orography
  3253. n = 0;
  3254. Frame[n].x = sfbox_x;
  3255. Frame[n].y = sfbox_y + FixFontHeight;
  3256. Frame[n].w = sfbox_w;
  3257. Frame[n].h = sfbox_h;
  3258. Frame[n].b = DarkGreen.pixel;
  3259. Frame[n].f = WhitePix;
  3260. strcpy(Frame[n].t[0],"Orography");
  3261. strcpy(Frame[n].t[1],"64 x 32");
  3262. strcpy(Frame[n].t[2],"");
  3263. Frame[n].Action = MarkRectangle;
  3264. Frame[n].xs = 110;
  3265. Frame[n].ys = 95;
  3266. Frame[n].ws = 60;
  3267. Frame[n].hs = 40;
  3268. SelAno->x = Frame[n].x;
  3269. SelAno->y = Frame[n].y + Frame[n].h + FixFontHeight;
  3270. SelAno->edco = 6;
  3271. SelAno->w = SelAno->edco * FixFontWidth + 2;
  3272. SelAno->xt = SelAno->x + (SelAno->edco + 1) * FixFontWidth;
  3273. SelAno->yt = SelAno->y + FixFont->ascent + 1;
  3274. strcpy(SelAno->teva," 0");
  3275. Button[4].y = SelAno->y - FixFontHeight/2; // Clear orography button
  3276. Button[4].x = Frame[n].x + Frame[n].w - 6 * FixFontWidth;
  3277. // T21 mode selector
  3278. ++n;
  3279. Frame[n].x = sfbox_x;
  3280. Frame[n].y = Frame[n-1].y + Frame[n-1].h + 4 * FixFontHeight;
  3281. moselw = sfbox_w;
  3282. if (SmallScreen)
  3283. {
  3284. moselw = WinYSize - Frame[n].y - FixFontHeight - FixFontHeight/2;
  3285. Frame[n].x += (sfbox_x - moselw) / 2;
  3286. }
  3287. Frame[n].w = moselw;
  3288. Frame[n].h = moselw;
  3289. Frame[n].b = BlackPix;
  3290. Frame[n].f = WhitePix;
  3291. strcpy(Frame[n].t[0],"Spherical Harmonics mode selector");
  3292. strcpy(Frame[n].t[1],"");
  3293. strcpy(Frame[n].t[2],"");
  3294. Frame[n].Action = ToggleMode;
  3295. sfbox_b = Frame[n].y + Frame[n].h - Frame[0].y + 2 * FixFontHeight + FixFontHeight/2;
  3296. Frames = n + 1;
  3297. }
  3298. void InitButtons(void)
  3299. {
  3300. int n;
  3301. bubox_w = 64;
  3302. bubox_h = 64;
  3303. bubox_x = WINDOW_WIDTH - 4 * 64 - 6 * FixFontWidth;
  3304. bubox_y = 8;
  3305. // Preprocess
  3306. n = 0;
  3307. Button[n].x = bubox_x;
  3308. Button[n].y = bubox_y;
  3309. Button[n].w = bubox_w;
  3310. Button[n].h = bubox_h;
  3311. Button[n].b = Yellow.pixel;
  3312. Button[n].f = BlackPix;
  3313. strcpy(Button[n].t[0],"Pre-");
  3314. strcpy(Button[n].t[1],"pro-");
  3315. strcpy(Button[n].t[2],"cess");
  3316. Button[n].Action = PreProcess;
  3317. // Save & Exit
  3318. ++n;
  3319. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3320. Button[n].y = Button[n-1].y;
  3321. Button[n].w = bubox_w;
  3322. Button[n].h = bubox_h;
  3323. Button[n].b = DarkGreen.pixel;
  3324. Button[n].f = WhitePix;
  3325. strcpy(Button[n].t[0],"Save");
  3326. strcpy(Button[n].t[1],"&");
  3327. strcpy(Button[n].t[2],"Exit");
  3328. Button[n].Action = SaveExit;
  3329. // Save & Run
  3330. ++n;
  3331. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3332. Button[n].y = Button[n-1].y;
  3333. Button[n].w = bubox_w;
  3334. Button[n].h = bubox_h;
  3335. Button[n].b = DarkBlue.pixel;
  3336. Button[n].f = WhitePix;
  3337. strcpy(Button[n].t[0],"Save");
  3338. strcpy(Button[n].t[1],"&");
  3339. strcpy(Button[n].t[2],"Run");
  3340. Button[n].Action = SaveRun;
  3341. // Abort
  3342. ++n;
  3343. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3344. Button[n].y = Button[n-1].y;
  3345. Button[n].w = bubox_w;
  3346. Button[n].h = bubox_h;
  3347. Button[n].b = Red.pixel;
  3348. Button[n].f = WhitePix;
  3349. strcpy(Button[n].t[1],"Abort");
  3350. Button[n].Action = Abort;
  3351. // Clear orography
  3352. ++n;
  3353. // Button[n].x = Button[n-1].x;
  3354. // Button[n].y = Button[n-1].y; // set in InitFrames
  3355. Button[n].w = 6 * FixFontWidth;
  3356. Button[n].h = 2 * FixFontHeight;
  3357. Button[n].b = DarkBlue.pixel;
  3358. Button[n].f = Yellow.pixel;
  3359. strcpy(Button[n].t[1],"Clear");
  3360. Button[n].Action = ClearOro;
  3361. }
  3362. /* ================== */
  3363. /* CreateModeSelector */
  3364. /* ================== */
  3365. void CreateModeSelector(int k)
  3366. {
  3367. int i,j,m,n;
  3368. int x,y,len,width,height;
  3369. int r,dx,dy,xp,yp;
  3370. char Text[20];
  3371. pix = Frame[k].pixmap = XCreatePixmap(display,Cow,Frame[k].w,Frame[k].h,ScreenD);
  3372. if (!Ampli) Ampli = malloc(DimSE * sizeof(int));
  3373. if (!ModeX) ModeX = malloc(DimSE * sizeof(int));
  3374. if (!ModeY) ModeY = malloc(DimSE * sizeof(int));
  3375. if (!ModeM) ModeM = malloc(DimSE * sizeof(int));
  3376. if (!ModeN) ModeN = malloc(DimSE * sizeof(int));
  3377. dxsh = Frame[k].w / (DimTr + 3);
  3378. dxs2 = dxsh / 2;
  3379. dx = dxsh;
  3380. r = (dx+dx) / 3;
  3381. ModeRadiusSq = r*r;
  3382. for (m=0,i=0 ; m <= DimTr ; ++m)
  3383. {
  3384. y = ModFontHeight + 4 + m * dx;
  3385. for (n=m ; n <= DimTr ; ++n,++i)
  3386. {
  3387. x = dx/2 + n * dx;
  3388. Ampli[i] = 1;
  3389. ModeX[i] = x + dx/2;
  3390. ModeY[i] = y + dx/2;
  3391. ModeM[i] = m;
  3392. ModeN[i] = n;
  3393. }
  3394. }
  3395. i = DimSH; // Button for all modes on
  3396. x = dx - ModFontWidth/2 + 1;
  3397. y = ModFontHeight + ModFont->descent + 2 + 20 * dx;
  3398. Ampli[i] = 0;
  3399. ModeX[i] = x + dx/2;
  3400. ModeY[i] = y + dx/2;
  3401. i++; // Button for all modes off
  3402. y += dx;
  3403. Ampli[i] = 0;
  3404. ModeX[i] = x + dx/2;
  3405. ModeY[i] = y + dx/2;
  3406. /* Draw mode legend */
  3407. dx = dxsh;
  3408. XSetForeground(display,gc,Red.pixel);
  3409. XSetBackground(display,gc,BlackPix);
  3410. for (i=0 ; i < 21 ; i+=2)
  3411. {
  3412. sprintf(Text,"%d",i);
  3413. len = strlen(Text);
  3414. width = XTextWidth(ModFont,Text,len);
  3415. height = ModFont->ascent + ModFont->descent;
  3416. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  3417. yp = ModFontHeight;
  3418. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3419. }
  3420. XSetForeground(display,gc,Blue.pixel);
  3421. for (i=0 ; i < 21 ; i+=2)
  3422. {
  3423. sprintf(Text,"%d",i);
  3424. len = strlen(Text);
  3425. width = XTextWidth(ModFont,Text,len);
  3426. height = ModFont->ascent + ModFont->descent;
  3427. xp = Frame[k].x + Frame[k].w - width - 2;
  3428. yp = 2 * ModFontHeight + i * dx;
  3429. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3430. }
  3431. }
  3432. void ShowButtons(void)
  3433. {
  3434. int i,k,l,x,y,d;
  3435. XSetFont(display, gc, FixFont->fid);
  3436. for (k=0 ; k < DIMBUTTON ; ++k)
  3437. {
  3438. if (k == 4 && SelAno->hide) continue;
  3439. d = Button[k].h / 4;
  3440. XSetForeground(display,gc,Button[k].b);
  3441. XFillRectangle(display,Cow,gc,Button[k].x,Button[k].y,Button[k].w,Button[k].h);
  3442. XSetForeground(display,gc,Button[k].f);
  3443. XSetBackground(display,gc,Button[k].b);
  3444. for (i=0 ; i < 3 ; ++i)
  3445. if (Button[k].t[i][0])
  3446. {
  3447. l = strlen(Button[k].t[i]);
  3448. x = Button[k].x + (Button[k].w - l * FixFontWidth) / 2;
  3449. y = Button[k].y + i * d + FixFont->ascent + d/2;
  3450. XDrawImageString(display,Cow,gc,x,y,Button[k].t[i],l);
  3451. }
  3452. }
  3453. }
  3454. int AllocateColorCells(struct ColorStrip cs[])
  3455. {
  3456. int i = 0;
  3457. XColor xcolor1,xcolor2;
  3458. while (cs[i].Name)
  3459. {
  3460. XAllocNamedColor(display,colormap,cs[i].Name,&xcolor1,&xcolor2);
  3461. cs[i].pixel = xcolor1.pixel;
  3462. ++i;
  3463. }
  3464. return i;
  3465. }
  3466. void InitColors(void)
  3467. {
  3468. /*
  3469. Visual *visual;
  3470. int count,st;
  3471. XStandardColormap *best_map_info;
  3472. XStandardColormap bmi[8];
  3473. visual = DefaultVisual(display,0);
  3474. memset(bmi,0,8*sizeof(XStandardColormap));
  3475. best_map_info = bmi;
  3476. count = 0;
  3477. st = XGetRGBColormaps(display,RootWindow(display,0),&best_map_info,&count,XA_RGB_DEFAULT_MAP);
  3478. {
  3479. printf("status = %d count=%d\n",st,count);
  3480. printf("colormap = %p\n",bmi[0].colormap);
  3481. printf("mapping = %x/%x/%x\n",bmi[0].red_max,bmi[0].green_max,bmi[0].blue_max);
  3482. }
  3483. */
  3484. // colormap = best_map_info->colormap;
  3485. AllocateColorCells(OroStrip);
  3486. AllocateColorCells(OroMarsStrip);
  3487. AllocateColorCells(GibbStrip);
  3488. AllocateColorCells(TStrip);
  3489. XAllocNamedColor(display,colormap,"red" ,&Red ,&Dummy);
  3490. XAllocNamedColor(display,colormap,"green" ,&Green ,&Dummy);
  3491. XAllocNamedColor(display,colormap,"blue" ,&Blue ,&Dummy);
  3492. XAllocNamedColor(display,colormap,"grey" ,&Grey ,&Dummy);
  3493. XAllocNamedColor(display,colormap,"hot pink" ,&LightRed ,&Dummy);
  3494. XAllocNamedColor(display,colormap,"dark red" ,&DarkRed ,&Dummy);
  3495. XAllocNamedColor(display,colormap,"light blue" ,&LightBlue ,&Dummy);
  3496. XAllocNamedColor(display,colormap,"dark blue" ,&DarkBlue ,&Dummy);
  3497. XAllocNamedColor(display,colormap,"light green",&LightGreen,&Dummy);
  3498. XAllocNamedColor(display,colormap,"dark green" ,&DarkGreen ,&Dummy);
  3499. XAllocNamedColor(display,colormap,"yellow" ,&Yellow ,&Dummy);
  3500. XAllocNamedColor(display,colormap,"cyan" ,&Cyan ,&Dummy);
  3501. TextC = Yellow.pixel;
  3502. HeadC = Cyan.pixel;
  3503. }
  3504. struct BMIstruct
  3505. {
  3506. int Size;
  3507. int Width;
  3508. int Height;
  3509. short Planes;
  3510. short Count;
  3511. int Compression;
  3512. int SizeImage;
  3513. int XPelsPerMeter;
  3514. int YPelsPerMeter;
  3515. int ClrUsed;
  3516. int ClrImportant;
  3517. };
  3518. // Convert an RGB value to an X11 Pixel
  3519. unsigned long create_pixel(long red, long green, long blue)
  3520. {
  3521. if (ScreenD == 24) // 24 bit true color
  3522. {
  3523. return blue | green << 8 | red << 16;
  3524. }
  3525. else if (ScreenD == 16) // 16 bit RGB 565
  3526. {
  3527. return blue >> 3 | (green >> 2) << 5 | (red >> 3) << 11;
  3528. }
  3529. else return 0;
  3530. }
  3531. void SwapIEEE16(char W[2])
  3532. {
  3533. char B;
  3534. B = W[0]; W[0] = W[1]; W[1] = B;
  3535. }
  3536. void SwapIEEE32(char W[4])
  3537. {
  3538. char B;
  3539. B = W[0]; W[0] = W[3]; W[3] = B;
  3540. B = W[1]; W[1] = W[2]; W[2] = B;
  3541. }
  3542. int ReadINT(FILE *fpi)
  3543. {
  3544. int k;
  3545. fread(&k,sizeof(k),1,fpi);
  3546. if (BigEndian) SwapIEEE32((char *)&k);
  3547. return k;
  3548. }
  3549. short ReadSHORT(FILE *fpi)
  3550. {
  3551. short k;
  3552. fread(&k,sizeof(k),1,fpi);
  3553. if (BigEndian) SwapIEEE16((char *)&k);
  3554. return k;
  3555. }
  3556. struct BMIstruct ImageBMI;
  3557. void ReadImage(struct MapImageStruct *ei, char *filename)
  3558. {
  3559. char ch;
  3560. int i,n,x,y,z;
  3561. long r,g,b;
  3562. int FileSize;
  3563. int Reserved;
  3564. int OffBits;
  3565. int PicBytes;
  3566. int ImgBytes;
  3567. int PadWidth;
  3568. int PadBytes;
  3569. int bpp;
  3570. int byr;
  3571. FILE *fp;
  3572. unsigned char *BuffImageData;
  3573. if (!(fp = fopen(filename,"r"))) return;
  3574. ch = fgetc(fp);
  3575. if (ch != 'B') return;
  3576. ch = fgetc(fp);
  3577. if (ch != 'M') return;
  3578. FileSize = ReadINT(fp);
  3579. Reserved = ReadINT(fp);
  3580. OffBits = ReadINT(fp);
  3581. if (Debug)
  3582. {
  3583. printf("Properties of %s:\n",filename);
  3584. printf("FileSize = %d\n",FileSize);
  3585. printf("FileOffset = %d\n",OffBits);
  3586. }
  3587. ImageBMI.Size = ReadINT(fp);
  3588. ImageBMI.Width = ReadINT(fp);
  3589. ImageBMI.Height = ReadINT(fp);
  3590. ImageBMI.Planes = ReadSHORT(fp);
  3591. ImageBMI.Count = ReadSHORT(fp);
  3592. ImageBMI.Compression = ReadINT(fp);
  3593. ImageBMI.SizeImage = ReadINT(fp);
  3594. ImageBMI.XPelsPerMeter = ReadINT(fp);
  3595. ImageBMI.YPelsPerMeter = ReadINT(fp);
  3596. ImageBMI.ClrUsed = ReadINT(fp);
  3597. ImageBMI.ClrImportant = ReadINT(fp);
  3598. PadWidth = (ImageBMI.Width + 7) & 0xFFF8;
  3599. bpp = ImageBMI.Count >> 3;
  3600. PadBytes = (4 - ((ImageBMI.Width * bpp) % 4)) % 4 ;
  3601. if (Debug)
  3602. {
  3603. printf("BMI Size = %d\n",ImageBMI.Size);
  3604. printf("BMI Width = %d\n",ImageBMI.Width);
  3605. printf("BMI Height = %d\n",ImageBMI.Height);
  3606. printf("BMI Planes = %d\n",ImageBMI.Planes);
  3607. printf("BMI Count = %d\n",ImageBMI.Count);
  3608. printf("BMI ClrUsed = %d\n",ImageBMI.ClrUsed);
  3609. printf("BMI ClrImpo = %d\n",ImageBMI.ClrImportant);
  3610. printf("Pad Bytes = %d\n",PadBytes);
  3611. printf("Pad Width = %d\n",PadWidth);
  3612. }
  3613. PicBytes = (bpp * ImageBMI.Width + PadBytes) * ImageBMI.Height;
  3614. ImgBytes = 4 * PadWidth * ImageBMI.Height;
  3615. ei->d = calloc(ImgBytes,1);
  3616. BuffImageData = malloc(PicBytes);
  3617. fseek(fp,OffBits,SEEK_SET);
  3618. n = fread(BuffImageData,1,PicBytes,fp);
  3619. fclose(fp);
  3620. if (Debug)
  3621. {
  3622. printf("Size Bytes = %d\n",PicBytes);
  3623. printf("Read Bytes = %d\n",n);
  3624. }
  3625. ei->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,
  3626. ei->d,PadWidth,ImageBMI.Height,8,0);
  3627. for (y = 0 ; y < ImageBMI.Height ; ++y)
  3628. {
  3629. for (x = 0 ; x < ImageBMI.Width ; ++x)
  3630. {
  3631. i = bpp * x + (ImageBMI.Height - 1 - y) * (ImageBMI.Width*bpp+PadBytes);
  3632. b = BuffImageData[i ];
  3633. g = BuffImageData[i+1];
  3634. r = BuffImageData[i+2];
  3635. XPutPixel(ei->X, x, y, create_pixel(r,g,b));
  3636. }
  3637. }
  3638. free(BuffImageData);
  3639. ei->w = ImageBMI.Width;
  3640. ei->h = ImageBMI.Height;
  3641. }
  3642. void ReadLogo(int logo, char *filename)
  3643. {
  3644. struct MapImageStruct MapI;
  3645. ReadImage(&MapI,filename);
  3646. Logo[logo].w = MapI.w;
  3647. Logo[logo].h = MapI.h;
  3648. Logo[logo].X = MapI.X;
  3649. if (logo)
  3650. {
  3651. Logo[logo].x = Logo[logo-1].x + Logo[logo-1].w;
  3652. Logo[logo].y = Logo[logo-1].y;
  3653. }
  3654. }
  3655. void ShowCopyright(void)
  3656. {
  3657. int x,y;
  3658. x = CowSizeHints.min_width - 18.5 * ModFontWidth;
  3659. y = CowSizeHints.min_height - ModFontHeight/2;
  3660. XSetFont(display, gc, ModFont->fid);
  3661. XSetForeground(display,gc,Grey.pixel);
  3662. XSetBackground(display,gc,BlackPix);
  3663. XDrawImageString(display,Cow,gc,x,y,"Image Credit: NASA",18);
  3664. }
  3665. int redaco;
  3666. int RedrawControlWindow(void)
  3667. {
  3668. int i,l,x,y;
  3669. double xrf,yrf;
  3670. struct SelStruct *Sel;
  3671. XWindowAttributes CurAtt;
  3672. if (Debug) printf("Redraw %d\n",redaco++);
  3673. XGetWindowAttributes(display,Cow,&CurAtt);
  3674. WinXSize = CurAtt.width;
  3675. WinYSize = CurAtt.height;
  3676. XSetWindowBackground(display,Cow,WinBG);
  3677. XClearWindow(display,Cow);
  3678. // Paint some stars on the sky
  3679. xrf = (double)WinXSize / pow(2.0,31),
  3680. yrf = (double)WinYSize / pow(2.0,31);
  3681. XSetForeground(display,gc,WhitePix);
  3682. srandom(Seed);
  3683. for (y = 0 ; y < ((WinXSize * WinYSize) >> 8) ; ++y)
  3684. XDrawPoint(display,Cow,gc,xrf*random(),yrf*random());
  3685. XSetForeground(display,gc,BlackPix);
  3686. XSetBackground(display,gc,WhitePix);
  3687. for (Sel = &SelStart ; Sel ; Sel = Sel->Next) ShowSelection(Sel);
  3688. if (CursorSel) CursorOn();
  3689. ShowOrography();
  3690. ShowFrame1();
  3691. ShowButtons();
  3692. for (l=0 ; l < Logos ; ++l)
  3693. {
  3694. XPutImage(display,Cow,gc,Logo[l].X,0,0,Logo[l].x,Logo[l].y,Logo[l].w,Logo[l].h);
  3695. }
  3696. if (Model == PLASIM) ShowCopyright();
  3697. XSync(display,0);
  3698. return 0;
  3699. }
  3700. void ChangeResolution(int NewRes)
  3701. {
  3702. int i;
  3703. struct SelStruct *Sel;
  3704. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3705. {
  3706. if (i == NewRes) Sel->iv = 1;
  3707. else Sel->iv = 0;
  3708. }
  3709. Resolution = NewRes;
  3710. }
  3711. void UpdateResolution(void)
  3712. {
  3713. int i;
  3714. struct SelStruct *Sel;
  3715. if (SelRes)
  3716. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3717. {
  3718. if (Sel && Sel->iv == 1) Resolution = i;
  3719. }
  3720. }
  3721. char *vcn[6] =
  3722. {
  3723. "StaticGray",
  3724. "GrayScale",
  3725. "StaticColor",
  3726. "PseudoColor",
  3727. "TrueColor",
  3728. "DirectColor"
  3729. };
  3730. void InitGUI(void)
  3731. {
  3732. int argc = 1;
  3733. int i,j,k;
  3734. char *WinconTitle1 = {"MoSt - Model Suite (17) - University of Hamburg"};
  3735. unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
  3736. FILE *ftp;
  3737. FILE *xpp;
  3738. XVisualInfo vinfo;
  3739. XGCValues values;
  3740. XEvent Event;
  3741. XWindowAttributes CurAtt;
  3742. if ((display=XOpenDisplay(display_name)) == NULL)
  3743. {
  3744. fprintf(stderr,"%s: cannot connect to X server %s\n",
  3745. progname, XDisplayName(display_name));
  3746. exit(1);
  3747. }
  3748. ScreenN = DefaultScreen(display);
  3749. ScreenD = XDefaultDepth(display,ScreenN);
  3750. ScreenW = DisplayWidth (display,ScreenN);
  3751. ScreenH = DisplayHeight(display,ScreenN);
  3752. if (ScreenHeight) ScreenH = ScreenHeight;
  3753. if (Debug)
  3754. {
  3755. i=5;
  3756. while (!XMatchVisualInfo(display,ScreenN,ScreenD,i--,&vinfo));
  3757. printf("Found visual %s at depth %d\n",vcn[++i],ScreenD);
  3758. printf("Red mask = %8lx\n",vinfo.red_mask);
  3759. printf("Green mask = %8lx\n",vinfo.green_mask);
  3760. printf("Blue mask = %8lx\n",vinfo.blue_mask);
  3761. }
  3762. BlackPix = BlackPixel(display,ScreenN);
  3763. WhitePix = WhitePixel(display,ScreenN);
  3764. SmallScreen = ScreenH < 768;
  3765. wm_hints.initial_state = NormalState;
  3766. wm_hints.input = True;
  3767. wm_hints.flags = StateHint | InputHint;
  3768. class_hints.res_name = progname;
  3769. class_hints.res_class = "MOST";
  3770. Delwin = XInternAtom(display,"WM_DELETE_WINDOW",0);
  3771. LoadFonts();
  3772. /* Setup control window */
  3773. CowSizeHints.flags = PPosition | PSize | PMinSize | PMaxSize;
  3774. CowSizeHints.min_width = MIN(WINDOW_WIDTH,ScreenW);
  3775. CowSizeHints.min_height = MIN(740,ScreenH);
  3776. CowSizeHints.max_width = CowSizeHints.min_width;
  3777. CowSizeHints.max_height = CowSizeHints.min_height;
  3778. Cow = XCreateSimpleWindow(display,RootWindow(display,ScreenN),
  3779. 0,0,
  3780. CowSizeHints.max_width,CowSizeHints.max_height,
  3781. 4,BlackPix,WhitePix);
  3782. XStringListToTextProperty(&WinconTitle1,1,&WinconName1);
  3783. XSetWMProtocols(display,Cow,&Delwin,1);
  3784. XSetWMProperties(display,Cow,&WinconName1,NULL,
  3785. NULL,0,&CowSizeHints,&wm_hints,&class_hints);
  3786. XSelectInput(display,Cow,ButtonPressMask | ButtonReleaseMask |
  3787. PointerMotionMask | KeyPressMask | ExposureMask);
  3788. XMapWindow(display,Cow);
  3789. XGetWindowAttributes(display,Cow,&CurAtt);
  3790. WinXSize = CurAtt.width;
  3791. WinYSize = CurAtt.height;
  3792. /* Prepare GC */
  3793. gc = XCreateGC(display, Cow, valuemask, &values);
  3794. XSetFont(display, gc, FixFont->fid);
  3795. colormap = XDefaultColormap(display,ScreenN);
  3796. XSetForeground(display,gc,BlackPix);
  3797. XSetBackground(display,gc,WhitePix);
  3798. Mok = XGetModifierMapping(display);
  3799. InitColors();
  3800. /* Look for Num_Lock key */
  3801. for (j=0 ; j < 8 ; ++j)
  3802. {
  3803. k = Mok->modifiermap[j * Mok->max_keypermod];
  3804. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Num_Lock ) NumLockMask = 1 << j;
  3805. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Mode_switch) ModeSwitchMask = 1 << j;
  3806. }
  3807. if (Debug) printf("NumLockMask = %02X\n",NumLockMask );
  3808. if (Debug) printf("ModeSwitchMask = %02X\n",ModeSwitchMask);
  3809. if (Debug)
  3810. for (j=0 ; j < 8 ; ++j)
  3811. {
  3812. printf("Mok [%d] %d:",Mok->max_keypermod,j);
  3813. for (i=0 ; i < Mok->max_keypermod ; ++i)
  3814. {
  3815. k = Mok->modifiermap[i+j*Mok->max_keypermod];
  3816. if (XkbKeycodeToKeysym(display,k,0,0) != NoSymbol)
  3817. printf(" %4x %-16.16s ",k,XKeysymToString(XkbKeycodeToKeysym(display,k,0,0)));
  3818. }
  3819. printf("\n");
  3820. }
  3821. XDisplayKeycodes(display,&EdiFirstKey,&EdiLastKey);
  3822. EdiKeymap = XGetKeyboardMapping(display,EdiFirstKey,EdiLastKey-EdiFirstKey+1,&EdiSymsPerKey);
  3823. /* Sun keymaps don't have lower case entries */
  3824. for (j=EdiFirstKey ; j <= EdiLastKey ; ++j)
  3825. {
  3826. k = (j - EdiFirstKey) * EdiSymsPerKey;
  3827. if (EdiKeymap[k] >= 'A' && EdiKeymap[k] < 0xE0 && EdiKeymap[k+1] == 0)
  3828. {
  3829. EdiKeymap[k+1] = EdiKeymap[k];
  3830. EdiKeymap[k] += 0x20;
  3831. }
  3832. }
  3833. //FL0318 xpp = fopen("Beginner","r"); // Expert mode ?
  3834. xpp = fopen("Expert","r"); // Expert mode ?
  3835. if (xpp)
  3836. {
  3837. //FL0318 Expert = 0;
  3838. Expert = 1;
  3839. fclose(xpp);
  3840. }
  3841. xpp = fopen("cat","r"); // Cat enabled
  3842. if (xpp)
  3843. {
  3844. CatEnabled = 1;
  3845. fclose(xpp);
  3846. }
  3847. xpp = fopen("sam","r"); // Sam enabled
  3848. if (xpp)
  3849. {
  3850. SamEnabled = 1;
  3851. fclose(xpp);
  3852. }
  3853. xpp = fopen("lsg/src/lsgmod.f90","r"); // LSG there ?
  3854. //FL0318 if (xpp)
  3855. if (xpp && Expert)
  3856. {
  3857. LsgEnabled = 1;
  3858. fclose(xpp);
  3859. }
  3860. xpp = fopen("puma/src/mpimod_multi.f90","r"); // Multirun module there ?
  3861. if (xpp)
  3862. {
  3863. MultirunEnabled = 1;
  3864. fclose(xpp);
  3865. }
  3866. // Read name of MPI execute command
  3867. xpp = fopen("most_compiler_mpi","r"); // MPI installed ?
  3868. if (xpp)
  3869. {
  3870. fgets(Buffer,LINEMAX,xpp);
  3871. if (Buffer[strlen(Buffer)-1] == 10) Buffer[strlen(Buffer)-1] = 0;
  3872. if (Buffer[strlen(Buffer)-1] == 13) Buffer[strlen(Buffer)-1] = 0;
  3873. if (!strncmp(Buffer,"MPI_RUN=",8)) strcpy(mpirun,Buffer+8);
  3874. fclose(xpp);
  3875. }
  3876. InitLogo();
  3877. ReadLogo(0,"images/KC-Logo_RGB.bmp");
  3878. ReadLogo(1,"images/puma.bmp");
  3879. ReadLogo(2,"images/cat.bmp");
  3880. ReadLogo(3,"images/plasim.bmp");
  3881. ReadImage(&MapHRE,"images/earth.bmp");
  3882. ReadImage(&MapHRM,"images/mars.bmp");
  3883. ReadImage(&MapLRK,"images/Kepler-16.bmp");
  3884. ReadImage(&MapLRL,"images/habit360x180.bmp");
  3885. opbox_y = 8 + 64 + 2 * FixFontHeight;
  3886. InitSelections();
  3887. InitNamelist();
  3888. ChangeModel(PLASIM);
  3889. NamelistSelector(PLASIM);
  3890. ChangeModel(CAT);
  3891. NamelistSelector(CAT);
  3892. ChangeModel(SAM);
  3893. NamelistSelector(SAM);
  3894. ChangeModel(PUMA);
  3895. NamelistSelector(PUMA);
  3896. if (ReadSettings(cfg_file))
  3897. {
  3898. UpdateResolution();
  3899. UpdateSelections(&SelStart);
  3900. }
  3901. InitFrames();
  3902. CalcFrame(nlat);
  3903. CreateModeSelector(1);
  3904. InitButtons();
  3905. MapLRE.w = Frame[1].w;
  3906. MapLRE.h = Frame[1].h;
  3907. MapLRK.w = Frame[1].w;
  3908. MapLRK.h = Frame[1].h;
  3909. MapLRM.w = Frame[1].w;
  3910. MapLRM.h = Frame[1].h;
  3911. MapLRL.w = Frame[0].w;
  3912. MapLRL.h = Frame[0].h;
  3913. MapLRM.l = -90;
  3914. AzimuthalImage(&MapHRE,&MapLRE);
  3915. AzimuthalImage(&MapHRM,&MapLRM);
  3916. RedrawControlWindow();
  3917. }
  3918. int HitBox(struct SelStruct *Sel)
  3919. {
  3920. return (
  3921. (WinEvent.xbutton.button == Button1) &&
  3922. (WinEvent.xbutton.x >= Sel->x) &&
  3923. (WinEvent.xbutton.x < Sel->x + Sel->w) &&
  3924. (WinEvent.xbutton.y >= Sel->y) &&
  3925. (WinEvent.xbutton.y < Sel->y + Sel->h));
  3926. }
  3927. int HitButton(int k)
  3928. {
  3929. return (
  3930. (WinEvent.xbutton.button == Button1) &&
  3931. (WinEvent.xbutton.x >= Button[k].x) &&
  3932. (WinEvent.xbutton.x < Button[k].x + Button[k].w) &&
  3933. (WinEvent.xbutton.y >= Button[k].y) &&
  3934. (WinEvent.xbutton.y < Button[k].y + Button[k].h));
  3935. }
  3936. int HitFrame(int k)
  3937. {
  3938. return (
  3939. (WinEvent.xbutton.x >= Frame[k].x) &&
  3940. (WinEvent.xbutton.x < Frame[k].x + Frame[k].w) &&
  3941. (WinEvent.xbutton.y >= Frame[k].y) &&
  3942. (WinEvent.xbutton.y < Frame[k].y + Frame[k].h));
  3943. }
  3944. int OnFrame(int i)
  3945. {
  3946. if (HitFrame(i))
  3947. {
  3948. FrameNo = i;
  3949. Frame[i].Action();
  3950. return 1;
  3951. }
  3952. return 0;
  3953. }
  3954. void OnMouseClick(void)
  3955. {
  3956. int i,j,cp;
  3957. struct SelStruct *Sel;
  3958. /* Check Action Buttons */
  3959. for (i=0 ; i < DIMBUTTON ; ++i)
  3960. {
  3961. if (HitButton(i))
  3962. {
  3963. Button[i].Action();
  3964. return;
  3965. }
  3966. }
  3967. /* Check for model switch */
  3968. for (i = PUMA , Sel = SelMod ; i < MODELS ; ++i , Sel = Sel->Next)
  3969. {
  3970. if (HitBox(Sel) && Model != i)
  3971. {
  3972. if (i == CAT && !CatEnabled) return; // Hide CAT
  3973. if (Debug) printf("Change model from %d to %d\n",Model,i);
  3974. ChangeModel(i);
  3975. CalcFrame(Latitudes);
  3976. return;
  3977. }
  3978. }
  3979. /* Continue with all other boxes */
  3980. for ( ; Sel ; Sel = Sel->Next)
  3981. {
  3982. if (Sel->type == SEL_CHECK && HitBox(Sel) && !Sel->no)
  3983. {
  3984. cp = -1;
  3985. for (i=0 ; i < PLANETS ; ++i) if (Sel == SelPlanet[i]) cp = i;
  3986. if (cp >= 0 && Planet != cp) ChangePlanet(cp);
  3987. else
  3988. {
  3989. Sel->iv = !Sel->iv;
  3990. if (Sel->piv) *Sel->piv = Sel->iv;
  3991. }
  3992. }
  3993. else if ((Sel->type == SEL_INT || Sel->type == SEL_REAL) && HitBox(Sel))
  3994. {
  3995. // printf("Target is <%s>\n",Sel->text);
  3996. if (Sel != CursorSel) FinishLine();
  3997. CursorSel = Sel;
  3998. CursorCol = (WinEvent.xbutton.x - Sel->x) / FixFontWidth;
  3999. if (CursorCol > Sel->edco-1) CursorCol = Sel->edco-1;
  4000. CursorOn();
  4001. }
  4002. }
  4003. /* Allow only one horizontal resolution */
  4004. if (SelRes)
  4005. {
  4006. for (i = RES_T21 , Sel = SelRes ; i <= RES_T42 ; ++i , Sel = Sel->Next)
  4007. if (HitBox(Sel))
  4008. {
  4009. ChangeResolution(i);
  4010. return;
  4011. }
  4012. }
  4013. }
  4014. int EdiDecodeKey(void)
  4015. {
  4016. int ShiftStatus;
  4017. int ShiftIndex;
  4018. int KeyCode;
  4019. int KeyIndex;
  4020. ShiftStatus = WinEvent.xkey.state;
  4021. KeyIndex = (WinEvent.xkey.keycode-EdiFirstKey) * EdiSymsPerKey;
  4022. KeyCode = EdiKeymap[KeyIndex]; // Code with no modifiers
  4023. if (ShiftStatus & 0x04) // Control
  4024. {
  4025. if (KeyCode >= 0x40 && KeyCode < 0x80) KeyCode &= 0x1F;
  4026. }
  4027. else if (ShiftStatus & NumLockMask &&
  4028. EdiKeymap[KeyIndex+1] >= XK_KP_Separator && // Numlock
  4029. EdiKeymap[KeyIndex+1] <= XK_KP_9) // PC Keypad
  4030. KeyCode = EdiKeymap[KeyIndex+1] - XK_KP_Space;
  4031. else if (ShiftStatus & NumLockMask &&
  4032. EdiKeymap[KeyIndex+2] >= XK_KP_Separator && // Numlock
  4033. EdiKeymap[KeyIndex+2] <= XK_KP_9) // SUN Keypad
  4034. KeyCode = EdiKeymap[KeyIndex+2] - XK_KP_Space;
  4035. else
  4036. {
  4037. if (ShiftStatus & ModeSwitchMask) ShiftIndex = 2; // Alt Gr
  4038. else ShiftIndex = ShiftStatus & 1; // Normal & Shift
  4039. if (ShiftIndex == 1 && KeyCode >= XK_F1 && KeyCode <= XK_F12) KeyCode += 12;
  4040. else KeyCode = EdiKeymap[KeyIndex + ShiftIndex];
  4041. }
  4042. if (ShiftStatus & 0x02) // Caps Lock
  4043. {
  4044. if (KeyCode >= 'a' && KeyCode <= 'z' ) KeyCode -= 0x20; // ASCII
  4045. if (KeyCode >= 0xE0 && KeyCode <= 0xFD) KeyCode -= 0x20; // Latin-1
  4046. }
  4047. if (KeyCode == XK_KP_Left ) KeyCode = XK_Left;
  4048. if (KeyCode == XK_KP_Right ) KeyCode = XK_Right;
  4049. if (KeyCode == XK_KP_Up ) KeyCode = XK_Up;
  4050. if (KeyCode == XK_KP_Down ) KeyCode = XK_Down;
  4051. if (KeyCode == XK_KP_Home ) KeyCode = XK_Home;
  4052. if (KeyCode == XK_KP_End ) KeyCode = XK_End ;
  4053. if (KeyCode == XK_KP_Page_Up ) KeyCode = XK_Page_Up;
  4054. if (KeyCode == XK_KP_Page_Down ) KeyCode = XK_Page_Down;
  4055. if (KeyCode == XK_KP_Enter ) KeyCode = XK_Return;
  4056. if (KeyCode == XK_KP_Add ) KeyCode = '+';
  4057. if (KeyCode == XK_KP_Subtract ) KeyCode = '-';
  4058. if (KeyCode == XK_KP_Multiply ) KeyCode = '*';
  4059. if (KeyCode == XK_KP_Divide ) KeyCode = '/';
  4060. if (Debug)
  4061. {
  4062. if (KeyCode >= ' ' && KeyCode <= 255) printf("'%c' ",KeyCode);
  4063. else printf("--- ");
  4064. printf("Key [%x] State [%2x]\n",WinEvent.xkey.keycode,WinEvent.xkey.state);
  4065. for (ShiftIndex = 0 ; ShiftIndex < EdiSymsPerKey ; ++ShiftIndex)
  4066. {
  4067. if (EdiKeymap[KeyIndex + ShiftIndex] && XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]))
  4068. {
  4069. printf(" %d: <%x> '%c' (%s)\n",
  4070. ShiftIndex,(unsigned int)EdiKeymap[KeyIndex + ShiftIndex],
  4071. (int)EdiKeymap[KeyIndex + ShiftIndex],
  4072. XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]));
  4073. }
  4074. }
  4075. printf("\n");
  4076. }
  4077. return KeyCode;
  4078. }
  4079. void EditUpLine(void)
  4080. {
  4081. FinishLine();
  4082. if (CursorSel == NULL || CursorSel->Prev == NULL) return;
  4083. if (CursorSel->Prev->type != SEL_INT && CursorSel->Prev->type != SEL_REAL) return;
  4084. CursorSel = CursorSel->Prev;
  4085. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4086. }
  4087. void EditDownLine(void)
  4088. {
  4089. FinishLine();
  4090. if (CursorSel == NULL || CursorSel->Next == NULL) return;
  4091. if (CursorSel->Next->type != SEL_INT && CursorSel->Next->type != SEL_REAL) return;
  4092. CursorSel = CursorSel->Next;
  4093. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4094. }
  4095. void EditReturnLine(void)
  4096. {
  4097. CursorCol = 0;
  4098. EditDownLine();
  4099. }
  4100. void EditLeftChar(void)
  4101. {
  4102. if (CursorCol > 0) --CursorCol;
  4103. }
  4104. void EditRightChar(void)
  4105. {
  4106. if (CursorCol < CursorSel->edco-1) ++CursorCol;
  4107. }
  4108. void EmitChar(int ch)
  4109. {
  4110. if (CursorSel)
  4111. {
  4112. CursorSel->teva[CursorCol] = ch;
  4113. EditRightChar();
  4114. }
  4115. } /* EmitChar */
  4116. void DeleteRightChar(void)
  4117. {
  4118. int i;
  4119. for (i=CursorCol ; i < CursorSel->edco-1 ; ++i)
  4120. CursorSel->teva[i] = CursorSel->teva[i+1];
  4121. CursorSel->teva[CursorSel->edco-1] = ' ';
  4122. }
  4123. void DeleteLeftChar(void)
  4124. {
  4125. if (CursorCol == 0) return;
  4126. EditLeftChar();
  4127. DeleteRightChar();
  4128. }
  4129. void InsertBlank(void)
  4130. {
  4131. int i;
  4132. for (i = CursorSel->edco-1 ; i > CursorCol ; --i)
  4133. CursorSel->teva[i] = CursorSel->teva[i-1];
  4134. CursorSel->teva[CursorCol] = ' ';
  4135. }
  4136. void ProcChar(int Key)
  4137. {
  4138. if (CursorSel)
  4139. switch (Key)
  4140. {
  4141. case XK_Up : EditUpLine(); break;
  4142. case XK_Left : EditLeftChar(); break;
  4143. case XK_Right : EditRightChar(); break;
  4144. case XK_Down : EditDownLine(); break;
  4145. case XK_Return : EditReturnLine(); break;
  4146. case XK_BackSpace: DeleteLeftChar(); break;
  4147. case XK_Delete : DeleteRightChar(); break;
  4148. case XK_Insert : InsertBlank(); break;
  4149. }
  4150. }
  4151. void LoopGUI(void)
  4152. {
  4153. int w;
  4154. while (1)
  4155. {
  4156. XNextEvent(display,&WinEvent);
  4157. switch (WinEvent.type)
  4158. {
  4159. case ClientMessage: return;
  4160. case MotionNotify : if (OnFrame(0)) ShowOrography();
  4161. if (Model == PUMA && OnFrame(1)) ShowModeSelector();
  4162. break;
  4163. case ButtonRelease: Button1Down = 0; if (OnFrame(0)) ShowOrography(); break;
  4164. case ButtonPress : Button1Down = 1;
  4165. if (!OnFrame(0) && !OnFrame(1)) OnMouseClick();
  4166. case Expose : RedrawControlWindow(); break;
  4167. case KeyPress : EdiKeyCode = EdiDecodeKey();
  4168. if (EdiKeyCode >= ' ' && EdiKeyCode < 256) EmitChar(EdiKeyCode);
  4169. else ProcChar(EdiKeyCode);
  4170. RedrawControlWindow();
  4171. break;
  4172. }
  4173. }
  4174. }
  4175. int CheckEndianess(void)
  4176. {
  4177. union EndianCheck
  4178. {
  4179. char b[sizeof(int)];
  4180. int i;
  4181. } ec;
  4182. ec.i = 8;
  4183. return (ec.b[0] == 0);
  4184. }
  4185. int main(int argc, char *argv[])
  4186. {
  4187. int ia;
  4188. for (ia = 1 ; ia < argc ; ++ia)
  4189. {
  4190. if (!strcmp(argv[ia],"-d")) Debug = 1;
  4191. else if (!strcmp(argv[ia],"-i")) cfg_file[0] = 0;
  4192. else if (!strcmp(argv[ia],"-h")) ScreenHeight = atoi(argv[++ia]);
  4193. else strncpy(cfg_file,argv[1],sizeof(cfg_file));
  4194. }
  4195. CurrentDate = time(NULL);
  4196. BigEndian = CheckEndianess();
  4197. InitGUI();
  4198. LoopGUI();
  4199. return 0;
  4200. }