most.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931
  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. fputs("srv2nc=../../tools/srv2nc\n",fp);
  1516. if (Multirun > 1) fprintf(fp,"INSTANCES=%d\n",Multirun);
  1517. if (ngui) fputs("# Remove '#' from 'while' and 'end' lines for restart loop\n",fp);
  1518. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1519. fputs("while [ $YEAR -lt $YEARS ]\n",fp);
  1520. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1521. fputs("do\n",fp);
  1522. fputs(" YEAR=`expr $YEAR + 1`\n",fp);
  1523. fputs(" DATANAME=`printf '%s.%03d' $EXP $YEAR`\n",fp);
  1524. fputs(" DIAGNAME=`printf '%s_DIAG.%03d' $EXP $YEAR`\n",fp);
  1525. fputs(" RESTNAME=`printf '%s_REST.%03d' $EXP $YEAR`\n",fp);
  1526. fputs(" OCENAME=`printf '%s_OCE.%03d.nc' $EXP $YEAR`\n",fp);
  1527. fputs(" ICENAME=`printf '%s_ICE.%03d.nc' $EXP $YEAR`\n",fp);
  1528. if (porm < 2)
  1529. {
  1530. fprintf(fp," ./%s\n",exec_name);
  1531. }
  1532. else
  1533. {
  1534. if (Multirun > 1)
  1535. {
  1536. fprintf(fp," %s",mpirun);
  1537. fprintf(fp," -np 1 %s : -np 1 %s\n",exec_name,exec_nam2);
  1538. }
  1539. else
  1540. {
  1541. fprintf(fp," %s -np %d %s\n",mpirun,porm,exec_name);
  1542. }
  1543. }
  1544. fputs(" [ -e Abort_Message ] && exit 1\n",fp);
  1545. if (Multirun > 1)
  1546. {
  1547. fputs(" INST=0\n",fp);
  1548. fputs(" while [ $INST -lt $INSTANCES ]\n do\n",fp);
  1549. fputs(" EXT=`printf '%02d' $INST`\n",fp);
  1550. fprintf(fp," [ -e %s_$EXT ] && mv %s_$EXT ${EXT}_$DATANAME\n",
  1551. outp_name,outp_name);
  1552. fprintf(fp," mv %s_$EXT ${EXT}_$DIAGNAME\n",diag_name);
  1553. fprintf(fp," cp %s_status_$EXT %s_restart_$EXT\n",
  1554. ShortModelName[model],ShortModelName[model]);
  1555. fprintf(fp," mv %s_status_$EXT ${EXT}_$RESTNAME\n",ShortModelName[model]);
  1556. fputs(" INST=`expr $INST + 1`\n",fp);
  1557. fputs(" done\n",fp);
  1558. }
  1559. else
  1560. {
  1561. fprintf(fp," [ -e %s ] && mv %s $DATANAME\n",outp_name,outp_name);
  1562. fprintf(fp," [ -e %s ] && mv %s $DIAGNAME\n",diag_name,diag_name);
  1563. fprintf(fp," [ -e ocean_output ] && $srv2nc ocean_output $OCENAME\n");
  1564. fprintf(fp," [ -e ice_output ] && $srv2nc ice_output $ICENAME\n");
  1565. fprintf(fp," [ -e %s_status ] && cp %s_status %s_restart\n",
  1566. ShortModelName[model],ShortModelName[model],ShortModelName[model]);
  1567. fprintf(fp," [ -e %s_status ] && mv %s_status $RESTNAME\n",
  1568. ShortModelName[model],ShortModelName[model]);
  1569. }
  1570. if (ngui) fputs("# ",fp); /* deactivate loop for GUI case */
  1571. fputs("done\n",fp);
  1572. fclose(fp);
  1573. sprintf(command,"chmod a+x %s",run);
  1574. system(command);
  1575. return 1; /* Success */
  1576. }
  1577. void BlueMessage(char *mess)
  1578. {
  1579. int l,x,y,w,h;
  1580. XSetFont(display, gc, FixFont->fid);
  1581. l = strlen(mess);
  1582. w = FixFontWidth * (l + 4);
  1583. h = FixFontHeight * 3;
  1584. if (w > WinXSize) w = WinXSize;
  1585. XSetForeground(display,gc,Blue.pixel);
  1586. x = (WinXSize - w) / 2;
  1587. y = (WinYSize - h) / 2;
  1588. XFillRectangle(display,Cow,gc,x,y,w,h);
  1589. XSetForeground(display,gc,WhitePix);
  1590. XSetBackground(display,gc,Blue.pixel);
  1591. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1592. x += 2 * FixFontWidth;
  1593. y += 2 * FixFontHeight;
  1594. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1595. XSync(display,0);
  1596. }
  1597. void RedMessage(char *mess)
  1598. {
  1599. int l,x,y,w,h;
  1600. XSetFont(display, gc, FixFont->fid);
  1601. l = strlen(mess);
  1602. w = FixFontWidth * (l + 4);
  1603. h = FixFontHeight * 3;
  1604. if (w > WinXSize) w = WinXSize;
  1605. XSetForeground(display,gc,Red.pixel);
  1606. x = (WinXSize - w) / 2;
  1607. y = (WinYSize - h) / 2;
  1608. XFillRectangle(display,Cow,gc,x,y,w,h);
  1609. XSetForeground(display,gc,WhitePix);
  1610. XSetBackground(display,gc,Red.pixel);
  1611. XDrawRectangle(display,Cow,gc,x-1,y-1,w+1,h+1);
  1612. x += 2 * FixFontWidth;
  1613. y += 2 * FixFontHeight;
  1614. XDrawImageString(display,Cow,gc,x,y,mess,l);
  1615. XSync(display,0);
  1616. }
  1617. int WriteRunPPP(void)
  1618. {
  1619. int i;
  1620. FILE *fp;
  1621. char command[256];
  1622. char run[256];
  1623. strcpy(run,ShortModelName[Model]);
  1624. strcat(run,"/run/");
  1625. strcat(run,run_ppp);
  1626. fp = fopen(run,"w");
  1627. if (fp == NULL)
  1628. {
  1629. printf("Could not open file <%s> for writing\n",run);
  1630. return 0; /* Failure */
  1631. }
  1632. fputs("#!/bin/bash\n",fp);
  1633. fprintf(fp,"# run-script generated by Most %s",ctime(&CurrentDate));
  1634. fputs("[ $# == 1 ] && cd $1\n",fp);
  1635. if (OroClear) fprintf(fp,"rm -f %s\n",oro_name);
  1636. else if (!OroAqua)
  1637. fprintf(fp,"[ ! -e %s -a -e ../dat/%s ] && cp -p ../dat/%s .\n",
  1638. oro_name,oro_name,oro_name);
  1639. fprintf(fp," ./%s >!ppp.out\n",exec_ppp);
  1640. fclose(fp);
  1641. sprintf(command,"chmod a+x %s",run);
  1642. system(command);
  1643. strcpy(command,run);
  1644. BlueMessage("Running PUMA Preprocessor");
  1645. sprintf(command,"%s/run/%s %s/run",ShortModelName[Model],run_ppp,ShortModelName[Model]);
  1646. system(command);
  1647. OroAno = 0; /* Anomaly applied -> reset it */
  1648. SelAno->iv = 0;
  1649. return 1; /* Success */
  1650. }
  1651. void WriteResmod(char *res)
  1652. {
  1653. FILE *fp;
  1654. int OldLat,OldLev,OldPro;
  1655. char Line[80];
  1656. // Read existing file if there and check for changes
  1657. fp = fopen(res,"r");
  1658. if (fp)
  1659. {
  1660. fgets(Line,80,fp); // header line 1
  1661. fgets(Line,80,fp); // header line 2
  1662. fgets(Line,80,fp);
  1663. OldLat = atoi(Line+27);
  1664. fgets(Line,80,fp);
  1665. OldLev = atoi(Line+27);
  1666. fgets(Line,80,fp);
  1667. OldPro = atoi(Line+27);
  1668. fclose(fp);
  1669. if (OldLat == Latitudes && OldLev == Levels && OldPro == Cores) return;
  1670. }
  1671. fp = fopen(res,"w");
  1672. if (fp == NULL)
  1673. {
  1674. printf("Could not open file <%s> for writing\n",res);
  1675. exit(1); /* Failure */
  1676. }
  1677. fprintf(fp," module resmod ! generated by MoSt %s\n",ctime(&CurrentDate));
  1678. fprintf(fp," parameter(NLAT_ATM = %d)\n",Latitudes);
  1679. fprintf(fp," parameter(NLEV_ATM = %d)\n",Levels);
  1680. fprintf(fp," parameter(NPRO_ATM = %d)\n",Cores);
  1681. fprintf(fp," end module resmod\n");
  1682. fclose(fp);
  1683. }
  1684. int Build(int model)
  1685. {
  1686. int i,l,x,y,ierr;
  1687. int porm;
  1688. FILE *fp;
  1689. char script_backup[256];
  1690. char command[256];
  1691. char message[256];
  1692. char bld[256];
  1693. char res[256];
  1694. char *shomo;
  1695. porm = MAX(Cores,Multirun);
  1696. shomo = ShortModelName[model];
  1697. strcpy(bld,shomo);
  1698. strcat(bld,"/bld/");
  1699. strcpy(res,bld);
  1700. strcat(bld,build_name);
  1701. strcat(res,res_name);
  1702. strcpy(script_backup,bld);
  1703. strcat(script_backup,".bak");
  1704. rename(bld,script_backup);
  1705. if (model == PLASIM) WriteResmod(res);
  1706. fp = fopen(bld,"w");
  1707. if (fp == NULL)
  1708. {
  1709. printf("Could not open file <%s> for writing\n",bld);
  1710. return 0; /* Failure */
  1711. }
  1712. fputs("#!/bin/bash\n",fp);
  1713. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1714. fputs("[ $# == 1 ] && cd $1\n",fp);
  1715. if (ForceRebuild) fputs("rm -f *.x *.o *.mod\n",fp);
  1716. fputs("cp -p ../src/* .\n",fp);
  1717. if (ngui == 0) // Use stub routines and switch off X11 lib
  1718. {
  1719. putenv("GUIMOD=guimod_stub");
  1720. putenv("PUMAX=pumax_stub");
  1721. putenv("GUIX11=guix11_stub");
  1722. putenv("GUILIB=");
  1723. }
  1724. if (Planet == MARS) putenv("PLAMOD=p_mars");
  1725. if (Planet == EXO ) putenv("PLAMOD=p_exo");
  1726. if (Lsg)
  1727. {
  1728. fputs("cp -p ../../lsg/src/lsgmod.f90 .\n",fp);
  1729. putenv("OCEANCOUP=cpl");
  1730. }
  1731. else
  1732. {
  1733. putenv("OCEANCOUP=cpl_stub");
  1734. }
  1735. if (Latitudes < 4) putenv("FFTMOD=fft991mod");
  1736. if (porm > 1)
  1737. {
  1738. fputs("[ ! -e MPI ] && rm -f *.o *.mod *.x\n",fp);
  1739. fputs("touch MPI\n",fp);
  1740. fputs("cat ../../most_compiler_mpi",fp);
  1741. if (Multirun > 1) putenv("MPIMOD=mpimod_multi");
  1742. }
  1743. else
  1744. {
  1745. fputs("[ -e MPI ] && rm -f *.o *.mod *.x MPI\n",fp);
  1746. fputs("cat ../../most_compiler",fp);
  1747. }
  1748. if (ndebug) fputs(" ../../most_debug_options",fp);
  1749. if (nprec)
  1750. {
  1751. fputs(" ../../most_precision_options",fp);
  1752. }
  1753. fprintf(fp," make_%s > makefile\n",shomo);
  1754. fputs("make -e\n",fp);
  1755. fprintf(fp,"[ $? == 0 ] && cp %s.x ../bin/%s\n",shomo,exec_name);
  1756. fclose(fp);
  1757. sprintf(command,"chmod a+x %s",bld);
  1758. system(command);
  1759. sprintf(message,"Building %s - wait a minute!",FullModelName[Model]);
  1760. BlueMessage(message);
  1761. strcat(bld," ");
  1762. strcat(bld,shomo);
  1763. strcat(bld,"/bld");
  1764. if (system(bld))
  1765. {
  1766. RedMessage("Error in build process");
  1767. sleep(5);
  1768. return 0; // error
  1769. }
  1770. sprintf(command,"cp %s/bin/%s %s/run\n",shomo,exec_name,shomo);
  1771. system(command);
  1772. // copy CAT simulation namelist to run directory
  1773. if (model == CAT)
  1774. {
  1775. sprintf(command,"cp cat/dat/sim_%4.4d.nl cat/run/sim_namelist\n",CatSim);
  1776. system(command);
  1777. }
  1778. // copy hires background bitmap for Earth or Mars
  1779. if (model == PUMA || model == PLASIM )
  1780. {
  1781. if (model == PLASIM && Planet == MARS)
  1782. {
  1783. sprintf(command,"cp images/mars.bmp %s/run/map.bmp\n",shomo);
  1784. system(command);
  1785. }
  1786. else if (model == PLASIM && Planet == EARTH)
  1787. {
  1788. sprintf(command,"cp images/earth.bmp %s/run/map.bmp\n",shomo);
  1789. system(command);
  1790. }
  1791. }
  1792. // copy surface data in matching resolution to run directory
  1793. if (model == PLASIM)
  1794. {
  1795. if (Planet == MARS)
  1796. sprintf(command,"cp plasim/dat/T%d_mars/* plasim/run/\n",Truncation);
  1797. else if (Planet == EXO)
  1798. sprintf(command,"cp plasim/dat/T%d_exo/* plasim/run/\n",Truncation);
  1799. else
  1800. sprintf(command,"cp plasim/dat/T%d/* plasim/run/\n",Truncation);
  1801. system(command);
  1802. if (Lsg) // copy data for LSG ocean model and select LSG GUI configuration
  1803. {
  1804. sprintf(command,"cp lsg/dat/* %s/run\n",shomo);
  1805. system(command);
  1806. sprintf(command,"cp %s/dat/GUI_LSG.cfg %s/run/GUI.cfg\n",shomo,shomo);
  1807. system(command);
  1808. }
  1809. else
  1810. {
  1811. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1812. system(command);
  1813. }
  1814. }
  1815. else
  1816. {
  1817. sprintf(command,"cp %s/dat/GUI.cfg %s/run\n",shomo,shomo);
  1818. system(command);
  1819. }
  1820. // copy second GUI configuration for two-instances run
  1821. if (Multirun > 1)
  1822. {
  1823. sprintf(command,"cp %s/dat/GUI_0?.cfg %s/run\n",shomo,shomo);
  1824. system(command);
  1825. }
  1826. return 1; /* Success */
  1827. }
  1828. int BuildPPP(void)
  1829. {
  1830. int i,l,x,y;
  1831. FILE *fp;
  1832. char script_backup[256];
  1833. char command[256];
  1834. char message[256];
  1835. char bld[256];
  1836. char res[256];
  1837. strcpy(bld,ShortModelName[Model]);
  1838. strcat(bld,"/bld/");
  1839. strcpy(res,bld);
  1840. strcat(bld,build_ppp);
  1841. strcat(res,res_name);
  1842. strcpy(script_backup,bld);
  1843. strcat(script_backup,".bak");
  1844. rename(bld,script_backup);
  1845. fp = fopen(bld,"w");
  1846. if (fp == NULL)
  1847. {
  1848. printf("Could not open file <%s> for writing\n",bld);
  1849. return 0; /* Failure */
  1850. }
  1851. fputs("#!/bin/bash\n",fp);
  1852. fprintf(fp,"# compile-script generated by Most %s",ctime(&CurrentDate));
  1853. fputs("[ $# == 1 ] && cd $1\n",fp);
  1854. fputs("rm -f *.o *.mod\n",fp);
  1855. fputs("cp ../src/make_ppp .\n",fp);
  1856. fputs("cp ../src/*.f90 .\n",fp);
  1857. fputs("cat ../../most_compiler",fp);
  1858. if (ndebug) fputs(" ../../most_debug_options",fp);
  1859. if (nprec) fputs(" ../../most_precision_options",fp);
  1860. fprintf(fp," make_ppp > makefile_ppp\n");
  1861. fputs("make -e -f makefile_ppp\n",fp);
  1862. fprintf(fp,"cp ppp.x ../bin/%s\n",exec_ppp);
  1863. fclose(fp);
  1864. sprintf(command,"chmod a+x %s",bld);
  1865. system(command);
  1866. sprintf(command,"%s/bin/%s",ShortModelName[Model],exec_ppp);
  1867. if (!(fp = fopen(command,"r")))
  1868. {
  1869. sprintf(message,"Building preprocessor ppp");
  1870. BlueMessage(message);
  1871. strcat(bld," ");
  1872. strcat(bld,ShortModelName[Model]);
  1873. strcat(bld,"/bld");
  1874. system(bld);
  1875. }
  1876. else fclose(fp); // Executable exists already
  1877. sprintf(command,"cp %s/bin/%s %s/run\n",ShortModelName[Model],exec_ppp,ShortModelName[Model]);
  1878. system(command);
  1879. return 1; /* Success */
  1880. }
  1881. void FinishLine(void)
  1882. {
  1883. char text[80];
  1884. int OldCores;
  1885. OldCores = Cores;
  1886. if (CursorSel == NULL) return;
  1887. if (CursorSel->type == SEL_INT)
  1888. {
  1889. CursorSel->iv = atoi(CursorSel->teva);
  1890. sprintf(text,"%10d",CursorSel->iv);
  1891. strcpy(CursorSel->teva,text+10-CursorSel->edco);
  1892. if (CursorSel->piv) *CursorSel->piv = CursorSel->iv;
  1893. }
  1894. if (CursorSel->type == SEL_REAL)
  1895. {
  1896. CursorSel->fv = atof(CursorSel->teva);
  1897. FormatReal(CursorSel->fv,text);
  1898. strcpy(CursorSel->teva,text);
  1899. }
  1900. /*
  1901. if (CursorSel == SelMulti) // Enable or disable Lat2
  1902. {
  1903. SelLat2->no = (SelMulti->iv != 2) ;
  1904. }
  1905. */
  1906. if (OldCores == 1 && Cores > 1) ForceRebuild = 1;
  1907. if (OldCores > 1 && Cores == 1) ForceRebuild = 1;
  1908. if (Model == PLASIM && OldCores != Cores) ForceRebuild = 1;
  1909. }
  1910. int CheckPumaNamelist(void)
  1911. {
  1912. int i,safe_ntspd;
  1913. int *ntspd;
  1914. int *nyoden = NULL;
  1915. double s;
  1916. struct SelStruct *Sel;
  1917. FILE *fp;
  1918. FinishLine();
  1919. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1920. {
  1921. if (Sel->piv) *Sel->piv = Sel->iv;
  1922. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1923. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1924. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1925. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1926. }
  1927. // Check for resolution defines as T value
  1928. if (Resolution > 0) Latitudes = ResLat[Resolution];
  1929. // Check # of latitudes for correct values (FFT requirements)
  1930. /*
  1931. if (Latitudes >= 2048) Latitudes = 2048; // T1365
  1932. else if (Latitudes >= 1024) Latitudes = 1024; // T682
  1933. else if (Latitudes >= 512) Latitudes = 512; // T341
  1934. else if (Latitudes >= 256) Latitudes = 256; // T170
  1935. else if (Latitudes >= 192) Latitudes = 192; // T127
  1936. else if (Latitudes >= 160) Latitudes = 160; // T106
  1937. else if (Latitudes >= 128) Latitudes = 128; // T85
  1938. else if (Latitudes >= 96) Latitudes = 96; // T63
  1939. else if (Latitudes >= 64) Latitudes = 64; // T42
  1940. else if (Latitudes >= 48) Latitudes = 48; // T31
  1941. else if (Latitudes >= 32) Latitudes = 32; // T21
  1942. else if (Latitudes >= 24) Latitudes = 24; // T15
  1943. else Latitudes = 16; // T10
  1944. */
  1945. if (Debug) printf("Latitudes = %d\n",Latitudes);
  1946. // Force # of levels to 20 for predefined Yoden profiles
  1947. if (nyoden && *nyoden > 0)
  1948. {
  1949. Levels = 20;
  1950. Yoden = *nyoden;
  1951. }
  1952. // Make sure # of CPU's is a power of 2 and in the proper range
  1953. if (Cores > Latitudes/2) Cores = Latitudes/2;
  1954. if (Cores < 1) Cores = 1;
  1955. if ((Latitudes/2) % Cores != 0) Cores = 1;
  1956. // Multirun is currently implemented for two runs only
  1957. if (Multirun >= 2)
  1958. {
  1959. Multirun = SelMulti->iv = 2;
  1960. Cores = SelCPU->iv = 1;
  1961. }
  1962. return 0; /* Success */
  1963. }
  1964. int CheckCATNamelist(void)
  1965. {
  1966. int i,safe_ntspd;
  1967. int *ntspd;
  1968. int *nyoden = NULL;
  1969. double s;
  1970. struct SelStruct *Sel;
  1971. FILE *fp;
  1972. FinishLine();
  1973. /*
  1974. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  1975. {
  1976. if (Sel->piv) *Sel->piv = Sel->iv;
  1977. if (!strcmp(Sel->text,"NTSPD" )) ntspd = &Sel->iv;
  1978. if (!strcmp(Sel->text,"NYODEN" )) nyoden = &Sel->iv;
  1979. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  1980. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  1981. }
  1982. */
  1983. // Check for resolution
  1984. if (Resolution == 1)
  1985. Latitudes = Latitude2 = 64;
  1986. else if (Resolution == 2)
  1987. Latitudes = Latitude2 = 256;
  1988. else if (Resolution == 3)
  1989. Latitudes = Latitude2 = 1024;
  1990. if (Debug) printf("NGX = NGY = %d\n",Latitudes);
  1991. return 0; /* Success */
  1992. }
  1993. int CheckPlasimNamelist(void)
  1994. {
  1995. int i,safe_mpstep;
  1996. int *mpstep;
  1997. double s;
  1998. struct SelStruct *Sel;
  1999. FILE *fp;
  2000. FinishLine();
  2001. for (Sel = &SelStart ; Sel ; Sel = Sel->Next)
  2002. {
  2003. if (Sel->piv) *Sel->piv = Sel->iv;
  2004. if (!strcmp(Sel->text,"MPSTEP")) mpstep = &Sel->iv;
  2005. if (!strcmp(Sel->text,"Orography" )) nreadsr = Sel->iv;
  2006. if (!strcmp(Sel->text,"Annual cycle")) nac = Sel->iv;
  2007. }
  2008. // Check for resolution defines as T value
  2009. if (Resolution > 0) Latitudes = ResLat[Resolution];
  2010. // LSG works currently only with T21 PlaSim
  2011. if (Lsg)
  2012. {
  2013. Resolution = RES_T21;
  2014. Latitudes = ResLat[RES_T21];
  2015. }
  2016. // Check # of latitudes for correct values (FFT requirements)
  2017. if (Latitudes >= 64) Latitudes = 64; // T42
  2018. else if (Latitudes >= 48) Latitudes = 48; // T31
  2019. else if (Latitudes >= 32) Latitudes = 32; // T21
  2020. else if (Latitudes >= 4) Latitudes = 4; // T2
  2021. else if (Latitudes >= 2) Latitudes = 2; // T1
  2022. // Set mpstep to a safe value
  2023. if (Resolution > 0) *mpstep = PlasimSteps[Resolution];
  2024. else
  2025. {
  2026. s = log(Latitudes / 32.0) / log(2.0);
  2027. safe_mpstep = 45.0 / pow(3.0,s);
  2028. if (*mpstep > safe_mpstep) *mpstep = safe_mpstep;
  2029. }
  2030. // Make sure # of CPU's is a power of 2 and in the proper range
  2031. if (Cores > Latitudes) Cores = Latitudes;
  2032. if (Cores < 1) Cores = 1;
  2033. if (Latitudes % Cores != 0) Cores = 1;
  2034. return 0; /* Success */
  2035. }
  2036. double *ReadGrid(const char *fn, int klat, double Sc)
  2037. {
  2038. int i,j,k,jlev,iteml,gridsize;
  2039. FILE *ifp;
  2040. int head[8];
  2041. double *gp;
  2042. char Line[256];
  2043. char Elem[16];
  2044. nlat = klat;
  2045. nlon = nlat + nlat;
  2046. VGAX = (double)sfbox_w / nlon;
  2047. VGAY = (double)sfbox_h / (nlat-1);
  2048. gridsize = (nlon+1) * nlat * sizeof(double);
  2049. gp = malloc(gridsize);
  2050. ifp = fopen(fn,"r");
  2051. if (!ifp) return NULL;
  2052. fgets(Line,sizeof(Line),ifp);
  2053. for (i = 0 ; i < 8 ; ++i)
  2054. {
  2055. strncpy(Elem,Line+i*10,10);
  2056. Elem[10] = 0;
  2057. head[i] = atoi(Elem);
  2058. }
  2059. if (head[4] == nlon && head[5] == nlat)
  2060. {
  2061. for (j = 0 ; j < nlat ; ++j)
  2062. {
  2063. for (i = 0 ; i < nlon ; i+=8)
  2064. {
  2065. fgets(Line,sizeof(Line),ifp);
  2066. iteml = strlen(Line) / 8;
  2067. for (k=0 ; k < 8 ; ++k)
  2068. {
  2069. strncpy(Elem,Line+k*iteml,iteml);
  2070. Elem[iteml] = 0;
  2071. gp[k+i+j*(nlon+1)] = atof(Elem) * Sc;
  2072. }
  2073. }
  2074. gp[nlon+j*(nlon+1)] = gp[j*(nlon+1)];
  2075. }
  2076. }
  2077. else printf("head[4] = %d head[5] = %d\n",head[4],head[5]);
  2078. fclose(ifp);
  2079. return gp;
  2080. }
  2081. void WriteOroNamelist(FILE *fp)
  2082. {
  2083. int lon1,lon2,lat1,lat2;
  2084. double w,h;
  2085. w = Frame[0].w;
  2086. h = Frame[0].h;
  2087. // Round to next gridpoint
  2088. lon1 = 1 + (Frame[0].xs * Latitudes*2) / w;
  2089. lon2 = 1 +((Frame[0].xs + Frame[0].ws) * Latitudes*2) / w;
  2090. lat1 = 1 + (Frame[0].ys * Latitudes) / h;
  2091. lat2 = 1 +((Frame[0].ys + Frame[0].hs) * Latitudes) / h;
  2092. fprintf(fp," %-8s=%d\n","NSRV" , 0);
  2093. fprintf(fp," %-8s=%d\n","NORO" ,noro);
  2094. fprintf(fp," %-8s=%d\n","OROANO" ,OroAno);
  2095. fprintf(fp," %-8s=%d\n","LON1ORO",lon1);
  2096. fprintf(fp," %-8s=%d\n","LON2ORO",lon2);
  2097. fprintf(fp," %-8s=%d\n","LAT1ORO",lat1);
  2098. fprintf(fp," %-8s=%d\n","LAT2ORO",lat2);
  2099. fprintf(fp," %-8s=%d\n","TGRANO" ,TgrAno);
  2100. lon1 = 1 + (Frame[1].xs * Latitudes*2) / Frame[1].w;
  2101. lon2 = 1 +((Frame[1].xs + Frame[1].ws) * Latitudes*2) / Frame[1].w;
  2102. lat1 = 1 + (Frame[1].ys * Latitudes) / Frame[1].h;
  2103. lat2 = 1 +((Frame[1].ys + Frame[1].hs) * Latitudes) / Frame[1].h;
  2104. fprintf(fp," %-8s=%d\n","LON1TGR",lon1);
  2105. fprintf(fp," %-8s=%d\n","LON2TGR",lon2);
  2106. fprintf(fp," %-8s=%d\n","LAT1TGR",lat1);
  2107. fprintf(fp," %-8s=%d\n","LAT2TGR",lat2);
  2108. return; /* Success */
  2109. }
  2110. void WriteResolutionNamelist(void)
  2111. {
  2112. int i;
  2113. char nln[256];
  2114. FILE *fp;
  2115. for (i = 0 ; i < Multirun ; ++i)
  2116. {
  2117. sprintf(nln,"%s/run/resolution_namelist",ShortModelName[Model]);
  2118. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",i);
  2119. fp = fopen(nln,"w");
  2120. if (fp == NULL)
  2121. {
  2122. printf("Could not open file <%s> for writing\n",nln);
  2123. return; /* Failure */
  2124. }
  2125. fprintf(fp," &RES\n");
  2126. fprintf(fp," NLAT = %d\n" ,Latitudes);
  2127. if (Model != SAM) fprintf(fp," NLEV = %d\n" ,Levels);
  2128. fprintf(fp," /END\n");
  2129. fclose(fp);
  2130. }
  2131. }
  2132. /* ================= */
  2133. /* WritePumaNamelist */
  2134. /* ================= */
  2135. int WritePumaNamelist(void) /* also used for model SAM */
  2136. {
  2137. int i,j,k,sum,val,imr;
  2138. FILE *fp;
  2139. char backup_name[256];
  2140. char nln[256];
  2141. struct SelStruct *Sel;
  2142. FinishLine();
  2143. // Write file <puma_namelist> or <sam_namelist>
  2144. for (imr = 0 ; imr < Multirun ; ++imr)
  2145. {
  2146. strcpy(nln,ShortModelName[Model]);
  2147. strcat(nln,"/run/");
  2148. strcat(nln,namelist_name);
  2149. if (Multirun > 1) sprintf(nln+strlen(nln),"_%2.2d",imr);
  2150. fp = fopen(nln,"w");
  2151. if (fp == NULL)
  2152. {
  2153. printf("Could not open file <%s> for writing\n",nln);
  2154. return 0; /* Failure */
  2155. }
  2156. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2157. if (Preprocessed || noro) nreadsr = 1;
  2158. fprintf(fp," %-8s=%6d\n","NOUTPUT" ,noutput);
  2159. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2160. if (nac)
  2161. {
  2162. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2163. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2164. }
  2165. else
  2166. {
  2167. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2168. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2169. }
  2170. if (ngui) fprintf(fp," %-8s=%6d\n","NYEARS",SimYears);
  2171. else fprintf(fp," %-8s=%6d\n","NYEARS",1);
  2172. fprintf(fp," %-8s=%6d\n","NMONTHS",0);
  2173. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2174. {
  2175. if (Sel->type == SEL_INT)
  2176. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2177. if (Sel->type == SEL_REAL)
  2178. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2179. }
  2180. // Check for mode selections
  2181. for (i=0,sum=0 ; i < DimSH ; ++i) sum += Ampli[i];
  2182. if (sum != DimSH) // Some modes are off
  2183. {
  2184. fprintf(fp," NSPECSEL = ");
  2185. i = 0;
  2186. while (i < DimSH)
  2187. {
  2188. val = Ampli[i];
  2189. j = i;
  2190. while (j < DimSH && Ampli[j] == val) ++j;
  2191. k = j - i;
  2192. if (k == 1) fprintf(fp,"%d",val);
  2193. else fprintf(fp,"%d*%d",k,val);
  2194. if (j < DimSH) fprintf(fp,",");
  2195. i = j;
  2196. }
  2197. fprintf(fp,"\n");
  2198. }
  2199. fprintf(fp," /END\n");
  2200. fclose(fp);
  2201. }
  2202. return 1; /* Success */
  2203. }
  2204. /* ================ */
  2205. /* WriteCatNamelist */
  2206. /* ================ */
  2207. int WriteCatNamelist(void)
  2208. {
  2209. int i,j,k,sum,val;
  2210. FILE *fp;
  2211. char backup_name[256];
  2212. char nln[256];
  2213. struct SelStruct *Sel;
  2214. FinishLine();
  2215. // Write file <cat_namelist>
  2216. strcpy(nln,ShortModelName[Model]);
  2217. strcat(nln,"/run/");
  2218. strcat(nln,namelist_name);
  2219. fp = fopen(nln,"w");
  2220. if (fp == NULL)
  2221. {
  2222. printf("Could not open file <%s> for writing\n",nln);
  2223. return 0; /* Failure */
  2224. }
  2225. fprintf(fp," &%s_nl\n",ShortModelName[Model]);
  2226. fprintf(fp," %-8s=%6d\n","NGUI" ,ngui);
  2227. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2228. {
  2229. if (Sel->type == SEL_INT)
  2230. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2231. if (Sel->type == SEL_REAL)
  2232. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2233. if (!strncmp(Sel->text,"NSIM",4))
  2234. {
  2235. if (Sel->iv > 0) CatSim = Sel->iv;
  2236. }
  2237. }
  2238. fprintf(fp," /END\n");
  2239. fclose(fp);
  2240. return 1; /* Success */
  2241. }
  2242. /* ================ */
  2243. /* WritePPPNamelist */
  2244. /* ================ */
  2245. int WritePPPNamelist(void)
  2246. {
  2247. int i,j,k,sum,val;
  2248. FILE *fp;
  2249. char backup_name[256];
  2250. char nln[256];
  2251. struct SelStruct *Sel;
  2252. FinishLine();
  2253. WriteResolutionNamelist();
  2254. /* 2. Write file <ppp_namelist> */
  2255. strcpy(nln,ShortModelName[Model]);
  2256. strcat(nln,"/run/");
  2257. strcat(nln,"ppp_namelist");
  2258. strcpy(backup_name,nln);
  2259. strcat(backup_name,".bak");
  2260. rename(nln,backup_name);
  2261. fp = fopen(nln,"w");
  2262. if (fp == NULL)
  2263. {
  2264. printf("Could not open file <%s> for writing\n",nln);
  2265. return 0; /* Failure */
  2266. }
  2267. fprintf(fp," &ppp_nl\n");
  2268. if (Preprocessed || noro) nreadsr = 1;
  2269. fprintf(fp," %-8s=%d\n","NOUTPUT" ,noutput);
  2270. fprintf(fp," %-8s=%d\n","NGUI" ,ngui);
  2271. if (nac)
  2272. {
  2273. fprintf(fp," %-8s=%11.4f\n","TAC" ,360.0);
  2274. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2275. }
  2276. else
  2277. {
  2278. fprintf(fp," %-8s=%11.4f\n","TAC" , 0.0);
  2279. fprintf(fp," %-8s=%11.4f\n","PAC" , 0.0);
  2280. }
  2281. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2282. {
  2283. if (Sel->type == SEL_INT)
  2284. fprintf(fp," %-8s=%6d\n",Sel->text,Sel->iv);
  2285. if (Sel->type == SEL_REAL)
  2286. fprintf(fp," %-8s=%s\n",Sel->text,Sel->teva);
  2287. }
  2288. WriteOroNamelist(fp);
  2289. fprintf(fp," /END\n");
  2290. fclose(fp);
  2291. return 1; /* Success */
  2292. }
  2293. /* ======== */
  2294. /* FillPoly */
  2295. /* ======== */
  2296. void FillPoly(int n, double Poly[])
  2297. {
  2298. int i;
  2299. XPoint xpol[8];
  2300. for (i=0; i < n ; ++i)
  2301. {
  2302. xpol[i].x = OffX + Poly[i+i ] + 0.5;
  2303. xpol[i].y = OffY + Poly[i+i+1] + 0.5;
  2304. }
  2305. XFillPolygon(display,pix,gc,xpol,n,Convex,CoordModeOrigin);
  2306. }
  2307. /* ======= */
  2308. /* IsoArea */
  2309. /* ======= */
  2310. void IsoArea(INT y, double vl, double vh, double Top[], double Bot[], INT Dim)
  2311. {
  2312. INT f,x,p;
  2313. double xl,xr,yt,yb;
  2314. double Poly[16];
  2315. for (x=0 ; x < Dim-1 ; x++)
  2316. {
  2317. Flag[x] = 0;
  2318. if (Top[x ] < vl) Flag[x] |= TOLELO;
  2319. if (Top[x ] >= vh) Flag[x] |= TOLEHI;
  2320. if (Top[x+1] < vl) Flag[x] |= TORILO;
  2321. if (Top[x+1] >= vh) Flag[x] |= TORIHI;
  2322. if (Bot[x ] < vl) Flag[x] |= BOLELO;
  2323. if (Bot[x ] >= vh) Flag[x] |= BOLEHI;
  2324. if (Bot[x+1] < vl) Flag[x] |= BORILO;
  2325. if (Bot[x+1] >= vh) Flag[x] |= BORIHI;
  2326. }
  2327. x = 0;
  2328. while (x < Dim-1)
  2329. {
  2330. xl = VGAX * x ;
  2331. xr = VGAX * (x+1);
  2332. yt = VGAY * y ;
  2333. yb = VGAY * (y+1);
  2334. f = Flag[x];
  2335. if (f == 0)
  2336. {
  2337. x++;
  2338. while (x < Dim-1 && Flag[x] == 0)
  2339. {
  2340. x++;
  2341. xr = VGAX * x;
  2342. }
  2343. Poly[0] = Poly[6] = xl;
  2344. Poly[1] = Poly[3] = yt;
  2345. Poly[2] = Poly[4] = xr;
  2346. Poly[5] = Poly[7] = yb;
  2347. FillPoly(4,Poly);
  2348. }
  2349. else if (f == (TOLELO | TORILO | BOLELO | BORILO) ||
  2350. f == (TOLEHI | TORIHI | BOLEHI | BORIHI)) x++;
  2351. else if (Top[x] < vl && Top[x+1] >= vl && Bot[x] >= vl && Bot[x+1] < vl)
  2352. {
  2353. Poly[1] = Poly[3] = yt;
  2354. Poly[2] = Poly[4] = xr;
  2355. Poly[0] = IPX(Top[x ],vl,Top[x+1]);
  2356. Poly[5] = IPY(Top[x+1],vl,Bot[x+1]);
  2357. FillPoly(3,Poly);
  2358. Poly[1] = Poly[3] = yb;
  2359. Poly[2] = Poly[4] = xl;
  2360. Poly[0] = IPX(Bot[x ],vl,Bot[x+1]);
  2361. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2362. FillPoly(3,Poly);
  2363. ++x;
  2364. }
  2365. else if (Top[x] >= vl && Top[x+1] < vl && Bot[x] < vl && Bot[x+1] >= vl)
  2366. {
  2367. Poly[0] = Poly[4] = xl;
  2368. Poly[1] = Poly[3] = yt;
  2369. Poly[2] = IPX(Top[x ],vl,Top[x+1]);
  2370. Poly[5] = IPY(Top[x ],vl,Bot[x ]);
  2371. FillPoly(3,Poly);
  2372. Poly[0] = Poly[2] = xr;
  2373. Poly[3] = Poly[5] = yb;
  2374. Poly[1] = IPY(Top[x+1],vl,Bot[x+1]);
  2375. Poly[4] = IPX(Bot[x ],vl,Bot[x+1]);
  2376. FillPoly(3,Poly);
  2377. ++x;
  2378. }
  2379. else
  2380. {
  2381. p = 0;
  2382. if (Top[x] < vl)
  2383. {
  2384. if (Top[x+1] >= vl)
  2385. {
  2386. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2387. Poly[p++] = yt;
  2388. }
  2389. if (Top[x+1] >= vh)
  2390. {
  2391. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2392. Poly[p++] = yt;
  2393. }
  2394. }
  2395. else if (Top[x] >= vh)
  2396. {
  2397. if (Top[x+1] < vh)
  2398. {
  2399. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2400. Poly[p++] = yt;
  2401. }
  2402. if (Top[x+1] < vl)
  2403. {
  2404. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2405. Poly[p++] = yt;
  2406. }
  2407. }
  2408. else
  2409. {
  2410. Poly[p++] = xl;
  2411. Poly[p++] = yt;
  2412. if (Top[x+1] < vl)
  2413. {
  2414. Poly[p++] = IPX(Top[x],vl,Top[x+1]);
  2415. Poly[p++] = yt;
  2416. }
  2417. if (Top[x+1] >= vh)
  2418. {
  2419. Poly[p++] = IPX(Top[x],vh,Top[x+1]);
  2420. Poly[p++] = yt;
  2421. }
  2422. }
  2423. if (Top[x+1] < vl)
  2424. {
  2425. if (Bot[x+1] >= vl)
  2426. {
  2427. Poly[p++] = xr;
  2428. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2429. }
  2430. if (Bot[x+1] >= vh)
  2431. {
  2432. Poly[p++] = xr;
  2433. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2434. }
  2435. }
  2436. else if (Top[x+1] >= vh)
  2437. {
  2438. if (Bot[x+1] < vh)
  2439. {
  2440. Poly[p++] = xr;
  2441. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2442. }
  2443. if (Bot[x+1] < vl)
  2444. {
  2445. Poly[p++] = xr;
  2446. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2447. }
  2448. }
  2449. else
  2450. {
  2451. Poly[p++] = xr;
  2452. Poly[p++] = yt;
  2453. if (Bot[x+1] < vl)
  2454. {
  2455. Poly[p++] = xr;
  2456. Poly[p++] = IPY(Top[x+1],vl,Bot[x+1]);
  2457. }
  2458. if (Bot[x+1] >= vh)
  2459. {
  2460. Poly[p++] = xr;
  2461. Poly[p++] = IPY(Top[x+1],vh,Bot[x+1]);
  2462. }
  2463. }
  2464. if (Bot[x+1] < vl)
  2465. {
  2466. if (Bot[x] >= vl)
  2467. {
  2468. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2469. Poly[p++] = yb;
  2470. }
  2471. if (Bot[x] >= vh)
  2472. {
  2473. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2474. Poly[p++] = yb;
  2475. }
  2476. }
  2477. else if (Bot[x+1] >= vh)
  2478. {
  2479. if (Bot[x] < vh)
  2480. {
  2481. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2482. Poly[p++] = yb;
  2483. }
  2484. if (Bot[x] < vl)
  2485. {
  2486. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2487. Poly[p++] = yb;
  2488. }
  2489. }
  2490. else
  2491. {
  2492. Poly[p++] = xr;
  2493. Poly[p++] = yb;
  2494. if (Bot[x] < vl)
  2495. {
  2496. Poly[p++] = IPX(Bot[x],vl,Bot[x+1]);
  2497. Poly[p++] = yb;
  2498. }
  2499. if (Bot[x] >= vh)
  2500. {
  2501. Poly[p++] = IPX(Bot[x],vh,Bot[x+1]);
  2502. Poly[p++] = yb;
  2503. }
  2504. }
  2505. if (Bot[x] < vl)
  2506. {
  2507. if (Top[x] >= vl)
  2508. {
  2509. Poly[p++] = xl;
  2510. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2511. }
  2512. if (Top[x] >= vh)
  2513. {
  2514. Poly[p++] = xl;
  2515. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2516. }
  2517. }
  2518. else if (Bot[x] >= vh)
  2519. {
  2520. if (Top[x] < vh)
  2521. {
  2522. Poly[p++] = xl;
  2523. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2524. }
  2525. if (Top[x] < vl)
  2526. {
  2527. Poly[p++] = xl;
  2528. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2529. }
  2530. }
  2531. else
  2532. {
  2533. Poly[p++] = xl;
  2534. Poly[p++] = yb;
  2535. if (Top[x] < vl)
  2536. {
  2537. Poly[p++] = xl;
  2538. Poly[p++] = IPY(Top[x],vl,Bot[x]);
  2539. }
  2540. if (Top[x] >= vh)
  2541. {
  2542. Poly[p++] = xl;
  2543. Poly[p++] = IPY(Top[x],vh,Bot[x]);
  2544. }
  2545. }
  2546. FillPoly(p>>1,Poly);
  2547. x++;
  2548. }
  2549. }
  2550. }
  2551. /* ======== */
  2552. /* IsoAreas */
  2553. /* ======== */
  2554. void IsoAreas(double *Field, int DimX, int DimY, struct ColorStrip Strip[])
  2555. {
  2556. INT i;
  2557. INT y;
  2558. double *Top;
  2559. double *Bot;
  2560. i = 0;
  2561. while (Strip[i].Name)
  2562. {
  2563. Top = Field;
  2564. Bot = Field + DimX;
  2565. XSetForeground(display,gc,Strip[i].pixel);
  2566. for (y = 0 ; y < DimY-1 ; y++)
  2567. {
  2568. IsoArea(y,Strip[i].Lo,Strip[i].Hi,Top,Bot,DimX);
  2569. Top += DimX;
  2570. Bot += DimX;
  2571. }
  2572. ++i;
  2573. }
  2574. }
  2575. /* ========= */
  2576. /* CalcFrame */
  2577. /* ========= */
  2578. int CalcFrame(int lats)
  2579. {
  2580. // int i,l,x,y;
  2581. // double *Grid;
  2582. //
  2583. // nlat = lats;
  2584. // nlon = lats * 2;
  2585. // VGAX = (sfbox_w - 1.0) / nlon;
  2586. // VGAY = (sfbox_h - 1.0) / (nlat-1);
  2587. //
  2588. // if (!Flag) Flag = malloc((nlon+1) * sizeof(int));
  2589. // if (!Frame[0].Grid) Frame[0].Grid = malloc(nlon * nlat * nlev * sizeof(double));
  2590. // if (!Frame[0].pixmap) Frame[0].pixmap = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2591. // pix = Frame[0].pixmap;
  2592. // Grid = Frame[0].Grid;
  2593. // IsoAreas(Grid,nlon+1,nlat,Frame[0].Strip);
  2594. return 0;
  2595. }
  2596. Pixmap IsoPixmap(double *Grid, int nlon, int nlat, struct ColorStrip Strip[])
  2597. {
  2598. pix = XCreatePixmap(display,Cow,sfbox_w,sfbox_h,ScreenD);
  2599. //XFillRectangle(display,Cow,gc,0,0,Frame[0].w,Frame[0].h);
  2600. Flag = realloc(Flag,(nlon+1) * sizeof(int));
  2601. IsoAreas(Grid,nlon+1,nlat,Strip);
  2602. //XSetForeground(display,gc,Blue.pixel);
  2603. //XDrawRectangle(display,Cow,gc,Frame[0].x-1,Frame[0].y-1,Frame[0].w,Frame[0].h);
  2604. return pix;
  2605. }
  2606. int PPPCompiled(void)
  2607. {
  2608. FILE *fp;
  2609. char fn[128];
  2610. strcpy(fn,"puma/run/");
  2611. strcat(fn,exec_ppp);
  2612. if ((fp = fopen(fn,"r")))
  2613. {
  2614. fclose(fp);
  2615. return 1;
  2616. }
  2617. return 0;
  2618. }
  2619. void ShowOrography(void)
  2620. {
  2621. int i,m,n,k,l,x,y,h,w,lon1,lon2,lat1,lat2,tx,ty,dx;
  2622. int r,len,width,height,xp,yp;
  2623. char Text[80];
  2624. double flon,flat;
  2625. XSetFont(display, gc, FixFont->fid);
  2626. XSetForeground(display,gc,TextC);
  2627. XSetBackground(display,gc,WinBG);
  2628. if (Model == PLASIM)
  2629. {
  2630. if (Planet == MARS)
  2631. {
  2632. XCopyArea(display,OpmMars,Cow,gc,0,0,
  2633. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2634. }
  2635. else if (Planet == EARTH)
  2636. {
  2637. XCopyArea(display,OpmEarth,Cow,gc,0,0,
  2638. Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2639. }
  2640. return;
  2641. }
  2642. k = 0; // Frame number 0 reserved for topography
  2643. if (noro == 0) // No orography
  2644. {
  2645. XSetForeground(display,gc,Blue.pixel);
  2646. XFillRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2647. XSetForeground(display,gc,WhitePix);
  2648. XSetBackground(display,gc,Blue.pixel);
  2649. l = strlen("Aqua Planet Setup");
  2650. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2651. ty = Frame[k].y + Frame[k].h / 2 - FixFontHeight;
  2652. XDrawImageString(display,Cow,gc,tx,ty,"Aqua Planet Setup",l);
  2653. l = strlen("To change mark option 'Orography'");
  2654. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2655. ty += FixFontHeight;
  2656. XDrawImageString(display,Cow,gc,tx,ty,"To change mark option 'Orography'",l);
  2657. XSetBackground(display,gc,WinBG);
  2658. }
  2659. else
  2660. {
  2661. XCopyArea(display,OpmPrep,Cow,gc,0,0,Frame[0].w,Frame[0].h,Frame[0].x,Frame[0].y);
  2662. XDrawRectangle(display,Cow,gc,Frame[k].x-1,Frame[k].y-1,Frame[k].w,Frame[k].h);
  2663. l = strlen(Frame[k].t[0]);
  2664. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2665. ty = Frame[k].y - FixFont->descent - 2;
  2666. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2667. }
  2668. if (noro && (Button1Down || (Frame[k].ws && Frame[k].hs)))
  2669. {
  2670. x = Frame[k].x+Frame[k].xs;
  2671. y = Frame[k].y+Frame[k].ys;
  2672. w = Frame[k].ws;
  2673. h = Frame[k].hs;
  2674. if (w < 0) {x += w; w = -w;}
  2675. if (h < 0) {y += h; h = -h;}
  2676. XSetForeground(display,gc,WhitePix);
  2677. XDrawArc(display,Cow,gc,x,y,w,h,0,FULLARC);
  2678. XSetForeground(display,gc,BlackPix);
  2679. XDrawArc(display,Cow,gc,x-1,y-1,w+2,h+2,0,FULLARC);
  2680. flon = 360.0 / Frame[k].w;
  2681. flat = 180.0 / Frame[k].h;
  2682. lon1 = flon * (x - Frame[k].x);
  2683. lon2 = lon1 + flon * w;
  2684. lat1 = 90.0 - flat * (y - Frame[k].y);
  2685. lat2 = lat1 - flat * h;
  2686. sprintf(Frame[k].t[1],"Lon: (%3d:%3d) Lat: (%3d:%3d)",lon1,lon2,lat2,lat1);
  2687. l = strlen(Frame[k].t[1]);
  2688. tx = Frame[k].x;
  2689. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2690. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2691. XSetForeground(display,gc,WhitePix);
  2692. sprintf(Frame[k].t[1],"Mouse marks area ");
  2693. l = strlen(Frame[k].t[1]);
  2694. tx = Frame[k].x;
  2695. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2696. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2697. }
  2698. }
  2699. void ShowMars(void)
  2700. {
  2701. if (Debug) printf("ShowMars\n");
  2702. XPutImage(display,Cow,gc,MapLRM.X,0,0,Frame[1].x,Frame[1].y,MapLRM.w,MapLRM.h);
  2703. }
  2704. void ShowExo(void)
  2705. {
  2706. XPutImage(display,Cow,gc,MapLRL.X,0,0,Frame[0].x,Frame[0].y,MapLRL.w,MapLRL.h);
  2707. XPutImage(display,Cow,gc,MapLRK.X,0,0,Frame[1].x,Frame[1].y,MapLRK.w,MapLRK.h);
  2708. }
  2709. void ShowEarth(void)
  2710. {
  2711. if (Debug) printf("ShowEarth\n");
  2712. XPutImage(display,Cow,gc,MapLRE.X,0,0,Frame[1].x,Frame[1].y,MapLRE.w,MapLRE.h);
  2713. }
  2714. void ShowModeSelector(void)
  2715. {
  2716. int k,dx,r,m,i,n,len,width,height,xp,yp,l,tx,ty;
  2717. char Text[80];
  2718. if (Debug) printf("ShowModeSelector\n");
  2719. k = 1; // Frame number 1 reserved for mode display
  2720. pix = Frame[k].pixmap;
  2721. strcpy(Frame[1].t[0],"Spherical Harmonics mode selector");
  2722. dx = dxsh;
  2723. r = (dx+dx) / 3;
  2724. XSetForeground(display,gc,BlackPix);
  2725. XFillRectangle(display,pix,gc,0,0,Frame[k].w,Frame[k].h);
  2726. for (m=0,i=0 ; m <= DimTr ; ++m)
  2727. {
  2728. for (n=m ; n <= DimTr ; ++n,++i)
  2729. {
  2730. if (Ampli[i]) XSetForeground(display,gc,Green.pixel);
  2731. else XSetForeground(display,gc, Red.pixel);
  2732. XFillArc(display,pix,gc,ModeX[i]-dxs2,ModeY[i]-dxs2,r,r,0,360*64);
  2733. }
  2734. }
  2735. /* Draw mode legend */
  2736. XSetForeground(display,gc,Yellow.pixel);
  2737. XSetBackground(display,gc,BlackPix);
  2738. XSetFont(display, gc, ModFont->fid);
  2739. for (i=0 ; i < 21 ; i+=2)
  2740. {
  2741. sprintf(Text,"%d",i);
  2742. len = strlen(Text);
  2743. width = XTextWidth(ModFont,Text,len);
  2744. height = ModFont->ascent + ModFont->descent;
  2745. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  2746. yp = ModFontHeight;
  2747. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2748. }
  2749. strcpy(Text,"PUMA T21 only!");
  2750. len = strlen(Text);
  2751. xp = dx - ModFontWidth/2 + 1;
  2752. yp = 2 * ModFontHeight + 10 * dx;
  2753. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2754. XSetForeground(display,gc,WhitePix);
  2755. strcpy(Text,"MB 1: Toggle mode");
  2756. len = strlen(Text);
  2757. yp += 2*dx;
  2758. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2759. strcpy(Text,"MB 2: Toggle column");
  2760. len = strlen(Text);
  2761. yp += dx;
  2762. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2763. strcpy(Text,"MB 3: Toggle row");
  2764. len = strlen(Text);
  2765. yp += dx;
  2766. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2767. XSetForeground(display,gc,Yellow.pixel);
  2768. strcpy(Text,"n : Total Wavenumber");
  2769. len = strlen(Text);
  2770. xp = dx - ModFontWidth/2 + 1;
  2771. yp = 2 * ModFontHeight + 18 * dx;
  2772. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2773. XSetForeground(display,gc,LightBlue.pixel);
  2774. for (i=0 ; i < 21 ; i+=2)
  2775. {
  2776. sprintf(Text,"%d",i);
  2777. len = strlen(Text);
  2778. width = XTextWidth(ModFont,Text,len);
  2779. height = ModFont->ascent + ModFont->descent;
  2780. xp = Frame[k].w - width - 2;
  2781. yp = 2 * ModFontHeight + i * dx;
  2782. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2783. }
  2784. strcpy(Text,"m : Zonal Wavenumber");
  2785. len = strlen(Text);
  2786. xp = dx - ModFontWidth/2 + 1;
  2787. yp = 2 * ModFontHeight + 19 * dx;
  2788. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2789. XSetForeground(display,gc,Green.pixel);
  2790. strcpy(Text,"Mode is on");
  2791. len = strlen(Text);
  2792. xp = dx - ModFontWidth/2 + 1;
  2793. yp = ModFontHeight + 16 * dx;
  2794. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2795. xp += dx;
  2796. yp += dx;
  2797. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2798. XSetForeground(display,gc,Red.pixel);
  2799. strcpy(Text,"Mode is off");
  2800. len = strlen(Text);
  2801. xp = dx - ModFontWidth/2 + 1;
  2802. yp = ModFontHeight + 17 * dx;
  2803. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2804. xp += dx;
  2805. yp += dx;
  2806. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2807. XSetForeground(display,gc,WhitePix);
  2808. strcpy(Text,"Switch all modes on");
  2809. len = strlen(Text);
  2810. xp = dx - ModFontWidth/2 + 1;
  2811. yp = ModFontHeight + 20 * dx;
  2812. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2813. xp += dx;
  2814. yp += dx;
  2815. XSetForeground(display,gc,Green.pixel);
  2816. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2817. XSetForeground(display,gc,WhitePix);
  2818. strcpy(Text,"Switch all modes off");
  2819. len = strlen(Text);
  2820. xp = dx - ModFontWidth/2 + 1;
  2821. yp = ModFontHeight + 21 * dx;
  2822. XFillArc(display,pix,gc,xp,yp+ModFont->descent+2,r,r,0,360*64);
  2823. xp += dx;
  2824. yp += dx;
  2825. XSetForeground(display,gc,Red.pixel);
  2826. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  2827. XCopyArea(display,Frame[k].pixmap,Cow,gc,0,0,Frame[k].w,Frame[k].h,Frame[k].x,Frame[k].y);
  2828. XSetForeground(display,gc,Cyan.pixel);
  2829. l = strlen(Frame[k].t[0]);
  2830. tx = Frame[k].x + (Frame[k].w - l * FixFontWidth) / 2;
  2831. ty = Frame[k].y - FixFont->descent - 2;
  2832. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[0],l);
  2833. XSetForeground(display,gc,LightBlue.pixel);
  2834. l = strlen(Frame[k].t[1]);
  2835. if (l)
  2836. {
  2837. tx = Frame[k].x + (3 * ModFontWidth) / 2;
  2838. ty = Frame[k].y + Frame[k].h + FixFont->ascent;
  2839. XDrawImageString(display,Cow,gc,tx,ty,Frame[k].t[1],l);
  2840. }
  2841. }
  2842. void ShowFrame1(void)
  2843. {
  2844. if (Model == PLASIM)
  2845. {
  2846. if (Planet == MARS) ShowMars();
  2847. else if (Planet == EXO ) ShowExo();
  2848. else ShowEarth();
  2849. }
  2850. else ShowModeSelector();
  2851. }
  2852. void PreProcess(void)
  2853. {
  2854. FinishLine();
  2855. if (Model == PUMA)
  2856. {
  2857. if (CheckPumaNamelist()) return;
  2858. GenerateNames();
  2859. WritePPPNamelist();
  2860. WritePumaNamelist();
  2861. if (!PPPCompiled()) BuildPPP();
  2862. WriteRunPPP();
  2863. Preprocessed = 1;
  2864. if (Latitudes < 1000)
  2865. sprintf(oro_name,"puma/run/N%3.3d_surf_0129.sra",Latitudes);
  2866. else
  2867. sprintf(oro_name,"puma/run/N%d_surf_0129.sra",Latitudes);
  2868. OroPrep = ReadGrid(oro_name,Latitudes,RevGra);
  2869. OpmPrep = IsoPixmap(OroPrep,2*Latitudes,Latitudes,OroStrip);
  2870. free(OroPrep);
  2871. OroClear = 0;
  2872. }
  2873. else
  2874. {
  2875. RedMessage("Preprocessing is for PUMA only");
  2876. sleep(5);
  2877. }
  2878. }
  2879. void ClearOro(void)
  2880. {
  2881. OroClear = 1;
  2882. OroAqua = 1;
  2883. OroAno = 0;
  2884. PreProcess();
  2885. }
  2886. void WriteNamelistFile(char *nl, int instance)
  2887. {
  2888. char *shomo = ShortModelName[Model];
  2889. struct SelStruct *Sel;
  2890. char fn[256];
  2891. FILE *fp;
  2892. if (Multirun > 1)
  2893. sprintf(fn,"%s/run/%s_namelist_%2.2d",shomo,nl,instance);
  2894. else
  2895. sprintf(fn,"%s/run/%s_namelist",shomo,nl);
  2896. fp = fopen(fn,"w");
  2897. if (fp == NULL)
  2898. {
  2899. printf("Could not open file <%s> for writing\n",fn);
  2900. return; /* Failure */
  2901. }
  2902. // Write namelist file
  2903. fprintf(fp," &%s_nl\n",nl);
  2904. // Write special parameters
  2905. if (!strcmp(nl,"icemod"))
  2906. {
  2907. fprintf(fp," %-12s=%6d\n","NICE",Ice);
  2908. }
  2909. if (!strcmp(nl,"oceanmod"))
  2910. {
  2911. fprintf(fp," %-12s=%6d\n","NOCEAN",Oce);
  2912. //FL0318
  2913. if (Lsg)
  2914. {
  2915. fprintf(fp," %-12s=%6d\n","NLSG" ,Lsg);
  2916. }
  2917. }
  2918. if (!strcmp(nl,"plasim"))
  2919. {
  2920. fprintf(fp," %-12s=%6d\n","NOUTPUT",noutput);
  2921. fprintf(fp," %-12s=%6d\n","NGUI" ,ngui);
  2922. fprintf(fp," %-12s=%6d\n","N_START_YEAR",SimStart);
  2923. if (Lsg) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",360);
  2924. else if (Planet != MARS) fprintf(fp," %-12s=%6d\n","N_DAYS_PER_YEAR",365);
  2925. if (ngui) fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",SimYears);
  2926. else fprintf(fp," %-12s=%6d\n","N_RUN_YEARS",1);
  2927. fprintf(fp," %-12s=%6d\n","N_RUN_MONTHS",0);
  2928. fprintf(fp," %-12s=%6d\n","N_RUN_DAYS",0);
  2929. }
  2930. for (Sel = ComEnd->Next ; Sel ; Sel = Sel->Next)
  2931. {
  2932. if (Sel->Item && !strcasecmp(Sel->Item->list,nl))
  2933. {
  2934. if (Sel->type == SEL_TEVA)
  2935. fprintf(fp," %-12s=%c%s%c\n",Sel->text,'\"',Sel->teva,'\"');
  2936. else
  2937. fprintf(fp," %-12s=%s\n",Sel->text,Sel->teva);
  2938. }
  2939. }
  2940. fprintf(fp," /END\n");
  2941. fclose(fp);
  2942. }
  2943. /* =================== */
  2944. /* WritePlasimNamelist */
  2945. /* =================== */
  2946. void WritePlasimNamelist(void)
  2947. {
  2948. int imr;
  2949. FinishLine();
  2950. for (imr = 0 ; imr < Multirun ; ++imr)
  2951. {
  2952. WriteNamelistFile("fluxmod" ,imr);
  2953. WriteNamelistFile("icemod" ,imr);
  2954. WriteNamelistFile("landmod" ,imr);
  2955. WriteNamelistFile("vegmod" ,imr);
  2956. WriteNamelistFile("miscmod" ,imr);
  2957. WriteNamelistFile("oceanmod",imr);
  2958. WriteNamelistFile("planet" ,imr);
  2959. WriteNamelistFile("plasim" ,imr);
  2960. WriteNamelistFile("radmod" ,imr);
  2961. WriteNamelistFile("rainmod" ,imr);
  2962. WriteNamelistFile("seamod" ,imr);
  2963. WriteNamelistFile("surfmod" ,imr);
  2964. }
  2965. }
  2966. void BuildScripts(void)
  2967. {
  2968. if (Model == PUMA)
  2969. {
  2970. if (CheckPumaNamelist()) return;
  2971. GenerateNames();
  2972. WritePumaNamelist();
  2973. if (!Build(PUMA)) Exit();
  2974. WriteRunScript(PUMA);
  2975. }
  2976. if (Model == SAM)
  2977. {
  2978. if (CheckPumaNamelist()) return;
  2979. GenerateNames();
  2980. WritePumaNamelist();
  2981. if (!Build(SAM)) Exit();
  2982. WriteRunScript(SAM);
  2983. }
  2984. if (Model == CAT)
  2985. {
  2986. if (CheckCATNamelist()) return;
  2987. GenerateNames();
  2988. WriteCatNamelist();
  2989. if (!Build(CAT)) Exit();
  2990. WriteRunScript(CAT);
  2991. }
  2992. if (Model == PLASIM)
  2993. {
  2994. CheckPlasimNamelist();
  2995. GenerateNames();
  2996. WritePlasimNamelist();
  2997. if (!Build(PLASIM)) Exit();
  2998. WriteRunScript(PLASIM);
  2999. }
  3000. }
  3001. void SaveExit(void)
  3002. {
  3003. BuildScripts();
  3004. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3005. Exit();
  3006. }
  3007. void SaveRun(void)
  3008. {
  3009. char command[256];
  3010. BuildScripts();
  3011. if ((noro || Yoden) && !Preprocessed && Model == PUMA) PreProcess();
  3012. sprintf(command,"%s/run/%s %s/run &",ShortModelName[Model],run_name,ShortModelName[Model]);
  3013. system(command);
  3014. printf("\n=== Success: Launched process %s ===\n\n",run_name);
  3015. Exit();
  3016. }
  3017. void MarkRectangle(void)
  3018. {
  3019. if (Model != PUMA) return;
  3020. if (WinEvent.type == ButtonPress)
  3021. {
  3022. Frame[FrameNo].xs = WinEvent.xbutton.x - Frame[FrameNo].x;
  3023. Frame[FrameNo].ys = WinEvent.xbutton.y - Frame[FrameNo].y;
  3024. Frame[FrameNo].ws = 0;
  3025. Frame[FrameNo].hs = 0;
  3026. }
  3027. if (WinEvent.type == ButtonRelease)
  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. if (WinEvent.type == MotionNotify && Button1Down)
  3033. {
  3034. Frame[FrameNo].ws = WinEvent.xbutton.x - Frame[FrameNo].x - Frame[FrameNo].xs;
  3035. Frame[FrameNo].hs = WinEvent.xbutton.y - Frame[FrameNo].y - Frame[FrameNo].ys;
  3036. }
  3037. }
  3038. /* ==================================================== */
  3039. /* AzimuthalImage - Display map in azimuthal projection */
  3040. /* ==================================================== */
  3041. void AzimuthalImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3042. {
  3043. int lam; // lambda pixel coordinate in source image
  3044. int phi; // phi pixel coordinate in source image
  3045. int x ; // x pixel coordinate in destination image
  3046. int y ; // y pixel coordinate in destination image
  3047. int dxc; // pixel coordinate of centre position
  3048. int dxl; // pixel coordinate of left most position
  3049. int dxr; // pixel coordinate of right most position
  3050. int dx ; // centre relative x position
  3051. int dy ; // centre relative y position
  3052. int p00; // euqator
  3053. int l00; // reference longitude
  3054. unsigned int dih; // destination image height
  3055. unsigned int diw; // destination image width
  3056. unsigned int dpw; // destination image padded width
  3057. double rad; // pixel radius of new image
  3058. double ysc; // yscale = source height / destination height
  3059. double rho; // distance from centre
  3060. double xpi; // x scale factor = 2 * PI / width
  3061. double ypi; // y scale factor = PI / height
  3062. double xrf;
  3063. double yrf;
  3064. XImage *sX; // source image
  3065. XImage *dX; // destination image
  3066. // Destroy old image structure inclusive data storage
  3067. if (d->X) XDestroyImage(d->X);
  3068. // Set width of new image
  3069. diw = d->w;
  3070. // Pad width of new image to a multiple of 8
  3071. dpw = (diw + 7) & 0xFFF8;
  3072. // Set height of new image
  3073. dih = d->h;
  3074. // Allocate space for image data
  3075. d->d = calloc(dpw * dih,4);
  3076. // Create image structure
  3077. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3078. sX = s->X;
  3079. p00 = s->h >> 1;
  3080. rad = dih >> 1;
  3081. dxc = diw >> 1;
  3082. dxl = MAX(dxc - rad, 0);
  3083. dxr = MIN(dxc + rad, diw);
  3084. xpi = s->w / M_PI * 0.5;
  3085. ypi = s->h / M_PI;
  3086. ysc = (double)s->h / (double)dih;
  3087. l00 = (int)((d->l * s->w) / 360 + s->w/2) % s->w;
  3088. xrf = (double)diw / pow(2.0,31),
  3089. yrf = (double)dih / pow(2.0,31);
  3090. // Paint some stars on the sky
  3091. srandom(Seed);
  3092. for (y = 0 ; y < (diw * dih) >> 8 ; ++y)
  3093. XPutPixel(dX,xrf*random(),yrf*random(),WhitePix);
  3094. for (y = 0 ; y < dih ; ++y)
  3095. {
  3096. dy = y - rad;
  3097. phi = y * ysc;
  3098. for (x = dxl ; x < dxr ; ++x)
  3099. {
  3100. dx = x - dxc;
  3101. rho = sqrt(dx * dx + dy * dy);
  3102. if (rho < rad)
  3103. {
  3104. lam = l00 + xpi * atan2(dx / rad, cos(asin(rho / rad)));
  3105. phi = p00 + ypi * asin(dy / rad);
  3106. XPutPixel(dX,x,y,XGetPixel(sX,lam,phi));
  3107. }
  3108. }
  3109. }
  3110. }
  3111. /* =============================================== */
  3112. /* RectImage - Display map in azimuthal projection */
  3113. /* =============================================== */
  3114. void RectImage(struct MapImageStruct *s, struct MapImageStruct *d)
  3115. {
  3116. int x ; // x pixel coordinate in destination image
  3117. int y ; // y pixel coordinate in destination image
  3118. unsigned int dih; // destination image height
  3119. unsigned int diw; // destination image width
  3120. unsigned int dpw; // destination image padded width
  3121. XImage *sX; // source image
  3122. XImage *dX; // destination image
  3123. // Destroy old image structure inclusive data storage
  3124. if (d->X) XDestroyImage(d->X);
  3125. // Set width of new image
  3126. diw = d->w;
  3127. // Pad width of new image to a multiple of 8
  3128. dpw = (diw + 7) & 0xFFF8;
  3129. // Set height of new image
  3130. dih = d->h;
  3131. // Allocate space for image data
  3132. d->d = calloc(dpw * dih,4);
  3133. // Create image structure
  3134. dX = d->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,d->d,dpw,dih,8,0);
  3135. sX = s->X;
  3136. for (y = 0 ; y < dih ; ++y)
  3137. {
  3138. for (x = 0 ; x < diw ; ++x)
  3139. {
  3140. XPutPixel(dX,x,y,XGetPixel(sX,x,y));
  3141. }
  3142. }
  3143. }
  3144. void ToggleMode(void)
  3145. {
  3146. int i,j,m,n,mx,my,dx,dy;
  3147. if (Model == PLASIM) // Rotate image
  3148. {
  3149. if (WinEvent.xbutton.button == Button1) // Rotate right
  3150. {
  3151. if (Planet == MARS)
  3152. {
  3153. MapLRM.l += 10;
  3154. if (MapLRM.l > 180) MapLRM.l -= 360;
  3155. AzimuthalImage(&MapHRM,&MapLRM);
  3156. }
  3157. else
  3158. {
  3159. MapLRE.l += 10;
  3160. if (MapLRE.l > 180) MapLRE.l -= 360;
  3161. AzimuthalImage(&MapHRE,&MapLRE);
  3162. }
  3163. }
  3164. else if (WinEvent.xbutton.button == Button3) // Rotate left
  3165. {
  3166. if (Planet == MARS)
  3167. {
  3168. MapLRM.l -= 10;
  3169. if (MapLRM.l < -180) MapLRM.l += 360;
  3170. AzimuthalImage(&MapHRM,&MapLRM);
  3171. }
  3172. else
  3173. {
  3174. MapLRE.l -= 10;
  3175. if (MapLRE.l < -180) MapLRE.l += 360;
  3176. AzimuthalImage(&MapHRE,&MapLRE);
  3177. }
  3178. }
  3179. return;
  3180. }
  3181. if (WinEvent.type == ButtonPress)
  3182. {
  3183. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3184. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3185. for (i=0 ; i < DimSE ; ++i)
  3186. {
  3187. dx = mx - ModeX[i];
  3188. dy = my - ModeY[i];
  3189. if ((dx*dx + dy*dy) < ModeRadiusSq) break;
  3190. }
  3191. if (WinEvent.xbutton.button == Button1)
  3192. {
  3193. if (i == DimSH ) // All modes on
  3194. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 1;
  3195. else if (i == DimSH+1) // All modes off
  3196. for (j=0 ; j < DimSH ; ++j) Ampli[j] = 0;
  3197. else Ampli[i] = 1 - Ampli[i];
  3198. }
  3199. else if (WinEvent.xbutton.button == Button2 && i < DimSH)
  3200. {
  3201. n = ModeN[i];
  3202. for (j=0 ; j < DimSH ; ++j)
  3203. if (ModeN[j] == n) Ampli[j] = 1 - Ampli[j];
  3204. }
  3205. else if (WinEvent.xbutton.button == Button3 && i < DimSH)
  3206. {
  3207. m = ModeM[i];
  3208. for (j=0 ; j < DimSH ; ++j)
  3209. if (ModeM[j] == m) Ampli[j] = 1 - Ampli[j];
  3210. }
  3211. }
  3212. if (WinEvent.type == MotionNotify)
  3213. {
  3214. mx = WinEvent.xbutton.x - Frame[FrameNo].x;
  3215. my = WinEvent.xbutton.y - Frame[FrameNo].y;
  3216. for (i=0 ; i < DimSH ; ++i)
  3217. {
  3218. dx = mx - ModeX[i];
  3219. dy = my - ModeY[i];
  3220. if ((dx*dx + dy*dy) < ModeRadiusSq)
  3221. {
  3222. sprintf(Frame[1].t[1],"Mode (%2d:%2d)",ModeN[i],ModeM[i]);
  3223. return;
  3224. }
  3225. }
  3226. Frame[1].t[1][0] = 0;
  3227. }
  3228. return;
  3229. }
  3230. void InitFrames(void)
  3231. {
  3232. int i,j,n;
  3233. int moselw;
  3234. FILE *ftp;
  3235. /* Size and position of orography window */
  3236. sfbox_w = 360;
  3237. sfbox_h = 180;
  3238. sfbox_x = WINDOW_WIDTH - sfbox_w - 3 * FixFontWidth;
  3239. sfbox_y = nlbox_y + 4;
  3240. /* Parameter for default orographies */
  3241. nlat = 64;
  3242. nlon = nlat * 2;
  3243. VGAX = (sfbox_w - 1.0) / nlon;
  3244. VGAY = (sfbox_h - 1.0) / (nlat-1);
  3245. /* Read T42 orography for Earth and Mars */
  3246. OroEarth = ReadGrid("plasim/dat/T42/N064_surf_0129.sra",nlat,RevGra);
  3247. OroMars = ReadGrid("plasim/dat/T42_mars/N064_surf_0129.sra",nlat,1.0/3.74);
  3248. OpmEarth = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3249. OpmPrep = IsoPixmap(OroEarth,nlon,nlat,OroStrip);
  3250. OpmMars = IsoPixmap(OroMars ,nlon,nlat,OroMarsStrip);
  3251. free(OroEarth);
  3252. free(OroMars);
  3253. /* Check for orography in run directory */
  3254. ftp = fopen(oro_name,"r");
  3255. if (ftp) fclose(ftp);
  3256. else strcpy(oro_name,"puma/dat/N064_surf_0129.sra");
  3257. // Orography
  3258. n = 0;
  3259. Frame[n].x = sfbox_x;
  3260. Frame[n].y = sfbox_y + FixFontHeight;
  3261. Frame[n].w = sfbox_w;
  3262. Frame[n].h = sfbox_h;
  3263. Frame[n].b = DarkGreen.pixel;
  3264. Frame[n].f = WhitePix;
  3265. strcpy(Frame[n].t[0],"Orography");
  3266. strcpy(Frame[n].t[1],"64 x 32");
  3267. strcpy(Frame[n].t[2],"");
  3268. Frame[n].Action = MarkRectangle;
  3269. Frame[n].xs = 110;
  3270. Frame[n].ys = 95;
  3271. Frame[n].ws = 60;
  3272. Frame[n].hs = 40;
  3273. SelAno->x = Frame[n].x;
  3274. SelAno->y = Frame[n].y + Frame[n].h + FixFontHeight;
  3275. SelAno->edco = 6;
  3276. SelAno->w = SelAno->edco * FixFontWidth + 2;
  3277. SelAno->xt = SelAno->x + (SelAno->edco + 1) * FixFontWidth;
  3278. SelAno->yt = SelAno->y + FixFont->ascent + 1;
  3279. strcpy(SelAno->teva," 0");
  3280. Button[4].y = SelAno->y - FixFontHeight/2; // Clear orography button
  3281. Button[4].x = Frame[n].x + Frame[n].w - 6 * FixFontWidth;
  3282. // T21 mode selector
  3283. ++n;
  3284. Frame[n].x = sfbox_x;
  3285. Frame[n].y = Frame[n-1].y + Frame[n-1].h + 4 * FixFontHeight;
  3286. moselw = sfbox_w;
  3287. if (SmallScreen)
  3288. {
  3289. moselw = WinYSize - Frame[n].y - FixFontHeight - FixFontHeight/2;
  3290. Frame[n].x += (sfbox_x - moselw) / 2;
  3291. }
  3292. Frame[n].w = moselw;
  3293. Frame[n].h = moselw;
  3294. Frame[n].b = BlackPix;
  3295. Frame[n].f = WhitePix;
  3296. strcpy(Frame[n].t[0],"Spherical Harmonics mode selector");
  3297. strcpy(Frame[n].t[1],"");
  3298. strcpy(Frame[n].t[2],"");
  3299. Frame[n].Action = ToggleMode;
  3300. sfbox_b = Frame[n].y + Frame[n].h - Frame[0].y + 2 * FixFontHeight + FixFontHeight/2;
  3301. Frames = n + 1;
  3302. }
  3303. void InitButtons(void)
  3304. {
  3305. int n;
  3306. bubox_w = 64;
  3307. bubox_h = 64;
  3308. bubox_x = WINDOW_WIDTH - 4 * 64 - 6 * FixFontWidth;
  3309. bubox_y = 8;
  3310. // Preprocess
  3311. n = 0;
  3312. Button[n].x = bubox_x;
  3313. Button[n].y = bubox_y;
  3314. Button[n].w = bubox_w;
  3315. Button[n].h = bubox_h;
  3316. Button[n].b = Yellow.pixel;
  3317. Button[n].f = BlackPix;
  3318. strcpy(Button[n].t[0],"Pre-");
  3319. strcpy(Button[n].t[1],"pro-");
  3320. strcpy(Button[n].t[2],"cess");
  3321. Button[n].Action = PreProcess;
  3322. // Save & Exit
  3323. ++n;
  3324. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3325. Button[n].y = Button[n-1].y;
  3326. Button[n].w = bubox_w;
  3327. Button[n].h = bubox_h;
  3328. Button[n].b = DarkGreen.pixel;
  3329. Button[n].f = WhitePix;
  3330. strcpy(Button[n].t[0],"Save");
  3331. strcpy(Button[n].t[1],"&");
  3332. strcpy(Button[n].t[2],"Exit");
  3333. Button[n].Action = SaveExit;
  3334. // Save & Run
  3335. ++n;
  3336. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3337. Button[n].y = Button[n-1].y;
  3338. Button[n].w = bubox_w;
  3339. Button[n].h = bubox_h;
  3340. Button[n].b = DarkBlue.pixel;
  3341. Button[n].f = WhitePix;
  3342. strcpy(Button[n].t[0],"Save");
  3343. strcpy(Button[n].t[1],"&");
  3344. strcpy(Button[n].t[2],"Run");
  3345. Button[n].Action = SaveRun;
  3346. // Abort
  3347. ++n;
  3348. Button[n].x = Button[n-1].x + Button[n-1].w + FixFontWidth;
  3349. Button[n].y = Button[n-1].y;
  3350. Button[n].w = bubox_w;
  3351. Button[n].h = bubox_h;
  3352. Button[n].b = Red.pixel;
  3353. Button[n].f = WhitePix;
  3354. strcpy(Button[n].t[1],"Abort");
  3355. Button[n].Action = Abort;
  3356. // Clear orography
  3357. ++n;
  3358. // Button[n].x = Button[n-1].x;
  3359. // Button[n].y = Button[n-1].y; // set in InitFrames
  3360. Button[n].w = 6 * FixFontWidth;
  3361. Button[n].h = 2 * FixFontHeight;
  3362. Button[n].b = DarkBlue.pixel;
  3363. Button[n].f = Yellow.pixel;
  3364. strcpy(Button[n].t[1],"Clear");
  3365. Button[n].Action = ClearOro;
  3366. }
  3367. /* ================== */
  3368. /* CreateModeSelector */
  3369. /* ================== */
  3370. void CreateModeSelector(int k)
  3371. {
  3372. int i,j,m,n;
  3373. int x,y,len,width,height;
  3374. int r,dx,dy,xp,yp;
  3375. char Text[20];
  3376. pix = Frame[k].pixmap = XCreatePixmap(display,Cow,Frame[k].w,Frame[k].h,ScreenD);
  3377. if (!Ampli) Ampli = malloc(DimSE * sizeof(int));
  3378. if (!ModeX) ModeX = malloc(DimSE * sizeof(int));
  3379. if (!ModeY) ModeY = malloc(DimSE * sizeof(int));
  3380. if (!ModeM) ModeM = malloc(DimSE * sizeof(int));
  3381. if (!ModeN) ModeN = malloc(DimSE * sizeof(int));
  3382. dxsh = Frame[k].w / (DimTr + 3);
  3383. dxs2 = dxsh / 2;
  3384. dx = dxsh;
  3385. r = (dx+dx) / 3;
  3386. ModeRadiusSq = r*r;
  3387. for (m=0,i=0 ; m <= DimTr ; ++m)
  3388. {
  3389. y = ModFontHeight + 4 + m * dx;
  3390. for (n=m ; n <= DimTr ; ++n,++i)
  3391. {
  3392. x = dx/2 + n * dx;
  3393. Ampli[i] = 1;
  3394. ModeX[i] = x + dx/2;
  3395. ModeY[i] = y + dx/2;
  3396. ModeM[i] = m;
  3397. ModeN[i] = n;
  3398. }
  3399. }
  3400. i = DimSH; // Button for all modes on
  3401. x = dx - ModFontWidth/2 + 1;
  3402. y = ModFontHeight + ModFont->descent + 2 + 20 * dx;
  3403. Ampli[i] = 0;
  3404. ModeX[i] = x + dx/2;
  3405. ModeY[i] = y + dx/2;
  3406. i++; // Button for all modes off
  3407. y += dx;
  3408. Ampli[i] = 0;
  3409. ModeX[i] = x + dx/2;
  3410. ModeY[i] = y + dx/2;
  3411. /* Draw mode legend */
  3412. dx = dxsh;
  3413. XSetForeground(display,gc,Red.pixel);
  3414. XSetBackground(display,gc,BlackPix);
  3415. for (i=0 ; i < 21 ; i+=2)
  3416. {
  3417. sprintf(Text,"%d",i);
  3418. len = strlen(Text);
  3419. width = XTextWidth(ModFont,Text,len);
  3420. height = ModFont->ascent + ModFont->descent;
  3421. xp = dx + i * dx - width/2 - ModFontWidth/2 + 1;
  3422. yp = ModFontHeight;
  3423. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3424. }
  3425. XSetForeground(display,gc,Blue.pixel);
  3426. for (i=0 ; i < 21 ; i+=2)
  3427. {
  3428. sprintf(Text,"%d",i);
  3429. len = strlen(Text);
  3430. width = XTextWidth(ModFont,Text,len);
  3431. height = ModFont->ascent + ModFont->descent;
  3432. xp = Frame[k].x + Frame[k].w - width - 2;
  3433. yp = 2 * ModFontHeight + i * dx;
  3434. XDrawImageString(display,pix,gc,xp,yp,Text,len);
  3435. }
  3436. }
  3437. void ShowButtons(void)
  3438. {
  3439. int i,k,l,x,y,d;
  3440. XSetFont(display, gc, FixFont->fid);
  3441. for (k=0 ; k < DIMBUTTON ; ++k)
  3442. {
  3443. if (k == 4 && SelAno->hide) continue;
  3444. d = Button[k].h / 4;
  3445. XSetForeground(display,gc,Button[k].b);
  3446. XFillRectangle(display,Cow,gc,Button[k].x,Button[k].y,Button[k].w,Button[k].h);
  3447. XSetForeground(display,gc,Button[k].f);
  3448. XSetBackground(display,gc,Button[k].b);
  3449. for (i=0 ; i < 3 ; ++i)
  3450. if (Button[k].t[i][0])
  3451. {
  3452. l = strlen(Button[k].t[i]);
  3453. x = Button[k].x + (Button[k].w - l * FixFontWidth) / 2;
  3454. y = Button[k].y + i * d + FixFont->ascent + d/2;
  3455. XDrawImageString(display,Cow,gc,x,y,Button[k].t[i],l);
  3456. }
  3457. }
  3458. }
  3459. int AllocateColorCells(struct ColorStrip cs[])
  3460. {
  3461. int i = 0;
  3462. XColor xcolor1,xcolor2;
  3463. while (cs[i].Name)
  3464. {
  3465. XAllocNamedColor(display,colormap,cs[i].Name,&xcolor1,&xcolor2);
  3466. cs[i].pixel = xcolor1.pixel;
  3467. ++i;
  3468. }
  3469. return i;
  3470. }
  3471. void InitColors(void)
  3472. {
  3473. /*
  3474. Visual *visual;
  3475. int count,st;
  3476. XStandardColormap *best_map_info;
  3477. XStandardColormap bmi[8];
  3478. visual = DefaultVisual(display,0);
  3479. memset(bmi,0,8*sizeof(XStandardColormap));
  3480. best_map_info = bmi;
  3481. count = 0;
  3482. st = XGetRGBColormaps(display,RootWindow(display,0),&best_map_info,&count,XA_RGB_DEFAULT_MAP);
  3483. {
  3484. printf("status = %d count=%d\n",st,count);
  3485. printf("colormap = %p\n",bmi[0].colormap);
  3486. printf("mapping = %x/%x/%x\n",bmi[0].red_max,bmi[0].green_max,bmi[0].blue_max);
  3487. }
  3488. */
  3489. // colormap = best_map_info->colormap;
  3490. AllocateColorCells(OroStrip);
  3491. AllocateColorCells(OroMarsStrip);
  3492. AllocateColorCells(GibbStrip);
  3493. AllocateColorCells(TStrip);
  3494. XAllocNamedColor(display,colormap,"red" ,&Red ,&Dummy);
  3495. XAllocNamedColor(display,colormap,"green" ,&Green ,&Dummy);
  3496. XAllocNamedColor(display,colormap,"blue" ,&Blue ,&Dummy);
  3497. XAllocNamedColor(display,colormap,"grey" ,&Grey ,&Dummy);
  3498. XAllocNamedColor(display,colormap,"hot pink" ,&LightRed ,&Dummy);
  3499. XAllocNamedColor(display,colormap,"dark red" ,&DarkRed ,&Dummy);
  3500. XAllocNamedColor(display,colormap,"light blue" ,&LightBlue ,&Dummy);
  3501. XAllocNamedColor(display,colormap,"dark blue" ,&DarkBlue ,&Dummy);
  3502. XAllocNamedColor(display,colormap,"light green",&LightGreen,&Dummy);
  3503. XAllocNamedColor(display,colormap,"dark green" ,&DarkGreen ,&Dummy);
  3504. XAllocNamedColor(display,colormap,"yellow" ,&Yellow ,&Dummy);
  3505. XAllocNamedColor(display,colormap,"cyan" ,&Cyan ,&Dummy);
  3506. TextC = Yellow.pixel;
  3507. HeadC = Cyan.pixel;
  3508. }
  3509. struct BMIstruct
  3510. {
  3511. int Size;
  3512. int Width;
  3513. int Height;
  3514. short Planes;
  3515. short Count;
  3516. int Compression;
  3517. int SizeImage;
  3518. int XPelsPerMeter;
  3519. int YPelsPerMeter;
  3520. int ClrUsed;
  3521. int ClrImportant;
  3522. };
  3523. // Convert an RGB value to an X11 Pixel
  3524. unsigned long create_pixel(long red, long green, long blue)
  3525. {
  3526. if (ScreenD == 24) // 24 bit true color
  3527. {
  3528. return blue | green << 8 | red << 16;
  3529. }
  3530. else if (ScreenD == 16) // 16 bit RGB 565
  3531. {
  3532. return blue >> 3 | (green >> 2) << 5 | (red >> 3) << 11;
  3533. }
  3534. else return 0;
  3535. }
  3536. void SwapIEEE16(char W[2])
  3537. {
  3538. char B;
  3539. B = W[0]; W[0] = W[1]; W[1] = B;
  3540. }
  3541. void SwapIEEE32(char W[4])
  3542. {
  3543. char B;
  3544. B = W[0]; W[0] = W[3]; W[3] = B;
  3545. B = W[1]; W[1] = W[2]; W[2] = B;
  3546. }
  3547. int ReadINT(FILE *fpi)
  3548. {
  3549. int k;
  3550. fread(&k,sizeof(k),1,fpi);
  3551. if (BigEndian) SwapIEEE32((char *)&k);
  3552. return k;
  3553. }
  3554. short ReadSHORT(FILE *fpi)
  3555. {
  3556. short k;
  3557. fread(&k,sizeof(k),1,fpi);
  3558. if (BigEndian) SwapIEEE16((char *)&k);
  3559. return k;
  3560. }
  3561. struct BMIstruct ImageBMI;
  3562. void ReadImage(struct MapImageStruct *ei, char *filename)
  3563. {
  3564. char ch;
  3565. int i,n,x,y,z;
  3566. long r,g,b;
  3567. int FileSize;
  3568. int Reserved;
  3569. int OffBits;
  3570. int PicBytes;
  3571. int ImgBytes;
  3572. int PadWidth;
  3573. int PadBytes;
  3574. int bpp;
  3575. int byr;
  3576. FILE *fp;
  3577. unsigned char *BuffImageData;
  3578. if (!(fp = fopen(filename,"r"))) return;
  3579. ch = fgetc(fp);
  3580. if (ch != 'B') return;
  3581. ch = fgetc(fp);
  3582. if (ch != 'M') return;
  3583. FileSize = ReadINT(fp);
  3584. Reserved = ReadINT(fp);
  3585. OffBits = ReadINT(fp);
  3586. if (Debug)
  3587. {
  3588. printf("Properties of %s:\n",filename);
  3589. printf("FileSize = %d\n",FileSize);
  3590. printf("FileOffset = %d\n",OffBits);
  3591. }
  3592. ImageBMI.Size = ReadINT(fp);
  3593. ImageBMI.Width = ReadINT(fp);
  3594. ImageBMI.Height = ReadINT(fp);
  3595. ImageBMI.Planes = ReadSHORT(fp);
  3596. ImageBMI.Count = ReadSHORT(fp);
  3597. ImageBMI.Compression = ReadINT(fp);
  3598. ImageBMI.SizeImage = ReadINT(fp);
  3599. ImageBMI.XPelsPerMeter = ReadINT(fp);
  3600. ImageBMI.YPelsPerMeter = ReadINT(fp);
  3601. ImageBMI.ClrUsed = ReadINT(fp);
  3602. ImageBMI.ClrImportant = ReadINT(fp);
  3603. PadWidth = (ImageBMI.Width + 7) & 0xFFF8;
  3604. bpp = ImageBMI.Count >> 3;
  3605. PadBytes = (4 - ((ImageBMI.Width * bpp) % 4)) % 4 ;
  3606. if (Debug)
  3607. {
  3608. printf("BMI Size = %d\n",ImageBMI.Size);
  3609. printf("BMI Width = %d\n",ImageBMI.Width);
  3610. printf("BMI Height = %d\n",ImageBMI.Height);
  3611. printf("BMI Planes = %d\n",ImageBMI.Planes);
  3612. printf("BMI Count = %d\n",ImageBMI.Count);
  3613. printf("BMI ClrUsed = %d\n",ImageBMI.ClrUsed);
  3614. printf("BMI ClrImpo = %d\n",ImageBMI.ClrImportant);
  3615. printf("Pad Bytes = %d\n",PadBytes);
  3616. printf("Pad Width = %d\n",PadWidth);
  3617. }
  3618. PicBytes = (bpp * ImageBMI.Width + PadBytes) * ImageBMI.Height;
  3619. ImgBytes = 4 * PadWidth * ImageBMI.Height;
  3620. ei->d = calloc(ImgBytes,1);
  3621. BuffImageData = malloc(PicBytes);
  3622. fseek(fp,OffBits,SEEK_SET);
  3623. n = fread(BuffImageData,1,PicBytes,fp);
  3624. fclose(fp);
  3625. if (Debug)
  3626. {
  3627. printf("Size Bytes = %d\n",PicBytes);
  3628. printf("Read Bytes = %d\n",n);
  3629. }
  3630. ei->X = XCreateImage(display,CopyFromParent,ScreenD,ZPixmap,0,
  3631. ei->d,PadWidth,ImageBMI.Height,8,0);
  3632. for (y = 0 ; y < ImageBMI.Height ; ++y)
  3633. {
  3634. for (x = 0 ; x < ImageBMI.Width ; ++x)
  3635. {
  3636. i = bpp * x + (ImageBMI.Height - 1 - y) * (ImageBMI.Width*bpp+PadBytes);
  3637. b = BuffImageData[i ];
  3638. g = BuffImageData[i+1];
  3639. r = BuffImageData[i+2];
  3640. XPutPixel(ei->X, x, y, create_pixel(r,g,b));
  3641. }
  3642. }
  3643. free(BuffImageData);
  3644. ei->w = ImageBMI.Width;
  3645. ei->h = ImageBMI.Height;
  3646. }
  3647. void ReadLogo(int logo, char *filename)
  3648. {
  3649. struct MapImageStruct MapI;
  3650. ReadImage(&MapI,filename);
  3651. Logo[logo].w = MapI.w;
  3652. Logo[logo].h = MapI.h;
  3653. Logo[logo].X = MapI.X;
  3654. if (logo)
  3655. {
  3656. Logo[logo].x = Logo[logo-1].x + Logo[logo-1].w;
  3657. Logo[logo].y = Logo[logo-1].y;
  3658. }
  3659. }
  3660. void ShowCopyright(void)
  3661. {
  3662. int x,y;
  3663. x = CowSizeHints.min_width - 18.5 * ModFontWidth;
  3664. y = CowSizeHints.min_height - ModFontHeight/2;
  3665. XSetFont(display, gc, ModFont->fid);
  3666. XSetForeground(display,gc,Grey.pixel);
  3667. XSetBackground(display,gc,BlackPix);
  3668. XDrawImageString(display,Cow,gc,x,y,"Image Credit: NASA",18);
  3669. }
  3670. int redaco;
  3671. int RedrawControlWindow(void)
  3672. {
  3673. int i,l,x,y;
  3674. double xrf,yrf;
  3675. struct SelStruct *Sel;
  3676. XWindowAttributes CurAtt;
  3677. if (Debug) printf("Redraw %d\n",redaco++);
  3678. XGetWindowAttributes(display,Cow,&CurAtt);
  3679. WinXSize = CurAtt.width;
  3680. WinYSize = CurAtt.height;
  3681. XSetWindowBackground(display,Cow,WinBG);
  3682. XClearWindow(display,Cow);
  3683. // Paint some stars on the sky
  3684. xrf = (double)WinXSize / pow(2.0,31),
  3685. yrf = (double)WinYSize / pow(2.0,31);
  3686. XSetForeground(display,gc,WhitePix);
  3687. srandom(Seed);
  3688. for (y = 0 ; y < ((WinXSize * WinYSize) >> 8) ; ++y)
  3689. XDrawPoint(display,Cow,gc,xrf*random(),yrf*random());
  3690. XSetForeground(display,gc,BlackPix);
  3691. XSetBackground(display,gc,WhitePix);
  3692. for (Sel = &SelStart ; Sel ; Sel = Sel->Next) ShowSelection(Sel);
  3693. if (CursorSel) CursorOn();
  3694. ShowOrography();
  3695. ShowFrame1();
  3696. ShowButtons();
  3697. for (l=0 ; l < Logos ; ++l)
  3698. {
  3699. XPutImage(display,Cow,gc,Logo[l].X,0,0,Logo[l].x,Logo[l].y,Logo[l].w,Logo[l].h);
  3700. }
  3701. if (Model == PLASIM) ShowCopyright();
  3702. XSync(display,0);
  3703. return 0;
  3704. }
  3705. void ChangeResolution(int NewRes)
  3706. {
  3707. int i;
  3708. struct SelStruct *Sel;
  3709. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3710. {
  3711. if (i == NewRes) Sel->iv = 1;
  3712. else Sel->iv = 0;
  3713. }
  3714. Resolution = NewRes;
  3715. }
  3716. void UpdateResolution(void)
  3717. {
  3718. int i;
  3719. struct SelStruct *Sel;
  3720. if (SelRes)
  3721. for (i=RES_T21 , Sel = SelRes ; i <= RES_T42; ++i , Sel = Sel->Next)
  3722. {
  3723. if (Sel && Sel->iv == 1) Resolution = i;
  3724. }
  3725. }
  3726. char *vcn[6] =
  3727. {
  3728. "StaticGray",
  3729. "GrayScale",
  3730. "StaticColor",
  3731. "PseudoColor",
  3732. "TrueColor",
  3733. "DirectColor"
  3734. };
  3735. void InitGUI(void)
  3736. {
  3737. int argc = 1;
  3738. int i,j,k;
  3739. char *WinconTitle1 = {"MoSt - Model Suite (17) - University of Hamburg"};
  3740. unsigned long valuemask = 0; /* ignore XGCvalues and use defaults */
  3741. FILE *ftp;
  3742. FILE *xpp;
  3743. XVisualInfo vinfo;
  3744. XGCValues values;
  3745. XEvent Event;
  3746. XWindowAttributes CurAtt;
  3747. if ((display=XOpenDisplay(display_name)) == NULL)
  3748. {
  3749. fprintf(stderr,"%s: cannot connect to X server %s\n",
  3750. progname, XDisplayName(display_name));
  3751. exit(1);
  3752. }
  3753. ScreenN = DefaultScreen(display);
  3754. ScreenD = XDefaultDepth(display,ScreenN);
  3755. ScreenW = DisplayWidth (display,ScreenN);
  3756. ScreenH = DisplayHeight(display,ScreenN);
  3757. if (ScreenHeight) ScreenH = ScreenHeight;
  3758. if (Debug)
  3759. {
  3760. i=5;
  3761. while (!XMatchVisualInfo(display,ScreenN,ScreenD,i--,&vinfo));
  3762. printf("Found visual %s at depth %d\n",vcn[++i],ScreenD);
  3763. printf("Red mask = %8lx\n",vinfo.red_mask);
  3764. printf("Green mask = %8lx\n",vinfo.green_mask);
  3765. printf("Blue mask = %8lx\n",vinfo.blue_mask);
  3766. }
  3767. BlackPix = BlackPixel(display,ScreenN);
  3768. WhitePix = WhitePixel(display,ScreenN);
  3769. SmallScreen = ScreenH < 768;
  3770. wm_hints.initial_state = NormalState;
  3771. wm_hints.input = True;
  3772. wm_hints.flags = StateHint | InputHint;
  3773. class_hints.res_name = progname;
  3774. class_hints.res_class = "MOST";
  3775. Delwin = XInternAtom(display,"WM_DELETE_WINDOW",0);
  3776. LoadFonts();
  3777. /* Setup control window */
  3778. CowSizeHints.flags = PPosition | PSize | PMinSize | PMaxSize;
  3779. CowSizeHints.min_width = MIN(WINDOW_WIDTH,ScreenW);
  3780. CowSizeHints.min_height = MIN(740,ScreenH);
  3781. CowSizeHints.max_width = CowSizeHints.min_width;
  3782. CowSizeHints.max_height = CowSizeHints.min_height;
  3783. Cow = XCreateSimpleWindow(display,RootWindow(display,ScreenN),
  3784. 0,0,
  3785. CowSizeHints.max_width,CowSizeHints.max_height,
  3786. 4,BlackPix,WhitePix);
  3787. XStringListToTextProperty(&WinconTitle1,1,&WinconName1);
  3788. XSetWMProtocols(display,Cow,&Delwin,1);
  3789. XSetWMProperties(display,Cow,&WinconName1,NULL,
  3790. NULL,0,&CowSizeHints,&wm_hints,&class_hints);
  3791. XSelectInput(display,Cow,ButtonPressMask | ButtonReleaseMask |
  3792. PointerMotionMask | KeyPressMask | ExposureMask);
  3793. XMapWindow(display,Cow);
  3794. XGetWindowAttributes(display,Cow,&CurAtt);
  3795. WinXSize = CurAtt.width;
  3796. WinYSize = CurAtt.height;
  3797. /* Prepare GC */
  3798. gc = XCreateGC(display, Cow, valuemask, &values);
  3799. XSetFont(display, gc, FixFont->fid);
  3800. colormap = XDefaultColormap(display,ScreenN);
  3801. XSetForeground(display,gc,BlackPix);
  3802. XSetBackground(display,gc,WhitePix);
  3803. Mok = XGetModifierMapping(display);
  3804. InitColors();
  3805. /* Look for Num_Lock key */
  3806. for (j=0 ; j < 8 ; ++j)
  3807. {
  3808. k = Mok->modifiermap[j * Mok->max_keypermod];
  3809. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Num_Lock ) NumLockMask = 1 << j;
  3810. if (XkbKeycodeToKeysym(display,k,0,0) == XK_Mode_switch) ModeSwitchMask = 1 << j;
  3811. }
  3812. if (Debug) printf("NumLockMask = %02X\n",NumLockMask );
  3813. if (Debug) printf("ModeSwitchMask = %02X\n",ModeSwitchMask);
  3814. if (Debug)
  3815. for (j=0 ; j < 8 ; ++j)
  3816. {
  3817. printf("Mok [%d] %d:",Mok->max_keypermod,j);
  3818. for (i=0 ; i < Mok->max_keypermod ; ++i)
  3819. {
  3820. k = Mok->modifiermap[i+j*Mok->max_keypermod];
  3821. if (XkbKeycodeToKeysym(display,k,0,0) != NoSymbol)
  3822. printf(" %4x %-16.16s ",k,XKeysymToString(XkbKeycodeToKeysym(display,k,0,0)));
  3823. }
  3824. printf("\n");
  3825. }
  3826. XDisplayKeycodes(display,&EdiFirstKey,&EdiLastKey);
  3827. EdiKeymap = XGetKeyboardMapping(display,EdiFirstKey,EdiLastKey-EdiFirstKey+1,&EdiSymsPerKey);
  3828. /* Sun keymaps don't have lower case entries */
  3829. for (j=EdiFirstKey ; j <= EdiLastKey ; ++j)
  3830. {
  3831. k = (j - EdiFirstKey) * EdiSymsPerKey;
  3832. if (EdiKeymap[k] >= 'A' && EdiKeymap[k] < 0xE0 && EdiKeymap[k+1] == 0)
  3833. {
  3834. EdiKeymap[k+1] = EdiKeymap[k];
  3835. EdiKeymap[k] += 0x20;
  3836. }
  3837. }
  3838. //FL0318 xpp = fopen("Beginner","r"); // Expert mode ?
  3839. xpp = fopen("Expert","r"); // Expert mode ?
  3840. if (xpp)
  3841. {
  3842. //FL0318 Expert = 0;
  3843. Expert = 1;
  3844. fclose(xpp);
  3845. }
  3846. xpp = fopen("cat","r"); // Cat enabled
  3847. if (xpp)
  3848. {
  3849. CatEnabled = 1;
  3850. fclose(xpp);
  3851. }
  3852. xpp = fopen("sam","r"); // Sam enabled
  3853. if (xpp)
  3854. {
  3855. SamEnabled = 1;
  3856. fclose(xpp);
  3857. }
  3858. xpp = fopen("lsg/src/lsgmod.f90","r"); // LSG there ?
  3859. //FL0318 if (xpp)
  3860. if (xpp && Expert)
  3861. {
  3862. LsgEnabled = 1;
  3863. fclose(xpp);
  3864. }
  3865. xpp = fopen("puma/src/mpimod_multi.f90","r"); // Multirun module there ?
  3866. if (xpp)
  3867. {
  3868. MultirunEnabled = 1;
  3869. fclose(xpp);
  3870. }
  3871. // Read name of MPI execute command
  3872. xpp = fopen("most_compiler_mpi","r"); // MPI installed ?
  3873. if (xpp)
  3874. {
  3875. fgets(Buffer,LINEMAX,xpp);
  3876. if (Buffer[strlen(Buffer)-1] == 10) Buffer[strlen(Buffer)-1] = 0;
  3877. if (Buffer[strlen(Buffer)-1] == 13) Buffer[strlen(Buffer)-1] = 0;
  3878. if (!strncmp(Buffer,"MPI_RUN=",8)) strcpy(mpirun,Buffer+8);
  3879. fclose(xpp);
  3880. }
  3881. InitLogo();
  3882. ReadLogo(0,"images/KC-Logo_RGB.bmp");
  3883. ReadLogo(1,"images/puma.bmp");
  3884. ReadLogo(2,"images/cat.bmp");
  3885. ReadLogo(3,"images/plasim.bmp");
  3886. ReadImage(&MapHRE,"images/earth.bmp");
  3887. ReadImage(&MapHRM,"images/mars.bmp");
  3888. ReadImage(&MapLRK,"images/Kepler-16.bmp");
  3889. ReadImage(&MapLRL,"images/habit360x180.bmp");
  3890. opbox_y = 8 + 64 + 2 * FixFontHeight;
  3891. InitSelections();
  3892. InitNamelist();
  3893. ChangeModel(PLASIM);
  3894. NamelistSelector(PLASIM);
  3895. ChangeModel(CAT);
  3896. NamelistSelector(CAT);
  3897. ChangeModel(SAM);
  3898. NamelistSelector(SAM);
  3899. ChangeModel(PUMA);
  3900. NamelistSelector(PUMA);
  3901. if (ReadSettings(cfg_file))
  3902. {
  3903. UpdateResolution();
  3904. UpdateSelections(&SelStart);
  3905. }
  3906. InitFrames();
  3907. CalcFrame(nlat);
  3908. CreateModeSelector(1);
  3909. InitButtons();
  3910. MapLRE.w = Frame[1].w;
  3911. MapLRE.h = Frame[1].h;
  3912. MapLRK.w = Frame[1].w;
  3913. MapLRK.h = Frame[1].h;
  3914. MapLRM.w = Frame[1].w;
  3915. MapLRM.h = Frame[1].h;
  3916. MapLRL.w = Frame[0].w;
  3917. MapLRL.h = Frame[0].h;
  3918. MapLRM.l = -90;
  3919. AzimuthalImage(&MapHRE,&MapLRE);
  3920. AzimuthalImage(&MapHRM,&MapLRM);
  3921. RedrawControlWindow();
  3922. }
  3923. int HitBox(struct SelStruct *Sel)
  3924. {
  3925. return (
  3926. (WinEvent.xbutton.button == Button1) &&
  3927. (WinEvent.xbutton.x >= Sel->x) &&
  3928. (WinEvent.xbutton.x < Sel->x + Sel->w) &&
  3929. (WinEvent.xbutton.y >= Sel->y) &&
  3930. (WinEvent.xbutton.y < Sel->y + Sel->h));
  3931. }
  3932. int HitButton(int k)
  3933. {
  3934. return (
  3935. (WinEvent.xbutton.button == Button1) &&
  3936. (WinEvent.xbutton.x >= Button[k].x) &&
  3937. (WinEvent.xbutton.x < Button[k].x + Button[k].w) &&
  3938. (WinEvent.xbutton.y >= Button[k].y) &&
  3939. (WinEvent.xbutton.y < Button[k].y + Button[k].h));
  3940. }
  3941. int HitFrame(int k)
  3942. {
  3943. return (
  3944. (WinEvent.xbutton.x >= Frame[k].x) &&
  3945. (WinEvent.xbutton.x < Frame[k].x + Frame[k].w) &&
  3946. (WinEvent.xbutton.y >= Frame[k].y) &&
  3947. (WinEvent.xbutton.y < Frame[k].y + Frame[k].h));
  3948. }
  3949. int OnFrame(int i)
  3950. {
  3951. if (HitFrame(i))
  3952. {
  3953. FrameNo = i;
  3954. Frame[i].Action();
  3955. return 1;
  3956. }
  3957. return 0;
  3958. }
  3959. void OnMouseClick(void)
  3960. {
  3961. int i,j,cp;
  3962. struct SelStruct *Sel;
  3963. /* Check Action Buttons */
  3964. for (i=0 ; i < DIMBUTTON ; ++i)
  3965. {
  3966. if (HitButton(i))
  3967. {
  3968. Button[i].Action();
  3969. return;
  3970. }
  3971. }
  3972. /* Check for model switch */
  3973. for (i = PUMA , Sel = SelMod ; i < MODELS ; ++i , Sel = Sel->Next)
  3974. {
  3975. if (HitBox(Sel) && Model != i)
  3976. {
  3977. if (i == CAT && !CatEnabled) return; // Hide CAT
  3978. if (Debug) printf("Change model from %d to %d\n",Model,i);
  3979. ChangeModel(i);
  3980. CalcFrame(Latitudes);
  3981. return;
  3982. }
  3983. }
  3984. /* Continue with all other boxes */
  3985. for ( ; Sel ; Sel = Sel->Next)
  3986. {
  3987. if (Sel->type == SEL_CHECK && HitBox(Sel) && !Sel->no)
  3988. {
  3989. cp = -1;
  3990. for (i=0 ; i < PLANETS ; ++i) if (Sel == SelPlanet[i]) cp = i;
  3991. if (cp >= 0 && Planet != cp) ChangePlanet(cp);
  3992. else
  3993. {
  3994. Sel->iv = !Sel->iv;
  3995. if (Sel->piv) *Sel->piv = Sel->iv;
  3996. }
  3997. }
  3998. else if ((Sel->type == SEL_INT || Sel->type == SEL_REAL) && HitBox(Sel))
  3999. {
  4000. // printf("Target is <%s>\n",Sel->text);
  4001. if (Sel != CursorSel) FinishLine();
  4002. CursorSel = Sel;
  4003. CursorCol = (WinEvent.xbutton.x - Sel->x) / FixFontWidth;
  4004. if (CursorCol > Sel->edco-1) CursorCol = Sel->edco-1;
  4005. CursorOn();
  4006. }
  4007. }
  4008. /* Allow only one horizontal resolution */
  4009. if (SelRes)
  4010. {
  4011. for (i = RES_T21 , Sel = SelRes ; i <= RES_T42 ; ++i , Sel = Sel->Next)
  4012. if (HitBox(Sel))
  4013. {
  4014. ChangeResolution(i);
  4015. return;
  4016. }
  4017. }
  4018. }
  4019. int EdiDecodeKey(void)
  4020. {
  4021. int ShiftStatus;
  4022. int ShiftIndex;
  4023. int KeyCode;
  4024. int KeyIndex;
  4025. ShiftStatus = WinEvent.xkey.state;
  4026. KeyIndex = (WinEvent.xkey.keycode-EdiFirstKey) * EdiSymsPerKey;
  4027. KeyCode = EdiKeymap[KeyIndex]; // Code with no modifiers
  4028. if (ShiftStatus & 0x04) // Control
  4029. {
  4030. if (KeyCode >= 0x40 && KeyCode < 0x80) KeyCode &= 0x1F;
  4031. }
  4032. else if (ShiftStatus & NumLockMask &&
  4033. EdiKeymap[KeyIndex+1] >= XK_KP_Separator && // Numlock
  4034. EdiKeymap[KeyIndex+1] <= XK_KP_9) // PC Keypad
  4035. KeyCode = EdiKeymap[KeyIndex+1] - XK_KP_Space;
  4036. else if (ShiftStatus & NumLockMask &&
  4037. EdiKeymap[KeyIndex+2] >= XK_KP_Separator && // Numlock
  4038. EdiKeymap[KeyIndex+2] <= XK_KP_9) // SUN Keypad
  4039. KeyCode = EdiKeymap[KeyIndex+2] - XK_KP_Space;
  4040. else
  4041. {
  4042. if (ShiftStatus & ModeSwitchMask) ShiftIndex = 2; // Alt Gr
  4043. else ShiftIndex = ShiftStatus & 1; // Normal & Shift
  4044. if (ShiftIndex == 1 && KeyCode >= XK_F1 && KeyCode <= XK_F12) KeyCode += 12;
  4045. else KeyCode = EdiKeymap[KeyIndex + ShiftIndex];
  4046. }
  4047. if (ShiftStatus & 0x02) // Caps Lock
  4048. {
  4049. if (KeyCode >= 'a' && KeyCode <= 'z' ) KeyCode -= 0x20; // ASCII
  4050. if (KeyCode >= 0xE0 && KeyCode <= 0xFD) KeyCode -= 0x20; // Latin-1
  4051. }
  4052. if (KeyCode == XK_KP_Left ) KeyCode = XK_Left;
  4053. if (KeyCode == XK_KP_Right ) KeyCode = XK_Right;
  4054. if (KeyCode == XK_KP_Up ) KeyCode = XK_Up;
  4055. if (KeyCode == XK_KP_Down ) KeyCode = XK_Down;
  4056. if (KeyCode == XK_KP_Home ) KeyCode = XK_Home;
  4057. if (KeyCode == XK_KP_End ) KeyCode = XK_End ;
  4058. if (KeyCode == XK_KP_Page_Up ) KeyCode = XK_Page_Up;
  4059. if (KeyCode == XK_KP_Page_Down ) KeyCode = XK_Page_Down;
  4060. if (KeyCode == XK_KP_Enter ) KeyCode = XK_Return;
  4061. if (KeyCode == XK_KP_Add ) KeyCode = '+';
  4062. if (KeyCode == XK_KP_Subtract ) KeyCode = '-';
  4063. if (KeyCode == XK_KP_Multiply ) KeyCode = '*';
  4064. if (KeyCode == XK_KP_Divide ) KeyCode = '/';
  4065. if (Debug)
  4066. {
  4067. if (KeyCode >= ' ' && KeyCode <= 255) printf("'%c' ",KeyCode);
  4068. else printf("--- ");
  4069. printf("Key [%x] State [%2x]\n",WinEvent.xkey.keycode,WinEvent.xkey.state);
  4070. for (ShiftIndex = 0 ; ShiftIndex < EdiSymsPerKey ; ++ShiftIndex)
  4071. {
  4072. if (EdiKeymap[KeyIndex + ShiftIndex] && XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]))
  4073. {
  4074. printf(" %d: <%x> '%c' (%s)\n",
  4075. ShiftIndex,(unsigned int)EdiKeymap[KeyIndex + ShiftIndex],
  4076. (int)EdiKeymap[KeyIndex + ShiftIndex],
  4077. XKeysymToString(EdiKeymap[KeyIndex + ShiftIndex]));
  4078. }
  4079. }
  4080. printf("\n");
  4081. }
  4082. return KeyCode;
  4083. }
  4084. void EditUpLine(void)
  4085. {
  4086. FinishLine();
  4087. if (CursorSel == NULL || CursorSel->Prev == NULL) return;
  4088. if (CursorSel->Prev->type != SEL_INT && CursorSel->Prev->type != SEL_REAL) return;
  4089. CursorSel = CursorSel->Prev;
  4090. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4091. }
  4092. void EditDownLine(void)
  4093. {
  4094. FinishLine();
  4095. if (CursorSel == NULL || CursorSel->Next == NULL) return;
  4096. if (CursorSel->Next->type != SEL_INT && CursorSel->Next->type != SEL_REAL) return;
  4097. CursorSel = CursorSel->Next;
  4098. if (CursorCol > CursorSel->edco-1) CursorCol = CursorSel->edco-1;
  4099. }
  4100. void EditReturnLine(void)
  4101. {
  4102. CursorCol = 0;
  4103. EditDownLine();
  4104. }
  4105. void EditLeftChar(void)
  4106. {
  4107. if (CursorCol > 0) --CursorCol;
  4108. }
  4109. void EditRightChar(void)
  4110. {
  4111. if (CursorCol < CursorSel->edco-1) ++CursorCol;
  4112. }
  4113. void EmitChar(int ch)
  4114. {
  4115. if (CursorSel)
  4116. {
  4117. CursorSel->teva[CursorCol] = ch;
  4118. EditRightChar();
  4119. }
  4120. } /* EmitChar */
  4121. void DeleteRightChar(void)
  4122. {
  4123. int i;
  4124. for (i=CursorCol ; i < CursorSel->edco-1 ; ++i)
  4125. CursorSel->teva[i] = CursorSel->teva[i+1];
  4126. CursorSel->teva[CursorSel->edco-1] = ' ';
  4127. }
  4128. void DeleteLeftChar(void)
  4129. {
  4130. if (CursorCol == 0) return;
  4131. EditLeftChar();
  4132. DeleteRightChar();
  4133. }
  4134. void InsertBlank(void)
  4135. {
  4136. int i;
  4137. for (i = CursorSel->edco-1 ; i > CursorCol ; --i)
  4138. CursorSel->teva[i] = CursorSel->teva[i-1];
  4139. CursorSel->teva[CursorCol] = ' ';
  4140. }
  4141. void ProcChar(int Key)
  4142. {
  4143. if (CursorSel)
  4144. switch (Key)
  4145. {
  4146. case XK_Up : EditUpLine(); break;
  4147. case XK_Left : EditLeftChar(); break;
  4148. case XK_Right : EditRightChar(); break;
  4149. case XK_Down : EditDownLine(); break;
  4150. case XK_Return : EditReturnLine(); break;
  4151. case XK_BackSpace: DeleteLeftChar(); break;
  4152. case XK_Delete : DeleteRightChar(); break;
  4153. case XK_Insert : InsertBlank(); break;
  4154. }
  4155. }
  4156. void LoopGUI(void)
  4157. {
  4158. int w;
  4159. while (1)
  4160. {
  4161. XNextEvent(display,&WinEvent);
  4162. switch (WinEvent.type)
  4163. {
  4164. case ClientMessage: return;
  4165. case MotionNotify : if (OnFrame(0)) ShowOrography();
  4166. if (Model == PUMA && OnFrame(1)) ShowModeSelector();
  4167. break;
  4168. case ButtonRelease: Button1Down = 0; if (OnFrame(0)) ShowOrography(); break;
  4169. case ButtonPress : Button1Down = 1;
  4170. if (!OnFrame(0) && !OnFrame(1)) OnMouseClick();
  4171. case Expose : RedrawControlWindow(); break;
  4172. case KeyPress : EdiKeyCode = EdiDecodeKey();
  4173. if (EdiKeyCode >= ' ' && EdiKeyCode < 256) EmitChar(EdiKeyCode);
  4174. else ProcChar(EdiKeyCode);
  4175. RedrawControlWindow();
  4176. break;
  4177. }
  4178. }
  4179. }
  4180. int CheckEndianess(void)
  4181. {
  4182. union EndianCheck
  4183. {
  4184. char b[sizeof(int)];
  4185. int i;
  4186. } ec;
  4187. ec.i = 8;
  4188. return (ec.b[0] == 0);
  4189. }
  4190. int main(int argc, char *argv[])
  4191. {
  4192. int ia;
  4193. for (ia = 1 ; ia < argc ; ++ia)
  4194. {
  4195. if (!strcmp(argv[ia],"-d")) Debug = 1;
  4196. else if (!strcmp(argv[ia],"-i")) cfg_file[0] = 0;
  4197. else if (!strcmp(argv[ia],"-h")) ScreenHeight = atoi(argv[++ia]);
  4198. else strncpy(cfg_file,argv[1],sizeof(cfg_file));
  4199. }
  4200. CurrentDate = time(NULL);
  4201. BigEndian = CheckEndianess();
  4202. InitGUI();
  4203. LoopGUI();
  4204. return 0;
  4205. }