most.c 123 KB

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